//Initialisation of the tween var. - When the page is load initLogin() is executed.
//{
	var loginBox;
	var isdatareceived;
	var firsttime;
	firsttime=true;
	try
	{
		window.addEvent('domready', init);
	}
	catch(e) {}
//}
//
var tween;

function ChangeBigImage(newsrc)
{
  document.getElementById("bigImg").src=newsrc;
}
///These are to be easier to get the next indexes when we want to start from the beggining, or from the end...
function NextIndex(num)
{
   if (currentindex+num<=maxindex) return currentindex+num;
   else return currentindex+num-maxindex-1;
}
function PrevIndex(num)
{
   if (currentindex-num>=0) return currentindex-num;
   else return currentindex-num+maxindex+1;
}
//////////////////////////////////////////////
function UpdateNavigation()
{
  document.getElementById("thumb_prev").src=PartyPicturesPath+arrpics[PrevIndex(1)][2];
  document.getElementById("thumb_next").src=PartyPicturesPath+arrpics[NextIndex(1)][2];
  document.getElementById("currentindex").innerHTML="#"+(currentindex+1);
}

function ShowBigImage()
{
  document.getElementById("bigImg").style.visibility="visible";
}
function DisplayById(id)
{
  var i,IDindex=0;
  for (i=0;i<=maxindex;i++) if (arrpics[i][0]==id) {IDindex=i; break;}
  currentindex=IDindex;
  if (maxindex>0)InitializeTmpVariables(currentindex);
  ChangeBigImage(PartyPicturesPath+arrpics[currentindex][1]);
  UpdateNavigation();
  ShowBigImage();
  firsttime=false;
  getResults(arrpics[currentindex][0]);
}

function FadeOutPast()
{
//  alert(picpast1.src);
  tween.start(0).chain(
  function(){
    document.getElementById("bigImg").src=picpast1.src;
    tween.start(1).chain(function(){
      currentindex=PrevIndex(1);
      UpdateNavigation();
      picnext2.src=picnext1.src;
      picnext1.src=thispic.src;
      thispic.src=picpast1.src;
      picpast1.src=picpast2.src;
      picpast2.src=PartyPicturesPath+arrpics[PrevIndex(2)][1];
      document.location.hash="id"+arrpics[currentindex][0]; 
      getResults(arrpics[currentindex][0]);
      });
    });
}
function FadeOutNext()
{
  tween.start(0).chain(
  function(){
    document.getElementById("bigImg").src=picnext1.src;
    tween.start(1).chain(function(){
      currentindex=NextIndex(1);
      UpdateNavigation();
      picpast2.src=picpast1.src;
      picpast1.src=thispic.src;
      thispic.src=picnext1.src;
      picnext1.src=picnext2.src;
      picnext2.src=PartyPicturesPath+arrpics[NextIndex(2)][1];
      document.location.hash="id"+arrpics[currentindex][0]; 
      getResults(arrpics[currentindex][0]);
      });
    });
}
/*
function FadeIn()
{
  tween.start(1);
}
*/

function init()
{
  tween = $("bigImg").get('tween', {property: 'opacity', duration:250});
}

//The temp variables.
var picnext1 = new Image();
var picnext2 = new Image();
var picpast1 = new Image();
var picpast2 = new Image();
var thispic = new Image();


function InitializeTmpVariables(currentindex)
{
  if (currentindex>0)picpast1.src=PartyPicturesPath+arrpics[currentindex-1][1]; else picpast1.src=PartyPicturesPath+arrpics[maxindex][1];
  if (currentindex>1)picpast2.src=PartyPicturesPath+arrpics[currentindex-2][1]; else picpast2.src=PartyPicturesPath+arrpics[maxindex-1][1];
  thispic.src=PartyPicturesPath+arrpics[currentindex][1];
  if (currentindex+1<=maxindex) picnext1.src=PartyPicturesPath+arrpics[currentindex+1][1]; else picnext1.src=PartyPicturesPath+arrpics[1][1];;
  if (currentindex+2<=maxindex) picnext2.src=PartyPicturesPath+arrpics[currentindex+2][1]; else picnext2.src=PartyPicturesPath+arrpics[0][1];;
}
//Get their value at begining...
if (maxindex>0) InitializeTmpVariables(currentindex);

function ChangeImagePrev()
{
  if (maxindex<=0) return;
  //if (currentindex<=0) currentindex=maxindex+1; // this is couse we have later currentindex=currentindex-1; so currindex will become maxindex;
  if (isdatareceived) Wait();
  if(!tween) init();
  FadeOutPast();
  if (firsttime) firsttime=false;
}

function ChangeImageNext()
{
  if (maxindex<=0) return;
  //if (currentindex>=maxindex) currentindex=-1; // this is couse we have later currentindex=currentindex+1; so currindex will become 0;
  if (isdatareceived) Wait();
  if(!tween) init();
  FadeOutNext();
  if (firsttime) firsttime=false;
}

function Wait()
{
  var loading = "<div class='loading'><div></div></div>";
//  document.getElementById("pixNavigateDiv").innerHTML+=loading;
  document.getElementById("StarsDiv").innerHTML="";
  document.getElementById("pixToolsDiv").innerHTML+=loading;
  document.getElementById("renderTagsDiv").innerHTML='';
  document.getElementById("commentsDivContainer").innerHTML="";
  document.getElementById("PictureTagsContainer").innerHTML="";
}


function getResults(picid)
{
	/** 
	 * the row below is commented, because the var iPhotographerPhotos is a Get var and doesn't affect the js algorithm in this file,
	 * otherwise we had to pass it as a second argument of the function getResults ...
	 */
	//isuserphotos = arguments.length > 1 ? "&userphotos=" + arguments[1] : "";
	
	//isuserphotos = "&userphotos=" + iPhotographerPhotos;
	
	var url;
	if (arrpics[currentindex][3]) url = MEMBERDISPATCH_URL+picid;
	else url = DISPATCH_URL+picid;
	var s;
//  DoWait("LargePicDiv","LargePicDivOver");
  isdatareceived=false;
  if (client == null)
  {
    client = new R9HTTPXml();
  }  
  if (!client.inprogress)
  {
    client.init(url, null, null);
    try
    {
    	client.asyncGET(new contentCallback());
    }
    catch (e)
    {
      alert(e);
    }
  }
  else
  {
    //alert("client inprogress is true");
  }
}

function contentCallback()
{
	this.onLoad = function(xmlobj)
  {
    // only if req shows "loaded"
    if (xmlobj.xmlhttp.readyState == 4)
    {
      // only if "OK"
      if (xmlobj.xmlhttp.status == 200)
      {
        // the XML  way
        var results = xmlobj.getTags("results");
        
				var scriptText = "";
				
//				scriptText = xmlobj.getTagText(results[0], "javascript", 0);
//				eval(scriptText);
        //copyResult(xmlobj,results,"LargePicDiv");
        //copyResult(xmlobj,results,"pixNavigateDiv");
        copyResult(xmlobj,results,"pixToolsDiv");
        copyResult(xmlobj,results,"renderTagsDiv");
        copyResult(xmlobj,results,"commentsDivContainer");
        copyResult(xmlobj,results,"StarsDiv");
        copyResult(xmlobj,results,"PictureTagsContainer");
        isdatareceived=true;
        RefreshBanners(true);
      }
      else
      {
        //alert("There was a problem getting results:\n" + xmlobj.xmlhttp.statusText);
        //nextResults();
      }
    }
    else
    {
      alert("There was a problem getting results (bad state) " + xmlobj.xmlhttp.readyState);
    }
  }
  this.onError = function(status,statusText)
  {
    //maxfailures--;
    //alert("Sorry, we could not retrieve results. We will try " + maxfailures + " more times before giving up.");
    alert("Error: " + statusText);
    //nextResults();
  }
}

function copyResult(xmlobj,results,el)
{
	var elContent = el+"Content";
	var elText = xmlobj.getTagText(results[0], elContent, 0);
	document.getElementById(el).innerHTML = elText;
}