﻿/// <reference path="jquery.intellisense-1.2.6.js" />
/**
* Site specific namespace
*/
var Vinter = {
	
	Ds: function() {
			
			var dev_hosts = [
				"new.liko.vinterwebb.se",
				"localhost"
			];
			
			if ($.inArray($.url.attr("host"), dev_hosts) != -1)
				$("body").addClass("dev-server");
			
	},
	
	Selector:
	{
		
		settings: {},
		
		init: function(settings)
		{
			Vinter.Selector.settings = jQuery.extend({
				selector: "#productSelector fieldset input[type=checkbox]",
				hidden_class: "hidden-class",
				fade_speed: "fast",
				fade_out_alpha: 0.33,
				fade_out: true
			}, settings);
			
			$(Vinter.Selector.settings.selector).bind("click", Vinter.Selector.filter);
		
		},
		
		items: null,
		groups: null,
		
		filter: function(e)
		{
			var a = null;

			if (Vinter.Selector.groups == null || Vinter.Selector.items == null)
			{
				alert("No groups/items");
				return;
			}

			Vinter.Selector.groups.each(function() {

				if (a == null)
				{
					a = [];
					if ($("input[checked]", this).length > 0)
					{
						$("input[checked]", this).each(function() {
							var c = this.value;

							Vinter.Selector.items.each(function(i, item) {
								if ($(item).hasClass(c) && jQuery.inArray(item, a) == -1)
									a.push($(item));
							});
						});
					}
					else
					{
						Vinter.Selector.items.each(function(i, item) {
							a.push($(item));
						});
					}
				}
				else
				{
					var b = [];

					if ($("input[checked]", this).length > 0)
					{
						$("input[checked]", this).each(function() {
						
							var c = this.value;

							$(a).each(function(i, item) {
								if ($(item).hasClass(c) && jQuery.inArray(item, b) == -1)
									b.push($(item));
							});
						});
					}
					else
						b = a;
					
					a = b;
				}
			});
			
			$(Vinter.Selector.items).each(function() {
				$(this).addClass(Vinter.Selector.settings.hidden_class)
			});
			
			$(a).each(function() {
				$(this).removeClass(Vinter.Selector.settings.hidden_class)
			});

			$(Vinter.Selector.items).each(function() {
				
				if (Vinter.Selector.settings.fade_out == true)
				{
					if ($(this).hasClass(Vinter.Selector.settings.hidden_class))
						$(this).fadeTo(Vinter.Selector.settings.fade_speed, Vinter.Selector.settings.fade_out_alpha);
					else
						$(this).fadeTo(Vinter.Selector.settings.fade_speed, 1);
				}
			
			});
		}
		
	},
	
	parseFileLinks: function(obj)
	{
		
		var links = $("a", obj);
		
		links.each(function(i, item) {
			
			var filetype = item.href.substring((item.href.lastIndexOf(".") + 1), item.href.length);
			
			$(item)
				.addClass("filelink")
				.addClass(filetype)
				.click(function() {
					// Add file download to google analytics
					pageTracker._trackPageview(item.href);
					window.open(this.href, "popwin");
					return false;
				});
		});
		
	},
	
	checkLinkTarget: function(e)
	{	
		// window.open(this.href, "popwin");
		return false;
	},
	
	formValidation: function(fieldset, cb)
	{
		
		return jQuery.validate({
			fieldset: fieldset,
			usedefault: true,
			onerror: function(err)
			{
				jQuery.blockUI({message: err[0].msg});
				setTimeout(jQuery.unblockUI, 2000);
			},
			oncomplete: cb
		});
		
	},
	
	dropdownMenu: {
		
		getText: function(s)
		{
            var m = /([a-z ]+) /i.exec(s);

            if (m != null && m.length > 1)
	            return m[1];
            else
	            return "";
		},

		show: function(e) 
		{
		    var t = Vinter.dropdownMenu.getText($(this).text());
		
			$(this)
				.css("color", "#000")
				.html(t + " &dArr;");
			$(this.parentNode)
				.find("div")
				.css("display", "block")
				.fadeTo("fast", 0.90);
				
			return false;
			
		},
		
		hide: function(e) 
		{
		    var t = Vinter.dropdownMenu.getText($(this).text());

			$(this)
				.css("color", "#FFF")
				.html(t + " &rsaquo;");
			
			$(this.parentNode)
				.find("div")
				.fadeOut("fast");
			
			return false;
			
		}
		
	},
	
	setTabsTitle: function(ui)
	{
		$(".ui-tabs-title").text($(ui.tab).attr("title"));
	},
	
	socialSharing: function(e)
	{
	    $(".socialsharing ul a, .socialsharing ul a.closebtn").click(function() { $.unblockUI() });
	
	    $.blockUI({ 
	        message: $(".socialsharing ul"), 
            css: { 
        		padding: 0, 
        		margin: 0, 
        		width: '20%', 
        		top: '35%', 
        		left: '40%', 
        		textAlign: 'left', 
        		color: '#000', 
        		border: 'none', 
        		backgroundColor: '#fff', 
        		cursor: 'wait' 
    		}
        });
	},
	
	init: function()
	{
		 // Append class to body if IE < 7
		// Do this so we don't need conditional comments
		if($.browser.msie === true && parseInt($.browser.version) <= 6)
		{
			// Add png8 to body... 
			$("body").addClass("png8");
			document.execCommand('BackgroundImageCache', false, true);
		}
		
		// Empty text fields on click
		$(".text").click(function() {
			if ($(this).val() == $(this).attr("title"))
				$(this).val("");
		});
		
		if (typeof $().formHints == "function")
		{
			$("form").formHints({
				css_class: "preview-value",
				elements: ".text-hint"
			});
		}
		
		$(".dropdown-trigger").toggle(Vinter.dropdownMenu.show, Vinter.dropdownMenu.hide);

		// Setup and create content tabs
		if (typeof $().tabs == "function")
			$(".tabbed-container > ul, .tabbed-top-box > ul").tabs();

		// Colorize table rows
		$(".zebra tr:even").addClass("even");
		$(".listing-table tbody tr, .technicalspecs-table tbody tr").hover(
			function(e) {
				$(this).addClass("tr-hover");
			}, 
			function(e) {
				$(this).removeClass("tr-hover");
			}
		);
		
		if (typeof $.overlay == "function")
		{
			$.overlay({
				trigger: "modal-popup"
			});
		}
		
		$(".fact-box").hover(
			function() {
				$(".show-onhover", this).addClass("show-onhover-visible");
			}, 
			function() {
				$(".show-onhover", this).removeClass("show-onhover-visible");
			}
		);
		
		$("#searchForm .text").bind("focus", function() {
			 $(this).css({
				width: "150px"
			 });
		});
		
		$("#searchForm .text").bind("blur", function() {
			 $(this).css({
				width: "100px"
			 });
		});
		
		// Add > to some links...
		$("h4 > a").append("<em class='linkarrow'>&nbsp;&rsaquo;</em>");

		$(".countrypicker").click(Vinter.setLanguageCookie);
		
		$(".keywords > a").toggle(function(e)
			{
				$("ul", this.parentNode).fadeIn("fast")
				$(this)
					.blur()
					.addClass("active");
			}, 
			function(e) {
				$("ul", this.parentNode).fadeOut("fast")
				
				$(this)
					.blur()
					.removeClass("active");
		});
	
		Vinter.parseFileLinks($(".filelist"));
		
		$("a[rel=external]").click(Vinter.checkLinkTarget);
		$("a[rel=externallink], a.externallink").click(function() {
			
			window.open(this.href, "popwin");
			return false;
			
		});
		
		$(".socialsharing > a").click(Vinter.socialSharing);
		
	}
	
};

$(document).ready(Vinter.init);