var quer = null;
var enableReflections = false;
var enableGlossy = false;
var enableInstant = true;
var loaded = 0;
var warn = Array();
var nextauto = false;
var prevauto = false;

if(enableReflections){
	document.getElementById('imageeffects').src = 'reflection.js';
	Reflection.defaultOpacity = 0.3;
}
else if(enableGlossy){
	document.getElementById('imageeffects').src = 'cvi_glossy_lib.js';
}
else if(enableInstant){
	document.getElementById('imageeffects').src = 'cvi_instant_lib.js';
}
function warning(on){
	if(!on){
		on = confirm('This will allow adult images in the search results, are you sure?');
		adult.checked = !on;
	}
}
function rotated(){
	if(window.orientation==0 || window.orientation==180 || typeof window.orientation=='undefined'){
		maxwidth = 320;
		maxheight = 416;
		document.getElementById("full").style.paddingTop = '110px';
		document.getElementById('out').style.minHeight = '290px';
	}
	else{
		maxwidth = 480;
		maxheight = 268;
		document.getElementById("full").style.paddingTop = '30px';
		document.getElementById('out').style.minHeight = '141px';
	}
	document.getElementById("bodydiv").style.width = maxwidth+'px';
	document.getElementById("full").style.width = maxwidth+'px';
	document.getElementById("full").style.height = maxheight+'px';
	document.getElementById("about").style.width = maxwidth+'px';
	document.getElementById("about").style.height = maxheight+'px';
	if(document.getElementById("full").style.display=='block'){
		var width = parseInt(ResultSet.Result[viewingimg].Width);
		var height = parseInt(ResultSet.Result[viewingimg].Height);
		if(width>=maxwidth){
			var newwidth = maxwidth;
			var newheight = Math.round(height*(newwidth/width));
			if(newheight>maxheight){
				newheight=maxheight;
				newwidth = Math.round(width*(newheight/height));
			}
		}
		else if(height>=maxheight){
			var newheight = maxheight;
			var newwidth = Math.round(width*(newheight/height));
		}
		else{
			var newwidth = width;
			var newheight = height;
		}
		document.getElementById('overlay').style.height = document.body.scrollHeight+'px';
		document.getElementById("full").style.backgroundPosition = ((maxwidth-newwidth)/2)+'px '+((maxheight-newheight)/2)+'px';
		document.getElementById("full").style.webkitBackgroundSize = newwidth+'px '+newheight+'px';
		document.getElementById("full").style.top = window.pageYOffset+'px';
		window.scrollTo(100,window.pageYOffset);
	}
	if(quer!=null){
		drawtable();
	}
}
var url = 'http://search.yahooapis.com/ImageSearchService/V1/imageSearch?appid=E9LEmPvV34GAaLMjbL.24t6wPQLrR3LHohjeG3DcPyWBvi2xiPCuLhNcpFT812h1&output=json&callback=results&query=';
var ResultSet;
var start = 1;
var naughty = '';
function formsub(){
	if(!adult.checked){ naughty='&adult_ok=1'; } else{ naughty=''; }
	search(query.value,1);
	document.getElementById("adult").focus();
	return false;
}
function search(q,st){
	loaded = 0;
	quer = q;
	document.getElementById("out").innerHTML = '<br/><br/>Loading Images...';
	if(window.orientation==0 || window.orientation==180 || typeof window.orientation=='undefined'){
		displayCPI(document.getElementById("out"),200,'#ffffff');
	}
	else{
		displayCPI(document.getElementById("out"),100,'#ffffff');
	}
	json = document.getElementById('json');
	if(json){ json.parentNode.removeChild(json); }
	json = document.createElement('script');
	json.type = 'text/javascript';
	json.id = 'json';
	json.src = url+escape(q)+'&start='+st+naughty;
	document.getElementsByTagName("head")[0].appendChild(json);
	start = st;
	return false;
}
function results(res){
	ResultSet = res.ResultSet;
	if(ResultSet.totalResultsAvailable=='0'){ document.getElementById("out").innerHTML = 'No results.'; }
	else{
		drawtable();
	}
}
function reflectMe(img){
	img.style.display = 'block';
	Reflection.add(img);
}
function glossMe(img){
	img.style.display = 'block';
	cvi_glossy.add(img);
}
function instantMe(img,num){
	//num = Math.floor(Math.random()*2);
	var tilty = 'n';
	if(num==0){
		tilty = 'l';
	}
	else if(num==2 || (num==1 && (window.orientation==0 || window.orientation==180 || typeof window.orientation=='undefined'))){
		tilty = 'r';
	}
	img.style.display = 'block';
	cvi_instant.add(img,{tilt:tilty,shadow:60});
}
function drawtable(){
	warn = Array();
	document.getElementById("out").innerHTML = '';
	var outtab = document.createElement("table");
	var tr;
	if(maxwidth==480){
		var rowelem = 3;
	}
	else{
		var rowelem = 2;
	}
	for(i=0;ResultSet.Result[i];i++){
		var filesize = parseInt(ResultSet.Result[i].FileSize);
		var width = parseInt(ResultSet.Result[i].Width);
		var height = parseInt(ResultSet.Result[i].Height);
		if(width>=((maxwidth/rowelem)-5)){
			var newwidth = ((maxwidth/rowelem)-5);
			var newheight = Math.round(height*(newwidth/width));
		}
		else{
			var newwidth = width;
			var newheight = height;
		}
		if(i%rowelem==0){ tr = document.createElement('tr'); }
		var td = document.createElement('td');
		if(filesize<256000){
			td.setAttribute('onClick','fullimage('+i+'),false');
			warn[i] = false;
		}
		else{
			td.setAttribute('onClick','fullimage('+i+',true)');
			warn[i] = true;
		}
		var img = document.createElement('img');
		img.src = ResultSet.Result[i].Thumbnail.Url;
		img.width = newwidth;
		img.height = newheight;
		img.style.marginTop = '20px';
		img.style.width = newwidth+'px';
		img.style.height = newheight+'px';
		img.style.display = 'none';
		if(enableReflections){ img.setAttribute('onLoad','loaded++; reflectMe(this)'); td.setAttribute('valign','bottom'); }
		else if(enableGlossy){ img.setAttribute('onLoad','loaded++; glossMe(this)'); }
		else if(enableInstant){ img.setAttribute('onLoad','loaded++; instantMe(this,'+(i%rowelem)+')'); }
		else{ img.setAttribute('onLoad','loaded++; this.style.display=\'block\''); }
		td.appendChild(img);
		td.style.height = (newheight+2)+'px';
		tr.appendChild(td);
		if(i%rowelem==rowelem-1){
			outtab.appendChild(tr);
			tr='nope';
			testheight=0;
		}
	}
	if(tr!='nope'){ outtab.appendChild(tr); }
	var tr = document.createElement('tr');
	tr.id = 'navrow';
	var td = document.createElement('td');
	if(start!=1){
		td.className = 'navbutton';
		td.style.textAlign = 'center';
		td.setAttribute('onClick','prev()');
		td.innerHTML = '&laquo; Prev';
	}
	tr.appendChild(td);
	if(rowelem==3){
		var td = document.createElement('td');
		tr.appendChild(td);
	}
	var td = document.createElement('td');
	if((start+ResultSet.totalResultsReturned)<ResultSet.totalResultsAvailable){
		td.className = 'navbutton';
		td.style.textAlign = 'center';
		td.setAttribute('onClick','next()');
		td.innerHTML = 'Next &raquo;';
	}
	tr.appendChild(td);
	outtab.appendChild(tr);
	document.getElementById("out").appendChild(outtab);
	if(nextauto){ nextauto = false; fullimage(0); }
	if(prevauto){ prevauto = false; fullimage(9); }
}
function prev(autoload){
	if(autoload){ prevauto = true; }
	search(quer,start-10);
}
function next(autoload){
	if(autoload){ nextauto = true; }
	search(quer,start+10);
}
function fullimage(i,warnme){
	if(i>9){
		closefullimage();
		next(true);
	}
	else if(i<0){
		closefullimage();
		prev(true);
	}
	else{
		viewingimg = i;
		document.getElementById("full").style.backgroundImage = '';
		document.getElementById("navright").style.display = 'none';
		document.getElementById("navleft").style.display = 'none';
		document.getElementById("full").innerHTML = '<br/>';
		var width = parseInt(ResultSet.Result[i].Width);
		var height = parseInt(ResultSet.Result[i].Height);
		if(width>=maxwidth){
			var newwidth = maxwidth;
			var newheight = Math.round(height*(newwidth/width));
			if(newheight>maxheight){
				newheight=maxheight;
				newwidth = Math.round(width*(newheight/height));
			}
		}
		else if(height>=maxheight){
			var newheight = maxheight;
			var newwidth = Math.round(width*(newheight/height));
		}
		else{
			var newwidth = width;
			var newheight = height;
		}
		document.getElementById("full").style.opacity = '0.5';
		document.getElementById("full").style.backgroundImage = 'url('+ResultSet.Result[i].Thumbnail.Url+')';
		document.getElementById("preloader").setAttribute('onError',"if(this.src!=''){ showarrows(); document.getElementById('full').style.backgroundImage = ''; document.getElementById('full').style.opacity = '1'; document.getElementById('full').innerHTML = '<span style=\"color:red; font-size:26px;\">Image failed to load.</span>'; }");
		document.getElementById("preloader").src = ResultSet.Result[i].Url;
		document.getElementById("full").style.backgroundPosition = ((maxwidth-newwidth)/2)+'px '+((maxheight-newheight)/2)+'px';
		document.getElementById("full").style.webkitBackgroundSize = newwidth+'px '+newheight+'px';
		document.getElementById("full").style.top = window.pageYOffset+'px';
		document.getElementById("navleft").style.top = (window.pageYOffset+5)+'px';
		document.getElementById("navright").style.top = (window.pageYOffset+5)+'px';
		document.getElementById('overlay').style.height = document.body.scrollHeight+'px';
		document.getElementById("overlay").style.display = 'block';
		document.getElementById("full").style.display='block';
		if(warnme | warn[i]){ document.getElementById("full").innerHTML = '<br/><br/><br/><br/>Warning: Image size is greater than 250KB'; }
		displayCPI(document.getElementById("full"),150,'#FFFFFF');
		window.scrollTo(100,window.pageYOffset);
		showarrows();
	}
}
function fullImageLoaded(){
	showarrows();
	document.getElementById('full').innerHTML = '';
	document.getElementById("full").style.opacity = '1';
	document.getElementById("full").style.backgroundImage = 'url('+document.getElementById("preloader").src+')';
	document.getElementById("preloader").setAttribute('onerror',"");
	document.getElementById("preloader").src = '';
}
function showarrows(){
	document.getElementById("navright").setAttribute('onclick','fullimage('+(viewingimg+1)+')');
	document.getElementById("navright").style.display = 'block';
	if(viewingimg!=0 || start>1){
		document.getElementById("navleft").setAttribute('onclick','fullimage('+(viewingimg-1)+')');
		document.getElementById("navleft").style.display = 'block';
	}
}
function closefullimage(){
	viewingimg = null;
	document.getElementById("navright").style.display = 'none';
	document.getElementById("navleft").style.display = 'none';
	document.getElementById("full").innerHTML = '';
	document.getElementById("full").style.display='none';
	document.getElementById("overlay").style.display = 'none';
	document.getElementById("full").style.backgroundImage = '';
	document.getElementById("preloader").setAttribute('onerror',"");
	document.getElementById("preloader").src = '';
}
function repos(){
	if(document.getElementById("full").style.display=='block'){
		document.getElementById("navleft").style.top = (window.pageYOffset+5)+'px';
		document.getElementById("navright").style.top = (window.pageYOffset+5)+'px';
		document.getElementById("full").style.top = window.pageYOffset+'px';
	}
	document.getElementById("about").style.top = window.pageYOffset+'px';
}
function aboutapp(){
	document.getElementById('about').style.display = 'block';
}
function closeabout(){
	document.getElementById('about').style.display = 'none';
}

