var _core              = {

    init    : function() {
        this.after();
    },

    before  : function() {
        //
    },
    after   : function() {
		_mainBanner.init();
		_mainNav.init();
    }
};


var _exists     = function(obj) {
    return ( $(obj).length  > 0 ) ? true : false;
};

var trace = function(value){
    try
    {
        console.log(value);            
    }
    catch(err)
    {
        alert(value)
    }
};

var _mainNav              = {

    init    : function() {
        if (this.check())  this.core();
    },

    def     : {
        element             :   'nav#main-navigation'        
    },

    check   : function() {
         return (_exists(_mainNav.def.element)) ? true : false;
    },

    core    : function() {
		var page = $("body").attr('class');
		$("#main-navigation li[rel=" + page + "]").addClass('selected')
	}
    
};


var _UI          = {
    init    : function() {
        if ( this.check() ) {
			// init
           // this.hoverFx();
			//this.listManagers();
			//this.showCaseMenus()
			//this.tooltips()
        }
    },
    check   : function() {
        return true
    },
	listManagers    : function() {
		
		// all tables
		$("table tbody").ozListManager(); 

		// lists	
		$("#mainContent ol").ozListManager();
		$(".grid_07 ul").ozListManager(); 			
	},
	showCaseMenus    : function() {
		// columns
		$("div.grid_31 , div.grid_15").ozShowCaseMenu ({ 
            duration    :   500
		});
        // footer and sitemap effects
        $("#sitemap > ul li.item").ozShowCaseMenu ({ 
            duration    :   500
        }); 
        
        $("#sitemap > ul li.item > ul li").ozShowCaseMenu ({ 
            duration    :   500
        }); 
        
		$("#institutional nav ul li").ozShowCaseMenu ({ 
            duration    :   500
        });  
        /*$("#footer ul#availables li").ozShowCaseMenu ({ 
            duration    :   500
        }); 
        */  
	},
	tooltips    : function() {
	
		// avaliables logos on footer right
		$("#footer ul#availables li a").ozTooltip ({ 
            duration    :   500,
            moveTo      :   -180,
            returns     :   -200
        });
	},
    hoverFx    : function() {
        var easing = 'easeOutQuad';
            
       // 'acesse sua conta' button on top right       
        $('#account-infos h3 a').mouseover( function(){
           $(this).stop().parent().animate(
                {backgroundPosition:"(-760px 0px)"}, 
                {duration:500}, easing 
           );

           $(this).stop().animate(
                {top:"12px"}, 
                {duration:500}, easing 
           );            
       })
       .mouseout(function(){
            $(this).stop().parent().animate(
                {backgroundPosition:"(-760px -10px)"}, 
                {duration:500}, easing 
           ); 
            $(this).stop().animate(
                {top:"7px"}, 
                {duration:500}, easing 
           );
             
       })
       
       // 'não sou cliente' button on top right     
        $('#account-infos p a').mouseover( function(){
           $(this).parent().stop().animate(
                {backgroundPosition:"(-866px 0px)"}, 
                {duration:500}, easing 
           );

           $(this).stop().animate(
                {top:"12px"}, 
                {duration:500}, easing 
           );            
       })
       .mouseout(function(){
            $(this).parent().stop().animate(
                {backgroundPosition:"(-866px -10px)"}, 
                {duration:500}, easing 
           ); 
            $(this).stop().animate(
                {top:"7px"}, 
                {duration:500}, easing 
           );
             
       })
       
        // aside background hover effect
        $('#content aside section li').hover( 
           function(){
               $(this).stop().animate(
                    {backgroundPosition:"(-50px -571px)"}, 
                    {duration:500}, easing 
                );
                $(this).children('a').animate(
                    {backgroundPosition:"(5px -71px"}, 
                    {duration:300}, easing 
                );
                
            },                  
            function(){
                $(this).stop().animate(
                    {backgroundPosition:"(-450px -570px)"}, 
                    {duration:300}, easing
                );
                $(this).children('a').animate(
                    {backgroundPosition:"(0px -71px"}, 
                    {duration:300}, easing 
                );
            }
       ) 

    }
};

/*  Google Analytics  */
var pageTracker = '';
var google             = {

    analytics   : {

        init    : function() {

            if (this.check()) {
                this.core();
            }
        },

        check   : function() {

            if ( typeof _gat == 'object' ) this.core();
        },

        core    : function() {
            try {
                pageTracker = _gat._getTracker(libraries.gAnalytics.code);
                pageTracker._setDomainName(libraries.gAnalytics.domain);
                pageTracker._trackPageview();
            } catch (err) { }
        }
    }
};

/* Gerencia o column system, eliminando a class quando houver menos de 1 coluna */
var _columns           = {
    init    : function() {
        if (this.check())  this.core();
    },

    def     : {
        element         :   'div.column'

    },

    check   : function() {
        var total = $(this.def.element).length;
        if ( total > 0 )  this.core( total )
    },

    core    : function(total) {
        
        if(total < 2)
        {
            //console.log($(this.def.element).length)
            $(this.def.element).each(function(){
                var gridSystem = $(this).attr('class').split(' ')[1];
                if(gridSystem.split('_')[0] == 'grid')
                {
                    $(this).removeClass (gridSystem).addClass('single'); 
                }
            })
        }
        else
        {
            //trace('2 ou mais colunas')
        }
    
    }
};

var _mainBanner         = {

    init    : function() {
        if (this.check())  this.core();
    },

    def     : {
        content: '#slides'
    },

    check   : function() {
        return ( jQuery(_mainBanner.def.content).length ) ? true : false;
    },

    core    : function() {
        /* This code is executed after the DOM has been completely loaded */
	
			var totWidth=0;
			var positions = new Array();
			
			$('#slides .slide').each(function(i){
				
				/* Traverse through all the slides and store their accumulative widths in totWidth */
				
				positions[i]= totWidth;
				totWidth += $(this).width();
				
				/* The positions array contains each slide's commulutative offset from the left part of the container */
				
				if(!$(this).width())
				{
					alert("Please, fill in width & height for all your images!");
					return false;
				}
				
			});
			
			$('#slides').width(totWidth);

			/* Change the cotnainer div's width to the exact width of all the slides combined */

			$('#menu ul li a').click(function(e,keepScroll){

					/* On a thumbnail click */

					$('li.menuItem').removeClass('act').addClass('inact');
					$(this).parent().addClass('act');
					
					var pos = $(this).parent().prevAll('.menuItem').length;
					
					$('#slides').stop().animate({marginLeft:-positions[pos]+'px'},450);
					/* Start the sliding animation */
					
					e.preventDefault();
					/* Prevent the default action of the link */
					
					
					// Stopping the auto-advance if an icon has been clicked:
					if(!keepScroll) clearInterval(itvl);
			});
			
			$('#menu ul li.menuItem:first').addClass('act').siblings().addClass('inact');
			/* On page load, mark the first thumbnail as active */
			
			
			
			/*****
			 *
			 *	Enabling auto-advance.
			 *
			 ****/
			 
			var current=1;
			function autoAdvance()
			{
				if(current==-1) return false;
				
				$('#menu ul li a').eq(current%$('#menu ul li a').length).trigger('click',[true]);	// [true] will be passed as the keepScroll parameter of the click function on line 28
				current++;
			}

			// The number of seconds that the slider will auto-advance in:
			
			var changeEvery = 5;

			var itvl = setInterval(function(){autoAdvance()},changeEvery*1000);

			/* End of customizations */
			
			//_linkManager.init();
			//_UI.init();

    }
};

$(document).ready(function() {
	_core.init();
});
