// Live site scripts
$(document).ready(function(){
		if($){$('body').addClass('js');};
		$('article h1').prepend('<span></span>');
		$('#nav ul li a').hover(
				function(){$(this).css({background: '#ffffff'})},
				function(){$(this).css({background: 'none'})}
		);
		
		if($('#hp-features').size() != 0){
				
			$('#hp-features').after('<div id="slides-nav">').cycle({
				speed: '1000',
				timeout: 12000, 
				fx:'scrollHorz',
				pager: '#slides-nav',
				pagerAnchorBuilder: function(idx, slide) {
					return '<li><a href="#slide'+idx+'">'+parseInt(idx+1)+'</a></li>';
				}														 
			});
		};
		
		$('#google_translate_element').hover(
			function(){
					$(this).append('<div id="translator-disclaimer"></div>');
					$('#translator-disclaimer').addClass('elem').html('NOTE: This feature is provided by Google. It is designed to help you understand the general content of a foreign language text, but does NOT deliver 100% accurate translations.');
				},
			function(){
					$('#translator-disclaimer').fadeOut(500).remove();
				}
		);
		// add opacity to linked images and initialize the tabs for product pages
		$('a img, a.sign-up').not('img.no-opacity').hover(function(){$(this).stop().animate({opacity: 0.8})},function(){$(this).stop().animate({opacity: 1})});
		//if($('#hp-features a').size() != 0){
			//$('#hp-features a').css({cursor: 'pointer'}).hover(
				//function(){$(this).children().filter('.imgfx').children('span').stop()
									//.animate({opacity: 0}, {queue: false, duration: 200}).animate({borderColor: '#dddddd'}, 200);},
				//function(){$(this).children().filter('.imgfx').children('span').stop()
									//.animate({opacity: 1}, {queue: false, duration: 200}).animate({borderColor: '#dddddd'}, 200);}
  			//);	
		//};
		
		
		// If there is a slideshow on the page load jQuery.cycle 
		if($('.slideshow2, .slideshow3').size() !=0){
			$.getScript('https://s3.amazonaws.com/giawellness/js/jquery.cycle.js', function(){
				$('.slideshow2, .slideshow3').cycle({fx:'fade',speed:3000,timeout:12000});
			});
		};
		
		// If there is a call to the colorbox js library load colorbox
		if($('.cb_img, a[rel="cb_grouped_img"], .btp_video, .cog_video, .pd_video, .raw_video, .ffl_video, .gbo_video, .pgb_video, .dt_video, .sfa_video, #companyOverview, #eventRecap, .studio_video, .tom_barrett, .co_video, .team_builder').size() != 0){ 
    		$("head").append("<link>");
    		css = $("head").children(":last");
    		css.attr({rel:  "stylesheet", type: "text/css", href: "/custom/DefaultTheme/css/colorbox.css", media: "screen"})

			$.getScript("/custom/DefaultTheme/js/colorbox/jquery.colorbox.js", function(){
				// If there is a hash in the URL on the video page, we're linking straight to a video so load it!
				if (location.href.indexOf("#") != -1) {
					if($('body').attr('id') == 'videos'){
        				var hash = location.href.substr(location.href.indexOf("#"));
						$.colorbox({iframe:true, innerWidth:560, innerHeight:345, href: $(hash).attr('href'), onCleanup:function(){ parent.$.fn.colorbox.close(); }})
					};
				};
				$('.cb_img, a[rel="cb_grouped_img"]').colorbox();
				$('.btp_video, .cog_video, .pd_video, .raw_video, .team_builder, .ffl_video, .gbo_video, .pgb_video, .dt_video, .sfa_video, #companyOverview, #eventRecap, .studio_video, .prod_test1_video, .co_video, .tom_barrett').colorbox({iframe:true, innerWidth:560, innerHeight:345});
				//$('').colorbox({iframe:true, innerWidth:560, innerHeight:345});
			});
		}; 
		// if there are videos present handle it
		if($('.video').size() !=0){
			function supports_video(){return !!document.createElement('video').canPlayType;};
			if(supports_video()){
					videos = $('video');
					$(videos).each(function(index, elem){
					elem.addEventListener('ended', function(){
							elem.currentTime = 0;
							elem.load()}, false);
						$(elem).bind('click', function(evt){
							elem.playbackRate = 0.5;
							if (elem.paused){
								//alert(elem.controls);
								elem.play();
							} else {
								elem.pause();
							};
						});
					});
			} else {
				// add back up code here for browsers that don't support html5 video
				$.getScript('http://ajax.googleapis.com/ajax/libs/swfobject/2.2/swfobject.js', function(){});
				$.getScript('https://s3.amazonaws.com/giawellness/js/ytplayer.js', function(){});
				
				video = $('.depvideo');
					video.each(function(){
						currentTab = $('#tabbed_interface').children('div').filter(':visible');
							if($('object').size() != 0){								
								newVideoId = currentTab.find('object').attr('id');
								currentTab.find('.depvideo').each(function(){$(this).prepend('<div id="'+newVideoId+'"></div>')});
								$('object').each(function(){$(this).remove()});
								currentTab.find('.videoLink').each(function(){
									$(this).show().bind('click', function(){
										var videoId = newVideoId;
										var params = {allowScriptAccess: 'always'};										
										var atts = {id: videoId};
										swfobject.embedSWF('http://www.youtube.com/v/'+videoId+'?enablejsapi=1&playerapiid='+videoId+'playId&rel=0&autoplay=1', videoId, '560', '340', '8', null, null, params, atts);
										$(this).hide();
									});
								});
							} else {
								$('.videoLink').show();
								$('.videoLink').each(function(){
									$(this).bind('click', function(){
										var videoId = $(this).prev('div').attr('id');
										var params = {allowScriptAccess: 'always'};
										//var atts = {id: videoId+'_wrapperId'};
										var atts = {id: videoId};
										swfobject.embedSWF('http://www.youtube.com/v/'+videoId+'?enablejsapi=1&playerapiid='+videoId+'playId&rel=0&autoplay=1', videoId, '560', '340', '8', null, null, params, atts);
									$(this).hide();
									});
								});
							};
					});
			}
		};
		$('#tabbed_interface').tabbed_interface();
		// add a print page feature to non shopping cart pages 
		if($('#sc').size() == 0){
			$('.page-title').prepend('<a class="print fr">Print page</a>');
			$('a.print').click(function(){
				$("head").append("<link>");
    			css = $("head").children(":last");
    			css.attr({rel: "stylesheet", type: "text/css", href: "https://s3.amazonaws.com/giawellness/css/print.css", media: "print"});
				window.print();
			});
		};
		// If there is content to be toggled initialize the controls
		if($('.toggleBox').size() !=0){
			$('.toggleBox').addClass('hidden').hide();
			$('.trigger').css({'cursor': 'pointer', 'color': '#0099CC'});
			$('.trigger').click(function(event) {
				event.preventDefault();
				switch( $('body').attr('class') ){
					case 'packages':
					if($(this).next('.toggleBox').is(':hidden')) { 
						$(this).html('Close Package Details').css({'background-position':'0 -12px', 'color': '#DF1E30'}).next('.toggleBox')
							.removeClass('hidden').fadeToggle(500).addClass('block');
						$(this).next('.toggleBox img').show(500);
					} else {
						$(this).html('View Package Details').css({'background-position':'0 5px', 'color': '#555'}).next('.toggleBox')
							.removeClass('block').fadeToggle(500).addClass('hidden');
					}
					break;	
					default:
					if($(this).next('.toggleBox').is(':hidden')) {
						$(this).css({'color': '#DF1E30'}).next('.toggleBox')
							.removeClass('hidden').addClass('block').slideDown(500);
					} else {
						$(this).css({'color': '#0099CC'}).next('.toggleBox')
							.removeClass('block').addClass('hidden').slideUp(500);
					}
					break;
				};
				var str = $(this).text();
				$(this).height($(this).height());
			});
			return true;
		};
		// If we are using jQuery to scroll nicely load the controls
		if($('.scroll').size() !=0){
			$('.scroll').each(function(){
				$(this).click(function(){
					var loc = $(this).attr('href').split('#')[1];
					var dest = $('#'+loc);
					if($(this).hasClass('top')) {
						$(this).closest('div').prev('.trigger').trigger('click');
						$('html,body').animate({scrollTop: $("#"+loc).offset().top},'slow');
					};
					$('html,body').animate({scrollTop: $("#"+loc).offset().top},'slow', 
						function(){
							if(dest.next('.toggleBox').hasClass('hidden')) {
								dest.trigger('click');
							};
						});
				});
			});
		};
});
(function($){
	$.fn.tabbed_interface = function(options){
		var defaults = {
			duration: 0,
			auto_rotate: false,
			delay: 12000
		}
		var options = $.extend(defaults, options);
		return this.each(function(){
			el = this;
			$(el).children('ul').removeClass('hidden');
			$(el).children('ul').children('li').children('a').click(function(evt){
				window.location.hash = $(this).attr('href')
				$('html,body').animate({scrollTop: $('#header').offset().top}, 0);
				$(el).children('div').fadeOut(options.duration/2).filter(this.hash).fadeIn(options.duration/2);
				
				function supports_video(){return !!document.createElement('video').canPlayType;};
				if(supports_video()){
					$(el).children('div').find('video').each(function(){this.pause()});
				} else {
					$(el).children('div').find('object').each(function(){
						if($(this)[0].getPlayerState() ==1){
							var newVideoId = $(this).attr('id');
							$(this).parent('div').prepend('<div id="'+newVideoId+'"></div>');
							$(this).next('.videoLink').show().bind('click', function(newVideoId){
								var videoId = newVideoId;
								var params = {allowScriptAccess: 'always'};
								var atts = {id: videoId};
								swfobject.embedSWF('http://www.youtube.com/v/'+videoId+'?enablejsapi=1&playerapiid='+videoId+'playId&rel=0&autoplay=1', videoId, '560', '340', '8', null, null, params, atts);
								$(this).hide();
							});
							$(this).remove();
						};
					});
				};
					$(el).children('ul').children('li').children('a').removeClass('active').filter(this).addClass('active');
	
					// This is where the old youtube video code went
					// return false so the link doesn't continue to be "clicked"
				return false;
			});
			
		$('#tabbed_interface a').not('#tabbed_interface ul li a').click(function(evt){
			var x = $(this).attr('href');
			
			$(el).children('ul').children('li').children('a').each(function(index, element){
				if( $(element).attr('href') === x ){ 
					$(element).trigger('click');
				};																
			});
		});
			
		// If there's not a hash in the URL open the first tab, otherwise open the tab specified in the URL	
		if(location.href.indexOf("#") == -1) {
			$(el).children('div').css('position','relative').not(':first').hide();
			$(el).children('ul').children('li').children('a').first().addClass('active');
		} else {
			$(el).children('div').hide().filter(location.hash).show();
			$(el).children('ul').children('li').children('a').removeClass('active');
		    $(el).children('ul').children('li').find('a[href="'+location.hash+'"]').addClass('active');	
		};	
		if(options.auto_rotate) {
			$(el).bind('tabTimer', function(){
				setTimeout(function(){
					var my_id = 
						$(el).children('div').not(':hidden').next().attr('id') ||
						$(el).children('div').not(':hidden').siblings('div').first().attr('id');
						$(el).children('ul').children('li').children('a[href="#'+my_id+'"]').click();	
						$(el).trigger('tabTimer');
					},options.delay);
				});
				$(el).trigger('tabTimer');
			};
		});
	};
})(jQuery);
