// JavaScript Document
//Globals
function getAccess(){
	x_check_login(check_login_cb);
}

function check_login_cb(result){
	if(result && result!='badLogin')
		access=1;
	else
		access=0;
}

function preloadMenu(){
	zipfly_on = new Image();
	zipfly_on.src='images/menu/zipfly_on.gif';
	projects_on = new Image();
	projects_on.src='images/menu/projects_on.gif';
	news_on = new Image();
	news_on.src='images/menu/news_on.gif';
	bazaar_on = new Image();
	bazaar_on.src='images/menu/bazaar_on.gif';
}

function bazaarLoad(section, BazaarProductsID){
	getAccess(); //Access Check
	preloadMenu();
	if(section=='products'&&BazaarProductsID){
		BazaarProductImagePreload();
		if(access==1){
			Sortable.create('productMediaThumbs', { ghosting:true, constraint:false, onUpdate: function(){ poststring = Sortable.serialize('productMediaThumbs'); BazaarProductThumbsSort(poststring);}});	
		}
	}
}


function grayOut(vis, onbox, options) {
  // Pass true to gray out screen, false to ungray
  // options are optional.  This is a JSON object with the following (optional) properties
  // opacity:0-100         // Lower number = less grayout higher = more of a blackout 
  // zindex: #             // HTML elements with a higher zindex appear on top of the gray out
  // bgcolor: (#xxxxxx)    // Standard RGB Hex color code
  // grayOut(true, {'zindex':'50', 'bgcolor':'#0000FF', 'opacity':'70'});
  // Because options is JSON opacity/zindex/bgcolor are all optional and can appear
  // in any order.  Pass only the properties you need to set.
  var options = options || {}; 
  var zindex = options.zindex || 50;
  var opacity = options.opacity || 70;
  var opaque = (opacity / 100);
  var bgcolor = options.bgcolor || '#000000';
  var dark=document.getElementById('darkenScreenObject');

  var onbox=document.getElementById(onbox);

	if (!dark) {
    // The dark layer doesn't exist, it's never been created.  So we'll
    // create it here and apply some basic styles.
    // If you are getting errors in IE see: http://support.microsoft.com/default.aspx/kb/927917
    var tbody = document.getElementsByTagName("body")[0];
    var tnode = document.createElement('div');           // Create the layer.
        tnode.style.position='absolute';                 // Position absolutely
        tnode.style.top='0px';                           // In the top
        tnode.style.left='0px';                          // Left corner of the page
        tnode.style.overflow='hidden';                   // Try to avoid making scroll bars            
        tnode.style.display='none';                      // Start out Hidden
        tnode.id='darkenScreenObject';                   // Name it so we can find it later
    tbody.appendChild(tnode);                            // Add it to the web page
    dark=document.getElementById('darkenScreenObject');  // Get the object.
  }
  if (vis) {
    // Calculate the page width and height 
    if( document.body && ( document.body.scrollWidth || document.body.scrollHeight ) ) {
        var pageWidth = document.body.scrollWidth+'px';
        var pageHeight = document.body.scrollHeight+'px';
	} else if( document.body.offsetWidth ) {
      var pageWidth = document.body.offsetWidth+'px';
      var pageHeight = document.body.offsetHeight+'px';
    } else {
       var pageWidth='100%';
       var pageHeight='100%';
    } 
	
	  
    //set the shader to cover the entire page and make it visible.
    dark.style.opacity=opaque;                      
    dark.style.MozOpacity=opaque;                   
    dark.style.filter='alpha(opacity='+opacity+')'; 
    dark.style.zIndex=zindex;        
    dark.style.backgroundColor=bgcolor;  
    dark.style.width= pageWidth;
    dark.style.height= pageHeight;
    dark.style.display='block';
	onbox.style.display='block';
	                         
  } else {
    dark.style.display='none';
	onbox.style.display='none';
  }
}

function closeEdit(){
//	Effect.Fade('projectWorkBox');
	grayOut(false, 'projectWorkBox');
}

function verifyEmail(str) { //Not foolproof but good enough
	return (str.lastIndexOf(".") > 2) && (str.indexOf("@") > 0) && (str.lastIndexOf(".") > (str.indexOf("@")+1)) && (str.indexOf("@") == str.lastIndexOf("@"));
}

function processNewsletter(){
	 var newsletterEmail=document.getElementById("newsletterEmail").value;
	 if(verifyEmail(newsletterEmail)){
		var newsLetter=document.getElementById("newsLetter");
		newsLetter.innerHTML='<img src="images/waiting.gif" width="20" height="20">';
		x_processNewsletterAdd(newsletterEmail, processNewsletter_cb);		
	 }else{
		alert("Please enter a valid email address");
	 }
}
function processNewsletter_cb(result){
	var newsLetter=document.getElementById("newsLetter");
	newsLetter.innerHTML='<span class="lowerList">Thank you for your interest.</span>';
}

function showAddEditNews(newsID){
	document.getElementById("wait").style.display='block';
	x_showAddEditNews(newsID, showAddEditNews_cb);
}

function showAddEditNews_cb(result){
	document.getElementById("projectWorkBox").innerHTML=result;
	document.getElementById("wait").style.display='none';
	grayOut(true, 'projectWorkBox', {'opacity':'70'});	
}

function uploadNews(){
	if(document.getElementById("newsTitle").value=='')
		alert ("You must have a title");
	else{
		document.getElementById("wait").style.display='block';
		grayOut(false, 'projectWorkBox');		
		document.newsForm.submit();
	}
}
function showNews(){
	x_showNews(showNews_cb);
}

function showNews_cb(result){
	document.getElementById("newsLeft").innerHTML=result;
	document.getElementById("wait").style.display='none';	
}

function processDeleteNews(newsID){
	document.getElementById("wait").style.display='block';
	grayOut(false, 'projectWorkBox');		
	x_deleteNews(newsID, processDeleteNews_cb);
}

function processDeleteNews_cb(result){
	showNews();
}

function showAddEditProductMedia(BazaarProductsID, BazaarProductMediaID){
	document.getElementById("wait").style.display='block';
	x_showAddEditProductMedia(BazaarProductsID, BazaarProductMediaID, showAddEditProductMedia_cb);	
}

function showAddEditProductMedia_cb(result){
	document.getElementById("projectWorkBox").innerHTML=result;
	document.getElementById("wait").style.display='none';
	grayOut(true, 'projectWorkBox', {'opacity':'70'});	
}

function processAddEditProductMedia(BazaarProductMediaID){
	grayOut(false, 'projectWorkBox');
	document.getElementById("wait").style.display='block';
	if((document.getElementById("productMediaThumb").value=='')&&(BazaarProductMediaID==0))
		alert('You must have a thumbnail');
	else{
		document.PhotoBoxUpload.submit();
	}
}

function showBazaarProduct(BazaarProductsID, BazaarProductMediaID){
	document.getElementById("wait").style.display='block';
	x_showTheProduct(BazaarProductsID, BazaarProductMediaID, showBazaarProduct_cb)
}
function showBazaarProduct_cb(result){
	
	document.getElementById("dynamicContent").innerHTML=result;
	document.getElementById("wait").style.display='none';	
	Sortable.create('productMediaThumbs', { ghosting:true, constraint:false, onUpdate: function(){ poststring = Sortable.serialize('productMediaThumbs'); BazaarProductThumbsSort(poststring);}});
}

function BazaarProductThumbsSort(sable){
		document.getElementById("wait").style.display='block';			
		x_sortBazaarProductThumbs(sable, RefreshBazaarProductThumbs);
}
function RefreshBazaarProductThumbs(result){
	document.getElementById("wait").style.display='block';
	var BazaarProductsID= document.getElementById("BazaarProductsID").value;
	showBazaarProduct(BazaarProductsID, '', '');
}
function showEditProduct(BazaarProductsID){
	document.getElementById("wait").style.display='block';
	x_showEditProduct(BazaarProductsID, showEditProduct_cb);	
}
function showEditProduct_cb(result){
	document.getElementById("projectWorkBox").innerHTML=result;
	document.getElementById("wait").style.display='none';
	grayOut(true, 'projectWorkBox', {'opacity':'70'});	
}

function uploadProduct(BazaarProductMediaID){
	grayOut(false, 'projectWorkBox');
	document.ProjectBoxUpload.submit();
}

function processDeleteProductMedia(BazaarProductMediaID){
	grayOut(false, 'projectWorkBox');
	document.getElementById("wait").style.display='block';
	x_processDeleteProductMedia(BazaarProductMediaID, processDeleteProductMedia_cb);
}

function processDeleteProductMedia_cb(result){
	var BazaarProductsID= document.getElementById("BazaarProductsID").value;
	document.getElementById("wait").style.display='none';
	showBazaarProduct(BazaarProductsID, '', '');
}


function tellAFriend(){
	var tellAFriendName = document.getElementById("tellAFriendName").value;
	var tellAFriendEmail = document.getElementById("tellAFriendEmail").value;
	var BazaarProductsID= document.getElementById("BazaarProductsID").value;
	if(tellAFriendName=='')
		alert("Please enter your name to tell a friend")
	else if(verifyEmail(tellAFriendEmail)==false){
		alert("Double check your email, it didn't validate")
	}
	else
		x_tellAFriend(tellAFriendName, tellAFriendEmail, BazaarProductsID, tellAFriend_cb);	
}

function tellAFriend_cb(result){
	if(result){
		document.getElementById("tellAFriend").innerHTML=result;
	}
}

function BazaarProductImagePreload(){
	var BazaarProductsID=document.getElementById("BazaarProductsID").value;
	x_getBazaarProductImagesForPreload(BazaarProductsID, BazaarProductImagePreload_cb);
}

function BazaarProductImagePreload_cb(result){
	var images = result.split(', ');
	for(j=1; j<=images.length; j++){
		varname='image_'+j;
			imgName=images[j];
			eval(varname+' = new Image()');
			eval(varname+'.src=\''+imgName+'\'');
		}
}


function showBazaarHomeEdit(){
	document.getElementById("wait").style.display='block';
	x_showBazaarHomeEdit(showBazaarHomeEdit_cb);	
}
function showBazaarHomeEdit_cb(result){
	document.getElementById("projectWorkBox").innerHTML=result;
	document.getElementById("wait").style.display='none';
	grayOut(true, 'projectWorkBox', {'opacity':'70'});	
}

function editBazaarMiscellany(ID){
	document.getElementById("wait").style.display='block';
	x_showEditBazaarMiscellany(ID, editBazaarMiscellany_cb);	
}
function editBazaarMiscellany_cb(result){
	document.getElementById("projectWorkBox").innerHTML=result;
	document.getElementById("wait").style.display='none';
	grayOut(true, 'projectWorkBox', {'opacity':'70'});	
}

function addBazaarMiscellany(){
	document.getElementById("wait").style.display='block';
	x_showAddBazaarMiscellany(addBazaarMiscellany_cb);	
}
function addBazaarMiscellany_cb(result){
	document.getElementById("projectWorkBox").innerHTML=result;
	document.getElementById("wait").style.display='none';
	grayOut(true, 'projectWorkBox', {'opacity':'70'});	
}

function processDeleteBazaarMiscellany(bazaarMiscellanyID){
	document.getElementById("wait").style.display='block';
	grayOut(false, 'projectWorkBox');		
	x_deleteBazaarMiscellany(bazaarMiscellanyID, processDeleteBazaarMiscellany_cb);
}

function processDeleteBazaarMiscellany_cb(result){
	location.reload(true);
}

function showHomeEdit(){
	document.getElementById("wait").style.display='block';
	x_showHomeEdit(showHomeEdit_cb);	
}
function showHomeEdit_cb(result){
	document.getElementById("projectWorkBox").innerHTML=result;
	document.getElementById("wait").style.display='none';
	grayOut(true, 'projectWorkBox', {'opacity':'70'});	
}


