// JavaScript Document


	window.onload = loadbody;
	var oTimeReel;
	var oDubReel;
	var oReel;
	
	function loadbody(){
		
		// change location of coriginal to cdrop if exist
		var cOrs = document.getElementsByName("coriginal");
		var cDrs = document.getElementsByName("cdrop");
		
		if (cOrs && cDrs){
			for (var i=0; i<cOrs.length; i++){
				if (cDrs[i]){
					cOrs[i].style.display = "none";
					cDrs[i].innerHTML = cOrs[i].innerHTML;
					
				}
			}
		}
		
		// load reels
		oTimeReel = document.getElementById("oTimeReel");
		oDubReel = document.getElementById("oDubReel");
		oReel = document.getElementById("oReel");
		
		//if (oTimeReel) setTimeout('fetchReel("oTimeReel",oTimeReelFile)',1000);
		//if (oDubReel) setTimeout('fetchReel("oDubReel", oDubReelFile)',2000);
		if (oTimeReel) setTimeout('fetchReel("oTimeReel", oTimeReel.getAttribute("reelFile"),"","time")',200);
		if (oDubReel) fetchReel("oDubReel", oDubReel.getAttribute("reelFile"), '', 'dub');

		
		
		initPrintbox();
		// catch event
		var printS = document.getElementById("printsetting");
		document.onclick = hideBox;
		if (printS) printS.onclick = cancelEvent;
		
		
		// fixes for old browsers
		if (String(navigator.userAgent).indexOf("MSIE 6") > -1) fixIE6();
	}
	var ajaxReel;
	
		
	function fetchReel(rId, reelFile, rank,type) {
		// display oReel first
		var orId = document.getElementById(rId);
		orId.innerHTML = oReel.getElementsByTagName("P")[0].innerHTML;
		
		
		ajaxReel = new sack(reelFile);
		if (ajaxReel.failed ){ // do something
			orId.innerHTML = oReel.getElementsByTagName("P")[1].innerHTML;
			return;
		}
		
		ajaxReel.myResponseElement = orId;
		ajaxReel.method = 'POST';
		ajaxReel.element = rId;
		ajaxReel.onLoading = function() { ajaxReel.myResponseElement.innerHTML = 'Sending Data...'; };
		ajaxReel.onLoaded = function() { ajaxReel.myResponseElement.innerHTML = 'Data Sent...'; };
		ajaxReel.onInteractive = function() { ajaxReel.myResponseElement.innerHTML = 'Processing Data...'; };
		ajaxReel.onCompletion = function() { ajaxReel.myResponseElement.innerHTML = 'Finishing...' };
		ajaxReel.runAJAX('rank='+ rank + '&type='+ type + '&rid='+ rId +'&file='+ reelFile);
		//return false;
	}

	
	
	function getResponseElement(rId) {
		var p = document.getElementById(rId);
		
		if (!p) {
			return false;
		}
		return p;
	}
	
	
	var req = new Array();
	var iCount = 0;
	
	
	function loadReel(filePath,rId, bSync){
		
		// try array elements
		for (var x = 0; x<req.length; x++){
			
			if (req[x]) iCount++;
		}
		if (window.XMLHttpRequest)
			req[iCount] = new XMLHttpRequest();
		else
			req[iCount] = new ActiveXObject("Microsoft.XMLHTTP");
		
		
		var url = filePath +"?__=" + encodeURIComponent((new Date()).getTime());
		
		req[iCount].open("GET", url, bSync);
		
		
		req[iCount].setRequestHeader("Cache-Control", "no-cache");
		req[iCount].rId = rId;
	   	req[iCount].onreadystatechange = callback;
   		req[iCount].send(null);
		

	}
	
	
	
	function callback() {
		// just go through them and handle if exists
		// problem: it may occur that some objects will still be hanging around!
		for (var x = 0; x<req.length; x++){
			
			if (req[x] && !req[x].finished){
				if (req[x].readyState == 4) {
					if (req[x].status == 200) {
						onDone(req[x].responseText,req[x].rId);
						req[x].finished = true; // so that it doesnt get called again, but i have to null it later
						// null it
						//alert(req[x].rId);
						req[x] = null;
					}
				}
				
			}else if(req[x] && req[x].finished) req[x] = null;
		}
	}
	
	function onDone(s,rId){
		// update with html content
		
		var oReel = document.getElementById(rId);
		
		if (oReel){
			
			oReel.innerHTML = s;
			
		}
		
		globalBDone = true;
		
		
	}
	
	function validateAlert(){
		//"/^([a-z0-9+_]|\\-|\\.)+@(([a-z0-9_]|\\-)+\\.)+[a-z]{2,6}\$/i";
		// dont let user submit without a name, email ,or url
		
		var oAuthor = document.getElementById("author");
		var oEmail = document.getElementById("email");
		var oComment = document.getElementById("comment");
		if (oAuthor && oEmail && oComment){
			var txMessage = "";
			if (oAuthor.value == '' || oEmail.value == ''){
				alert("If you are a blogger, you should know better than to leave your name or email empty!");
				return false;
			}
			var re = /^([a-z0-9+_]|\-|\.)+@(([a-z0-9_]|\-)+\.)+[a-z]{2,6}$/i;
			
			if (!re.test(oEmail.value)){
				alert("Your email address is incorrect!");
				return false;
			}
			if (oComment.value == ''){
				alert("What? No Comment?");
				return false;
			}
		}
		return true;
	}
	var oStyle;
	function initPrintbox(){
		if (!document.getElementsByTagName){ return; }
		var anchors = document.getElementsByTagName("a");
	
		// loop through all anchor tags
		for (var i=0; i<anchors.length; i++){
			var anchor = anchors[i];
	
			if (anchor.getAttribute("href") && (anchor.getAttribute("rel") == "printbox")){
				anchor.onclick = showBox;
			}
		}
		
		var objBody = document.getElementsByTagName("body").item(0);
		
		// get a stylesheet with print media for reference (in order to append to the rules)
		
		for (i=0; i<document.styleSheets.length; i++){
			if (document.styleSheets[i].media == 'print'){
				oStyle = document.styleSheets[i];
				break;
			}
			if (document.styleSheets[i].media.item){
				if (document.styleSheets[i].media.item(0) == 'print'){
					oStyle = document.styleSheets[i];
					break;
				}
			}
			
		}
		
		// if it doesnt exist, create a style tag
		if (!oStyle){
			var oCss= document.createElement("style");
			oCss.setAttribute("media","print");
			oCss.setAttribute("type","text/css");
			var objHead = document.getElementsByTagName("head").item(0);
			objHead.insertBefore(oCss,objHead.firstChild);
			// now its the first stylesheet
			oStyle = document.styleSheets[0];
		}
				
		/*
		<div id="overlay">&nbsp;</div>
		<div id="printsetting">
		<a href="javascript:SetPrint('rPrint')">[PRINT]</a>
		<input type="checkbox" name="rPrint" value="0" id="rPrint" checked="checked" /><label for="rPrint">BLOCK IMAGES</label>
		
		</div>
		*/
		
	
		var objOverlay = document.createElement("div");
		objOverlay.setAttribute('id','overlay');
		//objOverlay.onclick = function () {hideLightbox(); return false;}
		objOverlay.style.display = 'none';
		objOverlay.style.position = 'absolute';
		objOverlay.style.top = '0';
		objOverlay.style.left = '0';
		objOverlay.style.zIndex = '90';
		objOverlay.style.width = '100%';
		objBody.insertBefore(objOverlay, objBody.firstChild);
	
		var objPrintbox = document.createElement("div");
		objPrintbox.setAttribute('id','printsetting');
		objPrintbox.style.display = 'none';
		objPrintbox.style.position = 'absolute';
		objPrintbox.style.zIndex = '100';	
		objBody.insertBefore(objPrintbox, objOverlay.nextSibling);
		
		objPrintbox.innerHTML = '<a href="javascript:SetPrint(\'rPrint\',0)">[PRINT]</a><input type="checkbox" name="rPrint" value="0" id="rPrint" checked="checked" /><label for="rPrint">BLOCK IMAGES</label>';
		
	}
	function showBox(ev){
		// ask for printing images or not, ask for printing in black and white
		// todo: save in cookies and retrieve defaults
		
		cancelEvent(ev);
		var printS = document.getElementById("printsetting");
		var oOverlay = document.getElementById("overlay");
		
		if (printS){
			// set location to middle of viewed screen
			printS.style.display = "none";
			
			
			var iInnerHeight = 0;
			var iScrollTop = 0;
			var iPageHeight = 0;
			
			iInnerHeight = document.documentElement.clientHeight;
			iScrollTop = document.documentElement.scrollTop;
			iPageHeight = document.documentElement.scrollHeight;
			
			//iInnerHeight  = window.innerHeight;
			//iScrollTop = window.scrollY;

			printS.style.display = "block";
			var iTop = parseInt(iInnerHeight /2) + iScrollTop - (parseInt(printS.offsetHeight) / 2);
			var iLeft = parseInt(screen.availWidth / 2) - (parseInt(printS.offsetWidth) / 2);
			
			printS.style.top = iTop +"px";
			printS.style.left = iLeft + "px";
		
			// show ovelay
			if (oOverlay){
				oOverlay.style.height = iPageHeight + "px";
				oOverlay.style.display = "block";
			}
		}
	}
	function hideBox(noRestore){
		var printS = document.getElementById("printsetting");
		var oOverlay = document.getElementById("overlay");	
		printS.style.display = "none";
		oOverlay.style.display = "none";

		// restore print rules
		if (!noRestore) SetPrint('',1);
	}
	function cancelEvent(ev){
		
		if (window.event) event.cancelBubble = true;
		else ev.cancelBubble = true;
		
		
	}
	
	function SetPrint(rId,ind){
		// set the stylesheet to hide or show images, colored or non colored
		// ind = 0 , set print to display or hide images
		// ind = 1 , reset by removing rule
		
		//var ssPrint = document.styleSheets[2];
		var ssPrint = oStyle;
		
		if (ssPrint){
			var rules;
			var re = /display\: none/i;
			var bIe = false;
			
			if (ssPrint.rules){
				bIe = true;
				rules = ssPrint.rules;			
			}else{
				rules = ssPrint.cssRules;
			}
			
			var i;
			// get the checkbox for image blocking
			var rO = document.getElementById(rId);
			//span rules upwards, more chances of finding the img faster this way
			for (i=rules.length-1; i>=0; i--){
				if (String(rules[i].selectorText).toLowerCase() == "img" && re.test(rules[i].style.cssText)){
					break;
				}
				
			}
			
			// also if i > -1 then it is found
			if ((i>-1 && ind==0 && !rO.checked) || (i>-1 && ind == 1)){
				// it is found, and i want to remove
				//alert("found, and removing");
				if (bIe)	ssPrint.removeRule(i);
				else 		ssPrint.deleteRule(i);
				
			}
			
			if (i==-1 && ind == 0 && rO.checked ){
				//alert("not found, and adding");
				// if not found and im tryting to add
				if (bIe)	ssPrint.addRule("img","display: none;", rules.length);
				else 		ssPrint.insertRule("img {display: none;}",rules.length);
				
			}
			
		}
		if (ind == 0){
			hideBox(true);
			window.print();
			setTimeout("SetPrint('',1)",1000); // firefox seems to reset way too soon!
		}
		
	}

	
	function fixIE6(){
		// fix for IE 6 the divisions in the CSS file that has certain attributes
		var elems = document.getElementsByTagName("DIV");
		
		var pageAtt;
		for (var x=0; x< elems.length; x++){
			pageAtt = elems[x].attributes.getNamedItem("onpage");
			if (pageAtt.value) elems[x].className += " "+ pageAtt.value;
			
		}
		
		
		elems = document.getElementsByTagName("INPUT");
		for (x=0 ; x<elems.length; x++){
			pageAtt = elems[x].attributes.getNamedItem("type");

			if (pageAtt.value == "text" || pageAtt.value == "submit") elems[x].className += " "+ pageAtt.value;
		}
		
	}
	function callAyyash(){
		var re = /<br>/gim;
		// just popup to screen the inner html of ayyash
		var aO = document.getElementById("ayyash");
		var tO = document.getElementById("ayyash2");
		var cDr = document.getElementById("cdrop");
		if (aO && tO && cDr){
			// empty cdrop
			cDr.innerHTML = "";
			newStr = String(aO.innerHTML).replace(re,"<br />");
			//newStr = "<br>","</ br>");
			tO.value = newStr;
			loadbody();
		}
		//window.clipboardData.setData("Text",aO.innerHTML);
		
	}
	
	
	function addLoadEvent(func)
	{	
		var oldonload = window.onload;
		if (typeof window.onload != 'function'){
			window.onload = func;
		} else {
			window.onload = function(){
			oldonload();
			func();
			}
		}
	
	}
	