﻿/// <reference path="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js" />
/* Menu positioning code - mostly for IE */
var menuPositionTimer, menuWindowInterval;
var lastViewportSize;
var lastScrollOffsets;
var WindowChangedCounter = 0;

/*
(function ($) {
	$.fn.contentSlide = function (speed, callback) {
		if (this.is(':animated')) return;
		this.css('position', 'relative')
		var $wrap = $('<div/>')
			.css({ position: 'absolute', bottom: 0 })
			.append(this.children().remove())
			.appendTo(this);

		this.animate({ height: 'toggle' }, speed || 800, function () {
			$wrap.before($wrap.children().remove()).remove();
			if (callback) callback();
		});
	}
})(jQuery);
*/

function updateHCDialog(title, content) {
	$("#HCDialogTitle").html(title);
	$("#HCDialogContent").html(content);
}
function isFixedSupported() {
	if (navigator.userAgent.indexOf("MSIE") > -1) {
		return false;
	} else {
		return true;
	}
}
function showConsumerDialog() {
	$("#HCConsumerInfoDialog").css({ top: $("#HCConsumerInfoDialog").height() + "px", overflow: "" });
	// $("#HCConsumerInfoDialog").css({ height: "350px" });
	$("#HCConsumerInfoDialog").fadeIn();
	fixHCConsumerInfoDialogPosition();
	setTimeout("fixHCConsumerInfoDialogPosition();", 250);
}
function hideConsumerDialog() {
	$("#HCConsumerInfoDialog").fadeOut();
	//Dont, the above effect hides the dialog anyway:  $("#HCConsumerInfoDialog").css({ height: "0px", overflow: "hidden" });
}
function setupDialogForDisplay(menuItemId) {
	if (menuItemId == "saved") {
		updateHCDialog("Your Profile Overview", "<div id=\"ConsumerDataPanel\">Loading...</div>");
		showConsumerDialog();
	} else if (menuItemId == "stats") {
		updateHCDialog("Local Area Stats", "<div id=\"LocalAnalysisResults\">Loading...</div>");
		showConsumerDialog();
	}
}
function fixHCConsumerInfoDialogPosition() {
	if (navigator.userAgent.toLowerCase().indexOf("webkit") > -1) {
		/* Chrome/Safari doesn't support our html structure for the dialog */
		$("#HCConsumerInfoDialog").css({ position: 'absolute', top: ((getViewportSize().height + $(document).scrollTop()) * 0.4) + 'px' });
	}

	var baseTop = 0;
	var baseLeft = 0;

	baseTop = (jQuery("#HCConsumerInfoDialog").offset().top - (jQuery("#HCConsumerInfoDialog").height() * 2.0));
	baseLeft = (jQuery("#HCConsumerInfoDialog").offset().left);
	if (baseTop <= 0) { baseTop = 25; }
	if (baseLeft <= 0) { baseLeft = ((getViewportSize().width * 0.5) - 225); }
	if (getViewportSize().height < 450) { baseTop = 5; }

	jQuery("#HCConsumerInfoDialog").css({ position: "absolute", top: ((getViewportSize().height + $(document).scrollTop()) * 0.33) + "px", left: baseLeft + "px" });

}
function initHCMenuPosition() {
	/* only set the timeout for IE - other browsers support the CSS: 'position: fixed' */
	positionTimer = setTimeout("fixHCConsumerInfoDialogPosition();", 500);
	menuWindowInterval = window.setInterval("checkForBrowserWindowChanges()", 1000);
}
function checkForBrowserWindowChanges() {
	if (!lastViewportSize) { lastViewportSize = getViewportSize(); }
	if (!lastScrollOffsets) { lastScrollOffsets = $(document).scrollTop(); }
	/* check if the values have changed */
	if ((lastViewportSize.height != getViewportSize().height) || (lastScrollOffsets != $(document).scrollTop())) {
		lastViewportSize = getViewportSize();
		lastScrollOffsets = $(document).scrollTop();
		WindowChangedCounter += 1;
		/* Check for excess hits to this function */
		if (WindowChangedCounter > 50) {
			// slow the interval
			window.clearInterval(menuWindowInterval);
			menuWindowInterval = window.setInterval("checkForBrowserWindowChanges()", 2500);
		}
	}
}

//

/*ID, Created, MLSNum, Price, Status, Ad, Cmy, Notes
ID, RevID, DateTime, MLS, Price, Status, Ad, Cmy, Rating, Notes*/
function GetConsumerData() {
	setupDialogForDisplay("saved");
	jQuery("#ConsumerDataPanel").html("<div align='center'>Loading...<br /><img src=\"images/Loading-Glass-Small.gif\" border=0 alt=\"Loading...\" /></div>");
	var strUrl = "AjaxHandler.aspx?wID=" + CurrentAgent.WebID + "&Action=GetConsumerData&Output=JSON";
	var ajaxReq = $.ajax({ url: strUrl, success: GetConsumerData_onAjaxResponse, error: GetConsumerData_onAjaxError, dataType: "text" });
}
function GetConsumerData_onAjaxResponse(response) {
	var strHTML;
	var strJSON = response;
	if (strJSON.indexOf("ConsumerData") > -1) {
		eval(strJSON);
	}

	strHTML = "<div class=\"ConsDataTitle\">Saved Properties</div>\n";
	//ConsumerData.Bookmarks & ConsumerData.PropertyAlerts
	for (var i = 0; i < ConsumerData.Bookmarks.length; i++) {
		strHTML += "	<div class=\"ConsDataRow\">" + ConsumerData.Bookmarks[i][1] + " - MLS#: " + ConsumerData.Bookmarks[i][2] + " - " + ConsumerData.Bookmarks[i][5] + "</div>\n";
	}
	if (ConsumerData.Bookmarks.length <= 0) {
		strHTML += "	<div class=\"ConsDataMsg\">You don't have any saved properties currently.</div>\n";
	} else {
		strHTML += "	<div class=\"ConsDataRow\"><a href=\"BookShow.aspx?wID=" + CurrentAgent.WebID + "\">View your Saved Properties &raquo;</a></div>\n";
	}

	/* Now append the Alerts */
	strHTML += "<div class=\"ConsDataTitle\">Property Alerts</div>\n";
	//ConsumerData.Bookmarks & ConsumerData.PropertyAlerts
	for (var i = 0; i < ConsumerData.PropertyAlerts.length; i++) {
		strHTML += "	<div class=\"ConsDataRow\">" + ConsumerData.PropertyAlerts[i][2] + " - " + ConsumerData.PropertyAlerts[i][6] + " - $" + ConsumerData.PropertyAlerts[i][4] + "</div>\n";
	}
	if (ConsumerData.Bookmarks.length <= 0) {
		strHTML += "	<div class=\"ConsDataMsg\">You don't have any matching Property Alerts currently.</div>\n";
	} else {
		strHTML += "	<div class=\"ConsDataRow\"><a href=\"Prospects.aspx?wID=" + CurrentAgent.WebID + "\">View your Property Alerts &raquo;</a></div>\n";
	}

	jQuery("#ConsumerDataPanel").html(strHTML);
}
function GetConsumerData_onAjaxError(response) {
	jQuery("#ConsumerDataPanel").html('Error: ' + response);
}


function GetLocalStats(PropertyData, Miles) {
	if (typeof PropertyData != "undefined") {
		setupDialogForDisplay("stats");
		if (CurrentProperty.lat == -1 || CurrentProperty.lat == -1.1 || CurrentProperty.lat == 0) {
			/* Property location could not be found... */
			jQuery("#LocalAnalysisResults").html("<div id=\"NotAvailableMsg\">No accurate location information could be found for this property. Please check again later.</div>");
			return;
		}
		jQuery("#LocalAnalysisResults").html("<div align='center'>Loading...<br /><img src=\"images/Loading-Glass-Small.gif\" border=0 alt=\"Loading...\" /></div>");
		var strUrl = "AjaxHandler.aspx?Action=GetLocalStats&Lat=" + PropertyData.lat + "&Lon=" + PropertyData.lon + "&Radius=" + Miles + "&MLS=" + PropertyData.Board + "&Output=HTML&FieldData=Type|" + PropertyData.type + "";
		var ajaxReq = $.ajax({ url: strUrl, success: GetLocalStats_onAjaxResponse, error: GetLocalStats_onAjaxError, dataType: "text" });
	}
}
function GetLocalStats_onAjaxResponse(response) {
	var strHTML = response;
	jQuery("#LocalAnalysisResults").html(strHTML);
}
function GetLocalStats_onAjaxError(response) {
	jQuery("#LocalAnalysisResults").html('Error: ' + response);
}



