﻿
$(document).ready(function(){

    /** IE z-index fix helevete!! */
    if($.browser.msie){
        var zIndexNumber = 1000;
	    $("div").each(function() {
		    if($(this).css("zIndex") == 0){
		        $(this).css("zIndex", zIndexNumber);
		        zIndexNumber -= 1;
		    }
	    });
    }

    //cross browser genomskinlighet;
    $(".BlurbText").css("opacity","0.8");
    
    /** popup **/
    $(".PopUp").click(function(e){
        e.preventDefault();
        var url = $(this).attr("href");
        if(url == undefined)
            return;
        var params = "status=0,toolbar=0,location=1,menubar=0,directories=0,resizable=1,scrollbars=1,height=800,width=1010";
        var win = window.open(url, "Bink", params, true);
        try{
        win.focus();
        }
        catch(e){}
    });

    /** Lightbox stuff **/
    /** PRELOADER **/
    $(".lightBobo").each(function(){
        $(this).css("cursor", "pointer");
        
        //load each image to cache...
        var src = "";
        if(this.tagName.toLowerCase() == "a")
            src = this.href;
        else if(this.tagName.toLowerCase() == "img")
            src = this.src;
        if(src != ""){
            if(IsImage(src)){
                var i = new Image();
                i.src = src;
            }
        }
    });
    $(".lightBobo").click(function(e){
        e.preventDefault();
        $(this).lightBobo();
    });
});

function IsImage(filename){
    var ext = filename.split('.').pop().toLowerCase();
    var allow = new Array("gif","png","jpg","jpeg", "bmp");
    if(jQuery.inArray(ext, allow) == -1) {
        return false;
    }
    return true;
}
/// LightBox like style. 
/// Will open an image in an box on an overlay. Add the class lightBobo to an image or link to open the src/href in a lightbox
/// Uses opacity
/// Might not work in IE6
/// Robert van Seggelen
var openImgSrc = "";
jQuery.fn.lightBobo = function(e){
    if(e != undefined)
        e.preventDefault();
    return this.each(function(){
        
        var src = "";
        if(this.tagName.toLowerCase() == "a")
            src = this.href.toLowerCase();
        else if(this.tagName.toLowerCase() == "img")
            src = this.src.toLowerCase();
        else
            return;
            
        if(openImgSrc == src)
            return;
        if(!IsImage(src))
            return;
            
        openImgSrc = src;
        var img = new Image();
        img.src = openImgSrc;
        img.alt = openImgSrc;
        
        
        var imgW = img.width; //$(this).width();
        var imgH = img.height;
        
        if(imgW == undefined)
            return;
        
        if(imgW < 5)
            imgW = 600;
        
        if(imgH < 5)
            imgH = 600;
        
        var winW = $(window).width();  
        var winH = $(window).height();  
        var left = (winW/2)-(imgW/2);
        var top =  (winH/2)-(imgH/2) + $(document).scrollTop();
        top = 40 +$(document).scrollTop();
        //the semitransparant overlay over thw whole page
        var overlay = $("<div />");
        overlay.attr("class", "LightBobo_Overlay");
        overlay.css("width", winW+"px");
        overlay.css("height", $(document).height()+"px");
        overlay.css("opacity","0.8");
        
        //an element to hold our picture
        var container = $("<div />");
        container.attr("class", "LightBobo_Container");
        container.css("width",imgW+"px");
        container.css("position","absolute");
        container.css("top",top+"px");
        container.css("left",left+"px");
        container.css("opacity","1.0");
        
        //a link to close the overlay
        var closeLink = $("<div />");
        closeLink.attr("class", "LightBobo_CloseBtn");
        closeLink.click(function(){
            container.hide();
            overlay.fadeOut("fast");
            img = null;
            container = null;
            overlay = null;
            openImgSrc = "";
        });
        container.append(closeLink);
        container.append(img); //add image to div
        
        //overlay.append(d); //add div to overlay
        $("body").append(overlay); //add overlay to body
        $("body").append(container); //add div to body
        overlay.fadeIn("fast",function(){
            container.show();
        }); //show
        //d.show();
        
        $(overlay).click(function(){
            
            container.hide();
            overlay.fadeOut("fast");
            img = null;
            container = null;
            overlay = null;
            openImgSrc = "";
            
        });
    });
}

