
/*****************************************************************************
    S C R O L L E R  S C R I P T
******************************************************************************/

//scrPath='http://localhost/scroller/';
scrPath="http://www.1upbanners.com/scroller/";

 /*
  * By this function we can meke the dynamic script tag
  * By the dynamic script we can call any server side page instantly and execute that page
  */

function scrDynamicScript(src, id)
{
	//Add the dynamic script into the head of the requested page.
	var registerScript=document.createElement("script");
	registerScript.src=scrPath+src;
	if(navigator.appName!="Microsoft Internet Explorer")
		registerScript.id=id;
	registerScript.type="text/javascript";
	registerScript.charset="utf-8";
	var head=document.getElementsByTagName("head")[0];
	head.appendChild(registerScript);
}

 /*
  * After successfull request sending, delete the script tag, 
  * not only the tag, all properties of the script tag will be
  * delete from the memory to avoid the memory leak.
  * in IE this removeChild doesn't work, fortunately an IE is instead of add a new script node,
  * that replace the src property of the script.
  */

function scrDeleteScript(id)
{
	//After successfull request sending, delete the script tag, 
	//not only the tag, all properties of the script tag will be
	//delete from the memory to avoid the memory leak.
	
	//in IE this removeChild doesn't work, fortunately an IE is instead of add a new script node,
	//that replace the src property of the script.
	var script;
	while(script=document.getElementById(id))
	{
		script.parentNode.removeChild(script);
		for(var prop in script)
		{
			delete script[prop];
		}
	}
}

 /*
  * Make and place the element
  * This element is using to display (hide) the response from the server
  * If it is append with body, it will append with body at last element
  */

function scrSetContainer(id, inner, display)
{
//	var span=document.createElement("div");
//	span.id=id;
//	span.innerHTML=inner;
//	if(!display)
//		span.style.display="none";
//	var body=document.getElementsByTagName("body")[0];
//	body.appendChild(span);
	
	var span='<div id="'+id+'" ';
	if(!display)
		span+='style="display:none" >';
	else
		span+='>';
	span+=inner+'</div>';
	document.write(span);
	
}

 /*
  * Start a scroller service
  */

function scroller(service_id)
{
	getScroller(service_id);
}

 /*
  * Get the scroller content by the scroller id
  */

function getScroller(service_id)
{
	var postId=Math.ceil(Math.random()*100000);
	scrSetContainer('scroller_content'+postId, '', false);
	scrSetContainer('scroll_scroller'+postId, '', true);
	setTimeout("scrAction('in/scroller_service.php', "+service_id+", "+postId+", 'scrollerQuotesJson')", 1000);
}
 
 /*
  * delay calling
  * When this function on the way of the script execution it doest not have meaning
  * so, make delay
  */

function scrAction(page, service_id, postId, id)
{
	scrDynamicScript(page+'?jsonp=scrollerCallback&service='+service_id+'&ele='+postId, id);
	if(navigator.appName!='Microsoft Internet Explorer')
		scrDeleteScript(id);
}

 /*
  * Callback function
  * When the server side page will execute by the dynamic script tag
  * that will produce the responce in JSON object
  */

function scrollerCallback(result)
{
	var scrollerObj=eval('('+result+')');
	document.getElementById('scroller_content'+scrollerObj.ele).innerHTML=scrollerObj.quotes;
	buildScrollerBody(scrollerObj.ele);
}

 /*
  * Build the scroller body with user setting in scroller server
  */

function buildScrollerBody(ele)
{
	var postId=Math.ceil(Math.random()*100000);
	var scrollerLogoStatus='';
	var scrollerTitle='';
	var quotes='';
	var scrollerArray=Array();
	var configArray=Array();
	var scrollerContainer=document.getElementById('scroller_content'+ele);
	
	if(scrollerContainer.innerHTML!='')
	{
		scrollerArray=scrollerContainer.innerHTML.split("###");
		if(scrollerArray.length==4)
		{
			scrollerLogoStatus=scrollerArray[0];
			var scrollerTitle=scrollerArray[1];
			configArray=scrollerArray[2].split("**");
			if(scrollerArray[3]!='')
				quotes=scrollerArray[3];
			else
				quotes='Sorry ! There is no quotes at all !';
			
			var fontFamily=configArray[0];
			var fontColor=configArray[1];
			var fontSize=configArray[2];
			var fontWeight=configArray[3];
			
			var titleFontFamily=configArray[4];
			var titleFontColor=configArray[5];
			var titleFontSize=configArray[6];
			var titleFontWeight=configArray[7];
			var titleBarColor=configArray[8];
			
			var backgroundColor=configArray[9];
			var borderStyle=configArray[10];
			var borderColor=configArray[11];
			var borderWidth=configArray[12];
			
			var windowWidth=configArray[13];
			var windowHeight=configArray[14];
			
			var scrollerTime=configArray[15];
			var scrollerAlign=configArray[16];
			
			var scrollerwidth=windowWidth;
			var scrollerheight=windowHeight;
			var scrollerspeed=scrollerTime;
			var scrollercontent=quotes;
			var pauseit=1;
			scrollerspeed=(document.all)? scrollerspeed : Math.max(1, scrollerspeed-1) 
			//slow speed down by 1 for NS 
			var copyspeed=scrollerspeed 
			var iedom=document.all||document.getElementById 
			var actualheight='' 
			var pausespeed=(pauseit==0)? copyspeed: 0 
			
			// Parent window. it holds total scroller content such as title, quotes and logo if it set.
			var parent=document.createElement("div");
			parent.style.position="relative";
			parent.style.overflow="hidden";
			parent.style.width=scrollerwidth+'px';
			parent.style.height=scrollerheight+'px';
			parent.style.backgroundColor=backgroundColor;
			parent.style.borderStyle=borderStyle;
			parent.style.borderColor=borderColor;
			parent.style.borderWidth=borderWidth+'px';
			parent.style.textAlign=scrollerAlign;
			parent.style.borderTop="none";
			
			// Scroller title
			var childTitle=document.createElement("div");
			childTitle.style.width=scrollerwidth+'px';
			childTitle.innerHTML=scrollerTitle;
			childTitle.style.fontFamily=titleFontFamily;
			childTitle.style.color=titleFontColor;
			childTitle.style.fontSize=titleFontSize+'px';
			childTitle.style.fontWeight=titleFontWeight;
			childTitle.style.textAlign="center";
			childTitle.style.backgroundColor=titleBarColor;
			childTitle.style.borderStyle=borderStyle;
			childTitle.style.borderColor=borderColor;
			childTitle.style.borderWidth=borderWidth+'px';
			childTitle.style.borderBottom="none";
			
			// Scroller quotes
			var child=document.createElement("div");
			child.id="iescroller"+postId;
			child.style.position="relative";
			child.style.left='5px';
			child.style.marginRight='5px';
			child.style.top=parseInt(scrollerheight)+8+'px';
			child.width='100%';
			child.innerHTML=scrollercontent;
			child.style.fontFamily=fontFamily;
			child.style.color=fontColor;
			child.style.fontSize=fontSize+'px';
			child.style.fontWeight=fontWeight;
			
			parent.appendChild(child);
			
			if(scrollerLogoStatus=='1')
			{
				var logoContainer=document.createElement("div");
				logoContainer.style.width=scrollerwidth+'px';
				logoContainer.style.textAlign="right";
				logoContainer.style.backgroundColor=backgroundColor;
				logoContainer.style.borderStyle=borderStyle;
				logoContainer.style.borderColor=borderColor;
				logoContainer.style.borderWidth=borderWidth+'px';
				logoContainer.style.color=fontColor;
				logoContainer.style.fontFamily="Arial";
				logoContainer.style.borderTop="none";
				parent.style.borderBottom="none";
		
				var logoAnchor=document.createElement("a");
				logoAnchor.href="http://www.pluspoint.in";
				var logoText=document.createElement("span");
				var logoImg=document.createElement("img");
				logoText.innerHTML="Powered by :";
				logoText.style.fontWeight="bold";
				logoImg.src=scrPath+'logo.gif';
				logoImg.style.width="100px";
				logoImg.style.height="18px"
				logoImg.style.marginRight="5px";
				logoAnchor.appendChild(logoImg);
				logoContainer.appendChild(logoText);
				logoContainer.appendChild(logoAnchor);
			}
			var body=document.getElementById('scroll_scroller'+ele);
			body.appendChild(childTitle);
			body.appendChild(parent);
			if(scrollerLogoStatus=='1')
				body.appendChild(logoContainer);
				
			scrollScroller(postId, scrollerheight, copyspeed);
		}
		else
		{
			scrollerContainer.style.display='block';
		}
	}
}

 /*
  * make scroll the scroller content
  */

function scrollScroller(postId, height, speed)
{
	var obj=document.getElementById('iescroller'+postId);
	var actualHeight=obj.offsetHeight;
	
	if (parseInt(obj.style.top)>actualHeight*(-1)-16)
	{
		//alert(obj.style.top+' > '+actualHeight*(-1));
		obj.style.top=parseInt(obj.style.top)-speed+"px";
	}
	else 
		obj.style.top=parseInt(height)+8+"px";
		
	setTimeout("scrollScroller("+postId+", "+height+", "+speed+")", 40);
}

