// JavaScript Document
//global vars for image
var previmg=0;
var nextimg=0;
var arrseq=Array();

function ini_arr(str){
	arrseq=str.split("|");
	if(arrseq.length >= 1){
		previmg=arrseq.length - 1;
		nextimg=0;
	}
	
}
function getArrIndex(val){
	for(var i=0;i<arrseq.length;++i){
		if(parseInt(arrseq[i])==parseInt(val)){
			return i;
		}
	}
	return -1;
}
function GetXmlHttpObject(){
  var xmlHttp=null;
  try{
    // Firefox, Opera 8.0+, Safari
    xmlHttp=new XMLHttpRequest();
    }catch(e){
    // Internet Explorer
    try{
      	xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
      }catch(e){
     	 xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    }
  return xmlHttp;
}
function post(xmlHttp,data,file){
	xmlHttp.open("POST",file,true); 
  	xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8"); 
	xmlHttp.send(data); 
}
function get(xmlHttp,url){
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);	
}
function show_element(element){
	document.getElementById(element).style.display="block";
}
function hide_element(element){
	document.getElementById(element).style.display="none";
}
function get_proimage(frm){
	var imageid=frm.pimg.options[frm.pimg.selectedIndex].value;
	var nextind=(parseInt(frm.pimg.selectedIndex)<parseInt(frm.pimg.options.length)-1)?parseInt(frm.pimg.selectedIndex)+1:0;
	var prevind=(parseInt(frm.pimg.selectedIndex)>0)?parseInt(frm.pimg.selectedIndex)-1:parseInt(frm.pimg.options.length)-1;
	var nextimgid=frm.pimg.options[nextind].value;
	var previmgid=frm.pimg.options[prevind].value;
	
	if(imageid=="" || typeof(imageid)==null){
		return;	
	}else{
		xmlHttp=GetXmlHttpObject();
		if(xmlHttp==null){
			alert ("Your browser does not support AJAX!");
			return;
		}
		var image= "images/loading.gif";
		document.getElementById('proimage').innerHTML="<img src='"+image+"' border=0>";
		xmlHttp.onreadystatechange=function imgstatechanged(){
			if(xmlHttp.readyState==4){ 
				var result=xmlHttp.responseText;
				if(result.length>0){
					var arr=result.split("||");
					var image='img.php?file='+arr[0];
					document.getElementById('proimage').innerHTML="<a href='"+arr[2]+"'><img src='"+image+"' border=0></a>";
					document.getElementById('shortdesc').innerHTML=arr[1];
					preLoadImage(arr[3],arr[4]);
				}
			}
		}
		var postdata="imageid="+imageid+"&next="+nextimgid+"&prev="+previmgid;
		post(xmlHttp,postdata,'proimg.php');
	}
}
function navigateImage(nav){
	var imgno;
	if(parseInt(nav)==0){
		imgno=arrseq[previmg];
	}else if(parseInt(nav)==1){
		imgno=arrseq[nextimg];	
	}else{
		//modified
		imgno=parseInt(nav);	
	}
	xmlHttp=GetXmlHttpObject();
	if(xmlHttp==null){
		alert ("Your browser does not support AJAX!");
		return;
	}
	var image= "/images/loading.gif";
	document.getElementById('imgframe').innerHTML="<img src='"+image+"' border=0>";
	xmlHttp.onreadystatechange=function imgnavigate(){
			if(xmlHttp.readyState==4){ 
				var result=xmlHttp.responseText;
				if(result.length>0){
					var arr=result.split("||");
					var curimg=parseInt(arr[4]);
					var index=(curimg > 0)?getArrIndex(curimg):-1;
					previmg=(index >= 0)?index-1:0;
					nextimg=(index >= 0)?index+1:0;
					if(nextimg >= arrseq.length){
						nextimg=0;
					}
					if(previmg < 0){
						previmg=arrseq.length-1;
					}
					var image="/img.php?file="+arr[2];
					document.getElementById('imgframe').innerHTML="<img src='"+image+"' border=0>"+"<BR><div>"+arr[3]+"</div>";
				}
			}
		}
		var url="/imgnavigate.php?sequence="+imgno;
	get(xmlHttp,url);
}
function preLoadImage(img1,img2){
	try{
		//var pimg=new Image(215,300);
		//var nimg=new Image(215,300);
		//pimg.src="/img.php?file="+img1;
		//nimg.src="/img.php?file="+img2;
		document.getElementById('nextimg').src="/img.php?file="+img1;
		document.getElementById('previmg').src="/img.php?file="+img2;
	}catch(e){
		//alert('preload exception');
		//return;
	}
}
function imgPreLoad(location){
	if(typeof(location)!=null){
		arrimg=location.split("|");
		for(var i=0;i<arrimg.length;++i){
			var newimg=new Image(60,60);
			newimg.src='/img.php?file=images/models/'+src;
		}
	}
}
