/**
 * @author Alexander Farkas
 * v. 1.02
 */
(function($){
	$.extend($.fx.step, {
		backgroundPosition : function(fx){
			if(fx.state === 0 && typeof fx.end == 'string'){
				var start = $.curCSS(fx.elem,'backgroundPosition');
				start     = toArray(start);
				fx.start  = [start[0],start[2]];
				var end   = toArray(fx.end);
				fx.end    = [end[0],end[2]];
				fx.unit   = [end[1],end[3]];
			}
			var nowPosX = [];
			nowPosX[0] = ((fx.end[0] - fx.start[0]) * fx.pos) + fx.start[0] + fx.unit[0];
			nowPosX[1] = ((fx.end[1] - fx.start[1]) * fx.pos) + fx.start[1] + fx.unit[1];
			fx.elem.style.backgroundPosition = nowPosX[0]+' '+nowPosX[1];
			function toArray(strg){
				strg    = strg.replace(/left|top/g, '0px');
				strg    = strg.replace(/right|bottom/g, '100%');
				strg    = strg.replace(/([0-9\.]+)(\s|\)|$)/g,"$1px$2");
				var res = strg.match(/(-?[0-9\.]+)(px|\%|em|pt)\s(-?[0-9\.]+)(px|\%|em|pt)/);
				return [parseFloat(res[1], 10), res[2], parseFloat(res[3] , 10), res[4]];
			}
		}
	});
})(jQuery);


// animation for home page service blocks
$(function(){
	$('#nav-home')
		.css( {backgroundPosition: "9px 30px"} )
		.mouseover(function(){
			$(this).stop().animate({backgroundPosition:"(9px -22px)"}, {duration:200})
		}).mouseout(function(){
			$(this).stop().animate({backgroundPosition:"(9px 30px)"}, {duration:300, complete:function(){
		}})
	})
	$('#nav-homes')
		.css( {backgroundPosition: "-45px 30px"} )
		.mouseover(function(){
			$(this).stop().animate({backgroundPosition:"(-45px -22px)"}, {duration:200})
		}).mouseout(function(){
			$(this).stop().animate({backgroundPosition:"(-45px 30px)"}, {duration:300, complete:function(){
		}})
	})
	$('#nav-school')
		.css( {backgroundPosition: "-161px 30px"} )
		.mouseover(function(){
			$(this).stop().animate({backgroundPosition:"(-161px -22px)"}, {duration:200})
		}).mouseout(function(){
			$(this).stop().animate({backgroundPosition:"(-161px 30px)"}, {duration:300, complete:function(){
		}})
	})
	$('#nav-communities')
		.css( {backgroundPosition: "-287px 30px"} )
		.mouseover(function(){
			$(this).stop().animate({backgroundPosition:"(-287px -22px)"}, {duration:200})
		}).mouseout(function(){
			$(this).stop().animate({backgroundPosition:"(-287px 30px)"}, {duration:300, complete:function(){
		}})
	})
	$('#nav-about')
		.css( {backgroundPosition: "-387px 30px"} )
		.mouseover(function(){
			$(this).stop().animate({backgroundPosition:"(-387px -22px)"}, {duration:200})
		}).mouseout(function(){
			$(this).stop().animate({backgroundPosition:"(-387px 30px)"}, {duration:300, complete:function(){
		}})
	})
	$('#nav-links')
		.css( {backgroundPosition: "-446px 30px"} )
		.mouseover(function(){
			$(this).stop().animate({backgroundPosition:"(-446px -22px)"}, {duration:200})
		}).mouseout(function(){
			$(this).stop().animate({backgroundPosition:"(-446px 30px)"}, {duration:300, complete:function(){
		}})
	})
	$('#nav-builders')
		.css( {backgroundPosition: "-499px 30px"} )
		.mouseover(function(){
			$(this).stop().animate({backgroundPosition:"(-499px -22px)"}, {duration:200})
		}).mouseout(function(){
			$(this).stop().animate({backgroundPosition:"(-499px 30px)"}, {duration:300, complete:function(){
		}})
	})
});


$(document).ready(function(){
	// open gmap instructions box on click
	$("#openMapInstructions").click(function(){ 
		$("#mapInstructions").slideToggle("fast");
		$(this).slideToggle("fast");
		CookieCreate('hide_map_instructions', '0', 0);
	});
	// close gmap instructions box on click
	$("#mapInstructions").click(function(){ 
		$(this).slideToggle("fast");
		$("#openMapInstructions").slideToggle("fast");
		CookieCreate('hide_map_instructions', '1', 0);
	});
	// set default state to closed
	$("#gm_markers").hide();
	// open/close map results in box
	$('#results_list_toggle, #gm_markers').click(function () { 
		$("#gm_markers").slideToggle("fast"); 
	});
	// on page load
	var test = CookieGet('hide_map_instructions');
	if(test && test == 1){
		$("#mapInstructions").slideToggle("fast");
		$("#openMapInstructions").slideToggle("fast");
	}
	// contest form links
	$(".iframe").fancybox({
		'hideOnContentClick' : false,
		'width'              : 400,
		'height'             : 500,
		'padding'            : 0,
		'margin'             : 0,
		'overlayShow'        : true,
		'overlayOpacity'     : 0.8,
		'overlayColor'       : '#000',
		'centerOnScroll'     : true
	});
	// listing detail images
	$(".thickbox").fancybox({
		'padding'            : 0,
		'margin'             : 0,
		'overlayShow'        : true,
		'overlayOpacity'     : 0.8,
		'overlayColor'       : '#000',
		'centerOnScroll'     : true
	});
});


function CookieCreate(name, value, days){
	var expires = "";
	if(days){
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		expires = "; expires="+date.toGMTString();
	}
	document.cookie = name + "=" + value + expires + "; path=/";
}


function CookieGet(name){
	var nameEQ = name + "=";
	var ca     = document.cookie.split(';');
	for(var i = 0; i < ca.length; i++){
		var c = ca[i];
		while(c.charAt(0) == ' '){
			c = c.substring(1, c.length);
		}
		if(c.indexOf(nameEQ) == 0){
			return c.substring(nameEQ.length, c.length);
		}
	}
	return null;
}


function CookieDelete(name){
	createCookie(name, "", -1);
}

function GoogleAnalytics(){
	var _gaq = _gaq || [];
	_gaq.push(['_setAccount', 'UA-24010576-1']);
	_gaq.push(['_trackPageview']);
	(function(){
		var ga   = document.createElement('script');
		ga.type  = 'text/javascript';
		ga.async = true;
		ga.src   = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
		var s    = document.getElementsByTagName('script')[0];
		s.parentNode.insertBefore(ga, s);
	})();
}
