$().ready( function() {

	//////////////////////////* Portal da Transparência *////////////////////////

	var configurations = {
		 yScroll: "top"                          // the position of the marquee initially scroll (can be 
                                                 // either "top" or "bottom")
       , showSpeed: 850                          // the speed of to animate the initial dropdown of the messages
       , scrollSpeed: 12                         // the speed of the scrolling (keep number low)
       , pauseSpeed: 5000                        // the time to wait before showing the next message or 
                                                 // scrolling current message
       , pauseOnHover: true                      // determine if we should pause on mouse hover
       , loop: -1                                // determine how many times to loop through the marquees 
                                                 // (#'s < 0 = infinite)
       , fxEasingShow: "swing"                   // the animition easing to use when showing a new marquee
       , fxEasingScroll: "linear"                // the animition easing to use when showing a new marquee

       // event handlers
       , init: null                              // callback that occurs when a marquee is initialized
       , beforeshow: null                        // callback that occurs before message starts scrolling on screen
       , show: null                              // callback that occurs when a new marquee message is displayed
       , aftershow: null                         // callback that occurs after the message has scrolled	
	}

	$("#obras").marquee(configurations);
	$("#servidores").marquee(configurations);
	$("#licitacoes").marquee(configurations);
	$("#concursos").marquee(configurations);
	$("#aniversarios").marquee(configurations);

	/////////////////////////////////////////////////////////////////////////////

	/////////////////////////////* Highlights News */////////////////////////////

	newsHighlights();

	activeHighlights();

	var a = 1;
	var total = 0;

	$(".news-highlight-menu").find('a').each( function(){
		total++;
	});

	if(a < total){
		$.timer(8000, function (timer) {
			
			if(a < total) a++;
			else a = 1;

			$('.news-highlight-menu a[href=#news-highlight-'+a+']').click();
			
			$(".news-highlight-menu").find('div.text').each( function(){
				$(this).removeClass('active');
			});
			$('.news-highlight-menu a[href=#news-highlight-'+a+']').parent().parent().addClass('active');

			//timer.stop();
		});		
	}

	/////////////////////////////////////////////////////////////////////////////

	///////////////////////////////////* History *///////////////////////////////

	historyAjax();

	var u = 1;
	var totalH = 5;
	var stageH = 0;

	if(u < totalH){
		$.timer(8000, function (timerHistory) {
			
			if(u < totalH && stageH == 0) {
				u++;
				stageH = 0;
			}
			else if(u > 1) {
				u--;
				stageH = 1;
			}
			else if(u == 1) {
				u++;
				stageH = 0;
			}

			if(stageH == 0) $('.pagination a.next').click();
			else if(stageH == 1) $('.pagination a.prev').click();

			//timerHistory.stop();
		});		
	}

	/////////////////////////////////////////////////////////////////////////////

});

//////////////////////////////////* History */////////////////////////////////

function historyAjax(){

	$('.history .pagination a').live("click", function(){

		var page = $(this).attr('href');
		page = page.split('pagina=');
		page = page[1];

		jQuery.ajax({
			type: "POST",
			url: "modules/home/history-ajax.php",
			data: "pagina="+page,
			success: dropsResponse
		});

		return false;
	
	});

}

function dropsResponse(response){

	$('.history ul').fadeOut("slow", function(){

		$('.history').html(response).find('ul').each(function(){
		
			$(this).hide().fadeIn("slow");
		
		});
	
	});

}

//////////////////////////////////////////////////////////////////////////////

/////////////////////////////* Highlights News */////////////////////////////

function newsHighlights() {
	$("#news-highlight").wslide({
		width: 280,
		height: 210,
		pos: 2,
		horiz: true,
		duration: 1000,
		fade: true,
		autolink: false
	});
}

/////////////////////////////////////////////////////////////////////////////

function activeHighlights(){

	$('.news-highlight-menu a').bind('click', function(){

		$(".news-highlight-menu").find('div.text').each( function(){
			$(this).removeClass('active');
		});
		$(this).parent().parent().addClass('active');

		return false;

	});

}
