var backImgManager;
var sizes;
var curBackImg, nextBackImg;
var wrapper;

function readyState(){
    //initSizes();
    $(window).resize(function(){
        initSizes();
    }).resize();
    
    $(this).bind("contextmenu", function(e) {
                    e.preventDefault();
    }); 
                
    $("div").attr('unselectable', 'on')
    .css({
        '-moz-user-select':'none',
        '-webkit-user-select':'none',
        'user-select':'none'
    })
    .each(function() {
        this.onselectstart = function() {
            return false;
        };
    });
    
    var galeria = new galeriaController();
    
    $("#slider").cycle({
        fx: 'fade',
        timeout: '5000',
        speed : "2000"
    });
    
    $("#back-img").cycle({
        fx: 'fade',
        timeout: '6000',
        speed : "2000",
        before : function(currSlideElement, nextSlideElement){
            curBackImg = currSlideElement;
            nextBackImg = nextSlideElement;
        },
        pager:  '#imgs-galeria', 
        pagerAnchorBuilder: function(idx, slide) { 
            // return selector string for existing anchor 
            return '#imgs-galeria a:eq(' + idx + ')'; 
        } 
    });
    
    //$("#header").css("opacity", "0.8");
    $("#content").css("opacity", "0.95");
    
    $("#page").attr("myHeight", $("#page").height());
    $("#content")
    .mouseover(function(){
        openPage();
    })
    .mouseout(function(){
        closePage();
    });
    
    window.setTimeout(function(){
        $("#content").mouseout();
    }, 3000);
    //.delay(5000).mouseout();
    
    
    initForm();
    
    
//initMovement();
}

function initForm(){
    $("#form-limpar").click(function(){
        $("#form-faleconnosco")[0].reset();
    });
    $("#form-enviar").click(function(){
        var canSubmit = true;
        $(".validable").each(function(){
            if($(this).val().length < 5){
                $(this).animate({
                    "background-color" : "#ffc0c0"
                }, 1000);
                //$(this).css("background-color", "#ffc0c0");
                canSubmit = false;
            }
        });
        if(canSubmit)
            $("#form-faleconnosco")[0].submit();
    });
    
    $(".validable").focus(function(){
        $(this).animate({
            "background-color" : "#fff"
        }, 1000);
    //$(this).css("background-color", "white");
    });
}

function galeriaController(){
    var me = this;
    this.init = function(){
        this.leftButton = $("#galeria-left");
        this.rightButton = $("#galeria-right");
        this.container = $("#imgs-galeria");
        this.galeria = $("#galeria-container");
        this.actualImg = 0;
        this.leftButton.click(function(){
            me.previousImg();
        });
        this.rightButton.click(function(){
            me.nextImg();
        });
    }
    
    this.load = function(){
        var size = 0;
        this.images = new Array();
        $(".thumb").each(function(){
            size += $(this).width() + 10;
            me.images.push($(this));
            $(this).wrap("<a href='#' class='no-style'/>");
        });
        this.container.width(size);
        
        if(size <= $("#galeria-container").width()){
            me.leftButton.fadeOut("fast");
            me.rightButton.fadeOut("fast");
        }
    }
    
    this.nextImg = function(){
        if(me.actualImg < me.images.length -1){
            me.actualImg ++;
            var pos = me.images[me.actualImg].parent().position();
            if(pos.left < me.container.width() - me.galeria.width())
                me.container.animate({
                    "margin-left" : -pos.left + "px"
                }, 500);
        }
    }
    
    this.previousImg = function(){
        if(me.actualImg > 0){
            if(me.actualImg > me.images.length - 5)
                me.actualImg = me.images.length - 6;
            else
                me.actualImg --;
            var pos = me.images[me.actualImg].parent().position();
            if(-pos.left <= 0)
                me.container.animate({
                    "margin-left" : -pos.left + "px"
                }, 500);
        }
    }
    
    this.init();
    this.load();
}

function closePage(){
    $("#page").stop().animate({
        "height" : "0px"
    }, 1500);
}

function openPage(){
    $("#page").stop().animate({
        "height" : $("#page").attr("myHeight") + "px"
    }, 500);
}

function initMovement(){
    wrapper = $("#wrapper");
    $(document).mousemove(function(e){
        moveBackImg(e);
    //moveWrapper(e);
    });
}

function moveBackImg(e){   
    var img = $(nextBackImg);
    var imgSize = img.height();
    if(imgSize > sizes.documentY){
        var mousePercent = 100 * e.pageY / sizes.documentY;
        var difSize = imgSize - sizes.documentY;
        var newMargin = difSize * mousePercent / 100
        img.css("margin-top", -newMargin + "px");
    }
    imgSize = img.width();
    if(imgSize > sizes.documentX){
        mousePercent = 100 * e.pageX / sizes.documentX;
        difSize = imgSize - sizes.documentX;
        newMargin = difSize * mousePercent / 100
        img.css("margin-left", -newMargin + "px");
    }
}

function moveWrapper(e){
    var mousePos = e.pageX;
    var wrapperPos = parseInt(wrapper.css("margin-left"));
    var wrapperSize = wrapper.width();
    if(mousePos < wrapperPos){
        wrapper.css("margin-left", mousePos + "px");
    }else if(mousePos > wrapperPos + wrapperSize){
        wrapper.css("margin-left", mousePos - wrapperSize + "px");
    }
}

function initSizes(){
    sizes = {
        documentX : $("#back-img").width(),
        documentY : $("#back-img").height()
    }
    
    $("#back-img").children("img").each(function(){
        //alert($(this).height() + " - " + sizes.documentX);
        if($(this).height() < sizes.documentY){
            $(this).css({
                "width" : "auto",
                "height" : "100%"
            });
            var imgWidth = $(this).width();
            if(imgWidth > sizes.documentX){
                var dif = imgWidth - sizes.documentX;
                $(this).css("margin-left", -dif/2 + "px");
            }
        }else{
            var imgHeight = $(this).height();
            if(imgHeight > sizes.documentY){
                var dif = imgHeight - sizes.documentY;
                $(this).css("margin-top", -dif/2 + "px");
            }
        }
    });
}

function calculateBackImgSize ($this){
    $this.css({
        width : "100%",
        height :"auto",
        left : "0px"
    });
    if($this.height() < sizes.documentY){
        $this.css({
            width : "auto",
            height : "100%"
        });
        $this.css("left", (sizes.documentX - $this.width())/2 + "px");
    }
}

function loadPage(page){
    $("#content").load(page + ".php");
    $.ajax({
        url: "ajax/manageSession.php?page="+page,
        cache: false
    });
}
