// FIXME move somewhere else - doesn't belong here
$(document).ready(function(){
    var nav  = $('.nav');
    var path = window.location.pathname.split('/').slice(1,-1);
    if (path.length)
        for(; path.length; path.pop())
            act(path.join('/')+'/');
    else
        act('');

    function act(p) {

		nav.not(nav.find('.selected').parent())
			.find('a[@href="/'+p+'"]').parent().addClass('selected');
    }

});

$(document).ready(function(){
	$('a.popup').click(function(e){
		window.open(this.href, 'chi-chat', 'width=300,height=445,top=100,left=100,toolbar=0,status=0,menubar=0,location=1,resizable=1,scrollbars=0');
		e.preventDefault()
	})	
});

/*
 * Image Rotation
 *
 */

var speed = 10000;
var delay = 18000; /* awaiting time after the user change the image manually */
var paused = false;
var counter = 0;

function initRotate() {
	counter++;
	if (!paused || counter > (delay / speed)) {
		rotateImage();
	}
}

function changeImage(current, target, direction) {
	var image_v = $(current).next(".image").find("img");
	var image_n = $(target).next(".image").find("img");

	if (direction == 1) {
		var end_v = -444;
		var start_n = 444;
	} else {
		var end_v = 444;
		var start_n = -444;
	}

	$(image_v).animate({left: 0}, {duration: 1}).animate({left: end_v}, {duration: "slow", easing: 'swing'});
	$(image_n).animate({left: start_n}, {duration: 1}).animate({left: 0}, {duration: "slow", easing: 'swing'});

	$(target).addClass("visible");
	$(target).show();

	$(current).removeClass("visible");
	$(current).hide();
	
	currentTargetLink = $(target).next(".image").find("a").attr("href")
}

function rotateImage() {
	var visible = $("#mainListing .visible");
	var selected = $("#browseListing .selected");

	if ($(visible).next().next(".property").html() != null) {
		var next = $(visible).next().next(".property");
		var thumb = $(selected).next();
	} else {
		var next = $(visible).siblings(".property:first");
		var thumb = $(selected).siblings(":first");
	}
	
	changeImage(visible, next, 1);

	$(thumb).addClass("selected");
	$(selected).removeClass("selected");
	$(selected).removeClass("fixIE");

}

$(document).ready(function(){
	
	$('body').addClass('jsEnabled');

	$("#mainListing .property").hide()
	.next(".image").find("img").css({left: 444});
	
	$("#mainListing .visible").show()
	.next(".image").find("img").css({left: 0});

	currentTargetLink = $(".image:first").find("a").attr("href")
	$("#mainListing .image").click(function(){
		location.href = currentTargetLink;
	})
	
	$("#browseListing img").after('<strong></strong>');
	$("#browseListing li").css({ cursor: "pointer"});
	$("#browseListing li").click(function(){
		paused = true;
		counter = 0;
		var visible = $("#mainListing .visible");
		var visible_i = parseFloat($("#browseListing .selected").attr("class").split(" ")[0].replace("property_", ""));
		var choise = $("#mainListing ." + $(this).attr("class").split(" ")[0]);
		var choise_i = parseFloat($(this).attr("class").split(" ")[0].replace("property_", ""));
		if (choise_i != visible_i) {
			var direction = (choise_i - visible_i) > 0 ? 1 : 0;
			changeImage(visible, choise, direction);
			$("#browseListing .selected").removeClass("fixIE");
			$("#browseListing .selected").removeClass("selected");
			$(this).addClass("selected");
		}
	})
	
	setInterval("initRotate()", speed);

});
