﻿var Vinter = {
	
	activateMenu: function()
	{
		var m = /.+\/([^_-]+).*\.aspx/.exec(document.location.href);
		var f = m != null ? m[1].toLowerCase() : "default";
		$("#" + f).addClass("active");

		$("#subnav li").each(function(i, o) {
			
			// console.log(o.firstChild);
		
			if (o.firstChild.href && o.firstChild.href.toLowerCase() == document.location.href.toLowerCase())
				$(o).addClass("active");
		});
	},
	
	externalLinks: function() 
	{
		$("a[rel=external]").each(function(i, item)	{
			$(item).click(function(e) {
				window.open(this.href, "external");
				return false;
			});
		});
	},
	
	popup: function(e)
	{
		window.open(this.href, "popupwin", "width=400, height=550, scrollbars=1");
		return false;
	}
};

var Lunet = {
	
	cleanOnClick: function(event)
	{
	
		var t = event.target;
		t.select();
		t.style.color = "#000000";
		
	},
	
	showPanel: function(e)
	{
		var re = /#([a-z0-9]+)/;
		
		var ap = $(".splashlink a.active").get(0);
		var np = e.target;
		
		var api = re.exec(ap.href)[1];
		var npi = re.exec(np.href)[1];
		
		if (api == npi)
			return false;

		$("#" + api).fadeOut("medium", function() {
			$(ap).removeClass("active");
			$(np).addClass("active");
			
			$("#" + npi).fadeIn("medium");
		});
		
		return false;
	},
	
	loadPhoneSupplierData: function(e)
	{		
		$.ajax({
			type: "GET",
			url: "/plugins/phoneconfigurator/Data.xml",
			success: Lunet.parsePhoneSupplierData
		});
	},
	
	parsePhoneSupplierData: function(xml, status)
	{					
		table = $("<table />");		
		table.append("<tr class='row-headers'><th>Leverant&ouml;r</th><th>M&aring;nadsavgift</th><th>&Ouml;ppningsavgift</th><th>Sverigesamtal</th><th>Mobil</th><th>&Aring;rsavgift</th><th>Prisskillnad</th><th>Till&auml;gg</th></tr>");
				
		var monthly = $('#phoneMonthly').val();
		var openingFee = $('#phoneOpeningFee').val();
		var swedencalls = $('#phoneSweden').val();
		var mobile = $('#phoenMobile').val();
				
		var rowClass = "even"; // row bg-color
				
		if(monthly == "" || monthly == null) 
		{
			$(".phone-configurator-validation").fadeIn("slow", function()
			{
				setTimeout(function()
				{                   
					$(".phone-configurator-validation").fadeOut("slow");
				}, 3000);								
			});			
			return;
		}
		
		
		// get the phone-suppliers
		$(xml).find("item").each(function(i, item) {															
			
			supp = new Object();															
							
			supp.supplier = $($(item).find("supplier").get(0)).text();
			supp.monthly = $($(item).find("monthly").get(0)).text();
			supp.openingFee = $($(item).find("openingfee").get(0)).text();
			supp.swedenCalls = $($(item).find("swedencalls").get(0)).text();
			supp.mobile = $($(item).find("mobile").get(0)).text();
			supp.comments = $($(item).find("comments").get(0)).text();
			supp.yearly = (supp.monthly*12);												
			
			supp.diff = (supp.yearly)-($('#phoneMonthly').val()*12);							
			
			
			if (rowClass == "even") 
				supp.rowClass = "odd";									
			else
				supp.rowClass = "even";
			
			rowClass = supp.rowClass;
				
			table.append(Lunet.renderPhoneSupplierData(supp));
			
		});
		
		$(".phone-configurator-results").append(table);
		
		// show results
		$(".phone-configurator-results").slideDown("slow");
	},
	
	renderPhoneSupplierData: function(supp)
	{
		
		var difClass = "normal";
		var monClass = "normal";
		var opeClass = "normal";
		var sweClass = "normal"; 
		var mobClass = "normal"; 
				
		if(supp.diff < 0)
			difClass = "alert";
		
		if(supp.monthly < $('#phoneMonthly').val())
			monClass = "cheaper";
		
		if(supp.openingFee < $('#phoneOpeningFee').val())
			opeClass = "cheaper";
			
		if(supp.swedenCalls < $('#phoneSweden').val())
			sweClass = "cheaper";			
		
		if(supp.mobile < $('#phoneMobile').val())
			mobClass = "cheaper";			
					
		return $("<tr />")
				.addClass(supp.rowClass)
				.html("<td class='supplier'>" + supp.supplier + "</td><td class='"+monClass+"'>" + supp.monthly + "</td><td class='"+opeClass+"'>" + supp.openingFee + "</td><td class='"+sweClass+"'>" + supp.swedenCalls + "</td><td class='"+mobClass+"'>" + supp.mobile + "</td><td>" + supp.yearly + "</td><td class='"+difClass+"'>" + supp.diff + "</td><td class='comments'>" + supp.comments + "</td>");
	
	},
		
	loadSupplierData: function(e)
	{		
		$.ajax({
			type: "GET",
			url: "/plugins/configurator/Data.xml",
			success: Lunet.parseSupplierData
		});
	},
	
	parseSupplierData: function(xml, status)
	{					
		table = $("<table />");		
		table.append("<tr class='row-headers'><th>Leverant&ouml;r</th><th>M&aring;nadsavgift</th><th>Hastighet</th><th>&Aring;rskostnad</th><th>Prisskillnad/&aring;r</th><th>Kommentar</th></tr>");
		
		var monthly = $('#monthlyPrice').val();		
		var residence = $('#residence').val();
		var speed = $('#speed').val();
		
		var netCost = 0;

		var rowClass = "even";
				
		if(monthly == "" || monthly == null || residence == "" || residence == null || speed == "" || speed == null) {

			$(".configurator-validation").fadeIn("slow", function()
			{
				setTimeout(function()
				{                   
					$(".configurator-validation").fadeOut("slow");
				}, 3000);								
			});
			
			return;
		}
		
		
		// get Lunet prices
		$(xml).find("lunet").each(function(i, item) {
			
			var title = $($(item).find("title").get(0)).text();
			var netCostMonthly = $($(item).find("monthly").get(0)).text();
			
			if (title.replace(/[^a-z]+/ig, "") == residence.replace(/[^a-z]+/ig, "")) {
				netCost = (netCostMonthly*12);
			}					
		});
		
		// get the suppliers
		$(xml).find("item").each(function(i, item) {															
			
			obs = new Object();															
							
			obs.supplier = $($(item).find("supplier").get(0)).text();
			obs.monthly = $($(item).find("monthly").get(0)).text();
			obs.speed = $($(item).find("speed").get(0)).text();
			obs.comments = $($(item).find("comments").get(0)).text();
			obs.yearly = (obs.monthly*12);	
			obs.netCost = netCost;								
			obs.diff = (obs.yearly+obs.netCost)-($('#monthlyPrice').val()*12);				
			
			//if(item.tagName == "item")
			if(obs.speed == speed) 
			{
				if (rowClass == "even") 
					obs.rowClass = "odd";									
				else
					obs.rowClass = "even";
				
				rowClass = obs.rowClass;
				
				table.append(Lunet.renderSupplierData(obs));
			}
		});
		
		$(".configurator-results").append(table);
		
		// show results
		$(".configurator-results").slideDown("slow");
	},
	
	
	renderSupplierData: function(obj)
	{
		
		var colClass = "normal";
		
		if(obs.diff < 0)
			colClass = "alert";
						
		return $("<tr />")
				.addClass(obs.rowClass)
				.html("<td class='supplier'>" + obj.supplier + "</td><td>" + obj.monthly + "</td><td>" + obj.speed + "</td><td>" + (obs.yearly+obs.netCost) + "</td><td class='"+colClass+"'>" + obs.diff + "</td><td class='comments'>" + obj.comments + "</td>");
	
	},
	
	calculateCost: function(e)
	{
		
		if ($(".configurator-results").is(":visible")) {
			$(".configurator-results").slideUp("slow");	
		}	
		
		$("#calcylatorInformation").hide();	
		
		$(".configurator-results").text("");
		
		var speed = $('#speed').val();
		var monthlyPrice = $('#monthlyPrice').val();
		var residence = $('#residence').val();
		
		var resultTxt = "Din aktuella årskostnad: ";
		
		var results = (monthlyPrice*12);
		
		$("#yearlyCost").text(resultTxt + results + " kr");
		$("#yearlyCost").show("slow");
		
		if(residence == "Villa")
			$("#calcylatorInformation").show("slow");
		
		
		Lunet.loadSupplierData();				
		
	},
	
	calculatePhoneCost: function(e)
	{	
	
		if ($(".phone-configurator-results").is(":visible")) {
			$(".phone-configurator-results").slideUp("slow");	
		}	
		
		$(".phone-configurator-results").text("");		
		
		var monthly = $('#phoneMonthly').val();
		var openingfee = $('#phoneOpeningFee').val();
		var sweden = $('#phoneSweden').val();
		var mobile = $('#phoneMobile').val();
		var resultTxt = "Din aktuella fasta årskostnad: ";
		var yearly = (monthly*12);
		
		$("#phoneYearlyCost").text(resultTxt + yearly + " kr");
		$("#phoneYearlyCost").show("slow");
		
		Lunet.loadPhoneSupplierData();
			
	},
	
	activateWizard: function()
	{
		var total = 0;
		
		$("div#wizard ul li").bind("click", function(e) {						
			
		    var tmpvalue = parseInt($(this).attr("title"));
					
			if($(this).hasClass("active") == true)
				total -= tmpvalue;					
			else
				total += tmpvalue;
					
			$(this).toggleClass("active");
			Lunet.SetWizardResult(total);	
			
		});	
											
	},
	
	SetWizardResult: function(sum)
	{			
		// reset 
		$("div#wizardResults ul li#light div.results-content").slideUp(200);			
		$("div#wizardResults ul li#medium div.results-content").slideUp(200);			
		$("div#wizardResults ul li#heavy div.results-content").slideUp(200);
		
		// show 
		if(sum < 4 && sum > 0) {					   
		    $("div#wizardResults ul li#light").slideDown(300);
			$("div#wizardResults ul li#light div.results-content").slideDown(300);								
		}		
		if(sum > 3 && sum < 13) {
			$("div#wizardResults ul li#medium").slideDown(300);
			$("div#wizardResults ul li#medium div.results-content").slideDown(300);	
		}				
		if(sum > 12) {
			$("div#wizardResults ul li#heavy").slideDown(300);
			$("div#wizardResults ul li#heavy div.results-content").slideDown(300);			
		}
	},

	pageInit: 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);
		}
		
		// Fixa puffarna
		$(".puff").each(function(i, item) {
			$("<dd />")
				.attr("class", "puffbottom")
				.appendTo(item);
		})
		
		// Select inputs on click
		$(".cleanonclick").bind("click", Lunet.cleanOnClick);
	
		Vinter.activateMenu();
		
		// Hide splashes
		$(".splashcontent").hide();		
		
		// Switch splash
		$(".splashlink a").bind("click", Lunet.showPanel);
    		
		// Start splash
		$("#panel1").fadeIn("medium");
		
		$(".popup").click(Vinter.popup);
		
		// configurator				
		$("#calculate").bind("click", Lunet.calculateCost);		
		
		// phoneconfigurator				
		$("#phoneCalculate").bind("click", Lunet.calculatePhoneCost);								
		
		// wizard						
		Lunet.activateWizard();			
		
		// faq show answers
		$("dl.qa dt.question span").click(function(e)
		{	
			$(this).toggleClass("active");			
			$(this).parent().parent().find("dd").toggle();
				return false;
			});

			/*
			if( $(this).attr("class") == "active" )
				$(this).parent().parent().text("Visa svar");
			else
				$(this).parent().parent().text("Dölj svar");
			*/
												
		}

}

$(document).ready(Lunet.pageInit);