var CONTENT_MARGIN_INIT = 60;
var CONTENT_MARGIN_END = 40;

var LOADER_MARGIN_INIT = 220;
var LOADER_MARGIN_END = 200;

var CONTENT_WIDTH;

function array_unique (inputArr) {
    // http://kevin.vanzonneveld.net
    // +   original by: Carlos R. L. Rodrigues (http://www.jsfromhell.com)
    // +      input by: duncan
    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   bugfixed by: Nate
    // +      input by: Brett Zamir (http://brett-zamir.me)
    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Michael Grier
    // +   bugfixed by: Brett Zamir (http://brett-zamir.me)
    // %          note 1: The second argument, sort_flags is not implemented;
    // %          note 1: also should be sorted (asort?) first according to docs
    // *     example 1: array_unique(['Kevin','Kevin','van','Zonneveld','Kevin']);
    // *     returns 1: {0: 'Kevin', 2: 'van', 3: 'Zonneveld'}
    // *     example 2: array_unique({'a': 'green', 0: 'red', 'b': 'green', 1: 'blue', 2: 'red'});
    // *     returns 2: {a: 'green', 0: 'red', 1: 'blue'}

    var key = '', tmp_arr2 = {}, val = '';

    var __array_search = function (needle, haystack) {
        var fkey = '';
        for (fkey in haystack) {
            if (haystack.hasOwnProperty(fkey)) {
                if ((haystack[fkey] + '') === (needle + '')) {
                    return fkey;
                }
            }
        }
        return false;
    };

    for (key in inputArr) {
        if (inputArr.hasOwnProperty(key)) {
            val = inputArr[key];
            if (false === __array_search(val, tmp_arr2)) {
                tmp_arr2[key] = val;
            }
        }
    }

    return tmp_arr2;
}


function open(page) {
	if (!full && !in_animate) {					
		in_animate = true;
		
		$('#body').animate({width : CONTENT_WIDTH + 300}, 1000, "easeOutBounce", function () {			
			$('.column_right').css('display','block').animate({ width: CONTENT_WIDTH }, 1000, 'easeInOutCirc');
			$('#main-content').animate({ width: CONTENT_WIDTH + 215 }, 1000, 'easeInOutCirc', function () {
					in_animate = false;
					full = true;					
					
					load_page(page);						
			});
		});
	}				
}

function redimension(wd,callback) {	
	if( CONTENT_WIDTH != wd ) {
		if ( wd < 400 ) wd = 400;
		else if ( wd > 800 ) wd = 800;
		
		if (full && !in_animate) {
			in_animate = true;							
			$('#body').animate({width : wd + 300}, 1000, "easeInOutCirc");
			$('.column_right').animate({ width: wd }, 1000, 'easeInOutCirc');
			$('#main-content').animate({ width: wd + 215 }, 1000, 'easeInOutCirc', function () {
					in_animate = false;			
					CONTENT_WIDTH = wd;
					if(callback) {
						callback();
					}
			});
		}
	} else {
		if(callback) {
			callback();
		}
	}
}

function redimension_page() {		
	hh = $('#main-content .column_left').outerHeight(true) > $('#main-content .column_right').outerHeight(true) ? $('#main-content .column_left').outerHeight(true) : $('#main-content .column_right').outerHeight(true);
	
	//console.log('Redimencionando... W: %d, H: %d - %d', $(window).width(), $(window).height(), hh );
	
	if( $(window).height() > hh ) {
		$('#main-content','#body .columns').height( $('body').height() );
	} else {
		$('#main-content','#body .columns').height( hh );
	}
}

function close() {
	if (full && !in_animate) {
		in_animate = true;
		$('#nav li').removeClass('selected');
		$('.column.right').fadeOut("slow", function () {
			$('#body').animate({width : 320}, 1500, 'easeInOutCirc');		
			$('#main-content').animate({width: '-='+CONTENT_WIDTH+'px'}, 1000, 'easeOutBounce', function () { full = false; in_animate = false; page = 'home'; $('.column.right .content').empty(); CONTENT_WIDTH = 400; });
		});
	}
}

function show_loader(callback) {
	$('.column_right .content').css({marginTop : CONTENT_MARGIN_END, opacity : 1}).stop().animate( {marginTop: CONTENT_MARGIN_INIT, opacity: 0}, "fast", function () {
		$('.column_right .loader').css({marginTop: LOADER_MARGIN_INIT, display: 'block'});
		$('.column_right .content').css({ display: 'none' });
		$('.column_right .loader').animate( {marginTop: LOADER_MARGIN_END, opacity: 1}, "normal", function () {
			if( callback ) {
				callback();				
			}
		});
	});	
}

function hide_loader() {
	queue.exec('before');
	
	redimension( $('.column_right .content').outerWidth(), function () { 
		$('.right .loader').css({marginTop : LOADER_MARGIN_END, opacity : 1}).stop().animate( {marginTop: LOADER_MARGIN_INIT, opacity: 0}, "fast", function () {					
			$('.right .loader').css({display: 'none'});
			$('.right .content').css({marginTop: CONTENT_MARGIN_INIT, display: 'block'});
			$('.right .content').animate( {marginTop: CONTENT_MARGIN_END, opacity: 1}, "normal", function ()  {			
				queue.exec('after');								
				redimension_page();
			});
		});				
	} );
}								

function load_page(pg) {
	show_loader(function () {		
		$.ajax({
			url: base_url + pg,
			type: 'GET',
			dataType: 'html',
			
			success: function (data) {				
				setTimeout( 'hide_loader()', 300 );								
				$('.right .content').html(data);							
			}											
		});				
	});				
}

var photo = {	
	init : function (base) {
		$("a[rel^='prettyPhoto']",$(base)).prettyPhoto({
			theme:'dark_rounded',
			overlay_gallery: false,
			changepicturecallback: function () {
				_gaq.push(function () {
					pageTracker._trackPageview($(this).attr('href'));
				});	
			}
		});				
	}
}

var gallery = {
	init : function () {		
		
		$('ul.carrousel').each(function () {
			if( $('> li',this).length > 6 ) {
				var id = $(this).parent().parent().parent().parent().attr('id');
			
				$(this).parent().jCarouselLite({
					btnPrev: '#' + id + ' a.prev',
					btnNext: '#' + id + ' a.next',
					visible: 6,
					circular: false
				});
				
				$(this).parent().prev().removeClass('off');
				$(this).parent().next().removeClass('off');					
			} else if ( $('> li',this).length < 6 )  {
				//$(this).parent().next().css('margin-left', -5);
			}
		});
		
		photo.init('#gallery');
	}
};

redimension_page();

$(document).ready(function () {
	CONTENT_WIDTH = ( page == 'home' ? 400 : $('.column_right .content').outerWidth(true) );
	
	redimension_page();
	
	$('.button').button();	
	
	/*$('#all').bind('mousemove', function () { $('#body').animate({opacity: 1},800); });*/
	
	window.onresize =  function () {
		redimension_page();
	}	
	
	$('#main-content .credits a').hover(function () {
		$(this).stop().animate({opacity: 1},"normal");
	},
	function () {
		$(this).stop().animate({opacity: 0.6},"normal");
	});
	
	$('.logo a').click(function (event) { event.preventDefault(); top.location.hash = ''; close(); });
	
	$('#nav li a').tooltip({showURL: false, extraClass: 'ui-corner-all', delay: 500});
	
	$('#nav li a').click(function (event) {
		event.preventDefault();
		
		top.location.hash = '#!' + $(this).attr('href');
		
		var li = $(this).parent();
		
		if(!li.hasClass('selected') && li.attr('class') != page) {									
			page = li.attr('class');
			
			queue.add('after',function () {
				$('#body').attr('class',page);
			});
			
			$('#nav li').removeClass('selected');
			$(this).parent().addClass('selected');
			
			if( full ) {
				load_page(page);
				_gaq.push(function () {
					pageTracker._trackPageview($(this).attr('href'));
					pageTracker._setCustomVar(1,'categoria_acessos',$(this).parentAll('h3').eq(0).text(),3);
				});
			} {
				open(page);
			}
		}
	});

	queue.exec();
});					
