var msgWin = window.msgWin;
/* popup pro obrazky a galerie */
function popup(alink, type) {
	var retValue = false;
	switch(type) {
		case 1: // img_from_link
			prefix = "/popup_image.php?path=";
			path = prefix + alink.href.substr(alink.href.indexOf("/img/")) + "&title=" + alink.getAttribute("TITLE");
			break;
		case 2: // gal_from_link
			prefix = "/popup_gallery.php?js=true&adr=";
			ppth = alink.href.substr(alink.href.indexOf("adr=") + 4)
			path = prefix + ppth;
			break;
	}
	
	if (msgWin != null && !msgWin.closed) { 
		msgWin.location.href = path;
		msgWin.focus();
	} else {
		msgWin = window.open(path, "_gallery","height=150,menubar=0,personalbar=0,resizable=1,scrollbars=0,toolbar=0,width=150,top=100,left=100,status=0");
		if (msgWin == null || typeof(msgWin) == "undefined") {
			retValue = true; 
			}
		else { 
			alink.target = "_gallery";
			msgWin.focus(); 
		} 
	} 
	return retValue; 
}

function hotkeys(evt) {
	evt = (evt) ? evt : ((event) ? event : null);
	switch(evt.keyCode) {
		case(27):
			hideLB();
			break;
	}
}

// img preload
$.preloadImages = function() {
  for(var i = 0; i < arguments.length; i++) {
    $("<img>").attr("src", arguments[i]);
  }
}

$.fn.image = function(src, id, f){
	return this.each(function(){
		var i = new Image();
		i.src = src;
		i.id = id;
		i.onload = f;
		this.appendChild(i);
	});
}

$(document).ready(function(){
	
	$(".lbox").click(function(){
		showLB(this);
		return false;
	})
	
	$(".trevent").mouseover(function(){
		$(this).addClass("emph " + $(this).attr("class"));
		
	});
	
	$(".trevent").mouseout(function(){
		$(this).removeClass("emph");
	})
	
	$(".def").toggleClass("btnEmph");
	promoDescDefault = $("#promoDesc").html();
	
	$("#bck2").mouseover(function(){
		$("#indxImg").toggleClass("b2"); 
		$("#promoDesc").html("<strong>The Jews: History Of a People</strong>, Nathan Hanover, 2005");
		$("#bck1").toggleClass("btnEmph");
		$("#bck2").toggleClass("btnEmph");
	})
	
	$("#bck2").mouseout(function(){
		$("#indxImg").toggleClass("b2");
		$("#promoDesc").html(promoDescDefault);
		$("#bck1").toggleClass("btnEmph");
		$("#bck2").toggleClass("btnEmph");
	})
	
	$("#bck3").mouseover(function(){
		$("#indxImg").toggleClass("b3");
		$("#promoDesc").html("<strong>Lukáš Jůza</strong>, Listopad 2006");
		$("#bck1").toggleClass("btnEmph");
		$("#bck3").toggleClass("btnEmph");
	})
	
	$("#bck3").mouseout(function(){
		$("#indxImg").toggleClass("b3");
		$("#promoDesc").html(promoDescDefault);
		$("#bck1").toggleClass("btnEmph");
		$("#bck3").toggleClass("btnEmph");
	})
});


function showLB(a) {
	
	document.onkeydown = hotkeys;
	
	var im = new Image();
	im.id = "lbimg";
	im.src = a.href;
	
	// osetreni onload / cache kvuli casovani
	if(im.complete) {
		loadLB();
	} else {
		im.onload = loadLB;
	}
	
	function loadLB() {
		
		$("body").append('<div id="lb"><div id="lbcontent"><div id="lbTop"></div><div id="lbBot" class="clear"><span class="desc">' + a.title + '</span><a id="closeButton" class="close" href="#" title="Zavřít / Close">Zavřít / Close</a></div></div></div>');
		$("#lbTop").append(im);
		
		pg = getPageSize();
		// tmave pozadi (nastaveni vysky podle nactene stranky)
		$("#lb").css("height", pg[1] + "px");
		$("#lb").click(hideLB);
		// samotny box
		$("#lbcontent").css("marginTop", (getPageScroll() + 50) + "px");		// (podle nactene stranky nastavit horni okraj)
		$("#lbcontent").css("width", ($("#lbimg").width()) + "px");				// nastavit sirku boxu podle nacteneho obrazku (aby se vycentrovalo)
		$("#lbcontent").css("background","#eee");								// donastavit boxu sedobilou na pozadi, kvuli problikavani ve FF
		
		/* fov = new FlashObject(path, "swf_" + nick, swfWidth, swfHeight, "8", "#ffffff");
		fov.addParam("quality", "high");
		fov.write(nick + "Movie"); */
	}
}

function hideLB(){
	$("#lb").remove();
	document.onkeydown = "";
}

//
// getPageSize()
// Returns array with page width, height and window width, height
// Code from - quirksmode.org & Lightbox JS by Lokesh Dhakar
// Edit for Firefox by pHaez
//
function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}


	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}

//
// getPageScroll()
// Returns y page scroll values.
// Code from - quirksmode.org & Lightbox JS by Lokesh Dhakar
//
function getPageScroll(){

	var yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
	}

	return yScroll;
}