// support for insertAdjacentHTML,insertAdjacentText methods for non IE browsers
if (typeof HTMLElement!="undefined" && !
HTMLElement.prototype.insertAdjacentElement){
	HTMLElement.prototype.insertAdjacentElement = function
(where,parsedNode)
	{
		switch (where){
		case 'beforeBegin':
			this.parentNode.insertBefore(parsedNode,this)
			break;
		case 'afterBegin':
			this.insertBefore(parsedNode,this.firstChild);
			break;
		case 'beforeEnd':
			this.appendChild(parsedNode);
			break;
		case 'afterEnd':
			if (this.nextSibling) 
this.parentNode.insertBefore(parsedNode,this.nextSibling);
			else this.parentNode.appendChild(parsedNode);
			break;
		}
	}

	HTMLElement.prototype.insertAdjacentHTML = function
(where,htmlStr)
	{
		var r = this.ownerDocument.createRange();
		r.setStartBefore(this);
		var parsedHTML = r.createContextualFragment(htmlStr);
		this.insertAdjacentElement(where,parsedHTML)
	}


	HTMLElement.prototype.insertAdjacentText = function
(where,txtStr)
	{
		var parsedText = document.createTextNode(txtStr)
		this.insertAdjacentElement(where,parsedText)
	}
}
	var indeks;
	var operation;
	var url;
	var xmlhttp;
	var rotation_time=3000;
	var header_ids ;
	var timeout;
	var iframe;
	operation=4;   //type of operation
	//1- random 2-back 3-forward 4-init
	indeks=0;
	url = "ajax.php?init"; // The server-side script
	header_ids = new Array(); //array store article ids
//starting script. 
	function show_articles() {
		getHTTPObject(operation);
	}
//handling  response from server side 
	function handleHttpResponse(htmlresponse) {
	    if ((xmlhttp!=null && xmlhttp.readyState == 4) || (htmlresponse!=null)) {    //request completed
	  		var xmlDocument;
			if (htmlresponse!=null)
				{
						//handling html response, Opera
			/*		var elem=document.createElement('xml');
					var text=document.createTextNode(htmlresponse);
					elem.appendChild(htmlresponse.innerHTML);
					alert(this.iframe.childNodes.length);
					xmlDocument=elem;
			*/		
				}
			else{
					
					xmlDocument= xmlhttp.responseXML;     //handling XML response 
					
 			}	
	
			var type=xmlDocument.getElementsByTagName('type').item(0).firstChild.data;
			
		if (type=='init')
		   {
		  	   rotation_time = xmlDocument.getElementsByTagName('rotation_time').item(0).firstChild.data;
		       var elem = xmlDocument.getElementsByTagName('header'); 
		       for (i = 0; i < elem.length; i++){
			   header_ids[i]=elem[i].firstChild.data;
		       }
		       operation=1;
		       url = "ajax.php?id=";
		       timeout=setTimeout('getHTTPObject(operation);', rotation_time);
		   }
	    if (type=='article')
		  {	 
		        document.getElementById('headline').removeChild(document.getElementById('headline').firstChild);
				document.getElementById('headline').removeChild(document.getElementById('headline').firstChild);
				document.getElementById('headline').insertAdjacentHTML("afterBegin",xmlDocument.getElementsByTagName('tekst').item(0).firstChild.data);
					;
	   	  }
      
    }
	}

//removing the timer
   function clearTimer(){
	   if (timeout)
	   {
				clearTimeout(timeout);
	   }
   }
//play/oause or back or forward button
   function change_headline(param){
		if (param==1)      //user press play/pause button 
		{
			operation=(operation != (1)) ? (1):(-1);
 	      if (operation==-1)
		  {
            clearTimer();  
		    document.getElementById('pause').src="img/play.gif";
		  } else {
		    document.getElementById('pause').src="img/pause.gif";
		  }
		  getHTTPObject(operation);
		}else{			   //uses press back or forward button
		if (operation==1)  //play mode 
			{
				clearTimer(); 
				getHTTPObject(param);
				timeout=setTimeout('getHTTPObject(1);', rotation_time);
			}else {			// pause mode
				operation=param;
				getHTTPObject(operation);
			}
		}
		
   }

    function getHTTPObject(operation_type) {
	 var temp_url=url;;
	    if (operation<0)	{return false;}
        if (window.XMLHttpRequest ) { // Mozilla, Safari,...
              xmlhttp = new XMLHttpRequest();
              if (xmlhttp.overrideMimeType) {
                     xmlhttp.overrideMimeType('text/xml');
              }
        }else if (window.ActiveXObject) { // IE
                try {
                    xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
                } catch (e) {
                    try {
                        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
                    } catch (e) {}
                }
        }
		try{
        if (!xmlhttp) { //for non-xml browsers, use IFRAME (bloody OPERA)
            if (iframe==null)
            {
				var iframe = document.createElement('IFRAME');
				iframe.src = temp_url+'=1&i=1';
				// opera 7.54 doesn't like iframe styles 
				iframe.style.width = '0px';
				iframe.style.height = '0px';
				this.iframe = document.getElementById('headline').appendChild(iframe);
		 		
			}
			else{
				this.iframe.src = temp_url+'&i=1';
				
			}
        }
		
		switch (operation_type) 
		{
		case 2:{   //backward
			   indeks= indeks > 0 ? (indeks-1):(header_ids.length-1);
		       }break;
		case 1:
		case 3:{   //forward
			    indeks=indeks<(header_ids.length-1)?(indeks+1):0;
				}break;
		
		}
	   
      	if (operation_type!=4)
		{
			temp_url=url + escape(header_ids[indeks]);	
		}
		if (xmlhttp){
			xmlhttp.open("GET", temp_url, true);
			xmlhttp.onreadystatechange = handleHttpResponse;
			xmlhttp.send(null);
		}
	    if (operation_type==1) { // in case of "play" mode
			timeout=setTimeout('getHTTPObject(1);', rotation_time);
		}
	   }catch (e){
		   alert(e);
		    
	   }
    }
     
	