/** liScroll plugin **/
jQuery.fn.liScroll = function(settings) {
		settings = jQuery.extend({
		travelocity: 0.04
		}, settings);		
		return this.each(function(){
				var $strip = jQuery(this);
				$strip.addClass("newsticker")
				var stripWidth = 0;
				var $mask = $strip.wrap("<div class='mask'></div>");
				var $tickercontainer = $strip.parent().wrap("<div class='tickercontainer'></div>");								
				var containerWidth = $strip.parent().parent().width();	//a.k.a. 'mask' width 	
				$strip.find("li").each(function(i){
				stripWidth += jQuery(this, i).width();
				});
				$strip.width(stripWidth);			
				var defTiming = stripWidth/settings.travelocity;
				var totalTravel = stripWidth+containerWidth;								
				function scrollnews(spazio, tempo){
				$strip.animate({left: '-='+ spazio}, tempo, "linear", function(){$strip.css("left", containerWidth); scrollnews(totalTravel, defTiming);});
				}
				scrollnews(totalTravel, defTiming);				
				$strip.hover(function(){
				jQuery(this).stop();
				},
				function(){
				var offset = jQuery(this).offset();
				var residualSpace = offset.left + stripWidth;
				var residualTime = residualSpace/settings.travelocity;
				scrollnews(residualSpace, residualTime);
				});			
		});	
};

jQuery(function() {
	jQuery("#nav .children.home").appendTo("#nav li.homepage");
    jQuery("#nav .children.negocios").appendTo("#nav .cat-item-1");
	jQuery("#nav .children.empresas").appendTo("#nav .cat-item-4");
	jQuery("#nav .financas").prependTo("#nav .cat-item-5 .children");
	jQuery("#nav .todosCanais").prependTo("#nav .cat-item-12 .children");
	
	jQuery("#nossosCanais .section").jCarouselLite({
        btnNext: "#nossosCanais .nav .next",
        btnPrev: "#nossosCanais .nav .prev",
        visible:4,
        scroll:4
    });
    jQuery(".hid").hide();
		jQuery('#dateFilter').click( function() { 
      if (jQuery("#dateFilter").is(":checked")) {jQuery(".hid").slideDown();} else {jQuery(".hid").slideUp('slow');}
		});
    jQuery('.selectBox').sSelect({ddMaxHeight: '200px'});

	/** Scroll Effect for Ficancial Indicators **/
   	jQuery("ul.financialindicators").liScroll(); 

	jQuery("#noticiasMaisLidas li p").hide();
	jQuery("#noticiasMaisLidas li p:first").show();
	
	/** Comment Box**/
	jQuery(".hidden").hide();
	jQuery(".commentNow a").colorbox({width:"590px", height:"567px", inline:true, href:"#commentsBox", transition:"fade", preloading:"false"});
	jQuery(".commentsCount a").colorbox({width:"590px", height:"567px", inline:true, href:"#commentsBox", transition:"fade", preloading:"false"});
	
	
	/** Change to a class and make it compatible with every tabs and accordion call **/
	jQuery("#tabsNews").tabs();
	jQuery("#newsletterAndRecommend").tabs();
	
	jQuery(".accordion").accordion({event: "mouseover",autoHeight: false});


	jQuery("a[title='Enviar Notícia']").colorbox();
	
	jQuery.datepicker.setDefaults({
		dateFormat: 'dd/mm/yy',
		dayNames: [  
		'Domingo','Segunda','Terça','Quarta','Quinta','Sexta','Sábado','Domingo'  
		],  
		dayNamesMin: [  
		'D','S','T','Q','Q','S','S','D'  
		],  
		dayNamesShort: [  
		'Dom','Seg','Ter','Qua','Qui','Sex','Sáb','Dom'  
		],  
		monthNames: [  
		'Janeiro','Fevereiro','Março','Abril','Maio','Junho','Julho','Agosto','Setembro',  
		'Outubro','Novembro','Dezembro'  
		],  
		monthNamesShort: [  
		'Jan','Fev','Mar','Abr','Mai','Jun','Jul','Ago','Set',  
		'Out','Nov','Dez'  
		],  
		nextText: 'Próximo',  
		prevText: 'Anterior'
	});

	
	jQuery("#dtpickStart").datepicker({ 
		
		onSelect: function(dateText, inst){

			splitDate = dateText.split("/");
			var dateLimit = new Date(splitDate[2], splitDate[1]-1, splitDate[0]);			
            jQuery("#dtpickEnd").datepicker('option', 'minDate', dateLimit);
		}

	});

	jQuery("#dtpickEnd").datepicker();

	jQuery(".resultArea .extraInfo").hide();
	jQuery(".resultArea .item").hoverIntent(
      function () {
        jQuery(this).children(".extraInfo").slideDown();
      }, 
      function () {
        jQuery(this).children(".extraInfo").slideUp();
      }
    );
    
    
    /**Tabbed system**/
    function makeAccordion() {jQuery(".accordion").accordion('destroy').accordion({event: "mouseover",autoHeight: false});}
        
    jQuery(".listOfCategories a").click(function(){
    	var parentDiv 	= jQuery(this).parents("div").get(0);
		var url 		= jQuery(this).attr("href")+"&cachebuster=" + Math.floor(Math.random()*10001);

		jQuery(this).parents(".listOfCategories").find("li").removeClass("current");
		jQuery(this).parent("li").addClass("current");


		jQuery(parentDiv).find(".accordion")
			.html('<li class="loading">... Carregando ...</li>')
	        .load(url,'',makeAccordion);


		return false;
	});
    
    
    
    
    
    
});
