
jQuery(document).ready(function(){
	/** ==================================== 
	 *  JOI Corp. Footer
	 * ==================================== */
	var objJoCorpBrandFooter =new joCorpBrandFooter({scheme:"dark",bgcolor:"313431",loadAssetsFrom:"beta"});
	objJoCorpBrandFooter.write("joi-footer-dynamic");
	
	/** ==================================== 
	 *  Search
	 * ==================================== */
	jQuery('#header #topnav #search input').focus(function(){
		if (this.value == this.defaultValue) {
			this.value = '';
			jQuery(this).addClass('focused');
		}
	}).blur(function(){
		if (this.value == '') {
			jQuery(this).removeClass('focused');
			this.value = this.defaultValue;
		}
	});
	/** ==================================== 
	 *  Forms
	 * ==================================== */
	
	var registerSubmit = function(form){
		jQuery('#register-form').addClass('loading');
		var dat = jQuery('#register-form').serialize();
		jQuery.post('/bogusbackend.php', dat, function(data){
			if (data.status == 'ok') {
				jQuery('.form', form).css('visibility', 'hidden');
				jQuery('#thankyou', form).show();
			} else {
				alert(data.message);
			}
			jQuery('#register-form').removeClass('loading');
		}, 'json');
	};
	var sendToFriendSubmit = function(form){
		jQuery('#send-to-friend').addClass('loading');
		var dat = jQuery('#send-to-friend').serialize();
		jQuery.post('/bogusbackend.php', dat, function(data){
			if (data.status == 'ok') {
				jQuery('.form', form).css('visibility', 'hidden');
				jQuery('#thankyou', form).show();
			} else {
				alert(data.message);
			}
			jQuery('#send-to-friend').removeClass('loading');
		}, 'json');
	};
	
	//jQuery("#send-to-friend").validate({submitHandler:sendToFriendSubmit});
	//jQuery("#register-form").validate({submitHandler:registerSubmit});
	//jQuery("#catalog-form").validate();
	//jQuery("#contact-form").validate();
	 
	/** ==================================== 
	 *  Register for Updates Popups
	 * ==================================== */
	var registerIsDisplayed = false;
	jQuery('#topsubnav a.register').click(function(){
		if (registerIsDisplayed) {
			registerIsDisplayed = false;
			jQuery('#topsubnav a.register').removeClass('active');
			jQuery('#register-form').hide();
		} else {
			registerIsDisplayed = true;
			jQuery('#topsubnav a.register').addClass('active');
			jQuery('#register-form').show();
		}
		return false;
	});
	
	jQuery('#register-form a.close').click(function(){
		registerIsDisplayed = false;
		jQuery('#register-form').slideUp();
	});
	/** ==================================== 
	 *  Send to a Friend
	 * ==================================== */
	var sendToIsDisplayed = false;
	jQuery('#sendToFriend a.send').click(function(){
		if (sendToIsDisplayed) {
			sendToIsDisplayed = false;
			jQuery('#sendToFriend #send-to-friend').css("display","none");
			jQuery('#sendToFriend #send-to-friend .form').hide();
		} else {
			sendToIsDisplayed = true;
			jQuery('#sendToFriend #send-to-friend').css("display","block");
			jQuery('#sendToFriend #send-to-friend .form').show();
		}
		return false;
	});
	jQuery('#sendToFriend #send-to-friend .form a.close').click(function(){
		sendToIsDisplayed = false;
		jQuery('#sendToFriend #send-to-friend').css("display","none");
		jQuery('#sendToFriend #send-to-friend .form').hide();
		return false;
	});
	 
	
	/** ==================================== 
	*  force footer to bottom
	* ==================================== */
	var geoSizePage = function () {
		// force footer to bottom
		jQuery('#main-content').css('min-height','auto'); // reset height
		var bh = jQuery('body').outerHeight(true);
		var wh = jQuery(window).height();
		if (wh > bh) {
			jQuery('#main-content').css('min-height', (jQuery('#main-content').outerHeight(true)+wh-bh+14)+'px' );
		}
	};
	
	jQuery(window).resize(geoSizePage);
	geoSizePage();
	
	/** ==================================== 
	*  Topnav Product Dropdown
	* ==================================== */
	
		// topnav product dropdown - align heights
		// the product drop down must be showing to compute
		// the heights in order to align them
	var topSubNavHeightsAligned = false;
	var alignTopSubNavHeights = function(){
		if (topSubNavHeightsAligned) {
			return;
		} else {
			topSubNavHeightsAligned = true;
		}
		
		var topNavMaxH = 0;
		jQuery('#topnav > ul > li li.col').each(function(){
			topNavMaxH = Math.max(jQuery(this).outerHeight(), topNavMaxH);
		});
		jQuery('#topnav > ul > li li.col').height(topNavMaxH);
		
	};
	
		// topnav dropdowns - hover timeouts
	var topnavTimeout = false;
	var topnavOver = function(){
		window.clearTimeout(topnavTimeout);
		jQuery('#topnav > ul > li').removeClass('hover');
		jQuery(this).addClass('hover');
		
		if (jQuery(this).hasClass('product')) {
			alignTopSubNavHeights();
		}
	};
	var topnavOut = function(){
		topnavTimeout = window.setTimeout("jQuery('#topnav > ul > li').removeClass('hover');", 500);
	};
	
	jQuery('#topnav > ul > li').hover(topnavOver, topnavOut);
		//disable top nav category links
	jQuery("#topnav > ul > li > a").css('cursor','default').click(function(){
	    return false;
	}); 
	jQuery("#topnav > ul > li > li > a").css('cursor','pointer').click(function(){
	    return true;
	}); 
	
	/** ==================================== 
	*  Home page Hero Scroller
	* ==================================== */
	var heroScrollerCurIndex = 0;
	var heroScrollerCount = 0;
	var heroScrollerInterval = false;
	
	heroScrollerCount = jQuery('#hero-scroller .heroes img').length;
	
	heroScrollerNext = function(){
		if (heroScrollerCurIndex == heroScrollerCount-1) {
			heroScrollerCurIndex = 0;
		} else {
			heroScrollerCurIndex++;
		}
		
		jQuery('#hero-scroller .heroes').scrollTo( jQuery('#hero-scroller .heroes img:eq('+heroScrollerCurIndex+')') , {duration:700, axis:"x"});
		
		return false;
	};
	
	heroScrollerPrevious = function(){
		if (heroScrollerCurIndex == 0) {
			heroScrollerCurIndex = heroScrollerCount-1;
		} else {
			heroScrollerCurIndex--;
		}
		
		jQuery('#hero-scroller .heroes').scrollTo( jQuery('#hero-scroller .heroes img:eq('+heroScrollerCurIndex+')') , {duration:700, axis:"x"});
		
		return false;
	};
	
	jQuery('#hero-scroller a.next').click(heroScrollerNext);
	jQuery('#hero-scroller a.previous').click(heroScrollerPrevious);
	
	heroScrollerInterval = window.setInterval("heroScrollerNext();", 6000);
	
	jQuery('#hero-scroller').hover(function(){
		window.clearInterval(heroScrollerInterval);
	}, function(){
		heroScrollerInterval = window.setInterval("heroScrollerNext();", 6000);
	});
	
	/** ==================================== 
	*  Product Pages
	* ==================================== */	
	jQuery('#productPageIntro div.views a').click(function(){
    	var rel = jQuery(this).attr('rel');
    	jQuery('#productPageIntro div.views a').removeClass('active');
    	jQuery(this).addClass('active');
    	
    	jQuery('#productPageIntro #screenImages .view:visible').fadeOut('fast', function(){
    		jQuery('#productPageIntro #screenImages .view'+rel).fadeIn('fast');
    	});
    	
    	return false;
    });
	
		// Product Page Galleries
	var photoGalleryCurIndex = [];
	var photoGalleryCount = [];
	function prepPhotoGallery(id) {
		photoGalleryCurIndex[id] = 0;
		
		photoGalleryCount[id] = jQuery('#'+id+' div.image').length;
		jQuery('#'+id+' .meta .count').empty().append( photoGalleryCount[id] );
		
		jQuery('#'+id+' a.close').click(function(){
			jQuery('#'+id+'').fadeOut('fast', function(){
				jQuery('#screenImages').fadeIn('fast');
	    	});
		});
		
		jQuery('#'+id+' a.next').click(function(){
			if (photoGalleryCurIndex[id] == photoGalleryCount[id]-1) {
				photoGalleryCurIndex[id] = 0;
			} else {
				photoGalleryCurIndex[id]++;
			}
			
			jQuery('#'+id+' .lining').scrollTo( jQuery('#'+id+' .inner div.image:eq('+photoGalleryCurIndex[id]+')') , {duration:700, axis:"x"});
			jQuery('#'+id+' .meta .current').empty().append( photoGalleryCurIndex[id]+1 );
		});
		jQuery('#'+id+' a.previous').click(function(){
			if (photoGalleryCurIndex[id] == 0) {
				photoGalleryCurIndex[id] = photoGalleryCount[id]-1;
			} else {
				photoGalleryCurIndex[id]--;
			}
			
			jQuery('#'+id+' .lining').scrollTo( jQuery('#'+id+' .inner div.image:eq('+photoGalleryCurIndex[id]+')') , {duration:700, axis:"x"});
			jQuery('#'+id+' .meta .current').empty().append( photoGalleryCurIndex[id]+1 );
		});
		
	}
	
	var numGalleries = 0;
	jQuery('.productTab.gallery .photoGallery').each(function(){
			// set unique id on gallery if needed
		var id;
		var len = numGalleries++;
		var rel = jQuery(this).attr('rel');
		var a;
		var self = this;
		
		if (this.id) {
			id = this.id;
		} else {
			id = 'uniqDynGallery_'+ len;
			this.id = id;
		}
		
			// hide galleries other than the first
		if (len >= 1) {
			jQuery(this).hide();
		}
		
		if (len == 0) {
			a = jQuery('<a href="#gallery'+len+'" class="active">'+rel+'</a>');
		} else {
			a = jQuery('<a href="#gallery'+len+'">'+rel+'</a>');
		}
		
		a.click(function(){
			jQuery('.productTab.gallery .col-right a').removeClass('active');
			jQuery(this).addClass('active');
			jQuery('.productTab.gallery .photoGallery:visible').fadeOut('fast', function(){
				jQuery(self).fadeIn('fast');
				jQuery('.lining', self).scrollTo( jQuery('div.image:eq(0)', self) , {duration:0, axis:"x"});
				jQuery('.meta .current', self).empty().append( '1' );
				photoGalleryCurIndex[id] = 0;
			});
			return false;
		});
		
		jQuery('.productTab.gallery .col-right').append(a);
		
		prepPhotoGallery(id);
	});
	
	jQuery('#productPageFooter .overview .images .image.main a.view-other, .productTab.overview a.view-other').click(function(){
		
		jQuery("ul.productTabs li.last-child a").trigger("click");
		jQuery(".productTab.gallery .col-right a[href=#gallery1]").trigger("click");
		return false;
	});
	
		// Generic tabs
	jQuery('ul.productTabs li a').click(function(){
		var parent = jQuery(this).parents('ul:eq(0)');
		var pRel = jQuery(parent).attr('rel');
		var rel = jQuery(this).attr('rel');
		var pli = jQuery(this).parents('li:eq(0)');
		
		jQuery('li', parent).removeClass('active').removeClass('first-child-active').removeClass('last-child-active');
		pli.addClass('active');
		
		if (pli.hasClass('first-child')) {
			pli.addClass('first-child-active');
		} else if (pli.hasClass('last-child')) {
			pli.addClass('last-child-active');
		}
		
		jQuery('.'+pRel).removeClass('active');
		jQuery('.'+pRel+'.'+rel).addClass('active');
		
		return false;
	});
	
	if (jQuery.browser.safari) {
		jQuery('body').addClass('webkit');
	}
	if (navigator.userAgent.toLowerCase().indexOf("windows") != -1 || navigator.userAgent.toLowerCase().indexOf(" win ") != -1) {
		jQuery('body').addClass('win');
	}
	if (jQuery('#homeSwfCalloutInner').length) {
	    var flashvars = {
			media: "go-boldly-en",
			aspectratio: "4:3",
			autoplay: "0",
			avatar: "/images/video/avatars/go-boldly.jpg",
			fullscreenpage: "true",
			fsreturnpage: "true"
		};
		var params = {
			allowfullscreen: "true",
			bgcolor: "#000000",
			quality: "best"
		};
		var attributes = {};
   		swfobject.embedSWF("/_swf/SRVideoPlayer.swf", "homeSwfCalloutInner", "300", "225", "9.0.124.0","expressInstall.swf", flashvars, params, attributes);
	}
});