// JavaScript Document
srchstring="";
function updateImage(i,des) 
{

	var content = '<img class="center" src="' + i + '" alt="' + des + '" title="' + des + '">';
	var d = $("main_image");
	d.innerHTML = content;	
	content =	'<div id="main_zoom_title" ><h3>Title: ' + des + '</h3></div>';
	var t = $("main_zoom");
	t.innerHTML = content;	
}

function searchUpdateImage(i,des,album,artist,tags) 
{

	var content = '<img class="center" src="' + i + '" alt="' + des + '" title="' + des + '">';
	var d = $("main_image");
	d.innerHTML = content;	
	content =	'<div class="detailtable"><div class="leftcol">';
	content = content + 'Title: ' + des + '<br/>';
	content = content + 'Artist: ' + artist + '<br/>';
	content = content + 'Album: ' + album + '<br/>';
	content = content + '<br/></div><div class="rightcol">';
	content = content + 'Tags: '+ tags + '<br/></div></div>';
	var t = $("main_zoom_title");
	t.innerHTML = content;	
}
function photoextra(state) 
{
	if(state.checked)
	{
		var d = $("photoextra");
		var content = '<div class="formrow">';
		content = content +'<div class="leftcol_radio">';
		content = content +'<input class="phototype" type="radio" value="colour" name="phototype" checked "/>';
		content = content +'</div>';
		content = content +'<div class="rightcol_radio">';
		content = content +'<label>Colour</label>';
		content = content +'</div>';
		content = content +'</div>';
		content = content +'<div class="formrow">';
		content = content +'<div class="leftcol_radio">';
		content = content +'<input class="phototype"  type="radio" value="black-and-white" name="phototype"  "/>';
		content = content +'</div>';
		content = content +'<div class="rightcol_radio">';
		content = content +'<label>Black&amp;White</label>';
		content = content +'</div>';
		content = content +'</div>';
		content = content +'<div class="formrow">';
		content = content +'<div class="leftcol_radio">';
		content = content +'<input class="phototype"  type="radio" value="sepia" name="phototype"  "/>';
		content = content +'</div>';
		content = content +'<div class="rightcol_radio">';
		content = content +'<label>Sepia</label>';
		content = content +'</div>';
		content = content +'</div>';
		d.innerHTML = content;	
		//check if phototype button has previously been set - if so set relevant phototype button
		var ptIndex = get_cookie("phototype");
		if (ptIndex)
		{
			var pt = $$("input.phototype");
			pt[ptIndex].checked = true;
		}
	}else {
		var d = $("photoextra");
		content ="";
		d.innerHTML = content;

	}
}
function openBioWin() {

// open the window
bio1 = window.open("bio.html", "Window3", "width=400,height=350,scrollbars=yes,toolbar=no,directories=no"); 
}
/* ============ get a cookie!! ==============*/
function get_cookie(Name) {
//alert("cookiename" + Name);
  var search = Name + "="
  var returnvalue = "";
  if (document.cookie.length > 0) {
    offset = document.cookie.indexOf(search)
    // if cookie exists
    if (offset != -1) { 
      offset += search.length
      // set index of beginning of value
      end = document.cookie.indexOf(";", offset);
      // set index of end of cookie value
      if (end == -1) end = document.cookie.length;
      returnvalue=unescape(document.cookie.substring(offset, end))
      }
   }
  return returnvalue;
}

function getAllCookies() {
  var cookies  = document.cookie.replace(/ /g,"").split(";");
   for (var i = 0; i < cookies.length; i++){
     cookiePair = cookies[i].split("=");
     document.write("["+cookiePair[0]+"] = ["+
                    cookiePair[1]+"]<br><br>");
   }
 }
 function deletecookie(name)
{
    var d = new Date();
    document.cookie = name +"=1;expires=" + d.toGMTString() + ";" + ";";

    alert(document.cookie);
}

 /* ============ URLEncode ==============*/
 function URLEncode (clearString) {
  var output = '';
  var x = 0;
  clearString = clearString.toString();
  var regex = /(^[a-zA-Z0-9_.]*)/;
  while (x < clearString.length) {
    var match = regex.exec(clearString.substr(x));
    if (match != null && match.length > 1 && match[1] != '') {
    	output += match[1];
      x += match[1].length;
    } else {
      if (clearString[x] == ' ')
        output += '+';
      else {
        var charCode = clearString.charCodeAt(x);
        var hexVal = charCode.toString(16);
        output += '%' + ( hexVal.length < 2 ? '0' : '' ) + hexVal.toUpperCase();
      }
      x++;
    }
  }
  return output;
}

/* ============ Set the customer Centre search form values from the session cookie ==============*/
function setSrchForm(username){
	// username cookie
	document.cookie="username=" + username+ "; expires=0; path=/";
	var wrds = get_cookie("formwords");
	if (wrds!="")
	{
		var si = $("search_input");
		si.value = wrds;
	}
// set the image type values in the form	
	var itValues = get_cookie("imagetype");
	//alert(itIndex);
	var it = $$("input.imagetype");
	if (itValues) 
	{
		  i = 1; 
		  while (itValues.indexOf('^') != '-1') 
		  {
			// get the individual set checboxes between ^ sepaerator
			itIndex = itValues.substring(0,itValues.indexOf('^')); 
			it[itIndex].checked = true;
			i++;
			// chunk off the front end value
			itValues = itValues.substring(itValues.indexOf('^')+1, itValues.length);
		  }
	}
	//if photo ticked check and set phototype button if set
	var ptIndex = get_cookie("phototype");
	if (ptIndex)
	{
		p1 = $("p1");
		//display phootype buttons
		photoextra(p1);
	}
	//if orientation previously selected check and set orientation selection
	var otIndex = get_cookie("ornt");
	var ot = $("o_select")
	if (otIndex)
	{
		ot.selectedIndex = otIndex;
	}		
	//if theme previously selected check and set theme selection
	var thIndex = get_cookie("theme");
	var th = $("t_select")
	if (thIndex)
	{
		th.selectedIndex = thIndex;
	}
	//finally see if the lightbox cookie is set and show if it is
	var lbx = get_cookie("lightbox");
	if (lbx)
	{
		showLightbox();
	}
}

/* ============ Set the  session cookie, create search string and submit ==============*/
function doSearch() 
{
	
//get all the form values if they have been set 
    // get search box
	var si = $("search_input");
	var siValue = si.value;
	//alert("top: " +siValue);
	if (si!="")
	{
		document.cookie="formwords=" + siValue; 
	}
	// get Image Types
	var it = $$("input.imagetype");
	var itValue = new Array();
	if (it.length != 0)
	{
		var itIndex = "";
		var c=0;
		for (var i=0; i < it.length; i++)
	   {
		   
		   if (it[i].checked)
			  {
				// store the index number for storing in the cookie
				itIndex  += i + "^";
				// stor the value for later use in the search string
				itValue[c] = it[i].value
				c++;
			  } 
	   }
	   	document.cookie="imagetype=" + itIndex; 
	}
// get Phototypes Types
	var pt = $$("input.phototype");
	if (pt.length != 0)
	{
		var ptIndex = "";
		for (var i=0; i < pt.length; i++)
	   {
		   if (pt[i].checked)
			  {
				// store the index number for storing in the cookie
				ptIndex  = i 
				// stor the value for later use in the search string
				var ptValue = pt[i].value
			  } 
	   }
		document.cookie="phototype=" + ptIndex; 
		//alert("Phototype: " +pt);
	}
// get orientation dropdown box
	var ot = $("o_select")
	var otValue = "";
	if (ot.length != 0)
	{
	// store the index number for storing in the cookie
		var otIndex  = ot.selectedIndex
	// store the value for later use in the search string
		otValue = ot.options[otIndex].value
		document.cookie="ornt=" + otIndex; 
		
	}


// get theme dropdown box
	var th = $("t_select")
	var thValue = "";
	if (th.length != 0)
	{
	// store the index number for storing in the cookie
		var thIndex  = th.selectedIndex
	// store the value for later use in the search string
		thValue = th.options[thIndex].value
		document.cookie="theme=" + thIndex; 
		
	}
// build the search string
//getAllCookies();
	var howmany=0;
	srchstring = "";
	if (itValue.length != 0)
	{
		var sor = "  OR ";

		howmany =itValue.length;
		for (var i=0; i < howmany; i++)
	   {
		   // if first item start parenthesis
		   if (i==0){srchstring += "("};
		   // if only 1 checked or if its the last item unset or variable
		   if (i+1 == howmany){sor = ""};	
		   
		   if (itValue[i] == "photo")
		   {
				srchstring += "(" + itValue[i] + "&" + ptValue  + ")" + sor ;
		   } else {srchstring +=  itValue[i] +  sor ;}
			  
			// last item so set end parenthesis
			if(i+1 == howmany){srchstring += ")";}
	   }
	}
	var ddand= "";
	if (siValue!="" || howmany!=0){ddand = "&";}
	if (otValue != "" && otValue != "either" )
	{
		srchstring +=   ddand + otValue ;	
	} 
/*		else if (otValue != "" && otValue != "either" )
	{
		srchstring +=  "&" + otValue;
	}*/


	
	if (thValue != "" && thValue != "none" )
	{
		if (otValue != "" && otValue != "either" ){ddand="&";}
		srchstring +=  ddand + thValue ;	
	} 
/*		else if (thValue != "" && thValue != "none" )
	{
		srchstring +=  "&" + thValue;
	}*/	
/*	if (thValue != "" && thValue != "none")
	{
		
		srchstring +=  "&" + thValue;
	}*/
	

if(siValue!="")
   {
   		if(itValue.length == 0)
		{
			srchstring = siValue + srchstring;
			
		} else 
		{
			srchstring = siValue + "&" + srchstring ;
			//alert("tst: "+srchstring);
		}
   
   }
	

	// set input value to search
	var sw = $("search_input");
	sw.value = srchstring;
	var sf = $("search_form");
	sf.submit;


}
function infoWinOpen(name,info)
{
	var content = '<div class="infoWin">';
	content += '<h3>Image: ' + name  + '</h3>';
	content += '<strong>Image Information</strong>';
	content += '<p>'+info+'</p>';
	content += '<p><a href="javascript:void(0);" class="closeSticky">Close</a></p>';
	content += '</div>';
	var infoWin = new StickyWin({content: content,position: 'center'});

 
}
/* ============ get a check box value ==============*/
function get_check_value()
{
var c_value = "";
for (var i=0; i < document.orderform.music.length; i++)
   {
   if (document.orderform.music[i].checked)
      {
      c_value = c_value + document.orderform.music[i].value + "\n";
      }
   }
}


function showLightbox()
{
	
	// make lightbox visible
	var lb =$('lightbox');
	lb.style.display="block";
	// set link to hide lightbox
	var lh =$('sl1');
	lhcontent = '<a href="#" onClick="hideLightbox()" >Hide Current Lightbox</a>';
	lh.innerHTML = lhcontent;
	// check to see if a lightbox name is in the cookie lightbox
	var lbc	= get_cookie('lightbox');
	// if not set create a default value
	if (lbc == "")
	{
		lbc = "default";
		//document.cookie="lightbox=" + lbc + "; expires=0; path=/"; 	
		document.cookie="lightbox=" + lbc + "; expires=0; path=/"; 	
	}
	// display cookie name
	var cn = $('lbname');
	cn.style.float='left';
	cn.innerHTML = "<strong>Lightbox: <em>" + lbc + "</em></strong>";
	params = "lightbox=" + lbc;
	var contentPath = "themes/unitedart/showlightbox.php";
	//We can use one Request object many times.
	var req = new Request.HTML
		({ 
		 	url:contentPath, 
			onSuccess: function(html) 
				{
					
					//Clear the text currently inside the results div.
					$('lbcontent').set('text', '');
					//Inject the new DOM elements into the results div.
					$('lbcontent').adopt(html);
					//console.log("show successful");
				},
			//Our request will most likely succeed, but just in case, we'll add an
			//onFailure method which will let the user know what happened.
			onFailure: function(xhr) 
			{
				err = '<h1>updateLightbox Function: The request to update the lightbox failed:</h1> ' ;
				$('lbcontent').adopt(err);
				console.log(err);
			}
		}).send(params);
	
}


function hideLightbox(){
	// set link to show lightbox
	var ls =$('sl1');
	lscontent = '<a href="#" onClick="showLightbox()" >Show Current Lightbox</a>';
	ls.innerHTML = lscontent;
	// hide lightbox
	var lb =$('lightbox');
	lb.style.display="none";
	
}
function addtoLightbox(thumb,imageid) {
	showLightbox();
	// update database and display thumb
	//parameters for the updatelightbox.php
	var lbc	= get_cookie('lightbox');
	var params = "imageid="+imageid; 
	params += "&thumburl=" + thumb;
	params += "&lightbox=" + lbc;
	params += "&desc=a_light_box" ;
	var contentPath = "themes/unitedart/updatelightbox.php";
	//We can use one Request object many times.
	var req = new Request.HTML
		({ 
		 	url:contentPath, 
			onSuccess: function(html) 
				{
								
					
					//Clear the text currently inside the results div.
					$('lbcontent').set('text', '');
					//Inject the new DOM elements into the results div.
					$('lbcontent').adopt(html);
					//console.log("lightbox update successful");
					//console.log(disp);
				},
			//Our request will most likely succeed, but just in case, we'll add an
			//onFailure method which will let the user know what happened.
			onFailure: function(xhr) 
			{
				err = '<h1>updateLightbox Function: The request to update the lightbox failed:</h1> ' ;
				$('lbcontent').adopt(err);
				console.log(err);
			}
		}).send(params);
	
}
function loadLightbox()
{
	
// get load dropdown box
	var lb = $("lbload")
	var lbValue = "";
	if (lb.length != 0)
	{
	// store the index number 
		var lbIndex  = lb.selectedIndex;
	// store the value 
		lbValue = lb.options[lbIndex].value;
		if (!lbValue == "")
		{
			document.cookie="lightbox=" + lbValue + "; expires=0; path=/"; 
		} else 
		{
			var lbm = $('lbmessage');
			lbm.innerHTML = "Error: Select a Light box name to load";
		}
	}
	
}
function savetoLightbox()
{
	
// get load dropdown box
	var st = $("saveto")
	var stValue = "";
	if (st.length != 0)
	{
	// store the index number 
		var stIndex  = st.selectedIndex;
	// store the selected value for later 
		stValue = st.options[stIndex].value;
		current = get_cookie('lightbox');
		//document.cookie="lightbox=" + lbValue; 
	}

}
/*Functions for manage light box page*/
function man_showLightbox()
{

	// check to see if a lightbox name is in the cookie lightbox
	var lbc	= get_cookie('lightbox');
	// if not set create a default value
	if (lbc == "")
	{
		lbc = "default";
		//document.cookie="lightbox=" + lbc + "; expires=0; path=/"; 	
		document.cookie="lightbox=" + lbc + "; expires=0; path=/"; 	
	}
	// display cookie name
	var cn = $('man_lbname');
	cn.style.float='left';
	cn.innerHTML = "<strong>Current lightbox: <em>" + lbc + "</em></strong>";
	params = "lightbox=" + lbc;
	var contentPath = "themes/unitedart/showCustomLightbox.php";
	//We can use one Request object many times.
	var req = new Request.HTML
		({ 
		 	url:contentPath, 
			onSuccess: function(html) 
				{
					
					//Clear the text currently inside the results div.
					$('man_lbcontent').set('text', '');
					//Inject the new DOM elements into the results div.
					$('man_lbcontent').adopt(html);
					//console.log("show successful");
				},
			//Our request will most likely succeed, but just in case, we'll add an
			//onFailure method which will let the user know what happened.
			onFailure: function(xhr) 
			{
				err = '<h1>updateLightbox Function: The request to update the lightbox failed:</h1> ' ;
				$('man_lbcontent').adopt(err);
				console.log(err);
			}
		}).send(params);
	
}
function man_loadLightbox()
{
	
// get load dropdown box
	var lb = $("lbload")
	var lbValue = "";
	if (lb.length != 0)
	{
	// store the index number 
		var lbIndex  = lb.selectedIndex;
	// store the value 
		lbValue = lb.options[lbIndex].value;
		if (!lbValue == "")
		{
			//alert(document.cookie="lightbox=" + lbValue);
			document.cookie="lightbox=" + lbValue + "; expires=0; path=/"; 
			man_showLightbox();
			//alert(getAllCookies());
		} else 
		{
			var lbm = $('lbmessage');
			lbm.innerHTML = "Error: Select a Light box name to load";
		}
	}
	
}
function man_delLightbox()
{
// get delete dropdown box
	var lb = $("lbdelete")
	var lbValue = "";
	if (lb.length != 0)
	{
	// store the index number 
		var lbIndex  = lb.selectedIndex;
	// store the value 
		lbValue = lb.options[lbIndex].value;
		if (!lbValue == "")
		{
			// set params
			params = "lbdelete=" + lbValue ;
			var contentPath = "themes/unitedart/uasecurity/process.php";
			//We can use one Request object many times.
			var req = new Request.HTML
				({ 
					url:contentPath, 
					onSuccess: function(html) 
						{
							$('lbmessage').set('text', 'Lightbox Deleted');
							window.location.reload(true);
							setTimeout("$('lbmessage').set('text', '')", 2000);
							console.log("Lightbox " + lbValue +" deleted");
							//window.location.reload(true);
						},
					//Our request will most likely succeed, but just in case, we'll add an
					//onFailure method which will let the user know what happened.
					onFailure: function(xhr) 
					{
						err = '<h1>updateLightbox Function: The request to delete the image failed:</h1> ' ;
						$('man_lbcontent').adopt(err);
						console.log(err);
					}
				}).send(params);
		} else 
		{
			var lbm = $('lbmessage');
			lbm.innerHTML = "Error: Select a Light box name to delete";
		}
	}
	
}
function delLightboxImage(id)
{

	// check to see if a lightbox name is in the cookie lightbox
	var lbc	= get_cookie('lightbox');
	// set params
	params = "lightbox=" + lbc + "&lbdelimage=" + id;
	var contentPath = "themes/unitedart/uasecurity/process.php";
	//We can use one Request object many times.
	var req = new Request.HTML
		({ 
		 	url:contentPath, 
			onSuccess: function(html) 
				{
					man_showLightbox();
					$('lbmessage').set('text', 'Image Deleted');
					setTimeout("$('lbmessage').set('text', '')", 2000);
					console.log("image " + id +" deleted");
				},
			//Our request will most likely succeed, but just in case, we'll add an
			//onFailure method which will let the user know what happened.
			onFailure: function(xhr) 
			{
				err = '<h1>updateLightbox Function: The request to delete the image failed:</h1> ' ;
				$('man_lbcontent').adopt(err);
				console.log(err);
			}
		}).send(params);

}