var imgs = new Array();

$(function() {
	
    if (document.getElementById("index-text") != null) {
        if (getDocumentHeight() - 237 > $("#index-text").height()) {
            var h = $("#index-text").height();
            var newH = getDocumentHeight() - 237 - h;
            $("#index-text").css({
                marginTop: newH + "px"
            });
            $("#right-lbl").css({
                top: newH + "px"
            });
        }
        else {
            $("#index-text").css({
                marginTop: "57px"
            });
            $("#right-lbl").css("top", "57px");
        }
    }

    if (document.getElementById("contact-data-container") != null) {
        if (getDocumentHeight() - 196 > $("#contact-data-container").height()) {
            var h2 = $("#contact-data-container").height();
            var newH2 = getDocumentHeight() - 196 - h2;
            $("#contact-data-container").css({
                marginTop: newH2 + "px"
            });
        }
        else {
            $("#contact-data-container").css({
                marginTop: "33px"
            });
        }
    }
	
    if (document.getElementById("main-content") != null) {
        if (getDocumentHeight() - 213 > $("#main-content").height()) {
            var h = $("#main-content").height();
            var newH = getDocumentHeight() - 213 - h;
            $("#main-content").css({
                marginTop: newH + "px"
            });
            $("#right-lbl").css({
                top: newH + "px"
            })
        }
        else {
            $("#main-content").css({
                marginTop: "33px"
            });
            $("#right-lbl").css("top", "33px");
        }
    }
	
    setBgImg(document.getElementById("bg").src);

    $("#nwsltr-popup a.close").click(function() {
        $("#nwsltr-popup").hide();
        $(".li-newsltr > a").removeClass("act");
    });
	
    $("#right-lbl").hover(
        function() {
            $("#g-control").addClass("invisibled");
        },
        function() {
            $("#g-control").removeClass("invisibled");
        }
        );
	
    $(".li-newsltr > a").click(function() {
        $(this).toggleClass("act");
        if (this.className == "act") {
            $("#nwsltr-popup").show();
            var sell = document.getElementById("nwsltr-popup");
            var lnk = this;
            addEvent(document, 'click', clickFunc);
			
        }
        else {
            $("#nwsltr-popup").hide();
            removeEvent(document, 'click', clickFunc)
        }
		
        function clickFunc(event) {
            var event = event || window.event;
            var t = event.target || event.srcElement;
			
            if (t != sell && !isChildNode(sell, t) && t != lnk && t != lnk.childNodes[0]) {
                $("#nwsltr-popup").hide();
                $(".li-newsltr > a").removeClass("act");
            }
        }
		
    });

    if (document.getElementById("prop-img-list")) {
        setInterval("changePropsImg()", 3000);
    }

    $("#next-block, #prev-block").hover(
        function() {
            $("a", this).show();
        },
        function() {
            $("a", this).hide();
        }
        );

    // $(".props .main").hover(
    //         function() {
    //             $(".info", this.parentNode).show();
    //             $(this.parentNode).addClass("rol");
    //         }
    //         );
    // 
    //     $(".props .info").hover(
    //         function() {},
    //         function() {
    //             $(this).hide();
    //             $(this.parentNode).removeClass("rol");
    //         }
    //         );

    // Add validator to contact form
    $('#contact-form-container form').validate({
        highlight: function(element){
            $(element).addClass('error');
            $('#error-msg').show();
        },
        unhighlight: function(element){
            $(element).removeClass('error');
            if ($('#contact-form-container :input.error').length == 0) $('#error-msg').hide();
        },
        errorPlacement: function(){},
        submitHandler: function(form) {
            $(form).ajaxSubmit({
                success: function(responseValue) {
                    $(form).find('.result').show();
                    $(form).find('.button').hide();
                }
            });
        }
    });
    $('#contact-form-container .button').click(function(){
        $(this).parents('form').submit();
        return false;
    });

    // Add validation to newsletter form
    $('#signup-form').validate({
        highlight: function(element){
            $(element).addClass('error');
        },
        unhighlight: function(element){
            $(element).removeClass('error');
        },
        errorPlacement: function(){},
        submitHandler: function(form) {
            $(form).ajaxSubmit({
                success: function(responseValue) {
                    $(form).children().hide();
                    if (/.*ERROR.*/.test(responseValue)) {
                        $(form).find('.error-message').show();
                    } else {
                        $(form).find('.success-message').show();
                    }
                }
            });
        }
    });

    $('.props .item ul.info').click(function(){
        location.href = $(this).parent().find('a').attr('href');
    });
	
    if (document.getElementById("g-control") != null) {
        document.onmousemove = function(e) {
            var x = 0, y = 0;
            var arr = document.getElementById("g-control");
			
            if (!e) e = window.event;

            if (e.pageX || e.pageY) {
                x = e.pageX;
                y = e.pageY;
            }
            else if (e.clientX || e.clientY) {
                x = e.clientX + (document.documentElement.scrollLeft || document.body.scrollLeft) - document.documentElement.clientLeft;
                y = e.clientY + (document.documentElement.scrollTop || document.body.scrollTop) - document.documentElement.clientTop;
            }
			
            arr.style.display = "block";
            arr.style.top = y - 26 + "px";
            arr.style.left = x - 26 + "px";
			
            if (x < getClientWidth() / 2) {
                $(arr).addClass("prev");
            }
            else {
                $(arr).removeClass("prev");
            }
			
            if (y > getDocumentHeight() - 153 || $(arr).hasClass("invisibled")) {
                arr.style.display = "none";
            }
            else {
                arr.style.display = "block";
            }
        }
		
        document.onclick = function() {
            if ($("#g-control").is(":visible")) {
                changeBgImage();
            }
        }
    }
	
});

window.onresize = function() {
    setBgImg(document.getElementById("bg").src);
}

function changePropsImg() {
    var items = $("#prop-img-list li");
    var curItem = items.filter(":visible");

    if (curItem.next().is("li")) {
        curItem.fadeOut(1000, function() {

            });
        curItem.next().fadeIn(1000);
    }
    else {
        curItem.fadeOut(1000, function() {

            });
        items.eq(0).fadeIn(1000);
    }
}

function setBgImg(imgUrl) {
    var isLoaded = false;
    for (var j = 0; j < imgs.length; j++) {
        if (imgs[j] == imgUrl) {
            isLoaded = true;
            break;
        }
    }
    if (isLoaded) {
        var img = new Image();
        img.src = imgUrl;
        loadImage(img);
    }
    else {
        var img = new Image();
        img.src = imgUrl;
        img.onload = function() {
            loadImage(this);
        }
    }
	
    function loadImage(img) {
        var bgImg = $("#bg");
        bgImg.attr("src", img.src);
        var winW = $("#page").width() <= getClientWidth() ? getClientWidth() : $("#page").width();
        var winH = getDocumentHeight();
        var w = bgImg.width();
        var h = bgImg.height();
        var ph = h/winH;
        var pw = w/winW;
		
        bgImg.removeAttr("style");
		
        if (pw >= ph) {
            var cw = winH*w/h;
            bgImg.css({
                height: winH,
                width: cw
            });
            if (winW < cw) {
                bgImg.css("left", (winW - cw)/2 );
            }
        }
        else {
            var ch = winW*h/w;
            bgImg.css({
                width: winW,
                height: ch
            });
            if (winH < ch) {
                bgImg.css("top", (winH - ch)/2 );
            }
        }
        imgs.push(img.src);
    }
}

function changeBgImage() {
    var img = $("#bg");
    var imgUrls = img.attr("rel").split(";");
    var cur = parseInt(img.attr("class"));
    if (!$("#g-control").hasClass("prev")) {
        if ((cur + 1) < imgUrls.length) {
            cur = cur + 1;
        }
        else {
            cur = 0;
        }
    }
    else {
        if ((cur - 1) >= 0) {
            cur = cur - 1;
        }
        else {
            cur = imgUrls.length - 1;
        }
    }
    setBgImg(imgUrls[cur]);
    img.attr("class", cur);
}

function getClientWidth() {
    return document.compatMode == 'CSS1Compat' || window.opera ? document.documentElement.clientWidth : document.body.clientWidth;
}

var ua = navigator.userAgent.toLowerCase();
var isOpera = (ua.indexOf('opera') > -1);
var isIE = (!isOpera && ua.indexOf('msie') > -1);

function getViewportHeight() {
    return ((document.compatMode || isIE) && !isOpera) ? (document.compatMode == 'CSS1Compat') ? document.documentElement.clientHeight : document.body.clientHeight : (document.parentWindow || document.defaultView).innerHeight;
}

function getDocumentHeight() {
    return Math.max(document.compatMode != 'CSS1Compat' ? document.body.scrollHeight : document.documentElement.scrollHeight, getViewportHeight());
}

function getBodyScrollTop() {
    return self.pageYOffset || (document.documentElement && document.documentElement.scrollTop) || (document.body && document.body.scrollTop);
}

function addEvent(obj, type, fn) {
    if (obj.addEventListener)
        obj.addEventListener(type, fn, false);
    else if (obj.attachEvent)
        obj.attachEvent( "on"+type, fn );
}

function removeEvent(obj, type, fn) {
    if (obj.removeEventListener)
        obj.removeEventListener(type, fn, false);
    else if (obj.detachEvent)
        obj.detachEvent( "on"+type, fn );
}

function mousePageXY(e) {
    var x = 0, y = 0;

    if (!e) e = window.event;

    if (e.pageX || e.pageY) {
        x = e.pageX;
        y = e.pageY;
    }
    else if (e.clientX || e.clientY) {
        x = e.clientX + (document.documentElement.scrollLeft || document.body.scrollLeft) - document.documentElement.clientLeft;
        y = e.clientY + (document.documentElement.scrollTop || document.body.scrollTop) - document.documentElement.clientTop;
    }

    return {
        "x":x,
        "y":y
    };
}

function isChildNode(elem, sell) {
    for (var childItem in elem.childNodes) {
        if (elem.childNodes[childItem].nodeType == 1) {
            if (elem.childNodes[childItem] == sell)
                return true;
            else if (isChildNode(elem.childNodes[childItem], sell))
                return true;
        }
    }
    return false;
}
