$(document).ready(function() {

/**
 * Infinite Carasoel
 */
$('#banner').infiniteCarousel({
	// Options
	autoRotateSpeed : 8000,
	
	leftRightNavClass : 'banner-nav',
	leftNavClass : 'left',
	rightNavClass : 'right',
	
	dotNavClass : 'banner-nav-dot',
	
	dotNav : false
});

/**
 * Content Modifications
 */
// $('#content h1').addClass('lined').wrapInner('<span />');
$('#content h1').next('h2').addClass('excerpt');

// Remove added classes from listings page
$('#content .agent-listing h1').removeClass('lined').find('span a').unwrap();
$('#content .agent-listing h1').next('h2').removeClass('excerpt');

$('#content #featured-post h1').removeClass('lined').find('span a').unwrap();
$('#content #featured-post h1').next('h2').removeClass('excerpt');

$('#content #the-post h1').removeClass('lined').find('span a').unwrap();
$('#content #the-post h1').next('h2').removeClass('excerpt');

$('.archive #content h1').removeClass('lined').find('span a').unwrap();
$('.archive #content h1').next('h2').removeClass('excerpt');

// Add 'btn' class to inputs of type submit
$('#content input[type=submit]').addClass('btn');

/**
 * Main Navigation
 */
	
// Append span element after first row of links
$('#nav ul:first-child > li').append('<span></span>');

	
/**
 * Sidebar Dropdown Functionality
 */
 
$('#aside .children').wrap('<div class="dropdown" />');
 
// Append span element after first row of links
$('.dropdown-list > li .dropdown').parent().append('<span></span>');

// Behave on span click
$('.dropdown-list li span').click(function() {
		
	// If clicked item is active
	if( $(this).parent().hasClass('active') ) {
	
		// Remove all active classes and close all dropdowns
		$('.dropdown-list > li').removeClass('active');
		$('.dropdown').slideUp();
	
	// Else, it was not active
	} else {
		
		// Remove all active classes and add to clicked item
		$('.dropdown-list > li').removeClass('active');
		$(this).parent().addClass('active');
		
		// Close all dropdowns and open active dropdown
		$('.dropdown').slideUp();
		$(this).prev().slideDown();
		
	}

});// End of on click

/**
 * Agent Site Info Tabs
 */
// Hide all content and fade in first
$('.info .content').hide();
$('.info .content:first-child').show();
	
// Add active class to first nav item
$('.info .info-nav li:first').addClass('active');
	
// Behave on nav click 
$('.info .info-nav li a').click(function() {
	
	// If clicked item is active, do nothing
	if( $(this).parent().hasClass('active') ) {
		
		return false;
		
	// If it's not active, proceed
	} else {
		
		// Get the clocked items href value
		var clicked = $(this).attr('href');
		
		// Toggle active classes
		$('.info .info-nav li').removeClass('active');
		$(this).parent().addClass('active');
		
		// Toggle active content
		$('.info .content').hide();
		$(clicked).show();
		
	}
	
	return false;
	
});

/**
 * Agent Site / Sort Buttons
 */

// Add active class to first nav item
$('#page-utilities .sort ul li:first').addClass('active');

// Add sort arrows and class
$('#page-utilities .sort ul li').addClass('sort-asc').find('a').append('<span></span>');

// Behave on nav click 
$('#page-utilities .sort ul li a').click(function() {
	
	// If clicked item is active, do nothing
	if( $(this).parent().hasClass('active') ) {
		
		// Switch the sort
		$(this).parent().toggleClass('sort-desc');
		$(this).parent().toggleClass('sort-asc');
		
		return false;
		
	// If it's not active, proceed
	} else {
		
		// Toggle active classes
		$('#page-utilities .sort ul li').removeClass('active');
		$(this).parent().addClass('active');
		
	}
	
	return false;
	
});

/**
 * Agent Listing / Search Forms
 */

$('#aside .search-form form input.text').each(function() {
	toggle_form_state(this);
});
$('#aside .search-form form input.text').keyup(function() {
	toggle_form_state(this);
});
$('#aside .search-form form input.text').focus(function() {
	toggle_form_state(this);
});
$('#aside .search-form form input.text').blur(function() {
	toggle_form_state(this);
});

function toggle_form_state(obj) {
	
	var value = $(obj).val();
	
	if (value != '') {
		$(obj).parent().addClass('has-text').parent().addClass('active');
	} else {
		$(obj).parent().removeClass('has-text').parent().removeClass('active');
	}
}

$('#aside .search-form form input.submit').click(function() {
	
	if ( $(this).parent().hasClass('has-text') ) {
		$(this).parent().removeClass('has-text').parent().removeClass('active');
		return false;
	
	} else {
		
		// Ajax request for search goes here
		$(this).prev('.search_term').val('');
		return false;
		
	}
	
});

$(".search-form-form").keypress(function(e) {
  	if (e.which == 13) {
  		$(this).children(".search_button").click();
    	return false;
  	}
});

/**
 * Sticky Blocks
 */

// The sticky element(s)
var sticky = $('.sticky-block');

// Check if selector was found
if ( $('.sticky-block').length ) {

	// Top position and left position
	var topmost_point = sticky.offset().top;
	var left_point = $('#wrapper').offset().left + 20;
	var top_padding = 0;
	var real_top = topmost_point - top_padding;
	
	// Window element
	var the_window = $(window);
	
	// Boolean, if sticky element is fixed
	var is_fixed = (sticky.css('position') == 'fixed');
	
	// Heights of variouse elements
	var sticky_height = sticky.height() + 20 + 'px';
	var footer_height = $('#footer').height() + parseInt($('#footer').css('padding-bottom')) + parseInt($('#footer').css('padding-top')); 
	
	// Set element position to absolute
	sticky.css({'position' : 'absolute'});
	
	// Set min-height of containers
	$('#aside, #content').css({
		'min-height' : sticky_height
	});
	
	// The scroll function
	the_window.scroll(function () {
		
		if (the_window.scrollTop() > real_top) {
			
			if ($.browser.msie && $.browser.version == "6.0") {
				
				sticky.css('top', the_window.scrollTop() + top_padding);
				
			} else if (!is_fixed) {
				
				sticky.css({
					left: left_point,
					top: top_padding,
					position: 'fixed'
				});
				is_fixed = true;
			}
	
		} else {
			if (is_fixed) {
				sticky.css({
					position: 'absolute',
					top: topmost_point,
					left: ''
				});
				is_fixed = false;
			}
		}
	});
	
	// Compensate for browser resizing
	the_window.resize(function () {
	    left_point = $('#wrapper').offset().left + 20;
	    if (is_fixed) {
	   	    sticky.css('left', left_point);	        
	    }
	});

}

	
});// End of Document.Ready
