
var BIKERSEDGE = {};

$(function() {
	jQuery.BIKERSEDGE.mainNav();
	jQuery.BIKERSEDGE.TurnItOn("#navigation");
 	//jQuery.BIKERSEDGE.bikejack('#bicycleListType'); 
 	jQuery.BIKERSEDGE.hijackLink('#bicycleCloseoutsList .model'); 
 	jQuery.BIKERSEDGE.hijackLink('#bicycleList .model'); 
	jQuery.BIKERSEDGE.storeGallery();
	jQuery.BIKERSEDGE.showHeaderHours();
	jQuery.BIKERSEDGE.showFooterHours();
	jQuery.BIKERSEDGE.showBrand();
	jQuery.BIKERSEDGE.equipNav();
	jQuery.BIKERSEDGE.scrollBikeType('#proRoad');
	jQuery.BIKERSEDGE.slideSignup();

	$('.bicycleList').find('td').hover(function(){
		$(this).parent('tr').find('div.listThumb img').css('visibility', 'visible');	
	}, function(){
		$(this).parent('tr').find('div.listThumb img').css('visibility', 'hidden');	
	});

/*	$('#filterBikeType dd>span').toggle(function(){
		var fc = $(this).attr('class');
		$(this).addClass('typeOff');
		//$(this).parent('dd').find('li>span').addClass('typeOff').end();
		$(this).parent('dd').find('li>span').each(function(){
			var xyz = $(this).attr('class');
			$(this).addClass('typeOff');
			$('.bicycleList').find('tr.'+xyz).css('display', 'none');
		});
		$('.bicycleList').find('tr.'+fc).css('display', 'none');
	}, function(){
		$(this).removeClass('typeOff');
		var fc = $(this).attr('class');
		//$(this).parent('dd').find('li>span').removeClass('typeOff').end();
		$(this).parent('dd').find('li>span').each(function(){
			$(this).removeClass('typeOff');
			var xyz = $(this).attr('class')
			$('.bicycleList').find('tr.'+xyz).css('display', '');
		});
		$('.bicycleList').find('tr.'+fc).css('display', '');		
	});
	
	$('#filterBikeType li>span').toggle(function(){
		var fcc = $(this).attr('class');
		$(this).addClass('typeOff');
		$('.bicycleList').find('tr.'+fcc).css('display', 'none');
	}, function(){
		$(this).removeClass('typeOff');
		var fcc = $(this).attr('class');
		$('.bicycleList').find('tr.'+fcc).css('display', '');		
	});
	
	$('#filterBrand').find('dd').toggle(function(){
		var fb = $(this).attr('class');
		$(this).addClass('typeOff');
		$('.bicycleList').find('tr.'+fb).css('display', 'none');
	}, function(){
		$(this).removeClass('typeOff');
		var fb = $(this).attr('class');
		$('.bicycleList').find('tr.'+fb).css('display', '');
	});
*/
	
});

jQuery.BIKERSEDGE = {
	
	slideSignup : function()
	{
		var wfmt = $('#wrapForm').css('marginTop');
		
		$('#signupForm').find('img')
			.toggle(
				function(){
					$('#signupForm').addClass('formOn');
					$('#wrapForm').css('visibility', 'visible').animate({marginTop: '1px'}, 500, 'linear');
				},function(){
					$('#wrapForm').animate({marginTop: wfmt}, 500, 'linear', function(){
						$(this).css('visibility', 'hidden');
						 $('#signupForm').removeClass('formOn').animate({paddingBottom: '15px'}, 500, 'linear');
					 });
				});

		$("#newsletterForm").validate({
			submitHandler: function(form) {
				$('#signupForm').animate({paddingBottom: '100px'});
				$('#signupForm').css('background','#EE491D url(http://localhost/bikersedge.com/public/theme/2010/img/loadingAnimation.gif) 50px 190px no-repeat'); 
				$('#wrapForm').slideUp('slow');
				$(form).ajaxSubmit({
					debug: true,
					target: "#confirm", 
					success: function() { 
						$('#signupForm').css('backgroundImage','none'); 
						$('#signupForm').animate({width: '180px'}, 500, 'linear').find('img').slideUp('slow');
					}
				});
			}
		});

		/*$('#signupForm').find('input[type="submit"]').click(function(){
																	 
			$('#wrapForm').fadeOut('slow');																	 
																	 
			var namex = $('#signupForm').find('input#name').val();	
			var emailx = $('#signupForm').find('input#email').val();															 
		
			$.post("http://localhost/bikersedge.com/newsletter/send", { name: namex, email: emailx },
				  function(data){
					$('#wrapForm').fadeIn('slow');
					$('#signupForm').css('backgroundImage','none');
					$('#wrapForm').html(data);
				  });
			
			return false;
		
		});*/

	},
	
	scrollBikeType : function(img)
	{
		
		$(img+' h5').css({display: 'none'});
		$(img+' img')
			.animate({left: '-531px', top: '-42px', opacity: '0.3'}, 3500, 
				function(){
					$(this).animate({left: '-491px', opacity: '1'}, 1500);
					$(img+' h5').fadeIn('slow');
				});
	},
	
	equipNav : function()
	{
		$('#equipCats').find('h4').click(function(){
			//$('#equipCats ul li ul:visible').css('display','none');
			$('#equipCats ul li ul:visible').slideUp('fast');
			//$(this).children('ul').css('display', 'block');
			$(this).parent('li').children('ul').slideDown('fast');	
		});
	},

	mainNav : function()
	{
		$('.level_1').find('li').hover(function(){
			$(this).children('ul.level_2').css({display: 'block'});
		}, function() {
			$(this).children('ul.level_2').css({display: 'none'});
		});
		
		$('.level_1').find('a').each(function(){
			if( $(this).attr('href').split('/')[3] == '#'){ // 3 FOR LIVE SITE, 4 FOR LOCAL
				$(this).css('cursor', 'default').click(function(){
					return false;
				});
			}
		});
		
		$('.level_2').find('li').hover(function(){
			$(this).children('ul.level_3').css({display: 'block'});
		}, function() {
			$(this).children('ul.level_3').css({display: 'none'});
		});
	},
	
	TurnItOn : function (nav) 
	{
		$(nav).find("a").not('.homeLink').each(function(){
			if( location.href.indexOf(this.href) != -1)  {
				$(this).addClass(" selected")
					.parent()
					.parent("ul")
					.parent("li")
					.find("a:first").addClass(" selected");
			}
		});
	},
	
	showBrand : function()
	{
		$('.brandItemWrap').find('h4').live('click', function(){
			$('.brandItem').css({display: 'none'}).end();
			$('.brandItemWrap').find('h4').removeClass('selected').end();
			$(this).addClass('selected').next('.brandItem').css({display: 'block'});			
			return false;
		});
	},
	
	showHeaderHours : function()
	{
		$('#locations').find('a.hoursLink').click(function(){
			$('#locations').find('h5').css({opacity: '0.3'});
			$(this).parent('h5').css({opacity: '1'}).addClass('selected').next('div').css({display: 'block'});	
			$('#locations').click(function(){
					$('#locations').find('h5.selected').removeClass('selected').next('div').css({display: 'none'});	
					$('#locations').find('h5').css({opacity: '1'});			   
			});
			return false;
		});
	},
	
	showFooterHours : function()
	{
		$('#footer').find('a.hoursLink').click(function(){
			$('.f-storeHours:visible').slideUp('slow').end();
			$('#footer').find('p.selected').removeClass();
			$(this).parent('p').addClass('selected').next('div.f-storeHours:hidden').slideDown('slow');	
			$('html').click(function(){
				$('.wrapStore').find('p').css({opacity: '1'}).removeClass('selected');
				$(this).find('div.f-storeHours:visible').slideUp('slow');		   
			});
			return false;
		});
	},

	nextImg : function (x, total)
	{
		if (x <= total){
			$('#store_gallery').find('img:eq('+x+')').css({ opacity: '0', display: 'block'}).fadeTo(4000, 1, function(){															
			$(this).fadeTo(4000, 0);
				x++;
				jQuery.BIKERSEDGE.nextImg(x, total);
			});
		}
		if (x == total)
		{
				jQuery.BIKERSEDGE.storeGallery();
		}
		
	},
	
	storeGallery : function ()
	{
		var total = $('#store_gallery').find('img').length;
		var x = 0;
		
		$('#store_gallery').find('img').fadeTo(10, 0);
		$('#store_gallery').find('img:eq('+x+')').css({ opacity: '0', display: 'block'}).fadeTo(4000, 1, function(){																
			$(this).fadeTo(4000, 0);					 
			x++;
			jQuery.BIKERSEDGE.nextImg(x, total);
		});
		
	},
	
	bikejack : function ( trigger )
	{
		$(trigger).find('a').not('.edit')
			.click(function(){
				$(trigger).find('tr.on').removeClass('on').end();
				$(this).parent('td').parent('tr').addClass('on');
				var urlBikeJack = $(this).attr('href');
				$('.bikeTypeDesc').find('.wrap').fadeOut(800, function(){
					$(this).load(urlBikeJack, function(){ 
						var jackH2 = $('.bikeTypeDesc').find('h2').html();
						$('.bikeTypeDesc').find('h2').remove().end();
						$(this).prepend('<h3>'+jackH2+'</h3>');
						$(this).fadeIn(300);
					});
				});
		return false; 
		});
	},
	
 	// CREATE A NEW METHOD AND PROPERTIES
 	hijackLink : function( trigger ) 
	{
		// START AT THE TRIGGER <div> GET ALL THE CHILD <a> TAGS AND BUILD AN ARRAY TO HIGHJACK 
		$(trigger).find('a')
		
		// BY PASS ALL .delete BUTTONS TO PREVENT HIJACKLINK
		.not('.delete').not('.logout').not('.noHiJack')
		
		// PREPARE FOR THE ONCLICK EVENT
		.click(function() {  
						
			// HIDE ANY OBJECTS OR IFRAMES SO THAT Z-INDEX WORKS IN ALL BROWSERS - IE6 
			$('body').find('object').css({display: 'none'});
			$('body').find('iframe').css({display: 'none'});
		
			// INJECT THE MODAL INTO THE BODY TAG
			$('body').append('<div id="modalHJ"></div>'); 
			
			// 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 THE <body> HEIGHT IS LESS THAN THE WINDOW HEIGHT, USE WINDOW HEIGHT TO BUILD THE MODAL  
			** IF NOT, USE THE <body> HEIGHT TO COVER THE SCROLL AREA
			*/
			if(H < WH){ 
				var H = WH;
			}

			// STYLE THE MODAL 
			$('#modalHJ').css({display: 'block', width: W, height: H, opacity: 0.5});		 

			// CREATE THE AHAH WINDOW 
			$('body').append('<div id="newJackBox"></div>'); 
			
			// GET THE LINK TO HIJACK AS A STRING // THIS IS THE VALUE OF THE <a> TAG WE CLICKED TO START THIS
			var urlJack = $(this).attr('href'); 
			
			// STYLE THE AHAH WINDOW AND TRIGGER THE CANEL HIJACK
			var L = ($(window).width() - 1000 ) / 2; // CENTER THE MODAL WINDOW IN THE VIEWPORT
			$('#newJackBox').css({left: L}).load(urlJack, function(){ 
				$('#newJackBox').append('<span class="cancelHiJackHJ">Close</span>');
				jQuery.BIKERSEDGE.cancelHiJackHJ();
			});
			
			// RESET THE SCROLL VIEW TO THE TOP TO ENSURE IT'S IN THE USERS VIEWABLE WINDOW
			$('html, body').animate({scrollTop:0}, 'slow'); 

		// DISABLE THE <a> TAG CUZ WE'RE HIJACKIN' IT
		return false; 
		
		});
		
	},
	
 	// DEPENDENT WITH HIJACKLINK
	cancelHiJackHJ : function()
	{
		$('.cancelHiJackHJ').click(function(){
			$('#newJackBox').remove();	
			$('#modalHJ').css({display: 'none'});
			$('body').find('object').css({display: 'block'});
			$('body').find('iframe').css({display: 'block'});
		});
	}
	
};