var produceImage = true;  // to decide whether image should be shown when clicked

function getTags(divName) {
	return divs = document.getElementsByTagName(divName)
}
function hideTitle() {
	produceImage = true;
	divs = getTags('div');
	for ( var i=0;i<divs.length;i++) 
		 if ( endswith(divs[i].id,"_t") || endswith(divs[i].id,"_rs")  ) {
			document.getElementById(divs[i].id).style.display = 'none';
		 }
}
function showTitle() {
	produceImage = false;
	divs = getTags('div');
	for ( var i=0;i<divs.length;i++) 
		 if ( endswith(divs[i].id,"_t") || endswith(divs[i].id,"_rs")   ) {
			document.getElementById(divs[i].id).style.display = 'block';
		 }
}

function generateImage(myDiv,smallFile,bigFile) {
	document.popupsubmit.div_name.value = myDiv;
			document.popupsubmit.x.value = document.getElementById(myDiv).style.left;
			document.popupsubmit.y.value = document.getElementById(myDiv).style.top;
			document.popupsubmit.w.value = document.getElementById(myDiv).style.width;
			document.popupsubmit.h.value = document.getElementById(myDiv).style.height;
			/*at the moment x and y location are fixed for image on screen, need to pass that some how
				I would suggest the use of some sort of text box
			*/
			submitform('popupsubmit','dummy','processForm.php?command=createImage&x=150&y=150&smallFile='+smallFile+'&bigFile='+bigFile);		
			// the above code actually creates the image
}
function generateDivImage(smallFile,bigFile) {

	divs = getTags('div');
	var i;
	for ( i=0;i<divs.length;i++)
		if ( endswith(divs[i].id,"_b") )
			generateImage(divs[i].id,smallFile,bigFile);
}
function popwindow(mypage, myname, w, h, scroll, titlebar) {

	// to be implemented----> whenever a div  is clicked, its image will be created using cut_parts
			
	var i;
	var winl = (screen.width - w) / 2;
    var wint = (screen.height - h) / 2;
  winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars='+scroll+',resizable';
 	   
	win = window.open(mypage, 'justapage', winprops);
     
	if (parseInt(navigator.appVersion) >= 4) { 
        win.window.focus();
    }
}

// this function submits form for processing in a page
function submitform(formname,divname,actionurl){
var params = Form.serialize($(formname));
new Ajax.Updater(divname, actionurl, {asynchronous:true, parameters:params});
return false;
}

function createpopup(page,id)	{
	
	var div_name = page+"_"+id; // _b is added automatically, so no need to waste time here
	
	new popUp(200, 100, 300, 175, div_name, "", "", "black", "bold 10pt sans-serif", "", "navy", "white", "white", "gray", "black", true, true, true, true, false);		
}
function  endswith(haystack,needle) {
	var start = haystack.length - needle.length;
	var tailstr = haystack.substr(start,needle.length);
	if ( tailstr == needle ) return true;
	return false;
}

function Savepopup() {
	
	divs = getTags('div');
	for ( var i=0;i<divs.length;i++) 
		 if ( endswith(divs[i].id,"_b")  ) {
			document.popupsubmit.div_name.value = divs[i].id;
			document.popupsubmit.x.value = document.getElementById(divs[i].id).style.left;
			document.popupsubmit.y.value = document.getElementById(divs[i].id).style.top;
			document.popupsubmit.w.value = document.getElementById(divs[i].id).style.width;
			document.popupsubmit.h.value = document.getElementById(divs[i].id).style.height; 
			document.popupsubmit.display.value = Element.getStyle(divs[i].id, 'display' );
	        
			submitform('popupsubmit','dummy','lib/save_div.php');
		 }
}

