// remap $ to $
(function($){

    $.fn.slideMenu = function(settings) {

        var config = {
            duration: 200
        };
        if (settings) $.extend(config, settings);

        var bindLevel = function($l,l){

            if($l.children('ul').length>0){
                $l.children('ul').children('li').each(function(){
                    if($(this).children('ul').length>0){
                        $(this).children('ul').hide();
                    }
                    $(this).hover(function(){
                        $(this).children('ul').stop(true,true).slideDown(config.duration);
                    },function(){
                        $(this).children('ul').stop(true,true).slideUp(config.duration);
                    });

                    $(this).children('ul').children('li').each(function(){
                        if($(this).children('ul').length>0){
                            bindLevel($(this),l+1);
                        }
                    });

                });
            }

        }

        this.each(function() {

            var $t = $(this);
            bindLevel($t,0);

        });


        return this;

    };

    $.fn.jmenu = function(settings){
        var config = {
            easing:'linear',
            speed:200
        };
        if(settings) $.extend(config,settings);
        var mainobj = this;
        $('.ul>.li .ul',mainobj).each(function(){
            $(this).hide();
            var isFirst = ($(this).parent().parent().parent().attr('id')==$(mainobj).attr('id'))?true:false;
            if(isFirst){
                $(this).css({
                    top:$('a',$(this).parent()).outerHeight()+'px',
                    left:'0px'
                });
                $('a',$(this).parent()).css({
                    position:'relative',
                    zIndex:'100'
                });
                var width = $('a',$(this).parent()).width()+'px';
            }
            else{
                $(this).css({
                    top:'0px',
                    left:$('a',$(this).parent()).outerWidth()+'px',
                    zIndex:'9999'
                });
            }
        });
        $('.li',mainobj).hover(
            function(e){
                //                    e.stopPropagation();
                var isFirst = ($(this).parent().parent().attr('id')==$(mainobj).attr('id'))?true:false;
                if($(this).children('.ul').length>0){
                    if(isFirst){
                        $('.ul .li .ul',mainobj).hide();
                        $(this).children('.ul').stop(false,true).css({
                            visibility:'visible'
                        }).slideDown(config.speed);
                    }
                    else{
                        $(this).children('.ul').stop(false,true).show().animate({
                            left:$('a',$(this)).outerWidth(true)+'px'
                        },config.speed,config.easing).css({
                            visibility:'visible'
                        });
                    }
                }
            },
            function(e){
                var isFirst = ($(this).parent().parent().attr('id')==$(mainobj).attr('id'))?true:false;
                if(isFirst){
                    if($(this).children('.ul').length>0){
                        $(this).children('.ul').stop(false,true).css({
                            visibility:'hidden'
                        }).slideUp(config.speed);
                    }
                }
                else{
                    //                        e.stopPropagation();
                    if($(this).children('.ul').length>0){
                        $(this).children('.ul').stop(false,true).animate({
                            left:'0px'
                        },config.speed,config.easing).hide(config.speed,function(){
                            $(this).children('.ul').css({
                                visibility:'hidden'
                            })
                        });
                    }
                }
            }
            );
    };


        
    $.fn.fader = function(settings){
            
        var config = {
            speed: 1000,
            interval: 5000,
            squares: true,
            squaresCaptions: false,
	    maxsquares:false
        };
        if (settings) $.extend(config, settings);
        
        this.each(function(){
            var el = $(this);
        
            var $l = el.children('img').length;
            var $cur = 0;
            var ti = false;
        
            var next = function(i){
                if(config.squares){
                    $('.square',el).not('.square:eq('+i+')',el).removeClass('active');
                    $('.square:eq('+i+')',el).addClass('active');
                }
                $('img',el).not('img:eq('+i+')',el).fadeOut(config.speed);
                $('img:eq('+i+')',el).fadeIn(config.speed);
            }
            
            var start = function(){

                if(config.squares){
                    $('.square',el).not('.square:eq('+$cur+')',el).removeClass('active');
                    $('.square:eq('+$cur+')',el).addClass('active');
                }
                $('img',el).not('img:eq('+$cur+')',el).fadeOut(config.speed);
                $('img:eq('+$cur+')',el).fadeIn(config.speed);

                ti = window.setInterval(function(){
                    $cur = ($cur + 1) % $l;
                    next($cur);
                },config.interval);

            }
            
            var stop = function(){
                window.clearInterval(ti);
            }
        
            if($l>1){
                if(config.squares){
                    var sqrs = $('<div>').addClass('squares');

                    for(var i=0; i<$l; i++){
                        var sqr = $('<a>').attr('href','javascript:;').addClass('square');
                        if(config.squaresCaptions){
                            var caption = $('<span>').addClass('caption').text($('img:eq('+i+')',el).attr(config.squaresCaptions));
                            caption.appendTo(sqr);
                        }
                        if(i==0)
                            sqr.addClass('active');
                        sqr.appendTo(sqrs);
                    }
                    sqrs.appendTo(el);
                
                    $('.square',sqrs).click(function(){
                        stop();
                        $cur = $(this).index();
                        start();
                    });   

                }

                start();
            
            }else{
                $('img',el).fadeIn(config.speed)
            }
        });
        
        return this;
    }


})(this.$);

// usage: log('inside coolFunc',this,arguments);
// paulirish.com/2009/log-a-lightweight-wrapper-for-consolelog/
window.log=function(){
    log.history=log.history||[];
    log.history.push(arguments);
    if(this.console){
        console.log(Array.prototype.slice.call(arguments))
    }
};

// catch all document.write() calls
(function(doc){
    var write = doc.write;
    doc.write = function(q){
        log('document.write(): ',arguments);
        if (/docwriteregexwhitelist/.test(q)) write.apply(doc,arguments);
    };
})(document);


Object.size = function(obj) {
    var size = 0, key;
    for (key in obj) {
        if (obj.hasOwnProperty(key)) size++;
    }
    return size;
};

