/*******************************************************************************

 CSS on Sails Framework
 Title: New Orleans
 Author: XHTMLized (http://www.xhtmlized.com/)
 Date: August 2011

 *******************************************************************************/

// // DD_belatedPNG fix for IE6
// if( typeof DD_belatedPNG !== 'undefined') {
	// var PNG_fix_selectors = ['.navigation-wrapper', '.more-link-arrow span', '.meta-date', '#social-boxes h3' , '.find-home-form', '#testimonial', '.more-link-arrow', '.backward', '.forward', '.site-name'];
	// DD_belatedPNG.fix(PNG_fix_selectors.join(','));
// }

$(document).ready(function() {

	// Miscelanius
	Misc.init();
	// Map script
	Engine.init();
});
var Misc = {
	init : function() {
		var li = $('li');
		li.each(function() {
			var This = $(this);
			if(This.is(':first-child'))
				This.addClass('first');
			if(This.is(':last-child'))
				This.addClass('last')
		});
		//var counter = 0; 
		$('.floorplan-container').css({
			width: $('.floorplan-container > li').size() * 187
		})
		//console.log($('.floorplan-container > li').size());
		
		// $('.floorplan-container .floorplan_item').each(function(){
			// counter++;
			// console.log(counter);
		// })
// 		
		
		/*
		 * This javascript controls the opening and closing of the primary nav.  It's a bit
		 * difficult to follow.  The tricky part is the $('#navigation a') sector, which can
		 * either be referencing the link at the top of the primary nav, or one of the menu
		 * item links.  This means that when you mouse out of the top-nav link, the hovered
		 * class is removed and immediately re-added by the menu item's mouse hover.  At 
		 * least that's what I think is going on.  It's awefully difficult to follow.
		 */
		
		$('#navigation a').mouseenter(function() {
			
			$(this).parent('li').addClass('hovered').prev().removeClass('form-hovered');
			$(this).parents('.new-home').addClass('form-hovered');
			
			// Added by GL
			// Close drop down list on new home locator tab navigation
			//$('#state1').selectmenu('close');
			//$('#region1').selectmenu('close');
		});
		
		$('#navigation a').mouseleave(function(){

			// Temporarily removed by Bret so that I can more easily work with the CSS
			$(this).parent('li:not(.new-home)').removeClass('hovered');
			//$(this).parent('li').addClass('hovered').prev().removeClass('form-hovered');
			
			// Added by GL
			// Close drop down list on new home locator tab navigation
			//$('#state1').selectmenu('close');
			//$('#region1').selectmenu('close');
		})
		
		$('#navigation li.new-home').mouseleave(function(e){
			$(this).removeClass('hovered');
		});
		
		$('#navigation li.new-home .submenu').mouseleave(function(e){
			$(this).parent('.new-home').removeClass('hovered');
		});
		
		
		$('#navigation li.new-home').mouseenter(function(){
			/*
			$('<div/>').css({
			    'width':$('body').width(),
			    'height' :$('body').height()
			}).appendTo('body').attr('id', 'overlay');
			*/
		});
		
		$('#navigation li.new-home').mouseleave(function(){
			$(this).removeClass('form-hovered');
		});
		
		$('#find-home-navigation-form').mouseleave(function(e){
			$('#find-home-navigation-form').parents('.new-home').removeClass('form-hovered');
		});
		
		$('h1.site-name').mouseenter(function(e){
			$('#find-home-navigation-form').parents('.new-home').removeClass('form-hovered');
		});
		
		$('#find-home-navigation-form').mousemove(function(e){
			var x = e.pageX - this.offsetLeft;
	        var y = e.pageY - this.offsetTop;			
			$('#find-home-navigation-form').attr('mouse_x', x);
			$('#find-home-navigation-form').attr('mouse_y', y);

		});
		
		
		$('.find-home-form .close:visible').click(function(){
			$(this).closest('li').removeClass('form-hovered');
		})
		
		$('#sidebar .row:first-child').addClass('first');
		
		$('.area-info:visible').click(function(e){
			e.preventDefault();
			$('.area-general-info .about').toggle();
		})
		
		$('#sidebar .options:visible').click(function(e){
			e.preventDefault();
			var This = $(this);
			var container = $('#more-search');
			if(container.is(':visible')){
				This.removeClass('less').html("More Options<span></span>");
				$(container).slideUp();
			}
			else{
				This.addClass('less').html("Less Options<span></span>");
				$(container).slideDown();	
			}
		})
		
		$('.ui-selectmenu-menu').hover(function(){
			in_select_menu = 1;
			
		},
		function(){
			in_select_menu = 0;
			alert('test');
		});
		
		// Comment out by GL
		// Move to /js/site/homepage/homepage.js
		// Move to /js/site/microsites/microsites.js
		/*
		if($('#sliderNav').is(':visible')) {
			$('#sliderNav').tabs('#slider .panes > .pane',{
			rotate: true
			}).slideshow({
				clickable : false,
				next : '.next',
				prev : '.prev'
			});
		
			var api = $('#sliderNav').data('tabs');
			$('#slider .backward').click(function(e) {
				e.preventDefault();
				api.prev();
			})
			$('#slider .forward').click(function(e) {
				e.preventDefault();
				api.next();
			})
			
			
			
			$('#sliderNav a').hover(function(){
				// hover
				var This = $(this);
				if(!This.hasClass('current')){
					This.children('.thumbnail').css({
						left: 0
					})
				}
			},
			function(){
				var This = $(this);
				if(!This.hasClass('current')){
					This.children('.thumbnail').css({
						left: -99999
					})
				}
			}).click(function(){
				$('#sliderNav .thumbnail').css({
						left: -99999
					})
			})
		}
		*/
		
		// Comment out by GL
		/*
		if($('#home-description-nav').is(':visible')) {
			$('#home-description-nav ul').tabs('#home-description .panes > .pane',{
			rotate: true,
			initialIndex: 1
			}).slideshow({
				clickable : false
			});
		}
		*/
		
		// Comment out by GL
		// Move to /js/site/homepage/homepage.js
		/*
		$('.select').selectmenu({
				style:'dropdown', 
				width: '164px',
				menuWidth: '164px'
			
		});
		*/
		
		$('.select200').selectmenu({
				style:'dropdown', 
				width: 204,
				menuWidth: 204
			
		});
		
		$('.select80').selectmenu({
				style:'dropdown', 
				width: 84,
				menuWidth: 84
			
		});
		
		
		
		// $('.grey-select').selectmenu({
				// style:'dropdown', 
				// width: 125,
				// menuWidth: 1
// 			
		// });

		// Comment out by GL
		// Move to /js/site/quick-interest-list.js
		/* overlay */
		/*
		var triggers = $(".sign-up a, .pane-content a.zoom").overlay({
		
			// some mask tweaks suitable for modal dialogs
			mask: {
				color: '#000',
				loadSpeed: 200,
				opacity: 0.6,
				zIndex: 700
			},
			closeOnClick: true
		});		
		*/
		
		if ($.browser.msie && $.browser.version.substr(0,1)<7) {}
		else {
			/*scroll pane*/
			if(typeof $.fn.jScrollPane !== 'undefined') {
				$(function() {
					
					// Added by GL
					// Calculate the width of floorplan container
					var temp = $('.floorplan-container').find('li.floorplan-item').length;
					temp = 185 + 188 * (temp - 1);
					$(this).find('.floorplan-container').css('width', temp + 'px');
					
					$('.scroll-pane').jScrollPane({ showArrows: "true", horizontalGutter: 26, horizontalDragMinWidth: 215, horizontalDragMaxWidth: 215 });
				});		
			}
		}
	}
}

function close_finding_nav()
{
	//if($('.ui-selectmenu-menu').css('visibility') == 'hidden')
	//{
		$('#find-home-navigation-form').parent('.new-home').removeClass('form-hovered');
	//}
}

var Engine = {
	init : function() {
		this.map();
	},
	map : function() {
		var markers = $('#map_canvas .markers > li').remove();
		// console.log(markers.size())
		if(markers.size() > 0) {
			var latlng = new google.maps.LatLng(parseFloat(markers.eq(0).data('latitude')), parseFloat(markers.eq(0).data('longitude')));
			var mapOptions = {
				zoom : 10,
				center : latlng,
				mapTypeId : google.maps.MapTypeId.TERRAIN
			};
			var map = new google.maps.Map($("#map_canvas").get(0), mapOptions);

			/* Tooltip box */
			var ib = new InfoBox({
				alignBottom : true,
				boxClass : 'marker',
				closeBoxURL : '_ui/images/common/close.png',
				enableEventPropagation : true,
				pixelOffset : new google.maps.Size(-204, -47)
			});

			/* Numbered markers with labels */
			markers.each(function(index) {
				var marker = $(this);
				var gmarker = new MarkerWithLabel({
					position : new google.maps.LatLng(parseFloat(marker.data('latitude')), parseFloat(marker.data('longitude'))),
					map : map,
					icon : "_ui/images/common/ico_marker.png",
					labelContent : index + 1,
					labelAnchor : new google.maps.Point(7, 29)
				});

				google.maps.event.addListener(gmarker, 'click', function() {
					if(gmarker.getPosition() == ib.getPosition() && !ib.isHidden_) {
						ib.hide();
					} else {
						var c = '<div class="tooltip" id="g-' + marker.attr('id') + '">' + $('.tooltip',marker).html() + '</div>';
						ib.setContent(c);
						ib.open(map, this);
						ib.show();
					}
					map.setCenter(gmarker.getPosition());
				});
			});
		}
	}
}

if ($.browser.msie && $.browser.version.substr(0,1)<7) {}
else{
	/* hack for scrollpane to work correctly with tabs*/
	$.tools.tabs.addEffect("default", function(tabIndex, done) {
	
		this.getPanes().css({position: "absolute", left: "-10000px" }).eq(tabIndex).css({position: "relative", left: "0" });
		
		done.call();
	});	

}

// Test for outdated browser 

var $buoop = {vs:{i:7,f:4,o:10.6,s:3.2,n:9}};

$buoop.ol = window.onload;

window.onload=function()
{ 
    try {if ($buoop.ol) $buoop.ol();}catch (e) { } 
    var e = document.createElement("script"); 
    e.setAttribute("type", "text/javascript"); 
    e.setAttribute("src", "http://browser-update.org/update.js"); 
    document.body.appendChild(e); 
}
