var currentitem = false;
var currentthumb = false;
var popup = false;
var oldimage = false;
var newimage = false;
var veryoldimage = false;
var diapotimer = false;
var diapoindex = 1;
var diapoon = false;
var minleft = false;
var thumbtop = false;
var maxleft = false;
var doingamove = false;
var movecounter = 0;
var searchresults_inner = false;
var s_mintop = false;
var s_maxtop = false;
var rightmenu_inner = false;
var mintop = false;
var maxtop = false;
var selector = false;
var medthumbs = new Array();
var numimgs = new Array();
var ajaxloader = new Image();
ajaxloader.src = "images/ajax-loader.gif";
ajaxloader.style.marginTop = "150px";
ajaxloader.style.mardinLeft = "250px";

function toggle_artworkbuybtn(shown){
	var thebtn = document.getElementById("artworkbuybtn");
	if(!thebtn)	return;
	if(shown){
		thebtn.style.visibility = "visible";
	}else{
		thebtn.style.visibility = "hidden";
	}
	thebtn = null;
}

var thumbnewval = 0;
function eachmove(direction){
	var thumbs_inner = document.getElementById("thumbs_inner");
	if(!thumbs_inner)	return false;
    	movecounter = movecounter + 1;
	var speed = 2;
	if(!direction || direction != 'left')	speed = speed * (-1);
	thumbnewval = thumbnewval + speed;
	if(thumbcount && maxleft && thumbnewval < maxleft) thumbnewval = maxleft;
	if(thumbnewval > 0) thumbnewval = 0;
	thumbs_inner.style.left = thumbnewval +'px';
        if(movecounter > 156){
                clearInterval(movetimer);
                doingamove = false;
                movecounter = 0;
        }
}

function move_thumbs(direction){
	if(!doingamove){
		movetimer = setInterval("eachmove('"+direction+"')",10);
                doingamove = true;
        }
}

function itemshow(thumb){
	var itemid = thumb.id.substr(5);
	if(currentthumb != itemid){
		currentthumb = itemid;
		popup.innerHTML = '';
		if(medthumbs[thumb.id.substr(5)]){
			var medimg = medthumbs[thumb.id.substr(5)];
		}else{
			var medimg = new Image();
			medimg.src = "uploads/images/gallery/medium/"+thumb.id.substr(5) +".jpg";
			medimg.alt = thumb.id.substr(5);
			medimg.style.cursor = "pointer";
		}
		if(thumb.className == "smallthumb flvthumb") medimg.className = "flvmed";
		medimg.onclick = function() {
			pause_diapo();
			if(this.className == "flvmed"){
				callFLV(this.alt, true);	
			}else{
				callData(this.alt, true, true);
			}
		}
		popup.appendChild(medimg);
		posX = minleft + thumb.offsetLeft + (document.getElementById("thumbs_inner").offsetLeft)  - 16;
		popup.style.top = thumbtop;
		popup.style.left = posX + 'px';
		popup.style.display = 'block';
	}
}

function itemhide(){
	document.getElementById('thumbpopup').style.display = 'none';
	currentthumb = false;
}

function start_diapo(){
	if(!diapoon){
		if(diapoindex > (thumbcount - 2)){
			diapoindex = 0;
			newimage = new Image();
			newimage.style.position = 'absolute';
			newimage.style.zIndex = '200';
			newimage.style.display = "none";
			newimage.src = 'uploads/images/gallery/large/'+thumbids[0]+'.jpg';
		}
		diapoon = true;
		diapo_change();
		diapotimer = setInterval("diapo_change()",4000);
	}
}

function diapo_change(){
	var maindest = document.getElementById('innercontent');
	if(veryoldimage){
		maindest.removeChild(veryoldimage);
		veryoldimage = false;
	}
	if(oldimage){
		oldimage.style.zIndex = '100';
		oldimage.id = "old"+diapoindex;
		if(thumbids[diapoindex]) opacity("old"+diapoindex, 100, 0, 2000);
	}
	if(thumbids[diapoindex]){
		if(newimage){
			newimage.id = "new"+diapoindex;
			maindest.appendChild(newimage);
			changeOpac(0,"new"+diapoindex);
			opacity("new"+diapoindex, 0, 100, 2000);
		}
		diapo_callData(thumbids[diapoindex]);
		diapoindex = diapoindex + 1;
		veryoldimage = oldimage;
		oldimage = newimage;
		newimage = new Image();
		newimage.style.position = 'absolute';
		newimage.style.zIndex = '200';
		newimage.style.display = "none";
		newimage.src = 'uploads/images/gallery/large/'+thumbids[diapoindex]+'.jpg';
	}else{
		newimage = new Image();
		newimage.style.position = 'absolute';
		newimage.style.zIndex = '200';
		newimage.style.display = "none";
		newimage.src = 'uploads/images/gallery/large/'+thumbids[0]+'.jpg';
		diapoon = false;
		clearInterval(diapotimer);
	}
	maindest = null;
}
function pause_diapo(){
	diapoon = false;
	clearInterval(diapotimer);
}

var displayData = function(xml) {
	var maindest = document.getElementById('innercontent');
	var contenu = xml.responseText; 
	maindest.removeChild(ajaxloader);
	maindest.appendChild(oldimage);
	document.getElementById('rightpart').innerHTML = contenu;
	maindest = null;
}

function callData(imgid, hidecurrent){
	var maindest = document.getElementById('innercontent');
	toggle_artworkbuybtn(true);
	trigger_selector(imgid);
	currentitem = imgid;
	document.getElementById('rightpart').innerHTML = '<center><img src="images/ajax-loader.gif" alt="loading..."/></center>';

	if(veryoldimage){
		maindest.removeChild(veryoldimage);
		veryoldimage = false;
	}
	maindest.innerHTML = '';
	maindest.appendChild(ajaxloader);
	maindest = null;
	
	writenumber(imgid);

	oldimage = new Image();
	oldimage.style.position = 'absolute';
	oldimage.style.zIndex = '200';
	oldimage.src = 'uploads/images/gallery/large/'+imgid+'.jpg';

	var xmlhttp = new Ajax();
	xmlhttp.onsuccess = displayData;
	xmlhttp.URLString = 'id='+imgid;
	if(currenttype)	xmlhttp.URLString += '&type='+currenttype;
	xmlhttp.request(basepath+"modules/plgallery/getinfo.php", {method: 'POST'});
}

var diapo_displayData = function(xml) {
	var contenu = xml.responseText; 
	document.getElementById('rightpart').innerHTML = contenu;
}

function diapo_callData(imgid){
	toggle_artworkbuybtn(true);
	trigger_selector(imgid);
	currentitem = imgid;
	document.getElementById('rightpart').innerHTML = '<center><img src="images/ajax-loader.gif" alt="loading..."/></center>';
	writenumber(imgid);
	var xmlhttp = new Ajax();
	xmlhttp.onsuccess = diapo_displayData;
	xmlhttp.URLString = 'id='+imgid;
	if(currenttype)	xmlhttp.URLString += '&type='+currenttype;
	xmlhttp.request(basepath+"modules/plgallery/getinfo.php", {method: 'POST'});
}

function callFLV(fileid, autostart) {
	var maindest = document.getElementById('innercontent');
	
	toggle_artworkbuybtn(false);
	document.getElementById('rightpart').innerHTML = '<center><img src="images/ajax-loader.gif" alt="loading..."/></center>';
	var xmlhttp = new Ajax();
	xmlhttp.onsuccess = diapo_displayData;
	xmlhttp.URLString = 'id='+fileid;
	if(currenttype)	xmlhttp.URLString += '&type='+currenttype;
	xmlhttp.request(basepath+"modules/plgallery/getinfo.php", {method: 'POST'});
	
	maindest.innerHTML = '';
	maindest = null;
	
	var s1 = new SWFObject(basepath + "modules/plgallery/player.swf","ply",521,368,7);
	s1.addParam("allowscriptaccess","always");
	s1.addParam("allowfullscreen","true");
	s1.addParam("wmode","opaque");
	var flashvars = '';
	if(autostart) flashvars += 'autostart=false&';
	flashvars += 'controlbar=over&file='+basepath + 'uploads/images/gallery/flv/'+fileid+'.flv'+'&image='+basepath + 'uploads/images/gallery/large/'+fileid+'.jpg';
	s1.addParam("flashvars",flashvars);
	s1.write("innercontent");
}


function thumbassign(){
	var imgs = document.getElementById('thumbs_inner').getElementsByTagName('img');
	for(i=0; i< imgs.length ; i++) {
		imgs[i].onclick = function() {
			pause_diapo();
			if(this.className == 'smallthumb flvthumb'){
				callFLV(this.id.substr(5), false);	
			}else{
				callData(this.id.substr(5), true, true);
			}
			return false;
		}
		imgs[i].onmouseover = function() {
			itemshow(this);
		}
		imgs[i] = null;
	}
	imgs = null;
}


function move_searchresults(direction, speed){
	if(!speed)	speed = 62;
	if(!direction || direction != "up")	speed = speed * (-1);
	var newval = searchresults_inner.offsetTop - s_mintop + speed;
	if(newval < s_maxtop) newval = s_maxtop;
	if(newval > 0) newval = 0;
	searchresults_inner.style.top = newval+"px";
}

function writeOneItem(imgid){
	var thediv = document.createElement("div");
	thediv.className = "resultbox";
	thediv.id = "selekt"+imgid;
	thediv.onclick = function(){
		selekt(this, this.id.substr(6), "selektfinal");
	}
	var thea = document.createElement("a");
	var theimg = new Image();
	theimg.src = "uploads/images/gallery/search/"+imgid+".jpg";
	theimg.alt = imgid;
	var thespan = document.createElement("span");
	if(isselected(imgid, "selektfinal"))	thespan.className = "chosen";
	document.getElementById("searchresults_inner").appendChild(thediv);
	thediv.appendChild(thea);
	thea.appendChild(theimg);
	thea.appendChild(thespan);
	thespan = null
	thediv = null;
	thea = null;
}

function loadselected(){
	var chosen_ck = readCookie("selekt");
	if(!chosen_ck || chosen_ck == '')	return false;
	var tmp_ck = readCookie("selektfinal");
	if(!tmp_ck)	createCookie("selektfinal",chosen_ck);
	var chosen = chosen_ck.split("_");
	for(var i=0;i < chosen.length;i++) {
		writeOneItem(chosen[i]);
	}		
}

function buylist(linkobj){
	var chosen_ck = readCookie('selektfinal');
	if(!chosen_ck || chosen_ck == ""){
		alert('There is no image selected in your Selekt Lightbox');
		return false;
	}
	linkobj.href = linkobj.href + chosen_ck;
	return true;
}

function move_rightmenu(direction, speed){
	if(!speed)	speed = 42;
	if(!direction || direction != 'up')	speed = speed * (-1);
	var newval = rightmenu_inner.offsetTop - mintop + speed;
	if(newval < maxtop) newval = maxtop;
	if(newval > 0) newval = 0;
	rightmenu_inner.style.top = newval+'px';
}


function initcurrent(startdiapo){
	if(document.getElementById("searchresults_inner")){
		searchresults_inner = document.getElementById("searchresults_inner");
		s_mintop = searchresults_inner.offsetTop;
		s_maxtop = (-1)*(document.getElementById("searchresults_outer").offsetHeight);
	}
	if(document.getElementById("rightmenu_inner")){
		rightmenu_inner = document.getElementById("rightmenu_inner");
		mintop = rightmenu_inner.offsetTop;
		maxtop = (document.getElementById("rightmenu_outer").offsetHeight) * (-1);
	}
	if(document.getElementById("selector"))	selector = document.getElementById("selector");
	if(do_selektload)	loadselected();
	
	if(document.getElementById('thumbpopup')){
		popup = document.getElementById('thumbpopup');
		popup.onmouseout = function(){
			itemhide();
		}
	}
	var maindest = document.getElementById('innercontent');
	minleft = document.getElementById("pagewrapper").offsetLeft + 161;
	thumbtop = '552px';
	
	if(thumbcount){
		maxleft = ((thumbcount * -26) + document.getElementById("thumbs_outer").offsetWidth);
		if(maxleft > 0)	maxleft = 0;
	}else{
		maxleft = 0;
	}

	thumbassign();
	
	if(currentitem){
		if(currentitem.substr(0,3) == "flv"){
			currentisflv = true;
			callFLV(currentitem.substr(3), false);
		}else{
			diapo_callData(currentitem, false);
			oldimage = maindest.getElementsByTagName("img")[0];
			if(oldimage){
				oldimage.style.position = 'absolute';
				oldimage.style.zIndex = '100';
			}
		}		
	}
	
	if(!thumbids || !thumbids[1]) return;

	newimage = new Image();
	newimage.src = 'uploads/images/gallery/large/'+thumbids[1]+'.jpg';
	newimage.style.position = 'absolute';
	newimage.style.zIndex = '200';
	if(false)	preloadimgs();
	if(startdiapo)	start_diapo();
}

function writenumber(imgid){
    var idstr = imgid;
    if(idstr.length < 3) idstr = '0'+idstr;
    if(idstr.length < 3) idstr = '0'+idstr;
    var theoutput = '';
    for(i=0; i< 3; i++) {
    	var tmpnum = idstr.substr(i,1);
    	theoutput += '<img src="images/numbers/'+tmpnum+'.gif" alt="'+tmpnum+'" />';
	}
	document.getElementById('leftpart').innerHTML = '<p class="imgid">'+theoutput+'</p>';
}

function preloadimgs(){
	if(!thumbids) var thumbids = new Array();
	for(i=0; i< thumbids.length; i++) {
		var tmpimg = new Image();
		tmpimg.src = "uploads/images/gallery/medium/"+thumbids[i]+".jpg";
		tmpimg.alt = thumbids[i];
		medthumbs[thumbids[i]] = tmpimg;
	}
	for(i=0; i< 10; i++) {
		var tmpimg = new Image();
		tmpimg.src = "images/numbers/"+i+".gif";
		tmpimg.alt = i;
		tmpimg.style.cursor = "pointer";
		numimgs[i] = tmpimg;
	}
}

function gotolightbox(){
	chosen_ck = readCookie("selekt");
	if(!chosen_ck || chosen_ck == '' || chosen_ck == '_'){
		alert('There is no image selected.');
		return false;
	}else{
		return true;
	}
}
