$(document).ready(function(){
	$('.add_btn').click(function() {
		$('table th').removeClass('active');
		$(this).parents('th').addClass('active');
		return false;
	});
	
	$('.hoverable').hover(function() {
		$(this).addClass('hover');
	}, function(){
		$(this).removeClass('hover');
	});
	$('.clickable').click(function(){
		window.location = $(this).find('a').attr('href');
	});
	
	// Top Main Navigation animations for dropdowns 
	$("#primary_nav li").hoverIntent({
		over: makeTall,
		timeout: 150, // number = milliseconds delay before onMouseOut
		out: makeShort
	});
	
	// For login slide down effect
	$('#account_login').click(function(){
		if($('#account').is(':visible')) { 
			$(this).css('background-color', '#0d1622');
			$('#account').slideUp().show();
		}
		else { 
			$(this).css('background-color', '#1C4062');
			$('#account').slideDown().hide();
		}
	});
	
	// Homepage Feature Slider 
	if ($('.coda-slider-wrapper').length) {
		$('#coda-slider-1').codaSlider({
			autoHeight: false,
			autoSlide: true,
			dynamicArrows: false,
			dynamicTabs: false
		});
	}
		 //Homepage Feature Slider 
		/*$("#chained").scrollable({circular: true, mousewheel: true}).navigator("#main_nav").autoscroll({
		interval: 5000		
	});	*/
	
	// Animation and show/hide for .tab_nav 
/*		
	hash = hash.replace( /^#/, '' );
	var fx, node = $( '#' + hash );
	if ( node.length ) {
	  fx = $( '<div></div>' )
	          .css({
	              position:'absolute',
	              visibility:'hidden',
	              top: $.scroll().top + 'px'
	          })
	          .attr( 'id', hash )
	          .appendTo( document.body );
	  node.attr( 'id', '' );
	}
	document.location.hash = hash;
	if ( node.length ) {
	  fx.remove();
	  node.attr( 'id', hash );
	}
*/		
		
/*	if (window.location.hash) {
//			window.location.href.slice(window.location.href.indexOf('?')).split(/[&?]{1}[\w\d]+=/); THIS IS THE SHIT THAT HYUNG SUGGESTED
			var requestedTab = window.location.hash;
//			$(requestedTab).show();
//			$(".tab_nav li:has(a[href='"+requestedTab+"'])").addClass("active"); // Activate first tab
//			$(".tab_nav li a[href='"+requestedTab+"']").addClass("active"); // Activate first tab anchor
			$(".tab_nav li a[href='"+requestedTab+"']").trigger('click');
		}
		else {	
			$(".tab").hide();
			$(".tab_nav li:first-child").addClass("active"); // Activate first tab
			$(".tab_nav li:first-child a").addClass("active"); // Activate first tab anchor
			$(".tab_container div:first-child").show(); // Show first tab content	
	}
	
	$(".tab_nav li a").click(function() {
		$(".tab_nav li").removeClass("active"); // Remove any "active" class
		$(".tab_nav li a").removeClass("active"); // Remove any "active" class
		$(this).addClass("active"); // Add "active" class to selected anchor
		$(this).parent('li').addClass("active"); // Add "active" class to selected tab
		$(".tab").fadeOut('fast', selectedTabComesIn); //Hide all tab content
		
		var activeTab = $(this).attr("href"); //Find the href attribute value to identify the active tab + content
		var selectedTabComesIn = $(activeTab).fadeIn(500); //Fade in the active ID content
//		window.location = '#header';
		return false;
	});
*/
	function getUrlVars()
	{
	    var vars = [], hash;
	    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
	    for(var i = 0; i < hashes.length; i++)
	    {
	        hash = hashes[i].split('=');
	        vars.push(hash[0]);
	        vars[hash[0]] = hash[1];
	    }
	    return vars;
	}
	
	
	// Universal Form Clear on Focus
	$("input.text").each(function(){
		$(this).val($(this).attr("alt"));
	});
		// clear input.text on focus, if still in default
		$("input.text").focus(function() {
			if($(this).val()==$(this).attr("alt")) {
				$(this).val("");
			}
		});
		// if field is empty afterward, add text again
		$("input.text").blur(function() {
			if($(this).val()=="") {
				$(this).val($(this).attr("alt"));
			}
		});
	
	// Lightbox Sample Code (see http://colorpowered.com/colorbox/ for more examples)
	/*$('.cb').colorbox();
	$('.cb_inline').colorbox({ width: '35%', inline: true, opacity:'.65', overlayClose: false});
	$('.cb_iframe').colorbox({ width: '40%', height: '65%', iframe: true, opacity:'.65', overlayClose: false});*/
	
	/*// Newsletter Sign Up Form Lightbox
	function putEmail(){
		var promo_email = $('.legal .span-7 input.text').val();
		$('#promo_email_address').val(promo_email);
	}
	$('.legal .span-7 input.btn').colorbox({ width: '40%', href:'#newsletter_dialogue', onOpen: putEmail, inline: true, opacity:'.65', overlayClose: false})
	
	// Filter for Listing Page
	$("input.brand_check").live('click', function() {
		if($(this).is(':checked')) {
			$("#listing_container div."+$(this).attr('id')).removeClass('brand_hidden');
			$("#listing_container div").not("#no_phones, .brand_hidden, .type_hidden, .price_hidden").fadeIn(500, noPhones);
		} else {
			$("#listing_container div."+$(this).attr('id')).addClass('brand_hidden');
			$("#listing_container div."+$(this).attr('id')).fadeOut(500, noPhones);
		}
	});
	$("input.type_check").live('click', function() {
		if($(this).is(':checked')) {
			$("#listing_container div."+$(this).attr('id')).removeClass('type_hidden');
			$("#listing_container div").not("#no_phones, .brand_hidden, .type_hidden, .price_hidden").fadeIn(500, noPhones);
		} else {
			$("#listing_container div."+$(this).attr('id')).addClass('type_hidden');
			$("#listing_container div."+$(this).attr('id')).fadeOut(500, noPhones);

		}
	});
	$("input.price_check").live('click', function() {
		if($(this).is(':checked')) {
			$("#listing_container div."+$(this).attr('id')).removeClass('price_hidden');
			$("#listing_container div").not("#no_phones, .brand_hidden, .type_hidden, .price_hidden").fadeIn(500, noPhones);
		} else {
			$("#listing_container div."+$(this).attr('id')).addClass('price_hidden');
			$("#listing_container div."+$(this).attr('id')).fadeOut(500, noPhones);

		}
	});
	function noPhones() {
		if ( $("#listing_container .listing:visible").length == 0 ) {
			$("#no_phones").show();
		} 
		if ( $("#listing_container .listing:visible").length != 0 ) {
			$("#no_phones").hide();
		}
	}
	*/
	// Tour / My Account Pages (non-nested, iconified sidebar navigation)
		// Splits the url into 2 arrays 
		var current_section_arr = location.href.split('#');
	
		if(current_section_arr.length < 2) {
			// If statement for when the user enters the page the first time
			$('#content_slides .slide').hide();
			$('#content_slides .slide:first-child').show();
			$('#content_nav li:first-child').addClass('selected');
		} else {
			// If statement for when the user enters the page through the Tour left nav link
			var current_section = current_section_arr[1].replace('a_', ''); // ANCHOR LINKING - Removes the a_ from the link 
			var current_slide = current_section + '_slide';
			$('li#' + current_section).addClass('selected');
			$('#content_slides .slide').hide();
			$('#content_slides #'+current_slide).show();
		}
	
		// This code adds the Selected class to the button, Hides the rest. 
		$('#content_nav li a').click(function(){
			// Shows and hides Preloader animated gif in between transition, when links are clicked
			setTimeout(function() { $('#preloader').fadeIn('fast'); }, 1);
			setTimeout(function() { $('#preloader').fadeOut('fast'); }, 350);
		
			$('#content_nav li').removeClass('selected');
			$(this).parent('li').addClass('selected');
			$('.slide').fadeOut('fast', selectedSlide);
		
			var activeSlide = $(this).attr('href').replace('a_', '') + '_slide'; // ANCHOR LINKING - Removes the a_ from the link
			var selectedSlide = $(activeSlide).delay(500).fadeIn('fast');
		
			return false;
		});
		
	/*// FAQ Pages (nested sidebar navigation with nested link anchoring via URL)
		// Splits the url into 2 arrays 
		var current_section_arr = location.href.split('#');

		if(current_section_arr.length < 2) {
			// If statement for when the user enters the page the first time
			$('.faq_main .faq_section').hide();
			$('.faq_nav .faq_sub').hide();
			$('.faq_nav li:first-child .faq_sub').slideDown();
			$('.faq_nav li:first-child .faq_sub li:first-child').addClass('selected');
			$('.faq_main .faq_section:first-child').show();
		} else {
			// If statement for when the user enters the page through the Tour left nav link
			var current_section = current_section_arr[1].replace('a_', ''); // ANCHOR LINKING - Removes the a_ from the link
			var current_slide = current_section + '_slide';
			$('.faq_main .faq_section').hide();
			$('.faq_main #'+current_slide).show();
			$('.faq_nav .faq_sub').hide();
			$('.faq_nav .faq_sub li a[href="#a_'+current_section+'"]').parent('li').addClass('selected');
			$('.faq_nav .faq_sub li a[href="#a_'+current_section+'"]').parent('li').parent('.faq_sub').slideDown('fast');
		}
		$('.faq_main .answer').hide();
		$('.faq_nav .faq_cat > a').click(function(){
			$('.faq_nav .faq_sub').slideUp('fast');
			$(this).next().slideDown('fast');
		});
		// Show / Hide Answers to FAQ Questions
		$('.faq_main .question').click(function(){
			$(this).next().slideToggle('fast');
		});
		
		// This code adds the Selected class to the button, Hides the rest.
		$('.faq_nav .faq_sub a').click(function(){
			// Shows and hides Preloader animated gif in between transition, when links are clicked
			setTimeout(function() { $('#preloader').fadeIn('fast'); }, 1);
			setTimeout(function() { $('#preloader').fadeOut('fast'); }, 350);

			$('.faq_sub li').removeClass('selected');
			$(this).parent('li').addClass('selected');
			$('.faq_section').fadeOut('fast', selectedSlide);

			var activeSlide = $(this).attr('href').replace('a_', '') + '_slide'; // ANCHOR LINKING - Removes the a_ from the link
			var selectedSlide = $(activeSlide).delay(500).fadeIn('fast');

			return false;
		});
	
	// @end FAQ*/

	function makeTall(){
		$('#primary_nav li').removeClass('hover');
		$(this).stop().find(".sub").slideDown('fast').show();
		$(this).addClass('hover');
	}
	function makeShort(){
		$(this).removeClass('hover');
		$(this).stop().find(".sub").fadeOut('fast');
	}
	
	$('.info_tab_nav li a').click(function() {
		$('.info_tab_nav li a').removeClass('active');
		$(this).addClass('active');
		$('.info_tab').fadeOut('fast', selectedTab);

		var activeTab = $(this).attr("href");
		var selectedTab = $(activeTab).delay(500).fadeIn('fast'); 
		return false;
	});
	
	
	$('.quantity_container input[type="text"]').focus(function() {
			$(this).parents('.card_listing').addClass('active');
	});
	$('.quantity_container input[type="text"]').blur(function() {
		if($(this).val() == 0) {
			$(this).parents('.card_listing').removeClass('active');
		}	else  {
			
		}
	});
	
	$(".add_btn").click(function() {
		var $button = $(this);
		var targetInput = $button.parent().parent().find('input[type="text"]');
		var oldValue = $(targetInput).val();

		if ($button.text() == "add") {
			var newVal = parseFloat(oldValue) + 1;
		} else {
			if (oldValue >= 1) { 	// Don't allow decrementing below zero
				var newVal = parseFloat(oldValue) - 1;
			}
		}
		$(targetInput).val(newVal);
		$(targetInput).focus();
		updateTotals(this);
	});
	
	$("#transaction_container tr:even").addClass("even");
	
}); // END OF DOCUMENT READY 
