/********
	O slide está lendo verificar se tem a ver com:
    	- tamnho da fotos
    	- utilizar class no lugar de id
    	
    A exibição não está legal, tentar aumentar o espaçamento entre fotos do meio da div de exibição.
    A lista pode não ser circular, vai até o fim e depois tem que voltar um por um
*
*********/
function slide_right(where, width, next, time_animate, button, padding) {
	//var button = $(where+' .property_slide_top_button');
	if (button.hasClass('on')) {
		var panel_first = $(where+" .property_slide_bottom .items .panel:first");
		var panel_last = $(where+" .property_slide_bottom .items .panel:last");
		var panel_next = $(where+" .property_slide_bottom .items .panel:nth-child("+next+")");
		button.removeClass('on');
		if (is_IE) {
			var text_property = $(where+" .property_slide_bottom .items .panel:first .text_property");
			text_property.hide();
		};
		panel_first.animate({"width": "-="+width+"px", 'padding-left': '-='+padding+'px', 'padding-right': '-='+padding+'px'}, time_animate, "linear", function () {
			panel_first.hide();
			panel_last.after(panel_first);
			if (is_IE) {
				text_property.show();
			};
		});
		panel_next.animate({"width": "+="+width+"px", 'padding-left': '+='+padding+'px', 'padding-right': '+='+padding+'px'}, time_animate, "linear", function () {
			button.addClass('on');
		});
	};
	return false;
};
function slide_left(where, width, next, time_animate, button, padding) {
	//var button = $(where+' .property_slide_top_button');
	if (button.hasClass('on')) {
		var panel_first = $(where+" .property_slide_bottom .items .panel:first");
		var panel_last = $(where+" .property_slide_bottom .items .panel:last");
		panel_first.before(panel_last);
		panel_first = $(where+" .property_slide_bottom .items .panel:first");
		var panel_next = $(where+" .property_slide_bottom .items .panel:nth-child("+next+")");
		button.removeClass('on');
		if (is_IE) {
			var text_property = $(where+" .property_slide_bottom .items .panel:first .text_property");
			text_property.hide();
		};
		panel_next.animate({"width": "-="+width+"px", 'padding-left': '-='+padding+'px', 'padding-right': '-='+padding+'px'}, time_animate, "linear", function () {
			panel_next.hide();
		});
		panel_first.animate({"width": "+="+width+"px", 'padding-left': '+='+padding+'px', 'padding-right': '+='+padding+'px'}, time_animate, "linear", function () {
			if (is_IE) {
				text_property.show();
			};
			button.addClass('on');
		});
	};
};
function slide_enables(where, auto) {
	var id_where = $(where);
	if (id_where.hasClass('property_slide')) {
		var items = $(where+' .property_slide_bottom .items');
		var panel = $(where+' .property_slide_bottom .items .panel');
		var width = panel.width();
		var padding = 2*parseInt(panel.css('padding-left'));
		var per_page = Math.floor(items.width()/(width+padding));
		var next = per_page + 1;
		items.css('width', per_page*(width+padding)+'px');
		panel.hover(
			function () {
				$(this).addClass('selected');
			}, 
			function () {
				$(this).removeClass('selected');
			}
		);
		if($(where+' .property_slide_bottom .items .panel:nth-child('+next+')').width()) {
			var time_animate = 1000;
			var button = $(where+' .property_slide_top_button');
			panel.css('width', width);
			$(where+' .property_slide_bottom .items .panel:nth-child('+per_page+')').nextAll('.panel').css('width', 0).css('display', 'none');
			button.addClass('on').append('<img class=\'slide_left\' title=\'Deslizar para esquerda\' alt=\'Deslizar para esquerda\' src=\'lib/img/external/others/left_button.png\' /><img class=\'slide_right\' title=\'Deslizar para direita\' alt=\'Deslizar para direita\' src=\'lib/img/external/others/right_button.png\' />');
			button.children('.slide_right').click (function() {
				clearInterval(Interval);
				//$(where+" .property_slide_bottom .items .panel:first").addClass('selected');
				slide_right(where, width, next, time_animate, button, padding);
			});
			button.children('.slide_left').click (function() {
				clearInterval(Interval);
				slide_left(where, width, next, time_animate, button, padding);
			});
			if (auto) {
				var delay = 6000;
				var Interval = window.setInterval(function() {
					slide_right(where, width, next, time_animate, button, padding);
				},delay);
			};
		};
		return true;
	}
	else {
		return false;
	};
};
$(function() {
	is_IE = $.browser.msie;
	slide_enables('#highlights', true);
	slide_enables('#similar_property', false);
	slide_enables('#search_result_property', false);
});