
excel = {}

$(function(){
	excel.view_large_image();
	excel.side_nav('.level_1', '.level_2');
	excel.side_nav('.level_2', '.level_3');
	excel.nav_action_1();
	if(location.protocol !== 'https:'){
		excel.nav_action_email_list();
	}
	excel.show_cc_code();
	excel.no_eta();
	excel.email_notification();
	excel.hijack_popup('.bike_kit_desc');
	excel.hijack_popup('.wheel_desc');
	excel.sizing_chart();
	excel.shipping_rates_popup()
	excel.shop_by_brand();
	excel.shop_by_category();
	excel.search_box();
	$('.search_brand_link').click(function(){// search results trigger shop_by_brand
		$('.shop_by_brand_title').click();
	});
	$('.search_cat_link').click(function(){// search results trigger shop_by_category
		$('.shop_by_category_title').click();
	});
	excel.filter_brand();
	excel.clear_filter_brand();
	excel.sort_brand();
	excel.sort_price();
	excel.payment_methods();
});

/**
* hoverIntent r6 // 2011.02.26 // jQuery 1.5.1+
* <http://cherne.net/brian/resources/jquery.hoverIntent.html>
* 
* @param  f  onMouseOver function || An object with configuration options
* @param  g  onMouseOut function  || Nothing (use configuration options object)
* @author    Brian Cherne brian(at)cherne(dot)net
*/
(function($){$.fn.hoverIntent=function(f,g){var cfg={sensitivity:7,interval:100,timeout:0};cfg=$.extend(cfg,g?{over:f,out:g}:f);var cX,cY,pX,pY;var track=function(ev){cX=ev.pageX;cY=ev.pageY};var compare=function(ev,ob){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);if((Math.abs(pX-cX)+Math.abs(pY-cY))<cfg.sensitivity){$(ob).unbind("mousemove",track);ob.hoverIntent_s=1;return cfg.over.apply(ob,[ev])}else{pX=cX;pY=cY;ob.hoverIntent_t=setTimeout(function(){compare(ev,ob)},cfg.interval)}};var delay=function(ev,ob){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);ob.hoverIntent_s=0;return cfg.out.apply(ob,[ev])};var handleHover=function(e){var ev=jQuery.extend({},e);var ob=this;if(ob.hoverIntent_t){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t)}if(e.type=="mouseenter"){pX=ev.pageX;pY=ev.pageY;$(ob).bind("mousemove",track);if(ob.hoverIntent_s!=1){ob.hoverIntent_t=setTimeout(function(){compare(ev,ob)},cfg.interval)}}else{$(ob).unbind("mousemove",track);if(ob.hoverIntent_s==1){ob.hoverIntent_t=setTimeout(function(){delay(ev,ob)},cfg.timeout)}}};return this.bind('mouseenter',handleHover).bind('mouseleave',handleHover)}})(jQuery);

excel = {

	nav_action_1 : function()
	{
		function showNav(){  $(this).children('ul').slideDown('fast'); }
		function hideNav(){ $(this).children('ul').slideUp('fast'); }
		$('.action_ci').hoverIntent({
			over: showNav,
			timeout: 500,
			out: hideNav
		});
		$('.action_ci').find('a:first').click(function(event){
			event.preventDefault();
		});
	},

	nav_action_email_list : function()
	{
		$('.action_el').find('a').toggle(function(){
			$('.email_list:visible').remove();
			$('#container').prepend('<div class="wrap_list"><div class="email_list"><p class="loading">loading...</p></div></div>');
			$('.email_list').fadeIn('slow');
			$('.email_list').load('/inform.asp?page=5 .section', function(){
				$('.email_list').prepend('<div class="close_elist">Close Window</div>');
			});
		},function(){
				$('.email_list').fadeOut('slow', function() { $('.wrap_list').remove(); });
		});
		$('#container').on('click', '.close_elist', function() { $('.action_el').find('a').click(); });

	   $('#container').on('click', '.submit_email', function() {  

			$(".error").hide();
			var hasError = false;
			var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;

			var emailaddressVal = $("#email_address").val();
			if(emailaddressVal == '') {
				$("#email_address").after('<span class="error">Please enter your email address.</span>');
				hasError = true;
			}

			else if(!emailReg.test(emailaddressVal)) {
				$("#email_address").after('<span class="error">Enter a valid email address.</span>');
				hasError = true;
			}

			if(hasError == true) { return false; }
			
			else if(hasError == false) {
				var el_height = $('.email_list').height();
				$('.email_list').animate({
					top: '-'+(el_height+70)+'px'
				}, 700, function(){

					var form_data = $('.submit').parent('form').serialize();
					var uri_decode = decodeURIComponent(form_data);

					$.post("/inform.asp?page=5", uri_decode, function(data){

						var content = $(data).find('.section');
						$('.email_list').empty().append(content);
						
						$('.email_list').prepend('<div class="close_elist">Close Window</div>');
	
						$('.email_list').animate({
							top: '0px'
						}, 700 );
						
					});				
					
				});
				event.preventDefault();
			}
	 
		});		
		
	},

	view_large_image : function()
	{
		// preload the image
		$(window).on('load', function() { 
			if($('.view_large_image').length){ // check for large image and exit if none
				hijack = $('.view_large_image').attr('href');
				img_width = $('.view_large_image').attr('data-width');
				img_height = $('.view_large_image').attr('data-height');
				// 624 is the width of the .main_col_wrapper minus the image width plus padding then divide by two to center it on the page.
				position_left = (624 - img_width) / 2;
				$('.main_col_wrapper').prepend('<div style="display: none;left:'+position_left+'px;width:'+img_width+'px; height:'+img_height+'px" id="wrap_large_image"><div class="closeLargeImg"><img src="images/close_w.jpg"> Close Window</div><img src="'+hijack+'"></div>');
			}
		});

		$('.view_large_image').click(function(event){
			$('.main_col_wrapper').css('z-index', '9999'); // css z-index for IE8 fix
			$('#wrap_large_image').fadeIn(800);
			event.preventDefault();
		});
		$('.main_col_wrapper').on('click', '#wrap_large_image', function() {
			$(this).fadeOut('slow');
			$('.main_col_wrapper').css('z-index', '444') // IE8 z-index fix
		});
	},

	hijack_popup : function(hook)
	{
		$(hook).find('a').click( function(event) {
			$('.wrap_wdb:visible').fadeOut('fast', function(){ $(this).remove(); });
			hijack = $(this).attr('href');
			$(this).parent('p').parent(hook).before('<div class="wrap_wdb"><div class="wdisplay_box"></div></div>');
			$('.wdisplay_box').load(hijack+' .wdisplay', function(){
				$('.wrap_wdb').fadeIn();
				$('.wdisplay_box').prepend('<div class="close"><img src="images/close_w.jpg"> Close Window</div>');
			});
			event.preventDefault();
		});
		$('.article').on('click', '.close', function() { $(this).parent('div').fadeOut('fast', function(){ $(this).remove(); }); });	
		$('.article').on('click', '.wdisplay_large_img', function(){ $(this).fadeOut('fast', function(){ $(this).remove(); }); });
	},

	side_nav : function(parent, child )
	{
		function showNav(){  $(this).children(child).fadeIn('fast'); }
		function hideNav(){ $(this).children(child).fadeOut('fast'); }
		$(parent).find('li').hoverIntent({
			over: showNav,
			timeout: 500,
			out: hideNav
		});
		$('.level_1>li').find('a:first').not('.no_child').click(function(event){
			event.preventDefault();
		});
		$('.level_2>li').find('a.has_child').click(function(event){
			event.preventDefault();
		});
	},

	show_cc_code : function()
	{
		$('.show_cc_code').click(function(){
			$('.wrap_cc_code').css({display: 'block'});
		});
		$('.wrap_cc_code').click(function(){
			$(this).css({display: 'none'});
		});	
	},

	no_eta : function()
	{
		$('.no_eta').live('click', function(event) {
			$(this).parent('td').prepend('<div style="position: relative;"><div class="eta_wrap"><p class="loading">loading...</p></div></div>'); 
			$('.eta_wrap').load('eta.html .etaw', function(){
				$('.etaw').prepend('<div class="closeLargeImg"><img src="/images/close_w.jpg"> Close Window</div>');
			});
			event.preventDefault();
		});
		$('.eta_wrap').live('click', function() {
			$(this).parent('div').remove();
		});	
	},

	payment_methods : function()
	{
	$('.footer_payment_method').prepend('<div class="close">Close</div>');	$('.footer_payment_method_link').click(function(event){
			$('.footer_payment_method').slideDown('slow');
	      	event.preventDefault();
		});
		$('#footer').on('click', '.footer_payment_method', function() {
			$('.footer_payment_method').slideUp('slow');
		});
	},
	
	email_notification : function()
	{
		$('.email_notification_link').click(function(event){
			signup_link = $(this).attr('href');
			$(this).parent('td').prepend('<div style="position: relative;"><div class="email_notify_wrap"><p class="loading">loading...</p></div></div>'); 
			$('.email_notify_wrap').load(signup_link+' .section', function(){
				$('.section').prepend('<div class="close_popup">Close Window</div>');
			});
		event.preventDefault();
		});
		$('.article').on('click', '.close_popup', function() {
			$('.email_notify_wrap').remove();
		});

		$('#container').on('click', '.submit_email_notification', function(event) {  

			$(".error").hide();
			var hasError = false;
			var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;

			var emailaddressVal = $("#email").val();
			if(emailaddressVal == '') {
				$("#email").after('<span class="error">Please enter your email address.</span>');
				hasError = true;
			}

			else if(!emailReg.test(emailaddressVal)) {
				$("#email").after('<span class="error">Enter a valid email address.</span>');
				hasError = true;
			}

			if(hasError == true) { return false; }
			
			else if(hasError == false) {
				$('.email_notify_wrap').fadeOut('700', function(){
					var form_data = $('.submit_email_notification').parent('form').serialize();
					var uri_decode = decodeURIComponent(form_data);
					$.post("/inform.asp?page=17", uri_decode, function(data){
						var content = $(data).find('.section');
						$('.email_notify_wrap').empty().append(content).prepend('<div class="close_popup">Close Window</div>').fadeIn('700');
					});				
				});
				event.preventDefault();
			}
	 
		});	

	},

	sizing_chart : function()
	{
		$('.vendor_sizing_link').find('a').click(function(event){
			hijack = $(this).attr('href');
			$('.vendor_sizing_link').append('<div id="wrap_sizing"><p class="loading">loading...</p></div>');
			$('#wrap_sizing').fadeIn('slow');
			$('#wrap_sizing').load(hijack+' #sizing_charts', function(){
				$('#wrap_sizing').prepend('<div class="close_sizing">Close Window</div>');
			});
		event.preventDefault();
		});
		$('.vendor_sizing_link').on('click', '.close_sizing', function() { $('#wrap_sizing').fadeOut('slow', function(){ $(this).remove(); }) });
	},
	
	shipping_rates_popup : function()
	{
		$('.ups_ground_chart_link').click(function(event){
			$('.wrap_popup:visible').remove();
			shipping_chart = $(this).attr('href');
			$('.shipping_links').append('<div class="wrap_popup"><div class="close_popup">Close</div><img src="'+shipping_chart+'"></div>');
		event.preventDefault();
		});
		$('.section').on('click', '.wrap_popup', function() {
			$(this).remove();
		});
	},	

	shop_by_brand : function()
	{
		$('.shop_by_brand_title').toggle(function(){
			$('.close_category:visible').click(); // close other visible navs
			$('.shop_by_brand_title').addClass('slider_on');
			$('#nav_brand').css({display: 'block'});
			$('.wrap_brand').prepend('<div class="close_brand">Close Window</div>').fadeIn('fast');
		}, function(){
			$('.shop_by_brand_title').removeClass('slider_on');
			$('.wrap_brand').fadeOut('fast', function(){
				$('#nav_brand').css({display: 'none'});
			});
		});
		$('.wrap_brand').on('click', '.close_brand', function() { $('.close_brand').remove(); $('.slider_on').click(); });
	},

	shop_by_category : function()
	{
		$('.shop_by_category_title').toggle(function(){
			$('.close_brand:visible').click(); // close other visible navs
			$('.shop_by_category_title').addClass('slider_on');
			$('#nav_category').css({display: 'block'});
			$('.wrap_category').prepend('<div class="close_category">Close Window</div>').fadeIn('fast');
		}, function(){
			$('.shop_by_category_title').removeClass('slider_on');
			$('.wrap_category').fadeOut('fast', function(){
				$('#nav_category').css({display: 'none'});
			});
		});
		$('.wrap_category').on('click', '.close_category', function() { $('.close_category').remove(); $('.slider_on').click(); });
	},

	view_business_map : function(latitude, longitude)
	{
		var business = new google.maps.LatLng(latitude,longitude);

		var mapOptions = {
			center: business,
			zoom: 14,
			mapTypeId: google.maps.MapTypeId.ROADMAP
		};
		var map = new google.maps.Map(document.getElementById("map"), mapOptions);

		var streetViewService = new google.maps.StreetViewService();
		streetViewService.getPanoramaByLocation(business, 50, function (streetViewPanoramaData, status) {
			point = streetViewPanoramaData.location.latLng;
			var heading = google.maps.geometry.spherical.computeHeading(point,business);
			var panoramaOptions = {
				position: business,
				pov: {
					heading: heading,
					pitch: 10,
					zoom: 1
				},
				addressControl: false
			};
			var panorama = new  google.maps.StreetViewPanorama(document.getElementById("pano"),panoramaOptions);
			map.setStreetView(panorama);
		});

	},

	search_box : function()
	{
		$('#header').append('<div class="search_box"><form method="post" action="http://www.excelsports.com/products.asp"><input name="keywords" size="18" type="text" id="sfieldbox" value="Search"><button type="submit" id="sfieldicon">Go</button></form></div>');

		var $search = $('#sfieldbox');
		original_val = $search.val();
		$search.focus(function(){
			if($(this).val()===original_val){
				$(this).val('');
			}
		})
		.blur(function(){
			if($(this).val()===''){
				$(this).val(original_val);
			}
		});
	},

	filter_brand : function()
	{
		if(window.location.hash) {
				var hash = window.location.hash.substring(1);
				filter_id = hash;
				show_filter_id = '#'+hash.substring(1);
				$(show_filter_id).addClass('on');
				$('#wrap_items').find('.item_row').not(filter_id).slideUp('slow');
				$('.reset_brand_filter').removeClass('on');
		}
		var get_li = $('#filter_by_brand').find('li'); // cache
		get_li.click(function(){
			show_filter_id = '.'+$(this).attr('id');
			window.location.hash = show_filter_id;
			$(show_filter_id).slideDown('slow').end();		
			$(this).addClass('on');
			$('.reset_brand_filter').removeClass('on');
			get_li.not($(this)).removeClass('on').each(function(){
				filter_id = '.'+$(this).attr('id');
				$(filter_id).slideUp('slow');
			});
		});
	},

	clear_filter_brand : function()
	{
		$('.reset_brand_filter').click(function(){
			$(this).addClass('on');
			$('#filter_by_brand').find('li').removeClass('on');
			$('#wrap_items').find('.item_row').slideDown('slow');
			window.location.hash = "";
		});
	},

	sort_price : function()
	{
		$('.sortLowHigh').click(function(){
			$(this).removeClass('off');
			$('.sortHighLow, .wrapSortBrand>ul>li').addClass('off');
			$(".item_row").tsort(".item_price",{order:"asc"});
		});
		$('.sortHighLow').click(function(){
			$(this).removeClass('off');
			$('.sortLowHigh, .wrapSortBrand>ul>li').addClass('off');
			$(".item_row").tsort(".item_price",{order:"desc"});
		});
	},
	
	sort_brand : function()
	{
		$('.sortAZ').click(function(){
			$(this).removeClass('off');
			$('.sortZA, .wrapSortPrice>ul>li').addClass('off');
			$(".item_row").tsort(".item_description",{order:"asc"});
		});
		$('.sortZA').click(function(){
			$(this).removeClass('off');
			$('.sortAZ, .wrapSortPrice>ul>li').addClass('off');
			$(".item_row").tsort(".item_description",{order:"desc"});
		});
	}

}
