function ljr_dummy() {
	return false;
};

String.prototype.trim = function() {
	return this.replace(/^\s+|\s+%/g, '');
};

function ljr_submit(url, data, method) {
    var frm = document.createElement("FORM");

    document.body.appendChild(frm);
	if (!method) method = 'POST';
    frm.method = method;

	function add(key, value) {
		var elm;

		try {
			elm = document.createElement('<input name="' + key + '" type="hidden"/>"');
		} catch (e) {
			elm = document.createElement('input');

			elm.setAttribute('type', 'hidden');
			elm.setAttribute('name', key);
		}
		frm.appendChild(elm);
		elm.value = value;
	};

	if ($.isArray(data) || data.jquery)	{
		$.each(data, function() {
			add(this.name, this.value);
		});
	} else {
		for (var i in data) {
			if ($.isArray(data[i])) {
				$.each(a[i], function() {
					add(i, this);
				});
			} else {
				add(i, $.isFunction(data[i]) ? data[i]() : data[i]);
			}
		}
	}
	if (url){
        frm.action = url;
    }
    frm.submit();
};

function ljr_escapeHtml(str) {
	var div = document.createElement('div');

	div.appendChild(document.createTextNode(str));
	return div.innerHTML;
};

function ljr_getMetadata($j) {
	var comments = [],
		i = 0,
		data;

	$j.each(function() {
		var c = this.childNodes,
			l = c.length;

		for (var j = 0; j < l; ++j) {
			if (c[j].nodeType == 8) {
				data = c[j].nodeValue.replace(/\n|\r\n/g, '');
				if (!(/^\s*?\{.+\}\s*?$/.test(data))){
                    continue;
                }
				try {
					comments[i] = (new Function('return ' + data + ';'))();
					++i;
				} catch (e) { }
			}
		}
	});
	return comments;
};

function ljr_pdfPopup(url) {
	var wh = 'width=' + screen.availWidth + ',height=' + screen.availHeight;

	window.open(url, null, 'screenX=1,screenY=1,left=1,top=1,' + wh);
};

function ljr_isScrolledIntoView(elem) {
    var docViewTop = $(window).scrollTop(),
		docViewBottom = docViewTop + $(window).height(),
		elemTop = $(elem).offset().top,
		elemBottom = elemTop + $(elem).height();

    return ((elemBottom >= docViewTop) && (elemTop <= docViewBottom) && (elemBottom <= docViewBottom) && (elemTop >= docViewTop));
};

function ljr_fillCombo($c, values, selected, hasDefault) {
	var o = [],
		j = 0,
		l,
		v;

	if (typeof values == 'string') {
		var tmp;

		values = values.split('|');
		for (var i in values) {
			if (values[i].indexOf(';') >= 0) {
				tmp = values[i].split(';');
				values[i] = { v: tmp[0], k: tmp[1] };
			}
		}
	}
	$c.find(hasDefault ? ':not(option:first)' : 'option').remove();
	for (var i in values) {
		var v = values[i],
			ob = (typeof v) == 'object';

		if (ob) v = v.v;
		o[j++] = '<option value="';
		o[j++] = v;
		if (v == selected) o[j++] = '" selected="selected';
		o[j++] = '">';
		if (ob) {
			o[j++] = values[i].k;
		}
		else {
			var t = values[i].toString();

			if (t == parseInt(t)) {
				while (true) {
					if ((l = t.length % 3) == 0) l = 3;
					o[j++] = t.substr(0, l);
					if ((t = t.substr(l)).length == 0) break;
					o[j++] = ' ';
				}
			}
			else {
				o[j++] = t;
			}
		}
		o[j++] = '</option>';
	}
	$c.append(o.join(''))
};

// Menu principal
$(function() {
	$('#menu > li').hover(function () {
	  	$(this).addClass('activo').find('.submenu').show().find('a').addClass('submenuactivo');
	}, function () {
		$('.activo .submenu').hide();
		$('.activo a').removeClass('submenuactivo');
		$(this).removeClass('activo');
		$('.fijo').addClass('activo');
		$('.fijo a').addClass('submenuactivo');
	});
});

$(function() {
	$("#seccion.divisiones #principal ol li").toggle(function() {
		$(this).children().show();
	}, function() {
		$(this).children().hide();
		$("#seccion.divisiones #principal ol li a").show()
	});

});

$(function() {
	$(".toggle .btnA").click(function() {
		$(this).parents(".toggle").find(".mapa").hide().end().find(".foto").show();
	});
	$(".toggle .btnB").click(function() {
		$(this).parents(".toggle").find(".mapa").show().end().find(".foto").hide();
	});
});

// Navegacion entre fichas de propiedades
$(function() {
	$("#contenidoHead .nav a.ant").hover(function() {
		$("#contenidoHead .nav p.ant").show();
		$("#contenidoHead .nav a.ant").addClass("activo");
	}, function() {
		$("#contenidoHead .nav p.ant").hide();
		$("#contenidoHead .nav a.ant").removeClass("activo");
	});
});

$(function() {
	$("#contenidoHead .nav a.sig").hover(function() {
		$("#contenidoHead .nav p.sig").show();
		$("#contenidoHead .nav a.sig").addClass("activo");
	}, function() {
		$("#contenidoHead .nav p.sig").hide();
		$("#contenidoHead .nav a.sig").removeClass("activo");
	});
});

// Agrega Clearfix y Tablas
$(function() {
	$(".galeria.planos li, #resBusqueda .propiedad, .propiedades #sidebar .modulo, .accordion div, .track .modulo").addClass("clearfix");
	$('#ficha #sidebar tbody tr,table#comparacion tbody tr, #ficha .columnas table tbody tr').alternate({ odd: 'off', even: 'on', hover: false });
});

// Formularios
$(function() {
	var $f = $('.expandido');

	$('.mostrarform').removeAttr('href').click(function() {
		$f.animate({ height: 'toggle' }, 'slow', function() {
			if ($f.is(':visible')){
              $.scrollTo('img.iso', 'slow');
            }
		});
		return false;
	});
	$('form').attr ('autocomplete', 'off');
});

// Barra de acciones
$(function() {
	var cts = 0,
		inc;

	$('.acciones a.tamano').click(function() {
		if (++cts == 4) cts = 0;
		inc = (cts == 0) ? -3 : 1;
		$('.resizable').each(function() {
			var $t = $(this);

			$t.animate({ fontSize: (parseInt($t.css('font-size')) + inc) + 'px' }, 'fast');
		});
		return false;
	});

	$('.acciones a.imprimir').click(function() {
		var url = window.location.href.split('/');
		url[url.length - 1] = 'imprimir-' + url[url.length - 1];

		ljr_pdfPopup(url.join('/'));
		return false;
	});

	$('.acciones a.recomendar').click(function() {
		ljr_submit('recomendar.php', { url: window.location.href });
	});
});

// FAQ
$(function() {
	$('#seccion.divisiones #principal ol li').toggle(function() {
		$(this).children().show();
	}, function() {
		$(this).children().hide();
		$('#seccion.divisiones #principal ol li a').show();
	});
});

// Track record
$(function() {
	$('.modulo').each(function() {
		var $t = $(this);

		$t.find('.vermas').click(function() {
			$t.find('.detalles').slideToggle();
		});
	});
});

// Ficha propiedades - Galeria y planos
$(function() {
	function tab(t, x) {
		var $a = $('div.galeria').find('.' + t),
			$s = $a.filter(':not(li)'),
			$t = $a.filter('li');

		if (x) {
			$s.show().find('embed, object').each(function() {
				try {
					this.Play();
				}
				catch (e) { }
			});
			$t.addClass('activo');
		}
		else {
			$s.hide().find('embed, object').each(function() {
				try {
					this.Stop();
				}
				catch (e) { }
			});
			$t.removeClass('activo');
		}
	};

	$('div.galeria > ul.pestanas').find('li').each(function() {
		var $i = $(this),
			tabs = ['foto', 'video', 'tour'];

		$i.children('a').click(function() {
			var c = $i.attr('class');

			for (var i in tabs) tab(tabs[i], c == tabs[i]);
			return false;
		});
	}).find('a').first().click();

	$('div.galeria ul.thumb.foto a').lightBox({ fixedNavigation: true });
	//$('div.columna.galeria ul.thumb.foto a').lightBox({ fixedNavigation: true });
	//$('a:has(img)').lightBox();

	var pos = 0;

	$('div.galeria span.acciones a.ant').click(function() {
		var $v = $('ul.thumb:visible li');

		if (pos) {
			$v.eq(pos + 3).hide();
			$v.eq(--pos).show();
		}
		return false;
	});

	$('div.galeria span.acciones a.sig').click(function() {
		var $v = $('ul.thumb:visible li');

		if (pos + 4 < $v.length) {
			$v.eq(pos).hide();
			$v.eq(pos++ + 4).show();
		}
		return false;
	});

	$('ul.thumb:visible li:visible:gt(3)').hide();

	$('div.contenido.foto').click(function() {
		$('ul.thumb.foto').find('li:first > a').click();
	});

	$('ul.galeria.planos a').lightBox({ fixedNavigation: true });
	$('div.col1 > .columnas > .columna.plano img').click(function() {
		$('ul.planos > li:first > a').click();
	});
});

// Ficha emprendimientos - arreglar sidebar
(function() {
	var $sb = $('#ficha.emprendimientos #sidebar');

	if ($sb.length > 0) {
		$(window).load(function() {
			$sb.css('margin-top', ($('#ficha.emprendimientos div.caracteristicas > div:first').offset().top - ($sb.offset().top - parseInt($sb.css('margin-top')))) + 'px');
		});
	}
});

// Disponibilidad
$(function() {
	$('table.avail td.link').click(function() {
		ljr_submit('ficha.php?cod=' + ljr_getMetadata($(this).parents('tr'))[0].id + '&porcodigo=1', {});
	});
});

// Idioma
$(function() {
	$('#menuSec a.lang').each(function() {
		var $l = $(this),
			href = this.href,
			idx = href.lastIndexOf('#');

		if (idx >= 0) {
			href = href.substr(idx + 1);
			$l.click(function() {
				ljr_submit(window.location.href.split('/').pop() || 'home.php', { lang: href });
				return false;
			});
		}
	});
});

// Destacados
$(function() {
	var dir = 0,
		rotationTimer = false,
		$all = $('#destacados div[id^=destacado_]');

	if (!$all.length) return;

	function rotateFeatured() {
		$all.filter(':visible').fadeOut('slow', function() {
			$all.eq((parseInt(this.id.replace('destacado_', '')) + $all.length + dir) % $all.length).fadeIn('slow');
		});
	};

	function setDir(newDir) {
		dir = newDir;
		if (dir != 0 && !rotationTimer) {
			rotationTimer = setInterval(rotateFeatured, 5000);
		}
		else if (newDir == 0) {
			clearInterval(rotationTimer);
			rotationTimer = false;
		}
	};

	$('#destacados .btn1').click(function() {
		setDir(-1);
        rotateFeatured();
		return false;
	});
	$('#destacados .btn2').click(function() {
		setDir(0);
		return false;
	});
	$('#destacados .btn3').click(function() {
		setDir(1);
        rotateFeatured();
		return false;
	});

	$all.eq(0).show();
	setDir(1);


    $('#destacados div.destacado').click(function(ev) {
        ljr_submit(ljr_getMetadata($(this))[0].id, {});
		return true;
	});

});
