
/**
*    GOOGLE MAPS
*/

function getGPlacemarkInfo (place) {

    var countryCode = '';
    var region = '';
    var subregion = '';
    var city = '';
    var address = '';
    var code = '';
    var info = place.address;

    var re = new RegExp("([0-9]{2}-[0-9]{3})", "g");

    var m = re.exec(place.address);
    if (m == null) {
    } else {
        code = m[0];
    }

    if(place.AddressDetails && place.AddressDetails.Country) {
        if(place.AddressDetails.Country.AdministrativeArea) {
            region = place.AddressDetails.Country.AdministrativeArea.AdministrativeAreaName;
            if(place.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea) {
                subregion = place.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.SubAdministrativeAreaName;
                if(place.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality) {
                    city = place.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality.LocalityName;

                    if(place.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality.Thoroughfare) {
                        address = place.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality.Thoroughfare.ThoroughfareName;
                    }

                }

            }
        }
        countryCode = place.AddressDetails.Country.CountryNameCode;
    }


    return {
        countryCode: countryCode,
        region: region,
        subregion: subregion,
        city: city,
        address: address,
        code: code,
        info: info
    };

}

/**
*    COVER LOGIN FORM
*/
function showLoginForm () {
    _initLoginWindow();
    $('#coverContent').load('/user/auth/login');
    _showLoginWindow();
    return false;
}

function _initLoginWindow() {
    _hideLoginWindow();
    $('body').append('<div id="coverWindow"><a href="#" id="coverCloser">[x]</a><div id="coverContent"></div></div>');

    $('#coverCloser').click (function(){
        _hideLoginWindow();
        return false;
    });
}

function _showLoginWindow() {
    _vCoverInterface();

    var arrPageSizes = _vGetPageSize();

    var _top = _vGetPageScroll()[1]+100;
    var _left = (Math.round(arrPageSizes[0]/2))-320;

    $('#coverWindow').css({
            position: 'absolute',
            zIndex: 1000,
            top: _top,
            left: _left
        }).fadeIn();

    $('#coverCloser').css({
            position: 'absolute',
            zIndex: 1001
        });

    $('#coverWindow').show();

}

function _hideLoginWindow() {
    if($('#coverWindow')) {
        $('#coverWindow').fadeOut(function() { $('#coverWindow').remove(); });
    }
    _vHideCoverInterface();
}

/**
* COVER
*/

function _vCoverInterface() {
    $('body').append('<div id="vframe-cover"></div>');

    var arrPageSizes = _vGetPageSize();
    // Style overlay and show it
    $('#vframe-cover').css({
        position: 'absolute',
        zIndex: 1,
        top: 0,
        left: 0,
        backgroundColor:    '#000',
        opacity:            0.8,
        width:                arrPageSizes[0],
        height:                arrPageSizes[1]
    }).fadeIn();

}
function _vHideCoverInterface() {
    $('#vframe-cover').fadeOut(function() { $('#vframe-cover').remove(); });
}

function _vGetPageSize() {
    var xScroll, yScroll;
    if (window.innerHeight && window.scrollMaxY) {
        xScroll = window.innerWidth + window.scrollMaxX;
        yScroll = window.innerHeight + window.scrollMaxY;
    } else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
        xScroll = document.body.scrollWidth;
        yScroll = document.body.scrollHeight;
    } else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
        xScroll = document.body.offsetWidth;
        yScroll = document.body.offsetHeight;
    }
    var windowWidth, windowHeight;
    if (self.innerHeight) {    // all except Explorer
        if(document.documentElement.clientWidth){
            windowWidth = document.documentElement.clientWidth;
        } else {
            windowWidth = self.innerWidth;
        }
        windowHeight = self.innerHeight;
    } else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
        windowWidth = document.documentElement.clientWidth;
        windowHeight = document.documentElement.clientHeight;
    } else if (document.body) { // other Explorers
        windowWidth = document.body.clientWidth;
        windowHeight = document.body.clientHeight;
    }
    // for small pages with total height less then height of the viewport
    if(yScroll < windowHeight){
        pageHeight = windowHeight;
    } else {
        pageHeight = yScroll;
    }
    // for small pages with total width less then width of the viewport
    if(xScroll < windowWidth){
        pageWidth = xScroll;
    } else {
        pageWidth = windowWidth;
    }
    arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight);
    return arrayPageSize;
}

function _vGetPageScroll() {
    var xScroll, yScroll;
    if (self.pageYOffset) {
        yScroll = self.pageYOffset;
        xScroll = self.pageXOffset;
    } else if (document.documentElement && document.documentElement.scrollTop) {     // Explorer 6 Strict
        yScroll = document.documentElement.scrollTop;
        xScroll = document.documentElement.scrollLeft;
    } else if (document.body) {// all other Explorers
        yScroll = document.body.scrollTop;
        xScroll = document.body.scrollLeft;
    }
    arrayPageScroll = new Array(xScroll,yScroll);
    return arrayPageScroll;
}
