var jc = undefined;
var videoplayer = undefined;

function init_carousel(carousel, state) {
  if (state == 'init') {
    jc = carousel;
  }
};

function loadCarousel(video_url) {
  if ( jc == undefined ) return;
  
  var videos;
  $.ajax({
    type: "GET",
    url: video_url,
    dataType: "script",
    success: function (data, textStatus) {
      jc.reset();
      videos = eval(data);
      var html = '';
      for ( var i = 0 ; i < videos.length ; ++i ) {
        html = '<a href="'+ videos[i].link +'"><img src="'+ videos[i].image +'" alt="'+ videos[i].title +'" /><br /><span>'+ videos[i].description +'</span></a>';
        jc.add(i+1, html);
      }
      jc.size(videos.length);
      $('#videoplayer-nowplaying span').text($('#videoplayer-playlist a:first img').attr('alt'));
      $('#videoplayer-playlist ul.jcarousel-skin-vintage span').truncate( 40, {
        chars: /./
      });
      
      if ( videoplayer != undefined ) {
        videoplayer.sendEvent("LOAD", $('#videoplayer-playlist li a:first').attr('href'));
        
        $('#videoplayer-playlist li a').click(function () {
          $('#videoplayer-nowplaying span').text($('img', this).attr('alt'));
          videoplayer.sendEvent("LOAD", $(this).attr('href'));
          videoplayer.sendEvent("PLAY","true");
          return false;
        });
      }
    }
  });
}

function doPollPopup(target,pageLoad) {
  var _left = (screen.width - 350) / 2;
  var _top = (screen.height - 250) / 2;
  var _winops = "width=350, height=250, resizable=0, top="+_top+", left="+_left;
  var win = window.open(pageLoad, target, _winops);
  win.focus()
}

function playerReady(obj) {
  if ( obj['id'] == 'videoplayer' ) {
    videoplayer = document.getElementById('videoplayer');
    loadCarousel("video_featured.asp");
    $('#videoplayer-categories select').change(function() {
      loadCarousel($(this).val() + ".asp");
    });
  }
};

jQuery(document).ready(function() {
  if ( $('#videoplayer').length ) {
    
    jQuery('#videoplayer-playlist ul.jcarousel-skin-vintage').jcarousel({
      scroll: 6,
      initCallback: init_carousel
    });
    
    var player_vars = {
        autostart: 'false',
        backcolor: '#282828',
        frontcolor: '#999999',
        lightcolor: '#CC9900',
        //file: $('#videoplayer-playlist a:first').attr('href'),
        file: '/v2/content/preview3.jpg',
        image: '/v2/content/preview3.jpg',
        height: '344',
        type: 'video',
        width: '576',
        id: 'videoplayer'
      };
      var player_params = {
        bgcolor: '#222222',
        allowfullscreen: 'true',
        allowscriptaccess: 'always',
        id: 'videoplayer',
        name: 'videoplayer'
      };
      swfobject.embedSWF("/v2/content/player.swf", 'videoplayer', "576", "344", "9.0.0", "expressInstall.swf", player_vars, player_params);

  }
  
  if ( $('.inside-item-thumbnail-swf').length ) {
    $('.inside-item-thumbnail-swf').each(function () {
      swfobject.embedSWF($('.inside-item-thumbnail-swf').attr('title'), $('.inside-item-thumbnail-swf').attr('id'), "64", "54", "8.0.0");
    });
  }
  if ( $('#calendarjump').length ) {
    $('#calendarjump').change(function () {
      window.location = $('#calendarjump').val();
    });
  }
  if ( $('.flvvideo').length ) {
      $('.flvvideo').each(function () {
        var data = $(this).attr('title').split('|');

        var player_vars = {
          autostart: 'false',
          backcolor: '#0d0d0d',
          frontcolor: '#cccccc',
          lightcolor: '#ffffff',
          type: 'video',
          file: data[0],
          image: $('img', this).attr('src'),
          id: $(this).attr('id')
        };
        var player_params = {
          bgcolor: '#000000',
          allowfullscreen: 'true',
          allowscriptaccess: 'always',
          id: player_vars.id,
          name: player_vars.id
        };
        var player_attr = {
          'class': 'flvvideo'
        };
        swfobject.embedSWF('/v2/content/player.swf', player_vars.id, data[1], data[2], '9.0.0', 'expressInstall.swf', player_vars, player_params, player_attr);
      });

  }
  
  
});

