// JavaScript Document
var active_tab = 0;
$(document).ready(function() {
	$(document).pngFix('crop');
	
	/* RECAPS */
	recaps_block_w = $('.recaps-block-content img').width();
	recaps_count = $('.recaps-page-bar a').size();
	$('.recaps-block-content').css('width',recaps_count*395);
	$(".recaps-page-bar a").click(function() {
		id = $('.recaps-page-bar a').index($(this));
		slideRecaps(id);
		clearInterval(recaps_int);
	})
	
	/* RECAPS SLIDER */
	function slideRecaps(id) {
		$('.recaps-page-bar a').removeClass('selected');										   
		$('.recaps-page-bar a').eq(id).addClass('selected');
		$('.recaps-block-content').animate({left: -recaps_block_w*id},500);		
	}
	
	/* RECAPS AUTOPLAY */
	var recaps_active = 1;
	if(recaps_count > 1) { 
		var recaps_int = setInterval(function() {										  
			if(recaps_active > (recaps_count-1)) recaps_active = 0;
			slideRecaps(recaps_active)
			recaps_active++;
		},3000);
	}
	
	/* ENCYCLOPEDIA */
	$('.sub-button-bar td').click(function() {
		$('.sub-button-bar td').removeClass('active');																		
		$(this).addClass('active');
		lnk = $('a',this).attr('href');
		frames['internal'].location = lnk;		
	})
	
	/* MAIN */
	if($('.video_contents').height() > $('.video_holder').height()) 
	{
		$('.controls').show();
	}
	
	videos_pos = 0;
	video_pages = Math.ceil($('.video_contents').height() / $('.video_holder').height());
	$('.controls .down').click(function() {
		if(Math.abs(videos_pos) < video_pages-1) {
			videos_pos--;
			$('.video_contents').animate({top: videos_pos*$('.video_holder').height()},300);		
		}
		return false;
	})
	$('.controls .up').click(function() {
		if(parseInt($('.video_contents').css('top')) < 0) {
			videos_pos++;			
			$('.video_contents').animate({top: videos_pos*$('.video_holder').height()},300);
		}
		else {
			$('.video_contents').animate({top: 0},300);			
		}
		return false;
	})	
	
	/* FACEBOOK SHARE */
	$('.facebook-share').click(function() {
		title = document.title;
		link = window.location.href.toString();
		share('/main/php/share.php','facebook',link,title,'Pacific','/images/pacific_facebook_image.jpg')
	})
	
	/* CAST */	
	if($('#cast').size() > 0) {
		$('#cast .text-boxes div').jScrollPane();
	
		$('#cast .text-boxes .jScrollPaneContainer').hide();
		
		$('.characters div a').click(function() {
			id = $('.characters div a').index($(this));
			smallsrc = $('img',$(this)).attr('src');
			var large_array = smallsrc.split('/');
			$("#cast .image img").attr('src','../images/cast/large/'+large_array[3])
			$('#cast .text-boxes .jScrollPaneContainer').hide();		
			$('#cast .text-boxes .jScrollPaneContainer').eq(id).show();
			location.hash = "#"+id;
			return false;
		})
		
		var cId = location.hash.substring(1)
		$('.characters div a').eq(cId == "" ? 0 : cId).click()
	}
	
	
	/* EPISODES */
	$('#recaps .description div').eq(2).show();
	$('#recaps .gallery-image').eq(2).show();	
	$('.episode-list a').click(function() {
		id = $('.episode-list a').index($(this));
		$('#recaps .description div').hide();
		$('#recaps .gallery-image').hide();			
		$('#recaps .gallery-image').eq(id).show();			
		$('#recaps .description div').eq(id).show();
	})
	
	/* QA */
	$('#qa .description div').eq(0).show();	
	$('.episode-list a').click(function() {
		id = $('.episode-list a').index($(this));
		$('.description div').hide();
		$('.description div').eq(id).show();
	})
	
	/* GALLERY PAGE */
	//$('#gallery .tab-content').eq(0).addClass('active-content');
	
	/* COMMON */
	$('.selector .left').click(function() {
		scrollBar('left');
		return false;
	})
	$('.selector .right').click(function() {
		scrollBar('right');
		return false;
	})	
	
	$('.tab-content').eq(0).show();
	$('.tabs a').click(function() {
		id = $('.tabs a').index($(this));
		$('.tabs div').removeClass('selected');
		$(this).parent().addClass('selected');
		$('.tab-content').hide();
		$('.tab-content').removeClass('active-content');
		$('.tab-content').eq(id).show();
		$('.tab-content').eq(id).addClass('active-content')
		active_tab = id;
		$('.characters .active-content').css('left',0);	
		cast_i = 0;
		galleryAddE();
		scrollBar();		
		return false;
	})
	$('.tabs a').eq(0).click();
	$('.selector .left').click();
	
	galleryAddE();
	
})

function galleryAddE() {
	$('#gallery .characters .active-content a').click(function() {
		id = $('.characters .active-content a').index($(this))
		$('.gallery-image img').attr('src','../images/gallery/'+(active_tab+1)+'/img_'+id+'.jpg');
	})	
}

cast_i = 0;
function scrollBar(direction) {
	ct_w = 0;
	$('.characters .active-content a').each(function() {
		ct_w += parseInt($(this).width())+1;													   
	})
	$('.characters div').eq(active_tab).width(ct_w);
	
	var holder = $('.characters').width();
	pages = Math.ceil(parseInt(ct_w/holder))
	if(ct_w > holder) {
		if(direction == 'right') {
			if(cast_i < pages) {
				cast_i++;
				if(cast_i != pages) {
					$('.characters div').animate({left:(-holder*cast_i)},500,function() {});	
				}
				else {
					$('.characters div').animate({left:(-ct_w+holder)},500,function() {});	
				}
			}
		}
		else {
			cast_i--;									
			if(cast_i < 0) {
				cast_i = 0;	
			}
			$('.characters div').animate({left:(-holder*cast_i)},500,function() {});	
		}
	}
	galleryAddE();
}

function slideRecaps() {
	$("#recaps ")
}

function openWallpaper(path){
	window.open(path,"Pacific wallpaper","")
}

function showVideo(vPath){
	document.getElementById(attributes.id).showVideo(vPath)
}

/* MAIN FLASH VIDEO */
var flashvars = {
	skinPath: "../images/flash/videoPlayerSkin.swf",
	videoPath: "/data/0.flv"
};
var params = {
	bgcolor: '#000000',
	menu: "false",
	wmode: "window",
	allowScriptAccess: "always",
	allowFullScreen: "true"
};
var attributes = {
	id: "videoPlayer",
	name: "videoPlayer"
};

swfobject.embedSWF("../images/flash/videoPlayer.swf", "flash", "380", "285", "9.0.0", null, flashvars, params, attributes);