/*
 JS picture popup support (thickbox style)
 GPL Intime It 2010
 --------------------
 dependencies 
   popupbox.css
   jquery
 
*/


// base object with default values and methods
var popupbox = {
  // config vars
  initcalled:false,
  loading_url:jsrelpath+'images/tbf_loading.gif',
  closebut_url:jsrelpath+'images/close_win.png',
  loading_img: new Image(),
//  caption:'',
//  PB_height:550, PB_width:830,  // default size
  PB_maxheight:640,PB_maxwidth:830,
  PB_minmargin:10,
  
  init: function () {
    if (popupbox.initcalled) return;
    popupbox.loading_img.src = popupbox.loading_url; // preload
    popupbox.PB_maxheight = getmyHeight();
    popupbox.PB_maxwidth = getmyWidth();
    popupbox.initcalled = true;
  },
  
  
  showimg : function (url,params) {
    if (typeof params=="undefined") params = {};

    if (typeof params.height == "undefined") params.height = 0;
    if (typeof params.width == "undefined")  params.width = 0;
    if (typeof params.caption == "undefined") params.caption='';
    
    img_border_height = 100;
    img_border_width = 30;
    
    popupbox.init_win(params);
     
	PB_PrevCaption = "";
	PB_PrevURL = "";
	PB_PrevHTML = "";
	PB_NextCaption = "";
	PB_NextURL = "";
	PB_NextHTML = "";
	PB_imageCount = "";
	PB_FoundURL = false;
	if(params.imageGroup){
		PB_TempArray = $("a[@rel="+imageGroup+"]").get();
		
		PB_Counter = 0;
		while ((PB_Counter < PB_TempArray.length) && (!PB_FoundURL)) {
		
		  var hrf = PB_TempArray[PB_Counter].href; hrf = hrf.replace(/%20/g,' ');
		  var urf = url.replace(/%20/g,' ');
	
		  if (hrf.indexOf(urf)>=0) { 
		    PB_FoundURL=true;
		  } else {
		    PB_Counter++;
		  }
		};
		
		if (!PB_FoundURL) {
		  alert('Internal error, contact webmaster. Picture not found:'+url);  
		} else {
		  if (PB_Counter>0) {
		     PB_Prvidx = PB_Counter-1;
			 PB_PrevCaption = PB_TempArray[PB_Prvidx].title;
			 PB_PrevURL = PB_TempArray[PB_Prvidx].href;
			 PB_PrevHTML = "<span id='PB_prev'>&nbsp;&nbsp;<a href='"+PB_Prvidx+"'>&lt;&lt; Forrige</a></span>";
		  }
		  if (PB_Counter < (PB_TempArray.length-1)) {
		     PB_Nxtidx = PB_Counter+1;
			 PB_NextCaption = PB_TempArray[PB_Nxtidx].title;
			 PB_NextURL = PB_TempArray[PB_Nxtidx].href;
			 PB_NextHTML = "<span id='PB_next'>&nbsp;&nbsp;<a href='"+PB_Nxtidx+"'>N&#230ste &gt;&gt;</a></span>";
		  }
		  PB_imageCount = "" + (PB_Counter + 1) +" af "+ (PB_TempArray.length);											
		  if (caption=="") caption=PB_TempArray[PB_Counter].title;
		}
	} // endif imagegroup

	imgPreloader = new Image();
	imgPreloader.onload = function(){		
	  imgPreloader.onload = null;
	  
	  // fit to size?
	  if (params.height==0) {params.height = imgPreloader.height;}
	  if (params.width==0) {params.width= imgPreloader.width;}
	  
	  // too big to fit?
	  var h_maximg = popupbox.PB_maxheight-popupbox.PB_minmargin-img_border_height;
	  var w_maximg = popupbox.PB_maxwidth-popupbox.PB_minmargin-img_border_width;
	  if (params.height>h_maximg) {
	      params.width *= (h_maximg / params.height);
	      params.height = h_maximg;
	      if (params.width>w_maximg) {
  	         params.height *= (w_maximg / params.width);
	         params.width = w_maximg;
	      }
	  } else if (params.width>w_maximg) {
  	      params.height *= (w_maximg / params.width);
          params.width = w_maximg;
	      if (params.height>h_maximg) {
 	         params.width *= (h_maximg / params.height);
	         params.height = h_maximg;
	      }
	  }
	  
	
//	  PB_width = imageWidth + 30;
//	  PB_height = imageHeight + 60;
	  $("#PB_window").append(
	  
	      "<table cellpadding='0' cellspacing='0' border='0' width='100%'>"
	     +"<tr id='PB_topLine' height='20px' valign='middle'>"
	      +"<td><div id='PB_caption' align='left'>" + params.caption + "</div></td>"
	      +"<td><div id='PB_closeWindow' align='right'><a href='#' id='PB_closeWindowButton' title='Luk'><img width='16px' style='border:0' src='"+popupbox.closebut_url+"'></a></div></td>"
         +"</tr>"
         +"<tr><td colspan='2'>"
	       +"<a href='' id='PB_ImageOff' title='"+params.caption+"'>"
	         +"<img id='PB_Image' src='"+url +"' width='"+params.width+"' height='"+params.height+"' alt='"+params.caption+"'/>"
	       +"</a>"
	     +"</td></tr>"
	     
	     +"<tr><td colspan='2'>"
           +"<div id='PB_secondLine' align='center'>" + PB_PrevHTML +'&nbsp;&nbsp;'+ PB_imageCount +'&nbsp;&nbsp;'+ PB_NextHTML + "</div>"
	     +"</td></tr>"
	     +"</table>"
	     
/*			     
	     
	     
	     "<div id='PB_closeWindow'><a href='#' id='PB_closeWindowButton' title='Luk'>[x] Luk</a></div>"
	  
	    +"<a href='' id='PB_ImageOff' title='Close'>"
	    +"<img id='PB_Image' src='"+url +"' width='"+imageWidth+"' height='"+imageHeight+"' alt='"+caption+"'/>"
	  +"</a>"
	   
	  +"<div id='PB_caption'>" + caption + "</div>"
	  + "<div id='PB_secondLine'>" + PB_PrevHTML + PB_imageCount + PB_NextHTML + "</div>"
	  +""
	  
//			  +"<div id='PB_closeWindow'><a href='#' id='PB_closeWindowButton' title='Luk'>[x] Luk</a></div>"
*/
	); // onload ends 		
	
	$("#PB_closeWindowButton").click(popupbox.remove);
	
	if (!(PB_PrevHTML === "")) {
		function goPrev(){
			$("#PB_window").remove();
			$("body").append("<div id='PB_window'></div>");
			params.caption = PB_PrevCaption;
			popupbox.showimg(PB_PrevURL, params);
			return false;	
		}
		$("#PB_prev").click(goPrev);
	}
	
	if (!(PB_NextHTML === "")) {		
		function goNext(){
			$("#PB_window").remove();
			$("body").append("<div id='PB_window'></div>");
			params.caption = PB_NextCaption;
			popupbox.showimg(PB_NextURL, params);				
			return false;	
		}
		$("#PB_next").click(goNext);
		
	}

	document.onkeydown = function(e){ 	
		if (e == null) { // ie
			keycode = event.keyCode;
		} else { // mozilla
			keycode = e.which;
		}
		if(keycode == 27){ // close
			popupbox.remove();
		} else if( (keycode==190) || (keycode==40) || (keycode==39) ){ 
			if(!(PB_NextHTML == "")){
				document.onkeydown = "";
				goNext();
			}
		} else if( (keycode==188) || (keycode==37) || (keycode==38) ){ 
			if(!(PB_PrevHTML == "")){
				document.onkeydown = "";
				goPrev();
			}
		}	
	};
	
	popupbox.position(params.width+img_border_width,params.height+img_border_height);
	$("#PB_load").remove();
	$("#PB_ImageOff").click(popupbox.remove);
	$("#PB_window").css({display:"block"}); //for safari using css instead of show
	};
	
	imgPreloader.src = url;
  
  },// end function showimg

  
  init_win: function (params) {
  
       if(!popupbox.initcalled) popupbox.init();
  

//       if(popupbox.PB_height>popupbox.PB_maxheight) popupbox.PB_height = popupbox.PB_maxheight-10;
//       if(popupbox.PB_width>popupbox.PB_maxwidth) popupbox.PB_width= popupbox.PB_maxwidth-10;
       
		if (typeof document.body.style.maxHeight === "undefined") {//if IE 6
			$("body","html").css({height: "100%", width: "100%"});
			$("html").css("overflow","hidden");
			if (document.getElementById("PB_HideSelect") === null) {//iframe to hide select elements in ie6
				$("body").append("<iframe id='PB_HideSelect'></iframe><div id='PB_overlay'></div><div id='PB_window'></div>");
				$("#PB_overlay").click(popupbox.remove);
			}
		}else{//all others
			if(document.getElementById("PB_overlay") === null){
				$("body").append("<div id='PB_overlay'></div><div id='PB_window'></div>");
				$("#PB_overlay").click(popupbox.remove);
			}
		}
		
	    $("#PB_overlay").addClass("PB_overlayBG");//use background and opacity

		$("body").append("<div id='PB_load'><img src='"+popupbox.loading_img.src+"' /></div>");//add loader to the page
		$('#PB_load').show();//show loader


  },// end function init_win
  
  
  showhtml : function (url,params) {
    if (typeof params=="undefined") params = {};

       if (typeof params.height == "undefined") params.height = 300;
       if (typeof params.width == "undefined")  params.width = 300;
       if (typeof params.caption == "undefined") params.caption='';

    
        popupbox.init_win(params);
    
		ajaxContentW = popupbox.PB_width - 30;
		ajaxContentH = popupbox.PB_height - 45;
			
		if(0 /*url.indexOf('PB_iframe') != -1*/){// either iframe or ajax window		

/*				urlNoQuery = url.split('PB_');
					$("#PB_iframeContent").remove();
					if(params['modal'] != "true"){//iframe no modal
						$("#PB_window").append("<div id='PB_title'><div id='PB_ajaxWindowTitle'>"+params.caption+"</div><div id='PB_closeAjaxWindow'><a href='#' id='PB_closeWindowButton' title='Close'>close</a> or Esc Key</div></div><iframe frameborder='0' hspace='0' src='"+urlNoQuery[0]+"' id='PB_iframeContent' name='PB_iframeContent"+Math.round(Math.random()*1000)+"' onload='PB_showIframe()' style='width:"+(ajaxContentW + 29)+"px;height:"+(ajaxContentH + 17)+"px;' > </iframe>");
					}else{//iframe modal
					$("#PB_overlay").unbind();
						$("#PB_window").append("<iframe frameborder='0' hspace='0' src='"+urlNoQuery[0]+"' id='PB_iframeContent' name='PB_iframeContent"+Math.round(Math.random()*1000)+"' onload='PB_showIframe()' style='width:"+(ajaxContentW + 29)+"px;height:"+(ajaxContentH + 17)+"px;'> </iframe>");
					}
*/			
 		} else {// not an iframe, ajax
			        
		  if($("#PB_window").css("display") != "block"){
		    if(params['modal'] != "true"){//ajax no modal
		      $("#PB_window").append("<div id='PB_title'><div id='PB_ajaxWindowTitle'>"+params.caption+"</div><div id='PB_closeAjaxWindow'><a href='#' id='PB_closeWindowButton'><img width='16px' style='border:0' src='"+popupbox.closebut_url+"'></a></div></div><div id='PB_ajaxContent' style='width:"+ajaxContentW+"px;height:"+ajaxContentH+"px'></div>");
   		    }else{//ajax modal
			  $("#PB_overlay").unbind();
			  $("#PB_window").append("<div id='PB_ajaxContent' class='PB_modal' style='width:"+ajaxContentW+"px;height:"+ajaxContentH+"px;'></div>");	
  	        }
		  } else {//this means the window is already up, we are just loading new content via ajax
		  	  $("#PB_ajaxContent")[0].style.width = ajaxContentW +"px";
			  $("#PB_ajaxContent")[0].style.height = ajaxContentH +"px";
			  $("#PB_ajaxContent")[0].scrollTop = 0;
			  $("#PB_ajaxWindowTitle").html(params.caption);
		  }
		} // endif iframe or ajax
					
		$("#PB_closeWindowButton").click(popupbox.remove);
		
		// more....
   
    
  }, // end function showhtml  
  

  position: function (wi,hi) {
     $("#PB_window").css({marginLeft: '-' + parseInt((wi / 2),10) + 'px', width: wi + 'px'});
//	 if ( !(jQuery.browser.msie && jQuery.browser.version < 7)) { // take away IE6
		$("#PB_window").css({marginTop: '-' + parseInt((hi / 2),10) + 'px'});
//     }
  }, // end function position
  
  
  remove: function () {

	$("#PB_closeWindowButton").unbind("click");
	$("#PB_window").fadeOut("fast",function(){$('#PB_window,#PB_overlay,#PB_HideSelect').trigger("unload").unbind().remove();});
	$("#PB_load").remove();
	if (typeof document.body.style.maxHeight == "undefined") {//if IE 6
		$("body","html").css({height: "auto", width: "auto"});
		$("html").css("overflow","");
	}
	return false;
  }, // end function remove
  
  
  img_click: function (ptag,params) {
	  if (ptag.nodeName=='IMG') {
	    if (typeof params=="undefined") params = {};
	    if (!params.caption) params.caption = ptag.title;
	    popupbox.showimg(ptag.src,params);     
	  }
  }// end function img_click
  
  , message: function (mes,params) {
  
//  	    params.height = 420;  params.width = 380;
       if (typeof params == "undefined") params = {};
       if (typeof params.height == "undefined") params.height = 300;
       if (typeof params.width == "undefined")  params.width = 500;
       if (typeof params.caption == "undefined") params.caption='';

  	    
		popupbox.showhtml('',params);
		
        $("#PB_ajaxContent").hide();
	    $("#PB_ajaxContent").html(mes);
	    
	    popupbox.position(params.width,params.height);
        $("#PB_ajaxContent").show();
	     
	    $("#PB_load").remove();
		$("#PB_window").css({display:"block"});
  
  }
  
  
} // end var popupbox


	  


