var req;
var SDMode = 1;
var HDMode = 2;
var imageMode;
var thumb1 = new Array();
var thumb2 = new Array();
var image = new Array();
var hiresImage = new Array();
var description = new Array();

var cats = new Array();
var objects = new Array();

var divHeight, divWidth;
var bucket = -1;	

var currItem;

function AssetObject(medSrc,hdSrc,description){
	this.hdSrc = hdSrc;
	this.medSrc = medSrc;
	this.description = description;
}

function getBucketNumber(thumbId)
{
	var count = 0;
	var thumb = -1;
	for (var x=0; x<objects.length; x++)
	{
		count+=objects[x].length;
		if (count>thumbId){
			return x;
		}
	}
	return -1;
}

function getItemNumber(thumbId)
{
	var count = 0;
	var thumb = -1;
	var counter = thumbId;
	for (var x=0; x<objects.length; x++)
	{
		count+=objects[x].length;
		if (count>thumbId)
		{
			return counter;
		}
		counter -= objects[x].length
	}
	return -1;
}

function getAbsoluteItemNumber(index){
	var count = 0;
	for (var x=0; x<bucket; x++){
		count+=objects[x].length;
	}
	return count+index;
}

function rotateScreenshot(str){
	if (!(BrowserDetect.browser=="Firefox" && navigator.appVersion.indexOf("Mac")>-1)){
		$("ew_screenshot_swf").selectThumbById(getAbsoluteItemNumber(str));
	}
	currItem = str;
	drawScreenshot(str);
}

function setScreenshotControls(index){
	hidePreLoader();
	$("screenshot-wrapper").style.display = "block";
	
	var contentDiv = $("screenshot-controls-container");
	var img = $("screenshot-image").getElementsByTagName("IMG")[0];

	contentDiv.style.display = "none";
	if (imageMode==SDMode){
		contentDiv.style.width = (img.width)+"px";
		contentDiv.style.marginLeft = "0px";
	}
	else if (imageMode==HDMode) {
		contentDiv.style.marginLeft = ((img.width-parseInt(contentDiv.style.width.substring(0,contentDiv.style.width.indexOf("px"))))/2)+"px";
	}

	if (imageMode==SDMode){
		img.onclick = null;
	}else{
		img.onclick = function(){toggleImageMode(index);};
	}

	//$("screenshot-title").style.width = (imageMode==SDMode?"auto":img.width+"px");
	$("screenshot-title").innerHTML = objects[bucket][index].description+" // "+(index+1) + " von " + objects[bucket].length;
	$("screenshot-arrow-left").onclick = function(){ rotateScreenshot(index==0?objects[bucket].length-1:index-1); };
	$("screenshot-arrow-right").onclick = function(){ rotateScreenshot(index==objects[bucket].length-1?0:index+1); };
	$("reg-hd-link").onclick = function(){toggleImageMode(index);};
	$("hd-reg-link").onclick = function(){toggleImageMode(index);};
	
	if (objects[bucket].length<=1){
		$("screenshot-arrow-left").style.display = "none";
		$("screenshot-arrow-right").style.display = "none";
		$("screenshot-controls").style.width = "67px";
	} else{
		$("screenshot-arrow-left").style.display = "block";
		$("screenshot-arrow-right").style.display = "block";
		$("screenshot-controls").style.width = "135px";
	}

	$("screenshot-wrapper").style.width = img.width+"px";

	var scrollY = getScrollHeight();
	if (getWindowHeight()>(img.height+35)){
		scrollY += ((getWindowHeight()-(img.height+35))/2);
	}
	$("screenshot-wrapper").style.marginTop = scrollY+"px";

	$("screenshot-image").style.visibility = "visible";
	contentDiv.style.display = "block";
	BlankDivs.drawBlankDivs();
}

function setVerticalPosition(){
	var img = $("screenshot-image").getElementsByTagName("IMG")[0];
	var scrollY = getScrollHeight();

	if (getWindowHeight()>(img.height+95)){
		scrollY += ((getWindowHeight()-(img.height+35))/2);
		$("screenshot-wrapper").style.marginTop = scrollY+"px";
	}
	BlankDivs.drawBlankDivs();
}

function toggleImageMode(index){
	if (imageMode == SDMode){
		$("reg-hd-link").style.display = "none";
		$("hd-reg-link").style.display = "block";
//		$("screenshot-title").className = "center";
//		$("screenshot-controls").className = "center";
	}
	else{
		$("reg-hd-link").style.display = "block";
		$("hd-reg-link").style.display = "none";
//		$("screenshot-title").className = "left";
//		$("screenshot-controls").className = "right";
	}
	imageMode = (imageMode==SDMode?HDMode:SDMode);
	drawScreenshot(index);
}

function closeBackgrounds(){
	var screenshotBGDiv = $("screenshot-bg");
	var screenshotFGDiv = $("screenshot-fg");
	BlankDivs.hideBlankDivs();

	divHeight = Math.max(getBrowserHeight(), document.documentElement.scrollHeight);
	divWidth = Math.max(document.documentElement.scrollWidth);
	
	screenshotBGDiv.style.width = "0px";
	screenshotBGDiv.style.height = "0px";
	screenshotFGDiv.style.width = "0px";
	screenshotFGDiv.style.height = "0px";
	
	screenshotBGDiv.style.display = "none";
	screenshotFGDiv.style.display = "none";

	window.onresize = null;
	window.onscroll = null;
	
	if ((currItem||currItem==0) && BrowserDetect.browser=="Firefox" && navigator.appVersion.indexOf("Mac")>-1){
		$("ew_screenshot_swf").selectThumbById(getAbsoluteItemNumber(currItem));
	}
	$("ew_screenshot_swf").enableInteraction();
}

function drawBackgrounds(){
	var screenshotBGDiv = $("screenshot-bg");
	var screenshotFGDiv = $("screenshot-fg");

	divHeight = Math.max(getBrowserHeight(), document.documentElement.scrollHeight);
	divWidth = Math.max(document.documentElement.scrollWidth);
	
	screenshotBGDiv.style.width = divWidth + "px";
	screenshotBGDiv.style.height = divHeight + "px";
	screenshotFGDiv.style.width = divWidth + "px";
	screenshotFGDiv.style.height = divHeight + "px";
	
	screenshotBGDiv.style.display = "block";
	screenshotFGDiv.style.display = "block";
}

function showPreLoader(){
	var scrollY = getScrollHeight();

	scrollY += (getWindowHeight()-50)/2;
	$("screenshot-pre-loader").style.top = scrollY+"px";
	$("screenshot-pre-loader").style.left = (((getWidth()-50)/2))+"px";

	$("screenshot-pre-loader").style.display="block";
}

function hidePreLoader(){
	$("screenshot-pre-loader").style.display="none";
}

function drawScreenshot(index){
	var screenshot = objects[bucket][index];
	initControls();
	
	$("screenshot-wrapper").style.display = "none";
	$("screenshot-image").style.visibility = "hidden";
	$("screenshot-controls-container").style.display = "none";
	
	showPreLoader();
	$("screenshot-image").innerHTML = "<img src=\""+(imageMode==SDMode?screenshot.medSrc:screenshot.hdSrc)+"\" onload=\"setScreenshotControls("+index+")\"/>";
}

function initControls(){
	$("screenshot-arrow-left").onmouseover = function(){ this.className = "screenshot-arrow-left-hover"};
	$("screenshot-arrow-left").onmouseout = function(){ this.className = "screenshot-arrow-left"};
	$("screenshot-arrow-left").className = "screenshot-arrow-left";

	$("screenshot-arrow-right").onmouseover = function(){ this.className = "screenshot-arrow-right-hover"};
	$("screenshot-arrow-right").onmouseout = function(){ this.className = "screenshot-arrow-right"};
	$("screenshot-arrow-right").className = "screenshot-arrow-right";

	$("reg-hd-link").onmouseover = function(){ this.className = "reg-hd-link-hover"};
	$("reg-hd-link").onmouseout = function(){ this.className = "reg-hd-link"};
	$("reg-hd-link").className = "reg-hd-link";
	
	$("hd-reg-link").onmouseover = function(){ this.className = "hd-reg-link-hover"};
	$("hd-reg-link").onmouseout = function(){ this.className = "hd-reg-link"};
	$("hd-reg-link").className = "hd-reg-link";
	
	$("screenshot-close").onmouseover = function(){ this.className = "screenshot-close-hover"};
	$("screenshot-close").onmouseout = function(){ this.className = "screenshot-close"};
	$("screenshot-close").className = "screenshot-close";
}

function showScreenshot(str){
	bucket = getBucketNumber(str);
	var index = getItemNumber(str);
	imageMode = SDMode;
	$("reg-hd-link").style.display = "block";
	$("hd-reg-link").style.display = "none";
	$("screenshot-title").className = "left";
	$("screenshot-controls").className = "right";

	$("screenshot-controls").className = "right";
	$("ew_screenshot_swf").disableInteraction();

	drawBackgrounds();
	drawScreenshot(index);
	BlankDivs.init($("screenshot-wrapper"),closeBackgrounds,null);
	
	currItem = null;
	
	window.onresize = resizeBackgrounds;
	window.onscroll = setVerticalPosition;
}




function resizeBackgrounds(){
	var de = document.documentElement;

	var screenshotBGDiv = $("screenshot-bg");
	var screenshotFGDiv = $("screenshot-fg");

	divHeight = Math.max(getBrowserHeight(), document.documentElement.scrollHeight);
	divWidth = Math.min(document.documentElement.scrollWidth, Math.min(window.innerWidth , self.innerWidth , de&&de.clientWidth , document.body.clientWidth));
	if (BrowserDetect.browser=="Explorer"){
		divWidth = Math.max(document.documentElement.clientWidth, 996);
	}

	screenshotBGDiv.style.width = divWidth + "px";
	screenshotBGDiv.style.height = divHeight + "px";
	screenshotFGDiv.style.width = divWidth + "px";
	screenshotFGDiv.style.height = divHeight + "px";
	
	setVerticalPosition();
}

function getScrollHeight(){
	var h = window.pageYOffset || document.body.scrollTop || document.documentElement.scrollTop;
	return h ? h : 0;
}

function getWindowHeight(){
	if (window.innerHeight){
		return window.innerHeight;
	} else {
		return document.documentElement.clientHeight;
	}
}

function getBrowserHeight(){
	if(self.innerHeight)return self.innerHeight;return document.documentElement.offsetHeight;
}

function onMovieHeightChange(height){
	height = height<670?670:height;
	$("flash-container").style.height = Math.round(height)+"px";
	$("ew_screenshot_swf").style.height = Math.round(height)+"px";
}
