var myBook,p1,p2,p3,p4;
var pageBorderColor="#000000",pageBorderStyle="solid",pageBorderWidth="1";
var isMoving=false;
var nextPage=0;

var ID,numPixels=20;
var pWidth=300;
var pHeight=361;
var pSpeed=30;
var counter=0;
var pBorders;
var pages=new Array;
function ini(){
pBorders=parseInt(pageBorderWidth)*2;
	if(myBook=document.getElementById("bookflip")){
		myBook.style.width=(pWidth*2)+(pBorders*2)+'px';
		myBook.style.height=pHeight+pBorders+'px';
		myBook.style.position="relative";
		myBook.style.zIndex="0";
		myBook.style.overflow="hidden";


		for(i=4;i>=1;i--){
			var myDiv = document.createElement('div');

			myDiv.style.position="absolute";
			myDiv.style.left=pWidth+'px';
			myDiv.style.top="0px";
			myDiv.style.width=pWidth+"px";
			myDiv.style.height=pHeight+"px";
			myDiv.style.margin="0px";
			myDiv.style.overflow="hidden";
			myDiv.style.backgroundColor="#ffffff";

			myDiv.style.border=pageBorderWidth + "px " + pageBorderStyle + " " + pageBorderColor;

			myDiv.setAttribute("id","p"+i);
			myBook.appendChild(myDiv);

			ajaxFunction("p" + i,pages[i-1]);

		}
			p1=document.getElementById("p1");
			p2=document.getElementById("p2");
			p1.style.left=0+"px";

			ajaxFunction("next1",pages[2]);
			ajaxFunction("next2",pages[3]);
	}
}

function rotateDivs(){
			ajaxFunction("next1",pages[nextPage+2]);
			ajaxFunction("next2",pages[nextPage+3]);
			ajaxFunction("prev1",pages[nextPage-2]);
			ajaxFunction("prev2",pages[nextPage-1]);




	if(counter>0){
		p1=document.getElementById("p1");
		p2=document.getElementById("p2");
		p3=document.getElementById("p3");
		p4=document.getElementById("p4");
		counter=0;
	}else{
		p1=document.getElementById("p3");
		p2=document.getElementById("p4");
		p3=document.getElementById("p1");
		p4=document.getElementById("p2");
		counter=1;
	}

}

function clipmeR(){
	if(isMoving || nextPage<2){return;};
	isMoving=true;
	nextPage=nextPage-2;

	rotateDivs();

	//ajaxFunction(p1.id,pages[nextPage]);
	//ajaxFunction(p2.id,pages[nextPage+1]);

	p1.innerHTML=document.getElementById("prev1").innerHTML;
	p2.innerHTML=document.getElementById("prev2").innerHTML;


	p1.style.clip="rect(0px,"+0+"px,"+pHeight+pBorders+"px,"+0+"px)";//rect(top,right,bottom,left)
	p1.style.zIndex=3;
	p3.style.zIndex=1;
	p2.style.clip="rect(0px,"+0+"px,"+pHeight+pBorders+"px,"+0+"px)";//rect(top,right,bottom,left)
	p2.style.zIndex=4;
	p4.style.zIndex=2;

	goleft(-pWidth,pWidth);
}

function goleft(currLeft,currWidth){
	if(currLeft >=(pWidth-(numPixels))){
		window.clearTimeout(ID);
		p2.style.left=pWidth+"px";

		p1.style.clip="rect(0px,"+pWidth+pBorders+"px,"+pHeight+pBorders+"px,"+0+"px)";//rect(top,right,bottom,left)
		p2.style.clip="rect(0px,"+pWidth+pBorders+"px,"+pHeight+pBorders+"px,"+0+"px)";//rect(top,right,bottom,left)

		isMoving=false;

		return;
	}
	currLeft=currLeft+(numPixels*2);//2 * width reveal
	currWidth = currWidth-numPixels;
	hideWidth=pWidth-currWidth;
	p1.style.clip="rect(0px,"+hideWidth+"px,"+pHeight+pBorders+"px,"+0+"px)";//rect(top,right,bottom,left) //low to high

	p2.style.clip="rect(0px,"+pWidth+pBorders+"px,"+pHeight+pBorders+"px,"+currWidth+"px)";//rect(top,right,bottom,left)
	p2.style.left=currLeft+'px';

	ID = window.setTimeout('goleft('+currLeft+','+currWidth+')',pSpeed);
}



function clipmeL(){
	if(isMoving || (nextPage+3) > pages.length){return;};
	isMoving=true;
	nextPage=nextPage+2;

	rotateDivs();
	//ajaxFunction(p1.id,pages[nextPage]);
	//ajaxFunction(p2.id,pages[nextPage+1]);
	p1.innerHTML=document.getElementById("next1").innerHTML;
	p2.innerHTML=document.getElementById("next2").innerHTML;


	p1.style.clip="rect(0px,"+0+"px,"+pHeight+pBorders+"px,"+0+"px)";//rect(top,right,bottom,left) clip
	p1.style.zIndex=5;
	p2.style.clip="rect(0px,"+0+"px,"+pHeight+pBorders+"px,"+0+"px)";//rect(top,right,bottom,left) clip
	p2.style.zIndex=4;
	p3.style.zIndex=2;
	p4.style.zIndex=1;
	goright(pWidth*2,0);
}

function goright(currLeft,currWidth){
	if(currLeft <= numPixels*2){
		window.clearTimeout(ID);
		p1.style.clip="rect(0px,"+pWidth+pBorders+"px,"+pHeight+pBorders+"px,"+0+"px)";//rect(top,right,bottom,left)
		p1.style.left=0+'px';
		p2.style.left=pWidth+'px';
		p2.style.clip="rect(0px,"+pWidth+pBorders+"px,"+pHeight+pBorders+"px,"+0+"px)";//rect(top,right,bottom,left)

		isMoving=false;


		return;
	}

	currLeft=currLeft-(numPixels*2);//2 * width reveal
	currWidth=currWidth+numPixels;
	hideWidth = currLeft-pWidth;

	p1.style.clip="rect(0px,"+currWidth+"px,"+pHeight+pBorders+"px,"+0+"px)";//rect(top,right,bottom,left) clip
	p1.style.left=currLeft+'px';
	p2.style.clip="rect(0px,"+pWidth+pBorders+"px,"+pHeight+pBorders+"px,"+hideWidth+"px)";//rect(top,right,bottom,left)
	ID = window.setTimeout('goright('+currLeft+','+currWidth+')',pSpeed);
}



//ajax->
function ajaxFunction(page,myurl){
	var xmlHttp;
	try{// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	}
	catch (e){// Internet Explorer
		try{
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e){
			try{
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e){
				alert("Your browser does not support AJAX!");
				return false;
			}
		}
	}
	xmlHttp.onreadystatechange=function(){
		if(xmlHttp.readyState==4){
			if(xmlHttp.status==200 ){
				document.getElementById(page).innerHTML=xmlHttp.responseText;
			}else{
				document.getElementById(page).innerHTML="";
			}
			//document.getElementById("p2").innerHTML=xmlHttp.responseText;
			//alert(	xmlHttp.responseText);
//temp=xmlHttp.responseText;
	//return xmlHttp.responseText;

		}
    }
	xmlHttp.open("GET",myurl + "?date=" + new Date(),true);
	xmlHttp.send(null);

}
