function menuPodjetje() {
    $("#content").fadeOut('normal');
    $('#menu').show();
    if ($("div#menuContent").is(":visible")) {
        $('#menuContent').fadeOut('normal', function() {
            $('#menuContent').load('tpl/menuPodjetje_en.tpl').fadeIn();
        });
    } else {
        $('#menuContent').load('tpl/menuPodjetje_en.tpl').hide().fadeIn();
    }
}

function menuObjekti() {
    $("#content").fadeOut('normal');
    $('#menu').show();
    if ($("div#menuContent").is(":visible")) {
        $('#menuContent').fadeOut('normal', function() {
            $('#menuContent').load('tpl/menuObjekti_en.tpl').fadeIn();
        });
    } else {
        $('#menuContent').load('tpl/menuObjekti_en.tpl').hide().fadeIn();
    }
}

function menuRazno() {
    $("#content").fadeOut('normal');
    $('#menu').show();
    if ($("div#menuContent").is(":visible")) {
        $('#menuContent').fadeOut('normal', function() {
            $('#menuContent').load('tpl/menuRazno_en.tpl').fadeIn();
        });
    } else {
        $('#menuContent').load('tpl/menuRazno_en.tpl').hide().fadeIn();
    }
}

function home() {
    $("#content").fadeOut('normal');
    $('#menuContent').empty();
}
function sitemap() {
    $('#menu').show();
    $('#imageHolder').hide();
    $('#menuContent').load('tpl/menuRazno_en.tpl');
    $('#down').hide();
    $.get('sitemap.php?lang=en', function(data) {
        $('#content').html(data);
    });
    $('#content').fadeIn('normal');
}
function search(q) {
    $('#imageHolder').hide();
    //$('#down').load('search.php?q=' + q);
    $.post('search.php', {"q":q}, function(data) {
        $('#content').html(data);
    });
}

function GroupDelegate(id) {
    //console.log(id);
    //var objLink = document.getElementById(id);
    //Lightbox.prototype.start(objLink);
    $('#down a').lightBox();
    $('a#'+ id).trigger('click');
}

/***
    Simple jQuery Slideshow Script
    Released by Jon Raasch (jonraasch.com) under FreeBSD license: free to use or modify, not responsible for anything, etc.  Please link out to me if you like it :)
***/

function slideSwitch() {
    var $active = $('#slideshow IMG.active');

    if ( $active.length == 0 ) $active = $('#slideshow IMG:last');

    // use this to pull the images in the order they appear in the markup
    var $next =  $active.next().length ? $active.next()
        : $('#slideshow IMG:first');

    // uncomment the 3 lines below to pull the images in random order

    // var $sibs  = $active.siblings();
    // var rndNum = Math.floor(Math.random() * $sibs.length );
    // var $next  = $( $sibs[ rndNum ] );


    $active.addClass('last-active');

    $next.css({opacity: 0.0})
        .addClass('active')
        .animate({opacity: 1.0}, 1000, function() {
            $active.removeClass('active last-active');
        });
}

$(function() {

    flashembed("leftUp", "pontingen.swf");

    //$("#list").hide();
    setInterval( "slideSwitch()", 5000 );

    function addHoverHandlers() {
        //over objet image
        $('#list ul#objektiList li a').hover(
            function(e) {
                target = $(e.target);
                objId = target.attr('rel');
                //first hide all imges
                $('div#photosBack img').hide();
                $('div#photosBack').hide();
                //and the image frame
                //show current one
                if (!target.hasClass('noimg')) {
                    $('div#photosBack').show();
                    $('div#photosBack img#' + objId).show();
                }
            },
            function() {
                //console.log('Off the object');
            }
        );
        $('#list ul#objektiList').hover(
            function() {},
            function() {
                //set photoBack from active item
                selImg = $("input#selObj").val();
                if (selImg) {
                    $('div#photosBack img').hide();
                    $('div#photosBack').hide();
                    $('div#photosBack').show();
                    $('div#photosBack img#' + selImg).show();
                }
            }
        );

        //handler for objects
        $('#list').click(function(e) {
            obj = $(e.target).attr('rel');
            //load gallery and info
            if (obj) {
                $("#down").load("gallery.php?lang=en&id=" + obj);
                //mark active item
                $("ul#objektiList > li > a").removeClass('active');
                $(e.target).addClass('active');
                $("input#selObj").val(obj);
            }
        });
    }
    
    function addClickHandlers(type, key, objId) {

        //objekti in razno
        if (type == 'obj') {
            //for all objects
            $("#list").show();
            $("#list").load("objektiList.php?lang=en&key=" + key, function() {
                $('#pane1').jScrollPane({showArrows:false, scrollbarOnLeft:true, scrollbarMargin:35});
                addHoverHandlers();
            });

            if (objId) {
                $("#down").load("gallery.php?lang=sl&id=" + objId);
            }

        } else if (type == 'etc') {
            //for all objects
            $("#list").show();
            $("#list").load("raznoList.php?lang=en&key=" + key, function() {
                $('#pane1').jScrollPane({showArrows:false, scrollbarOnLeft:true, scrollbarMargin:35});
                addHoverHandlers();
            });

            if (objId) {
                $("#down").load("gallery.php?lang=sl&id=" + objId);
            }

        } else {
            $("#list").hide();
        }

        if (tpl == 'kje_nas_najdete') {
            $('#imageHolder').show();
            $('#imageHolder').load('tpl/kje_nas_najdete_en.tpl');
        } else if (tpl == 'dejavnost') {
            $('#imageHolder').show();
            $('#imageHolder').load('tpl/dejavnostSlike.tpl');
        } else if (tpl == "preglednica_strani") {
            sitemap();
        } else {
            $('#imageHolder').hide();
            $('#imageHolder').html('');
            $('.down').hide();
        }
    }

    var tpl;

    //handler for top menu
    $('#menuContent').click(function(e, template, mtype, objId) {

        //console.log("Mtype: " + mtype);

        target = $(e.target);
        if (template) {
            tpl = template;
        } else {
            tpl = target.attr('rel');
        }
        if (mtype) {
            type = mtype;
        } else if (target.hasClass('obj')) {
            type = 'obj';
        } else if (target.hasClass('etc')) {
            type = 'etc';
        } else {
            type = '';
        }

        $('#content').fadeOut('normal', function() {

            //mark active item
            if (tpl) {
                $("div.menuItem > a").removeClass('active');
                $(e.target).addClass('active');
            }

            //GET THE CONTENT
            $.get('content.php?lang=en&key=' + tpl, function(data) {
                $('#content').html(data);
                addClickHandlers(type, tpl, objId);
                $('#content').fadeIn('normal');
            });
        });
    });
   


    //global handler for content (sitemap, search,...)
    $('div#content').click(function(e) {
        target = $(e.target);
        var menu;
        var link;
        var objId;
        
        if (target.attr('rel')) {
            menu = target.attr('rel');
        } else {
            menu = "";
        }
        if (target.attr('href')) {
            if (target.attr('href').indexOf("#") >= 0) {
                //IE6&7
                if (!jQuery.support.hrefNormalized) {
                    linkParts = target.attr('href').split('#');
                    link = linkParts[1];
                } else {
                    link = target.attr('href').substr(1);
                }
                if (link.indexOf("-") >= 0) {
                    parts = link.split('-');
                    link = parts[0];
                    objId = parts[1];
                }
            } else {
                return true;
            }
        } else {
            link = "";
            objId = "";
        }

        //first load the correct menue
        if (menu == 'menuPodjetje') {
            menuPodjetje();
            //$("div#menuContent a[rel=" + link + "]").trigger('click');
            if (link) {
                $('div#menuContent').trigger('click', link);
            }
        } else if (menu == 'menuObjekti') {
            menuObjekti();
            if (target.hasClass('obj') && link) {
                $('div#menuContent').trigger('click', [link, 'obj', objId]);
            }
        } else if (menu == 'menuRazno') {
            if (target.hasClass('sitemapl')) {
                return false;
            }
            menuRazno();
            if (target.hasClass('etc') && link) {
                $('div#menuContent').trigger('click', [link, 'etc', objId]);
            } else if (link) {
                $('div#menuContent').trigger('click', link);
            }
        }
    });
});

