var allData = "";
var oldSearch = "zzz";
var searchResult = new Array();
var currentSearchValue = "";
var fadeInterval = 0;

function loadTab(which){
	//the tabs are aboutpage and faqpage
	document.getElementById("aboutPage").style.display = "none";
	document.getElementById("faqPage").style.display = "none";
	document.getElementById("downloadPage").style.display = "none";
	document.getElementById("instructionsPage").style.display = "none";
	document.getElementById(which).style.display = "block";


}

function loaded(){

}

/*function goToScreenshot(folder, num) {
	document.getElementById("iPhoneScreenshot").src = "images/screenshots/"+sName+"/"+which+".jpg";

}*/

function showVideo(){

	$("videoHolder").setOpacity(0.0);

	$("videoHolder").style.display = "block";

//	Effect.Fade("iPhoneScreenshot");
//	Effect.show("videoHolder");
	$("videoHolder").appear();

}

function hideVideo(){




	$("videoHolder").style.display = "block";

//	Effect.Fade("iPhoneScreenshot");
//	Effect.show("videoHolder");
	$("videoHolder").fade();


	
}

function goToScreenshot(sName, which){

if (fadeInterval) window.clearInterval(fadeInterval);


//fadeInterval = window.setInterval("showNextImage('"+sName+"');", 4000);

	if (fadeInterval) clearInterval(fadeInterval);
	fadeInterval = window.setInterval("showNextImage('"+sName+"');", 3000);
	currentShirtImage = which;
	nextShirtImage = currentShirtImage+1;
	if (nextShirtImage > maxShirtImage) nextShirtImage = 1;

//	Element.setOpacity("iPhoneScreenshot", 1);
	document.getElementById("iPhoneScreenshot").style.display = "block";
	document.getElementById("iPhoneScreenshot").src="/newmagnetapps/Screenshots/"+sName+"/" + currentShirtImage+".jpg";

	document.getElementById("iPhoneScreenshotBG").src="/newmagnetapps/Screenshots/"+sName+"/" +nextShirtImage+".jpg";
//	highlightNumber(which);
/*	o("navNumberImage1").setOpacity(0.3);
	o("navNumberImage2").setOpacity(0.3);
	o("navNumberImage3").setOpacity(0.3);
	o("navNumberImage4").setOpacity(0.3);
	o("navNumberImage5").setOpacity(0.3);

	o("navNumberImage"+which).setOpacity(1);*/

}

function testAlert(){
	
}

function jumpToScreenshot(sName, which){
	
}


function showNextImage(whichShirt){


	document.getElementById("iPhoneScreenshot").style.display = "block";
	Element.setOpacity("iPhoneScreenshot", 1.0);

	Effect.Fade("iPhoneScreenshot");
	//highlightNumber(nextShirtImage);
	
	window.setTimeout("changeOP('"+whichShirt+"');", 1500);
	
/*	o("navNumberImage1").setOpacity(0.3);
	
	o("navNumberImage2").setOpacity(0.3);
	o("navNumberImage3").setOpacity(0.3);
	o("navNumberImage4").setOpacity(0.3);
	o("navNumberImage5").setOpacity(0.3);

	o("navNumberImage"+currentShirtImage).setOpacity(1);*/
}

function changeOP(whichShirt){
	currentShirtImage = nextShirtImage;
	nextShirtImage++;
	if (nextShirtImage > maxShirtImage) nextShirtImage = 1;

	document.getElementById("iPhoneScreenshot").src = "/newmagnetapps/Screenshots/"+whichShirt+"/" + currentShirtImage+".jpg";
	Element.setOpacity("iPhoneScreenshot", 1.0);
	document.getElementById("iPhoneScreenshot").style.display = "block";
	document.getElementById("iPhoneScreenshotBG").src = "/newmagnetapps/Screenshots/"+whichShirt+"/" + nextShirtImage+".jpg";
//highlightNumber(currentShirtImage);

}


function highlightNumber(which){
	//maxShirtImage
	var opI = 1;

	while (opI <= maxShirtImage) {
		Element.setOpacity("navNumberImage"+opI, .3);
		opI++;
	}

	Element.setOpacity("navNumberImage"+which, 1.0);
}

function debug(msg) {
	if (!debug.box) {
		debug.box = document.createElement("div");
		debug.box.setAttribute("style", "background-color: white; " +
										"color: black; " +
										"font-family: monospace; " +
										"font-size: 10; " + 
										"border: solid black 3px; " +
										"position: absolute;top:0px;" +
										"z-index: 100;" +
										"opacity:.8;" +
										"padding: 2px;");
		document.body.appendChild(debug.box);
	}
	
	var p = document.createElement("p");
	p.appendChild(document.createTextNode(msg));
	debug.box.appendChild(p);
}

function voteFor(winner){

	var voteSTR = winner;
	var i = 0; 
	while (i < captionList.length){
		if (captionList[i] != winner) {
			voteSTR += "|"+captionList[i];

		}
		i++;
	}
	vote(voteSTR);

	slideOut(winner, 0);
//	getNew();
}


function fadeOutOthers(winner, op){

	op = op-10;
	var i = 0;
	while (i < captionList.length) {
		if (captionList[i] != winner) {
			document.getElementById(captionList[i]).style.opacity = op/100;
		}
		i++;
	}

	if (op > 20) window.setTimeout("fadeOutOthers("+winner+", "+op+");", 50);
	else slideOut(winner, 0);
}

function slideOut(winner, loc){
	loc = loc + 20;
	document.getElementById('captions').style.left = 20 - loc;
	document.getElementById(winner).style.left = loc*2;
	
	var i = 0;
	while (i < captionList.length) {
		if (captionList[i] != winner) {
			document.getElementById(captionList[i]).style.opacity = 1-loc/320;
		}
		i++;
	}
	
	if (loc < 320) window.setTimeout("slideOut("+winner+", "+loc+");", 50);
	else getNew();
}

function getNew(){
	if(navigator.appName == "Microsoft Internet Explorer") {
		xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
	} else {
		xmlHttp = new XMLHttpRequest();
	}

	var url="getMore.php";

	xmlHttp.onreadystatechange=getNewHandler;

	xmlHttp.open("GET",url,true);

	xmlHttp.send(null);	
}

function getNewHandler(){

	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){
		allData = xmlHttp.responseText;


		allDataArray = allData.split('|');


		document.getElementById('captions').style.opacity = 0;
		document.getElementById('captions').style.left = "20px";
		document.getElementById('captions').innerHTML = allDataArray[0];

		captionList[0] = allDataArray[1];
		captionList[1] = allDataArray[2];
		captionList[2] = allDataArray[3];

		fadeInCaptions(0);
		
	}	
}

function fadeInCaptions(op){
	op = op + 125;
	document.getElementById('captions').style.opacity = op / 1000;
	if (op < 1000) window.setTimeout("fadeInCaptions("+op+");", 50);
	
}


function vote(voteStr){
	if(navigator.appName == "Microsoft Internet Explorer") {
		xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
	} else {
		xmlHttp = new XMLHttpRequest();
	}

	var url="vote.php?voteStr="+voteStr;

	xmlHttp.onreadystatechange=voteHandler;

	xmlHttp.open("GET",url,true);

	xmlHttp.send(null);	
}

function voteHandler(){

	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){
		allData = xmlHttp.responseText;
				allDataArray = allData.split('|');


		//		document.getElementById('captions').style.opacity = 0;
		//		document.getElementById('captions').style.left = "20px";

				document.getElementById('secondList').innerHTML = allDataArray[0];

				captionList[0] = allDataArray[1];
				captionList[1] = allDataArray[2];
				captionList[2] = allDataArray[3];

		//		fadeInCaptions(0);


		        setTimeout(swipePage, 0, document.getElementById("albums"), document.getElementById('secondList'), 1);
		
	}	
}

function mainSearch(searchValue){
	if (searchValue.length > 1) {
		if (searchValue.search(oldSearch) != -1) {
	//		debug("OK " + searchValue);
			currentSearchValue = searchValue;
			search();
		} else {
			oldSearch = searchValue;
			currentSearchValue = searchValue;
			//get the new search from php
			getFromDB(searchValue);
	//		debug("SET NEW" + oldSearch);
		}
	}
}

function getFromDB(s){

	//xmlHttp = new XMLHttpRequest();
	if(navigator.appName == "Microsoft Internet Explorer") {
		xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
	} else {
		xmlHttp = new XMLHttpRequest();
	}

	var url="indiv.php?searchString="+s;

	xmlHttp.onreadystatechange=getFromDBHandler;

	xmlHttp.open("GET",url,true);

	xmlHttp.send(null);	
}

function getFromDBHandler(){

	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){
		allData = xmlHttp.responseText;
		search();
	}	
}

function search(){

	regexp = new RegExp(currentSearchValue, 'gi');

	var allDataTemp = allData;
	var j = 0;
	var listOfInd = new Array();
	var notdone = true;
	var lastone = 0;
	while (notdone) {
		


		tempIndex = allDataTemp.search(regexp);
		if (tempIndex != -1) {
				listOfInd[j] = tempIndex+lastone;
				lastone = tempIndex+lastone+1;
				allDataTemp = allDataTemp.substr(tempIndex+1, allDataTemp.length);
				j++;
		} else break;
				

	}


	document.getElementById("searchResults").innerHTML = "";

	var begin = 0;
	var end = 0;
	var longHTML = "";

	for (i = 0; i < listOfInd.length; i++){
		begin = allData.lastIndexOf("|", parseInt(listOfInd[i]));
		end = allData.indexOf("|", parseInt(listOfInd[i]));

			tempString = allData.substring(begin+1, end);
			
			tempNumbers = tempString.lastIndexOf(",", tempString.length);
			orderNumber = parseInt(tempString.substring(tempNumbers+2));
			
			loc = tempString.search(regexp);
			firstString = tempString.substring(0, loc);
			secondString = tempString.substring(loc, currentSearchValue.length+loc);
			thirdString = tempString.substring(currentSearchValue.length+loc);
			rowClass = "searchResultRow1";
			if (i % 2 == 0) rowClass = "searchResultRow2";
			longHTML = longHTML + "<p class='searchResult "+rowClass+"' onclick='getIndividualOrder("+orderNumber+")'>"+ firstString + "<strong>" + secondString + "</strong>" + thirdString + "</p>";
		
	}
		document.getElementById("searchResults").innerHTML = longHTML;//allData.getIndices(regexp).length;//searchMatch[0];
}

function getIndividualOrder(which){

	if(navigator.appName == "Microsoft Internet Explorer") {
		xmlHttp2 = new ActiveXObject("Microsoft.XMLHTTP");
	} else {
		xmlHttp2 = new XMLHttpRequest();
	}

	var url="indivPreview.php?orderid="+which;

	xmlHttp2.onreadystatechange=getIndividualOrderHandler;

	xmlHttp2.open("GET",url,true);

	xmlHttp2.send(null);	
}

function getIndividualOrderHandler(){

	if (xmlHttp2.readyState==4 || xmlHttp2.readyState=="complete"){
		document.getElementById("orderInfo").innerHTML = xmlHttp2.responseText;
	}	
}