var ajax = new Object();
var photoCount=1;
var photoCountLimit = 11;
ajax.has_xml_http = false;
ajax.xml_http     = null;
ajax.result_cache = new Object();

/******************************************************************************/
ajax.getXmlHttpObject = function() {
  var obj = null;
  if(window.XMLHttpRequest) {
    obj = new XMLHttpRequest();
  } else if(window.ActiveXObject) {
    obj = new ActiveXObject("Microsoft.XMLHTTP");
  }
  return obj;
};
ajax.has_xml_http = ajax.getXmlHttpObject() ? true : false;

/******************************************************************************/
ajax.cacheResults = function(key, page, value) {
  ajax.result_cache[key] = value;
  //ajax.result_cache[key + '-' + page] = value;
};

/******************************************************************************/
ajax.getFromCache = function(key) {
  return ajax.result_cache[key];
};

/******************************************************************************/
ajax.throwError = function(s) {
  alert(s);
};

/******************************************************************************/
ajax.sendRequest = function(url, method, do_eval) {
  if (ajax.xml_http && ajax.xml_http.readyState!=0) {
    ajax.xml_http.abort()
  }
  ajax.xml_http = ajax.getXmlHttpObject();
  if (ajax.xml_http) {
    ajax.xml_http.open(method, url, true);
    ajax.xml_http.onreadystatechange = function() {
      if (ajax.xml_http.readyState==4 && ajax.xml_http.responseText) {
        if (do_eval) {
          eval(ajax.xml_http.responseText);
        } else {
          document.write(ajax.xml_http.responseText);
        }
      }
    };
    ajax.xml_http.send(null);
  }
};

/******************************************************************************/
function prevPhoto() {
  var currentId;
  var albumId;
  var url;
  currentId = document.getElementById('display_order').value;
  albumId = document.getElementById('album_id').value;
  url = "album.php?current="+currentId+"&album_id="+albumId+"&mode=prev";
  try{
	  ajax.sendRequest(url, "GET", true );
  }catch(e){
	//  countPhoto();
  }
}

/******************************************************************************/
function nextPhoto() {
  var currentId;
  var albumId;
  var url;
  currentId = document.getElementById('display_order').value;
  albumId = document.getElementById('album_id').value;
  url = "album.php?current="+currentId+"&album_id="+albumId+"&mode=next";
  try{
	  ajax.sendRequest(url, "GET", true );
  }catch(e){
	//  countPhoto();
  }
}

/******************************************************************************/
function displayNext(src,nextId,total,height,width,caption) {
	//console.group('arguments:');
	//console.log(arguments);
	//console.groupEnd();
	//console.group('photoCount % photoCountLimit:');
	//console.log(photoCount % photoCountLimit);
	//console.groupEnd();
	//console.group('photoCount: ');
	//console.log(photoCount)
	//console.groupEnd();
	//console.group('photoCountLimit: ');
	//console.log(photoCountLimit);
	//console.groupEnd();
	countPhoto();
	if(photoCount % photoCountLimit == 0 && photoCount != 0){
		
		document.getElementById('gallery_photo').innerHTML = "<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\" width=\"100%\" height=\"100%\"><tr><td>&nbsp;</td></tr><tr><td width=\"100%\" align=\"center\" valign=\"bottom\" style='font-family:Arial;font-size:12px;'>Advertisement</td></tr><tr><td width=\"100%\" height=\"250\" align=\"center\" valign=\"middle\"><iframe src=\"./300x250ad.html?galleryId="+galleryAdNumber+"\" width=\"300\" height=\"250\" align=\"center\" scrolling=\"no\" frameborder=\"0\" allowtransparency=\"true\"></iframe></td></tr><tr><td>&nbsp;</td></tr></table>"; //aws
		document.getElementById('gallery_text').innerHTML = "";
		try{
			document.getElementById('galleryAdCover').style.visibility='visible';
			document.getElementById('gallery_leaderboard').style.visibility = 'hidden';
			document.getElementById('gallery_adbg').style.visibility = 'hidden';
			
		}catch(e){
		}
	}else{
		try{
			document.getElementById('galleryAdCover').style.visibility='';
			document.getElementById('gallery_leaderboard').style.visibility = 'visible';
			document.getElementById('gallery_adbg').style.visibility = 'visible';
		}catch(e){
		}
		var newcaption;
		if(src != '') {
			var currentId;
			currentId = document.getElementById('display_order').value;
			if(src.substr(0,7) != 'http://') { //aws
				document.getElementById('gallery_photo').innerHTML = "<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\" width=\"100%\" height=\"100%\"><tr><td width=\"100%\" height=\"100%\" align=\"center\" valign=\"middle\"><img src=\"thumb.php?src="+src+"&wmax=446&hmax=410&quality=90\"></td></tr></table>"; //aws
			} else { //aws
				document.getElementById('gallery_photo').innerHTML = "<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\" width=\"100%\" height=\"100%\"><tr><td width=\"100%\" height=\"100%\" align=\"center\" valign=\"middle\"><img src=\""+src+"\"></td></tr></table>"; //aws
			} //aws
			document.getElementById('display_order').value = nextId;
			document.getElementById('sequence').innerHTML = nextId+" of "+total;
			var current_location = document.getElementById('leaderboard_frame').src;
			document.getElementById('leaderboard_frame').src = current_location;
			var current_location2 = document.getElementById('234x60_frame').src;
			document.getElementById('234x60_frame').src = current_location2;
			newcaption = caption.replace(/\+/g,' ');
			newcaption = unescape(newcaption);
			newcaption = newcaption.replace(/\\/g,'');
			document.getElementById('gallery_text').innerHTML = newcaption;
		} else {
		}
	}
	
}

/******************************************************************************/
function swapAlbum() {
  var albumId = document.getElementById('newAlbumId').value;
  if(albumId == 'default') {
    return true;
  }
  //document.location = '/cc-common/gallery/display.html?album_id='+albumId;
  document.location = 'display.html?album_id='+albumId;
}

function countPhoto(){
	photoCount=photoCount+1;
	//tracking via global trackingCon1Dart, trackingCon2, and trackingCon1Display, useOmnitureS variables from display.html
	if(photoCount % photoCountLimit== 0 && photoCount != 0){
		if(useOmnitureS){
			s.t();
		}
	}else{
		if(useOmnitureS){
			s.t();
		}
	}
}
