var hintcontainer = null;  
 function showhint(obj, txt) {  
    if (hintcontainer==null) {  
       hintcontainer = document.createElement("div");  
       hintcontainer.className="hintstyle";  
       document.body.appendChild(hintcontainer);  
    }  
    obj.onmouseout = hidehint;  
    obj.onmousemove=movehint;
    hintcontainer.innerHTML='<img src='+txt+'>';  
 }  
 function movehint(e) {
     if (!e) e = event; //line for IE compatibility

	var scrollLeft = window.pageXOffset || document.documentElement.scrollLeft || 0;
	var scrollTop = window.pageYOffset || document.documentElement.scrollTop || 0;

	var x = e.clientX+20 + scrollLeft
	var y = e.clientY+20 + scrollTop

	if(e.clientX+hintcontainer.clientWidth + 25 > document.body.clientWidth)
		x -=  hintcontainer.clientWidth + 35

	var resy = e.clientY+hintcontainer.clientHeight + 70
	if(resy > document.body.clientHeight)
		y -= resy - document.body.clientHeight

	hintcontainer.style.top =  y+"px"
     //hintcontainer.style.top =  (e.clientY+document.documentElement.scrollTop+2)+"px";
	hintcontainer.style.left =  x+"px"
     //hintcontainer.style.left = (e.clientX+document.documentElement.scrollLeft+10)+"px";
	hintcontainer.style.display="";
	/*if($(hintcontainer).css('display'=='none'))
		$(hintcontainer).fadeIn(100);*/
 }
 function hidehint() {  
    hintcontainer.style.display="none";
	//$(hintcontainer).fadeOut('fast');
 }

 function mvhint() {

	var scrollLeft = window.pageXOffset || document.documentElement.scrollLeft || 0;
	var scrollTop = window.pageYOffset || document.documentElement.scrollTop || 0;

	var x = pt.x+20 + scrollLeft
	var y = pt.y+20 + scrollTop

	var w,h
	var idata = pt.db[pt.o.id]
	w = idata.tw + 15
	h = idata.th + 90
	/*if($(hintcontainer).css('display')=='none'){
		var img = $('img', hintcontainer).get(0)
		w = img.width + 15
		h = img.height + 80
	}
	else{
		w = hintcontainer.clientWidth
		h = hintcontainer.clientHeight
	}*/

	if(pt.x+ w + 10 > $(window).width())
		x -=  w + 35

	var resy = pt.y+ h + 30
	if(resy > $(window).height())
		y -= resy - $(window).height()

	$(hintcontainer).css('top', y)
	$(hintcontainer).css('left', x)

}
function thumb_size(w,h){
	var sz = 380, wn = 0, hn = 0
    if(w > h) {
        hn = Math.round(h/(w/sz))
        wn = sz
    }
    else if(h > w) {
        wn = Math.round(w/(h/sz))
        hn = sz
    }
    else wn = hn = sz
    return [wn, hn]
}
function calc_thumb_size(idata){
	if (idata.tw == undefined || idata.th == undefined){
		var tsz = thumb_size(idata.w, idata.h)
		idata.tw = tsz[0]
		idata.th = tsz[1]
	}
}
function update_hint(idata){
	$('#h_thumb').attr('src', pt.o.src).attr('width', idata.tw).attr('height', idata.th)
	$('#h_nail').replaceWith('<img src='+ idata.thumb +' id="h_nail" />')
	$('#h_text').css('width', idata.tw)
	$('#h_title').html(idata.title)
	$('#h_descr').html(idata.descr)
	$('#h_size').text(idata.w +' x '+ idata.h +' ('+ idata.mp +' MP)')
	$('#h_author').text(idata.author)

}
function update_hint_home(idata){
	$('#h_thumb').attr('src', pt.o.src).attr('width', idata.tw).attr('height', idata.th)
	$('#h_nail').replaceWith('<img src='+ idata.thumb +' id="h_nail" />')
	$('#h_text').css('width', idata.tw)
	$('#h_descr').html(idata.descr)
}
function inithint() {
	var idata = pt.db[pt.o.id]
	calc_thumb_size(idata)
	pt.updfn(idata)
	mvhint()
	//hintcontainer.style.display = ''
	$(hintcontainer).fadeIn('fast');
 }

var pt = {x:0, y:0, o:undefined, updfn: update_hint}

function snap(x, y, ob){
	pt.x = x
	pt.y = y
	pt.o = ob
}

function checkhint(e){
	if (!e) e = event; //line for IE compatibility
	snap(e.clientX, e.clientY, this)
	$(this).mousemove(function(e){
			if (!e) e = event;
			snap(e.clientX, e.clientY, this)
			if(hintcontainer.style.display == 'block')
				mvhint()
		}).mouseout(function(){
			hintcontainer.style.display="none"
			pt.o = null
		})
	function mkchk(elem){
		return function() {
		 return pt.o == elem}
	}
	var chk = mkchk(this)
	setTimeout(function(){ if(chk()) inithint()}, 300)
}

function getPX(e,pox) {
	if (!e) e = event;
	var scrollLeft = window.pageXOffset || document.documentElement.scrollLeft || 0;
	var mx = e.clientX + scrollLeft + pox;
	return mx;
}
function getPY(e,pox) {
	if (!e) e = event;
	var scrollTop = window.pageYOffset || document.documentElement.scrollTop || 0;
	var my = e.clientY + scrollTop + pox;
	return my;
}
var tm10;
function showPH(e,name) {
	clearTimeout(tm10)
	if (!e) e = event
	var tmp_hint = $('#'+name).appendTo(document.body).get(0)
	$('#'+name).css('position', 'absolute').css('top', getPY(e,10)).css('left', getPX(e,10)).css('margin', 0)
	tm10 = setTimeout("$('#"+name+"').fadeIn(150)", 500)
}
function hidePH(e,name) {
	clearTimeout(tm10)
	if (!e) e = event
	$('#'+name).fadeOut(100)
}
function setPosition(id,prT,prL) {
	pTop = (document.documentElement.scrollTop || document.body.scrollTop) + Math.round(prT * (document.documentElement.offsetHeight || document.body.clientHeight) / 100);
	pLeft = (document.documentElement.scrollLeft || document.body.scrollLeft) + Math.round(prL * (document.documentElement.offsetWidth || document.body.clientWidth) / 100);
	$('#'+id).css('top', pTop)
	$('#'+id).css('left', pLeft)
}
function setPositionMod(id,prT,prL) {
	pTop = Math.round(prT * (document.documentElement.offsetHeight || document.body.clientHeight) / 100);
	pLeft = Math.round(prL * (document.documentElement.offsetWidth || document.body.clientWidth) / 100);
	$('#'+id).css('top', pTop)
	$('#'+id).css('left', pLeft - Math.round($('#'+id).width() / 2))
	$('#'+id).css('margin-left', 0)
}
function getHeightWin() {
	pHeight = (document.documentElement.offsetHeight || document.body.clientHeight);
	return pHeight;
}
function getWidthWin() {
	pWidth = (document.documentElement.offsetWidth || document.body.clientWidth);
	return pWidth;
}

function WinKeyPress() {
	$('.kpe').keydown(function(e){
		if (!e) e = event
		id = $(this).attr('id')
		if (e.keyCode == 13) $('#' + id + ' .kpe13').click()
		if (e.keyCode == 27) $('#' + id + ' .kpe27').click()
	})
}
