// Initialize ajax loader images
if(loaderImgL==null){ var loaderImgL = "<div id=\"loader\"><img src='inc/loading.gif' width='32' height='32' alt='Loading...' /></div>"; }
if(loaderImgS==null){ var loaderImgS = "<div id=\"loader\"><img src='inc/loading_s.gif' width='16' height='16' alt='Loading...' /></div>"; }

$(function() {
	updateStyles();
	
	// global loading spinner
	$('#spinner').hide()
		.ajaxStart(function() { $(this).show(); })
		.ajaxStop(function() { $(this).hide(); });
		
	// Colorbox global loader listener
	$(document).bind('cbox_complete', function(){
	    updateStyles();
	});
});

submitForm = function(div,url,formName){
	var dataString = $("#"+formName).serialize();
	$("#"+div).fadeTo('slow',0.5);
	$.ajax({  
		type: "POST", url: url, data: dataString,  
		error: function(msg,msg2,errorMsg){
			$("#"+div).fadeTo('slow',1.0);
			alert('error returned in call to '+url+': '+errorMsg);
		},
		success: function(msg) {
			$("#"+div).html(msg);
			$("#"+div).fadeTo('slow',1.0);
		}
	});  
	return false;
}
submitFormModal = function(url,formName,evalFunctions){
	var dataString = $("#"+formName).serialize();
	$.ajax({  
		type: "POST", url: url, data: dataString,  
		error: function(msg,msg2,errorMsg){
			alert('error returned in call to '+url+': '+errorMsg);
		},
		success: function(msg) {
			try{ $.colorbox.close(); }catch(e){}
			try{ dialogClose(); }catch(e){}
			try{ eval(evalFunctions); }catch(e){}
		}
	});  
	return false;
}
submitFormEval = function(url,formName,evalFunctions){
	var dataString = $("#"+formName).serialize();
	$.ajax({  
		type: "POST", url: url, data: dataString,  
		error: function(msg,msg2,errorMsg){
			alert('error returned in call to '+url+': '+errorMsg);
		},
		success: function(msg) {
			try{ eval(evalFunctions); }catch(e){}
		}
	});  
	return false;
}
loadContent = function(div,url){
	//alert('loading div ('+div+') with url ('+url+')');
	$("#"+div).fadeTo('fast',0.5);
	$("#"+div).load(url, function(response) {
		//alert('performing load of "'+url+'" into div "'+div+'"...\nRESPONSE: '+response);
		$("#"+div).fadeTo('fast',1.0);
		updateStyles();
	});
}
loadContentToID = function(id,url){
	$(id).html(loaderImgL).load(url, function(){
		updateStyles();
	});
}
// List helpers
loadTable = function(div,url,title){
	$(div).html(loaderImgL).load(url, function(){
		updateStyles();
	});
	if(div+"Title"){ $(div+"Title").html(title); }
}
// Style
updateStyles = function(){
	$(".modal").colorbox({ overlayClose:false, opacity:0.4, maxWidth:850, initialWidth:80, initialHeight:80 }); // Create needed dialogs
	$(".inlinemodal").colorbox({ inline:true, overlayClose:false, opacity:0.4, maxWidth:850, initialWidth:80, initialHeight:80 }); // Create needed dialogs
	$("[rel=tooltips]").twipsy({ "placement":"right", "offset":5 }); // initialize tooltips
	$("[rel=guide]").twipsy({ "placement":"left", "offset":5, "html":true, "trigger":"manual" }); // initialize tooltip guide
}
// View/edit dialog
openEditCompDialog = function(xItemID){
	var xTitle = "Edit Component";
	var loadURL = "dialog/component_edit.php?id="+xItemID;
	var dialogH = 640;
	var dialogW = 820;
	// load remote content
	var dialogDiv = '<div style="display:hidden;" id="dialogDiv"></div>';
	var dialog = $(dialogDiv).appendTo('body');
    if( activeDialog && activeDialog.dialog("isOpen") ){ return; }
	activeDialog = dialog.load(
        loadURL, 
        {},
        function (responseText, textStatus, XMLHttpRequest) {
			dialog.dialog({
				title: xTitle,
				resizable: true, autoOpen: true, modal: true,
				height: dialogH, width: dialogW,
				open: function(){
					$(this).scrollTop(0);
					$("[rel=tooltips]").twipsy({ "placement":"right", "offset":5, "html":true }); // initialize tooltips
					$("[rel=guide]").twipsy({ "placement":"left", "offset":5, "html":true, "trigger":"manual" }); // initialize tooltip guide
				},
				close: function(event, ui) {
					$(this).dialog("destroy").remove();
					activeDialog = "";
				}
			});
        }
    );
}
dialogClose = function(){
	if( activeDialog.dialog("close") ){ activeDialog = ""; }
}
dialogDeleteClose = function(){
	if( activeDeleteDialog.dialog("close") ){ activeDeleteDialog = ""; }
}
/*
fadeIn = function(div) {
	if($(div).fade){ $(div).fade({ duration: 0.3, from: 0.0, to: 1 }); }
	$('shipmentsBox').show();
}
*/