var TIM = 6000;
var lis ;

var imageList = [
    {url: "img/lf1.jpg"},
    {url: "img/lf2.jpg"},
    {url: "img/lf3.jpg"},
    {url: "img/lf4.jpg"},
    {url: "img/lf5.jpg"}
];

function getGroupItems(opts) {
    jQuery.each(imageList, function(i, val) {
        opts.itemArray.push(val);
    });
}

$(document).ready(function() {
    $('a.fb').fancybox(
    {
        hideOnContentClick:    true,
        zoomSpeedIn:        500,
        zoomSpeedOut:        500,
        frameWidth:            400,
        frameHeight:        300,
        overlayShow:        true,
        overlayOpacity:        0.8,
        itemLoadCallback:    null
    });
    $('a.fbv').fancybox(
    {
        hideOnContentClick:    true,
        zoomSpeedIn:        500,
        zoomSpeedOut:        500,
        frameWidth:            400,
        frameHeight:        300,
        overlayShow:        true,
        overlayOpacity:        0.8,
        itemLoadCallback:    null
    });
    $('a.fbc').fancybox(
    {
        hideOnContentClick:    false,
        zoomSpeedIn:        500,
        zoomSpeedOut:        500,
        frameWidth:            400,
        frameHeight:        300,
        overlayShow:        true,
        overlayOpacity:        0.8,
        'itemLoadCallback': getGroupItems
    });


    lis = $("td#destaque ul li");
    lis.css("display", "none");
    $("td#destaque ul").css("display", "inline");


    $(lis[0]).css("display", "inline").attr("imon", 1);
    window.setTimeout(destaquesTimer, TIM);

    $("input[type=text],textarea").bind("click focus", clearFormVal);

    if (getQueryVariable("didMail") != null) {
        var na = $(document.createElement("A"));
        $(document.body).append(na);
        na.attr("href", "thanks.html").fancybox({
            "frameWidth":400,
            "frameHeight":100,
            "hideOnContentClick":true
        }).click();
    }

    if ($.cookies.get("email")) $("#email").val($.cookies.get("email")).attr("cl", true);
    if ($.cookies.get("nome")) $("#nome").val($.cookies.get("nome")).attr("cl", true);
    if ($.cookies.get("telefone")) $("#telefone").val($.cookies.get("telefone")).attr("cl", true);
    if ($.cookies.get("distrito")) $("#distrito").val($.cookies.get("distrito")).attr("cl", true);
	if ($.cookies.get("obs")) $("#obs").val($.cookies.get("obs")).attr("cl", true);
});

function clearFormVal() {
    if (!$(this).attr("cl"))
        $(this).attr("cl", true).val("");
}

function destaquesTimer() {
    var lion = $("td#destaque ul li[imon=1]");
    if (lis.length <= 1 || !lion.length) return;
    var pos = jQuery.inArray(lion[0], lis);
    var npos = pos == (lis.length - 1) ? 0 : pos + 1;

    lion.animate({opacity:0}, TIM / 15, function() {
        lion.attr("imon", 0);
        lion.css({display:"none"});
        lion = $(lis[npos]);
        lion.attr("imon", 1);
        lion.css({opacity:0,display:"inline"});
        lion.animate({opacity:1}, TIM / 15);
        window.setTimeout(destaquesTimer, TIM);
    })
}

function checkform() {
    var filled = ($("#email").attr("cl") && $("#email").val().length > 5 && $("#email").val().indexOf("@")>1 && $("#email").val().indexOf(" ")<0) ||
                 ($("#telefone").attr("cl") && $("#telefone").val().length > 8);
    if (!filled)
    {
        alert("Deve preencher o telefone ou o email");
    }
    else
    {
        $.cookies.set("email", $("#email").val());
        $.cookies.set("nome", $("#nome").val());
        $.cookies.set("telefone", $("#telefone").val());
        $.cookies.set("distrito", $("#distrito").val());
		$.cookies.set("obs", $("#obs").val());	
        $("#contact").attr("action", "domail.jsp");
        return true;
    }
    return false;
}

function getQueryVariable(variable) {
    var query = window.location.search.substring(1);
    var vars = query.split("&");
    for (var i = 0; i < vars.length; i++) {
        var pair = vars[i].split("=");
        if (pair[0] == variable) {
            return pair[1] == null ? "" : pair[1];
        }
    }
    return null;
}

