///////////////////////////////////////////////////////////////////////////////////////
// This Script is Designed and Powered for ECOMOST project by alireza hatami © 2006  //
// develeoped for persigulf.com by Alireza Hatami @ 2009							 //
///////////////////////////////////////////////////////////////////////////////////////

var timeoutDelay = 10000; // Refresh Delay
var webServiceURL = "NewsReader.php"; // Web Service File's URL
var chSectionID = "sandbox"; // the changable Section's ID
var theAnchorID = "ticketAnchor";
var theCharacterTimeout = 50;
var theWidgetOne        = "";
var theWidgetTwo        = "|";
var theWidgetNone       = "";
var theLeadString       = "";
var theSummaries = new String();
var theSiteLinks = new String();
var theItemCount = 1;
var blinked = false;
var cursorOn = false;
var isTyping = false;
var blinkingTimeout = 500;

function $(id){ return document.getElementById(id); }
function createXMLHttpRequest( ) {
      try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) {}
      try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) {}
      try { return new XMLHttpRequest( ); } catch(e) {}
      alert("XMLHttpRequest not supported");
      return null;
}

function xhReqfunc(){
	if(isTyping){ return false; }
	xhReq = createXMLHttpRequest();
    xhReq.onreadystatechange = function( ) {
      if (xhReq.readyState==4) { 
        if (xhReq.status==200) {
			if(xhReq.responseText != '' && xhReq.responseText != ' '){
				startTicker(xhReq.responseText);
			}else{ return false; }
        } else {

        }
      }
    }
	var myDate= new Date();
	var dateStr= myDate.getDate()+'i'+myDate.getMonth()+'i'+myDate.getFullYear()+'i'+myDate.getHours()+'i'+myDate.getMinutes()+'i'+myDate.getSeconds();
	try{
	xhReq.open("GET",webServiceURL+"?"+dateStr,true);
	xhReq.send(null); } catch(e) {}

}

function parseAString(RCVDText){
	sharpPos=RCVDText.indexOf('####');
	var articleTitle=RCVDText.substring(0,sharpPos);
	var articleURL=RCVDText.substring(sharpPos+4,RCVDText.length);
	var theArticle = new Array();
	theArticle[0] = articleTitle;
	theArticle[1] = articleURL;
	return theArticle;
}


function startTicker(RCVDText)
{

	theCurrentStory     = -1;
	theCurrentLength    = 0;

	if (document.getElementById) {	
			isTyping = true;
		    theAnchorObject = $(theAnchorID);
			runTheTicker(RCVDText);   	
		 }
	else {
            return true;
	}
}
function runTheTicker(RCVDText)
{
	var myTimeout;  
	if(theCurrentLength == 0)
	{
		if(blinked) { clearTimeout(cursorTimeout); }
		theSummaries = parseAString(RCVDText)[0];
		theTargetLink = parseAString(RCVDText)[1];
		theCurrentStory      = theCurrentStory % theItemCount;
		theStorySummary      = theSummaries.replace(/&quot;/g,'"');		
		theAnchorObject.href = theTargetLink;
		thePrefix 	     = "<span class=\"tickls\">" + theLeadString + "</span>";
		theCursor = "<span style=\"visibility:hidden;\" id=\"theCursor\">" + theWidgetTwo + "</span>";
	}

	theAnchorObject.innerHTML = thePrefix + 
	theStorySummary.substring(0,theCurrentLength) + whatWidget() + theCursor;

if(theCurrentLength != theStorySummary.length)
	{
		theCurrentLength++;
		myTimeout = theCharacterTimeout;
		setTimeout("runTheTicker()", myTimeout);
	}else{
		isTyping = false;
		theBlinker();
	}
}
function theBlinker() {
	if(cursorOn) { 
		$("theCursor").style.visibility = "hidden";
		cursorOn = false;
	}else{
		$("theCursor").style.visibility = "visible";
		cursorOn = true;
	}
	cursorTimeout = setTimeout(theBlinker, blinkingTimeout);
	blinked = true;
}

function whatWidget()
{
	if(theCurrentLength == theStorySummary.length)
	{
		return theWidgetNone;
	}

	if((theCurrentLength % 2) == 1)
	{
		return theWidgetOne;
	}
	else
	{
		return theWidgetTwo;
	}
}

////////////////////////////////
// BreakNews
////////////////////////////////
function getxmlhttp(){
	var xmlHttp = false;
	if (window.XMLHttpRequest){
		var xmlHttp = new XMLHttpRequest();
	}else{
		if (window.ActiveXObject){
			var xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	return xmlHttp;
}

function process_ajax(){
	
	phpPage = 'BreakNews.php';
	objID = 'BreakSection';
	txtID = 'BreakLink';
	
	xmlhttp = getxmlhttp();
	var obj = document.getElementById(objID);
	var txt = document.getElementById(txtID);
	xmlhttp.open("GET",phpPage);
	xmlhttp.onreadystatechange = function(){
		if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
			if(xmlhttp.responseText != 0) {
				if(xmlhttp.responseText == 'expired') {
					obj.style.display = "none";
				}
				else {
					obj.style.display = "inline-table";
					txt.innerHTML = xmlhttp.responseText;
				}
			}
		}
	}
	xmlhttp.send(null);
}
////////////////////////////////
// Update Indicator
////////////////////////////////
function process_ajax_updateIndicator_News(){
	
	phpPage = 'UpdateIndicator_news_check.php';
	xmlhttp = getxmlhttp();
	xmlhttp.open("GET",phpPage);
	xmlhttp.onreadystatechange = function(){
		if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
			if(xmlhttp.responseText == '1') {
				document.getElementById('NewsIndUp').src = 'IMG/Head/new.gif';
			}
			else {
				document.getElementById('NewsIndUp').src = 'IMG/Head/old.gif';
			}
		}
	}
	xmlhttp.send(null);
}
function process_ajax_updateIndicator_Article(){
	
	phpPage = 'UpdateIndicator_Article_check.php';
	xmlhttp = getxmlhttp();
	xmlhttp.open("GET",phpPage);
	xmlhttp.onreadystatechange = function(){
		if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
			if(xmlhttp.responseText == '1') {
				document.getElementById('ArticleIndUp').src = 'IMG/Head/new.gif';
			}
			else {
				document.getElementById('ArticleIndUp').src = 'IMG/Head/old.gif';
			}
		}
	}
	xmlhttp.send(null);
}
function process_ajax_updateIndicator_PhotoReport(){
	
	phpPage = 'UpdateIndicator_phr_check.php';
	xmlhttp = getxmlhttp();
	xmlhttp.open("GET",phpPage);
	xmlhttp.onreadystatechange = function(){
		if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
			if(xmlhttp.responseText == '1') {
				document.getElementById('PhotoReportIndUp').src = 'IMG/Head/new.gif';
			}
			else {
				document.getElementById('PhotoReportIndUp').src = 'IMG/Head/old.gif';
			}
		}
	}
	xmlhttp.send(null);
}
////////////////////////////////

window.onload = function(){
	setInterval(xhReqfunc,timeoutDelay);
	setInterval(process_ajax,20000);
	setInterval(process_ajax_updateIndicator_News, 22000);
	setInterval(process_ajax_updateIndicator_Article, 25000);
	setInterval(process_ajax_updateIndicator_PhotoReport, 28000);

}

window.onresize = function() {
	reSetPos('FastList_Dp', 'FastList');
	reSetPos_CustumCX('TagsList_Dp', 'TagsList', 60,0);
}
