var baseHref = null;
$(document).ready(function(){
	$('table.listTable')
		.find('tr:even').addClass("even").end()
		.find('tr').hover(
			function(){ $(this).addClass("hover") },
			function(){ $(this).removeClass("hover") }
		);
	baseHref = getBaseHref();
});

var $customerPopup = null;
function addCustomer(callBack){ editCustomer(0, callBack); }
function editCustomer(customerId, callBack){
	return $customerPopup = $('<div><iframe src="customers/customerInfo.php?cid=' + customerId + '" frameborder="0" width="100%" height="98%"></iframe></div>').dialog({
		title: (customerId == 0? 'Add Customer' : 'Edit Customer'),
		modal:true,
		height:430,
		//height: 600,
		width: 825,
		buttons: {
			"OK": function(){ 
				validateCustomer(this, callBack);
			},
			"Cancel": function(){ $(this).dialog("close") }
		}
	});
}

function validateCustomer(popup, callBack){
	var $doc = $($(popup).find('iframe')[0].contentWindow.document);
	if(typeof callBack == "function"){
		$(popup).bind("complete", function(cI, cN){ callBack(cI, cN); }, function(ev){
			var $doc = $($(this).find('iframe')[0].contentWindow.document);
			//console.log($doc.find("#customer_id").val());
			ev.data(
				$doc.find("#customer_id").val(),
				$doc.find("#customer_name").val()
			);
			$(this).dialog('close');
		});
	}
	else{ 
		//$(popup).dialog('close');
	}
	$doc.find('form')[0].submit();
}

function showAddCustomer(){
	addCustomer();
	$customerPopup.dialog('option', 'buttons',{
		"OK": function(){ 
			validateCustomer(this, function(cId, cName){
				$('<option value="' + cId + '">' + cName + '</option>').appendTo('#customer_id')
					.attr('selected', true);
			});
		},
		"Cancel": function(){ $(this).dialog("close") }
	});
}

var editFlag = false;
var newContactCount = 0;
function editContact(){
	
}
function addContact(){
	newContactCount++;
	//$('#contactInfo').after();
}
function getContactCallback(contact){
	
}

function updateOption(){

}

function showButtons(value){
	editFlag = true;
	$('#contacts').attr("disabled", true);
	$("input#deleteContact").val("Remove Contact").show().click(function(){
		
	});
}
/*
var editFlag = false;
function editContact(){
	var contact, value = $("#contacts option:selected").eq(0).val();
	$("#contactInfo div").fadeTo(1, 1);
	$("#contactInfo h2").fadeTo(1, 1);
	
	if(value === "0"){		
		var contact = {
			first_name:"", last_name:"", title:"", phone1:"", phone2:"", email:"", notes:""
		}
		
		getContactCallback(contact);
	}
	else if(value > 0){
		if(editFlag != false){
			$('<div>Please complete or save the current contact before loading a new one</div>').dialog({ modal: true });
			return;
		}
		
		$("#first_name").blur(function(){ });
		$("#last_name").blur(function(){ });
		
		$.getJSON(baseHref + "customers/getContact.php?cid=" + value, function(contact){ getContactCallback(contact); });
	}	
}
function addContact(){
	if(editFlag != false){
		$('<div>Please complete or save the current contact before loading a new one</div>').dialog({ modal: true })
		return;
	}
	editFlag = true;
	$('#contacts').attr("disabled", true);
	
	$('<option>New Customer</option>').val("0").attr("selected", true).appendTo("#contacts");
	$("#first_name").focus().blur(function(){ updateOption() });
	$("#last_name").blur(function(){ updateOption() });
	showButtons(0);
	
	editContact();
}
function getContactCallback(contact){
	$("#contact_id").val(contact.contact_id);
	$("#first_name").val(contact.first_name).keyup(function(){ showButtons(contact.contact_id) });
	$("#last_name").val(contact.last_name).keyup(function(){ showButtons(contact.contact_id) });
	$("#title").val(contact.title).keyup(function(){ showButtons(contact.contact_id) });
	$("#phone1").val(contact.phone1).keyup(function(){ showButtons(contact.contact_id) });
	$("#phone2").val(contact.phone2).keyup(function(){ showButtons(contact.contact_id) });
	$("#email").val(contact.email).keyup(function(){ showButtons(contact.contact_id) });
	$("#notes").val(contact.notes).keyup(function(){ showButtons(contact.contact_id) });
}

function updateOption(){
	// console.log($("#contacts option:selected").eq(0).text($("#first_name").val() + " " + $("#last_name").val()));
	var contact_name = $("#first_name").val() + " " + $("#last_name").val();
	if(contact_name.length > 2){ //if there is at least a first and last initial
		$("#contacts option:selected").eq(0).text(contact_name);
	}
}

function showButtons(value){
	editFlag = true;
	$('#contacts').attr("disabled", true);
	if(value > 0){
		$("input#deleteContact").val("Delete Contact").show().click(function(){
			
		});
		$("input#submitContact").val("Update Contact").show().click(function(){
			
		});
		
	}
	else if(value == 0){
		$("input#deleteContact").val("Cancel").show().click(function(){
			
		});
		$("input#submitContact").val("Save Contact").show().click(function(){
			
		});
	}
}
*/
function getBaseHref(){
	var baseTag = document.getElementsByTagName("base");
	return baseTag[0].href;
}

function doRedirect(url){
	window.location.href = baseHref + url;
}

/*
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
try {
	var pageTracker = _gat._getTracker("UA-304590-4");
	pageTracker._trackPageview();
} 
catch(err) {}
*/