var highlightsPreload = new Array;

$(document).ready(function(){

	$(highlightsPreload[actualHighlight]).load(function(){
		initHighlights();
	});
	
	$('.hcontrol').live('click', function(){
		highlightControl( $(this) );
	})
	
	setTimeout( function(){ loadWeather() }, 10);
	
	/* .column_a_bigger: protects against bigger images in texts */
	$('.column_a_bigger .text img').each( function(){
		var x = $(this).attr('width');
		var maxWidth = 610;
		if( x > maxWidth ){

			var y = $(this).attr('height');
			var proportion = x / y;
			
			$(this).attr('width', maxWidth);
			var newY = parseInt(maxWidth / proportion );
			$(this).attr('height', newY );

		}
	});
	/* sub navigation */
	subNav('#nav_pages', '.sub_nav_pages');
	subNav('#nav_articles', '.sub_nav_articles');
	
	if( $(".classified_ad_text").length > 0 ){
		setTimeout(function(){ restrictClassifiedAdCharacters($(".classified_ad_text")); }, 1000);
		$(".classified_ad_text").bind('keyup change focus blur', function(){ restrictClassifiedAdCharacters($(this)) });
	}
	PhotoGallery.init();
	
})


$(window).load(function(){

	
})
	
	
/* HIGHLIGHT */
	var actualHighlight = 0;
	var hTimer = 0;

	function initHighlights(){
		fadeIn('#highlight_controls', 2000);
	
		triggerHighlights();
	}

	function triggerHighlights(){

		if( actualHighlight > $(highlightsPreload).length-1 )
			actualHighlight = 0;
		else if	( actualHighlight < 0 )
			actualHighlight = $(highlightsPreload).length-1;

		var h = highlightsPreload[actualHighlight];
		$('#highlight_background').css('background-image', 'url(' + h.src + ')');
		$('.highlight_link').attr('href', h.name );
	
		if( h.title != '' ){
			if( isIe() )
				fadeIn( $('#highlight_text') );
			else
				$('#highlight_text').css('display', 'table');

		} else
			$('#highlight_text').hide();
	
		$('#highlight_text').html( h.title );
	

		$('#highlight_text').css('color', '#e7e7e7');
		$('#highlight_text').hover(
			function(){ $(this).css('color', '#27B7EA'); },
			function(){ $(this).css('color', '#e7e7e7'); }
		);

		actualHighlight++;
		hTimer = setTimeout(function(){
			triggerHighlights();
		}, 8000);
	}
	
	function highlightControl(el){
		
		if( el.hasClass('pause') ){
			clearTimeout(hTimer);
			fadeOut(el, 800);
		} else {
			fadeIn( '.hcontrol.pause' , 800);
			
			if( el.hasClass('next') ){
				clearTimeout(hTimer);
				triggerHighlights();
			} else if( el.hasClass('previous') ){
				clearTimeout(hTimer);
				actualHighlight = actualHighlight-2;
				triggerHighlights();
			}
		}		
	}
	
/* WEATHER */
	function loadWeather(){
		$('#weather .loading.failed').hide();
		$('#weather .loading.waiting').show();
		$.ajax({
			type: "POST",
			url: webroot+ "weather/first/",
			data: ({  }),
			success: function(response){
				
				if( response == '' ){
					$('#weather .loading.waiting').hide();
					$('#weather .loading.failed').show();
					return false;
				}
				
				$('#weather .loading.waiting').hide();
				$('#weather .current, #weather .forecast').show();
				
				var p = $.evalJSON(response);
				var value = p.current_conditions[0];
				// hoje
				$('#weather .current').append(
				'<div class="item">'+
					'<span class="image">'+
						'<span class="containner"><img src="http://www.google.com'+value.icon+'" title="'+value.condition+'" /></span>'+
					'</span>'+
					'<span class="info">'+
						'<div class="header">'+
							value.temp_c+'°C'+
						'</div>'+
					'</span>'+
				'</div>'
				);
				
				// próximos dias
				$.each( p.forecast_conditions, function(index, value){
					
					$('#weather .forecast').append(
					'<div class="item">'+
						'<span class="image">'+
							'<span class="containner"><img src="http://www.google.com'+value.icon+'" title="'+value.condition+'" /></span>'+
						'</span>'+
						'<span class="info">'+
							'<div class="header">'+
								value.day_of_week+', '+value.low+'/'+value.high+'°C'+
							'</div>'+
							'<div class="text">'+
								value.condition+''+
							'</div>'+
						'</span>'+
					'</div>'
					);
					
				});
			}
		});
	}
	
	function restrictClassifiedAdCharacters(_this){
		console.log('aqui');
		var textValue = $(_this).val();
		var maxCharacters = parseInt($(".classified_characters_left").attr('data-maxcharacter'));
		var leftCharacters = maxCharacters - textValue.length;
		if( leftCharacters <= 0 ){
			$(_this).val(textValue.substr(0, maxCharacters));
			maxCharacters = parseInt($(".classified_characters_left").attr('data-maxcharacter'));
			textValue = $(_this).val();
			leftCharacters = maxCharacters - textValue.length;
		}
		$(".classified_characters_left").html("Você pode digitar ainda <strong>"+leftCharacters+"</strong> caracteres.");
	}

