// JavaScript Document

jQuery(document).ready(function(){
								
	/*** Navigation ***/
	/** ----------------------------------------------------- **/
											  
	jQuery('#main_menu').superfish({
		delay:       0,
		animation:   { opacity: 'show', height:'show' },
		speed:       1000,
		autoArrows:  true,
		anchorClass : 'sf-with-ul',
		arrowClass  : 'sf-sub-indicator',
		dropShadows: false,
		easing: 'easeOutQuart'
	}); 
	
	jQuery('.widget_pages ul, .widget_categories ul, .widget_nav_menu ul').superfish({
		delay:       0,
		animation:   { opacity: 'show', height:'show' },
		speed:       1000,
		autoArrows:  true,
		anchorClass : 'sf-with-ul',
		arrowClass  : 'sf-sub-indicator',
		dropShadows: false,
		easing: 'easeOutQuart'
	});
	
	lava_lamp();
		
	/*** Slides ***/
	/** ----------------------------------------------------- **/
	
	slides();
		
	/*** Hover Effects ***/
	/** ----------------------------------------------------- **/
		
	jQuery('#feature_controller ul li a').hover(
		function () {
		jQuery(this).find('img').stop(false, true).animate({top: -7},{duration: 350, easing: 'easeInOutBack'});
	},
		function () {
		jQuery(this).find('img').stop(false, true).animate({top: 3},{duration: 350, easing: 'easeInOutBack'});
	})
	
	jQuery('#feature_scroll_left').hover(
		function () {
		jQuery(this).stop(false, true).animate({right: 102},{duration: 350, easing: 'easeInOutBack'});
	},
		function () {
		jQuery(this).stop(false, true).animate({right: 92},{duration: 350, easing: 'easeInOutBack'});
	})
	
	jQuery('#feature_scroll_right').hover(
		function () {
		jQuery(this).stop(false, true).animate({right: 7},{duration: 350, easing: 'easeInOutBack'});
	},
		function () {
		jQuery(this).stop(false, true).animate({right: 15},{duration: 350, easing: 'easeInOutBack'});
	})
	
	jQuery('#start_search_button, #widget_twitter_next, #widget_twitter_prev').hover(
		function () {
		jQuery(this).css({backgroundPosition: 'top left'});
	},
		function () {
		jQuery(this).css({backgroundPosition: 'bottom right'});
	})
	
	jQuery('#widget_twitter_holder').hover(
		function () {
		jQuery(this).css({backgroundPosition: 'top left'});
		jQuery('.twitter_follow_button').stop(false, true).show(400);
	})
	
	jQuery('#widget_twitter_holder').mouseleave(function () {
	
		jQuery('.twitter_follow_button').stop(false, true).hide(400);
		
	});
	
	/*** Contact Form ***/
	/** ----------------------------------------------------- **/
	
	var loader = jQuery('<div id="loader"><span>Sending Message...</span></div>')
		.appendTo('#form_result')
		.hide();
	jQuery().ajaxStart(function() {
		jQuery('#contact_form').hide(250);
		loader.show(250);
	}).ajaxStop(function() {
		loader.hide(250);
	}).ajaxError(function(a, b, e) {
		throw e;
	});
	
	$("#contact_form").validate({
			
		rules: {
			input_name: 'required',
			input_email: {
				required: true,
				email: true
			},
			input_subject: 'required',
			textarea_message: 'required'
		},
		messages: {
			input_name: 'Name is required',
			input_email: {
				required: 'Email is required',
				email: 'Invalid email format'
			},
			input_subject: 'Please specify a subject',
			textarea_message: 'Message is empty'
		},
		errorElement: 'span',
		errorClass: 'error_message',
		errorPlacement: function(error, element) {
			error.appendTo(element.parent('span').prev('label'));
		},
		submitHandler: function(form) {
			jQuery(form).ajaxSubmit({
				target : '#form_result'
			});
		}
			
	});
	
	/*** Miscellaneous ***/
	/** ----------------------------------------------------- **/
	
	jQuery('.basic_style_1 blockquote p').addClass('fix_png');
	
	zebra_table('.archive_table table');
		
	jQuery(window).status='Elegant Real Estate';
	
	jQuery('#widget_holder ul').addClass('parent_drop_downs')
	
	jQuery('#widget_holder ul ul').removeClass('parent_drop_downs');
	
	ds_twitter('designspike', 10, '#widget_twitter_wrapper');
	
	jQuery('#footer_menu li:not(li:last-child)').append('&bull;')
	
});

/*** Functions ***/
/** ----------------------------------------------------- **/

jQuery(function(){ //smoothscroll
					 
	jQuery('.smooth_scroll a').click(function() {
		if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') 
			&& location.hostname == this.hostname) {
				var $target = $(this.hash);
				$target = $target.length && $target || $('[name=' + this.hash.slice(1) +']');
			if ($target.length) {
				var targetOffset = $target.offset().top;
				$('html,body').animate({scrollTop: targetOffset}, 1000);
				return false;
			}
		}
	});
	
});

function zebra_table($object){ 

	jQuery( $object + ' tbody tr:nth-child(odd)').addClass('odd');
	
	jQuery( $object + ' tbody tr').hover(
		function () {
		jQuery(this).addClass('trover');
	},
		function () {
		jQuery(this).removeClass('trover');
	})
  
}

function feature_carousel_initCallback($carousel) {
	
	jQuery('.jcarousel-control a').bind('click', function() {
		$carousel.scroll(jQuery.jcarousel.intval(jQuery(this).attr('rel')));
		$carousel.startAuto(0);
		return false;
	});
	
	jQuery('#feature_scroll_right a, #feature_desc_wrapper a.carousel_desc_next').bind('click', function() {
		$carousel.next();
		$carousel.startAuto(0);
		return false;
	});
	
	jQuery('#feature_scroll_left a, #feature_desc_wrapper a.carousel_desc_prev').bind('click', function() {
		$carousel.prev();
		$carousel.startAuto(0);
		return false;
	});

};

function twitter_carousel_initCallback($carousel) {
	
	jQuery('#widget_twitter_next').bind('click', function() {
		$carousel.next();
		return false;
	});
	
	jQuery('#widget_twitter_prev').bind('click', function() {
		$carousel.prev();
		return false;
	});

};

function ds_twitter($username,$qty,$appendItTo) {

	$.getJSON('http://twitter.com/status/user_timeline/' + $username + '.json?count=' + $qty + '&callback=?', function(data){
	    $($appendItTo).append('<ul id="widget_twitter" class="jcarousel-list" ></ul>');
		$.each(data, function(index, item) {
			$('#widget_twitter').prepend(
			'<li>' +
			'<a href="http://www.twitter.com/' + item.user.screen_name + '"  class="user" >' + item.user.name + '</a>:' +
			 item.text.linkify() +
			'<span class="time">' + relative_time(item.created_at) + '</strong>' +
			'<a href="http://www.twitter.com/' + item.user.screen_name + '" target="_blank" class="read_it_link">Read it on Twitter</a>' +
			'</li>'
			);
		});
		$('#widget_twitter').jcarousel({
			scroll: 1,
			vertical: true,
			auto: 10,
			wrap: 'both',
			animation: 800,
			initCallback: twitter_carousel_initCallback,
			buttonNextHTML: null,
			buttonPrevHTML: null,
			easing: 'easeInOutExpo'
		});
	});
	
	function relative_time(time_value) {
	  var values = time_value.split(" ");
	  time_value = values[1] + " " + values[2] + ", " + values[5] + " " + values[3];
	  var parsed_date = Date.parse(time_value);
	  var relative_to = (arguments.length > 1) ? arguments[1] : new Date();
	  var delta = parseInt((relative_to.getTime() - parsed_date) / 1000);
	  delta = delta + (relative_to.getTimezoneOffset() * 60);
	  
	  var r = '';
	  if (delta < 60) {
		r = 'a minute ago';
	  } else if(delta < 120) {
		r = 'couple of minutes ago';
	  } else if(delta < (45*60)) {
		r = (parseInt(delta / 60)).toString() + ' minutes ago';
	  } else if(delta < (90*60)) {
		r = 'an hour ago';
	  } else if(delta < (24*60*60)) {
		r = '' + (parseInt(delta / 3600)).toString() + ' hours ago';
	  } else if(delta < (48*60*60)) {
		r = '1 day ago';
	  } else {
		r = (parseInt(delta / 86400)).toString() + ' days ago';
	  }
	  
	  return r;
	}
	
	String.prototype.linkify = function() {
		return this.replace(/[A-Za-z]+:\/\/[A-Za-z0-9-_]+\.[A-Za-z0-9-_:%&\?\/.=]+/, function(m) {
			return m.link(m);
		});
	};
	
}
	
function lava_lamp() {
	
	jQuery('#mast_head #main_menu li').addClass('put_lava');
	jQuery('#mast_head #main_menu li li').removeClass('put_lava').addClass('no_lava');
	
	jQuery('#lava_box_holder').width(jQuery('#mast_head').width());
	jQuery('#lava_box_holder').height(jQuery('#mast_head').height());
	
	var style = 'easeOutElastic';
	
	if ( $('#mast_head #main_menu li.current_page_item, #mast_head #main_menu li.current_page_ancestor, #mast_head #main_menu li.current-menu-item, #mast_head #main_menu li.current-menu-ancestor, #mast_head #main_menu li.current-menu-parent, #mast_head #main_menu li.current_page_parent').length == 0 ) {
		
		jQuery('#mast_head #main_menu').prepend('<li class="current_page_item" style="width: 1px;" /></li>');
		jQuery('#lava_box').hide(1);
		
	}
	
	var default_left = Math.round(jQuery('#mast_head #main_menu li.current_page_item, #mast_head li.current_page_ancestor, #mast_head li.current-menu-item, #mast_head li.current-menu-ancestor, #mast_head li.current-menu-parent, #mast_head li.current_page_parent').offset().left - jQuery('#mast_head').offset().left);
	var default_width = jQuery('#mast_head li.current_page_item, #mast_head li.current_page_ancestor, #mast_head li.current-menu-item, #mast_head li.current-menu-ancestor, #mast_head li.current-menu-parent, #mast_head li.current_page_parent').width();
	
	jQuery('#lava_box').css({left: default_left});
	jQuery('#lava_box').css({bottom: 0});
	jQuery('#lava_box').css({width: default_width});
	
	jQuery('#mast_head #main_menu li.put_lava').hover(function () {
																		
		if ( jQuery(this).attr('class') != 'no_lava' ) {
				
				left = Math.round(jQuery(this).offset().left - jQuery('#mast_head').offset().left);
				width = jQuery(this).width();
				
				jQuery('#lava_box').stop(false, true).animate({left: left, width: width},{duration: 1500, easing: style});
				
		}
	
	}).click(function () {
				
		jQuery('#mast_head #main_menu li').removeClass('current_page_item, current_page_ancestor, current_page_ancestor, current-menu-item, current-menu-ancestor, current-menu-parent');	
		
		jQuery(this).addClass('current_page_item');
	
	});
	
	jQuery('#mast_head #main_menu').mouseleave(function () {
																		  
		if (jQuery('#main_menu li.put_lava').is('.current_page_item, .current_page_ancestor, .current-menu-item, .current-menu-ancestor, .current-menu-ancestor, .current-menu-parent')) {
			
			default_left = Math.round(jQuery('#mast_head #main_menu li.current_page_item, #mast_head li.current_page_ancestor, #mast_head li.current-menu-item, #mast_head li.current-menu-ancestor, #mast_head li.current-menu-parent, #mast_head li.current_page_parent').offset().left - jQuery('#mast_head').offset().left);
			default_width = jQuery('#mast_head li.current_page_item, #mast_head li.current_page_ancestor, #mast_head li.current-menu-item, #mast_head li.current-menu-ancestor, #mast_head li.current-menu-parent, #mast_head li.current_page_parent').width();
			
			jQuery('#lava_box').stop(false, true).animate({left: default_left, width: default_width},{duration: 1500, easing: style});
			
		} else {
			
			jQuery('#lava_box').hide(1);
			
		}
		
	});
	
}

/*** Cufon ***/
/** ----------------------------------------------------- **/

Cufon.replace(
				  
	'#feature_desc_wrapper h2 a,' +
	'#feature_desc_wrapper p.carousel_desc_info,' +
	'#feature_desc_wrapper p.carousel_desc_price,' +
	'.post_preview_content .post_listing_info,' +
	'.post_preview .post_preview_title,' +
	'.archive_list_content .post_listing_info,' +
	'.archive_list .archive_list_title,' +
	'.basic_style_1 h1,' +
	'.basic_style_1 h2,' +
	'.basic_style_1 h3,' +
	'.basic_style_1 h4,' +
	'.basic_style_1 h5,' +
	'.comment-author cite,' +
	'.comment-author span.says,' +
	'.sidebar_style_1 .widget_title,' +
	'.footer_style1 li.widget h3,' +
	'.font_geo_sans'
				  
,{ fontFamily: 'Geo Sans', hover: true });

