$(document).ready(function() {
// Check for touch based device
if (navigator.userAgent.match(/iPhone|iPad|iPod|Android/i)) {
	var iOS = true;
	$('body').addClass('ios');
}

// Tag 'on' states via the page_slug
	var whichBrand = 0;
	var myClass = $('body').attr('className');
	var classArray = myClass.split(' ');
	$.each(classArray,function() {
		$('#nav-'+this).addClass('active');
		if (this == 'samson') {
			$('#nav-hartke').addClass('inactive');
			$('#nav-zoom').addClass('inactive');
			whichBrand = 'nav-samson';
		} else if (this == 'hartke') {
			$('#nav-samson').addClass('inactive');
			$('#nav-zoom').addClass('inactive');
			whichBrand = 'nav-hartke';
		} else if (this == 'zoom') {
			$('#nav-samson').addClass('inactive');
			$('#nav-hartke').addClass('inactive');
			whichBrand = 'nav-zoom';
		}
	});

// Keep Nav Open Function
	var nav_open = $.cookie('nav_open');
	if (($.cookie('nav_open') == null) && (iOS == true)) {
		toggle_nav(true);
		//alert($.cookie('nav_open'));
	} else {
		if( $.cookie('nav_open') ) toggle_nav(true);
	}

	$('#nav-toggler').live('click', function(event) {
		event.preventDefault();

		if ( $.cookie('nav_open') == null ) {
			toggle_nav(true);
		} else if ( $.cookie('nav_open') == 'true' ) {
			toggle_nav(false);
		} else {
			toggle_nav(true);
		}

		return false;
	});

	function toggle_nav(open) {
		if(open) {
			$('#nav-toggler').removeClass('nav-open').addClass('nav-close');
			$('#nav-toggler').html('Hide Navigation');
			$('#'+whichBrand+'-open').css('display','block');
			$.cookie('nav_open', 'true', { expires: 7, path: '/' });
		} else {
			$('#nav-toggler').removeClass('nav-close').addClass('nav-open');
			$('#nav-toggler').html('Keep Navigation Open');
			$('#'+whichBrand+'-open').css('display','none');
			$.cookie('nav_open', $(this).val(), { path: '/', expires: 7 });
		}
	}

// Mega Dropdown Transitions
	var opentimer = { 'nav-samson' : '', 'nav-hartke' : '', 'nav-zoom': '' };
	var closetimer = { 'nav-samson' : '', 'nav-hartke' : '', 'nav-zoom': '' };
	if (iOS != true) {
		$('.nav-item').mouseover(function(){

			clearTimeout(opentimer[this.id]);
			clearTimeout(closetimer[this.id]);
			whichNav = this.id;
			var openmenu = function(nav) {
				return function() {
					if($.cookie('nav_open') != 'true') {
						$(nav).find('.mega-hover').addTransitionClass('mega-open');
					} else if (whichBrand != whichNav) {
						$(nav).find('.mega-hover').addTransitionClass('mega-open');
					}
				} ;
			}
			opentimer[this.id] = setTimeout(openmenu(this), 300);
		});
		$('.nav-item').mouseout(function(){
			clearTimeout(opentimer[this.id]);
			var closemenu = function(nav) {
				return function() {
					$(nav).find('.mega-hover').removeTransitionClass('mega-open');
				//	if($.cookie('nav_open') == null) {
				//		$(nav).find('.mega-hover').removeTransitionClass('mega-open');
				//	} else if (whichBrand != whichNav) {
				//		$(nav).find('.mega-hover').removeTransitionClass('mega-open');
				//	}
				};
			}
			closetimer[this.id] = setTimeout(closemenu(this), 100);
		});
	}
// Search Focus
	$('#main-search').focus(function(){
		if ($(this).val() == 'Search') $(this).val('');
		$('#main-search-form').css('background-position','0 -30px');
	});
	$('#main-search').blur(function(){
		if ($(this).val() == '') $(this).val('Search');
		$('#main-search-form').css('background-position','0 0');
	});

// Email Signup Focus
	$('#email-signup').focus(function(){
		if ($(this).val() == 'Enter Email Address') $(this).val('');
		$('#email-signup-form').css('background-position','0 -30px');
	});
	$('#email-signup').blur(function(){
		if ($(this).val() == '') $(this).val('Enter Email Address');
		$('#email-signup-form').css('background-position','0 0');
	});

// Tab sets
	$("#tab-set").css('display','block');
	$(function() {
		$("#tab-set").tabs();
	});
	$(function() {
		$("#dealer-tab-set").tabs(function() {
		return false;
		});
	});

// Show/Hide Dealer Locator
	$("#show-hide-dealer").click(function(e) {
		$('#aside-dealer-locator').slideToggle('slow');
		e.preventDefault();
	});
//Notify Me
	$('#notify-me').click(function() {
		$('#notify-pop').toggle();
	});
});
// Home DIP Hightlight
//$(window).load(function() {
//	$(".dip-highlight").addTransitionClass('dip-highlight-position1');
//});

