
document.petpicsdir = '/petpics/';
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}
addLoadEvent(function(){
  if($('image_table')){
    $('image_table').style.display = "";
		$('loading_message').style.display = "none";
  }
});


function showForm() {
  var theSpan = document.getElementById('newcatinput');
  theSpan.innerHTML = '<form action="?ac=yes" method="POST"><input type="text" name="addnewcat" id="addnewcat" class="long"> <input type="submit" value="Add">&nbsp; [<a href="javascript:hideForm();" class="norm">Close</a>]</form>';

}

/** When a user changes the view on his configuration settings,
 *    they may have the options to change the way the grid is
 *    displayed. 
 */ 
function view_change(){
  
  if($('view').value == 'slideshow'){
    $('grid_options').style.display = "none";  
  }else{
    $('grid_options').style.display = "";
  }
}

function hideForm() {
  var theSpan = document.getElementById('newcatinput');
  theSpan.innerHTML = "<a href='javascript:showForm()' class='admin'>Add new category</a>";
}

function hideUploads() {
  var theSpan = document.getElementById('admin_container_u');
var theRSpan = document.getElementById('admin_header_span');
  theSpan.style.display = "none";
theRSpan.innerHTML = "<a href='javascript:showUploads();' class='admin'>Approve or Delete uploads</a>";
}

function showUploads() {
  var theSpan = document.getElementById('admin_container_u');
var theRSpan = document.getElementById('admin_header_span');
  theSpan.style.display = "block";
theRSpan.innerHTML = "<a href='javascript:hideUploads();' class='admin'>Approve or Delete uploads</a>";

}

function hideCats() {
  var theSpan = document.getElementById('admin_container');
var theRSpan = document.getElementById('admin_header_spanc');
  theSpan.style.display = "none"
theRSpan.innerHTML = "<a href='javascript:showCats();' class='admin'>Categories</a>";

}

function showCats() {
  var theSpan = document.getElementById('admin_container');
var theRSpan = document.getElementById('admin_header_spanc');
  theSpan.style.display = "block";
theRSpan.innerHTML = "<a href='javascript:hideCats();' class='admin'>Categories</a>";

}

function preview(){
  var LoadDiv = document.getElementById('loading');
var CaptionSpan = document.getElementById('caption');
  LoadDiv.style.display = "block";
  var iFrameDiv = document.getElementById('theImage');
  var imageUrl = document.imagecaption.imageUrl.value;
  CaptionSpan.style.display = "block";
CaptionSpan.innerHTML = document.imagecaption2.Utext.value;

iFrameDiv.src = document.petpicsdir + "image.php?image="+imageUrl+"";
}

function hideLoad() {
  /*
  var LoadDiv = document.getElementById('loading');
  LoadDiv.style.display = "none";
 */
 if($('loading')){
  $('loading').style.display = "none";
 }

}

function process() {
//image = document.imagecaption.imageUrl.value;
//caption = document.imagecaption2.Utext.value;
//cat = document.imagecaption3.cat.value;
//window.location = "?img="+image+"&caption="+caption+"&cat="+cat
  $('imageUrl').disabled = false;
  // do some client-side validation
  if($('Utext').value == ''){
    alert('Please enter a caption');
  }else{
    if($('imageUrl').value == '' && 
       $('file').value == ''){
      alert('Please enter an image URL or upload and image');
    }else{
      document.imageupload.submit();
    }
  }

}

function showEdit(id) {
  var theSpan = document.getElementById('editcaption_'+id);
  theSpan.style.display = "block";
}

function showEditAll(id) {
  var theSpan = document.getElementById('editcaptionall_'+id);
  theSpan.style.display = "block";
}

function hideFind() {
  var theSpan = document.getElementById('admin_container_find');
var theRSpan = document.getElementById('admin_header_spansearch');
  theSpan.style.display = "none"
theRSpan.innerHTML = "<a href='javascript:showFind();' class='admin'>Find images by user</a>";

}

function showFind() {
  var theSpan = document.getElementById('admin_container_find');
var theRSpan = document.getElementById('admin_header_spansearch');
  theSpan.style.display = "block";
theRSpan.innerHTML = "<a href='javascript:hideFind();' class='admin'>Find images of user</a>";

}

function captEdit(span, formVal, id) {

  var theSpan = document.getElementById(span);
theSpan.innerHTML = "<form action='' method='POST'><input type='text' id='input_"+span+"' name='edit_capt' class='user_small'><input type='hidden' name='editcaptid' value='"+id+"'> <input type='image' src='http://www.beyondindigopets.com/comfiles/petpics/images/edit.png' height='14' width='14'>";
var theInput = document.getElementById('input_'+span);

theInput.value = formVal;

}

function hideBlurb() {
  var theSpan = document.getElementById('admin_container_blurb');
var theRSpan = document.getElementById('admin_header_spanblurb');
  theSpan.style.display = "none"
theRSpan.innerHTML = "<a href='javascript:showBlurb();' class='admin'>Gallery Home Page Text</a>";
}

function showBlurb() {
  var theSpan = document.getElementById('admin_container_blurb');
var theRSpan = document.getElementById('admin_header_spanblurb');
  theSpan.style.display = "block"
theRSpan.innerHTML = "<a href='javascript:hideBlurb();' class='admin'>Gallery Home Page Text</a>";
}

function hideView() {
  var theSpan = document.getElementById('admin_container_view');
var theRSpan = document.getElementById('admin_header_spanview');
  theSpan.style.display = "none"
theRSpan.innerHTML = "<a href='javascript:showView();' class='admin'>Default View</a>";
}

function showView() {
  var theSpan = document.getElementById('admin_container_view');
var theRSpan = document.getElementById('admin_header_spanview');
  theSpan.style.display = "block"
theRSpan.innerHTML = "<a href='javascript:hideView();' class='admin'>Default View</a>";
}

function hideAll() {
  var theSpan = document.getElementById('admin_container_a');
var theRSpan = document.getElementById('admin_header_spanall');
  theSpan.style.display = "none"
theRSpan.innerHTML = "<a href='javascript:showAll();' class='admin'>All approved images</a>";
}

function showAll() {
  var theSpan = document.getElementById('admin_container_a');
var theRSpan = document.getElementById('admin_header_spanall');
  theSpan.style.display = "block"
theRSpan.innerHTML = "<a href='javascript:hideAll();' class='admin'>All approved images</a>";
}


function updateLink() {
  var linkSpan = document.getElementById('previewspan');
  var URL = document.getElementById('imageUrl').value;
  var caption = document.getElementById('Utext').value;
  var PreviewImg = document.getElementById('PreviewImg');
  PreviewImg.href = document.petpicsdir + "image.php?image="+URL;
  PreviewImg.title = caption;
}

function getCookie( name ) {
	var start = document.cookie.indexOf( name + "=" );
	var len = start + name.length + 1;
	if ( ( !start ) && ( name != document.cookie.substring( 0, name.length ) ) ) {
		return null;
	}
	if ( start == -1 ) return null;
	var end = document.cookie.indexOf( ';', len );
	if ( end == -1 ) end = document.cookie.length;
	return unescape( document.cookie.substring( len, end ) );
}

function setCookie( name, value, expires, path, domain, secure ) {
	var today = new Date();
	today.setTime( today.getTime() );
	if ( expires ) {
		expires = expires * 1000 * 60 * 60 * 24;
	}
	var expires_date = new Date( today.getTime() + (expires) );
	document.cookie = name+'='+escape( value ) +
		( ( expires ) ? ';expires='+expires_date.toGMTString() : '' ) + //expires.toGMTString()
		( ( path ) ? ';path=' + path : '' ) +
		( ( domain ) ? ';domain=' + domain : '' ) +
		( ( secure ) ? ';secure' : '' );
}

function deleteCookie( name, path, domain ) {
	if ( getCookie( name ) ) document.cookie = name + '=' +
			( ( path ) ? ';path=' + path : '') +
			( ( domain ) ? ';domain=' + domain : '' ) +
			';expires=Thu, 01-Jan-1970 00:00:01 GMT';
}

function ChangeOrder() {
  select = document.getElementById('order').value;
  // window.location = "?c=" + select;
  // setCookie('order', select);
  $('interface').submit(); 
}

function ChangeLayout() {
  select = document.getElementById('layout').value;
  // window.location = "?l=" + select;
  // setCookie('layout', select);
    $('interface').submit();
}

function ChangeCategory() {
  select = document.getElementById('category').value;
  // window.location = "?c=" + select;
  // setCookie('category', select);
    $('interface').submit();
}

function hoverStars(num, id) {
var thespan = document.getElementById('response_'+id);
theSpansInner = thespan.innerHTML;

if (theSpansInner !== "Rated, thanks!") {

var i=1;
sub = -1;
var val = num-sub;
// alert("Red: "+i+"-"+num+"    Grey: "+val+"-5");
for (i=1;i<=num;i++)
{
theStar = document.getElementById('star_'+id+'_'+i);
theStar.src = "http://www.beyondindigopets.com/comfiles/petpics/images/starrate.png";
}

for (j=val;j<=5;j++)
{
theStarG = document.getElementById('star_'+id+'_'+j);
theStarG.src = "http://www.beyondindigopets.com/comfiles/petpics/images/greystar.png";
}

}
}



function returnStars(num, id) {
var i=1;
sub = -1;
var val = num-sub;
for (i=1;i<=num;i++)
{
theStar = document.getElementById('star_'+id+'_'+i);
theStar.src = "http://www.beyondindigopets.com/comfiles/petpics/images/star.png";
}

for (j=val;j<=5;j++)
{
theStarG = document.getElementById('star_'+id+'_'+j);
theStarG.src = "http://www.beyondindigopets.com/comfiles/petpics/images/greystar.png";
}


}



function rate(id, star){
 
 var current_num_votes = parseInt($('votes_' +id).innerHTML); 
 
 var thespan = document.getElementById('response_'+id);
 theSpansInner = thespan.innerHTML;

if (theSpansInner == "Rated, thanks!") {
  
  alert("You have already voted on this image!");
  
} else {
	var ajaxRequest;  // The variable that makes Ajax possible!
	
	try{
		// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
	} catch (e){
		// Internet Explorer Browsers
		try{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				// Something went wrong
				alert("We've encountered an error. Please try again later");
				return false;
			}
		}
	}
	// Create a function that will receive data sent from the server
	ajaxRequest.onreadystatechange = function(){
		if(ajaxRequest.readyState == 4){
		  var thespan = document.getElementById('response_'+id);
			thespan.innerHTML = "Rated, thanks!";
      $('votes_' +id).innerHTML = current_num_votes + 1;
			
		}
	}
  
  queryString = id+"&star="+star;
	ajaxRequest.open("GET", document.petpicsdir + "rate.php?id="+queryString, true);

	ajaxRequest.send(null); 
  var thespan = document.getElementById('response_'+id);
 	thespan.innerHTML = "Please wait";
  
}

  

}

function findIndex(ng_array, n_value){
  for (var i=0; i < ng_array.length; i++) {
    if (ng_array[i] == n_value) {
     return i;
    }
  }
}

function isInt(x) {
   var y=parseInt(x);
   if (isNaN(y)) return false;
   return x==y && x.toString()==y.toString();
} 

function alertArray(ng_array, bAlert){
  szDebug = 'DEBUG\n';
  for(key in ng_array){
    if(isInt(key)){
      szDebug += "\t" + key + ":" + ng_array[key] + "\n";
    }
  }
  
  if(bAlert){
    alert(szDebug);
  }else{
    return szDebug;
  }
}

function NextImage(w){
  
  if(!$('current_id')){
    currentIndex = 0;
  }else{
    currentId = $('current_id').value;
    currentIndex = findIndex(document.slideshow_ids, currentId);
  }
  nextid = document.slideshow_ids[currentIndex + 1];
  
  // Go back to the first index if this is the last
  // index in the array
  if(!isInt(nextid)){
    nextid = document.slideshow_ids[0];
  }
  
  szDebug = alertArray(document.slideshow_ids, false);  
  szDebug += "current Index: " + currentIndex + "\n";
  szDebug += "nextid: " + nextid + "\n";

  //alert(szDebug);
  
  AjaxUpdateImage('image_container', nextid, w);
}

function PrevImage(w){

  if(!$('current_id')){
    currentIndex = 0;
  }else{
    currentId = $('current_id').value;
    currentIndex = findIndex(document.slideshow_ids, currentId);
  }
  
  previd = document.slideshow_ids[currentIndex - 1];
  // Go to the last index if this is the first
  // index in the array
  if(!isInt(previd)){
    previd = document.slideshow_ids[document.slideshow_ids.length - 1];
  }
  
  szDebug = alertArray(document.slideshow_ids, false);  
  szDebug +=  "current Index: " + currentIndex + "\n";
  szDebug += "previd: " + previd + "\n";

 // alert(szDebug);
  
  AjaxUpdateImage('image_container', previd, w);
}

/* Will update the element with the css id elementid to 
 * hold the image and rating box of the image with id of
 * id
 */
function AjaxUpdateImage(elementid, id, width){
  $url = document.petpicsdir + "AjaxFeeder.php?imageid=" + id + "&width=" + width;
  
  new Ajax.Request(
    $url, { 
      method:'get',
      onSuccess: function(transport){
        var ajaxResponse = transport.responseText || "false";
        $(elementid).innerHTML = ajaxResponse;
      }
  });
  
  
  
}

