// to aviod $ conflicts with prototype:
// jq replaces $ in jQuery.noConflict();
// var jq = jQuery.noConflict();
// I put it at the bottom of the jquery liabrary.
jq(function() {
  jq('#video_data').remove();
	
	// initialize videos
	jq('#video_panel [id^=video]').each(function(i) { initVideo($video_data[i], i, bind_video_data('video_' + $video_data[i].id, location.hash.match('#video') && i == 0)) });
	
	// set video slide button callbacks
	jq('#video_previous').click(function() {
		vid_previous();
		return false;
	});
	jq('#video_next').click(function(){
		vid_next();
		return false;
	});
	
	// set link: toggle & track_view
  jq('#video_toggle').click(function() {
    var video = jq('#video_panel').children(':first');
		
		vid_toggle();
    update_video_views(video);
  });
});

// sets callback for swfObject
function bind_video_data(vid, incr_view) {
	return function() {
		var loaded_video = jq('#' + vid),
				container = jq('#video_panel');
		container.data(vid, { viewed: false, played: false });
		
		if ( incr_view ) {
	    vid_toggle();
	    update_video_views(loaded_video);
		}
	}
}

function playerReady(playa) {
  var player = jq('#' + playa.id);
  
  playa = player.get(0);
  playa.addViewListener('PLAY', 'update_video_plays');
}

function update_video_plays(video) {
  var playa = jq('#' + video.id).get(0),
      loc   = start_path() + 'update_video_plays/' + video.id.replace('video_', '');
      
  jq.get(loc);
  playa.removeViewListener('PLAY', 'update_video_plays');
	playa.addViewListener('PLAY', 'press_play');
  press_play(playa);
}

function press_play(video){
	var container = jq('#video_panel');
	container.data(video.id, { viewed: true, played: !container.data(video.id).played });
}

function update_video_views(video) {
	var container = jq('#video_panel');
  if ( !container.data(video.attr('id')).viewed ) {
    var loc = start_path() + 'update_video_views/' + video.attr('id').replace('video_', '');
    
    jq.get(loc);
		container.data(video.attr('id'), { viewed: true, played: false });
  }
}

function vid_previous() {
	var collection      = jq('#video_panel'),
			video_detail		= jq('#video_detail_contain div[id^=video_details]:visible'),
	    i               = parseInt(video_detail.attr('id').replace('video_details_', '')),
	    collection_size = collection.find('object').length,
	    videos          = collection.children();
	
	if ( i == 1 )
		var ml = -320 * (collection_size - 1), new_i = collection_size;
	else
		var ml = 320, new_i = i - 1;
					
	pause_video(videos.eq(--i));		
	collection.animate({ marginLeft: '+=' + ml + 'px' }, 600, 'easeInOutQuint', function() { update_description(new_i, collection_size) });
  update_video_views(videos.eq(new_i - 1));
}

function vid_next() {
	var collection      = jq('#video_panel'),
			video_detail		= jq('#video_detail_contain div[id^=video_details]:visible'),
	    i               = parseInt(video_detail.attr('id').replace('video_details_', '')),
	    collection_size = collection.find('object').length,
	    videos          = collection.children();
	
	if ( i == collection_size )
		var ml = -320 * (collection_size - 1), new_i = 1;
	else
		var ml = 320, new_i = i + 1;
					
	pause_video(videos.eq(--i));		
	collection.animate({ marginLeft: '-=' + ml + 'px' }, 600, 'easeInOutQuint', function() { update_description(new_i, collection_size) });
  update_video_views(videos.eq(new_i - 1));
}

// pause viewed if playing
function pause_video(video) {
	var container = jq('#video_panel');
	if ( container.data(video.attr('id')).played ) {
		video.get(0).sendEvent('PLAY', 'false');
		container.data(video.attr('id'), { viewed: true, played: false });
	}
}

function update_description(i, collection_size){
	jq('#video_detail_contain div[id^=video_details]:visible').hide();
	jq('#video_details_' + i).show();
	jq('#count').html(i + ' of ' + collection_size + ' videos');
}