// JavaScript Document
// Written by Kevin Yeo. This JS document is free for distribution on the agreement of not removing or amending the codes or this disclaimer without prior written consent.
// All questions or queries can be directed to contact@kevinyeo.com


function checkbasket() {
	document.getElementById('cart').style.display=='none' ? new fader(document.getElementById('cart'),0,1,250) : document.getElementById('cart').style.display='none' ;
}



// Javascript DOM Fader
function fader (param,start,end,time) {
	if (arguments.length<4) return;
	param.style.filter='alpha(opacity=\''+start*100+'\')';
	param.style.opacity=String(start);
	param.style.display='none' ? param.style.display='block' : null;
	var timer=time/10;
	for(i=0; i<11; i++) { 
		(function (i) { setTimeout(function () { changeopacity((start-((start-end)/10)*i),param); }, timer*i); })(i);
	}; 
	return;
}
function changeopacity (opacityvar,param) {
	with (param) { style.opacity=opacityvar; style.MozOpacity=opacityvar; style.filter="alpha(opacity="+opacityvar*100+")"; }
}

// JS DOM-Maker and Remover functions.
function createdom (type,variable,css_styles,append_parent,innerhtml) {
	var newdom=document.createElement(type);
	type=='div' ? newdom.id=variable : null; 
	type=='img' ? newdom.src=variable : null;
	if (append_parent!='none') { append_parent.appendChild(newdom) };
	for (var i in css_styles) { 
		newdom.style[css_styles[i].split('=')[0]]=String(css_styles[i].split('=')[1]); 
		if (css_styles[i].split('=')[0]=='opacity') {
			newdom.style.filter='alpha(opacity=\''+css_styles[i].split('=')[1]*10+'\')';
		}
	}
	if(innerhtml) type=='div' ? newdom.innerHTML=innerhtml : newdom.alt=innerhtml; return newdom;
}
function createanchor(datavalue,inner,css_styles,append_parent) {
	var newanchor=document.createElement('a');
	for (var i in datavalue) { 
		if (datavalue[i].split('=')[0]=='onclick') {
			(function (i) { 
				newanchor.onclick=function () { var newfunc= new Function (datavalue[i].split('=')[1]); newfunc(); }
			})(i);
		} else {
			newanchor.setAttribute(String([datavalue[i].split('=')[0]]),String([datavalue[i].split('=')[1]]));
		}
	}
	for (var i in css_styles) { newanchor.style[css_styles[i].split('=')[0]]=String(css_styles[i].split('=')[1]);}
	if(inner!='none') newanchor.innerHTML=String(inner); 
	append_parent!='none' ? append_parent.appendChild(newanchor) : null; 
	return newanchor;
}

function removedivdom (divs,returnfunc) {
	for (var i in divs) document.getElementById(divs[i]).parentNode.removeChild(document.getElementById(divs[i]));
	if(returnfunc) { var newfunc = new Function (returnfunc); newfunc(); }
	return;
}


//AJAX XML RETRIEVE FUNCTION
function loadxmldata(xmlurl) {
	var xmlrequest;
	if (window.XMLHttpRequest) { //Safari, Mozilla etc
		xmlrequest = new XMLHttpRequest();
		if (xmlrequest.overrideMimeType) xmlrequest.overrideMimeType('text/xml');  
	} else if (window.ActiveXObject) { //IE
		try { xmlrequest = new ActiveXObject("Msxml2.XMLHTTP"); }   
        catch (e) {
			try { xmlrequest = new ActiveXObject("Microsoft.XMLHTTP"); }
			catch (e) { }
		}
	}
    if (!xmlrequest) alert('Your browser does not seem to support the required services to load the page. Please try again with a different browser. Thank you.');
	xmlrequest.open("GET", xmlurl, false);
	xmlrequest.send(null);
	if (xmlrequest && xmlrequest!=null && xmlrequest!=undefined) {
		return xmlrequest.responseXML;
	}
}



//LIGHTBOX FUNCTIONS

function resetdimensions (newwidth,newheight,div_id) {
	document.getElementById(div_id).style.width=newwidth+'px'; 
	document.getElementById(div_id).style.height=newheight+'px';
	document.getElementById(div_id).style.margin='-'+(newheight/2)+'px 0 0 -'+(newwidth/2)+'px';
}

//ENOLIGHTBOX UTILS//
var currentimgarray=[];
var currentaltarray=[];

function enolightbox (masterdiv,url,imgarr,altarr) {
	currentimgarray=String(imgarr).split(',');//Sets global Variables for easy retrieve
	currentaltarray=String(altarr).split(',');
	createdom('div','lightbox',['width=100%','height=100%','position=absolute','margin=0','zIndex=99'],document.getElementById(masterdiv));
	createdom('div','layermask',['width=100%','height=600px','background=#000000 none','opacity=0.8','position=absolute'],document.getElementById('lightbox'));
	createdom('div','lightboxcontent',['width=1000px','height=600px','position=absolute','left=50%','top=0','margin=0 0 0 -500px'],document.getElementById('lightbox'));
	createdom ('div','displayscreen',['width=600px','height=400px','background=#000000','left=50%','top=49%','margin=-200px 0 0 -300px','position=absolute','overflow=hidden','padding=5px'],document.getElementById('lightboxcontent'));
	fader(document.getElementById('layermask'), 0, 0.9, 250); fader(document.getElementById('displayscreen'), 0, 1, 250);
	lightbox_controls();
	setTimeout(function () { loadbuffer(document.getElementById('displayscreen'), 'loadimage2(document.getElementById(\'displayscreen\'),\''+url+'\',0)' ); }, 300);
}
function loadbuffer (holderdiv,returnfunc) {
	holderdiv.innerHTML='';
	var newimage=new Image();
	newimage.onload= function () {
		var newimgdom=createdom ('img','/wp-content/themes/enoshop/template_images/loading.gif',['width=auto','height=auto','background=transparent none','top=50%','left=50%','position=absolute','border=0'],holderdiv,'LOADING');
		newimgdom.style.margin='-'+newimgdom.offsetHeight/2+'px 0 0 -'+newimgdom.offsetWidth/2+'px';
		if (returnfunc && returnfunc!='none') { setTimeout('this.passargument()',50); } else return;
		this.onload=null;
	}
	newimage.onerror=function () { 
		createdom('div','loadingerror',['width=100%','height=auto','top=48%','position=absolute','textAlign=center','font=12px Verdana','color=#FFFFFF'],holderdiv,'LOADING');
		if (returnfunc && returnfunc!='none') { setTimeout('passargument()',50); } else return;
		this.onload=null;
	}
	newimage.src='template_images/loading.gif';
	passargument = function () { var newfunc = new Function (returnfunc); newfunc(); };
}

function lightbox_controls () {
	createdom('div','brand',['width=200px','height=auto','left=0','margin=10px 0 0 0','font=bold 12px verdana','color=#666666','position=absolute'],document.getElementById('lightboxcontent'),"ENO LIGHTBOX");
	createdom('div','closelightbox',['width=100px','height=20px','right=0','background=transparent url(/wp-content/themes/enoshop/template_images/lightbox_close.png) no-repeat','margin=10px 0 0 0','position=absolute','textAlign=Center','padding=4px 0 0 0','font=11px Verdana'],document.getElementById('lightboxcontent'));
	createanchor(['href=#','onclick=javascript:clearlightbox();return false'],'Close',['color=#FFFFFF','textDecoration=none'],document.getElementById('closelightbox'));
	if (currentimgarray.length>1) {
		createdom('div','controlbar',['width=400px','height=20px','background=url(/wp-content/themes/enoshop/template_images/lightbox_menubar.png)','bottom=10px','left=50%','margin=0 0 0 -200px','position=absolute'],document.getElementById('lightboxcontent'));
		createdom('div','imageprev',['width=100px','left=0px','font=11px Verdana','position=absolute','margin=3px 0 0 0','textAlign=center'],document.getElementById('controlbar'));
		createanchor([],'Previous',['textDecoration=none','color=#999999'],document.getElementById('imageprev'));
		createdom('div','imagenext',['width=100px','right=0px','font=11px Verdana','position=absolute','margin=3px 0 0 0','textAlign=center'],document.getElementById('controlbar'));
		createanchor(['href=#','onclick=javascript:nextimage(\'1\');return false'],'Next',['textDecoration=none','color=#FFFFFF'],document.getElementById('imagenext'));
		var currentnumber=String(1+' of '+currentimgarray.length);
		createdom('div','imagenumber',['width=100px','left=50%','margin=4px 0 0 -50px','font=11px Verdana','color=#999999','textAlign=center','position=absolute'],document.getElementById('controlbar'),currentnumber);
		fader(document.getElementById('controlbar'),0,1,500);
	} return;
}

function loadimage2 (holderdiv,url,numvar) {
	var newimage= new Image();
	newimage.onload=function () {
		holderdiv.innerHTML='';
		var imgtag=createdom ('img',url,['visibility=hidden'],holderdiv,currentaltarray[numvar]);
		imgtag.onmousedown=null;
		var preparedisplay=resizedisplay();
	}
	newimage.onerror=function () {
		holderdiv.innerHTML='';
		createdom('div','errordiv',['width=100%','height=20px','top=48%','position=absolute','font=12px Verdana','color=#AAAAAA','textAlign=center'],holderdiv,'Error Loading Image');
	}
	newimage.src=url; 
	function resizedisplay () {
		var element=holderdiv.getElementsByTagName('img')[0];
		var elementwidth=holderdiv.offsetWidth;
		var elementheight=holderdiv.offsetHeight;
		if (element.offsetWidth>800 || element.offsetHeight>500 ) { //If Image is wider than 800px or higher than 500px then image is resized//
			element.offsetWidth>800 ? largerdimension='width' : largerdimension='height'; 
			if (largerdimension=='width') {
				element.style.height=element.offsetHeight/(element.offsetWidth/800)+'px';// Calculates ratio of width to height of image//
				element.style.width=800+'px'; 
			}
			if (largerdimension=='height') {
				element.style.width=element.offsetWidth/(element.offsetHeight/500)+'px';// Calculates ratio of height to width of image//
				element.style.height=500+'px';
			}
			var newwidth=element.offsetWidth; var newheight=element.offsetHeight;
		}  else {
			var newwidth=element.offsetWidth; 
			var newheight=element.offsetHeight;
		}
		with(element.style) { top='0'; left='0'; margin='0'; }
		var timer=5;
		for (var i=1; i<=50; i++) {
			var adjustwidth=(Math.round(elementwidth-(((elementwidth-newwidth)/50)*i)));
			var adjustheight=(Math.round(elementheight-(((elementheight-newheight)/50)*i)));
			window.setTimeout('resetdimensions(\''+adjustwidth+'\',\''+adjustheight+'\',\''+holderdiv.id+'\')',i*timer);
			if (i==50) { window.setTimeout( function () { imageisset(); },i*timer); }
		}
	}
	var imageisset = function () {
		holderdiv.getElementsByTagName('img')[0].style.visibility='visible';
		fader(holderdiv.getElementsByTagName('img')[0],0,1,250);
	}
}

function clearlightbox () {
	removedivdom(['lightbox']); currentimgarray=[]; currentaltarray=[];// Flushes Arrays
}
	
function nextimage(numvar) {
	Number(numvar) > 0 ? imgtype='next' : imgtype='prev';
	var newnumber;
	var imgdiv=document.getElementById('displayscreen').getElementsByTagName('img')[0];
	var temparray=imgdiv.src.split('/');
	var currentimg=temparray[temparray.length-1];
	
	for (var i in currentimgarray) { 
		var xmarraytemp = currentimgarray[i].split('/');
		
		if (currentimg==xmarraytemp[xmarraytemp.length-1]) { var currentposition=i; break; }; 
		
	}
	if (currentposition!=undefined) {
		if (imgtype=='next') {
			Number(currentposition)+1 < currentimgarray.length ? newnumber=Number(currentposition)+1 : newnumber=Number(currentposition);
			document.getElementById('imagenumber').innerHTML=(newnumber+1)+' of '+currentimgarray.length;
		} else if (imgtype=='prev') {
			Number(currentposition) > 0 ? newnumber=Number(currentposition)-1 : newnumber=Number(currentposition);
			document.getElementById('imagenumber').innerHTML=(newnumber+1)+' of '+currentimgarray.length;
		}
		checklightboxnav(newnumber+1);
		var url=String(currentimgarray[newnumber]);
		loadbuffer(document.getElementById('displayscreen'),'loadimage2(document.getElementById(\'displayscreen\'),\''+url+'\','+newnumber+')');
	}
}

function checklightboxnav (numvar) {
	var nextdiv=document.getElementById('imagenext').getElementsByTagName('a')[0];
	var prevdiv=document.getElementById('imageprev').getElementsByTagName('a')[0];
	
	
	if (numvar<currentimgarray.length && numvar>1) {
		if (nextdiv.onclick==null) { 
			nextdiv.setAttribute('href','#'); 
			nextdiv.onclick=function () { nextimage(1); } 
			nextdiv.style.color="#FFFFFF"; 
		}

		if (prevdiv.onclick==null) { 
			prevdiv.setAttribute('href','#'); 
			prevdiv.onclick=function () { nextimage(-1); }
			prevdiv.style.color="#FFFFFF";
		}
	} 
	if (numvar==currentimgarray.length) { 
		 
		nextdiv.removeAttribute('href'); 
		nextdiv.onclick=null; 
		nextdiv.style.color='#999999';
		
		if (prevdiv.onclick==null) { 
			prevdiv.setAttribute('href','#'); 
			prevdiv.onclick=function () { nextimage(-1);} 
			prevdiv.style.color="#FFFFFF";
		}
	}
	if (numvar==1) { 
		
		prevdiv.removeAttribute('href'); 
		prevdiv.onclick=null; 
		prevdiv.style.color='#999999'; 
		
		if (nextdiv.onclick==null) { 
			nextdiv.setAttribute('href','#'); 
			nextdiv.onclick = function () { nextimage(1); }
			nextdiv.style.color="#FFFFFF";
		}
	}
	return;
}




