var js = {
    baseUrl : '',
    debug : false,
    backgroundimage : '',
    istouch : false,
    scrollinterval : 0,
    currentSection : '',
    auto_interval : 0,
    auto_interval_direction : 1,
    main  : function(debug, baseUrl, backgroundimage, currentSection, animation)
    {
        this.debug = debug;
        this.baseUrl = baseUrl;
        this.backgroundimage = backgroundimage;
        this.currentSection = currentSection;
        
        this.d("main func with arguments: ");
        for(var arg in arguments)
            this.d("- "+arguments[arg]);
            
        if(this.currentSection == 'home'){
            $j('#sitoIntero').hide();
        }
        var scope = this;
        $j.vegas({
            src : baseUrl+"assets/images/tipo_BG.jpg",
            fade : scope.currentSection == 'home' ? 3500 : 0,
            complete:function() {
                $j('#sitoIntero').fadeIn();
            }            
        });
            
        /*
        if(this.currentSection == 'company' && animation == 1){
            $j('#menu').hide();
            $j('#right').hide();
            $j('#company').hide();
            $j('#sword6644').show();
        }else{
            $j('#sword6644').hide();
        }
        
        if(this.currentSection == 'press'){
            $j('body').css('overflow', 'hidden');
        }
        
        var scope = this;
        $j.vegas({
            src : this.backgroundimage,
            fade : scope.currentSection == 'company' ? 3500 : 1000,
            complete:function() {
                if(scope.currentSection == 'company' && animation == 1){
                    $j('#sword6644').fadeOut(600, function(){
                        var menuLeft = parseInt($j("#menu").css('left').replace('px', ''));
                        var rightMenuRight = parseInt($j("#right").css('right').replace('px', ''));
                        scope.d(menuLeft + " | " +rightMenuRight);
                        $j("#menu").css('left', '-200px');
                        $j("#right").css('right', '-200px');
                        $j("#menu").show();
                        $j("#right").show();
                        $j('#menu').animate({left:menuLeft}, 600);
                        $j('#right').animate({right:rightMenuRight}, 600, function(){
                            $j('#company').fadeIn(600); 
                        });
                    });
                }
            }
        });
        */
        if(navigator.userAgent.match(/iPhone/i) || navigator.userAgent.match(/iPod/i) || navigator.userAgent.match(/iPad/i))
            this.istouch = true;
            
        if(this.istouch){
            /*
            $j('#sitoIntero').css('position', 'relative');
            $j('#sitoIntero').css('margin-left', '0');
            $j('#sitoIntero').css('margin-top', '100px');
            $j('#sitoIntero').css('margin-bottom', '0');
            $j('#sitoIntero').css('margin-right', '0');
            $j('#sitoIntero').css('top', '0');
            $j('#sitoIntero').css('left', '0');
            */
            $j('#sitoIntero').css('top', '0');
            $j('#sitoIntero').css('margin-top', '100px');
            $j('#sitoIntero').css('margin-bottom', '100px');
        }
            
    },
    d : function(message)
    {
        if(this.debug)
            console.log(message);
    },
    setscroll : function(idbutton1, idbutton2, idcontent, orientation, auto)
    {
        if(auto == undefined)
            auto = false;
            
        this.d("set scroll "+arguments[0]+ " | "+arguments[1]+ " | "+arguments[2]+ " | "+arguments[3]+ " | "+arguments[4]);
        /*
        if(this.istouch){
            $j('#'+idbutton1).hide();
            $j('#'+idbutton2).hide();
            return;
        }
        */
        if(orientation == "vertical"){
            if($j('#'+idcontent).attr('scrollHeight') <= $j('#'+idcontent).height()){
                $j('#'+idbutton1).hide();
                $j('#'+idbutton2).hide();
                return false;
            }
        }else{
            if($j('#'+idcontent).attr('scrollWidth') <= $j('#'+idcontent).width()){
                $j('#'+idbutton1).hide();
                $j('#'+idbutton2).hide();
                return false;
            }            
        }

        var scope = this;
        $j('#'+idbutton1).click(function(){
            return false;
        });
        $j('#'+idbutton2).click(function(){
            return false;
        });
        
        /* tap events */
        /*
        $j('#'+idbutton1).bind('tap', function(){
            scope.scroll(idcontent, -1, orientation);
            return false
        });
        $j('#'+idbutton2).bind('tap', function(){
            scope.scroll(idcontent, 1, orientation);
            return false;
        });*/
        /*end tap events*/
        $j('#'+idbutton1).bind('touchstart', function(){
            scope.scroll(idcontent, -1, orientation);
            return false
        });
        $j('#'+idbutton2).bind('touchstart', function(){
            scope.scroll(idcontent, 1, orientation);
            return false;
        });
        
        $j('#'+idbutton1).bind('touchend', function(){
            clearInterval(scope.scrollinterval);
            return false
        });
        $j('#'+idbutton2).bind('touchend', function(){
            clearInterval(scope.scrollinterval);
            return false;
        });
        
        $j('#'+idbutton1).mousedown(function(){
            scope.scroll(idcontent, -1, orientation);
            return false;
        });
        $j('#'+idbutton1).mouseup(function(){
            clearInterval(scope.scrollinterval);
            return false;
        });
        
        $j('#'+idbutton2).mousedown(function(){
            scope.scroll(idcontent, 1, orientation);
            return false;
        });
        $j('#'+idbutton2).mouseup(function(){
            clearInterval(scope.scrollinterval);
            return false;
        });
        
        if(auto && orientation == 'vertical'){
            this.d("vertical automation");
            var scope = this;
            clearInterval(this.auto_interval);
            this.auto_interval = setInterval(
                function(){
                    scope.d("interval");
                    var scrollTop = $j("#"+idcontent).attr('scrollTop');
                    var newScrollTop = scrollTop +1*scope.auto_interval_direction;
                    if(scope.auto_interval_direction == 1){
                        if(newScrollTop >= $j("#"+idcontent).attr('scrollHeight') - 430){
                            scope.auto_interval_direction = -1;
                        }else{
                            $j("#"+idcontent).attr({scrollTop: newScrollTop});
                        }
                    }else{
                        if(newScrollTop <= 1){
                            scope.auto_interval_direction = 1;
                        }else{
                            $j("#"+idcontent).attr({scrollTop: newScrollTop});
                        }
                    }
                },
                80
            );
            this.d(this.auto_interval);
        }else if(auto == true && orientation == 'horizontal'){
            
            var scope = this;
            clearInterval(this.auto_interval);
            this.auto_interval = setInterval(
                function(){
                    var scrollLeft = $j("#"+idcontent).attr('scrollLeft');
                    var newScrollLeft = scrollLeft +1*scope.auto_interval_direction;
                    if(scope.auto_interval_direction == 1){
                        if(newScrollLeft >= $j("#"+idcontent).attr('scrollWidth') - 379){
                            scope.auto_interval_direction = -1;
                        }else{
                            $j("#"+idcontent).attr({scrollLeft: newScrollLeft});
                        }
                    }else{
                        if(newScrollLeft <= 1){
                            scope.auto_interval_direction = 1;
                        }else{
                            $j("#"+idcontent).attr({scrollLeft: newScrollLeft});
                        }
                    }
                },
                80
            );
            
        }
        
    },
    scroll : function(id, direction, orientation)
    {
        clearInterval(this.auto_interval);
        var scope = this;
        clearInterval(this.scrollinterval);
        this.scrollinterval = setInterval(
            function(){
                scope.d("scrolling "+$j("#"+id)+ " in "+orientation);
                if(orientation == "vertical"){
                    var scrollTop = $j("#"+id).attr('scrollTop');
                    var newScrollTop = scrollTop +10*direction;
                    $j("#"+id).attr({scrollTop: newScrollTop});
                }else if(orientation == "horizontal"){
                    var scrollLeft = $j("#"+id).attr('scrollLeft');
                    var newScrollLeft = scrollLeft +10*direction;
                    $j("#"+id).attr({scrollLeft: newScrollLeft});
                }
            },
            50
        );   
    },
    changeCampaignImage : function(newImg, campaignThumbId)
   {
       $j('#campaignThumbsScroller a img').each(function(i,e){
           $j(e).css('opacity', 1);
       });
       $j('#'+campaignThumbId+" img").css('opacity', '.4');
       $j('#campaignImage img').attr('src', newImg);
   }
};
