$(document).ready(function() {
	// Check for iPad
	// you can additionally add |iPod|iPhone to this statement
	if (navigator && navigator.platform && navigator.platform.match(/^(iPad)$/)) {
		$('#promo-tabs-container') 
		.after('<ul id="ipad-promo-tabs-selectors">') 
		.cycle({
			cleartype:  1, 
			timeout: 0, 
			effect: 'fade',
			pager:  '#ipad-promo-tabs-selectors',
			pagerEvent:   'click',
			pagerAnchorBuilder: function(idx, slide) { 
				return '<li class="' + jQuery(slide).attr("id") + ' group"><a href="#" class="group"><span class="wrap">' + jQuery(slide).find("div.tab-text").html() + '</span><span class="extra"></span></a></li>'; 
			}
		});    
	} else {  
		$('#promo-tabs-container') 
		.after('<ul id="promo-tabs-selectors">') 
		.cycle({
			cleartype:  1, 
			timeout: 8000, 
			effect: 'fade',
			speed: 500,
			pause: true,
			pager:  '#promo-tabs-selectors',
			pagerEvent:   'click',
			pagerAnchorBuilder: function(idx, slide) { 
				return '<li class="' + jQuery(slide).attr("id") + ' "><a href="#"><span class="wrap">' + jQuery(slide).find("div.tab-text").html() + '</span><span class="extra"></span></a></li>'; 
			}
		});
	}

	$('#promo-tabs-container').hover(function(){
		$('#promo-tabs-container').cycle("pause");
	});
	
	if($.browser.msie) {
		$('#promo-tabs-selectors li').css('filter','progid:DXImageTransform.Microsoft.BasicImage(rotation=1)')
		$('#promo-tabs-selectors li').css('width','320');
	}
	
}); // end document.ready


// This function is called when an error is thrown by the player
function onPlayerError(errorCode) {
	alert("An error occured of type:" + errorCode);
}

// for each promo content 1 2 etc, hide the image
// also, make slideshow pause when video is playing

// take away image thumb
function videoBegin1(playState) { checkPlayState(1, playState); }
function videoBegin2(playState) { checkPlayState(2, playState); }
function videoBegin3(playState) { checkPlayState(3, playState); }
function videoBegin4(playState) { checkPlayState(4, playState); }
function videoBegin5(playState) { checkPlayState(5, playState); }

function checkPlayState(playerId, playState) {
	
	// for Omniture stuff later
	var ytplayer = document.getElementById("myytplayer"+playerId);
	var video_name = $('#promo-content'+playerId+' .tab-text').text();
	
	// removes image thumbnail when video plays
	// and adds image thumbnail when video is ended or cued
	//console.log(playState);

	$('#play' + playerId).click(function() {
		$(this).addClass('playing');
	});

	if( playState == 1 ) { // removes image when video is played
		$('#promo-tabs-container').cycle("pause");

		$('img.vthumb'+playerId).hide();
		
		$('.video-wrap').animate({
				height: '529px'
		}, 500, 
		function() {
		     $('.promo-text').fadeOut();        
		});
    	
		$('#promo-tabs-selectors li a').fadeOut();

		$('.vid-controls').animate({
			top: '479px'
		}, 500);
		
		// Omniture media tracking
		s.Media.play(video_name, ytplayer.getCurrentTime());

	} else if(playState == 0 || playState == 5 || playState == -1) { // adds image back when video ends or is cued up
		//$('#promo-tabs-container').cycle("resume");
		$('#play' + playerId).removeClass('playing');

		$('.video-thumb').show();
					
		$('.video-wrap').animate({
			height: '320px'
		}, 500);
		
		if($.browser.msie) {
			$('#promo-tabs-selectors li a').fadeIn();
		} else {
			$('#promo-tabs-selectors li a').animate({
				width:'310px'
			});
		}		
		$('.promo-text').show().css('z-index', '100');
					
		$('.vid-controls').animate({
			top: '270px'
		}, 500);
		
		// Omniture media tracking
		s.Media.open(video_name, ytplayer.getDuration(), 'YouTube Chromeless Player');
		
	} else if(playState == 2){
		$('#play' + playerId).removeClass('playing');
					
		$('.video-wrap').animate({
			height: '320px'
		}, 500);
		
		$('#promo-tabs-selectors li a').fadeIn();
		
		$('.promo-text').show();
					
		$('.vid-controls').animate({
			top: '270px'
		}, 500);
		
		// Omniture media tracking
		s.Media.stop(video_name, ytplayer.getCurrentTime());
	}
}

function setVolume(newVolume) {
	if (ytplayer) {
		ytplayer.setVolume(37);
	}
}

function playerParams(ytplayer, videoId, playerId) {
	ytplayer.cueVideoById(videoId);
	ytplayer.addEventListener("onStateChange", "videoBegin"+playerId);
}

var ytplayerIds = [];

function onYouTubePlayerReady(playerId) {
	ytplayer = document.getElementById("myytplayer"+playerId);
	
	videoId = ytplayerIds[playerId - 1];
	// videoId = $("#myytplayer"+playerId+' param[name="videoId"]').attr('value');
	playerParams(ytplayer, videoId, playerId);
}

function playYoutubeVideo(playerId){
	ytplayer = document.getElementById("myytplayer"+playerId);
	ytplayer.playVideo();
	$('#promo-tabs-container').cycle("pause");
	
	return false;
}

function pauseYoutubeVideo(playerId) {
	ytplayer = document.getElementById("myytplayer"+playerId);
	ytplayer.pauseVideo();
	
	return false;
}
