/*Copyright Trinity Innovations LTD 2011. All rights reserved*/

//globals advanced------------------->
var DEBUG = false;
var LOADPAGES_TIMER = -1;
var pWidth;
var pHeight;
var isMoving = false;
var isHidden = false, AF;
var hideDiv, revealDiv, revealHtmlDiv, hideHtmlDiv, hasPlayed = false, pSound, tempSound, soundManager;
var myBook, myBinder, counter = 0;
var numPixels = 20; // number of pixels to slide per move
var allowNavigation = false;
var tempPixelsToMove, tempSpeed;
var allowPageClick = false;
var doIncrementalAutoFlip = false;
var selectNavigation=false; 
// <----------------end globals


function ini() {
	
	if (pSound) {
		flipSoundIni();
	}
	
	if (!document.getElementById("myBook")) {
		return;
	}
	
	allowNavigation = selectNavigation;
	/* numPixels=numPixelsToMove; */
	numPixels = 100;
	/*
	 * for 100 its going fine for 10 its lazy
	 */

	if (numPixels > 100) {
		numPixels = 100;
	}
	

	pWidth = myPageW_L + (pageBorderWidth * 2);/*externs*/
	pHeight = myPageH_L + (pageBorderWidth * 2);
	myBook = document.getElementById("myBook");
	myBook.style.width = (pWidth * 2) + 'px';
	myBook.style.height = pHeight + 'px';
	myBook.style.position = "relative";
	myBook.style.zIndex = "0";
	// alert(myBook.childNodes.length);

	for ( var i = 0; i < myBook.childNodes.length; i++) {
		if (myBook.childNodes[i].nodeType == 1) { // element nodes only
			var original = myBook.childNodes[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.backgroundColor = "#f1f1f1";
			myDiv.style.overflow = "hidden";
			// myDiv.style.zIndex=myBook.childNodes.length-counter;
			myDiv.style.zIndex = totalPageCount; // extern
			myDiv.setAttribute("id", "flipPage" + counter);

			original.parentNode.replaceChild(myDiv, original);
			original.setAttribute("id", "flipHtml" + counter);
			original.style.position = "absolute";
			original.style.width = myPageW_L + "px";
			original.style.height = myPageH_L + "px";
			original.style.top = "0px";
			original.style.left = "0px";
			original.style.overflow = "hidden";
			//original.style.borderWidth = pageBorderWidth + "px";
			//original.style.borderColor = pageBorderColor;
			//original.style.borderStyle = pageBorderStyle;
			// original.style.backgroundImage=""; //debug

			if (!number_check(counter)) {
				if (document.all) {
					original.style.filter = "progid:DXImageTransform.Microsoft.Alpha(Opacity=100, FinishOpacity=30, Style=1, StartX=80, FinishX=100, StartY=0, FinishY=0)";
				} else {
					var myPngDiv = document.createElement('div');
					myPngDiv.style.position = "absolute";
					myPngDiv.style.left = (pWidth - 80) + 'px';
					myPngDiv.style.top = "0px";
					myPngDiv.style.width = 80 + "px";
					myPngDiv.style.height = pHeight + "px";
					/*myPngDiv.style.background = "transparent url(images/black_gradient.png) top right repeat-y";*/
					myPngDiv.style.opacity = "0.7";
					myPngDiv.style.filter = "alpha(opacity=30)";
					original.appendChild(myPngDiv);
				}
			}
			
			myDiv.appendChild(original);

			if (allowNavigation && document.getElementById("flipSelect")) {
				// name pages for navigation
				var y = document.createElement('option');
				if (counter == 0) {
					// alert(original.getAttribute("name"));
					if (original.getAttribute("name")) {
						y.text = original.getAttribute("name");
					} else {
						y.text = "Cover ";
					}
				} else {
					if (original.getAttribute("name")) {
						y.text = original.getAttribute("name");
					} else {
						y.text = counter;
					}
				}
				y.value = counter;

				// y.selected="selected";
				var x = document.getElementById("flipSelect");
				x.style.display = "";
				x.onchange = function() {
					this.blur();
					document.body.focus();
					autoFlip(this.value);
				};
				try {
					x.add(y, null); // standards compliant
				} catch (ex) {
					x.add(y); // IE only
				}
			}
			counter++;
		}
	}	
	// external call to cc_functions	
	_loadPages(IMAGE_PRELOAD_L_COUNT);/*extern*/	
	myBook.style.display = ""; // all setup, display the book
}

function number_check(value) {
	// returns true if value is even, false if value is odd
	return (1 - (value % 2))
}

function autoFlip(topage) {
	currentLeft = leftStart - 1;
	if (topage > (currentLeft + 1)) {
		cycles = topage - currentLeft;
		if (number_check(cycles)) {
			cycles = cycles / 2;
		} else {
			cycles = (cycles - 1) / 2;
		}

		if (doIncrementalAutoFlip) {
			tempSound = pSound;
			pSound = false;
			tempPixelsToMove = numPixels;
			tempSpeed = pSpeed;
			pSpeed = 0;
			numPixels = (pWidth / 4);
			doAutoFlip(cycles, true);
		} else {
			rightDirectFlip(cycles);
		}
	} else if (topage == currentLeft || topage == currentLeft + 1) {
		return false;
	} else {
		cycles = currentLeft - topage;
		if (number_check(cycles)) {
			cycles = cycles / 2;
		} else {
			cycles = (cycles + 1) / 2;
		}
		if (doIncrementalAutoFlip) {
			tempSound = pSound;
			pSound = false;
			tempPixelsToMove = numPixels;
			tempSpeed = pSpeed;
			pSpeed = 0;
			numPixels = (pWidth / 4);
			doAutoFlip(cycles, false);
		} else {
			leftDirectFlip(cycles);
		}
	}
}

function doAutoFlip(num, dir) {
	if (num == 0) {
		AF = window.clearTimeout(AF);
		numPixels = tempPixelsToMove;
		pSpeed = tempSpeed;
		pSound = tempSound;
		return;
	}
	;
	if (dir) {
		gonext();
	} else {
		goprev();
	}
	num--;
	AF = window.setTimeout('doAutoFlip(' + num + ',' + dir + ')', 100);
}

function rightDirectFlip(num) {
	for ( var i = 0; i < num; i++) {
		if (!document.getElementById("flipPage" + rightStart)) {
			document.getElementById("gotoFlip").value = num;
			return;
		}
		;

		hideDiv = document.getElementById("flipPage" + leftStart);
		hideHtmlDiv = document.getElementById("flipHtml" + leftStart);

		revealDiv = document.getElementById("flipPage" + rightStart);
		revealHtmlDiv = document.getElementById("flipHtml" + rightStart);

		revealDiv.style.left = 0 + 'px';
		revealDiv.style.width = pWidth + 'px';
		revealDiv.style.zIndex = 0;

		hideDiv.style.left = "";
		hideDiv.style.right = "0px";
		hideDiv.style.width = "0px";

		hideHtmlDiv.style.left = "";
		hideHtmlDiv.style.right = "0px";

		revealHtmlDiv.style.left = "";
		revealHtmlDiv.style.right = "0px";

		leftStart = leftStart + 2;
		rightStart = rightStart + 2;

	}
	$("#ff_panel").hide();
}

function leftDirectFlip(num) {
	
	for ( var i = 0; i < num; i++) {

		if (!document.getElementById("flipPage" + (leftStart - 2))) {
			document.getElementById("gotoFlip").value = '1';
			return false;
		}
		

		leftStart = leftStart - 2;
		rightStart = rightStart - 2;

		hideDiv = document.getElementById("flipPage" + rightStart);
		hideHtmlDiv = document.getElementById("flipHtml" + rightStart);

		revealDiv = document.getElementById("flipPage" + leftStart);
		revealHtmlDiv = document.getElementById("flipHtml" + leftStart);

		hideDiv.style.left = "";
		hideDiv.style.right = pWidth + "px";
		hideDiv.style.width = 0 + 'px';

		revealDiv.style.left = pWidth + "px";
		revealDiv.style.width = pWidth + "px";

		revealHtmlDiv.style.left = "0px";
		hideHtmlDiv.style.left = "0px";

	}
}

function gonext() {
	
	//window.clearTimeout(LOADPAGES_TIMER);
	//setCurPage(Number(getCurPage()));

	if (isMoving || rightStart >= totalPageCount) {
		return;
	}

	
	isMoving = true;
	hideDiv = document.getElementById("flipPage" + leftStart);
	hideHtmlDiv = document.getElementById("flipHtml" + leftStart);

	revealDiv = document.getElementById("flipPage" + rightStart);
	revealHtmlDiv = document.getElementById("flipHtml" + rightStart);

	revealDiv.style.width = "0px";
	revealDiv.style.left = pWidth * 2 + 'px';
	revealDiv.style.zIndex = 99;

	document.getElementById("gotoFlip").value = rightStart + 2 >= totalPageCount ? totalPageCount : rightStart + 2;

	goright(pWidth * 2, 0);	

}

function goright(currLeft, currWidth) {	

	if (currLeft <= numPixels * 2) {

		window.clearTimeout(ID);
		revealDiv.style.left = 0 + 'px';
		revealDiv.style.width = pWidth + 'px';
		revealDiv.style.zIndex = revealDiv.style.zIndex - 99;

		hideHtmlDiv.style.left = "";
		hideHtmlDiv.style.right = "0px";

		revealHtmlDiv.style.left = "";
		revealHtmlDiv.style.right = "0px";

		leftStart = leftStart + 2;
		rightStart = rightStart + 2;

		isHidden = false;
		isMoving = false;
		
		if (allowNavigation && document.getElementById("flipSelect")) { // update
																		// page
																		// select
			document.getElementById("flipSelect").selectedIndex = leftStart - 1;
		}
		hasPlayed = false;
		_loadPages(); // pre-load some pages in the bg
		activateMap();		
		return;
	}

	currLeft = currLeft - (numPixels * 2);// 2 * width reveal
	currWidth = currWidth + numPixels;

	revealDiv.style.left = currLeft + 'px';
	revealDiv.style.width = currWidth + 'px';

	hideWidth = currLeft - pWidth;
	if (hideWidth > 0) {
		hideDiv.style.width = hideWidth + 'px';
	} else {
		if (!isHidden) {
			hideDiv.style.width = 0 + 'px';
			isHidden = true;
		}
	}
	if (currLeft < (pWidth /*+ binderWidth */+ numPixels) && !hasPlayed) {		
		if (pSound) {
			soundManager.play('flip');
		}
		hasPlayed = true;		
	}
	// ID = window.setTimeout('goright('+currLeft+','+currWidth+')',pSpeed);
	ID = window.setTimeout(goright, pSpeed, currLeft, currWidth);
	/*if (DEBUG)
		console.log("Timeout Set:" + ID + ", for " + pSpeed / 1000	+ " Seconds");*/

}

function goprev() {
	
	setCurPage((getCurPage()-2));
	
	if (isMoving) {
		if (DEBUG)
			console.log("GoPrev: Return Moving")
		return;
	}
	;
	if (!document.getElementById("flipPage" + (leftStart - 2))) {
		return false;
	}
	isMoving = true;

	leftStart = leftStart - 2;
	rightStart = rightStart - 2;

	hideDiv = document.getElementById("flipPage" + rightStart);
	hideHtmlDiv = document.getElementById("flipHtml" + rightStart);

	revealDiv = document.getElementById("flipPage" + leftStart);
	revealHtmlDiv = document.getElementById("flipHtml" + leftStart);

	revealDiv.style.left = "0px";
	revealDiv.style.width = "0px";

	hideDiv.style.left = "";
	hideDiv.style.right = pWidth + "px";

	document.getElementById("gotoFlip").value = leftStart + 1;

	goleft(0, 0);
	
}

function goleft(currLeft, currWidth) {

	if (currLeft >= (pWidth - (numPixels * 2))) {

		window.clearTimeout(ID);

		revealDiv.style.left = pWidth + "px";
		revealDiv.style.width = pWidth + "px";

		revealHtmlDiv.style.left = "0px";
		hideHtmlDiv.style.left = "0px";

		isHidden = false;
		isMoving = false;
		

		if (allowNavigation && document.getElementById("flipSelect")) { // update
																		// page
																		// select
			if (leftStart - 1 > 0) {
				document.getElementById("flipSelect").selectedIndex = leftStart - 1;
			} else {
				document.getElementById("flipSelect").selectedIndex = leftStart;
			}
		}

		hasPlayed = false;
		activateMap();		
		return;
	}

	currLeft = currLeft + numPixels;// 1 * width reveal
	currWidth = currWidth + numPixels;

	revealDiv.style.width = currWidth + 'px';
	revealDiv.style.left = currLeft + 'px';

	hideWidth = pWidth - currWidth;

	if (hideWidth > (pWidth / 2)) {
		hideDiv.style.width = hideWidth - numPixels + 'px';
	} else {
		if (!isHidden) {
			hideDiv.style.width = 0 + 'px';
			isHidden = true;
		}
	}

	if (currLeft > ((pWidth / 2) - numPixels) && !hasPlayed) {
		
		if (pSound) {
			soundManager.play('flip');
		}
		hasPlayed = true;
		
	}
	// ID = window.setTimeout('goleft('+currLeft+','+currWidth+')',pSpeed);
	ID = window.setTimeout(goleft, pSpeed, currLeft, currWidth);
}

function whichElement(e, dir) {
	var targ;
	if (!e) {
		var e = window.event;
	}

	if (e.target) {
		targ = e.target;
	} else if (e.srcElement) {
		targ = e.srcElement;
	}

	if (targ.nodeType == 3) { // defeat Safari bug
		targ = targ.parentNode;
	}

	// var tname;
	pname = targ.parentNode.tagName;

	tname = targ.tagName;
	if (tname == "A" || tname == "INPUT" || pname == "A" || pname == "INPUT") {
		// alert("You clicked on a " + tname + " element. pname:"+pname);
		return false;
	} else {
		// alert("You clicked on a " + tname + " element. pname:"+pname);
		if (dir) {
			return gonext();
		} else {
			return goprev();
		}
	}
}


function toggleSound() {
	pSound = (pSound != false ? false : true);
}

function goTo(pageNo) {
	/* Is Page Loaded */
	if(pageNo > totalPageCount) return;
	
	if($('#ff_panel').css('visibility') != 'visibile'){
		$('#imgLoadScreen').show();
	}
	if (getPageLoadedCount() < pageNo) {
		if(DEBUG)
			console.log("Load Pages On timer:"+ getPageLoadedCount() +"<"+ pageNo);
		_loadPages( Number( Number(pageNo) - Number(getPageLoadedCount())) );
		window.setTimeout(goTo, 200 , pageNo); //no? set timeout and comeback while we load some more pages
		return;
	}
	if(document.getElementById("gotoFlip"))
		document.getElementById("gotoFlip").value = pageNo;
	pageNo--;
	
	autoFlip(pageNo);
	$('#imgLoadScreen').hide();	
	
}

function gotoFlip(e) {
	if (e.which == '13') {
		var gotoFlip = document.getElementById("gotoFlip");
		var FlipValue = (gotoFlip.value) - 1;
		autoFlip(FlipValue);
		return true;
	}
}

function isValidate() {
	var email = /^[a-zA-Z0-9_\.\-]+\@([a-zA-Z0-9\-]+\.)+[a-zA-Z0-9]{2,4}$/;
	var name = /^[a-zA-Z\ \']+$/;
	var urName = document.getElementById('txtUrName');
	var frndName = document.getElementById('txtFrndName');
	var urEmail = document.getElementById('txtUrEmail');
	var frndEmail = document.getElementById('txtFrndEmail');
	var message = document.getElementById('message');
	if (urName.value == '') {
		$('#nmEmpty').show();
		urName.focus();
		return false;
	} else if (!urName.value.match(name)) {
		$('#nmEmpty').hide();
		$('#nmInvalid').show();
		urName.focus();
		return false;
	} else if (urEmail.value == '') {
		$('#nmEmpty').hide();
		$('#nmInvalid').hide();
		$('#urEmailEmpty').show();
		urEmail.focus();
		return false;
	} else if (!urEmail.value.match(email)) {
		$('#urEmailEmpty').hide();
		$('#nmInvalid').hide();
		$('#urEmailInvalid').show();
		urEmail.focus();
		return false;
	} else if (frndName.value == '') {
		$('#urEmailEmpty').hide();
		$('#urEmailInvalid').hide();
		$('#frndNameEmpty').show();
		frndName.focus();
		return false;
	} else if (!frndName.value.match(name)) {
		$('#frndNameEmpty').hide();
		$('#urEmailInvalid').hide();
		$('#frndNameInvalid').show();
		frndName.focus();
		return false;
	} else if (frndEmail.value == '') {
		$('#frndNameEmpty').hide();
		$('#frndNameInvalid').hide();
		$('#frndEmailEmpty').show();
		frndEmail.focus();
		return false;
	} else if (!frndEmail.value.match(email)) {
		$('#frndNameInvalid').hide();
		$('#frndEmailEmpty').hide();
		$('#frndEmailInvalid').show();
		frndEmail.focus();
		return false;
	} else if (message.value == '' || message.value != '') {
		$('#frndEmailInvalid').hide();
	} else {
		return true;
	}
}

function tb_remove() {
	$("#TB_imageOff").unbind("click");
	$("#TB_closeWindowButton").unbind("click");
	$("#TB_window").fadeOut(
			"fast",
			function() {
				$('#TB_window,#TB_overlay,#TB_HideSelect').trigger("unload")
						.unbind().remove();
			});

	$("#TB_load").remove();
	if (typeof document.body.style.maxHeight == "undefined") {// if IE 6
		$("body", "html").css( {
			height :"auto",
			width :"auto"
		});
		$("html").css("overflow", "");
	}
	document.onkeydown = "";
	document.onkeyup = "";
	return false;
}

// For Zooming Effect Of Images

var zoomfactor = 0.05
// Enter factor (0.05=5%)

function zoomhelper(what) {
	if (parseInt($("#" + what).attr('width')) > 400
			&& parseInt($("#" + what).attr('height')) > 570) {
		$("#" + what).attr(
				'width',
				parseInt($("#" + what).attr('width'))
						+ parseInt($("#" + what).attr('width')) * zoomfactor
						* prefix);

		$("#" + what).attr(
				'height',
				parseInt($("#" + what).attr('height'))
						+ parseInt($("#" + what).attr('height')) * zoomfactor
						* prefix);

		if (parseInt($("#" + what).attr('width')) > 451) {
			// alert("width= "+parseInt(whatcache.style.width))
			clearzoom()
		}

	}
}

function zoom(originalW, originalH, what, state) {

	if (!document.all && !document.getElementById)
		return

		alert("shrikant-->" + $("#" + what).attr('height'));

	prefix = (state == "in") ? 1 : -1

	if ($("#" + what).attr('width') == "" || state == "restore") {

		$("#" + what).attr('width', originalW + "px")

		$("#" + what).attr('height', originalH + "px")

		if (state == "restore")
			return
	} else {
		zoomhelper(what)
	}

	beginzoom = setInterval("zoomhelper()", 100)
}

function clearzoom() {

	if (window.beginzoom)
		clearInterval(beginzoom)
}

function zoomin(kk) {

	// alert(kk);
	// $("#"+kk).attr("src","large/"+kk+".jpg");
	$("#" + kk).attr("width", "1240");
	$("#" + kk).attr("height", "1754");
}