/*
*	This function opens a popup window to view an image preview
*/
function imagePreviewWindow(strFile,nWidth,nHeight) {
	
	width 	= parseInt (nWidth) - 2;
	height 	= parseInt (nHeight) - 2;
	
	window.open(strFile, "Image","toolbar=no,location=no,directories=no,menubar=no,status=yes,scrollbars=no,resizable=no,width=" + width + ",height=" + height);

}

function setPointer(theRow, theRowNum, theAction)
{
    var theCells = null;

    // 1.1 Sets the mouse pointer to pointer on mouseover and back to normal otherwise.
    if (theAction == "over" || theAction == "click") {
        theRow.style.cursor='pointer';
    } else {
        theRow.style.cursor='default';
    }
    
    return true;
} // end of the 'setPointer()' function


function confirmLink(theLink, theCommand)
{
    // Confirmation is not required in the configuration file
    if (confirmMsg == '') {
        return true;
    }

    var is_confirmed = confirm('Are you sure you want to:\n'+theCommand);
    if (is_confirmed) {
        theLink.href += '&is_js_confirmed=1';
    }

    return is_confirmed;
} // end of the 'confirmLink()' function

function confirmSubmit(theCommand)
{
    var msg=theCommand;
    return confirm(msg);
}

    
function enableField()
{
document.edit_collection.photographer.disabled=false;
document.edit_collection.img_price.disabled=false;
document.edit_collection.img_url.disabled=false;
document.edit_collection.max_res.disabled=false;
document.edit_collection.keywords.disabled=false;
}

function genPopup(url, target, width, height) {
	window.open(url, target, 'width='+width+',height='+height+',toolbar=0,location=0,resizable=yes');
}


function openPop(strFile,nWidth,nHeight,bStatus,bscroll,btoolbar,blocation,bresize,bdirectories,bmenubar) {

window.open(strFile, "help","toolbar=" + btoolbar + ",location=" + blocation + ",directories=" + bdirectories + ",menubar=" + bmenubar + ",status=" + bStatus + ",scrollbars=" + bscroll +",resizable=" + bresize + ",width=" + nWidth + ",height=" + nHeight);
}


function textCounter(field, countfield, maxlimit) {
if (field.value.length > maxlimit) // if too long...trim it!
field.value = field.value.substring(0, maxlimit);
// otherwise, update 'characters left' counter
else
countfield.value = maxlimit - field.value.length;
}


var DHTML = (document.getElementById || document.all || document.layers);

function getObj(name)
{
  if (document.getElementById)
  {
        this.obj = document.getElementById(name);
        this.style = document.getElementById(name).style;
  }
  else if (document.all)
  {
        this.obj = document.all[name];
        this.style = document.all[name].style;
  }
  else if (document.layers)
  {
        this.obj = document.layers[name];
        this.style = document.layers[name];
  }
}

function show_image_editor(flag)
{
        if (!DHTML) return;
        var x = new getObj('edit-image');
        x.style.display = (flag) ? 'inline' : 'none';
        
        if (!DHTML) return;
        var x = new getObj('view-image');
        x.style.display = (flag) ? 'none' : 'inline';
}


function show_img_dele(flag)
{
        if (!DHTML) return;
        var x = new getObj('img-dele');
        x.style.display = (flag) ? 'none' : 'inline'
}


/*
*	Selects all thumbnail checkboxes with name formElement
*/
function selectAllImages(formElement)   { setAllSelect(true,formElement); }

/*
*	Deselects all thumbnail checkboxes with name formElement
*/
function deselectAllImages(formElement) { setAllSelect(false,formElement); }
	
/*
*	Toggles selection state (boolean) for all checkboxes formElement
*/
function setAllSelect(state,formElement) {
	
	var formName	= getThumbnailFormId();
	var elFrm		= document.getElementById(formName);
	
	for (var i = 0; i < elFrm.elements.length; i++) {
		var ourEl = elFrm.elements[i];
		if (ourEl.type == "checkbox")
			if (ourEl.name.indexOf(formElement) != -1)
				ourEl.checked = state;
		}
		
}

/*
*	Returns array of all checkboxes with the name photoid[]
*/
function getSelectedImages() {
	
	var formName	= getThumbnailFormId();
	var elFrm		= document.getElementById(formName);
	var strParams	= "";	
	
	//If selected, add image Id to the list
	for (var i = 0; i < elFrm.elements.length; i++) {
		var ourEl = elFrm.elements[i];
		if (ourEl.type == "checkbox")
			if (ourEl.checked && (ourEl.name.indexOf("photoid[]") != -1))				 
				strParams = strParams + ourEl.value + ",";					
	}
	//Remove Last delimiter.
	strParams = strParams.substr(0,strParams.length-1);	
	return strParams;
	
}

/*
*	Submits the thumbnail grid add to lightbox form
*/
function addSelectionToLightbox () {

	var formName	= getThumbnailFormId();
	var elFrm		= document.getElementById(formName);
	elFrm.submit();
	
}

/*
*	Submits the thumbnail grid remove from lightbox form
*/
function removeSelectionFromLightbox () {

	var formName	= getThumbnailFormId();
	var elFrm		= document.getElementById(formName);
	elFrm.submit();
	
}

/*
*	Returns the id of the thumbnail form being viewed
*/
function getThumbnailFormId() {
	
	var thumbsFormSmall = new getObj('image-small');
	var thumbsFormLarge = new getObj('image-large');
	
	if (thumbsFormSmall.style.display == 'none') {
		return 'thumb_grid_large';
	} else {
		return 'thumb_grid_small';
	}
	
}

function getSelectedCheckbox(buttonGroup) {
   // Go through all the check boxes. return an array of all the ones
   // that are selected (their position numbers). if no boxes were checked,
   // returned array will be empty (length will be zero)
   var retArr = new Array();
   var lastElement = 0;
   if (buttonGroup[0]) { // if the button group is an array (one check box is not an array)
      for (var i=0; i<buttonGroup.length; i++) {
         if (buttonGroup[i].checked) {
            retArr.length = lastElement;
            retArr[lastElement] = i;
            lastElement++;
         }
      }
   } else { // There is only one check box (it's not an array)
      if (buttonGroup.checked) { // if the one check box is checked
         retArr.length = lastElement;
         retArr[lastElement] = 0; // return zero as the only array value
      }
   }
   return retArr;
} // Ends the "getSelectedCheckbox" function

function getSelectedCheckboxValue(buttonGroup) {
   // return an array of values selected in the check box group. if no boxes
   // were checked, returned array will be empty (length will be zero)
   var retArr = new Array(); // set up empty array for the return values
   var selectedItems = getSelectedCheckbox(buttonGroup);
   if (selectedItems.length != 0) { // if there was something selected
      retArr.length = selectedItems.length;
      for (var i=0; i<selectedItems.length; i++) {
         if (buttonGroup[selectedItems[i]]) { // Make sure it's an array
            retArr[i] = buttonGroup[selectedItems[i]].value;
         } else { // It's not an array (there's just one check box and it's selected)
            retArr[i] = buttonGroup.value;// return that value
         }
      }
   }
   return retArr;
} // Ends the "getSelectedCheckBoxValue" function

function changecss(theClass,element,value) {
//documentation for this script at http://www.shawnolson.net/a/503/
 var cssRules;
 if (document.all) {
  cssRules = 'rules';
 }
 else if (document.getElementById) {
  cssRules = 'cssRules';
 }
 for (var S = 0; S < document.styleSheets.length; S++){
  for (var R = 0; R < document.styleSheets[S][cssRules].length; R++) {
   if (document.styleSheets[S][cssRules][R].selectorText == theClass) {
	document.styleSheets[S][cssRules][R].style[element] = value;
   }
  }
 }	
}

Event.observe(window,'load',function(){ changecss('.thumbnail','background','#FFF'); },false);

