// Perform initial setup tasks when DOM is ready
$(document).ready( function() {
	
	// Center loading dialog
	$("#loading").css('top','169px').center({vertical:false});
	
		// uncover - curr slide moves off next slide
// fadeZoo
// cover - curr slide covered by next slide
$.fn.cycle.transitions.cover2 = function($cont, $slides, opts) {
	var d = opts.direction || 'right';
	var w = $cont.css('overflow','hidden').width();
	var h = $cont.height();
	opts.before.push(function(curr, next, opts) {
		$.fn.cycle.commonReset(curr,next,opts);
		if (d == 'right') {
			opts.cssBefore.left = -60;
			opts.cssBefore.opacity = 0;
		}
		else if (d == 'up')
			opts.cssBefore.top = h;
		else if (d == 'down')
			opts.cssBefore.top = -h;
		else
			opts.cssBefore.left = w;
	});
	opts.animIn = { left: 0, top: 0, opacity: 1};
	opts.animOut = { opacity: 1 };
	opts.cssBefore = { top: 0, left: 0, opacity: 0 };
};

	// Homepage slideshow
	if($('#slideshowItems').length) {
		
		
		$('#slideshowItems').cycle({ 
 	  		speed: 1000, //Fading-/Sliding-Speed in milliseconds or keywords (slow, normal or fast) (Default: 'normal'), 
   			timeout: 6000, //Time between the fades in milliseconds (Default: '2000'), 
			fx:        'cover2',
			prev:   '#slideshowPrev', 
    		next:   '#slideshowNext'
  		}); 
		
		$("#slideshowRolloverDesignBuild").css('opacity',0);
		 $("#slideshowRolloverShowhomes").css('opacity',0);
		
		//Image hover functions
		 $(".showHomesRollover").hover(function () {
			  $("#slideshowRolloverShowhomes").stop().animate({
				bottom : '25px',
				opacity: 1
			  }, 250);
		 }, 
		 function () {
			  $("#slideshowRolloverShowhomes").stop().animate({
				bottom : '-96px',
				opacity: 0
			  }, 250);
		 });
		 
		 
		 //Image hover functions
		 $(".designBuildRollover").hover(
		function () {
			  $("#slideshowRolloverDesignBuild").stop().animate({
				bottom : '25px',
				opacity: 1
			  }, 250);
		 }, 
		 function () {
			  $("#slideshowRolloverDesignBuild").stop().animate({
				bottom : '-96px',
				opacity: 0
			  }, 250);
		 });
		
	}
	/*
	$("#slideshowLinks a").hover(function(){
		$(".rollover").show();									  
	});*/
	
	

	

	// If we're on homes page
	if($("#homes").length) {
		$('.homesImages a').click(function(){
			// Display loading dialog;
			$("#loading").css('top','169px').css('margin-top','0px').css('width','48px').css('text-align','center').center({vertical:false});
			$("#loading").show();
			// Build request
			var imageID = $(this).attr('rel');
			$("#homesPopup").load('/ajax/homes/',{'imageID': imageID},function(){
				$.fn.colorbox({
					'inline': true,
					'href': '#homesPopup',
					'open': true,
					'opacity': 0.5
				});
				$("#loading").hide();
				Cufon.refresh();
			});
			
				
		});
		
		$(".locationPopup").click(function(){
			//$("#loading").show();
			var homeID = $(this).attr('rel');
			$.fn.colorbox({
				'iframe': true,
				'href': "/ajax/map/?id="+homeID,
				'width': 450,
				'height': 350,
				'open': true,
				'opacity': 0.5
			});
		});
	}


	// Sliding dropdown menu for homes page
	$("#dropdown").hover(
	function () {
		$("#dropdownmenu").stop().animate({
			//bottom : '25px',
			opacity: 1
		}, 250);
	}, 
	function () {
		$("#dropdownmenu").stop().animate({
			//bottom : '-96px',
			opacity: 0
		}, 250);
	});
	
	$(".testimonials-image a").colorbox({

	});


});

/**
 * @author Alexandre Magno
 * @desc Center a element with jQuery
 * @version 1.0
 * @example
 * $("element").center({
 *
 * 		vertical: true,
 *      horizontal: true
 *
 * });
 * @obs With no arguments, the default is above
 * @license free
 * @param bool vertical, bool horizontal
 * @contribution Paulo Radichi
 *
 */
jQuery.fn.center = function(params) {

		var options = {

			vertical: true,
			horizontal: true

		}
		op = jQuery.extend(options, params);

   return this.each(function(){

		//initializing variables
		var $self = jQuery(this);
		//get the dimensions using dimensions plugin
		var width = $self.width();
		var height = $self.height();
		//get the paddings
		var paddingTop = parseInt($self.css("padding-top"));
		var paddingBottom = parseInt($self.css("padding-bottom"));
		//get the borders
		var borderTop = parseInt($self.css("border-top-width"));
		var borderBottom = parseInt($self.css("border-bottom-width"));
		//get the media of padding and borders
		var mediaBorder = (borderTop+borderBottom)/2;
		var mediaPadding = (paddingTop+paddingBottom)/2;
		//get the type of positioning
		var positionType = $self.parent().css("position");
		// get the half minus of width and height
		var halfWidth = (width/2)*(-1);
		var halfHeight = ((height/2)*(-1))-mediaPadding-mediaBorder;
		// initializing the css properties
		var cssProp = {
			position: 'absolute'
		};

		if(op.vertical) {
			cssProp.height = height;
			cssProp.top = '50%';
			cssProp.marginTop = halfHeight;
		}
		if(op.horizontal) {
			cssProp.width = width;
			cssProp.left = '50%';
			cssProp.marginLeft = halfWidth;
		}
		//check the current position
		if(positionType == 'static') {
			$self.parent().css("position","relative");
		}
		//aplying the css
		$self.css(cssProp);


   });

};


