// substituicao de imagem
// http://www.webdesignerwall.com/tutorials/jquery-tutorials-for-designers/
// http://www.webdesignerwall.com/demo/jquery/img-replacement.html

var s = null;

$(document).ready(function(){  
    // slideshow
    $.getJSON("slideshow.php", { category: $('#category').val() }, function(json) { 
        s = new SlideShow(json); 
        $("#slideshow").show();

        // play slideshow
        s.start('s');
    });

    // efeito nos thumbnails
    efeitos_thumbnails();

    // galeria, thumbs grid
    $(".thumbs li").click(function(){
        galeria(this);
    });

    // shuffle thumbs on click at gray image
    $(".empty").click(function() {
        thumbs();
    });

    // mail link
    var mymailel = "";
    $(".rcpt").hover(function() {
        var at = / -at- /;
        var dot = / -dot- /g;
        var addr = $(this).attr("title").replace(at,"@").replace(dot,".");

        mymailel = $(this).replaceWith($('<a id="rcpt" href="mailto:' +addr+ '">'
            +$(this).text()+ '</a>'));
        },
        function() {
                $(this).replaceWith($(mymailel));
    });

    // contact form
    $("form").submit(function(){
        var str = $("form").serialize();
        $.ajax({ type: "POST", url: "contact.php", data: str, success: function(msg){
            $("#note").ajaxComplete(function(event, request, settings){
                if(msg == 'OK') { // Message Sent? Show the 'Thank You' message and hide the form
                    result = '<div class="notification_ok">Your message has been sent. Thank you!</div>';
                    $("#fields").hide();
                }
                else {
                    result = msg;
                }

                $(this).html(result);
            });
        } });
        
        return false;
     });
});

function efeitos_thumbnails() {
    $(".thumbs img").fadeTo("slow", 0.6); // This sets the opacity of the thumbs to fade down to 60% when the page loads
    $(".thumbs img").hover(function(){
        $(this).fadeTo("slow", 1.0); // This should set the opacity to 100% on hover
            },function() {
                $(this).fadeTo("slow", 0.6); // This should set the opacity back to 60% on mouseout
            });
}

function galeria(elemento) {
    var largePath = $(elemento).attr("title");

    if ( largePath ) {
        $("#largeImg").attr({ src: largePath });
        $("#largeImgA").attr({ href: largePath });
    }
}

function tabchange(page) {
    s.stop('s');
    $.ajax({url:page,type:"post",data:"",success:function(data){$("#main_content").html(data);}})
    
}

// slideshow inicio
// http://jonraasch.com/blog/a-simple-jquery-slideshow

// thumbs
function thumbs() {
    // pedido de ajax para fazer refresh dos thumbnails
    $.get("thumbs.php", { category: $('#category').val() }, function(data){
        $(".thumbs").html(data);

        $(".thumbs li").unbind();

        // efeitos
        efeitos_thumbnails();

        // shuffle thumbs on click at empty
        $(".empty").click(function() {
            thumbs();
        });
    });
}

//
function myLink(args) {
    // if exists, remove
    $('#LinkForm').remove();

    // create a hidden div
    var $hiddenDiv = $('<div style="hidden" />');

    /// create the form
    var $form = $('<form name="LinkForm" id="LinkForm" method="POST" action="" />');
    var $submitData = $('<input type="hidden" name="category" id="category" value="'+args+'"/>');
    $($form).append($submitData);
    $($hiddenDiv).append($form);
    $('body').append($hiddenDiv);
    $('#LinkForm').submit();
    teste();
}

// slideshow
function SlideShow(files)
{
    this._files = files;
    //console.debug(this._files);
    this._current_index = -1;
    this._interval = null;

    this.next = function() {   
        this._current_index += 1;
        if (this._current_index > (this._files.length - 1))
            this._current_index = 0;

        //hide img
        $("#slideimg").remove();

        var obj_img = $("<img id='slideimg' />");
        
        //change img attributes
        $(obj_img).attr('height', this._files[this._current_index]['height']);
        $(obj_img).attr('width', this._files[this._current_index]['width']);
        $(obj_img).attr('src', this._files[this._current_index]['file']);
        $(obj_img).attr('style', 'opacity: 1');
        $(obj_img).attr('class', '');
        
        //show img
        $("#main_content").html(obj_img);
    }

    this.start = function(object_name)
    {
        this.next();
        this._interval = window.setInterval(object_name + ".next();", 3500);
    }

    this.stop = function() {
        if (this._interval != null)
            window.clearInterval(this._interval);
    }
}

function ShowImg(file)
{
    s.stop();
    $("#main_content").html("<img src='"+file+"' >");
}

function StopSlideShow()
{
    s.stop();
}

function StartSlideShow()
{
    s.start('s');
}
