var body_height = $('body').height();
var body_width = $('body').width();

/*
* JavaScript Debug - v0.3 - 6/8/2009
* http://benalman.com/projects/javascript-debug-console-log/
*
* Copyright (c) 2009 "Cowboy" Ben Alman
* Dual licensed under the MIT and GPL licenses.
* http://benalman.com/about/license/
*
* With lots of help from Paul Irish!
* http://paulirish.com/
*/
window.debug=(function(){var c=this,e=Array.prototype.slice,b=c.console,i={},f,g,j=9,d=["error","warn","info","debug","log"],m="assert clear count dir dirxml group groupEnd profile profileEnd time timeEnd trace".split(" "),k=m.length,a=[];while(--k>=0){(function(n){i[n]=function(){j!==0&&b&&b[n]&&b[n].apply(b,arguments)}})(m[k])}k=d.length;while(--k>=0){(function(n,o){i[o]=function(){var q=e.call(arguments),p=[o].concat(q);a.push(p);h(p);if(!b||!l(n)){return}b.firebug?b[o].apply(c,q):b[o]?b[o](q):b.log(q)}})(k,d[k])}function h(n){if(f&&(g||!b||!b.log)){f.apply(c,n)}}i.setLevel=function(n){j=typeof n==="number"?n:9};function l(n){return j>0?j>n:d.length+j<=n}i.setCallback=function(){var o=e.call(arguments),n=a.length,p=n;f=o.shift()||null;g=typeof o[0]==="boolean"?o.shift():false;p-=typeof o[0]==="number"?o.shift():n;while(p<n){h(a[p++])}};return i})();

(function ($) {
	// VERTICALLY ALIGN FUNCTION
	$.fn.vAlign = function() 
	{
		return this.each(function(i){
		var ah = $(this).height();
		var ph = $(this).parent().height();
		var ppt = parseInt($(this).parent().css('paddingTop'));
		var ppb = parseInt($(this).parent().css('paddingBottom'));
		var mh = (ph - ah) / 2;
		$(this).css('margin-top', mh);
		});
	};
	
})(jQuery);

function makeScrollable(wrapper, scrollable){
	// Get jQuery elements
	var wrapper = $(wrapper), scrollable = $(scrollable);
	
	// Hide images until they are not loaded
	scrollable.hide();
	
	// Timeout added to fix problem with Chrome
	setTimeout(function(){
		
		// Remove scrollbars	
		wrapper.css({overflow: 'hidden'});						
		
		scrollable.slideDown('slow', function(){
			enable();	
		});					
	}, 1000);	
	
	function enable(){
		// height of area at the top at bottom, that don't respond to mousemove
		var inactiveMargin = 99;					
		// Cache for performance
		var wrapperWidth = wrapper.width();
		var wrapperHeight = wrapper.height();
		// Using outer height to include padding too
		var scrollableHeight = scrollable.outerHeight() + 2*inactiveMargin;
		// Do not cache wrapperOffset, because it can change when user resizes window
		// We could use onresize event, but it's just not worth doing that 
		// var wrapperOffset = wrapper.offset();
		
		var lastTarget;
		//When user move mouse over menu			
		wrapper.mousemove(function(e){
			// Save target
			lastTarget = e.target;

			var wrapperOffset = wrapper.offset();
			
			// Scroll menu
			var top = (e.pageY -  wrapperOffset.top) * (scrollableHeight - wrapperHeight) / wrapperHeight - inactiveMargin;
			if (top < 0){
				top = 0;
			}			
			wrapper.scrollTop(top);
		});
	}
};

var aba_default_width = $('#aba').width();
var aba_open_close_width = $('#aba a#aba_open').width();

var toggleSidebar = function()
{
	aba_width = $('#aba').width();
	
	// Abrindo aba
	if ( $('#aba').is('.close') ) 
	{
		$('#aba_inner').show();
		$('#aba')
			.css('width', aba_default_width)
			.animate(
					{ marginLeft: 0 }, 
					750, 
					'easeOutBounce', 
					function() 
					{																				
						$(this).removeClass('close').addClass('open');													 
					}
			)
		;
	}
	// Fechando aba
	else
	{
		$('#aba')
			.animate(
				{ marginLeft: -(aba_width - aba_open_close_width) }, 
				1000, 
				function()
				{		
					$(this).removeClass('open').addClass('close').css({ 'width' : aba_default_width, 'marginLeft' : -(aba_default_width - aba_open_close_width)});										 
				}
			)
			.find('#aba_loader').hide().end();
		;
	}
	
	$('#aba_open, #aba_close').animate({ "opacity": "toggle" }, 1000);
};

var changeBrand = function(url)
{
	var location_url = ( typeof url !== "undefined" ) ? url : OTHER_BRAND_URL;
	
	$('#aba').removeClass('displayNone');
	var aba_class = ( $('#aba').is('.open') ) ? 'open' : 'close';
	
	$('#aba_inner').hide();
	$('#aba_loader').show();
	
	$('#aba').animate({ marginLeft: 0, width: body_width }, 1010, function(){
		if ( aba_class == 'open' ) 
		{
			$('#aba_open, #aba_close').animate({ "opacity": "toggle" }, 0);
		}
		$(this).removeClass('close').addClass('open');
		setTimeout('toggleSidebar()', 750);
		setTimeout('location.href = "'+location_url+'"', 1750);
	});
	
	$('#aba_open, #aba_close').animate({ "opacity": "toggle" }, 1000);
};