$(document).ready(function () {
	// put anything that needs to run on page load here

	// bind main menu hover events
	$("div#banner div#menu li").hover(
		function () {
			$(this).addClass("hover");
		},
		function () {
			$(this).removeClass("hover");
		}
	);

	// clickable boxes
	$(".clickable").click(function (e) {
				$("a:first", this).click();
		$("input:first", this).click();
		e.stopPropagation();
		return false;
	});

	// style the message box as a popup, dialog message, or hide it
	var ignore = '#find';
	var dialog_popup = '#select_agent_dialog,#save_search_dialog,#addtofavorites,#email_listing,#request_contact';
	if ($(ignore).length == 0 && $("#messagebox").length > 0) {
		if ($("#messagebox").html().trim() == '') {
			$("#messagebox").remove();
		}
		else {
			if ($(dialog_popup).length > 0) {
				$("#messagebox").addClass('messagebox_dialogstyle');
				$("#messagebox").show();
			}
			else {
				$("#messagebox").prepend("<div class=\"title\" onclick=\"$('#messagebox').hide();\" title=\"Click to close\"></div>");
				$("#messagebox").addClass('messagebox_popupstyle');
				$("#messagebox").show();
			}
		}
	}

	// hidden global login
	$("#global_login").click(function () {
		window.location = "http://www.skylineproperties.com/siteadmin/global/index.php";
	});
});
 
 /*
 * Popup boxes (EVIL!)
 */
function popupSettings(classSelector, loggedIn)
{
	// popup dimensions
	if (classSelector.indexOf('savesearch') > -1)
	{
		this.width = 350;
		this.height = 475;
	}
	if (classSelector.indexOf('addtofavorites') > -1)
	{
		this.width = 350;
		this.height = 475;
	}
	if (classSelector.indexOf('requestcontact') > -1)
	{
		this.width = 450;
		this.height = 550;
	}
	if (classSelector.indexOf('emailproperty') > -1)
	{
		this.width = 450;
		this.height = 550;
	}
	if (classSelector.indexOf('selectagent') > -1)
	{
		this.width = 300;
		this.height = 450;
	}
	if (classSelector.indexOf('findhelp') > -1)
	{
		this.width = 780;
		this.height = 650;
	}
	if (!loggedIn && classSelector.indexOf('login') > -1)
	{
		this.width = 790;
		this.height = 650;
	}

	// window attributes - same for all popups in this theme
	this.windowAttributes = 'toolbar=0,'
			  			  + 'location=0,'
			 			  + 'directories=0,'
			  			  + 'status=0,'
			  			  + 'menubar=0,'
			 			  + 'scrollbars=1,'
			 			  + 'resizable=1,'
						  + 'copyhistory=0,'
						  + 'width=' + this.width + ','
						  + 'height=' + this.height;
}
function popupify(loggedIn, context)
{
	// add a click event to every tag that has a class of `popupify`
	$(".popupify", context).click(function(e)
	{
		console.log(e.currentTarget.nodeName);
		// get the settings for this popup
		var popSet = new popupSettings($(this).attr("class"), loggedIn);
		// open the window using the href from the a tag
		window.open($(this).attr('href'), $(this).attr('id'), popSet.windowAttributes);
		
		// stop the a tag from doing it's default behavior
		e.stopPropagation();
		return false;
	});
}