/**
 * Westco specific functionality
 */
$(function(){
	Menu.init();
	Various.init();
});

var Various = {
	init: function(){
		this.multipleBgs();
		this.resizeCategories();
		this.resizeProductListings();
		this.newProductsRotate();
		this.makeFAQ();
		this.makeLightbox();
		this.searchHandler();

		$('.submitForm').click(function(){
			$(this).closest('form').submit();
			return false;
		});
	}, 
	
	searchHandler: function(){
		$('#siteSearch').focus(function(){
			if($(this).val() == 'Search Website') $(this).val('');
		}).blur(function(){
			if($(this).val() == '') $(this).val('Search Website');
		});
		$('#siteSearchForm').submit(function(){
			if($('#siteSearch').val() == 'Search Website') {
				return false;
			}
			if($('#siteSearch').val() == '') {
				return false;
			}
		});
	}, 
	
	makeLightbox: function(){
		$(".lightbox").lightbox();
	},
	
	makeFAQ: function(){
		$('div#faqAccordion').accordion({
			autoHeight: false,
			navigation: true});
	},
	
	multipleBgs: function(){
		//$.fn.multipleBgApplyCss();
	}, 
	
	resizeCategories: function(){
		var categoryHeight = $('div#leftMenuWrapper').height();
		var calulatedHeight = ($('div#wrapper').height() - ($('div#footer').height() + $('div#header').height()));
		if(categoryHeight <= calulatedHeight) $('#catalogueCategories #leftMenuWrapper').css({height: calulatedHeight});
	}, 
	
	resizeProductListings: function(){
		var productHeight = 200;
		$('ul.productList li').each(function(){
			if($(this).height() > productHeight) productHeight = $(this).height();
		});
		$('ul.productList li').css({height: productHeight});	
	},

    setZIndex: function(curr, next, opts) {
        $('#newProductsRotate div').css({zIndex: 0});
        $(next).css({zIndex: 99});
    },

	
	newProductsRotate: function(){
		if($('div#newProductsRotate').length){
            $('div#newProductsRotate div:not(:first)').css({opacity:0});
			$('div#newProductsRotate').cycle({fx: 'fade', speed: 1500, before: Various.setZIndex});
		}
	}
};

var Menu = {
	
	time: '',
	
	init: function(){
		this.setup();
	},
	
	setup: function(){
		var menus = ["home", "about", "videos", "downloads", "news", "faq", "contact"];
		for(var i in menus){
			//alert(menus[i]);
			Menu.positionDropDown(menus[i]);
			Menu.dropDown(menus[i]);
		}
	},

	positionDropDown: function(parent) {
		if($('ul.submenu').length > 0){
			var theLink = $('a.page-'+parent).parent().offset();
			var theMenuOffset = $('#navigation').offset(); 
			$('ul#'+parent+'-submenu').css({bottom: theLink.bottom, left: (theLink.left - theMenuOffset.left) }).hide();
		}
	},

	dropDown: function(parent){
		$('a.page-'+parent).mouseenter(function(){
			clearTimeout(Menu.time);
			if($('ul.submenu:visible').length > 0 && !$('ul#'+parent+'-submenu').is(':visible')){
				$('ul.submenu:visible').slideUp('fast', function(){
					$('a.hover').removeClass('hover');
					if($('ul#'+parent+'-submenu').length > 0){
						$('a.page-'+parent).addClass('hover');
						$('ul#'+parent+'-submenu').slideDown('fast');
					}
				});
			}
			else {
				if($('ul#'+parent+'-submenu').length > 0){
					$('a.page-'+parent).addClass('hover');
					$('ul#'+parent+'-submenu').slideDown('fast');
				}
			}
		});
		
		$('a.page-'+parent).mouseleave(function(){
			Menu.time = setTimeout('Menu.hideMenu()', 1000);
		});
		
		$('ul#'+parent+'-submenu').mouseleave(function(){
			Menu.time = setTimeout('Menu.hideMenu()', 1000);
		}).mouseenter(function(){
			clearTimeout(Menu.time);
		});
	}, 
	
	hideMenu: function(){
		$('ul.submenu:visible').slideUp('fast', function(){
			$('a.hover').removeClass('hover');
		});
		clearTimeout(Menu.time);
	}

};

