﻿
/// <reference path="jquery-1.4.1-vsdoc.js" />

// Variabel för att hålla koll på nuvarande index i startsidans galleri
var currentGalleryIndex = 0;
var slotTime = 10000;
var tid = setInterval("showNext()", slotTime);
var nrCase = 0;

$.extend($.expr[':'], {
    'containsi': function (elem, i, match, array) {
        return (elem.textContent || elem.innerText || '').toLowerCase()
    .indexOf((match[3] || "").toLowerCase()) >= 0;
    }
});



$(document).ready(function () {

	if (window.location.hash != "") {
		var o_hash = window.location.hash;
		//alert(o_hash.substring(1,o_hash.length));
		//console.log($(".areas a[omrade=" + o_hash.substring(1, o_hash.length) + "]").length);
		if ($(".areas a[omrade=" + o_hash.substring(1, o_hash.length) + "]").length > 0) {
			FilterOmrade(o_hash.substring(1, o_hash.length))
		}
	}

	if ($(".activityCity").length > 0) {
		console.log($(".activityCity").prev("p").html());
		var sideP = "<p></p>"
		$("#content-side").append(sideP);
		$("#content-side p").append($(".activityCity").prev("p").html());
		$(".activityCity").prev("p").remove();
		$("#content-side").append($(".activityCity"));
	}

	$('input').keydown(function (e) {
		if (e.keyCode == 13) {
			$(this).parents('form').submit();
			return false;
		}
	});
	//Eshop login 
	$("#wd_username").focus(function () {
		if ($("#wd_username").val() == "Användarnamn") {
			$("#wd_username").css("color", "#000");
			$("#wd_username").val("")
		}

	})
	$("#wd_username").blur(function () {
		if ($("#wd_username").val() == "") {
			$("#wd_username").css("color", "#666");
			$("#wd_username").val("Användarnamn");
		}
	})
	/*
	$("#wd_password").focus(function () {
	if ($("#wd_password").val() == "Lösenord") {
	$("#wd_password").attr("type", "password");
	$("#wd_password").val("")
	}
	})
	$("#wd_password").blur(function () {
	if ($("#wd_password").val() == "") {
	$("#wd_password").attr("type", "text");
	$("#wd_password").val("Lösenord");
	}
	})
	*/
	$("#wd_password_dummy").focus(function () {
		$("#wd_password_dummy").hide();
		$("#wd_password").show();
		$("#wd_password").focus();
	});
	$("#wd_password").blur(function () {
		if ($("#wd_password").val() == "") {
			$("#wd_password").hide();
			$("#wd_password_dummy").show();
		}
	});

	nrCase = $(".case").length;
	// Sättter höjden på iframen för anmälning till aktiviteter
	var iHeight = $(".iframeContent").height();
	//console.log($(".iframeContent").length);
	//if ($(".iframeContent").html() != "") {
	if ($(".iframeContent").length > 0) {
		if (iHeight > 0) {
			$("iframe").attr("height", iHeight + 50);
		}
		else {
			$("iframe").attr("height", 2000);
		}

		$(".iframeContent").html("");
	}




	$("#content-side img").each(function () {

		if ($(this).width() == 200) {
			var ratio = $(this).width() / $(this).height()
			$(this).css("width", 225);
			$(this).css("height", 225 / ratio);
		}
	});

	$("#content-main table").each(function () {

		if ($(this).width() > 490) {

			$(this).css("width", 470);

		}
	});

	$("#content-side .customBorderGrey").each(function () {
		$(this).css("width", 225);
	});



	for (var i = 0; i < nrCase; i++) {
		$(".indexBox").append("<a href='javascript:void(0)'>&nbsp;</a>")
	}

	$(".indexBox a").eq(0).addClass("active");
	Cufon.replace("#content-main h1,#startContent h1, .caseText p, #campaign-page h1");



	$(".selectArea").hover(function (event) {
		$(this).find(".areas").show();
	},
    function () {
    	$(this).find(".areas").hide();
    });


	$("#menu .level1, .toplinks .shortcuts").hover(function (event) {
		//$(this).find("ul").slideDown(200);
		$(this).find("ul").show();
	},
    function () {
    	//$(this).find("ul").slideUp(200);
    	$(this).find("ul").hide();
    });

	//Funktionalitet för projektbilder

	$("#gallery .case").eq(0).fadeIn(1000);
	//showPreview(0);

	$("#gallery").hover(function () {
		abortTimer();
		$(".prevNext").fadeIn(300);


	}, function () {
		abortTimer();
		tid = setInterval("showNext()", slotTime);
		$(".prevNext").fadeOut(300);
	})


	$("#gallery .indexBox a").click(function () {
		showPreview($(this).index());
	});
	$(".areas a").click(function () {
		//console.log($(this).attr("omrade"));
		FilterOmrade($(this).attr("omrade"));
		$(".selectHeader a").eq(0).html($(this).attr("omrade"))
		$(".areas").hide();
	});

	//search focus
	$("#searchtext").focus(function () {
		if ($("#searchtext").val() == "Sök på atea") {
			$("#searchtext").val("")
		}

	})
	$("#searchtext").blur(function () {
		if ($("#searchtext").val() == "") {
			$("#searchtext").val("Sök på atea");
		}
	})

	//new menu stuff
	// $("#pagemenu > li > ul,#offermenu > li > div").hide();
	$("#pagemenu > li").has("a.selected").children("ul").show();
	$("#pagemenu > li").has("a.selected").children("a.showMenu").addClass("opened");
	$("#offermenu > li:has(a)").hover(function () {
		$(this).addClass("hover");
		$("#offermenu").css("backgroundPosition", "0 -27px");

	},
	function () {
		$(this).removeClass("hover");
		$("#offermenu").css("backgroundPosition", "0 0");

	});
	$("#offermenu > li:has(a)").hoverIntent({
		over: showOfferMenu,
		interval: 200,
		timeout: 200,
		out: hideOfferMenu
	});
	$("#pagemenu > li > a.showMenu").click(function () {
		$(this).toggleClass("opened");
		$(this).parent().children("ul").slideToggle(200);
	});

});
function showOfferMenu() {
	$(this).find("div").show();
}
function hideOfferMenu() {
	$(this).find("div").hide();
}

function showNext() {
    abortTimer();        
    if (currentGalleryIndex + 1 < $("#gallery .case").length) {

        showPreview(currentGalleryIndex + 1);
    }
    else {
        
        showPreview(0);
    }    
}

function showPrev() {
    abortTimer();
    if (currentGalleryIndex - 1 >= 0) {
        showPreview(currentGalleryIndex - 1);
    }
    else {
        
        showPreview($("#gallery .case").length - 1);
    }
    
    
}

function abortTimer() {
    clearInterval(tid);
}


function showPreview(i) {

    if (i != currentGalleryIndex) {
        $("#gallery .case").eq(currentGalleryIndex).fadeOut(300, function () {
            $("#gallery .case").eq(i).fadeIn(300);
            $("#gallery .indexBox a").removeClass();
            $("#gallery .indexBox a").eq(i).addClass("active");
        });
        abortTimer();
        currentGalleryIndex = i;
        tid = setInterval("showNext()", slotTime);
        //console.log(tid);
    }
}

function FilterOmrade(chosenOmrade) {
    //var chosenOmrade = $(dd).find('option:selected').val();
    //var chosenOmrade = $(dd).find('option:selected').val();
    if (chosenOmrade != "") {
        if (chosenOmrade == "Alla") {
            $("#referensUL li").show();
        }
        else {
            $("#referensUL li[omrade='" + chosenOmrade + "']").show();
            $("#referensUL li[omrade!='" + chosenOmrade + "']").hide();

            $("#referensSearch").val("");

        }
        
    }
    else {
        $("#referensUL li").show();
    }
}

function FilterList(searchtxtbox) {
    //FilterOmrade($("#OmradeSelect"));
    var filterText = $(searchtxtbox).val();
    var chosenOmrade = $(".selectHeader a:first").text();

    if (chosenOmrade == 'Områden' || chosenOmrade == 'Alla') {
        $("#referensUL li").show();
        //$("#referensUL .referensContent").text().not(":contains(" + filterText + ")").parent().hide();
        $("#referensUL .referensContent").not(":containsi(" + filterText + ")").parent().hide();
    }
    else {
        $("#referensUL li").show();
        $("#referensUL li[omrade!='" + chosenOmrade + "']").hide();
        $("#referensUL li:visible .referensContent").not(":containsi(" + filterText + ")").parent().hide();
        $("#referensUL li:visible .referensContent").children().text().not(":containsi(" + filterText + ")").parent().hide();
    }
    
    //$("#referensUL .referensContent").not(":contains(" + filterText + ")").parent().css("border", "1px solid red");
      }
      function EshopLogin() {
      	if ($("#wd_username").val() != "" && $("#wd_username").val() != "Användarnamn" && $("#wd_password").val() != "") {
      		document.loginform.submit();
      		return true;
      	}
      	else return false;
      }
