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

	CSS on Sails Framework
	Title: Saratoga Homes
	Author: XHTMLized (http://www.xhtmlized.com/)
	Date: January 2011

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

$.fn.extend({
	/* Return #hash string from href in IE */
	hrefHash: function(){
		return $(this).attr('href').substr($(this).attr('href').indexOf('#'));
	},
	
	/* Set equal heights to columns */
	equalHeights: function(){
		var tallest= 0; 
		
		$(this).each(function(){
			if($(this).height() > tallest){
				tallest = $(this).height();
			}
		}).css({'min-height': tallest});
		
		if($.browser.msie && parseInt($.browser.version, 10) === 6) {
			$(this).height(tallest);
			return this;
		}
	}
});


var Engine = {
	enhancements: {
		accordion: function() {
			var accordion = $('.accordion');
			
			$('.content', accordion).hide();
			
			$('.header', accordion)
				.click(function(){
					if(!$(this).parent().is('.active')) {
						
						$('.active', accordion)
							.removeClass('active')
							.find('.content').slideToggle();
						
						$(this).parent().addClass('active');
						
						$(this).next('.content').slideToggle();
					}
				
					return false;
				});
			
			$('.header.expanded').click();
		},
		
		mainNav: function(){
			$('#navigation > ul > li').has('ul').hover(function(){
				$(this).addClass('sub-nav');
			},function(){
				$(this).removeClass('sub-nav');
			});
		},
		
		/*
		tellAFriend: function(){
			$('.tell-friend a').click(function(){
				if(!$('#tell-a-friend').size()){
					$('<div class="hide"></div>')
						.appendTo('#wrapper')
						.load($(this).attr('href') + ' #tell-a-friend',function(){
							$('#tell-a-friend',this).dialog({
								autoOpen: false,
								dialogClass: 'tell-a-friend',
								draggable: false,
								resizeable: false,
								modal: true,
								closeText: 'Close'
							});
							$('#tell-a-friend').dialog('open');
						});
				} else {
					$('#tell-a-friend').dialog('open');
				}
				return false;
			});
		},
		*/
		
		tabs: function() {
			$('.tabs').each(function(){
				var nav = $('.nav', this),
					tabsContent = $('.tabs-content', this),
					id = '';
				
				$('a', nav).click(function(){
					if(!$(this).is('.active')) {
						id = $(this).hrefHash();
						$('.active', nav).removeClass('active');
						$(this).addClass('active');
						
						tabsContent.hide().filter(id).show();
					}

					return false;
				}).filter(':first').click();
			});
		},
		
		rotator: function() {
			$('.rotator').each(function(i){
				var rotator = $(this),
					items = $('.photos', this),
					toolbar = $('<div class="toolbar" />'),
					nav = $('<div class="nav" />').appendTo(toolbar),
					caption = $('<p class="caption" />').appendTo(toolbar),
					ie = $.browser.msie ? true : false;

				toolbar.append()
				toolbar.insertBefore(items);
				
				items
					.before(toolbar) 
					.cycle({ 
					    fx: 'fade',
					    speed: 'slow',
					    pager: nav,
					    delay: 0,
					    before: function() {
					    	if(ie) {
					    		caption.html($(this).find('.caption').html());
					    	} else {
						    	var item = $(this);
						    	caption.fadeOut(function() {
						    		$(this).html(item.find('.caption').html()).fadeIn();
						    	});
					    	}
					    }
					});
			});
		},
		
		mapTips: function(){
			$('.map-tip')
				.hide()
				.append('<a href="#" class="close" title="close"></a>')
				.children('.close')
					.click(function(){
							$(this).parent().hide();
							return false;
						});
			$('.map-marker')
				.mouseenter(function(){
					$('.map-tip').not(':hidden').hide();
					p = $(this).position();
					$(this)
						.next('.map-tip')
							.css('left',p.left + $(this).width() / 2)
							.css('top',p.top)
							.show();
				});
			$('.map-area .map, .map-area .map-state, .map-area img').click(function(){
				$('.map-tip').not(':hidden').hide();
			});
		}
	}
}

$(document).ready(function() {
	$('body').addClass('js');

	Engine.enhancements.accordion();
	Engine.enhancements.mainNav();
	// Engine.enhancements.tellAFriend();
	Engine.enhancements.tabs();
	Engine.enhancements.rotator();
	Engine.enhancements.mapTips();

	// Add press class
	$('.btn-submit-small, .btn-submit-large').mousedown(function() { 
		$(this).addClass('press');
	}).mouseup(function(){
		$(this).removeClass('press');
	});

	// Referral program - equal columns 
	$('.referral-container').find('.form-wrapper, .rules').equalHeights();
});


