/*
Simple Image Trail script- By JavaScriptKit.com
Visit http://www.javascriptkit.com for this script and more
This notice must stay intact
*/

var offsetfrommouse=[30,20]; //image x,y offsets from cursor position in pixels. Enter 0,0 for no offset
var displayduration=0; //duration in seconds image should remain visible. 0 for always.
var currentimageheight = 640;	// maximum image size.
var displayDelay = 200;
var dontDisplayTrail = false;
var imageHeight = 0;
var imageWidth = 0;
var saveHTML = "";
if (document.getElementById || document.all){
	document.write('<div id="trailimageid" style="position:absolute;">');
	document.write('</div>');
}

function gettrailobj(){
	if (document.getElementById)
	return document.getElementById("trailimageid").style
	else if (document.all)
	return document.all.trailimagid.style
}

function gettrailobjnostyle(){
	if (document.getElementById)
	return document.getElementById("trailimageid")
	else if (document.all)
	return document.all.trailimagid
}


function truebody(){
	return (!window.opera && document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
}

function showtrail(imageSrc, title, width, height, author){
	if (dontDisplayTrail == true) {
        return false;
    }

    imageWidth = width;
	imageHeight = height;

	document.onmousemove=followmouse;
	newHTML = '<div class="trail-wrap">';
	if (timestamp) {
	   timestamp = "?time=" + new Date().getTime();
	} else {
	   var timestamp = "";
	}

	newHTML = newHTML + '<div class="trail-wrap-in"">';
	newHTML = newHTML + '<img src="' + imageSrc + timestamp + '" width="' + width +'" height = "' + height +'" border="0"/>';
	newHTML = newHTML + '<div class="trail-title"><strong>' + title + '</strong> by ' + author + '</div></div>';
	newHTML = newHTML + '</div>';
	saveHTML = newHTML;
	setTimeout("displayTrail(saveHTML)", displayDelay);
}

function showtrailnosize(imageSrc, title, sizeFlag) {
    if (dontDisplayTrail == true) {
        return false;
    }
    if (sizeFlag == 1) {
        imageWidth = 300;
        imageHeight = 300;
    }
    else {
        imageWidth = 400;
        imageHeight = 400;
    }
    document.onmousemove = followmouse;
    newHTML = '<div class="trail-wrap">';
    if (timestamp) {
        timestamp = "?time=" + new Date().getTime();
    } else {
        var timestamp = "";
    }

    newHTML = newHTML + '<div class="trail-wrap-in"">';
    newHTML = newHTML + '<div class="trail-title"><strong>' + title + '</strong></div>';
    newHTML = newHTML + '<img src="' + imageSrc + '" border="0"/></div>';
    
    newHTML = newHTML + '</div>';
    saveHTML = newHTML;
    setTimeout("displayTrail(saveHTML)", displayDelay);
}

function displayTrail(newHTML) {
    gettrailobjnostyle().innerHTML = newHTML;
    gettrailobj().display="inline";
}

function switchDisplayTrail() {
    dontDisplayTrail = !dontDisplayTrail;
    createCookie("dontDisplayTrail", dontDisplayTrail, 7);
}

function showHiresTrail(event, urlZoomu, rodic){
	var children = rodic.childNodes;

	for (var i=children.length-1; i >= 0; i--)
	{
		if (children[i].nodeName.toLowerCase() == "img")
		{
			var image = rodic.childNodes[i];

			if (image.getAttribute("class") == "thumbzoomimage")
			{
				return;
			}
		}
	}

	imageSouradnice = souradniceObjektu(image); //x,y souradnice snimku

	//souradnice kliknuti ve snimku
	if (event.offsetX && event.offsetY)
	{
		var pos_x = event.offsetX;
		var pos_y = event.offsetY;
	}
	else
	{
		var pos_x = event.pageX - imageSouradnice[0];
		var pos_y = event.pageY - imageSouradnice[1];
	}

	//zoomovany snimek
	var newimage = document.createElement("img");
	newimage.setAttribute("src", urlZoomu + "?x=" + pos_x + "&y=" + pos_y);
    urlZoomu = urlZoomu.replace("thumb", "large");
	var thumbZoomImage = "thumbzoomimage"+Math.round(Math.random()*5000);
	newimage.setAttribute("id", thumbZoomImage);
	newimage.height = 200;
	newimage.width = 200;
	//newimage.setAttribute("class", "thumbzoomimage");

	//kurzory pro zoomout
	newimage.style.cursor = "url(/js/graphics/zoomout.cur), pointer";

	document.onmousemove=followmouse;

	newHTML = '<div style="padding: 5px; background-color: #FFF; border: 1px solid #888;">';
	newHTML = newHTML + "hires vyrez" + '<br/>';

	newHTML = newHTML + '<div align="center" style="padding: 8px 2px 2px 2px;">';
	newHTML = newHTML + '<img src="' + newimage.src + '" border="0" width="' + newimage.width + '" height="' + newimage.height + '" /></div>';

	newHTML = newHTML + '</div>';
	gettrailobjnostyle().innerHTML = newHTML;
	gettrailobj().display="inline";
}

function showtrailBatch(imagename,title,filetype){
	document.onmousemove=followmouseBatch;

	cameraHTML = '';

	newHTML = '<div style="padding: 5px; background-color: #FFF; border: 1px solid #888;" id="trailInnerDiv">';
	newHTML = newHTML + '<h2>' + title + '</h2>';

	newHTML = newHTML + '<div align="center" style="padding: 8px 2px 2px 2px;">';
	if(filetype == 8) { // Video
		newHTML = newHTML +	'<object width="380" height="285" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0">';
		newHTML = newHTML + '<param name="movie" value="video_loupe.swf">';
		newHTML = newHTML + '<param name="quality" value="best">';
		newHTML = newHTML + '<param name="loop" value="true">';

		newHTML = newHTML + '<param name="FlashVars" value="videoLocation=' + imagename + '">';
		newHTML = newHTML + '<EMBED SRC="video_loupe.swf" LOOP="true" QUALITY="best" FlashVars="videoLocation=' + imagename + '" WIDTH="380" HEIGHT="285">';
		newHTML = newHTML + '</object></div>';
	} else {
		newHTML = newHTML + '<img src="' + imagename + '" border="0"></div>';
	}
	newHTML = newHTML + '</div>';
	gettrailobjnostyle().innerHTML = newHTML;
	gettrailobj().display="inline";
	gettrailobj().position="absolute";
	currentimageheight = $('trailInnerDiv').offsetHeight;

}

function hidetrail(){
	gettrailobj().innerHTML = " ";
	gettrailobj().display="none"
	document.onmousemove=""
	gettrailobj().left="-500px"
    gettrailobj().top=""
}

/* Largely rewritten by Melda, 2009-02-15*/
function followmouse(e){
	var topBound = 0;
	var bottomBound = 0;

	var xcoord=offsetfrommouse[0];
	var ycoord=offsetfrommouse[1];
	var viewWidth = document.all ? truebody().scrollLeft+truebody().clientWidth : pageXOffset+window.innerWidth-15
	var viewHeight = document.all ? Math.min(truebody().scrollHeight, truebody().clientHeight) : Math.min(window.innerHeight)

	divHeight = imageHeight + 100;
	topBound = document.documentElement.scrollTop;
	bottomBound = document.documentElement.scrollTop + viewHeight;

	// Get basic position
	if (!e) var e = window.event;
    if (e.pageX || e.pageY) { // px from top of document
		xcoord = e.pageX;
		ycoord = e.pageY;
    } else if (e.clientX || e.clientY) { // px from top of view
		xcoord = e.clientX;
		ycoord = e.clientY + document.documentElement.scrollTop;
		bottomBound = viewHeight + document.documentElement.scrollTop;
    }
	xcoord += offsetfrommouse[0];
	ycoord -= (divHeight/2);

    // Adjust if necessary - height
	if (ycoord < topBound) {
		ycoord = (topBound + 10); // just a bit below the top of the view
	} else if (ycoord > (bottomBound - divHeight)) {
		ycoord = (bottomBound - divHeight);
	}
	// Adjust if necessary - width
	if (xcoord > (viewWidth - imageWidth)) {
		xcoord -= (imageWidth + (offsetfrommouse[0] * 2));
	}

    gettrailobj().left = xcoord + "px";
    if(gettrailobj().top == '') {
       gettrailobj().top = ycoord + "px"
    }
}

function followmouseBatch(e){
	var xcoord=offsetfrommouse[0]
	var ycoord=offsetfrommouse[1]

	var docwidth=document.all? truebody().scrollLeft+truebody().clientWidth : pageXOffset+window.innerWidth-15
	var docheight=document.all? Math.min(truebody().scrollHeight, truebody().clientHeight) : Math.min(window.innerHeight)

	var trailInnerDiv = $('trailInnerDiv');
	var currentimageheight = trailInnerDiv.offsetHeight;
	var currentimagewidth = trailInnerDiv.offsetWidth;

	scrollPos = Position.realOffset(truebody());

	if (typeof e != "undefined"){
		if (docwidth - e.pageX < 440){
			xcoord = e.pageX - xcoord - 400; // Move to the left side of the cursor
		} else {
			xcoord += e.pageX;
		}
		if ((e.pageY - scrollPos[1]) + currentimageheight > docheight){
			ycoord = -ycoord + (e.pageY - currentimageheight);
		} else {
			ycoord += e.pageY;
		}
	} else if (typeof window.event != "undefined"){
		if (event.clientX + currentimagewidth > docwidth){
			xcoord = -xcoord + ((event.clientX + scrollPos[0]) - currentimagewidth); // Move to the left side of the cursor
		} else {
			xcoord += (event.clientX + scrollPos[0]);
		}
		if (event.clientY + currentimageheight > docheight){
			ycoord = -ycoord + ((event.clientY + scrollPos[1]) - currentimageheight);
		} else {
			ycoord += (event.clientY + scrollPos[1]);
		}
	}

	if(ycoord < 0) { ycoord = ycoord*-1; }

	gettrailobj().left=xcoord+"px"
	gettrailobj().top=ycoord+"px"

}
