/* Author: Matevz Mihalic

*/

var currpos = 3;

$(document).ready(function() {
    
    
    $('#gallery a').click(function() {
        if($(this).attr("id") != "first") {
            change_img($(this));
        } else if (!$('#gallery').hasClass("detail")) {
            return true;
        }
        
        return false;
    });
    
    currpos = 3;
    
    if(!$('#gallery').hasClass("detail")) {
        $('#gallery').height("248px");
        $('#gallery').width("320px");
        $('#gallery').css("overflow", "hidden");
        $('#gallery').randomize("a:not(#first)");
        
        $("#gallery #first").hover(function() {
            $(this).append('<div class="overlay"></div>');
        }, function() {
            $(this).find(".overlay").remove();
        });
    }
    
    if ($('#gallery.detail').hasClass("menulist")) {
        $("#first").append('<a href="#" id="galleryLevo">&nbsp;</a>');
        $("#first").append('<a href="#" id="galleryDesno">&nbsp;</a>');
        
        $("#first #galleryLevo, #first #galleryDesno").click(btnclicks);
        
        setPosition();
        
        $("#first #galleryDesno").fadeOut("fast");
        $("#first #galleryLevo").fadeOut("fast");
        
        $(window).resize(function() {
            var pos = $("#first img").position();
            $("#first #galleryLevo").css("left", pos.left + 20);
            $("#first #galleryDesno").css("left", (pos.left + $("#first img").width() - 60));
        });
        
        currpos = 2;
        
        $("#first img").mouseenter(function(e) {
            $(this).mousemove(function(e) {
                var x = e.pageX - this.offsetLeft;
                var y = e.pageY - this.offsetTop;
                
                if (x<($(this).width()/2)) {
                    if (!$("#first #galleryLevo").is(":visible")) {
                        $("#first #galleryLevo").fadeIn("fast");
                    }
                    if ($("#first #galleryDesno").is(":visible")) {
                        $("#first #galleryDesno").fadeOut("fast");
                    }
                } else {
                    if (!$("#first #galleryDesno").is(":visible")) {
                        $("#first #galleryDesno").fadeIn("fast");
                    }
                    if ($("#first #galleryLevo").is(":visible")) {
                        $("#first #galleryLevo").fadeOut("fast");
                    }
                }
            }).mouseleave(function(e) {
                if (($(e.relatedTarget).attr("id") != "galleryLevo") && ($(e.relatedTarget).attr("id") != "galleryDesno")) {
                    if ($("#first #galleryLevo").is(":visible")) {
                        $("#first #galleryLevo").fadeOut("fast");
                    }
                    if ($("#first #galleryDesno").is(":visible")) {
                        $("#first #galleryDesno").fadeOut("fast");
                    }
                }
                
            });
        }).click(btnclicks);
    }
    
    if (!$('#gallery').hasClass("detail") || $('#gallery').hasClass("list")) {
        setInterval("scroll_gallery()", 3000);
    }
    
});

function btnclicks() {
    if ($("#first #galleryLevo").is(":visible")) {
        if (currpos==2) {
            currpos=$('#gallery a').length-2;
        } else {
            currpos--;
        }
        console.log(currpos);
        change_img($('#gallery a:nth-child(' + currpos + '):not(#galleryLevo, #galleryDesno)'));
    }
    if ($("#first #galleryDesno").is(":visible")) {
        if ($('#gallery a').length == (currpos+2)) {
            currpos=2;
        } else {
            currpos++;
        }
        
        
        change_img($('#gallery a:nth-child(' + currpos + '):not(#galleryLevo, #galleryDesno)'));
    }
}

function change_img(img) {
    /*
    if ($("#first #galleryLevo").is(":visible")) {
        $("#first #galleryLevo").fadeOut("fast");
    }
    if ($("#first #galleryDesno").is(":visible")) {
        $("#first #galleryDesno").fadeOut("fast");
    }
    */
    
    $('#gallery #first').height( $('#gallery #first').height());
    $('#gallery #first img').fadeOut("fast", function() {
        var margin = $('#gallery h3').css("margin-bottom");
        //
        var href = $('#gallery #first img').attr("src");
        $('#gallery #first img').attr("src", img.attr("href"));
        img.attr("href", href);
        var small = $("img", img).attr("src");
        $("img", img).attr("src", $('#gallery #first').data("small"));
        $('#gallery #first').data("small", small);
        $('#gallery h3').css("margin-bottom", margin);
        $('#gallery #first img').fadeIn("fast", function() {
            if($('#gallery').hasClass("detail") && !$('#gallery').hasClass("list")) {
                $('#gallery #first').height( "auto");
                //$.scrollTo($("#gallery #first"), 200);
                if ($('#gallery.detail').hasClass("menulist")) {
                    setPosition();
                }
            }
        });
        img.fadeIn("fast");
        
        
    });
    img.fadeOut("fast");
    
}

function setPosition() {
    var pos = $("#first img").position();

    if((pos.left + 20) > (pos.left + $("#first img").width() - 60)) {
        setTimeout("setPosition()", 500);
    } else {
        $("#first #galleryLevo").css("top", (pos.top + $("#first img").height()/2 - 37));
        $("#first #galleryDesno").css("top", (pos.top + $("#first img").height()/2 - 37));
        $("#first #galleryLevo").css("left", pos.left + 20);
        $("#first #galleryDesno").css("left", (pos.left + $("#first img").width() - 60));
    }
    
}

function scroll_gallery() {
    change_img($('#gallery a:nth-child(' + currpos + ')'));
    //if ($('#gallery a').length == currpos-1) {
    if (currpos == 7) {
        currpos = 3;
    } else {
        currpos++;
    }
    
}

