var HOLTS = {};

$(function(){
		jQuery.HOLTS.navigation();
		jQuery.HOLTS.home_title();
		jQuery.HOLTS.home_animation();
		
		//jQuery.HOLTS.home_buttons('#homeButtons .button', 195, 87);
		//jQuery.HOLTS.home_buttons('#homeMid .button', 262, 182);
				
		jQuery.HOLTS.page_title();
		jQuery.HOLTS.product_title('#productGroup');
		jQuery.HOLTS.parent_title();
		jQuery.HOLTS.product_image('#productGroup');
		jQuery.HOLTS.product_categories('#productGroup');
		jQuery.HOLTS.product_nav('#productGroup');
		jQuery.HOLTS.multiTabs('#faqsPage','#faqsNav', '.section');
		jQuery.HOLTS.multiTabs('#newsPage','#newsNav', '.section');
		jQuery.HOLTS.multiTabs('#hintsPage','#hintsNav', '.section');
		jQuery.HOLTS.Compactor('.faqSection', '.question', '.answer');
		jQuery.HOLTS.multiTabs('#distributorsPage','#distributorsNav','.region' );
		jQuery.HOLTS.Compactor('#distributorsPage','h4','div.distributor');
		jQuery.HOLTS.videos();		
				
		jQuery.HOLTS.sitemapCompactor('#sitemap', '.individualProductsBtn', '.individualProducts');
				
		//jQuery.HOLTS.product_ahah();
		jQuery.HOLTS.product_selector('#productGroup');
		
		jQuery.HOLTS.link_target('.xLink', '_blank');
		
		jQuery.HOLTS.modal_hijack_link(document, '.enlarged', 600, 600, true);
		jQuery.HOLTS.modal_hijack_link(document, '.printView', 600, 600);
		jQuery.HOLTS.modal_hijack_link(document, '.gasTipsButton', 591, 570);
		
		$('#pageSubNav').css({ opacity: '0.95'});
});

jQuery.HOLTS = {

	navigation : function() {
		$('#nav').find('li').hover(
			function(){
				$(this).children('ul').css({display: 'block', opacity: '0.95' });
			},
			function() {
				$(this).children('ul').css({display: 'none'});
			}
		);
	},
	
	multiTabs : function(wrapper, linkList, items) {
		$(wrapper).find(items).css({display: 'none'});
		var valid = $(wrapper).find(linkList);
		if (valid.length > 0) {
		
			var links = $(linkList).find('a');
			
			$(links).click(function() { 
				var href = $(this).attr('href');
				$(linkList).find("a").not(this).removeClass("select");
				$(this).addClass(" select");
				$("div" + items +":visible").not(href).css({display: 'none'});
				$(href).css({display: 'block'});
				return false;
			});
			
			var hashButton = document.location.hash;
			// THE LINK HAS A CLASS THAT IS THE SAME AS THE ID TO CLICK TO // REPLACE HASH WITH DOT
			var buttonClass = hashButton.replace ('#','.');
			
			var button = $(linkList).find(buttonClass);
			
			if (button.length > 0) {
				button.click();
			} else {
				$(linkList).find('a:first').click();
			}
		}
	},
	
	Compactor : function ( list, btnLink, targetItem ) {
		
		$(list).find(targetItem).css({display: "none"});
		$(list).find(btnLink).addClass("link").hover( function () {
			$(this).addClass(" linkOver");
		}, function () {
			$(this).removeClass("linkOver");
		}).toggle( function () {
			$(this).siblings(targetItem+":visible").slideUp("fast");
			$(this).siblings(btnLink).removeClass("linkOn");
			$(this).addClass(" linkOn").next(targetItem+":hidden").slideDown("fast");
			
		}, function () {
			$(this).removeClass("linkOn").next(targetItem+":visible").slideUp("fast");
		});
		
		$(list).find(btnLink+':first').click();
	},	
	
	sitemapCompactor : function ( list, btnLink, targetItem ) {
		
		$(list).find(targetItem).css({display: "none"});
		$(list).find(btnLink).addClass("link").hover( function () {
			$(this).addClass(" linkOver");
		}, function () {
			$(this).removeClass("linkOver");
		}).toggle( function () {
			$(targetItem+":visible").slideUp("fast");
			
			$('.linkOn').text('See Individual Products');
			$(btnLink).removeClass("linkOn");
			$(this).text('Hide Individual Products');
			$(this).addClass(" linkOn").next(targetItem+":hidden").slideDown("fast");
			
		}, function () {
			$(this).text('See Individual Products');
			$(this).removeClass("linkOn").next(targetItem+":visible").slideUp("fast");
		});
		
	},	
	
	link_target : function (ID, target) {
		$(document).find('a'+ID).each(
			function (){
				$(this).click(
					function (){
						var xLink = $(this).attr('href');
						window.open(xLink, target);
						return false;
					}
				);
			}
		);		
	},
	
	// CREATE A NEW METHOD AND PROPERTIES
 	modal_hijack_link : function( wrapper, trigger, width, height, download) 
	{
		// START AT THE TRIGGER <div> GET ALL THE CHILD <a> TAGS AND BUILD AN ARRAY TO HIGHJACK 
		$(wrapper).find('a'+trigger)
		
		// BY PASS ALL .delete BUTTONS TO PREVENT HIJACKLINK
		.not('.delete')
		
		// PREPARE FOR THE ONCLICK EVENT
		.click(function() {  
		
			// BUILD WIDTH AND HEIGHT FOR MODAL
			var W = $(window).width();
			var WH = $(window).height();
			var H = $('html').height();
			
			// HELPER FOR MODAL TO WORK ON SHORT PAGES
			if(H < WH){ 
				H = WH;
			}

			// INJECT THE MODAL INTO THE BODY TAG
			$("body").append('<div id="modal"></div>'); 
			
			// STYLE THE MODAL 
			$("#modal").css({display: "block", width: W, height: H, opacity: 0.7});		 

			// CREATE THE AHAH WINDOW TO EDIT THE RESULTS IN
			$("body").append('<div id="modalBox"></div>'); 
			
			// GET THE LINK TO HIJACK AS A STRING
			var urlJack = $(this).attr('href'); 
			var modalTitle = $(this).attr('rel');
			// STYLE THE AHAH WINDOW AND TRIGGER THE CANEL HIJACK
			var L = ($(window).width() - width ) / 2; // CENTER THE MODAL WINDOW IN THE VIEWPORT
			
			var modal = '<div id="modalTop">';
			if (modalTitle) {
				modal += '<h1>'+modalTitle+'</h1>';
			}
			modal += '<a id="cancelModal">Cancel</a>';
			modal += '</div><iframe src="" />';
			
			$("#modalBox").css({left: L, width: width+2}).append(modal);
			$('#modalBox iframe').attr({
				src: urlJack,
				width: width,
				height: height,
				frameborder: 0
			});
			
			if (download === true) {
				$("#modalBox").append('<div id="downloadExplaination"><a href="#">Download Image</a><p>To download image right click on image and select &quot;save as&quot;.</p></div>');
			
				$('#downloadExplaination p').css({display: 'none'});
				$('#downloadExplaination a').click(
					function () {
						var shown = $('#downloadExplaination p').css('display');
						
						if (shown === 'none') {
							$('#downloadExplaination p').css({display: 'block'});
						} else {
							$('#downloadExplaination p').css({display: 'none'});
						}
						
						return false;						
					}
				);
			}
			
			
			jQuery.HOLTS.modal_cancel();
							
			// FOR IE6
			jQuery.each(jQuery.browser, function(i, val) {
				if(i=="msie" && jQuery.browser.version < 7) {
					$('html, body').animate({scrollTop:0}, 300); 
				}
			});
		// DISABLE THE <a> TAG CUZ WE'RE HIJACKIN' IT
		return false; 
		
		});
		
	},
	
 	// CREATE A NEW METHOD AND PROPERTIES
	modal_cancel : function()
	{
		$('#cancelModal').click(function(){
			$("#modalBox").remove();	
			$("#modal").css({display: 'none'});
		});
	},
	
	
	home_title : function() {
		var homeTitle = $('#homeTop h1').text();
		var title = homeTitle.replace('&', '%26');
		var flashvars = 'titleVal='+title;
				
		var holder = $('#homeTop h1');
		
		if (holder.length > 0) {
			$.post("/utilities/flash", { src:'/flash/homeTitle.swf', width:300, height:32, flashvars: flashvars },
				function(data){
					$(holder).html(data);
					$(holder).css({display: "block"});
				}
			);
		}
	},
	
	home_animation : function() {
		var holder = $('#homeAnimations');
		
		if (holder.length > 0) {
			$.post("/utilities/flash", { src:'/flash/homeAnimations.swf', width:495, height:412, flashvars: 'xmlPath=/flash/xml/homeContent.xml' },
				function(data){
					$(holder).html(data);
				}
			);
		}
	},
	
	home_buttons : function (ID, width, height) {
		
		$(ID).each(
			function () {
				var holder = $(this);
				var href = $(this).find('a').attr('href');
				var rel = $(this).find('a').attr('rel');
				
				if (rel && rel !== '') {
					
					$(holder).css({display: 'none'});
					$(holder).css({background: 'transparent'});
					
					$(this).find('a').click(function (){ return false; });
					
					$(holder).click( 
						function () {
							window.open(href, '_self');	
						}
					);
					
					$.post("/utilities/flash", { src:'/flash/home-buttons/'+rel, width:width, height:height},
						function(data){
							$(holder).html(data);
							$(holder).css({display: 'block'});
							$(holder).find('embed').css({cursor: 'pointer'});
						}
					);
				}
			}
		);
	},
	
	parent_title : function() {
		var parentTitle = $('#pageParentTitle').text();
		var title = parentTitle.replace('&', '%26');
		var flashvars = 'titleVal='+title;
				
		var holder = $('#pageParentTitle');
		$.post("/utilities/flash", { src:'/flash/pageDirectoryTitle.swf', width:75, height:600, flashvars: flashvars },
			function(data){
				$(holder).html(data);
				$(holder).css({display: "block"});
			}
		);
	},	
	
	page_title : function() {
		var pageTitle = $('#pageTitle').text();
		var title = pageTitle.replace('&', '%26');
				
		var flashvars = 'titleVal='+title;
				
		var holder = $('#pageTitle');
		$.post("/utilities/flash", { src:'/flash/pageTitle.swf', width: 785, height:35, flashvars: flashvars },
			function(data){
				$(holder).html(data);
				$(holder).css({display: "block"});
			}
		);
	},
	
	product_title : function(ID) {
		//var featured = $(ID).find('h3:first');
		var product_counter = 0;
		
		$(ID).find('h3').each(
			function (){
				var h3 = $(this).find('a').text();
				var h3span = $(this).find('span').text();
				
				var title = h3.replace('&', '%26');
				var describer = h3span.replace('&', '%26');
								
				var flashvars = 'titleVal='+title+'&titleDescriberVal='+describer+'&featured_val='+product_counter;
				
				/*if ($(this) != featured) {
					flashvars += '&featured=true';	
					alert('first');
				}*/
				
				var holder = $(this);
				$.post("/utilities/flash", { src:'/flash/productTitle.swf', width: 550, height: 35, flashvars: flashvars },
					function(data){
						$(holder).append(data);
						$(holder).css({display: "block"});
					}
				);
				
				product_counter++;
			}
		);		
	},
	
	product_image : function(ID) {
		$(ID).find('.productImage').each(
			function (){
				var src = $(this).find('img').attr('src');
				var flashvars = 'src='+src;
				
				var holder = $(this);
				$.post("/utilities/flash", { src:'/flash/productImage.swf', width: 215, height:215, flashvars: flashvars },
					function(data){
						$(holder).append(data);
						$(holder).css({display: "block"});
					}
				);
			}
		);		
	},
	
	product_categories : function(ID) {
		$(ID).find('.productContentWrap').find('.productDescriptions:first').css({display: 'block'});
		$(ID).find('.productContentWrap').find('.productContentNav>ul>li>a:first').addClass('select');
		$(ID).find('.productContentWrap').find('.productContentNav>ul>li>a').not('.faq').not(".testimonial").click(
			function(){
				var href = $(this).attr('href');
				
				var pos = (href+'').indexOf('#');   
				var ID = href.substr(pos);
				
				$(this).parents('.productContentWrap').find('.productContentNav>ul>li>a').removeClass('select');
				$(this).addClass('select');
				$(this).parents('.productContentWrap').find('.productDescriptions').css({display: 'none'});
				$(this).parents('.productContentWrap').find(ID).css({display: 'block'});
				return false;
			}
		);		
	},
	
	product_nav : function (ID) {
		$(ID).find('.productContentNav').each(
			function(){		
				
				var first = $(this).find('a:first');
				var last = $(this).find('a:last');
			
				first.addClass('PCNFirst');
				last.addClass('PCNLast');
				
				if($(first).hasClass('PCNLast')) {
					$(this).hide();			 
				}
				
			}
		);
	},
	
	product_selector : function (ID) {
		$(ID).find('.productSelector').each(
			function(){
				var holder = $(this);
				var selector_id = $(this).attr('id');
				var label = $(this).find('h5').text();
				var current_variant = $(this).find('h6').text();
				var selector = '<label for="product_'+selector_id+'">'+label+': </label>'; 
				selector +=	'<select name="product_'+selector_id+'" id="product_'+selector_id+'">';	
				
				$(this).find('a').each(
					function() {
						var href = $(this).attr('href');
						var option = $(this).text();
						
						selector += '<option value="'+href+'" ';
						if (option == current_variant){
							selector += 'selected="selected"';						
						}
						selector += '>'+option+'</option>';
					}
				);
				
				selector +=	'</select>';
				$(holder).html(selector);
				jQuery.HOLTS.product_ahah('#product_'+selector_id);
			}
		);
	},	
	
	product_ahah : function (selector_id) {
		var ID = $(selector_id).attr('id');
		var holder = $(selector_id).parents('.productItem');
		
		$(selector_id).bind('change', 
			function(){
				var selector = document.getElementById(ID);
				var selector_value = selector.options.selectedIndex;
				var href = selector.options[selector_value].value;	
				
				$.post(href, {},
					function(data){
						var product = $(data).find('#product').html();
						$(holder).html(product);
						
						jQuery.HOLTS.product_title(holder);
						jQuery.HOLTS.product_image(holder);
						jQuery.HOLTS.product_categories(holder);
						jQuery.HOLTS.product_nav(holder);
						jQuery.HOLTS.product_selector(holder);
						jQuery.HOLTS.modal_hijack_link(document, '.enlarged', 600, 600, true);
					}
				);
				return false;				   
			}
		);
	},
	
	videos : function () {
		var holder = ('body#videos #content #videosContainer');
		
		$.post("/utilities/flash", { src:'/flash/videoPlayer.swf', width: 750, height:425, flashvars: 'xmlPath=/flash/xml/videos.xml' },
			function(data){
				$(holder).html(data);
				$(holder).css({display: "block"});
			}
		);
		
		
	}
						  
						  
					
	
	
	
};