// JavaScript Document
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

//-->
function erresefield(){

document.searchForm.keywords.value='';
}
//replacing index.php
function enumerateAllItems()
{
    elementCount = document.all.length

    for(i = 0; i < elementCount; i++)
    {
        if (document.all[i].tagName == "a" || document.all[i].tagName == "A")
        {
            element = document.all[i];
            if (matchIndexDotPhp(element))
			{						
				var oldHref=String(element);		
				//http://www.staging.iglooreview.rating.php.php/?i=191
				//var replstr=oldHref.substring(31,40);
				var newHref =oldHref.replace("index.php","rating.php");		
				document.all[i].href = newHref;
			}
        }
     }
}

function matchIndexDotPhp(text) {

  var re = new RegExp("[0-9a-zA-Z]+/index.php[?]+i=[0-9]");
  if (re.exec(text)) {
        return true;
  }
  else {
        return false;
  }
}

var ajax = new sack();
var ajax_list_objects = new Array();
var getting_file = false;

function sliding(operation,image_id)
{
	if (getting_file == false)
	{
		getting_file = true;
		var url='AjaxSlideShow.php?opt='+operation+'&i='+image_id;
		//alert(operation);
		ajax.requestFile = url;
	
		ajax.onCompletion = displayDetails;	
		ajax.runAJAX();
	}
}

function prevnext(operation,image_id)
{
	StopTheClock();
	sliding(operation,image_id);
}

function displayDetails()
{
	var content = ajax.response;
	var ajax_total_objects = content.split('^');

	if(content!="")
	{
		var divText = ajax_total_objects[3];
		if(ajax_total_objects[4]==6)
		{
			//document.getElementById("ImgContainer").style.backgroundImage = 'imagesnew/loading_video.gif';
			document.getElementById("ImageDisplayDiv").innerHTML = "<span>"+divText+"</span>";
			StopTheClock();
		}
		else
		{
			var newImagePath = ajax_total_objects[5];
			
			if(null == document.getElementById("ProjectImage"))
			{
				ImageStr = "<img src='" + newImagePath + "' border='0' alt='$imageAltText' title='$imageAltText'  width='483' id='ProjectImage' />";
				document.getElementById("ImageDisplayDiv").innerHTML = ImageStr;
			}
			else
			{
				//StopTheClock();
				changeImage(newImagePath);
				//alert("create image done");
				//InitializeTimer();
			}
			
		}
		
		document.getElementById("prev").innerHTML = ajax_total_objects[0];
		document.getElementById("pause").innerHTML = ajax_total_objects[1];
		document.getElementById("next").innerHTML = ajax_total_objects[2];
		opt = ajax_total_objects[4];
		
		//alert(opt);
		//getting_file = false;
	}	
	
}
var nextPicture;

function changeImage(newImagePath) {
   nextPicture = new Image(483,348);
   nextPicture.src = newImagePath;

   // image is in cache (IE6 & IE7 ... Firefox can handle the onload well even file was in cache);
   if (nextPicture.complete) 
   {
   	   //alert("complete");
       document.getElementById("ProjectImage").src = nextPicture.src;
	   getting_file = false;
   }
   else // image not in cache
   {
		   nextPicture.onload = function() {
			    document.getElementById("ProjectImage").src = nextPicture.src;
	   			getting_file = false;
		   }
   }
}
var secs=0;
var timerID = null;
var timerRunning = false;
var delay = 5000;
var opt = 1;
//var hasMoreImages = true; /* this value will be set from backend*/

function InitializeTimer()
{
    // Set the length of the timer, in seconds
//  secs = 4;
    StopTheClock();
	
	try 
	{
		// See if image_id is present. If not present no image is present. Hence don't start the timer
		if (image_id == "")
			return;
	}
	catch(err)
	{
		return;	
	}
	try
	{
		if (hasMoreImages) {
			StartTheTimer();
		}
	}
	catch (err)
	{
		return;
	}
}

function StopTheClock()
{
   getting_file = false;
   if(timerRunning) {
        clearTimeout(timerID)
    	timerRunning = false
	}
}

function StartTheTimer()
{
    timerID = self.setInterval("StartImageDisplay()", delay);
	timerRunning = true;
}


function StartImageDisplay()
{
	//if(opt==4)
	if(opt==6)
	{
		// We are in video display, no timmer is needed.
		StopTheClock();
	}
	else
		sliding(opt,image_id);
}

