﻿var ajaxObjects = new Array();
var ajaxObjectsDelayProtect = new Array();

function trim(str) {
    return str.replace(/^\s*|\s*$/g, "");
}

var is = new function() {
    this.VER = navigator.appVersion
    this.AGENT = navigator.userAgent
    this.DOM = document.getElementById ? 1 : 0
    this.IE = (document.all && !this.DOM) ? 1 : 0;
    this.MAC = this.AGENT.indexOf("Mac") > -1
    this.NS6 = document.getElementById && !document.all ? 1 : 0
    this.NS4 = (document.layers && !this.DOM) ? 1 : 0;
    this.OPERA = this.AGENT.indexOf('Opera') > -1
    return this
}

function getElement(id) {
    if (is.DOM) return document.getElementById(id)
    else
        if (is.IE) return eval("document.all." + id)
    else
        if (is.NS) return eval("document." + id)
}

function toggle_visibility(id) {
	var e = document.getElementById(id);
	if(e.style.display == 'block')
	  e.style.display = 'none';
   else
	  e.style.display = 'block';
}

function toggle_visibility_pesquisa(id) {	
	var e = document.getElementById(id);
	var s = document.getElementById('seta_'+id);
	var n = document.getElementById('nome_'+id);
	if(e.style.display == 'block')
	{
	  e.style.display = 'none';
	  s.src='/images/seta_close.png';
	  n.style.display = 'block';
   } else {
	  e.style.display = 'block';
	  s.src='/images/seta_open.png';
	  n.style.display = 'none';
   }
}

/*
function AddToFavorites(domain)
{
    var title = document.title;
    var url = "http://" + domain;
    if (window.sidebar) // Firefox
	    window.sidebar.addPanel(title, url, '');
    else if(window.opera && window.print) // Opera
    {
	    var elem = document.createElement('a');
	    elem.setAttribute('href',url);
	    elem.setAttribute('title',title);
	    elem.setAttribute('rel','sidebar'); // required to work in opera 7+
	    elem.click();
    } 
    else if(document.all) // IE
	    window.external.AddFavorite(url, title);
}
*/

function AddToFavorites(errmsg, domain) {
    var title = document.title;
    var url = "http://" + domain;

    if (window.sidebar) {
        window.sidebar.addPanel(title, url, "");
    } else if (document.all) {
        window.external.AddFavorite(url, title);
    } else if (window.opera && window.print) {
        alert(errmsg);
    } else if (window.chrome) {
        alert(errmsg);
    }
}

function Imprimir() {
    var a = window.open('', '', 'scrollbars=yes,width=720,height=500');
    a.document.open("text/html");
    a.document.write('<html>\n<head>\n<link rel="stylesheet" href="/css/main.css" media="all" type="text/css" />\n<style>\n.noprint{display:none}\n<\/style>\n</head>\n<body style="background-image:none;background-color:#FFFFFF;">\n');
    a.document.write('<table align="center" width="700" border="0" cellpadding="10" cellspacing="0">\n<tr>\n<td align="left">\n<img align="absmiddle" src="/images/banner_impressao.jpg" border="0" alt="" /><br/><br/>');
    a.document.write('<tr>\n<td>\n');
    a.document.write(document.getElementById('print').innerHTML);
    a.document.write('\n<\/td>\n<\/tr>\n');
    a.document.write('<\/table>\n');
    a.document.write('<\/body>\n<\/html>');
    a.document.close();
    a.print();
}

function set_style(idx, stl) {
    var obj = document.getElementById(idx)
    //alert(obj.className);
    obj.className = stl;
}

function testField(field) {
    var regExpr = new RegExp("^\d*\.?\d*$");
    if (!regExpr.test(field.value)) {
      // Case of error
      field.value = "";
    }
}


//function changeFontSize(element, step) {

//    alert(step);
//    step = parseInt(step, 10);
//    var el = document.getElementById(element);
//    var curFont = parseInt(el.style.fontSize, 10);
//    el.style.fontSize = (curFont + step) + 'px';
//}


function changeFontSize(element, step) {

    
        var ourText = $(element);

        var currFontSize = ourText.css('fontSize');
        var finalNum = parseFloat(currFontSize, 10);
        steps = parseFloat(step, 10);
        var stringEnding = currFontSize.slice(-2);
        if (step == '+2') {
        finalNum *= 1.2;
        }
        else if (step == '-2') {
        finalNum /= 1.2;
        }

        ourText.css('line-height', '1.5em');
        ourText.animate({ fontSize: finalNum + stringEnding }, 600);
   
}




//***************************************

function changeSize(obj, value) {
    try {
        elements = obj.split(",");
        for (i = 0; i < elements.length; i++)
            document.getElementById(elements[i]).style.fontSize = (parseInt(document.getElementById(elements[i]).style.fontSize) + value) + 'px';
    } catch (e) {
        alert(e.description);
    }
}

function findPos(obj) {
    var curleft = curtop = 0;
    if (obj.offsetParent) {
        do {
            curleft += obj.offsetLeft;
            curtop += obj.offsetTop;
        } while (obj = obj.offsetParent);
    }
    return [curleft, curtop];
}

function testaIE6() {
	if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)){ 
		var ieversion=new Number(RegExp.$1)
		if (ieversion<7) {
			return true;
		} else  {
			return false;
		}
	} else  {
		return false;
	}
}

/* ****/

function CheckIsIE() {
    if (navigator.appName.toUpperCase() == 'MICROSOFT INTERNET EXPLORER') { return true; }
    else { return false; }
}


/*----------------Ajax getContent -------------------*/
function getContent(url,divobj,fnc_completo,params){
	try {
	pageTracker._trackPageview(url);
	} catch(err) {}
	var ajaxIndex = ajaxObjects.length;
	ajaxObjectsDelayProtect[url] = ajaxIndex;
	ajaxObjects[ajaxIndex] = new sack();
	ajaxObjects[ajaxIndex].requestFile = url;
	ajaxObjects[ajaxIndex].encodeURIString = false;
	ajaxObjects[ajaxIndex].onCompletion = function() {showContent(divobj,ajaxIndex,url,fnc_completo);};
	ajaxObjects[ajaxIndex].runAJAX(params);
	showWaitMessage(divobj);
} 

function showWaitMessage(divobj){
	divobj.innerHTML = '<img src="/images/ajax-loader.gif" alt="A carregar dados..." />';
}

function showContent(divobj,index,url,fnc_completo){
	if (ajaxObjectsDelayProtect[url] == index){divobj.innerHTML = ajaxObjects[index].response;	}
	if (fnc_completo)eval(fnc_completo);
}

function showhide2(id) {
    obj = getElement(id);
    viz = obj.style.display;
    if (viz == 'block') {
        obj.style.display = "none";
    } else {
        obj.style.display = "block";
    }
}

function Reload(obj1, id) {
    window.parent.document.getElementById('iframe_recomendar').src = "/form_recomendar.aspx?idcont=" + id;
}

function mostrar_pedirinfo(obj, obj1) {
    document.getElementById(obj1).style.display = 'block';
    esconder_sn();
    pos = findPos(obj);
    pos_x = pos[0];
    document.getElementById(obj1).style.left = pos_x-270 + 'px';
    pos_y = pos[1];
    pos_y = pos_y;
    document.getElementById(obj1).style.top = pos_y + 30 + 'px';
}

function esconder_pedirinfo(obj1) {
    //getElement(obj1).style.display = 'none';
    window.parent.document.getElementById(obj1).style.display = 'none';
}

function mostrar_social(obj, obj1) {
    esconder_pedirinfo('recomendar_imovel');
    document.getElementById(obj1).style.display = 'block';
    pos = findPos(obj);
    pos_x = pos[0];
    document.getElementById(obj1).style.left = pos_x-160 + 'px';
    pos_y = pos[1];
    pos_y = pos_y;
    document.getElementById(obj1).style.top = pos_y + 30 + 'px';
}

function esconder_sn() {
    getElement('social_networks').style.display = 'none';
}



//---------------------------------------------------------------------------------------------------------------------
//GSP Converter

function DMS_to_Degrees(d,m,s,dir) {
        var deg = parseFloat(Math.abs(d)) + parseFloat(Math.abs(m)/60) + parseFloat(Math.abs(s)/3600);
        if (dir == 'W' || dir == 'S') { deg = parseFloat(-1*deg); }
        if (d == '' && m == '' && s == '') { deg = ''; }
        return comma2point(deg);
}

function Degrees_to_DMM(deg,type,spacer,minutemark) {
        if (!deg.toString().match(/[0-9]/)) { return ''; }
        if (!spacer) { spacer = ''; }
        if (!minutemark) { minutemark = ''; }
        if (type == 'lat') {   
                if (parseFloat(deg) < 0) { var dir = 'S'; } else { var dir = 'N'; }
        } else {
                if (parseFloat(deg) < 0) { var dir = 'W'; } else { var dir = 'E'; }
        }
        var d = Math.floor(Math.abs(parseFloat(deg)));
        var m = 60 * (Math.abs(parseFloat(deg)) - parseFloat(d))
        m = Math.round(1000000 * m) / 1000000;
        if (type == 'lon') {
                if (d < 10) { d = '00'+d; } else if (d < 100) { d = '0'+d; }
        } else {
                if (d < 10) { d = '0'+d; }
        }
        if (parseFloat(m) == Math.floor(parseFloat(m))) { m = m + '.0'; }
        return dir + spacer + d + String.fromCharCode(176) + spacer + comma2point(m) + minutemark;
}

function Degrees_to_DMS(deg,type,spacer) {
        if (!deg.toString().match(/[0-9]/)) { return ''; }
        if (!spacer) { spacer = ''; }
        if (type == 'lat') {   
                if (parseFloat(deg) < 0) { var dir = 'S'; } else { var dir = 'N'; }
        } else {
                if (parseFloat(deg) < 0) { var dir = 'W'; } else { var dir = 'E'; }
        }
        var d = Math.floor(Math.abs(parseFloat(deg)));
        var mmm = 60 * (Math.abs(parseFloat(deg)) - parseFloat(d))
        mmm = Math.round(1000000 * mmm) / 1000000;
        var m = Math.floor(parseFloat(mmm));
        var s = 60 * (parseFloat(mmm) - parseFloat(m))
        s = Math.round(1000 * s) / 1000;
        return dir + spacer + d + String.fromCharCode(176) + spacer + m + '\'' + spacer + comma2point(s) + '"';
}

function deg2rad (deg) {
        return (parseFloat(comma2point(deg)) * 3.14159265358979/180);
}
function rad2deg (radians) {
        return (Math.round(10000000 * parseFloat(radians) * 180/3.14159265358979) / 10000000);
}
function comma2point (number) {
        number = number+''; // force number into a string context
        return (number.replace(/,/g,'.'));
}

function parseCoordinate(coordinate,type,format,spaced) {
        coordinate = coordinate.toString();
        var neg = 0; if (coordinate.match(/(^-|[WS])/i)) { neg = 1; }
        if (coordinate.match(/[EW]/i) && !type) { type = 'lon'; }
        if (coordinate.match(/[NS]/i) && !type) { type = 'lat'; }
        coordinate = coordinate.replace(/[NESW\-]/gi,' ');
        if (!coordinate.match(/[0-9]/i)) {
                return '';
        }
        parts = coordinate.match(/([0-9\.\-]+)[^0-9\.]*([0-9\.]+)?[^0-9\.]*([0-9\.]+)?/);
        if (!parts || parts[1] == null) {
                return '';
        } else {
                n = parseFloat(parts[1]);
                if (parts[2]) { n = n + parseFloat(parts[2])/60; }
                if (parts[3]) { n = n + parseFloat(parts[3])/3600; }
                if (neg && n >= 0) { n = 0 - n; }
                if (format == 'dmm') {
                        if (spaced) {
                                n = Degrees_to_DMM(n,type,' ');
                        } else {
                                n = Degrees_to_DMM(n,type);
                        }
                } else if (format == 'dms') {
                        if (spaced) {
                                n = Degrees_to_DMS(n,type,' ');
                        } else {
                                n = Degrees_to_DMS(n,type,'');
                        }
                } else {
                        n = Math.round(10000000 * n) / 10000000;
                        if (n == Math.floor(n)) { n = n + '.0'; }
                }
                return comma2point(n);
        }
}

// lat - Gmap Lat coordinate
// lng - Gmap Lng coordinate
// space - True/False (space between values)
function Convert_Coordinates(lat,lng,space) {
        var lat = lat;
        var lon = lng;
        var spaced = space;

      //DDD - Decimal Degrees
      //DMM - Degrees Minutes  (GPS)
      //DMS - Degrees Minutes Seconds

      var coordinates_lat_ddd;
      var coordinates_lat_dmm;
      var coordinates_lat_dms;
      var coordinates_lng_ddd;
      var coordinates_lng_dmm;
      var coordinates_lng_dms;
      var coordinates_pair_ddd;
      var coordinates_pair_dmm;
      var coordinates_pair_dms;

       
        coordinates_lat_ddd = parseCoordinate(lat,'lat','ddd',spaced);
        coordinates_lat_dmm = parseCoordinate(lat,'lat','dmm',spaced);
        coordinates_lat_dms = parseCoordinate(lat,'lat','dms',spaced);
       
        coordinates_lon_ddd = parseCoordinate(lon,'lon','ddd',spaced);
        coordinates_lon_dmm = parseCoordinate(lon,'lon','dmm',spaced);
        coordinates_lon_dms = parseCoordinate(lon,'lon','dms',spaced);
       
        coordinates_pair_ddd = coordinates_lat_ddd+', '+coordinates_lon_ddd;
        coordinates_pair_dmm = coordinates_lat_dmm+', '+coordinates_lon_dmm;
        coordinates_pair_dms = coordinates_lat_dms+', '+coordinates_lon_dms;

      //alert("Coordinates: DDD = "+coordinates_pair_ddd+" / DMM= "+coordinates_pair_dmm+" / DMS="+coordinates_pair_dms);
	  document.write(coordinates_pair_dms);
}

//---------------------------------------------------------------------------------------------------------------------

function divide_string(param){
    var newStr = param.substring(1,param.length-1);
    var mytool_array = newStr.split(",");
    Convert_Coordinates(mytool_array[0], mytool_array[1], true);
}

function actualiza_fundo() {
    var theWindow = $(window),
	    $bg = $("#bg"),
        aspectRatio = $bg.width() / $bg.height();
    function resizeBg() {
        if ((theWindow.width() / theWindow.height()) < aspectRatio) {
            $bg
			    .removeClass()
			    .addClass('bgheight');
        } else {
            $bg
			    .removeClass()
			    .addClass('bgwidth');
        }

    }
    theWindow.resize(function () {
        resizeBg();
    }).trigger("resize");
    
};
