$(document).ready(function(){
	formatRows();
	
	if($('#bnSubmit').length > 0 && $('#searchContainer').length > 0)
	{
		$('#bnSubmit').click(function() {
			$.get('/lib/ajax/getSearchResults.php', $('#frmAdvSearch').serialize(), function(data) {
				var html = '';
				
				$('#searchContainer').html(data);
				$('#hdShowTags').val('');
			});
		});
		if($('#hdShowTags').val() != '')
		{
			$('#bnSubmit').click();
			$('#searchCriteria').html("<h3>Sessions tagged with '"+ $('#hdShowTags').val() + "'.</h3>");
		}
		if($('#hdSearch').val() != '')
		{
			$('#bnSubmit').click();
		}
	}
	
	if($('#frmAdvSearch #tbDate').length > 0)
	{
		$('#tbDate').datepicker();
	}
	
	addCorners();
	linkEditWindows()
});

function formatRows(){
	$(".grid > tbody > tr:even").attr("class", "normalRow");
	$(".grid > tbody > tr:odd").attr("class", "altRow");
	//$(".tags > li > a:odd").css("color", "#496cb2");
	$(".button").corner("round 5px");
	$(".gridButton").corner("round 3px")
}

function updateSessionDialog(id, popupTitle)
{
	$.get('/lib/ajax/getSessionById.php', {id: id}, function(data) {
		$('#lightBox').html(data);
		$('#lightBox').dialog( 
			"option", "buttons", { 
				"Close": function() { $(this).dialog("close"); },
				"Print": function() { $("#ifPrint").attr("src", "/lib/ajax/getSessionById.php?print=true&id=" + id);},
				"Email to a Friend": function() { openEmail(id); }
			}
		);
		$("#lightBox").dialog({ title : popupTitle }).dialog('open');
	});
}

function openEmail(id){
	$.get('/lib/ajax/getEmailForm.php', {sessionID: id}, function(data) {
		$('#lightBox').html(data);
	});
}

function addCorners(){
	$(".content-white-box").corner("round 7px").parent().css('padding', '1px').corner("round 7px");
	$(".red-box").corner("round 7px").parent().css('padding', '1px').corner("round 7px");
	$(".black-box").corner("round 7px").parent().css('padding', '1px').corner("round 7px");
	$(".white-box").corner("round 7px").parent().css('padding', '1px').corner("round 7px");
	$(".left-white-box").corner("round 7px").corner("round 7px").parent().css('padding', '1px').corner("round 7px");
	$(".right-white-box").corner("round 7px").corner("round 7px").parent().css('padding', '1px').corner("round 7px");
	$(".right-orange-box").corner("round 7px").corner("round 7px").parent().css('padding', '1px').corner("round 7px");
	$(".orange-box").corner("round 5px");
	$(".flash-white-box").corner("round 7px").parent().css('padding', '1px').corner("round 7px");
	$(".center-white-box").corner("round 7px").parent().css('padding', '1px').corner("round 7px");
	$(".front-page-white-box").corner("round 7px").parent().css('padding', '1px').corner("round 7px");
	$(".menu-red-box-leaf").corner("round 5px");
	$(".menu-red-box-collapsed").corner("round 5px");
	$(".menu-red-box-expanded").corner("round 5px");
	$(".front-page-red-box").corner("round 7px").parent().css('padding', '1px').corner("round 7px");
	$(".general-inner").corner("round 7px").parent().css('padding', '1px').corner("round 7px");
}

function linkEditWindows(){
	$("a.editWindow").click(function(){
		window.open($(this).attr("href"), 'editWindow');
		return false;
	});
}


function emailToFriend(){
	if($("#frmEmail").valid()){
		$.ajax({
			type: "POST",
			url: "email.php",
			data: {
				"txtYourName": $("#txtYourName").val(),
				"txtYourEmail": $("#txtYourEmail").val(),
				"txtFriendsName": $("#txtFriendsName").val(),
				"txtFriendsEmail": $("#txtFriendsEmail").val(),
				"hdnSendID": $("#hdnSendID").val()
			},
			success: function(data){
				loadSession($("#hdnSendID").val());
			},
			error: function(obj, text, error){
				// alert(text);
				// alert(error);
			},
			dataType: "html"
		});
	}
}

function loadSession(id){
	$.get('/lib/ajax/getSessionById.php', {id: id}, function(data) {$('#lightBox').html(data);});
}
