function toggleSelect(selected)
{
	var items = document.getElementById("deletePhotosForm").elements;
	
	for(var i=2; i<items.length-2;i++)
	{
		items[i].checked = selected;
	}
}


function showLoading()
{
	document.getElementById("loading").style.display = "block";
}

function checkBox(inElement)
{
	var box = document.getElementById("box_" + inElement.id);
	if(box.checked == true)
	{
		box.checked = false;
	}
	else
	{
		box.checked = true;
	}
	
}

function hasClass(ele,cls) {
	return ele.className.match(new RegExp('(\\s|^)'+cls+'(\\s|$)'));
}
function addClass(ele,cls) {
	if (!this.hasClass(ele,cls)) ele.className += " "+cls;
}
function removeClass(ele,cls) {
	if (hasClass(ele,cls)) 
	{
		var reg = new RegExp('(\\s|^)'+cls+'(\\s|$)');
		ele.className=ele.className.replace(reg,'');
	}
}

function checkUsername()
{
	if(document.getElementById('username').value == "Username")
	{
		alert("Please enter a Username to view their albums");
		return false;
	}
		
	return true;
}

var dateObj = new Date();

function GetXmlHttpObject()
{
	var xmlHttp=null;
	try
	 {
	 // Firefox, Opera 8.0+, Safari
	 xmlHttp=new XMLHttpRequest();
	 }
	catch (e)
	 {
	 //Internet Explorer
	 try
	  {
	  xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
	  }
	 catch (e)
	  {

	  xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
	  }
	 }
	return xmlHttp;
}

function rotateImage(albumid, imageName, degree, theImage)
{
	document.getElementById("rotating").style.visibility = "visible";
	var xmlhttp = GetXmlHttpObject();

	var d = new Date();
	var url = "modules/rotate.php";
	url +="?imageName=" + imageName;
	url += "&albumid=" + albumid;
	url += "&degree=" + degree;
	url += "&time=" + d.getTime();

	xmlhttp.open("GET",url,true);
	xmlhttp.send(null);
	  
	xmlhttp.onreadystatechange=function()
	{
	if(xmlhttp.readyState==4)
	  {
		document.getElementById(theImage).src = document.getElementById(theImage).src + "?datetime=" + d.getTime();
		document.getElementById("rotating").style.visibility = "hidden";
	  }
	}

}

function updateAlbumCover(albumId, photoId)
{
	var xmlhttp = GetXmlHttpObject();

	var d = new Date();
	var url = "modules/updateAlbumCover.php";
	url +="?albumId=" + albumId;
	url += "&photoId=" + photoId;

	xmlhttp.open("GET",url,true);
	xmlhttp.send(null);
	  
	xmlhttp.onreadystatechange=function()
	{
	if(xmlhttp.readyState==4)
	  {
			var oldImage = document.getElementsByClassName("cover");
			if(oldImage != null && oldImage[0] != null)
			{
				removeClass(oldImage[0],"cover");
			}

			var newImage = document.getElementById(photoId);
			addClass(newImage, "cover"); 
			alert("New Album cover set successfully");
	  }
	}

}

//when image has been changed, reload it
function reloadImage(imageId)
{
	var d = new Date();
	document.getElementById("img_"+imageId).src = document.getElementById("img_"+imageId).src + "?time=" + d.getTime();
}

function reloadAdminThumb(imageId)
{
	var d = new Date();
	document.getElementById(imageId).src = document.getElementById(imageId).src + "?time=" + d.getTime();
}

var validWBox = 800; /* imageBox width */
var validWImg = 150; /* image width */
var numImg = 1; /* count image */

/* Function to scroll through images*/
function moveToPrevious() {
		document.getElementById("notice").innerHTML = "";
	var validMove = getMoveStatus('prev');
	if(validMove) 
	{
		new Effect.Move('imageBoxInside', { x: validWBox, y: 0, transition: Effect.Transitions.sinoidal });
	}
	else
	{
		document.getElementById("notice").innerHTML = "You are already at the start";
	}
}

function moveToNext() {
	document.getElementById("notice").innerHTML = "";
	var validMove = getMoveStatus('next');
	if(validMove) {
	new Effect.Move('imageBoxInside', { x: (0-validWBox), y: 0, transition: Effect.Transitions.sinoidal });
	}
	else
	{
		document.getElementById("notice").innerHTML = "You are already at the end at the end";
	}
}

function getMoveStatus(currMove) 
{
	var theLeft = document.getElementById('imageBoxInside').style.left;
	/* current left position of ImageBoxInside */

	var imgNumComul = numImg * validWImg;
	/* comulative image width (10*130) */

	theLeft = parseInt(theLeft.replace('px',''));
	
	if(currMove == 'next') 
	{
		//theLeft = theLeft - validWBox;
	} 
	else 
	{
		theLeft = theLeft + validWBox;
	}
	//alert("theLeft = "+theLeft+" | validWBox = "+validWBox+" | imgNumComul = "+imgNumComul);
	
	if(theLeft == validWBox) 
	{
		//alert("1");
		return false;
	} 
	else if(theLeft <= (0-imgNumComul)) //at end
	{
		//alert("2");
		return false;
	} 
	else 
	{
		//alert("3");
		return true;
	}
}

function deleteComment(commentId, photoId)
{
	var xmlhttp = GetXmlHttpObject();

	var d = new Date();
	var url = "modules/deleteComment.php";
	url += "?photoId=" + photoId;
	url += "&commentId=" + commentId;

	xmlhttp.open("GET",url,true);
	xmlhttp.send(null);
	  
	xmlhttp.onreadystatechange=function()
	{
	if(xmlhttp.readyState==4)
	  {
		fade("comment_"+commentId);
		setTimeout("removeElement('commentList', 'comment_"+commentId+"')",1200);
		document.getElementById('numComments').innerHTML = xmlhttp.responseText;
	  }
	}
}

function deletePhoto(albumId, photoId, photoName, albumCover)
{
	var xmlhttp = GetXmlHttpObject();

	var d = new Date();
	var url = "modules/deletePhoto.php";
	url +="?albumId=" + albumId;
	url += "&photoId=" + photoId;
	url += "&photoName=" + photoName;
	url += "&albumCover=" + albumCover;

	xmlhttp.open("GET",url,true);
	xmlhttp.send(null);
	  
	xmlhttp.onreadystatechange=function()
	{
	if(xmlhttp.readyState==4)
	  {
		fade("photo_"+photoId);
		setTimeout("removeElement('adminPhotosList', 'photo_"+photoId+"')",1200);
	  }
	}
}

var  TimeToFade = 1000.0;

function fade(eid)
{
  var element = document.getElementById(eid);
  if(element == null)
    return;
   
  if(element.FadeState == null)
  {
    if(element.style.opacity == null
        || element.style.opacity == ''
        || element.style.opacity == '1')
    {
      element.FadeState = 2;
    }
    else
    {
      element.FadeState = -2;
    }
  }
   
  if(element.FadeState == 1 || element.FadeState == -1)
  {
    element.FadeState = element.FadeState == 1 ? -1 : 1;
    element.FadeTimeLeft = TimeToFade - element.FadeTimeLeft;
  }
  else
  {
    element.FadeState = element.FadeState == 2 ? -1 : 1;
    element.FadeTimeLeft = TimeToFade;
    setTimeout("animateFade(" + new Date().getTime() + ",'" + eid + "')", 33);
  }  
}

function  animateFade(lastTick, eid)
{  
  var curTick = new Date().getTime();
  var elapsedTicks = curTick - lastTick;
 
  var element = document.getElementById(eid);
 
  if(element.FadeTimeLeft <= elapsedTicks)
  {
    element.style.opacity = element.FadeState == 1 ? '1' : '0';
    element.style.filter = 'alpha(opacity = '
        + (element.FadeState == 1 ? '100' : '0') + ')';
    element.FadeState = element.FadeState == 1 ? 2 : -2;
    return;
  }
 
  element.FadeTimeLeft -= elapsedTicks;
  var newOpVal = element.FadeTimeLeft/TimeToFade;
  if(element.FadeState == 1)
    newOpVal = 1 - newOpVal;

  element.style.opacity = newOpVal;
  element.style.filter = 'alpha(opacity = ' + (newOpVal*100) + ')';
 
  setTimeout("animateFade(" + curTick + ",'" + eid + "')", 33);
}

function removeElement(parentDiv, childDiv)
{
     if (childDiv == parentDiv) 
     {
          alert("The parent div cannot be removed.");
     }
     else if (document.getElementById(childDiv)) 
     {     
          var child = document.getElementById(childDiv);
          var parent = document.getElementById(parentDiv);
          parent.removeChild(child);
     }
     else 
     {
          alert("Child div has already been removed or does not exist.");
          return false;
     }
}

function showPreview(id)
{
	addClass(document.getElementById(id), "showPreview");
}

function hidePreview(id)
{
	removeClass(document.getElementById(id), "showPreview");
}

function addCaption(theArea)
{
	if(theArea.value == "...add caption")
	{
		theArea.style.fontStyle="normal";
		theArea.style.color="#000";
		theArea.value = "";
	}
}

function blurCaption(theArea)
{
	if(theArea.value == "")
	{
		theArea.style.fontStyle="italic";
		theArea.style.color="#999";
		theArea.value = "...add caption";
	}
}

