$(window).load(function(){
    equalize();
})
$(function(){
    scroller();
    filters();
    mainPushing();
    extWindows();
	popBox();
	printMe();
    boxLinks();
    bankTip();
})
$("html").addClass("JS");
function equalize(){
    $("div.interviewBoxes div.box").equalHeights();
    $("div.contentBoxes div.contentBox").equalHeights();
    $("div.subContentBoxes > div").equalHeights(50,200);
}
function scroller(){
    var $thePushes = $("#contentScroller div.push,")
    var $theScroll = $("#contentScroller div.scroller")
    $thePushes.bind("click",function(){
        window.location.href = $("a:first",this).attr("href");
    })
    $thePushes.bind("mouseenter",function(){
        $(".info",this).fadeIn();
    })
    $thePushes.bind("mouseleave",function(){
        $(".info",this).fadeOut();
    })
    $theScroll.wrap('<div></div>');
    scrollerWidth = 916;
    scrollerHeight = 330;
    $theScroll.parent().css({
        width: scrollerWidth + "px",
        height: scrollerHeight + "px",
        position: "relative",
        left:"2px",
        overflow:"hidden"
    });
    $theScroll.css({
        width: (scrollerWidth + 4) + "px",
        height: scrollerHeight + "px",
        position: "absolute",
        top:0,
        left:0,
        overflow:"hidden"
    });
    theCounter = 1
    $("#contentScroller > a").click(function(e){
       e.preventDefault();
       if(theCounter == 1){
            theCounter++
            theLink = $(this).attr("href").split("?");
            theHref= theLink[0]
            theQuery = theLink[1].split("=");
            theData = theQuery[1] 
            $theScroll.queue()
            var theWhere = $(this).attr("class");
            $.get(theHref,{start : theData}, function(data) {
                linkPrev = $(data).find("a.prev").attr("href");
 	            linkNext = $(data).find("a.next").attr("href");
	            $("#contentScroller a.prev").attr("href",linkPrev) ;
	            $("#contentScroller a.next").attr("href",linkNext) ;
                if(theWhere == "prev"){
	                $theScroll.prepend('<div class="push">' + $(data).find("#contentScroller div.push:first").html() + '</div>');
	                $theScroll.css({
	                    left: (0 - (scrollerWidth/4)) + "px",
	                    width:(scrollerWidth + (scrollerWidth/4)) + "px"
	                });
	                $theScroll.animate({
	                    left: 0
	                },500,"swing", function(){
	                    $theScroll.find(".push:last").remove()
	                    $theScroll.css({
	                        left: 0,
	                        width:(scrollerWidth) + "px"
    	                });
                        theCounter --;
	                });
	            } else {
	                $theScroll.append('<div class="push">' + $(data).find("#contentScroller div.push:last").html() + '</div>');
	                $theScroll.css({
	                    left: 0,
	                    width:(scrollerWidth +(scrollerWidth/4)) + "px"
	                })
	                $theScroll.animate({
	                    left:(0 - (scrollerWidth/4)) + "px"
	                },500,"swing", function(){
	                    $theScroll.find(".push:first").remove()
	                    $theScroll.css({
	                        left: 0,
	                        width:(scrollerWidth) + "px"
	                    });
                        theCounter = 1;
    	           
	                });
	            }
	            $(document).ajaxError(function(){
	                theCounter = 1;
	            })
	            $thePushes = $("#contentScroller div.push")
                $thePushes.bind("click",function(){
                    window.location.href = $("a:first",this).attr("href");
                });
                $thePushes.bind("mouseenter",function(){
                    $(".info",this).fadeIn();
                });
                $thePushes.bind("mouseleave",function(){
                    $(".info",this).fadeOut();
                });
	        });
	    }
    });
}
function filters(){
    $("div.filter p").click(function(){
        $("div.filter ul").slideToggle(200,function(){
            $("div.filter .cleaner").toggle();
 
        })
        $("div.filter p").toggleClass("white")
    })
     $("div.filter :radio,div.filter :checkbox").click(function(e){
        formData = $("form").serialize();
        theHref = $("form").attr("action");
        $.post(theHref, formData, function(data) {
	        $(".filterFine").html($(data).find(".filterFine").html());
        });
     });
     $("div.filter .cleaner").click(function(){
        $(".filter ul").each(function(){
            $("li:first :radio",this).attr("checked","checked");
        })
        $(".filter :checkbox").removeAttr("checked");
           formData = $("form").serialize();
            theHref = $("form").attr("action");
            $.post(theHref, formData, function(data) {
	        $(".filterFine").html($(data).find(".filterFine").html());
        });
     })
}
function extWindows(){
	$("a[rel=external], area[rel=external]").attr("target","_blank");
	$("a[rel^=popup], area[rel^=popup]").click(function(){
		theHref = $(this).attr("href");
		var popupWidth, popupHeight
		var relSplit = $(this).attr("rel").split("|");
		if(relSplit[1]){
			popupWidth = relSplit[1];
			popupHeight = relSplit[2];
		} else {
			popupWidth = 560;
			popupHeight = 620;
		}
		popMeUp(theHref,popupWidth,popupHeight)
		return false;
	});
}

function popMeUp(strURL, strWidth, strHeight) {
    theWin = window.open(strURL, "popupWin", "scrollbars,resizable,height=" + strHeight + ",width=" + strWidth);
    theWin.focus();
}
function popBox(){
    $("a[rel*='popbox']").click(function(e){
        var theHref = $(this).attr("href");
        var theRel = $(this).attr("rel")
        if((/small/).test(theRel)){
            var theformClass = "popupSmall";
        } else {
            var theformClass = "popupLarge";
        } 
        
        popABox(e,theHref,theformClass)
        
    });
   
}
function popABox(e,theHref,theformClass) {
        e.preventDefault();
        scrollTop = $(window).scrollTop();
        
        
        $("body").append('<div class="popupBg"></div>');
        $("body").append('<form action="' + theHref + '" method="post" class="' + theformClass + '"><div class="popupTop"></div><div class="popupBody"></div><img src="/UI/Images/button.pop.close.png" class="popupClose"/><div class="popupBottom"></div></form>');
        theLeft = (($(window).width() / 2) - ($("." + theformClass).width() / 2)) + "px";
        theTop = ($("." + theformClass).height()>($(window).height()-20))?(10 + scrollTop) + "px":(($(window).height() / 2) - ($("." + theformClass).height() / 2) + scrollTop) + "px";
        $("." + theformClass).css({
            display:"none",
            left:theLeft
        });
	    $.get(theHref, function(data) {
	        if($(data).find("#content").length > 0) {
		        $(".popupBody").html('<div id="content" class="form">' + $(data).find("#content").html() + '</div>')
		    } else {
		        $(".popupBody").html('<div id="content">' + $(data).find("#popContent").html() + '</div>')
		    }
            theTop = ($("." + theformClass).height()>($(window).height()-20))?(10 + scrollTop) + "px":(($(window).height() / 2) - ($("." + theformClass).height() / 2) + scrollTop) + "px"
            $("." + theformClass).css({
                display:"block",
                top:theTop 
            });
        });
        $("form." + theformClass).submit(function(e){
            e.preventDefault();
	        formData = $("form").serialize();
	        $.post(theHref, formData, function(data) {
		        $(".popupBody").html('<div id="content" class="form">' + $(data).find("#content").html() + '</div>')
	        });
        });
        $("#content select").livequery("change",function(){
	        formData = $("form").serialize();
	        $.post(theHref, formData, function(data) {
		        $(".popupBody").html('<div id="content" class="form">' + $(data).find("#content").html() + '</div>')
	        });
        });
        $(".popupBg,.popupClose").click(function(){
            $(".popupBg").remove();
            $("." + theformClass).remove();
            $("#content select").die("change");
        });
        return false;
}
function printMe() {
	if(window.print) {
		$("#printMe").html('<span>Skriv ut <img src="/UI/Images/icon.print.png" /></span>');
		$("#printMe span:first").addClass("printButton");
		$("#printMe span:first").click(function(){
			window.print();
		});
	}
}

function mainPushing() {
    $("#mainPush div.mainPush:first").show()
    //$("#mainPush div.mainPush:first").addClass("active");
    if($("#mainPush div.mainPush").size()<=1) return;
    var $pushWhere = 0

    $theBoxes = $("#mainPush .mainPush");
    $theBoxesLength = $theBoxes.size();
    $("#mainPush").append('<div id="mainPushButtons"></div>')
    $theButtons = ""
    $theBoxes.each(function(){
        $theButtons += '<img src="/UI/Images/button.toppush.stop.png" alt="*"/>';
    })
    $("#mainPushButtons").append($theButtons)
    $("#mainPushButtons img:first").attr("src","/UI/Images/button.toppush.run.png").addClass("mainPushButtonActive");
    $theButtons = $("#mainPushButtons img")
   // $(document).oneTime(3000,"mainPushAnim", function(i) {
        $(document).everyTime(10000,"mainPushAnim", function(i) {
            if($pushWhere == ($theBoxesLength - 1)){
                $pushWhere = 0
            } else {
                $pushWhere ++
            }
            $theBoxes.fadeOut();
            $theBoxes.eq($pushWhere).fadeIn();
            $theButtons.attr("src","/UI/Images/button.toppush.stop.png").removeClass("mainPushButtonActive");
            $theButtons.eq($pushWhere).attr("src","/UI/Images/button.toppush.run.png").addClass("mainPushButtonActive");
            
        });
   // });
    $theButtons.click(function(){
        $theIndex = $("#mainPushButtons img").index(this);
        $pushWhere = $theIndex
       
        if($(this).hasClass("mainPushButtonActive")){
            $(this).attr("src","/UI/Images/button.toppush.pause.png").removeClass("mainPushButtonActive").addClass("mainPushButtonPassive");
            $(document).stopTime("mainPushAnim");
        } else if($(this).hasClass("mainPushButtonPassive")){
            $pushWhere = $theIndex
            $(this).attr("src","/UI/Images/button.toppush.run.png").addClass("mainPushButtonActive").removeClass("mainPushButtonPassive");
   //         $(document).oneTime(3000,"mainPushAnim", function(i) {
                $(document).everyTime(10000,"mainPushAnim", function(i) {
                    if($pushWhere == ($theBoxesLength - 1)){
                        $pushWhere = 0;
                    } else {
                        $pushWhere ++;
                    }
                    $theBoxes.fadeOut();
                    $theBoxes.eq($pushWhere).fadeIn();
                    $theButtons.attr("src","/UI/Images/button.toppush.stop.png").removeClass("mainPushButtonActive").removeClass("mainPushButtonPassive");
                    $theButtons.eq($pushWhere).attr("src","/UI/Images/button.toppush.run.png").addClass("mainPushButtonActive");
                    
                });
     //       });

        } else {
            $theButtons.attr("src","/UI/Images/button.toppush.stop.png").removeClass("mainPushButtonActive").removeClass("mainPushButtonPassive");
            $(this).attr("src","/UI/Images/button.toppush.pause.png").addClass("mainPushButtonPassive");
            $(document).stopTime("mainPushAnim");
            $theBoxes.fadeOut();
            $theBoxes.eq($theIndex).fadeIn();
        }
    });
}

function boxLinks(){
    $("div.box a[rel*='popbox']").each(function(){     
        $(this).click(function(e){
            e.preventDefault();
            return false;
        });
        $(this).parents("div.box").css("cursor","pointer");
        var theHref = $(this).attr("href");
        var theRel = $(this).attr("rel")
        if((/small/).test(theRel)){
            var theformClass = "popupSmall";
        } else {
            var theformClass = "popupLarge";
        } 
        $(this).parents("div.box").click(function(e){
            e.preventDefault();
            popABox(e,theHref,theformClass)
        });
    })
    $("div.box a:not([rel*='popbox'])").each(function(){
        $(this).click(function(e){
            e.preventDefault();
        });
        $(this).parents("div.box").css("cursor","pointer");
        var theHref = $(this).attr("href");
        $(this).parents("div.box").click(function(){
            window.location.href = theHref;
        });
    })
}
function bankTip(){
    $("div.popupBody div.bankTip h3").live("click",function(){
        if ($(this).parent().find("ul.theTip").css("display") == "none"){
            $("div.popupBody div.bankTip ul.theTip").slideUp()
            $(this).parent().find("ul.theTip").slideDown()
            $("div.popupBody div.bankTip h3").removeClass("active")
            $(this).addClass("active")
        }   else{
            $(this).parent().find("ul.theTip").slideUp()
            $(this).removeClass("active")
        }
       
    })
}
