﻿// IE fix for setTimeout/setInterval (Now you can include args like this in ie too: setTimeout(function,interval,arg1,arg2)
/*@cc_on
(function(f){
 window.setTimeout =f(window.setTimeout);
 window.setInterval =f(window.setInterval);
})(function(f){return function(c,t){var a=[].slice.call(arguments,2);return f(function(){c.apply(this,a)},t)}});
@*/

// ColorBox v1.2.9b - a full featured, light-weight, customizable lightbox based on jQuery 1.3
(function(C){var I,X,Y,d,A,k,b,H,c,T,F,f,s,j,m,R,l,J,t,E,Z,i,g,a,w,S,p,B,V,x,K,q="colorbox",o="hover",z,e,U,N,M,L,r,O,y="cbox_open",Q="cbox_load",u="cbox_complete",h="cbox_close",n="cbox_closed",D={transition:"elastic",speed:350,width:false,height:false,initialWidth:"400",initialHeight:"400",maxWidth:false,maxHeight:false,resize:true,inline:false,html:false,iframe:false,photo:false,href:false,title:false,rel:false,opacity:0.9,preloading:true,current:"Bild {current} av {total}",previous:"previous",next:"next",close:"close",open:false,overlayClose:true,slideshow:false,slideshowAuto:true,slideshowSpeed:2500,slideshowStart:"start slideshow",slideshowStop:"stop slideshow"};function P(aa){if(aa.keyCode===37){aa.preventDefault();J.click()}else{if(aa.keyCode===39){aa.preventDefault();l.click()}}}function G(aa,ab){ab=ab==="x"?document.documentElement.clientWidth:document.documentElement.clientHeight;return(typeof aa==="string")?(aa.match(/%/)?(ab/100)*parseInt(aa,10):parseInt(aa,10)):aa}function v(aa){return V.photo?true:aa.match(/\.(gif|png|jpg|jpeg|bmp)(?:\?([^#]*))?(?:#(\.*))?$/i)}function W(){for(var aa in V){if(typeof(V[aa])==="function"){V[aa]=V[aa].call(p)}}}C.fn.colorbox=function(ab,aa){if(this.length){this.each(function(){var ac=C(this).data(q)?C.extend({},C(this).data(q),ab):C.extend({},D,ab);C(this).data(q,ac).addClass("cboxelement")})}else{C(this).data(q,C.extend({},D,ab))}C(this).unbind("click.colorbox").bind("click.colorbox",function(ad){p=this;V=C(p).data(q);W();C().bind("keydown.cbox_close",function(ae){if(ae.keyCode===27){ae.preventDefault();t.click()}});if(V.overlayClose===true){I.css({cursor:"pointer"}).one("click",O)}p.blur();K=aa||false;var ac=V.rel||p.rel;if(ac&&ac!=="nofollow"){c=C(".cboxelement").filter(function(){var ae=C(this).data(q).rel||this.rel;return(ae===ac)});B=c.index(p);if(B<0){c=c.add(p);B=c.length-1}}else{c=C(p);B=0}if(!x){C.event.trigger(y);t.html(V.close);I.css({opacity:V.opacity}).show();x=true;M(G(V.initialWidth,"x"),G(V.initialHeight,"y"),0);if(C.browser.msie&&C.browser.version<7){T.bind("resize.cboxie6 scroll.cboxie6",function(){I.css({width:T.width(),height:T.height(),top:T.scrollTop(),left:T.scrollLeft()})}).trigger("scroll.cboxie6")}}r();N();ad.preventDefault()});if(ab&&ab.open){C(this).triggerHandler("click.colorbox")}return this};U=function(){function aa(ab){return C('<div id="cbox'+ab+'"/>')}T=C(window);X=C('<div id="colorbox"/>');I=aa("Overlay").hide();Y=aa("Wrapper");d=aa("Content").append(F=aa("LoadedContent").css({width:0,height:0}),f=aa("LoadingOverlay"),s=aa("LoadingGraphic"),j=aa("Title"),m=aa("Current"),R=aa("Slideshow"),l=aa("Next"),J=aa("Previous"),t=aa("Close"));Y.append(C("<div/>").append(aa("TopLeft"),A=aa("TopCenter"),aa("TopRight")),C("<div/>").append(k=aa("MiddleLeft"),d,b=aa("MiddleRight")),C("<div/>").append(aa("BottomLeft"),H=aa("BottomCenter"),aa("BottomRight"))).children().children().css({"float":"left"});C("body").prepend(I,X.append(Y));if(C.browser.msie&&C.browser.version<7){I.css("position","absolute")}d.children().addClass(o).mouseover(function(){C(this).addClass(o)}).mouseout(function(){C(this).removeClass(o)}).hide();Z=A.height()+H.height()+d.outerHeight(true)-d.height();i=k.width()+b.width()+d.outerWidth(true)-d.width();g=F.outerHeight(true);a=F.outerWidth(true);X.css({"padding-bottom":Z,"padding-right":i}).hide();l.click(e);J.click(z);t.click(O);d.children().removeClass(o)};M=function(ad,ac,ab,ae){var af=document.documentElement.clientHeight,ah=af/2-ac/2,ag=document.documentElement.clientWidth/2-ad/2,aa;if(ac>af){ah-=(ac-af)}if(ah<0){ah=0}if(ag<0){ag=0}ah+=T.scrollTop();ag+=T.scrollLeft();ad=ad-i;ac=ac-Z;aa=(X.width()===ad&&X.height()===ac)?0:ab;Y[0].style.width=Y[0].style.height="9999px";function ai(aj){A[0].style.width=H[0].style.width=d[0].style.width=aj.style.width;s[0].style.height=f[0].style.height=d[0].style.height=k[0].style.height=b[0].style.height=aj.style.height}X.dequeue().animate({height:ac,width:ad,top:ah,left:ag},{duration:aa,complete:function(){ai(this);Y[0].style.width=(ad+i)+"px";Y[0].style.height=(ac+Z)+"px";if(ae){ae()}},step:function(){ai(this)}})};L=function(af){if(!x){return}T.unbind("resize.cbox_resize");var ab,al,ag,ae,ac,ai,am,aa,ad=V.transition==="none"?0:V.speed;F.remove();F=C(af);function aj(){if(V.width){ab=w}else{ab=w&&w<F.width()?w:F.width()}return ab}function ah(){if(V.height){al=S}else{al=S&&S<F.height()?S:F.height()}return al}F.hide().appendTo("body").attr({id:"cboxLoadedContent"}).css({width:aj()}).css({height:ah()}).prependTo(d);if(C.browser.msie&&C.browser.version<7){C("select:not(#colorbox select)").filter(function(){return C(this).css("visibility")!=="hidden"}).css({visibility:"hidden"}).one(h,function(){C(this).css({visibility:"inherit"})})}aa=C("#cboxPhoto")[0];if(aa&&V.height){ag=(al-parseInt(aa.style.height,10))/2;aa.style.marginTop=(ag>0?ag:0)+"px"}function ak(ao){var an=ab+a+i,ap=al+g+Z;M(an,ap,ao,function(){if(!x){return}if(C.browser.msie){if(aa){F.fadeIn(100)}X.css("filter","")}d.children().show();C("#cboxIframeTemp").after("<iframe id='cboxIframe' name='iframe_"+new Date().getTime()+"' frameborder=0 src='"+(V.href||p.href)+"' />").remove();f.hide();s.hide();R.hide();if(c.length>1){m.html(V.current.replace(/\{current\}/,B+1).replace(/\{total\}/,c.length));l.html(V.next);J.html(V.previous);C().unbind("keydown",P).bind("keydown",P);if(V.slideshow){R.show()}}else{m.hide();l.hide();J.hide()}j.html(V.title||p.title);C.event.trigger(u);if(K){K.call(p)}if(V.transition==="fade"){X.fadeTo(ad,1,function(){if(C.browser.msie){d.css("filter","")}})}T.bind("resize.cbox_resize",function(){M(an,ap,0)})})}if(V.transition==="fade"){X.fadeTo(ad,0,function(){ak(0)})}else{ak(ad)}if(V.preloading&&c.length>1){ae=B>0?c[B-1]:c[c.length-1];ai=B<c.length-1?c[B+1]:c[0];am=C(ai).data(q).href||ai.href;ac=C(ae).data(q).href||ae.href;if(v(am)){C("<img />").attr("src",am)}if(v(ac)){C("<img />").attr("src",ac)}}};N=function(){var aa,ad,ab,ac;p=c[B];V=C(p).data(q);W();C.event.trigger(Q);aa=V.height?G(V.height,"y")-g-Z:false;ad=V.width?G(V.width,"x")-a-i:false;ab=V.href||p.href;f.show();s.show();t.show();if(V.maxHeight){S=V.maxHeight?G(V.maxHeight,"y")-g-Z:false;aa=aa&&aa<S?aa:S}if(V.maxWidth){w=V.maxWidth?G(V.maxWidth,"x")-a-i:false;ad=ad&&ad<w?ad:w}S=aa;w=ad;if(V.inline){C('<div id="cboxInlineTemp" />').hide().insertBefore(C(ab)[0]).bind(Q+" "+h,function(){F.children().insertBefore(this);C(this).remove()});L(C(ab).wrapAll("<div/>").parent())}else{if(V.iframe){L(C("<div><div id='cboxIframeTemp' /></div>"))}else{if(V.html){L(C("<div/>").html(V.html))}else{if(v(ab)){ac=new Image();ac.onload=function(){ac.onload=null;if((S||w)&&V.resize){var ag=this.width,ae=this.height,ai=0,ah=this,af=function(){ae+=ae*ai;ag+=ag*ai;ah.height=ae;ah.width=ag};if(w&&ag>w){ai=(w-ag)/ag;af()}if(S&&ae>S){ai=(S-ae)/ae;af()}}L(C("<div />").css({width:this.width,height:this.height}).append(C(this).css({width:this.width,height:this.height,display:"block",margin:"auto",border:0}).attr("id","cboxPhoto")));if(c.length>1){C(this).css({cursor:"pointer"}).click(e)}if(C.browser.msie&&C.browser.version==7){this.style.msInterpolationMode="bicubic"}};ac.src=ab}else{C("<div />").load(ab,function(ae,af){if(af==="success"){L(C(this))}else{L(C("<p>Request unsuccessful.</p>"))}})}}}}};e=function(){B=B<c.length-1?B+1:0;N()};z=function(){B=B>0?B-1:c.length-1;N()};r=function(){var ab,aa,ac="cboxSlideshow_";R.bind(h,function(){clearTimeout(aa);R.unbind()});function ad(){R.text(V.slideshowStop).bind(u,function(){aa=setTimeout(e,V.slideshowSpeed)}).bind(Q,function(){clearTimeout(aa)}).one("click",function(){ab();C(this).removeClass(o)});X.removeClass(ac+"off").addClass(ac+"on")}ab=function(){clearTimeout(aa);R.text(V.slideshowStart).unbind(u+" "+Q).one("click",function(){ad();aa=setTimeout(e,V.slideshowSpeed);C(this).removeClass(o)});X.removeClass(ac+"on").addClass(ac+"off")};if(V.slideshow&&c.length>1){if(V.slideshowAuto){ad()}else{ab()}}};O=function(){C.event.trigger(h);x=false;C().unbind("keydown",P).unbind("keydown.cbox_close");T.unbind("resize.cbox_resize resize.cboxie6 scroll.cboxie6");I.css({cursor:"auto"}).fadeOut("fast");d.children().hide();X.stop(true,false).removeClass().fadeOut("fast",function(){F.remove();X.css({opacity:1});C.event.trigger(n)})};E=C.fn.colorbox;E.init=U;E.next=e;E.prev=z;E.close=O;E.load=N;E.position=M;E.dimensions=L;E.element=function(){return p};E.settings=D;C(function(){U()})}(jQuery));


/* Login
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
var OneClickLogin = {
    _keyPressCount: 0,
	init : function()
	{
		if(this.cssUrl)
		    document.write('<link href="' + this.cssUrl + '" rel="stylesheet" type="text/css" />');
		$(document).bind("keyup", OneClickLogin.eventHandler);
	}, 
	
	eventHandler : function(ev)
	{
		ev = (typeof(ev)== "undefined") ? window.event : ev;
		if(ev.type == "keyup")
			return OneClickLogin.keyHandler(ev);
	}, 
	
	keyHandler : function(ev)
	{
	    var kc = ev.keyCode || ev.which
	    
	    if($("#OneClick_login").length != 0 && kc==27)
	        $("#OneClick_login").remove();
	        
		if(kc == 17 || kc == 32)
			this._keyPressCount++;
		else
			this._keyPressCount = 0;
			
		if(this._keyPressCount > 2)
		{
			window.scrollTo(0,0);
			this.addLoginBox();
			this._keyPressCount = 0;
			this.cancelEvListener(ev);	
			return false;
		}
	}, 
	cancelEvListener : function(ev)
	{
		if(typeof(ev.preventDefault) != "undefined")
		{
			ev.preventDefault();
		}
		ev.returnValue = false;
		ev.cancelBubble = true;
	}, 
	
	addLoginBox : function()
	{
		if($("#ocAdminLogin").length != 0 || $("#ocAdminToolbar").length != 0)
			return;
        $("body").append("<div id='ocAdminLogin'></div>");
        $("#ocAdminLogin").append("<iframe scrolling='no' frameborder='0' hspace='0' src='"+this.loginUrl+'?redirect='+this.scriptName()+ "&random=" + new Date().getTime()+"'></iframe>");

	}, 
	
	hideLoginBox:function()
	{
		if($("#OneClick_login").length != 0)
            $("#OneClick_login").remove();
	},
	scriptName:function()
	{
		var l = location.href;
		if(l.indexOf('http://') == 0)
			l = l.substring(7);
		l = l.substring(l.indexOf("/"));
		return l;		
	}

}
OneClickLogin.loginUrl = "/admin/OneClick_loginbox/OneClick_login.aspx";
//OneClickLogin.cssUrl = "/admin/OneClick_loginbox/oneclick_login.css";
OneClickLogin.init();


/* Popup
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
function checkPopWin(url)
{
	if(popWin == null)
	{
		self.location.href = url;
	}
}

function tmSizedPopup(url,winName,w,h) {	
    var useScroll = arguments[4] == undefined?0:arguments[4];
	popWin = window.open(url,winName,'fullscreen=0,width='+w+',height='+h+',toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars='+useScroll+',resizable=1');
}


/* ImageArea
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
function fileBrowserCallBack(i_fieldName) {
    imageBrowserWin = window.open('/admin/FileManager/','imageBrowser','width=760,height=420,status=yes,toolbar=no,scrollbars=yes,resizable=yes');
    document.imageWinFieldname = i_fieldName;
    imageBrowserWin.focus();
}
function fileBrowserCallBack_return(fileRef)
{
    
    if(document.imageWinRef != null)
        document.imageWinRef.document.forms[0][document.imageWinFieldname].value=fileRef;
    else
        document.forms[0][document.imageWinFieldname].value=fileRef;
    if (document.imageWinFieldname.indexOf("imgLink")>-1)
    {
        var splitName = document.imageWinFieldname.substring("imgLink".length);
        updateImage(splitName, fileRef);
    }
    else if (document.imageWinFieldname.indexOf("swfLink")>-1)
    {
        var splitName = document.imageWinFieldname.substring("swfLink".length);
        updateSWF(splitName, fileRef);
    }
}
function updateImage(i_key, i_url)
{
    
    var w = document.getElementById('width' + i_key).value;
    var h = document.getElementById('height' + i_key).value;
    $.post("/admin/ImageArea_update.aspx",{key: i_key,url: i_url,width: w, height: h}, function(i_val) {
        if (i_val == "width")
        {
            if (confirm("Vald bilds bredd är större än ytan specificerade bredd, vill du ändå fortsätta?"))
            {
                $.post("/admin/ImageArea_update.aspx",{key: i_key,url: i_url,width: w, height: h, confirmed: "true"}, function(i_val2){
                    if (i_val2 == "not_found")
                        alert("Bilden blev borttagen någon gång under processen, kontrollera att den finns!");
                    else
                    {
                        window.location = window.location;
                        window.focus();
                    }
                });
            }
        }
        else if (i_val == "height")
        {
            if (confirm("Vald bilds höjd är större än ytan specificerade höjd, vill du ändå fortsätta?"))
            {
                $.post("/admin/ImageArea_update.aspx",{key: i_key,url: i_url,width: w, height: h, confirmed: "true"}, function(i_val2){
                    if (i_val2 == "not_found")
                        alert("Bilden blev borttagen någon gång under processen, kontrollera att den finns!");
                    else
                    {
                        window.location = window.location;
                        window.focus();
                    }
                });
                
            }
        }
        else if (i_val == "widthheight")
        {
            if (confirm("Bildens storlek är både bredare och högre än ytans storlek, vill du ändå fortsätta?"))
            {
                $.post("/admin/ImageArea_update.aspx",{key: i_key,url: i_url,width: w, height: h, confirmed: "true"}, function(i_val2){
                    if (i_val2 == "not_found")
                        alert("Bilden blev borttagen någon gång under processen, kontrollera att den finns!");
                    else
                    {
                        window.location = window.location;
                        window.focus();
                    }
                });
            }
        }
        else if (i_val == "not_found")
            alert("Bilden blev borttagen någon gång under processen, kontrollera att den finns!");
        else
        {
            window.location = window.location;
            window.focus();
        }
    }); 
}


/* Email-protection
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
function ShowEmail(i_where, i_beforeAt, i_afterAt, i_afterDot, i_additional)
{
    if (i_additional == null)
        i_additional = "";
    builtStr = i_beforeAt + "@" + i_afterAt + "." + i_afterDot;
    $("#" + i_where).append(i_additional + '<a href="mailto:' + builtStr + '">' + builtStr + '</a>');
}

/* SlideShow
------------------------------------------------------------------------------------------------*/
function slideShow($elm) {
	var $active = $elm.find('img.active');
	
	if ( $active.length == 0 ) 
	{
		$active = $elm.find('img:last');
	}

	var $next = ($active.next().length)? $active.next() : $elm.find('img:first');

	$active.addClass('last-active');
	$next.css({opacity: 0.0})
		.addClass('active')
		.animate({opacity: 1.0}, 1000, function() 
		{
			$active.removeClass('active last-active');
		});
}
var currSize = 100;
var currLevel = 0;
$(function() {

    /* SlideShow INIT
    ------------------------------------------------------------------------------------------------*/
    if ($('.slideshow-images img').length > 1) {
        var slideShowInterval = null;
        $('.slideshow-images img:first').addClass('active');
		slideShowInterval = setInterval(slideShow, 5000, $('.slideshow-images'));
    }

    //Text-size controls
    var $incSize = $('<a />').attr({
        id: 'inc',
        title: 'Öka textstorlek'
    }).text('Öka textstorlek');
    var $decSize = $('<a />').attr({
        id: 'dec',
        title: 'Minska textstorlek'
    }).text('Minska textstorlek');
    var incAmount = 10;
    //var currSize = 100;
    var maxLevel = 5;
    var minLevel = -1;
    //var currLevel = 0;


    $('#text-scale').append($incSize, $decSize);
    $incSize.bind('click', function() {
        if (currLevel < maxLevel) {
            $('#content').css('fontSize', (currSize += incAmount) + '%');
            currLevel++;
            $.post("/content/ajax/setTextSize.aspx", { currLevel: currLevel, currSize: currSize });
        }
    });
    $decSize.bind('click', function() {
        if (currLevel > minLevel) {
            $('#content').css('fontSize', (currSize -= incAmount) + '%');
            currLevel--;
            $.post("/content/ajax/setTextSize.aspx", {currLevel:currLevel, currSize:currSize});
        }
    });

    if ($('#finished-projects').length > 0) {

        // Colorbox init
        $('.media a').colorbox();

        $('.long-description').hide();

        $('.project').each(function() {

            var $longDesc = $(this).find('.long-description');
            var $media = $(this).find('.media');
            var $mediaLink = $media.find('a');
            var $mediaImg = $media.find('img');

            if ($longDesc.text().length > 11)
                var $moreLink = $('<a />').attr('class', 'more').text('Läs mer').data('active', false).insertBefore($longDesc);
            if ($mediaImg.length > 0)
                $mediaLink.attr('rel', $mediaLink.attr('href'));

        });
        $('.project .more').bind('click', function() {
            var $this = $(this);
            if ($this.data('active') == true) {
                $this.text('Läs mer').css({ backgroundPosition: '0 -1243px' });
                $this.data('active', false);
            }
            else {
                $this.text('Mindre information').css({ backgroundPosition: '0 -1264px' });
                $this.data('active', true);
            }
            if ($(this).next().is('.long-description')) {
                $(this).next().animate({ height: 'toggle' }, 200);
            }
        });
    }


    // Enhance Admin-Contentareas
    $('.edit-link, .delete-link').css({ display: 'none' });

    $('.newsitem').hover(
	function() {
	    $(this).find('.edit-link, .delete-link').css({ display: 'block' });
	},
	function() {
	    $(this).find('.edit-link, .delete-link').css({ display: 'none' });
	});

    $('.admin-image-area').hover(
	function() {
	    $(this).find('.edit-link').css({ display: 'block' });
	},
	function() {
	    $(this).find('.edit-link').css({ display: 'none' });
	});

    $('.admin-text-area').hover(
	function() {
	    $(this).find('.edit-link').css({ display: 'block' });
	},
	function() {
	    $(this).find('.edit-link').css({ display: 'none' });
	});
});