// functions for displaay more puzzle dans displayt cuts
//******************************* more puzzle stuff ******************************************
// called when you click on the link

function showMorePuzzles(){ 
	my_glider = null; 
	stopcount = true;  
	var url = 'ajax_morepuzzle.php?ts=' + new Date().getTime();	
	AjaxRequest.get(		
	{ 			
		'url':url,			
		'onSuccess':function(req){ loadMorePuzzles(req.responseXML); },			
		'onError':function(){ noPuzzle();}		
	}	
	);
}

function noPuzzle(){  
	var cont = document.getElementById("puzzleapplet");  
	cont.innerHTML="<div>no puzzle!</div>";
}

function loadMorePuzzles(xml){  
	var html_content = xml.getElementsByTagName('list_dir').item(0).firstChild.nodeValue;  
	var breadscrumb = xml.getElementsByTagName('breadscrumb').item(0).firstChild.nodeValue;  
	var cont = document.getElementById("puzzleapplet");  
	cont.innerHTML= html_content;  
	var pagename = document.getElementById("pageName");  
	pagename.innerHTML=breadscrumb;
}

function saveNewTime(thistime,pid,cid,rot){  
	var url = 'ajax_savetimes.php?newtime=' + thistime + '&puzid=' + pid + '&cutid=' + cid + '&rotation=' + rot;	
	AjaxRequest.get(		
	{ 			
		'url':url,			
		'onSuccess':function(req){ UpdatePuzzleTimes(req.responseXML,pid,cid,rot); },			
		'onError':function(){ noPuzzle();}		}	
	);
}

// function called when user clicks on a category that contains sub categories
function showSubCategory(imgdir){	
	
	my_glider = null; 
	// strip Archives from the imgdir and pass to showCategory	
	var catdir = imgdir.substring(9,imgdir.length);	
	showCategory(catdir);
}

function UpdatePuzzleTimes(xml,puzid,cutid,rot){  
  //alert(puzid + " - " + cutid + " - " + rot);
	var html_avgtime = xml.getElementsByTagName('avgtime').item(0).firstChild.nodeValue;  
	var html_besttime = xml.getElementsByTagName('besttime').item(0).firstChild.nodeValue;  
	//alert(xml.getElementsByTagName('avgtime').item(0).firstChild.nodeValue + " - " + xml.getElementsByTagName('besttime').item(0).firstChild.nodeValue);
	var bestt = document.getElementById("bigtime");  
	var avg = document.getElementById("avgtime");
	var your = document.getElementById("besttime");
	
	bestt.innerHTML = html_besttime;
	avg.innerHTML = html_avgtime;
	// get users time and check if it is the best time  
	var thistime = document.getElementById('yourtime');
	if ( (thistime.innerHTML=="00:00") && (html_besttime=="00:00"))  {  }  
	else  {    
		if (thistime.innerHTML == html_besttime)    { 	  
			generateNewRecordCode();    
		}  
	}
}

// function called when use clicks on the image of a category
function  showCategory(catName){ 
	var rot = document.getElementById('rotation');
	if (rot.checked == false){
		rot = 0;
	}else
	{
		rot = 5;
	}
	
	var url = 'ajax_showcategory.php?cat=' + catName + '&rot=' + rot;	
	AjaxRequest.get(		
		{ 			
		'url':url,			
		'onSuccess':function(req){ loadCategoryPuzzles(req.responseXML); },			
		'onError':function(){ noPuzzle();}		
		}	
	);
}

function loadCategoryPuzzles(xml){  
	var cont = document.getElementById("puzzleapplet");  
	var html_content;
	if( browser == "IE"){
		html_content = xml.getElementsByTagName('cat_dir').item(0).firstChild.nodeValue;  
	}
	else{
		html_content = xml.getElementsByTagName('cat_dir')[0].textContent;
	}
	//var html_content = xml.getElementsByTagName('cat_dir').item(0).firstChild.nodeValue;  
	var cat_content = xml.getElementsByTagName('cat_name').item(0).firstChild.nodeValue;  
	var breabscrumb = xml.getElementsByTagName('breadscrumb').item(0).firstChild.nodeValue;
	cont.innerHTML= html_content;  
	var pagename = document.getElementById("pageName");  
	pagename.innerHTML = breabscrumb;
}

//************************** cut stuff **********************************************************
// replaces the applet with a table that shows all cuts possible
function showCuts(){  
	AjaxRequest.get(	
	{ 		
		'url':'tablecuts.html',		
		'onSuccess':function(req){ displayCuts(req.responseText);}	
	}  
	);
}
//called when user clicks on a cut in the display cut table
function newChangeCut(acut,mclip){	
	img_cut = acut;	
	clip = mclip;	
	SetCookie("favorite_cut",acut,exp);	
	var appid = document.getElementById("puzzle");	
	// resets the time best time and avt times	
	resetStats();	
	generateAppletCode(img_big,ssid,img_cut,clip);
}

function changeCut(acut){	
	newChangeCut(acut,'true');
}

function changeOverColor(tagid){	
	var aCut = document.getElementById(tagid);	
	aCut.color="red";
}

function displayCuts(text){  
	var cont = document.getElementById("puzzleapplet");  
	cont.innerHTML= text;
}

function saveRecordPopup(){  
	var theurl="/updateRecord.php?puzid=" + puzzleid + "&cutid=" + img_cut;  
	var winpops=window.open(theurl ,"mywindow","width=800,height=400");
}
