Cufon.replace('#contact_number,.cufon');

$(function(){
	$('#top_login,#footer_login').loginPopup();
	$('#slider').homeSlider();
	
	$('#inspira_team a').colorbox({
		opacity: .8,
		width: '810px'
	});
	
	$("#contact_form, #newsletter").validate();
	
});

$.fn.homeSlider = function() {
	return this.each(function(){
		var SCROLL_TIME = 500;
		var timer;
		var container = $(this).scrollTop(0);
		var items = container.children();
		var itemHeight = items.eq(0).outerHeight();
		var scrollingDown = true;
		
		var slide = function() {
			var currentScroll = container.scrollTop();
			var newScroll = scrollingDown ? currentScroll+itemHeight : currentScroll-itemHeight;
			
			container.animate({
				scrollTop: newScroll
			},{
				duration: SCROLL_TIME,
				complete: function(){
					if (newScroll == (items.length-1) * itemHeight) {
						scrollingDown = false;
					} else if (newScroll == 0) {
						scrollingDown = true;
					}
				}
			});
		}
		
		container.hover(function(){
			window.clearInterval(timer);
		},function(){
			timer = window.setInterval(slide,5000);
		})
		
		timer = window.setInterval(slide,5000);
	});
};


$.fn.loginPopup = function(){
	var header = $('#header');
	var footer = $('#footer');
	var body = $('body');

	return this.each(function(){
		var isBottom = $(this).attr('id')=='footer_login';

		$(this).children('a').click(function(){
			if ($('#login_form').length==0) {
				$.get($(this).attr('href'),function(data){
					var loginBox = $(data);
					if (isBottom) loginBox.addClass('bottom');
					if (isBottom){
						loginBox.appendTo(footer);
					} else {
						loginBox.appendTo(header);
					}
					loginBox.find('input').first().focus();
					body.bind('click',function(e){
						var isLoginBox = $(e.target).closest('#login_form').length;
						if (!isLoginBox) {
							loginBox.fadeOut('fast',function(){
								loginBox.remove();
							});
							body.unbind('click');
						}
					});
				});
			}
			return false;
		});
	});
}
