// 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=400;

	var header_ids ;

	var timeout;

	var iframe;

	operation=4;   //type of operation

	//1- random 2-back 3-forward 4-init

	indeks=0;

	url = "news_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 removeChildren(node){

		var tempChild = node.firstChild;

		while (tempChild != null){

			node.removeChild(tempChild);

			tempChild = node.firstChild;

    	}

	}



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++){

					//////////////////////////////////// fill header ids

					header_ids[i]=elem[i].firstChild.data;

				}

				operation=1;

				url = "news_ajax.php?id=";

				timeout=setTimeout('getHTTPObject(operation);', rotation_time);

			}

			

			if (type=='article'){

				var headline_image =  document.getElementById('headline_image');

				//var headline_href =  document.getElementById('headline_href');

				//var headline_created = document.getElementById('headline_created');

				var headline_caption =  document.getElementById('headline_caption');

				//var headline_author =  document.getElementById('headline_author');

				var headline_body =  document.getElementById('headline_body');

				var headline_link =  document.getElementById('headline_link');

				



				var article = xmlDocument.getElementsByTagName('article').item(0);

				

				headline_link.href = article.getAttribute('href');

				

				var node = article.firstChild;

				

				while (node != undefined){

					switch (node.nodeName){

						/*case 'created':

							removeChildren(headline_created);

							if (node.hasChildNodes()){								

								headline_created.appendChild(document.createTextNode(node.firstChild.nodeValue));

							}

							break;*/
						case 'image':
							headline_image.setAttribute('src', node.getAttribute('src'));
							break;
						case 'caption':

							removeChildren(headline_caption);

							

							headline_caption.appendChild(document.createElement('a'));

							if (node.hasChildNodes()){								

		headline_caption.firstChild.appendChild(document.createTextNode(node.firstChild.firstChild.nodeValue));

		headline_caption.firstChild.setAttribute('href',node.firstChild.getAttribute('href'));

								

							}

							break;		



						case 'body':

							removeChildren(headline_body);

							if (node.hasChildNodes()){

								headline_body.appendChild(document.createTextNode(node.firstChild.nodeValue));

							}else{

								headline_body.appendChild(document.createTextNode(''));

							}

							break;

					}

					node = node.nextSibling;

				}

//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="images/play.gif";

		  } else {

		    document.getElementById('pause').src="images/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);

		    

	   }

    }

// That's All Folks.

	
