// JavaScript Document
function initGallery ( tempgallery, count, first_id, startwidth, startheight ) {
	imggallery = tempgallery;
	if (preloadimg=="yes"){
		for (x=0; x<imggallery.length; x++){
			var myimage=new Image()
			myimage.src=imggallery[x][0]
		}
	}
	thumbnailnum = imggallery.length;
	current_imgid = first_id;
	currentwidth=startwidth;
	currentheight=startheight;
	window.addEvent('load', function() {
		//if(thumbnailnum>maxthumbvisible){
			//lefthtml = "<div id='leftmore'><ul><li><a href=\"javascript:checkbutton(addposition('minus'));movethumbs('plus');\"><img src='rsrc/buttonblank.gif' width='15' height='115' border='0' /></a></li></ul></div>";
			//righthtml = "<div id='rightmore'><ul><li><a href=\"javascript:checkbutton(addposition('plus'));movethumbs('minus');\"><img src='rsrc/buttonblank.gif' width='15' height='115' border='0' /></a></li></ul></div>";
			//$('back').setHTML(lefthtml);
			//$('more').setHTML(righthtml);
		//}
		var setloadersize = new Fx.Styles('main_image_wrapper',{duration:transspeed,onComplete: function(){loadfirstimage(currentwidth,currentheight,first_id)}});
		setloadersize.start({
			'width':imggallery[first_id][1],
			'height':imggallery[first_id][2]	
		});
	});
	
	
}
window.addEvent('domready', function() {
	var sPath = window.location.pathname;
	var sPage = sPath.substring(sPath.lastIndexOf('/') + 1);
	sPage = sPage.replace(".htm","");
	//alert(sPage);

	imgindex = 0;

	for (i=0; i<tempgallery.length; i++){
		var tmpPage = tempgallery[i][4];
		while (tmpPage.indexOf(" ")>0){
			tmpPage = tmpPage.replace(" ","");
		}
		while (tmpPage.indexOf("'")>0){
			tmpPage = tmpPage.replace("'","");
		}

		if (sPage == tmpPage){
			imgindex = tempgallery[i][8];
		}
	}
	initGallery( tempgallery, tempgallery.length, tempgallery[imgindex][8], tempgallery[imgindex][1], tempgallery[imgindex][2]);
});

function getstarted(width, height, loadarea, imgindex, img_id, current_imgid){
	checknext(img_id);
	if(current_imgid!=img_id){
		if(firstimagestart==1){
			currentwidth=firstimagewidth;
			currentheight=firstimageheight;
			firstimagestart=0;		
		}
		if(nextorprev==1){
			currentwidth=cwidth;
			currentheight=cheight;
			nextorprev=0;		
		}
		
		var resizeDivHeight = new Fx.Styles('main_image_wrapper',{duration:transspeed, onComplete: function(){modifyimage(loadarea, imgindex, img_id);currentheight=height;currentwidth=width;} });
		var fader = new Fx.Style('imgloader','opacity', {duration:fadespeed, onComplete: function(){	resizeDivHeight.start({'height': [currentheight,height],'width': [currentwidth,width]});} });
		fader.start(1,0);	
		//var fadeiptc = new Fx.Style('iptc_btn','opacity', {duration:transspeed });
		//fadeiptc.set(0);
		var titlefade = new Fx.Style('imgtitle','opacity', {duration:transspeed });
		titlefade.set(0);

		if (document.getElementById('imgdetails')){
			var detailsfade = new Fx.Style('imgdetails','opacity', {duration:transspeed });
			detailsfade.set(0);
		}

		if (document.getElementById('imgdescription')){
			var descriptionfade = new Fx.Style('imgdescription','opacity', {duration:transspeed });
			descriptionfade.set(0);
		}

		if (document.getElementById('exception')){
			var originalpricefade = new Fx.Style('exception','opacity', {duration:transspeed });
			originalpricefade.set(0);
		}

		if (document.getElementById('imgsaleprice')){
			var salepricefade = new Fx.Style('imgsaleprice','opacity', {duration:transspeed });
			salepricefade.set(0);
		}

		fadeout=0
		//if(fadeout==0){
			//var fademe = new Fx.Style('iptc_info','opacity', {duration:transspeed });
			//fademe.set(0);
			//fadeout=0;
		//}else{
			//var fademe = new Fx.Style('iptc_info','opacity', {duration:transspeed });
			//fademe.start(1,0);
			//fadeout=0
		//}
	}
}
function loadfirstimage(currentwidth,currentheight,imgindex){
	var fadefirst = new Fx.Style('imgloader','opacity', {duration:fadespeed });
	fadefirst.set(0);
	var firsttitlefade = new Fx.Style('imgtitle','opacity', {duration:transspeed });
	firsttitlefade.set(0);

	var fadefirst2 = new Fx.Style('imgdetails','opacity', {duration:fadespeed });
	fadefirst2.set(0);
	var firstdetailsfade = new Fx.Style('imgdetails','opacity', {duration:transspeed });
	firstdetailsfade.set(0);

	var fadefirst3 = new Fx.Style('imgdescription','opacity', {duration:fadespeed });
	fadefirst3.set(0);
	var firstdescriptionfade = new Fx.Style('imgdescription','opacity', {duration:transspeed });
	firstdescriptionfade.set(0);

	var fadefirst4 = new Fx.Style('imgsaleprice','opacity', {duration:fadespeed });
	fadefirst4.set(0);
	var firstsalepricefade = new Fx.Style('imgsaleprice','opacity', {duration:transspeed });
	firstsalepricefade.set(0);

	var fadefirst5 = new Fx.Style('exception','opacity', {duration:fadespeed });
	fadefirst5.set(0);
	var firstoriginalpricefade = new Fx.Style('exception','opacity', {duration:transspeed });
	firstoriginalpricefade.set(0);

	function setfirstimage(){
		//alert(imgindex);
		var newHTML = "<img src='"+tempgallery[imgindex][0]+"' onmouseover='TJPzoom(this, \""+tempgallery[imgindex][0].replace('-295','-xl')+"\");' />";
		$('imgloader').setHTML(newHTML);
		$$('.thumbnail').each(function(el){
			var thumbnail = new Fx.Style(el,'opacity', {duration:fadespeed });
			thumbnail.set(0.5);
		});
		var firstthumbnail = new Fx.Style($$('.thumbnail')[imgindex],'opacity', {duration:fadespeed });
		firstthumbnail.start(0.5,1);

		var firsttitle=document.getElementById('imgtitle');
		firsttitle.innerHTML=tempgallery[imgindex][4];
		fadefirst.start(0,1);
		firsttitlefade.start(0,1);

		var firstdetails=document.getElementById('imgdetails');
/*VVVVVVV*/
		firstdetails.innerHTML=tempgallery[imgindex][3];
/*^^^^^^^*/
		fadefirst2.start(0,1);
		firstdetailsfade.start(0,1);

		var firstdescription=document.getElementById('imgdescription');
		firstdescription.innerHTML=tempgallery[imgindex][7];
		fadefirst3.start(0,1);
		firstdescriptionfade.start(0,1);

// limited edition
		var firstsaleprice=document.getElementById('imgsaleprice');
		var btnLimitedEdition = document.getElementById('btnPurchaseLimitedEdition');
		var price = tempgallery[imgindex][6];
		while (price.indexOf('$')>=0){
			price = price.replace('$','');
		}
		while (price.indexOf(',')>=0){
			price = price.replace(',','');
		}
		if (price == parseFloat(price)){ // price has a dollar amount
			firstsaleprice.innerHTML='<a href="https://'+document.domain+'/buy/'+tempgallery[imgindex][9]+'.htm">'+tempgallery[imgindex][6]+'</a>';
			if (btnLimitedEdition){
				btnLimitedEdition.style.display = "";
				btnLimitedEdition.href='https://'+document.domain+'/buy/'+tempgallery[imgindex][9]+'.htm';
			}
		}
		else{ // "sold" or "n/a"... not a price
			firstsaleprice.innerHTML='<a href="/contact.htm?paintingid='+tempgallery[imgindex][10]+'">'+tempgallery[imgindex][6]+'</a>';
			if (btnLimitedEdition){
				btnLimitedEdition.style.display = "none";
				btnLimitedEdition.href='';
			}
		}
		fadefirst4.start(0,1);
		firstsalepricefade.start(0,1);

// original
		var firstoriginalprice=document.getElementById('exception');
		var btnOriginal = document.getElementById('btnPurchaseOriginal');

		var originalprice = tempgallery[imgindex][5];
		while (originalprice.indexOf('$')>=0){
			originalprice = originalprice.replace('$','');
		}
		while (originalprice.indexOf(',')>=0){
			originalprice = originalprice.replace(',','');
		}
		if (originalprice == parseFloat(originalprice)){ // price has a dollar amount
			firstoriginalprice.innerHTML='<a href="https://'+document.domain+'/buy/'+tempgallery[imgindex][9]+'.htm">'+tempgallery[imgindex][5]+'</a>';
			if (btnOriginal){
				btnOriginal.style.display = "";
				btnOriginal.href='https://'+document.domain+'/buy/'+tempgallery[imgindex][9]+'.htm';
			}
		}
		else{ // "sold" or "n/a"... not a price
			firstoriginalprice.innerHTML='<a href="/contact.htm?paintingid='+tempgallery[imgindex][10]+'">'+tempgallery[imgindex][5]+'</a>';
			if (btnOriginal){
				btnOriginal.style.display = "none";
				btnOriginal.href='';
			}
		}

		fadefirst5.start(0,1);
		firstoriginalpricefade.start(0,1);

		var firstlnkDetail=document.getElementById('lnkDetail');

		currentheight=imggallery[imgindex][2];
		currentwidth=imggallery[imgindex][1];

		//if (tempgallery[imgindex][3].length==0 && tempgallery[imgindex][7].length==0){
		if (tempgallery[imgindex][3].length==0){
			//firstlnkDetail.style.display = "none";
			//firstlnkDetail.style.display = "";
			firstlnkDetail.innerHTML = "Hover To Magnify";
			firstlnkDetail.href='#';
			firstlnkDetail.style.cursor = 'default';
			//alert('hit');
		}
		else{
			//firstlnkDetail.style.display = "";
			firstlnkDetail.innerHTML = "View Painting Detail";
			//alert(gallery);
			if (gallery=='Limited Edition'){
				var subfolder = '/limitededition/';
			}
			else{
				//alert('hit');
				var subfolder = '/painting/';
			}
			firstlnkDetail.href=subfolder+tempgallery[imgindex][9]+'.htm';
			firstlnkDetail.style.cursor = 'pointer';
		}
	}
	new Asset.image(imggallery[imgindex][0], {onload: setfirstimage});
}
function nextimage(current_imgid){
	
	newimgid = Number(current_imgid)+1;
	newwidth =imggallery[newimgid][1]
	newheight =imggallery[newimgid][2]
	newimgindex =imggallery[newimgid][8]
	newimgid = imggallery[newimgid][8]
	cwidth=imggallery[current_imgid][1]
	cheight=imggallery[current_imgid][2]
	checknext(newimgid);
	nextorprev=1;
	getstarted(Number(newwidth), Number(newheight), 'imgloader',Number(newimgindex) ,Number(newimgid) , Number(current_imgid), Number(cwidth), Number(cheight))
}

function previmage(current_imgid){
	newimgid = Number(current_imgid)-1;
	newwidth =imggallery[newimgid][1]
	newheight =imggallery[newimgid][2]
	newimgindex =imggallery[newimgid][8]
	newimgid = imggallery[newimgid][8]
	cwidth=imggallery[current_imgid][1]
	cheight=imggallery[current_imgid][2]
	checknext(newimgid);
	nextorprev=1;
	getstarted(Number(newwidth), Number(newheight), 'imgloader',Number(newimgindex) ,Number(newimgid) , Number(current_imgid), Number(cwidth), Number(cheight))
}
