var Search = {
	submit : function(form){
			if(form){
				form = $(form);
			}else{
				form = $('#search-topnav form');
			}
			$('#search-spinner').show();
			$('#search-dropdown').show();
			$.get('/en/search/dropdown?'+form.serialize(), function(data){
				$('#search-dropdown-results').html(data);
				$('#search-spinner').hide();
			});
		},
		
	update : function(field, key){
			if(key != 27 && $(field).val().length>2){ //not escape and more than 2 letters
				if(this.timeout){
					clearTimeout(this.timeout);
				}
				this.timeout=setTimeout('Search.submit()',300);
			}
		},
	timeout : ""
};

//Serialize object for form posts
$.fn.serializeObject = function()
{
    var o = {};
    var a = this.serializeArray();
    $.each(a, function() {
        if (o[this.name]) {
            if (!o[this.name].push) {
                o[this.name] = [o[this.name]];
            }
            o[this.name].push(this.value || '');
        } else {
            o[this.name] = this.value || '';
        }
    });
    return o;
};

var MyLysol = {
	save : function(form){
		//form = $('#save-this-form');
		form = $(form);

		MyLysol.track(form);
		
		var obj = form.serializeObject();
		
		obj['a']=1;
		
		$.post('/en/my-lysol/save',obj, function(data){
			$('.save-this').html(data);
		});
	},
	
	getType : function(type_id){
		var types = {1:"products",2:"articles",3:"videos",4:"coupons",5:"conversations",6:"reviews"};
		return types[type_id];
	},
	
	track : function(form){
		var type = MyLysol.getType(form.find('input[name*=item_type_id]').val());
		var item_id = form.find('input[name*=item_id]').val();
		var action = form.find('input[name*=set_tagged]').val()?'add-to-list':'remove-from-list';
		
		_gaq.push(['_trackPageview', '/'+type+'/'+item_id]);
		
		//exitTracker._trackEvent(action,'/'+type+'/'+item_id, 1);
	}

};


$(function () {
	/* the hover flyout nav */

	$('ul#top-nav li a').not('#nav-special-offers').hoverIntent(
		function () {
			var that = $(this);
			$('#nav-popout').modal({
				overlayId: 'nav-overlay',
				containerId: 'nav-container',
				autoPosition: false,
				onShow: function (dialog) {
					$("#" + that.attr('id').replace(/nav-/, 'nav-out-')).next('div.flyout').show();

					dialog.overlay.bind('mouseenter', function () {
						$.modal.close();
					});

				}
			});
		},
		function () {
		}
	);
	$('ul#top-nav-out a.action').hover(function () {
		$('div.flyout:visible').hide();
		$(this).next('div.flyout').show();
	});


	/* Detects escape clicks and cancels anything with a class of 'cancel' */
	$(document).keyup(function (e) {
		if (e.keyCode == 27) { //escape
			$('.cancel').click();
		}
	});

	// Detects clicks outside of the search box and automatically closes it 
	$(document).click(function (e) {
		if (!$(e.target).parents().filter('#search-topnav').length) {
			$('#search-topnav .cancel').click();
		}
	});

	// Detects any keypress in the q field
	$('#search-topnav-q').keyup(function (e) {
		Search.update(this, e.keyCode);
	});

	$('.autoclear').focus(function () {
		if ($(this).val() == $(this).attr('title')) {
			$(this).val("");
		}
	});

	$('.autoclear').blur(function () {
		if ($(this).val() == "") {
			$(this).val($(this).attr('title'));
		}
	});



	// Switch tabs for healthy families content

	$('.tabs li a').click(function () {
		handleTabClick($(this));
	/*
		$('.tab-content').removeClass('active');
		//$('.tab-content').each(function(){alert ($(this).id)});
		$('.tabs li a.active').removeClass('active');
		var name = $(this).attr('id');
		var box = name.replace('tab', 'content');
		$('#' + box).addClass('active');
		$('#' + name).addClass('active');
*/
	});
	
	
	// Switching tabs from in-content links

	$('a.tab-link').click(function () {
		handleTabLinks($(this));
	
	});

	//Javascript for hover info bubbles.	
	$('.tracker-pin').hover(
		function () {
			$('.tracker-data').removeClass('show');
			var name = $(this).attr('id');
			var popup = name.replace('pin', 'info');
			$('#' + popup).addClass('show');
		},
		function () {
			$('.tracker-data').removeClass('show');
		}
	);
	$('.tracker-data').hover(
		function () {
			var name = $(this).attr('id');
			$('#' + name).addClass('show');
		},
		function () {
			$('.tracker-data').removeClass('show');
		}
	);

	/* slide out */
	var init = false,
		pb_nav = $('#products-bottom-nav'),
		p_img = $('#products-image'),
		pn_tog = $('#products-bottom-nav-toggle'),
		pn_con = $('#products-bottom-nav-content');
	$('#products-bottom-nav-toggle').toggle(
		function (e) {
			e.preventDefault();
			if (!init) {
				$.get('/en/cleaning-products/sliders', function (data) {
					init = true;
					pn_con.html(data);
					initProdSlide();
				});
			}
			pb_nav.animate({
				height: '435px'
			}, 500).addClass('expanded');
			p_img.animate({
				top: '-455px'
			}, 500);
			pn_tog.animate({
				top: '-418px'
			}, 500);
		}, function (e) {
			e.preventDefault();
			pb_nav.animate({
				height: '90px'
			}, 500).removeClass('expanded');
			p_img.animate({
				top: '-110px'
			}, 500);
			pn_tog.animate({
				top: '-70px'
			}, 500);
		}
	);
	$("ul#top-nav li a#nav-products").focus(function (e) {
		//e.preventDefault();	
		$("#hover-nav-products").modal({
			containerId: 'hover-nav-products-container',
			opacity: 60,
			overlayClose: true,
			minWidth: 647,
			minHeight: 354
		});
	});

	/*$('.share-open').click(function () {
		$('.supernav-links').toggleClass('show');
		$('#global-nav-content').addClass('expanded');
		$('.share-open').addClass('hide');
	}); 
	$('.expandable').addClass('hide');
	$('#global-nav-content').removeClass('expanded');
	*/

	if ($('div.no-flash #home-breadcrumb').length != 0){
		$('#inner-flash-container').css('z-index','200');
	} 
	
	$(".toggleProvince").click(function(e) {
		e.preventDefault();
		$(this).next(".toggle").slideToggle(600);
	});
	
});

function handleTabLinks($this)
{
		$('.tab-content').removeClass('active');
		$('.tabs li a.active').removeClass('active');
		var name = $this.attr('id');
		var box = name.replace('link', 'content');
		var link = name.replace('link', 'tab');
		$('#' + box).addClass('active');
		$('#' + link).addClass('active');
}

function handleTabClick($this)
{
	$('.tab-content').removeClass('active');
	$('.tabs li a.active').removeClass('active');
	var name = $this.attr('id');
	var box = name.replace('tab', 'content');
	$('#' + box).addClass('active');
	$('#' + name).addClass('active');
}

//makin' it better.
function showHide(){
	$('.expandable').removeClass('hide');
}

//social popup
function openSocial(){
	$.get('/lysol-community/social', function(data) {
		$('#modal-content-social').html(data);
		$("#modal-content-social").modal({
			overlayId: 'modal-overlay-social',
			containerId: 'modal-container-social',
			opacity:80, 
			overlayClose:true,
			minWidth:715,
			minHeight:350,
			onShow: function (dialog) {
				dialog.overlay.bind('mouseenter', function () {
					//$.modal.close();
				});
			}
		});
	});
}

function playMovie(movieName) {
	$("#modal-movie").modal({
		overlayId: 'movie-overlay',
		containerId: 'movie-container',
		opacity: 80,
		overlayClose: true,
		minWidth: 640,
		minHeight: 400,
		onShow: function (dialog) {
			var movieso = new SWFObject("/images/flash/LightboxVideoPlayer.swf", "home", "640", "400", "9", "#FFFFFF");
			movieso.addParam("wmode", "transparent");
			movieso.addVariable("pathToVideo", "/images/flash/" + movieName);
			movieso.write("modal-movie");
		}
	});
}


