AUI().use('cookie', 'trf-scroll-nav', 'aui-tabs', 'substitute', 'aui-paginator', function(A) {
	var body = A.one('body');
	var head = A.one('head');

	/* Configurações */

	MENU_MAX_ITEMS = 12;

	/* Scroll Nav */

	var scroll = A.one('.scroll-nav');

	if (scroll != null) {
		var scrollNav = new A.ScrollNav({
			boundingBox: '.scroll-nav',
			items: '.scroll-nav > li'
		})
		.render();

		var count = scrollNav.get('items').size();
		A.one('#scrollCount').set('innerHTML', count + ' itens');

		A.one('#scrollDown').on('click', A.bind(scrollNav.next, scrollNav, 6));
		A.one('#scrollUp').on('click', A.bind(scrollNav.prev, scrollNav, 6));

			A.one('#scrollDown').on('mousedown', function(){


			A.one('#scrollDown').setStyle('background-position','0 -27px');


			});
		A.one('#scrollDown').on('mouseup', function(){


		A.one('#scrollDown').setStyle('background-position','0 0');


		});

		A.one('#scrollUp').on('mousedown', function(){


			A.one('#scrollUp').setStyle('background-position','0 -27px');


			});
		A.one('#scrollUp').on('mouseup', function(){


		A.one('#scrollUp').setStyle('background-position','-55px 0');


		});



	}

	/* Navigation */

	var centralize = function(node, target) {
		node.setX(target.getX() + ((target.get('offsetWidth') - node.get('offsetWidth')) / 2 )+ 2);

		var wrapper = A.one('#wrapper');

		if (wrapper) {
			wrapperLeft = wrapper.getX();
			wrapperRight = wrapperLeft + wrapper.get('offsetWidth');

			if (node.getX() < wrapperLeft) {
				node.setX(wrapperLeft);
			}

			if (node.getX() + node.get('offsetWidth') > wrapperRight) {
				node.setX(wrapperRight - node.get('offsetWidth'));
			}
		}
	}

	var hideMenus = function(force){
		clearTimeout(window.navigationDelay);

		var hide = function(){
			A.all('#navigation ul .overlay-menu, .navigation-arrow').setStyle('display', 'none');
		}

		if (force) {
			hide();
		}
		else {
			window.navigationDelay = setTimeout(hide, 300);
		}
	}

	var getMenuArrow = function() {
		var arrow = A.one('.navigation-arrow');

		if (!arrow) {
			arrow = A.Node.create('<div class="navigation-arrow"></div>');

			A.one(document.body).append(arrow);
		}

		return arrow;
	}

	var getOverlayMenu = function(){
		return A.Node.create('<div class="overlay-menu"><h2></h2><a href="javascript:;" class="ico-close"></a></div>');
	}

	var unLimitMenu = function(menu) {
		menu.all('li').show();

		var esconder = A.Node.create('<li class="esconder"><a href="javascript:void(0);">Esconder</a></li>');

		esconder.on('click', function() {
			limitMenu(MENU_MAX_ITEMS, menu);
		});

		menu.append(esconder);

		var vejaMais = menu.one('.veja-mais');

		if (vejaMais) {
			vejaMais.remove();
		}
	}

	var limitMenu = function(limit, menu) {
		menu.all('li').each(function(item, index, list){
			if (index >= limit) {
				item.hide();
			}
		});

		var vejaMais = A.Node.create('<li class="veja-mais"><a href="javascript:void(0);">Veja mais</a></li>');

		vejaMais.on('click', function() {
			unLimitMenu(menu);
		});

		menu.append(vejaMais);

		var esconder = menu.one('.esconder');

		if (esconder) {
			esconder.remove();
		}
	}

	A.all("#navigation ul ul ul").remove();

	A.all('#navigation ul ul').each(function(currentMenu){
		var overlayMenu = getOverlayMenu();

		currentMenu.get('parentNode').append(overlayMenu);
		overlayMenu.append(currentMenu);

		if (currentMenu.all('li').size() > MENU_MAX_ITEMS) {
			limitMenu(MENU_MAX_ITEMS, currentMenu);
		}
	});

	A.delegate('click', function(e) {
		hideMenus(true);
	}, '#navigation', '.ico-close');

	A.delegate('click', function(e){
		var target = e.currentTarget;
		var overlayMenu = target.get('parentNode').one('.overlay-menu');

		if (overlayMenu) {
			e.preventDefault();
		}

		e.stopPropagation();
	}, '#navigation > ul', '> li > a');

	A.delegate('mouseleave', function(e) {
		hideMenus();
	}, '#navigation > ul', '> li');

	A.delegate('mouseenter', function(){
		clearTimeout(window.navigationDelay);
	}, 'body', '.navigation-arrow');

	A.delegate('mouseenter', function(e) {
		var arrow = getMenuArrow();
		var target = e.currentTarget;
		var overlayMenu = target.one('.overlay-menu');

		if (overlayMenu) {
			hideMenus(true);

			overlayMenu.one('h2').set('innerHTML', target.one('a').text());

			overlayMenu.setStyle('display', 'block');
			arrow.setStyle('display', 'block');

			centralize(arrow, target);
			centralize(overlayMenu, target);

			var arrowY = overlayMenu.getY() - arrow.get('offsetHeight');
			arrow.setY(arrowY);
		}
	}, '#navigation > ul', '> li');

	/* Minimize section */

	A.delegate("click", function(e) {
		var target = e.currentTarget;
		var parent = target.ancestor('.content-separator');

		parent.all('.aui-column-first, .aui-column-last').toggle();
		target.toggleClass('down');
		parent.toggleClass('down');

    }, "#wrapper", ".bt-minimize");

    /* Font size */

    var getFontSize = function() {
    	return parseFloat(body.getStyle('fontSize'));
    }

    var setFontSize = function(fontSize) {
		A.one(document.body).setStyle('fontSize', fontSize + 'px');
    }

    var variation = parseFloat(A.Cookie.get("font_variation"));

    if (!variation || isNaN(variation)) {
    	variation = 1;
    }

    var originalFontSize = getFontSize();

    setFontSize(originalFontSize * variation);

    A.one('.ico-font-plus').on(
    	'click',
    	function() {
    		variation += 0.1;

			setFontSize(originalFontSize * variation);

			A.Cookie.set("font_variation", variation);
    	}
    );

	A.one('.ico-font-minus').on(
    	'click',
    	function() {
    		variation -= 0.1;

			setFontSize(originalFontSize * variation);

			A.Cookie.set("font_variation", variation);
    	}
    );

	/* Contrast */
	
	/*
    
    var setContrastMode = function() {
		body.addClass('contrast');
    }

    var unsetContrastMode = function() {
		body.removeClass('contrast');
    }

    if (A.Cookie.get("contrast_mode") == 'on') {
    	setContrastMode();
    }

    A.one('.ico-contrast').on(
    	'click',
    	function() {
			if (!body.hasClass('contrast')) {
				setContrastMode();

				A.Cookie.set("contrast_mode", "on");
			}
			else {
				unsetContrastMode();

				A.Cookie.set("contrast_mode", "");
			}
    	}
    );
    
    */
    
    /* Browser */

    if (A.UA.gecko > 0) {
    	body.addClass('margin-fix');
    }

    /* Paginator */

    window.paginator = new A.Paginator({
		containers: '.paginator',
		total: 10,
		maxPageLinks: 10,
		rowsPerPage: 5,
		rowsPerPageOptions: false,
		nextPageLinkLabel: 'Próxima',
		prevPageLinkLabel: 'Anteriror',
		pageReportLabelTemplate: '({page} de {totalPages})',
		template: '{PrevPageLink} {PageLinks} {NextPageLink} {CurrentPageReport}',
		on: {
			changeRequest: function(event) {
				var instance = this;
				var newState = event.state;
				var page = newState.page;

				var content = A.one('#content');

				if (newState.before) {
					var lastPage = newState.before.page;

					content.unplug(A.Plugin.IO);
					content.plug(A.Plugin.IO, {
						uri: "index.php",
						method: 'GET',
						data: {
							option: 'com_ultimas_noticias',
							view: 'main',
							format: 'raw',
							page: page,
							year: paginator.get('year'),
							month: paginator.get('month')
						},
						showLoading: true
					});
				}

				instance.setState(newState);
			}
		}
	})
	.render();
});

