//playerState variable shows status of YouTube video. 
//see http://code.google.com/apis/youtube/youtube_player_demo.html
//currently hardcoded with id of video
function onYouTubePlayerReady(){
  ytplayer = document.getElementById('myytplayer');
  ytplayer.addEventListener('onStateChange','onytplayerStateChange');
}
function onytplayerStateChange(newState) {
  window.playerState = newState;
}

//Simple JavaScript Rotating Banner Using jQuery
//www.mclelun.com
var jqb_vCurrent = 0;
var jqb_vTotal = 0;
var jqb_vDuration = 5000;
var jqb_intInterval = 0;
var jqb_vGo = 1;
var jqb_vIsPause = false;
var jqb_tmp = 20;
var jqb_title;
var jqb_imgW = 990;
var jqb_imgH = 376;

window.playerState = -1;

var $j = jQuery.noConflict();

jQuery(document).ready(function($){
	
	/* Begin jqbanner.js */
	
	jqb_vTotal = $(".jqb_slides").children().size() -1;
	$(".jqb_info").text($(".jqb_slide").attr("title"));	
	jqb_intInterval = setInterval(jqb_fnLoop, jqb_vDuration);
			
	//Horizontal
	$("#jqb_object").find(".jqb_slide").each(function(i) { 
		jqb_tmp = ((i - 1)*jqb_imgW) - ((jqb_vCurrent -1)*jqb_imgW);
		$(this).animate({"left": jqb_tmp+"px"}, 500);
	});
	
	/*
	//Vertical
	$("#jqb_object").find(".jqb_slide").each(function(i) { 
		jqb_tmp = ((i - 1)*jqb_imgH) - ((jqb_vCurrent -1)*jqb_imgH);
		$(this).animate({"top": jqb_tmp+"px"}, 500);
	});
	*/
	
	$("#btn_pauseplay").click(function() {
		if(jqb_vIsPause){
			jqb_fnChange();
			jqb_vIsPause = false;
			$("#btn_pauseplay").removeClass("jqb_btn_play");
			$("#btn_pauseplay").addClass("jqb_btn_pause");
		} else {
			clearInterval(jqb_intInterval);
			jqb_vIsPause = true;
			$("#btn_pauseplay").removeClass("jqb_btn_pause");
			$("#btn_pauseplay").addClass("jqb_btn_play");
		}
	});
	$("#btn_prev").click(function() {
		jqb_vGo = -1;
		jqb_fnChange();
	});
		
	$("#btn_next").click(function() {
		jqb_vGo = 1;
		jqb_fnChange();
	});
	
	/* End jqbanner.js */
	
	$('ul.topnav li').hover(
		function() {
			clearTimeout($(this).data('timeoutID')); // Quick! Cancel the timer before it hides the subnav!
			$('ul', this).slideDown(300).show();	// slideDown(300) = kind of fast
		},
		function() {
			// Set timer before we hide dropdown. Store the timeout ID for destruction
			// by clearTimeout() if we find that the mouse has returend to the element in question 
			var timeoutID = setTimeout(
					function(){ $('ul.topnav li').find(".subnav").slideUp('slow'); }
					, 500 );
			$(this).data('timeoutID', timeoutID);
		}
	);
	
	// Ensure that we don't accidentally hide when hovering over
	// dropdown menu elements
	$('ul.topnav li ul li').hover(
		function() {
			clearTimeout($(this).data('timeoutID')); // Quick! Cancel the timer before it hides the subnav!
			$('ul', this).parent().slideDown(300).show();	// slideDown() = kind of fast
		}
	);
	
	// Apply style to top navigation if IE < v6.0 is detected
	if($.browser.msie) {
		var x = parseFloat($.browser.version);
		if( x  <= 6 ) {
			$('#obr-topnav ul.topnav li ul.subnav').addClass('addredbg');
		}
	}
	
	// Apply tooltip for category pages
	// $(".catalogitem a img").tooltip();
	$('.catalogitem img[title]').tooltip({ delay: 0, effect: 'fade' });
});

function jqb_fnChange(){
	if( window.playerState == 1 ) return;
	clearInterval(jqb_intInterval);
	jqb_intInterval = setInterval(jqb_fnLoop, jqb_vDuration);
	jqb_fnLoop();
}

function jqb_fnLoop(){
	if( window.playerState == 1 ) return;
	if(jqb_vGo == 1){
		jqb_vCurrent == jqb_vTotal ? jqb_vCurrent = 0 : jqb_vCurrent++;
	} else {
		jqb_vCurrent == 0 ? jqb_vCurrent = jqb_vTotal : jqb_vCurrent--;
	}
	
	
	var x = $j("#jqb_object").find(".jqb_slide");
			
	$j("#jqb_object").find(".jqb_slide").each(function(i) { 
		if(i == jqb_vCurrent){
			jqb_title = $j(this).attr("title");
			$j(".jqb_info").animate({ opacity: 'hide', "left": "-50px"}, 250,function(){
				$j(".jqb_info").text(jqb_title).animate({ opacity: 'show', "left": "0px"}, 500);
			});
		} 
			
			
		//Horizontal Scrolling
		jqb_tmp = ((i - 1)*jqb_imgW) - ((jqb_vCurrent -1)*jqb_imgW);
		$j(this).animate({"left": jqb_tmp+"px"}, 500);
		
		
		/*
		//Vertical Scrolling
		jqb_tmp = ((i - 1)*jqb_imgH) - ((jqb_vCurrent -1)*jqb_imgH);
		$j(this).animate({"top": jqb_tmp+"px"}, 500);
		*/
		
		/*
		//Fade In & Fade Out
		if(i == jqb_vCurrent){
			$j(".jqb_info").text($j(this).attr("title"));
			$j(this).animate({ opacity: 'show', height: 'show' }, 500);
		} else {
			$j(this).animate({ opacity: 'hide', height: 'hide' }, 500);
		}
		*/
		
	});


}
