/*
	DATE		MODIFIER	COMMENTS
	2011-03-15	MRB			Created document.
*/

$(document).ready(function () { 

	var	highlightedProperties	= { 
									"1": {name: "1551 Broadway",	location: "New York, NY"},
									"2": {name: "2121 K St NW",		location: "Washington, DC"},
									"3": {name: "Battery Place",	location: "New York, NY"},
									"4": {name: "Chatham 44",		location: "New York, NY"},
									"5": {name: "Trump Park Ave",	location: "New York, NY"},
									"6": {name: "1330 Ave of the Americas",	location: "New York, NY"}
	},
	highlightedPropertyIndex	= (Math.floor(Math.random() * (6 - 1 + 1) + 1)).toString(10);

	$("#highlighted_property")
		.addClass("photo_" +  highlightedPropertyIndex)
		.find("#caption p").each(function (index) { 
		
			if (0 == index)	$(this).text(highlightedProperties[highlightedPropertyIndex]["name"]);
			else			$(this).text(highlightedProperties[highlightedPropertyIndex]["location"]);
		});

	// !Enable drop-down menus in primary navigation
	$("#primary_navigation").supersubs({ 
		autoArrows:    true,
		dropShadows:   true,
		extraWidth:  1,
		minWidth:    10,
		maxWidth:    20
	}).superfish();

	// !Enable tooltips on featured properties on the homepage
	if (0 != $("#properties li").length) {
	
		$("#properties li").qtip({
					content: {
						text: function (api) { 
						
								var propertyDetails	= $(api.elements.target).find("p");
	
								return	"<h2>" + propertyDetails.filter("p.name").text() + "</h2>" +
										"<h3>" + propertyDetails.filter("p.location").text() + "</h3>" + 
										"<p>" + propertyDetails.filter("p.description").html() + "</p>" +
										"<p>Financing Amount:</p>" +
										"<p class='list_price'>" + propertyDetails.filter("p.list_price").text() + "</p>" +
										"<p>" + propertyDetails.filter("p.link").html() + "</p>";
						},
						title: {
							text:	false,
							button:	false
						}
					},
/* 					hide: false, // Don't specify a hide event either! */
					hide: {
						delay:		500,
						event:		"mouseleave",
						fixed:		true,
						inactive:	5000
					},
					position: {
						my: "bottom right",
						at: "center center"
					},
					prerender: true,
					show: {
						event: "mouseenter",
						ready: false,
						solo:	true
					},
					style: {
						classes:	'ui-tooltip-light',
						tip:		{
										corner: true,
										height:	15,
										offset: 10,
										width:	20
						}
	
					}
				});
	}

	// !Load the latest rates on the homepage	
	if (0 != $("#us_treasuries").length) {
	
		$.getJSON("/service.php?section=rates&type=treasuries", {}, function (data, textStatus) { 
	
			$("#us_treasuries dd").not(".heading").each(function (index) { 

				var	rate	= data[index];
				
				if ("n/a" == rate)	rate	= "Unavailable";
				else				rate	= (parseFloat(rate).toFixed(3)).toString() + "%";

				$(this).text(rate);
			});
		});
		
		$.getJSON("/service.php?section=rates&type=swaps", {}, function (data, textStatus) { 
	
			$("#swap_spreads dd").not(".heading").each(function (index) { 

				var	rate	= data[index];
				
				if ("n/a" == rate)	rate	= "Unavailable";
				else				rate	= ((parseFloat(rate) * 100).toFixed(0)).toString() + " bps";

				$(this).text(rate);
			});
		});
		
		$.getJSON("/service.php?section=rates&type=libor", {}, function (data, textStatus) { 
	
			$("#libor dd").not(".heading").each(function (index) { 

				var	rate	= data[index];
				
				if ("n/a" == rate)	rate	= "Unavailable";
				else				rate	= ((parseFloat(rate) * 100).toFixed(3)).toString() + "%";

				$(this).text(rate);
			});
		});
	}
	
	var	config	= {
					append:		false,
					page:		0,
					perPage:	8,
					type:		"web",
					searchSite:	true,
					siteURL:	"meridiancapital.com"
	};

	// !Load the 5 most recent tweets on the homepage
	if (1 == $("#recent_tweets").length) {
	
		$("#recent_tweets").twitter({
			from:			"meridiancapllc",
			limit:			3,
/* 			notFoundText:	"No recent tweets found. <a href='http://twitter.com/meridiancapllc' title='Follow Us' target='_blank'>Follow us</a> on Twitter.", */
			notFoundText:	"No tweets within the past 7 days found.",
			replies:		true,
			retweets:		false
		});
	}


	$("#search_form").submit(function (e) {
	
		// Prevent the form from being submitted
		e.preventDefault();

		googleSearch();
	});
	
			$(document).click(function(e)
			{
				if ($(e.target).parents("#results_container").length <= 0)
				{
					$("#results_container").hide();						
				}
			});

	function googleSearch(settings) {
	
		settings		= $.extend({}, config, settings);
		settings.term	= settings.term || $("#query").val();

		// Using the Google site:example.com to limit the search to a specific domain:	
		if (settings.searchSite)
			settings.term	= "site:" + settings.siteURL + " " + settings.term;
	
		// URL of Google's AJAX search API
		var	apiURL				= "http://ajax.googleapis.com/ajax/services/search/" + settings.type + "?v=1.0&callback=?",
			resultsContainer	= $("<div />", {id: "results_container"}).appendTo("body");
			
$("#results_container")
.position({
	at:		"center bottom",
	my:		"center top",
	of:		"#search_form_container",
	offset:	"0 -10"
})
.append($("<div />", {html: '<a href="http://www.google.com/" target="_blank" title="powered by">powered by <img src="http://www.google.com/uds/css/small-logo.png" width="51" height="15" /></a>', id: "credits"}));

		$.getJSON(apiURL, {q: settings.term, rsz: settings.perPage, start: (settings.page * settings.perPage)}, function (r) {
            
			var results = r.responseData.results;
//			$('#more_results').remove();

			if (results.length) {
                
				// If results were returned, add them to a pageContainer div,
				// after which append them to the #resultsDiv:
				
				var resultSet = $("<div />", {className: "result_set"});
				
				for (var i = 0; i < results.length; i++) {
				
					// Creating a new result object and firing its toString method:
					resultSet.append(new result(results[i]) + "");
				}
                
/*
				if (!settings.append) {
				
					// This is executed when running a new search, 
					// instead of clicking on the More button:
					resultsContainer.empty();
				}
*/
                
				resultSet
					.hide().appendTo(resultsContainer)
					.fadeIn("slow");
				
				var	cursor	= r.responseData.cursor;
                
				// Checking if there are more pages with results, 
				// and deciding whether to show the More button:
/*
				if (+cursor.estimatedResultCount > ((settings.page + 1) * settings.perPage)) {
				
					$("<div />", {id: "more_results", text: "More Results"}).appendTo(resultsContainer).click(function () {
					
						googleSearch({append: true, page: (settings.page + 1)});
						
						$(this).fadeOut();
					});
				}
*/
			}
			else {
	
				// No results were found for this search.
				resultsContainer.empty();
				
				$("<p>", {className: "no_results", text: "No Results Were Found!"}).hide().appendTo(resultsContainer).fadeIn();
			}
			
			$("<div />", {id: "b"}).appendTo(resultsContainer);
		});
	};
    
	function result (r) {
	
		// This is class definition. Object of this class are created for
		// each result. The markup is generated by the .toString() method.
	
		var arr = [];
	
		// GsearchResultClass is passed by the google API
		switch (r.GsearchResultClass) {

			case 'GwebSearch':
				arr = [
					'<div class="web_result">',
					'<div class="favicon"><img src="http://www.google.com/s2/favicons?domain=' + r.url + '"></div>',
					'<h2><a href="',r.unescapedUrl,'" target="_blank">',r.title.substring(0, 35),' ...</a></h2>',
					'<p>',r.content.substring(0, 115),' ...</p>',
					'</div>'
				];
			break;
			case 'GimageSearch':
				arr = [
						'<div class="image_result">',
						'<a target="_blank" href="',r.unescapedUrl,'" title="',r.titleNoFormatting,'" class="pic" style="width:',r.tbWidth,'px;height:',r.tbHeight,'px;">',
						'<img src="',r.tbUrl,'" width="',r.tbWidth,'" height="',r.tbHeight,'" /></a>',
						'<div class="clear"></div>','<a href="',r.originalContextUrl,'" target="_blank">',r.visibleUrl,'</a>',
						'</div>'
				];
			break;
            case 'GvideoSearch':
                arr = [
                    '<div class="video_result">',
                    '<a target="_blank" href="',r.url,'" title="',r.titleNoFormatting,'" class="pic" style="width:150px;height:auto;">',
                    '<img src="',r.tbUrl,'" width="100%" /></a>',
                    '<div class="clear"></div>','<a href="',r.originalContextUrl,'" target="_blank">',r.publisher,'</a>',
                    '</div>'
                ];
            break;
            case 'GnewsSearch':
                arr = [
                    '<div class="news_result">',
                    '<h2><a href="',r.unescapedUrl,'" target="_blank">',r.title,'</a></h2>',
                    '<p>',r.content,'</p>',
                    '<a href="',r.unescapedUrl,'" target="_blank">',r.publisher,'</a>',
                    '</div>'
                ];
            break;
        }

		// The toString method.
		this.toString = function () {
		
			return arr.join("");
		}
	};
});
