var isComplete=false; 
var adjCel=8; 
var move=0; 
var firstmove=0;
var finished=0;
var times=0;
var level=1;
var maxCoin=17;
var maxMove=100;
function Preload() {
 	var args = Preload.arguments;
	document.imageArray = new Array(args.length);
	for(var i=0; i<args.length; i++) {
		document.imageArray[i] = new Image;
		document.imageArray[i].src = args[i];

	}
}
  
function test(iid,lev){
	var dd=document.getElementById(iid); 
	var ddleft=findPosX(dd); 
	var ddtop=findPosY(dd);
	level=lev;
	if(lev==1){
		maxCoin=17;
		maxMove=100;
	
	}
	else if(lev==2){
		maxCoin=31;
		maxMove=225;
	}
	for(var g=0; g<maxCoin; g++){ 
		var cc=document.getElementById('i'+g); 
		var ccleft=findPosX(cc); 
		var cctop=findPosY(cc); 
		cc.style.top=ddtop+cctop-15+"px"; 
		cc.style.left=ddleft+ccleft-15+"px"; 
	}

} 
function findPosX(obj){ 
	var curleft = 0; 
	if (obj.offsetParent){ 
		while (obj.offsetParent){ 
			curleft += obj.offsetLeft 
			obj = obj.offsetParent; 
		} 
	} 
	else if (obj.x) 
		curleft += obj.x; 
	return curleft; 
} 
function findPosY(obj){ 
	var curtop = 0; 
	if(obj.offsetParent){ 
		while (obj.offsetParent){ 
			curtop += obj.offsetTop 
			obj = obj.offsetParent; 
		} 
	} 
	else if (obj.y) 
		curtop += obj.y; 
	return curtop; 
} 
function click(id){ 
	if(isComplete==false&&isEmpty(id)==false){ 
		if(findAdjCel(id)==true){
                    if(finished==0)
                    {
                        timer();
                   	swap('i'+adjCel,'i'+id); 
			move++; 
             		document.getElementById('move').value=move;
                        isFineshed();
                  } 
		} 
		if(move>=maxMove){ 
			alert("You lost the Game Try Again"); 
			isComplete=true; 
		} 
             }  
	 
	else{ 
	} 
} 
function isEmpty(id){ 
        id='i'+id;
	var path=document.images[id].src; 
    	var file=path.substring(path.lastIndexOf("/")+1,path.length); 
		if(file=="empty.gif"){ 
			return true; 
		} 
		else{					 
			return false; 
		} 
} 
function swap(c1,c2){//swap the images 
        var temp=document.images[c2].src; 
	document.images[c2].src=document.images[c1].src; 
	document.images[c1].src=temp; 
} 
function findAdjCel(id){ 
	var left=true;//-1 
	var right=true;//+1 
	var top=true;//-3 
	var bottom=true;//+3 
	var nLeft=false; 
	var nRight=false; 
	var nTop=false; 
	var nBottom=false;
	var width=1;
	var height=3;
	if(level==1){
		if(id==2||id==5||id==10||id==13||id==16){ 
			right=false; 
		} 
		if(id==0||id==3||id==6||id==11||id==14){ 
			left=false; 
		} 
		if(id==0||id==1||id==2||id==9||id==10){ 
			top=false; 
		} 
		if(id==6||id==7||id==14||id==15||id==16){ 
			bottom=false; 
		} 
		if(id==2||id==5||id==8||id==9||id==10||id==13||id==16){ 
			nLeft=true; 
		} 
		if(id==0||id==3||id==6||id==7||id==8||id==11||id==14){ 
			nRight=true; 
		} 
		if(id==6||id==7||id==8||id==11||id==14||id==15||id==16){ 
			nTop=true; 
		} 
		if(id==0||id==1||id==2||id==5||id==8||id==9||id==10){ 
			nBottom=true; 
		} 
	}
	else if(level==2){
		width=1;
		height=4;
		if(id==3||id==7||id==11||id==18||id==22||id==26||id==30){ 
			right=false; 
		} 
		if(id==0||id==4||id==8||id==12||id==19||id==23||id==27){ 
			left=false; 
		} 
		if(id==0||id==1||id==2||id==3||id==16||id==17||id==18){ 
			top=false; 
		} 
		if(id==12||id==13||id==14||id==27||id==28||id==29||id==30){ 
			bottom=false; 
		} 
		if(id==2||id==6||id==10||id==14||id==3||id==7||id==11||id==15||id==16||id==17||id==18||id==21||id==25||id==29||id==22||id==26||id==30){ 
			nLeft=true; 
		} 
		if(id==0||id==4||id==8||id==12||id==1||id==5||id==9||id==13||id==14||id==15||id==19||id==23||id==27||id==16||id==20||id==24||id==28){ 
			nRight=true; 
		} 
		if((id>=8&&id<=15)||(id>=23&&id<=30)||id==19){ 
			nTop=true; 
		} 
		if((id>=0&&id<=7)||(id>=15&&id<=22)||id==11){ 
			nBottom=true; 
		} 

	}
	if(left==true){ 
		if(isEmpty(parseInt(id)-width)==true){ 
			adjCel=parseInt(id)-width; 
			return true; 
		} 
	} 
	if(right==true){ 
		if(isEmpty(parseInt(id)+width)==true){ 
			adjCel=parseInt(id)+width; 
			return true; 
		} 
	} 
	if(top==true){ 
		if(isEmpty(parseInt(id)-height)==true){ 
			adjCel=parseInt(id)-height; 
			return true; 
		} 
	} 
	if(bottom==true){//alert("bottom"+id); 
		if(isEmpty(parseInt(id)+height)==true){ 
			adjCel=parseInt(id)+height; 
			return true; 
		} 
	} 
	if(nLeft==true){
		if(isEmpty(parseInt(id)-2*(width))==true){ 
			adjCel=parseInt(id)-2*(width); 
			return true; 
		} 
	} 
	if(nRight==true){ 
		if(isEmpty(parseInt(id)+2*(width))==true){ 
			adjCel=parseInt(id)+2*(width); 
			return true; 
		} 
	} 
	if(nTop==true){ 
		if(isEmpty(parseInt(id)-2*(height))==true){ 
			adjCel=parseInt(id)-2*(height); 
			return true; 
		} 
	} 
	if(nBottom==true){//alert("bottom"+id); 
		if(isEmpty(parseInt(id)+2*(height))==true){ 
			adjCel=parseInt(id)+2*(height); 
			return true; 
		} 
	} 
	return false; 
} 
function isFineshed(){ 
	var temp=true; 
	for(var i=0;i<Math.floor(maxCoin/2);i++){ 
		var iid='i'+i;
		var path=document.images[iid].src; 
		var file=path.substring(path.lastIndexOf("/")+1,path.length); 
		if(file!="white.gif"){ 
			temp=false; 
		} 
	} 
	for(var i=Math.floor(maxCoin/2)+1;i<maxCoin;i++){ 
		var iid='i'+i;
		var path=document.images[iid].src; 
		var file=path.substring(path.lastIndexOf("/")+1,path.length); 
		if(file!="black.gif"){ 
			temp=false; 
		} 
	} 
	isComplete=temp;
	if(temp==true){
                finished=1; 
                clearTimeout(timera);
	//	alert("You Won the game"); 
                testAjax(move,timee);
	} 
 
}
