// JavaScript Document

function toggleDiv(divID){
  var tDiv=document.getElementById(divID);
  
  if(tDiv.style.display=='none'){
    tDiv.style.display='block';
  } 
  else {
	this.style.display='block';
  }
}

function toggleDiv2(divID,div2ID){
  var tDiv=document.getElementById(divID);
  var tDiv2=document.getElementById(div2ID);
  
  if(tDiv.style.display=='none'){
    tDiv.style.display='block';
	tDiv2.style.display='none';
  } 
  else {
	tDiv2.style.display='block';
	tDiv.style.display='none';
  }
}

function getElementsByClass(searchClass,node,tag) {
	var classElements = new Array();
	if ( node == null )
		node = document;
	if ( tag == null )
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}

function showCatReferences(catID){
  
  var refContainers = getElementsByClass('cListingName');
  for(i=0;i<refContainers.length;i++){
	if(refContainers[i].id == catID){
	  refContainers[i].style.display="block";
	}else{
	  refContainers[i].style.display="none";
	}
  }
  var refContentContainers = getElementsByClass('referenceListing');
  for(i=0;i<refContentContainers.length;i++){
	  refContentContainers[i].style.display="none";
  }

}

function showCatListings(catID){
  
  var refContainers = getElementsByClass('mListing');
  for(i=0;i<refContainers.length;i++){
	if(refContainers[i].id == catID){
	  refContainers[i].style.display="block";
	}else{
	  refContainers[i].style.display="none";
	}
  }
}

function showUpdateKeyLink(linkID,key){
  var newKeyFieldID = 'newKeyField'+key;
  document.getElementById(newKeyFieldID).style.color='red';
  var newKey = document.getElementById(newKeyFieldID).value;
  var linkElement = document.getElementById(linkID);
  
  linkElement.title='Swap Gallery Image Listing Keys ( '+newKey+' <--> '+key+' ).';
  linkElement.style.display='inline';
}

function showDiv(divID){
  document.getElementById(divID).style.display='block';
}

function hideDiv(divID){
  document.getElementById(divID).style.display='none';
}

function navTip(container,str)
{
	document.getElementById(container).innerHTML=str;
}

function clearNavTip(container)
{
	document.getElementById(container).innerHTML='';
}

function showBorder(container){
	document.getElementById(container).style.borderColor='blue';
	document.getElementById(container).style.borderWidth='1px';
	document.getElementById(container).style.borderStyle='solid';
}

function hideBorder(container){
	document.getElementById(container).style.borderStyle='none';
}

function closeMiniGallery(){
	document.getElementById('darkBackgroundLayer').style.display='none';
	document.getElementById('miniGallery').style.display='none';
	document.getElementById('miniGalleryPicDiv').style.display='none';
	document.getElementById('closeDiv').style.display='none';
	document.getElementById('backDiv').style.display='none';
}

function showMiniGallery(cKey,mKey){
	document.getElementById('darkBackgroundLayer').style.display='block';
	document.getElementById('miniGallery').style.display='block';
	document.getElementById('miniGalleryPicDiv').style.display='block';	
	document.getElementById('closeDiv').style.display='block';
	document.getElementById('backDiv').style.display='block';
	document.getElementById('backDiv').innerHTML="<img onmouseover='showBorder(&#39;backDiv&#39;);' onmouseout='hideBorder(&#39;backDiv&#39;);' onclick='showMiniGallery(&#39;"+cKey+"&#39;,&#39;"+mKey+"&#39;);' src='images/backButton_blueBox.jpg'/>";
	ajaxGallery("miniGalleryPicDiv",cKey,mKey,"../PHP/getListingGallery.php");
}

function displayDiv(div) {
  document.getElementById(div).style.visibility='visible';
}

function displayPicture(str){
	document.write('<img src="image/'+str+'" width="325" id="floatLeft"/>');
}

function showMiniGalleryPic(picFile,name,description){
	document.getElementById('miniGalleryPicDiv').innerHTML = '<img src="images/gallery/'+picFile+'" width="325" align="center"/>';
	document.getElementById('miniGalleryPicDiv').innerHTML += '<br/><span style="font-size:18px;color:white;width:100%;text-align:center;">'+name+'</span><br/><span style="width:100%;text-align:center;color:#6FF;font-size:12px;">'+description+'</span>';
	
}

function rgbToHex(rgb) {
    if (rgb.match(/^#[0-9A-Fa-f]{6}$/)) {
        return rgb;
    }
    var rgbvals = /rgb\((.+),(.+),(.+)\)/i.exec(rgb);
    if (!rgbvals) {
        return rgb;
    }
    var rval = parseInt(rgbvals[1]);
    var gval = parseInt(rgbvals[2]);
    var bval = parseInt(rgbvals[3]);
    var pad = function(value) {
        return (value.length < 2 ? '0' : '') + value;
    };
    return '#' + pad(rval.toString(16)) + pad(gval.toString(16)) + pad(bval.toString(16));
}

function loadCaption(){
  var titles = $("#focusGallery img").each(function(){
	if($(this).css('opacity') == 1) {			   
      caption = $(this).attr("alt");
	}
  });
  $('#spaceCaption').html(caption);
  $('#spaceCaption').show('fast');
}
function destroyCaption(){
  $('#spaceCaption').hide('medium');
}

function toggle(container,container2,picture){
	var c = document.getElementById(container);
	c.innerHTML="<img src='images/"+picture+"' id='"+container2+"' />";
}

function showViewer(picFile,description,name,key){
    
    var mediumfile = picFile.replace(".jpg","_medium.jpg");
	
	document.getElementById('darkBackgroundLayer').style.display='block';
	document.getElementById('closePic').style.display='block';
	document.getElementById('galleryImg').style.display='block';
	document.getElementById('imgDiv').style.display='block';

	
	document.getElementById('imgDiv').innerHTML="<span id='subTitle'>"+name+"</span><br/><a href='images/gallery/"+picFile+"' id='imgGP' class='MagicZoom' title='"+description+"' rel='zoom-position:custom; zoom-width:180px; zoom-height:200px;' ><img src='images/gallery/medium/"+mediumfile+"'/></a><br/><br/>";
	
	document.getElementById('imgGP-big').style.display='block';	
	document.getElementById('galleryDescription').style.display='block';
	document.getElementById('dText').style.display='block';
	document.getElementById('dText').innerHTML=description;
	
//	document.getElementById('galleryControlLeft').style.display='block';
//	document.getElementById('galleryControlLeft').innerHTML="<img id='leftArrow' onclick='javascript:loadLeft(&#39;"+key+"&#39;)' src='images/leftArrowBlue.jpg'/>";
	
//	document.getElementById('galleryControlRight').style.display='block';
//	document.getElementById('galleryControlRight').innerHTML="<img id='rightArrow' onclick='javascript:loadRight(&#39;"+key+"&#39;)' src='images/rightArrowBlue.jpg'/>";
}

function loadLeft(k){
  //need to incriment k and then write picture with the key k+1 to the imgDiv's innerHTML (somehow we need to parse this info using php and send the results back, this will have to be a TRUE AJAX implementation	
}

function loadRight(k){
  //need to incriment k and then write picture with the key k+1 to the imgDiv's innerHTML (somehow we need to parse this info using php and send the results back, this will have to be a TRUE AJAX implementation	
}

function closeViewer(){
	document.getElementById('darkBackgroundLayer').style.display='none';
	document.getElementById('closePic').style.display='none';
	document.getElementById('galleryImg').style.display='none';
	document.getElementById('imgGP-big').style.display='none';
	document.getElementById('imgDiv').style.display='none';
	document.getElementById('galleryDescription').style.display='none';
	document.getElementById('dText').style.display='none';
	document.getElementById('galleryControlLeft').style.display='none';
	document.getElementById('galleryControlRight').style.display='none';
}

function ajaxPreview(filename){
	var divID = "imgPreview";
	var path = "../images/gallery/"+filename;
	document.getElementById(divID).innerHTML="<img class='imgPreview' src='"+path+"'/>";
}

function ajaxPreview2(path,filename){
	var divID = "imgPreview";
	var path = path+filename;
	document.getElementById(divID).innerHTML="<img class='imgPreview' src='"+path+"'/>";
}

function ajaxPreview3(gid){
	/* THIS FXN NEEDS TO LOOK UP THE filename from the gid in the file gallery.xml  I could call a php file as js and change the header so i could query for filename from gid and gallery.xml */
	var divID = "imgPreview";
	var path = "../../images/gallery/"+filename;
	document.getElementById(divID).innerHTML="<img class='imgPreview' src='"+path+"'/>";
}

//////////////////////////////////////// AJAX STUFF //////////////////////////////////////////////////
var xmlHttp

function ajaxContent(container,str,url,xml)
{
	if(str.length==0){
		document.getElementById(container).innerHTML="This Link Appears To Be Broken.<br/><a href='..'/>Click Here To Go Back To Our Home Page.</a>";
		return;
	}
	xmlHttp=GetXmlHttpObject();
	if(xmlHttp==null){
		alert("You browser does not support AJAX, Please try a different internet browser such as Mozilla Firefox.");
	}
	url=url+"?q="+str;
	url+="&x="+xml;
	url+="&sid="+Math.random();
	xmlHttp.onreadystatechange=stateChanged;
	xmlHttp.open("GET",url,true);
	xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
	xmlHttp.send(null);
}

function stateChanged()
{ 
	if (xmlHttp.readyState==4 || xmlHttp.readyState == "complete"){ 
  		document.getElementById("ajaxDiv").innerHTML=xmlHttp.responseText;
	}
}

function ajaxGallery(container,str1,str2,url)
{
	if(str1.length==0){
		document.getElementById(container).innerHTML="This Link Appears To Be Broken.<br/><a href='..'/>Click Here To Go Back To Our Home Page.</a>";
		return;
	}
	xmlHttp=GetXmlHttpObject();
	if(xmlHttp==null){
		alert("You browser does not support AJAX, Please try a different internet browser such as Mozilla Firefox.");
	}
	url=url+"?q="+str1;
	url=url+"&mk="+str2;
	xmlHttp.onreadystatechange=galleryStateChanged;
	xmlHttp.open("GET",url,true);
	xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
	xmlHttp.send(null);
}

function galleryStateChanged()
{ 
	if (xmlHttp.readyState==4 || xmlHttp.readyState == "complete"){ 
  		document.getElementById("miniGalleryPicDiv").innerHTML=xmlHttp.responseText;
	}
}

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)
    {
    try
      {
      xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    catch (e)
      {
      alert("Your browser does not support AJAX!");
      return false;
      }
    }
  }
  return xmlHttp;
}

function getURLVar(urlVarName) {
//divide the URL in half at the '?' 
  var urlHalves = String(document.location).split('?');
  var urlVarValue = '';
  if(urlHalves[1]){
    //load all the name/value pairs into an array 
    var urlVars = urlHalves[1].split('&');
    //loop over the list, and find the specified url variable 
    for(i=0; i<=(urlVars.length); i++){
      if(urlVars[i]){
        //load the name/value pair into an array 
        var urlVarPair = urlVars[i].split('=');
        if (urlVarPair[0] && urlVarPair[0] == urlVarName) {
           //I found a variable that matches, load it's value into the return variable 
           urlVarValue = urlVarPair[1];
        }
      }
    }
  }
  return urlVarValue;   
}
