$(function() {
    
    $.fn.slideMenu = function(demo){
        var t=$(this), nav = t.find('ul:first'), navTimer, ulOpen=null, demo = demo || false, n = 'navDemo';
        
        nav.hide();
            
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                if (cookie.substring(0, n.length + 1) == (n + '=')) {
                    demo = (decodeURIComponent(cookie.substring(n.length + 1)) == "done") ? false : true;
                    break;
                }
            }
        }

        if(demo === true) {
            setTimeout(function(){ t.addClass('open'); nav.slideDown(750); }, 1000);
            setTimeout(function(){ nav.slideUp(750, function() {
                t.removeClass('open');
                nav.children('li').find('ul').hide();
            })}, 2250);
            document.cookie = n+"=done";
        } else {
            nav.children('li').find('ul').hide();
        }
        
        t.hover(function(){
            if(navTimer) { clearTimeout(navTimer); }
            t.addClass('open');
            nav.slideDown(250);
        },function(){
            navTimer = setTimeout(function(){
                nav.slideUp(250, function(){
                    if(ulOpen) {
                        ulOpen.find('ul').hide();
                        ulOpen.children('span').removeClass('current');
                        ulOpen = null;
                    }
                    t.removeClass('open');
                }
            )}, 750);
        });
        
        nav.children('li').click(function(){
            if(ulOpen) {
                ulOpen.children('span').removeClass('current');
                ulOpen.find('ul:first').slideUp(350);
                ulOpen = null;
            }
            
            ulOpen = $(this);
            hasUl = ulOpen.find('ul:first').slideDown(350, function() {
                ulOpen.children('span').addClass('current');
            });
            
            if(hasUl.length <= 0) {
                ulOpen = null;
            }
        });
    };
    
    $.fn.scrollableCount = function(c, n) {
        var api = $(this).data("scrollable");
        
        $(c).text(api.getSize());
        $(n).text('1');
        
        api.onSeek(function() {
            $(n).text(api.getIndex()+1);
        });
    };
    
    $.fn.newsMgmt = function() {
        var o, sApi, tApi, nt = $("#newsText"), ts = $('div.txtScroll');
        
        ts.customScroller();
        
        $("div.pane a.ntTrigger").click(function() {
            var id = $(this).attr('rel');
            nt.show(400);
            o = $(id).show();
            ts.setCustomScroller();
        });
        
        $("#newsText a.newsTextClose").click(function() {
            nt.hide(400, function() { o.hide(); });
        });
        
        $('div.scrollableItem').slideUp();
        
        this.find('h2').each(function() {
            var id = $(this).attr('id'), x = 'div.'+id+' div.prevNext';
            $('div.'+id).scrollable({items: '#newsImagesItems', next: x+' a.next', prev: x+' a.prev', onSeek: function() {
                var t = $("div."+id+" div.photo img").eq(this.getIndex()).attr("title");
                $('div.'+id+' div.photoMeta div.txt').text(t);
                $(x+' span.galleryCount').text(this.getSize());
                $(x+' span.galleryNumber').text(this.getIndex()+1);
            }});
            
            sApi = $('div.'+id).data("scrollable").seekTo(0);
        });
        
        return this.each(function() {
            
            tApi = $(this).tabs("#newsBox div.pane", {
                tabs: 'h2', 
                effect: 'default',
                initialIndex: 0,

                onClick: function(t, i) {
                    var o = $('div.scrollableItem'), d = this.getIndex();

                    o.eq(d).slideUp(300);
                    o.eq(i).slideDown(300);
                }
            });
        });
    };

    $.fn.galleryOverlay = function() {
        var elements = $.makeArray(this);

        return this.each(function() {
            var i = $.inArray(this, elements);
            api = $(this).overlay({
                target: "#pOverlay",
                top: '13%',
                mask: {color: '#000000', startOpacity: 0.8, closeSpeed: 0},

                onBeforeLoad: function() {
                    var overlay = this.getOverlay(), imgURL = this.getTrigger().attr("href"), imgTitle = this.getTrigger().attr("title");
                    
                    function clearBG() {
                        overlay.css("background", "#FFFFFF").find(".progress").show();
                    }

                    if(i > 0) {
                        clearBG();
                        overlay.find("a.prev").show().unbind("click").click(function(evt) {
                            $(elements[i-1]).data("overlay").load();
                        });
                    } else {
                        overlay.find("a.prev").hide();
                    }

                    if(i < (elements.length - 1)) {
                        clearBG();
                        overlay.find("a.next").show().unbind("click").click(function(evt) {
                            $(elements[i+1]).data("overlay").load();
                        });
                    } else {
                        overlay.find("a.next").hide();
                    }
                    
                    $("<img/>").attr("src", imgURL).css("visibility", "hidden").bind("load", function() {
                        var img = $(this), width = img[0].width, height = img[0].height, left = Math.floor(($(window).width() - width) / 2 );
                        $(overlay).animate({width: width, height: height, left: left}, 350, function() {
                            $(overlay).css("background", "#FFFFFF url(" + imgURL + ") no-repeat 12px 12px");
                        }).find(".progress").hide();
                        $("#pOverlay div.info").html('<span>'+imgTitle+'</span><br /><span>'+(i+1)+' / '+elements.length+'</span>');
                    }).each(function() {
                        if(this.complete) {
                            $(this).trigger("load");
                        }
                    });
                }
            });
        });
    };
    
    $.fn.boxImage = function() {
        var img_box = "/images/checkbox.png";
        var img_box_c = "/images/checkbox_c.png";
        var img_radio = "/images/radio.png";
        var img_radio_c = "/images/radio_c.png";
        var img_class = "box_" + Math.round((Math.random()+1*571773)*Math.random());
        
        $(this).each(function() {
            var obj = $(this);
            var type = obj.attr('type'); // radio or checkbox
            var id = obj.attr('id');

            obj.css('display','none');

            if(type == 'radio'){
                obj.before('<img src="' + img_radio + '" alt="'+type+'" class="' + img_class + '" id="box_img_' + id + '" style="cursor:pointer;" />');
                
                if(obj.attr('checked')){ $("#box_img_" + id).attr('src', img_radio_c); }
                
                $("#box_img_"+id).click(function(){
                    $("." + img_class).attr('src', img_radio);
                    $("#" + id).trigger("click");
                    $(this).attr('src', img_radio_c);
                });

            } else if(type == 'checkbox'){
                obj.before('<img src="' + img_box + '" alt="'+type+'" class="' + img_class + '" id="box_img_' + id + '" style="cursor:pointer;" />');
                
                if(obj.attr('checked')){ $("#box_img_" + id).attr('src', img_box_c); }
                
                $("#box_img_" + id).click(function(){
                    $("#" + id).trigger("click");
                    if($(this).attr('src') == img_box){ $(this).attr('src', img_box_c); }
                    else { $(this).attr('src', img_box); }
                });
            }
        });
    };
    
    $.fn.slideLoginRegister = function() {
        var t = $(this), o = null;
        
        t.each(function() {
            var id = $(this).attr('id');
            if(id != 'lbFb') { $('.'+id+'Box').slideUp(); }
        });
        
        t.click(function() {
            var id = $(this).attr('id');
            if(o) { o.slideUp(200); }
            if(id != 'lbFb') {
                o = $('.'+id+'Box').slideDown(300);
            } else {
                o = null;
            }
        });
    };
    
    $.fn.txtScroller = function() {
        var t = $(this);
        t.customScroller();
        $('img', t).load(function() {
            t.setCustomScroller();
        });
    };
    
});

