//REMOTE DATA LIGHTBOX FUNCTIONS HERE

var LIGHTBOX_HTML = "Data not set yet";
 
function doDataAction(state,params){
 var msg = '';
   // var myUserID = getUserID();
 var myUserID = 1 //"<?php echo($_SESSION['uid']); ?>" ;// Gets session userid - Not used for this lightbox implementation 
 var myPHP_SID = 1 //"<?php echo(session_id()); ?>"; // Gets  session userid - Not used for this lightbox implementation 
 var sUrl = 'http://www.animal-library.com/lightbox.php'; 
	switch (state){
	 case 'L01': //Add image to lightbox
	   var slid = "x";
	   var pars = 'phpsid=' + myPHP_SID + '&uid='+ myUserID + '&state=' + state + '&img_id=' + params.img_id + '&slid=' + slid  + '&rnd='+ Math.random( );	
	   sUrl = sUrl + "?" + pars;
	   var transaction = YAHOO.util.Connect.asyncRequest('GET', sUrl, callback, null);
	 break;
	 case 'L99': //Get lightbox html 
	   var slid = "x";
	   var pars = 'phpsid=' + myPHP_SID + '&uid='+ myUserID + '&state=' + state + '&rnd='+ Math.random( );	
	   sUrl = sUrl + "?" + pars;
	   
	   var transaction = YAHOO.util.Connect.asyncRequest('GET', sUrl, callback_Lightbox, null); 
	  break;
	 case 'L02': //Remove image from lightbox
	   if (confirm('Sure you want to remove this image from your lightbox?')){
	   	var slid = "x";
	    var pars = 'phpsid=' + myPHP_SID + '&uid='+ myUserID + '&state=' + state + '&auto_id='+ params.auto_id +'&rnd='+ Math.random( );	
	    sUrl = sUrl + "?" + pars;
	    
		var el_id = "lb_id_" + params.auto_id;
		
		var el = document.getElementById(el_id);
	    var r =  el.parentNode.removeChild(el);
		
		var transaction = YAHOO.util.Connect.asyncRequest('GET', sUrl, callback, null); 
		//updateLightbox();
	   }
	  break;
	 case 'L22': //Clear lightbox
	   if (confirm('Sure you want to clear all images from your lightbox?')){
	   	var slid = "x";
	    var pars = 'phpsid=' + myPHP_SID + '&uid='+ myUserID + '&state=' + state +'&rnd='+ Math.random( );	
	    sUrl = sUrl + "?" + pars;
	    var transaction = YAHOO.util.Connect.asyncRequest('GET', sUrl, callback, null); 
		   if (YAHOO.imageaccess.lightboxpanel.lightbox) {
		    YAHOO.imageaccess.lightboxpanel.lightbox.cancel();
		   }
	   }
	  break;
	 case 'L03': //Email lightbox order
	   	var slid = "x";
	   //alert (params);
		
		var pars = 'phpsid=' + myPHP_SID + '&uid='+ myUserID + '&state=' + state + '&notes='+ params.notes+'&address='+params.address +'&email='+ params.email+'&contact='+ params.contact+'&company='+params.company + '&useage=' + params.useage + '&contact_tel='+ params.contact_tel + '&permission='+ params.permission + '&rnd='+ Math.random( );	
	    sUrl = sUrl + "?" + pars;
	    
		
		var transaction = YAHOO.util.Connect.asyncRequest('GET', sUrl, callback, null); 
	  break;
	} 
}

var callback =   
 {   
  success: function(o) {
   alert(o.responseText);
  },   
  failure: function(o) {alert("Problem updating");},   
  argument: ["a", "b", "c"]   
 } 

var callback_Lightbox =   
 {   
  success: function(o) {   
   if (YAHOO.imageaccess.lightboxpanel.lightbox) {
    YAHOO.imageaccess.lightboxpanel.lightbox.setBody(o.responseText);
   }else{
   	alert("could not update lightbox body")
   }
  },   
  failure: function(o) {alert("Could not retreive data");},   
  argument: ["a", "b", "c"]   
 } 
 
 //Functions used by YUI lightbox dialog widget						   
var handleEmailOrder = function() { 
 var d = YAHOO.imageaccess.lightboxpanel.lightbox.getData();  
	var validFlag = true;
	
	//var validArray = [];
	if (d.contact == "") {
		alert("Missing contact details");
		validFlag = false;
	}
	else {
	
	}
	
	if (d.contact_tel==""){
		alert("Missing contact telephone");
		validFlag = false;
	}
	
	if (d.useage==""){
		alert("Missing useage");
		validFlag = false;
	}
	
	if (d.company==""){
		alert("Missing company details");
		validFlag = false;
	}
	
	if (d.email == "") {
		alert("Missing email details");
		validFlag = false;
	}
	
	
	if (d.tandc == false) {
		alert("Please accept terms and conditions");
		validFlag = false;
	}
	
	if (validFlag) {
		if (confirm("Sure you want send order request for these items?")) {
			var obj_data = {'contact': d.contact,'email': d.email,'address': d.address,'company': d.company,'notes': d.notes,'useage': d.useage ,'contact_tel': d.contact_tel ,'permission': d.permission  };
			doDataAction("L03",obj_data);
		    this.cancel();
		}
	}
}; 

var handleClearImages = function() { 
	var r = doDataAction("L22","") ;	
	}; 

var handleCancel = function() {  
	this.cancel(); 
    //this.destroy();
};
	
var onSuccess = function(o) { 
	//objData = YAHOO.lang.JSON.parse(o.responseText);
	 //setSub_Cat_Choice(objData.folio_name);
     //doArtistThumbs(objData.aid);
	  alert(o.responseText);
	  //document.location.reload();
};

var onFailure = function(o) { 
    alert("Your submission failed. Status: " + o.status); 
};

YAHOO.namespace("imageaccess.lightboxpanel");
 function initLightboxPanel(body_html) {
    if (!YAHOO.imageaccess.lightboxpanel.lightbox) {
		// Initialize the temporary Panel to display while waiting for external content to load
		YAHOO.imageaccess.lightboxpanel.lightbox = new YAHOO.widget.Dialog("lightbox", {
			width: "580px",
			fixedcenter: true,
			draggable: true,
			zindex: 4,
			modal: true,
			visible: false,
			buttons: [{
				text: "Clear all",
				handler: handleClearImages,
				id:"clear"
			}, {
				text: "Email order",
				handler: handleEmailOrder
			}, {
				text: "Close lightbox",
				handler: handleCancel
			}]
		});
		YAHOO.imageaccess.lightboxpanel.lightbox.setHeader("View lightbox");
		YAHOO.imageaccess.lightboxpanel.lightbox.setBody(body_html);
		YAHOO.imageaccess.lightboxpanel.lightbox.render(document.body);
		YAHOO.imageaccess.lightboxpanel.lightbox.validate = function() {
			var data = this.getData();
			if (data.firstname == "" || data.lastname == "") {
				alert("Please enter your first and last names.");
				return false;
			} else {
				return true;
			}
		};
;
	// Validate the entries in the form to require that both first and last name are entered
	}
    // Show the Panel
    
	YAHOO.imageaccess.lightboxpanel.lightbox.show();
	updateLightbox();
  }
  
function updateLightbox(){
	doDataAction("L99","");
}
 
