(function($) {
    $.fn.fadeIn = function(speed, callback) {
        return this.animate({opacity: 'show'}, speed, function() {
                if ( $.browser.msie )
                {
                        this.style.removeAttribute('filter');
                }
                if ( $.isFunction(callback) )
                {
                        callback.call(this);
                }
        });
    };

    $.fn.fadeOut = function(speed, callback) {
        return this.animate({opacity: 'hide'}, speed, function() {
                if ( $.browser.msie )
                {
                        this.style.removeAttribute('filter');
                }
                if ( $.isFunction(callback) )
                {
                        callback.call(this);
                }
        });
    };

    $.fn.fadeTo = function(speed, to, callback) {
        return this.animate({opacity: to}, speed, function() {
                if ( to == 1 && $.browser.msie )
                {
                        this.style.removeAttribute('filter');
                }
                if ( $.isFunction(callback) )
                {
                        callback.call(this);
                }
        });
    };
})(jQuery);


$(document).ready(
	function() {
		pager();
		showPage(0);
		$("div.qa").hide();
		qaIndex = Math.floor(Math.random() * $("div.qa").length);
		$("div.qa").eq(qaIndex).show();
		
		function previous_qa(){
			if ( ! $.browser.msie )
			{
				$("div.qa").eq(qaIndex).fadeOut(500,
					function() {
						qaIndex = qaIndex - 1;
						if (qaIndex < 0) { qaIndex = $("div.qa").length - 1; };
						$("div.qa").eq(qaIndex).fadeIn(500);
						return false;
					}
				);
			} else {
				$("div.qa").eq(qaIndex).hide();
				qaIndex = qaIndex - 1;
				if (qaIndex < 0) { qaIndex = $("div.qa").length - 1; };
				$("div.qa").eq(qaIndex).show();
				return false;
					
			}
		}
		$("#previousqa").click(function(){previous_qa(); paused=true; $("#pauseqa").fadeOut(200); return false;});
		function next_qa(){
			if ( ! $.browser.msie )
			{
				$("div.qa").eq(qaIndex).fadeOut(500,
					function() {
						qaIndex = qaIndex + 1;
						if (qaIndex > $("div.qa").length - 1 ) { qaIndex = 0; };
						$("div.qa").eq(qaIndex).fadeIn(500);
						return false;
					}
				);
			} else {
				$("div.qa").eq(qaIndex).hide();
				qaIndex = qaIndex + 1;
				if (qaIndex > $("div.qa").length - 1 ) { qaIndex = 0; };
				$("div.qa").eq(qaIndex).show();
				return false;
			}
		}
		$("#nextqa").click(function(){
			next_qa();
			paused=true;
			$("#pauseqa").fadeOut();
			return false;}
		);
		var paused = false;
		function animate_next(){
			if(!paused)
				next_qa();			
		}
		setInterval(animate_next, 15000);
		$("#pauseqa").click(function(){
			paused = true;
			$(this).fadeOut();
			return false;
			
		});
		$("#coopfilter").click(
			function() {
				filter("coop");
				pager();
				showPage(0);
				return false;
			}
		);
		$("#condofilter").click(
			function() {
				filter("condo");
				pager();
				showPage(0);
				return false;
			}
		);
		$("#bothfilter").click(
				function() {
					filter("general");
					pager();
					showPage(0);
					return false;
				}
			);
		$("#removefilter").click(
			function() {
				filter("");
				pager();
				showPage(0);
				return false;
			}
		);
		$("ul#overview-list li a").hover(
			function () {
				$(this).siblings("div.teaser").show();
				//$(this).siblings("div.teaser").css('visibility', 'visible');
			}, 
			function () {
				$(this).siblings("div.teaser").hide();
				//$(this).siblings("div.teaser").css('visibility', 'hidden');
			}
		);
	}
);
var filter = function(filter) {
	if (filter == "condo") {
		$("#overview-list li:not(.condoitem)").addClass("filtered").hide();
		$("#overview-list li.condoitem").show().removeClass("filtered");
	} else if (filter == "coop") {
		$("#overview-list li:not(.coopitem)").addClass("filtered").hide();
		$("#overview-list li.coopitem").show().removeClass("filtered");
	} else if (filter == "general") {
		$("#overview-list li:not(.generalitem)").addClass("filtered").hide();
		$("#overview-list li.generalitem").show().removeClass("filtered");
	} else {
		$("#overview-list li").removeClass("filtered").show();;
	}
};
var qaIndex = 0;
var numPerPage = 13;
var showPage = function(currentPage) {
    var s = currentPage * numPerPage;
    var e = (currentPage + 1) * numPerPage - 1;
    $("#overview-list li.filtered").hide();
    $("#overview-list li:not(.filtered)").show();
    $("#overview-list li:not(.filtered):lt("+ s +")").hide();
    $("#overview-list li:not(.filtered):gt("+ e +")").hide();
};
var pager = function() {
	var numItems = $("#overview-list li:not(.filtered)").length;
    var numPages = Math.ceil(numItems / numPerPage);	
    $("#pages").text("");
    for (var page = 0; page < numPages; page++) {
        $('<a href="#">' + (page + 1) + '</a>')
          .bind('click', {'newPage': page}, function(event) {
        	var target_offset = $("#quick-form").offset();
        	var target_top = target_offset.top;
        	$('html, body').animate({scrollTop:target_top}, 100);
        	currentPage = event.data['newPage'];
        	showPage(currentPage);
        	$(this).addClass('selected').siblings().removeClass('selected');
            return false;
          })
          .appendTo($("#pages"));
    }
    $("#pages a:first").addClass('selected');
};

