var backgroundPages = {
    init : function(){
        if(specific_img){
            $('body').css('background', 'url('+specific_img+') repeat scroll 0 0 transparent');
            return;
        }
        if($.cookie("bg_pattern")){
            $('body').css('background', 'url('+$.cookie("bg_pattern")+') repeat scroll 0 0 transparent');
        }else if(default_pattern){
            $('body').css('background', 'url('+default_pattern+') repeat scroll 0 0 transparent');
        }
        if($('#switch').hasClass('opened')){
            backgroundPages.toggleBox(this);
        }
        
        backgroundPages.setNewBackground();
        //backgroundPages.checkUserDimensions();
        backgroundPages.clearBackground();
        backgroundPages.switchBox();
        backgroundPages.resetCookie();
        $('#scrollbar1').tinyscrollbar();
    },
    /* checkUserDimensions : function(){
        var image_src = $('body').css('background-image');
        image_src = image_src.replace(/url[(]/, '');
        image_src = image_src.replace(/[)]/, '');
        var imgObj = new Image();
        imgObj.onload = function() {
            if(imgObj.width > 600){
                $('body').addClass('background_size')
            }
        }
        imgObj.src = image_src;
    }, */
    setNewBackground : function(){
        $('#patterns .var').click(function(){
            var img = $(this).find('img').css({width: '', height: ''}); 
            var img_width = img.width();
            img.css({width: '30px', height: '30px'}); 
            /* if(img_width < 600) {
                $('body').css('background', 'url('+$(this).find('img').attr('src')+') repeat scroll 0 0 transparent').removeClass('background_size');
            } else {
                $('body').css({
                    background: 'url('+$(this).find('img').attr('src')+') repeat scroll 0 0 transparent', 
                })
                .addClass('background_size');
            } */
            $('body').css('background', 'url('+$(this).find('img').attr('src')+') repeat scroll 0 0 transparent');
            $('#patterns .var, #patterns .non-var').removeClass('active');
            $(this).addClass('active');
            $.cookie("bg_pattern", $(this).find('img').attr('src'));
        });
    },
    clearBackground : function(){
        $('#del_bg').click(function(){
            $('body').css('background', 'none');
            $('#patterns .var').removeClass('active');
            $(this).addClass('active');
            $.cookie("bg_pattern", "none");
        });
    },
    resetCookie : function(){
        $('#reset_cookie').click(function(){
            $.cookie("bg_pattern", null);
            $('body').css('background', 'url('+default_pattern+') repeat scroll 0 0 transparent');
            $('#patterns .var, #patterns .non-var').removeClass('active');
            var is_empty = true;
            $('#patterns div div').each(function(){
                if($(this).find('img').attr('src')==default_pattern){
                    is_empty = false;
                    $(this).addClass('active');
                }
            });
            if(is_empty){
                $('#patterns .non-var').addClass('active');
            }
        });
    },
    switchBox : function(){
        $('#switch').click(function(){
            backgroundPages.toggleBox(this);
        });
        if(page_name=='index'){
            backgroundPages.toggleBox('#switch');
        }
    },
    toggleBox : function(obj){
        $(obj).toggleClass('opened').toggleClass('closed');
        var $lefty = $('.bg_controller');
        $lefty.animate({
          left: parseInt($lefty.css('left'),10) == 0 ?
            -$lefty.outerWidth() :
            0
        });
    }
}

//when document is loaded...
$(document).ready(function(){
    backgroundPages.init();
})