$().ready( function(){
	bindEvents();
	var galleryAnimate = true;
} );

// bindEvents on ready -------------------------------------------------
function bindEvents() {
	// home boxes image fade on hover
	$('#home div.boxes div.frame div.fr_content a,a.hvr_fade,.bluray .frame_sml .fr_content_sml a').hoverFade({
		endOpacity:1,
		startOpacity: 0.7,
		obj: 'img'
		//,border: 'solid 2px #fff'
	});
	
	// Share links
	
	//$('div.socialLinks ul li:not(.remind) a').click(function(){
		//socialLinks($(this).find('span').text());
		//return false;
	//});
	
	// Gallery ----------------------------------------------------- */
		
		$('div#gallery_thumbs,div.gallery_thumbs').not('div#behind div.gallery_thumbs').gallery();
		$('div#behind div.gallery_thumbs').gallery({amount:6});
		
		$('div.video_thumbs').each( function(){
			$(this).gallery({amount: 3});
		} );

		$('div#gallery_thumbs div.thumbs_container a,div.gallery_thumbs div.thumbs_container a')
			.colorbox({
				rel: $('#gallery div.thumbs_container a'),
				current: '{current} / {total}',
				onComplete: colorboxLoad
			})
			.hoverFade({
				startOpacity: 0.7,				   
				endOpacity:1,
				obj: 'img' 
			});
	
	// Videos ---------------------------------------------------- */
	$('div.thumbs_container li a').click( function(){
		$(this).parents('ul').find('a.active').removeClass('active');
		$(this).addClass('active');
	});
			
	// random star Pos hover 
	$('#reminder ul.cal li a').hover( function() {
		c = parseInt( (Math.random() * 4 )+3);
		
		$(this).everyTime(100,'addStar',function(){randStar($(this));},c);
		
	},function(){
		$(this).stopTime().find('img.star').stop();
		if( (getClient() == 'IE7') || (getClient() == 'IE6') || (getClient() == 'IE8') ) {
			$(this).find('img.star').remove();
		} else {
			$(this).find('img.star').stop().fadeOut('',function(){$(this).remove()});
		}
	});
	
	// tabs ----------------------------------------------------- */
	
	$('div.tabs ul li a').click(function(){
		li = $(this).parent();
		ul = li.parent();
		ul.find('li.active').removeClass('active');
		li.addClass('active');
		
		// show the new tab
		
		current = $('div#tab_load div.tab_content');
		newTab = $(''+$(this).attr('href'));
		
		current.hide();
		newTab.show();
		
		return false;
	});
	
	// Competition -------------------------------------------- */
	$('div#competition div#answers li a').click(function(){
		$('input#answer').val($(this).find('span.chk').text());
		jQuery(this).parents('ul:first').find('a.active').not(jQuery(this)).removeClass('active');		
		jQuery(this).toggleClass('active');
	});
	
	// special terms popup
	$('#special_terms_btn').colorbox({
		href: jQuery(this).attr('href'),
		title: ' '
	});
	
	// soundtrack --------------------------------------------- */
	$('div#soundtrack ol.track_list li a').each(function(i){
		$('<span/>',{
			className: 'trackNum',
			text: i+1+'.'
		}).prependTo($(this));
	});
	
	/* Leave Overlay */
	$('a.leave, div.socialLinks a:not(:first)').click(function(){
		if($(this).parents('div:first').hasClass('socialLinks')){
			var lnk = $(this).find('span').text();
		} else {
			var lnk = $(this).attr('href');
		}
		$('#leavingOverlay')
			.height($(document).height())
			.load('_include/ssi/leaveOverlay.html',function(){
				$('#continue').live('click',function(){
					$('#leavingOverlay').hide();								 
					socialLinks(lnk);
				});
				$(this).find('a.leaveClose')
					.live('click',function(){
						$('#leavingOverlay').fadeOut();
					})
			})
			.fadeIn();
			return false;
	});
	
	// key terms show hide ------------------------------------------ 
	$('#keyTerms').hide();
	$('#keyTermsBtn').click(function(){
		$(this).toggleClass('opened');
		showHide($(this).attr('href'));
		return false;
	})
	
} // end bind

function playPause(video,btn){
	if(video.paused) {
		video.play();
		btn.css({backgroundPosition:'-100px 0'});
	} else {
		video.pause();
		btn.css({backgroundPosition:'0 0'});
	}
	return false;
}

function colorboxLoad() {
	$('div#cboxTitle').wrapInner('<span/>');
}

// check if device is iPhone / iPad
function isiPhone(){
	if( (navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPad/i)) || (navigator.userAgent.match(/iPod/i)) ) {
		return true;
	} else {
		return false;
	}
}

function getClient() {
	if ( (navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPad/i)) || (navigator.userAgent.match(/iPod/i)) ) {
		return 'iPhone';
	}
	else if ( navigator.userAgent.match(/MSIE 7/i) ) {
		return 'IE7';
	}
	else if ( navigator.userAgent.match(/MSIE 8/i) ) {
		return 'IE8';
	}
	else if ( navigator.userAgent.match(/MSIE 6/i) ) {
		return 'IE6';
	} 
	else {
		return navigator.userAgent;
	}
}

function randStar(obj) {
	oW = obj.width() - 147;
	oH = obj.height() - 38;
	var x = parseInt (Math.random() * oW) +118;
	var y = parseInt (Math.random() * oH);
	var size = parseInt (Math.random() * 50)+ 50;
	nw = (29 / 100) *size;
	nh = (38 / 100) *size;
	
	var star = $('<img />');
	star
		.attr('src','images/content/star.png')
		.css({
			position: 'absolute',
			top: y,
			left: x,
			display: 'none',
			width: nw,
			height: nh
		})
		.addClass('star');
	
	obj.append(star);
	$star = obj.find('img.star');
	if( (getClient() =='IE6') || (getClient() =='IE7') || (getClient() =='IE8') ) {
		$star.show();
	} else {
		$star.fadeIn();
	}
}

function autoLoadVideo(){
	var videoIn=(location.href);
	var videoData=new Array();
	var videoData=videoIn.split('?');	
	if(videoData[1] != null){
		loadVideo(videoData[1]);
	}
}

function loadVideo(src){
	
	$('#flash_player').html('<div id="videoPlayer"></div>');
	swfobject.embedSWF('flash/videoplayer.swf', 'videoPlayer', '563', '316', '9.0.115', '',{
	autoStart:'true',
	src: src,
	fbLink:'http://www.disney.co.uk/sorcerers_apprentice/videos.htm'
	},
	{allowFullScreen: 'true', allowscriptaccess: 'always', bgcolor: '#000000',wmode:"opaque", scale: "noScale"},
	{id: 'flashvideo'}
	);
}

function socialLinks(lnk){
	var URL = encodeURIComponent(window.location);
	var Title = encodeURIComponent(document.title);
	
	if(lnk == 'Facebook') {
		window.open('http://www.facebook.com/sharer.php?u='+URL+'&t='+Title);
		return false;
	} else if(lnk == 'Bebo') {
		window.open('http://bebo.com/c/share?Url='+URL+'&Title='+Title); 
		return false;
	} else if (lnk == 'Twitter') {
		window.open('http://twitter.com/home?status=Currently reading '+URL);
		return false;
	}
	else if(lnk == 'SchulerVZ') {
		window.open('http://www.schuelervz.net/Disney_Movies'); 
		return false;
	}
	
	if ( (lnk !='Facebook') || (lnk !='Bebo') || (lnk !='Twitter') ) {
		window.location = lnk;
	}
	return false;
}

function flashLeave(lnk) {
	$('#leavingOverlay')
		.height($(document).height())
		.load('_include/ssi/leaveOverlay.html',function(){
			$('#continue').live('click',function(){
				$('#leavingOverlay').hide();								 
				socialLinks(lnk);
			});
			$(this).find('a.leaveClose')
				.live('click',function(){
					$('#leavingOverlay').fadeOut();
				})
		})
		.fadeIn();
}
function showHide(id){
	ele = $(''+id);
	if (ele.is(':visible')){
		ele.hide();
	} else {
		ele.show();
	}
}
