$().ready(function(){
	/*
	* Confederation Countries
	*/
	var confStatus = false;
	$('#layout_nav_itens').find('a').click(function(){
		if (confStatus==true){
			 return false;
		}
		confStatus = true;
		var url = $(this).attr('href');
		var selected = $('#layout_nav_itens').find('a.selected').attr('rel');
		$('#layout_nav_itens').find('a.selected').removeClass('selected').removeClass(selected+'_selected').addClass(selected);
		
		$(this).addClass('selected').addClass($(this).attr('rel')+'_selected');
				
		var obj = this;
		$('#layout_nav_center').removeClass('header_'+selected).fadeOut('fast',function(){
			
			$('#layout_nav_center').addClass('header_'+$(obj).attr('rel')).fadeIn('',function(){
				confStatus = false;
			});		
		});
		$('#country_nav').addClass('country_nav_loader').find('a').addClass('loading');
		
		$('.country_list').fadeOut('fast');
		$('#country_nav').find('a').removeClass('selected');			
		$('#country_nav').find('a').html('Seleccione um país');
		
		$.ajax({
			type: "GET",
			url: url,
			dataType: "html",
			success: function(response){
				$('#country_nav').removeClass('country_nav_loader').find('a').removeClass('loading');				
				$('.country_list').html(response);
				$('.country_list ul li a').click(function(){
					$('#country_nav a').html($(this).html());
				});				
			}
		});
		
				
		return false;
	});
	
	
	/*
	 * Country Navigation
	 */
	$('#country_nav').find('a').click(function(){		
		if ($(this).hasClass('loading')){
			return false;
		}
		if ($('.country_list').css('display')=='none'){
			$('.country_list').fadeIn();			
			$('#country_nav').find('a').addClass('selected');
		}else{
			$('.country_list').fadeOut('fast');
			$('#country_nav').find('a').removeClass('selected');	
		}
		
		return false;
	});	
	$('html').click(function(){
		if ($('.country_list').css('display')!='none'){
			$('.country_list').fadeOut('fast');
			$('#country_nav').find('a').removeClass('selected');
		}				
	});
	
	$('.country_list ul li a').click(function(){
		$('#country_nav a').html($(this).html());
	});
	
	
	$('#layout_search').find('form').submit(function(){		
		var $key = $('#search_key').val();
		if ($key=='Pesquisar') $key = '';
		window.location.href = '/pesquisa/' +  $key;		
		return false;
	});
	
	$('#layout_search').find('input').click(function(){
		var $key = $(this).val();
		if ($key=='Pesquisar'){
			$(this).val('');
		}
		
	}).blur(function(){
		if ($(this).val()==''){
			$(this).val('Pesquisar');
		}
	});		
	
	$('#layout_search').find('a.layout_search_submit').click(function(){
		$('#layout_search').find('form').submit();
	});

});

/*
 * Homepage
 */
var homepageActive   = 0;
var homepageInterval = '';

function homePageStart(){
	//thumb click event
	$('#homepage_main .home_thumbs').find('a').hover(function(){
		clearInterval(homepageInterval);
		if ($(this).hasClass('selected')){
			return false;
		}
		homepageActive = $(this).attr('rel');
		var id = 'home_main_' + $(this).attr('rel');
		$('#homepage_main .home_thumbs').find('a.selected').removeClass('selected');
		$(this).addClass('selected');

		$('#home_main').find('a.selected').hide().find('.home_label').hide();
		$('#'+id).show().addClass('selected').find('.home_label').slideDown();
		
		return false;
	});
	
	//solve a bug on IE7
	$('#home_main').find('a').click(function(){ 
		window.location.href = $(this).attr('href');
		return false; 
	});

	//start action
	$('#home_main').find('a.selected').find('.home_label').slideDown();
	homepageInterval = window.setInterval("homePageSlideShow()", 8000);	
}


function homePageSlideShow(){
	if (homepageActive+1<=homepageLimit){
		homepageActive++;
	}else{
		homepageActive = 0;
	}
	
	var id = 'home_main_' + homepageActive;
	$('.home_thumbs').find('a.selected').removeClass('selected');
	$('#home_main_th_'+homepageActive).addClass('selected');

	$('#home_main').find('a.selected').hide().find('.home_label').hide();
	$('#'+id).show().addClass('selected').find('.home_label').slideDown();
	
	return false;	
}

function formContact(){
		$('#form_contact_content .bt_submit a').click(function(){
			$('#form_contact_content form').submit();
			
			return false;
		});
		
		$('#form_contact_content input, #form_contact_content textarea').bind('focus',function(){
			if ($(this).val()==$(this).attr('default_text')){
				$(this).val('');
			}
		}).bind('blur',function(){
			if ($(this).val()==''){
				$(this).val($(this).attr('default_text'));
			}			
		});		
		
		
		/** Ajax form */
	    var options = { 
	        beforeSubmit:  showRequest,  // pre-submit callback 
	        success:       showResponse,  // post-submit callback 
	    	dataType:	   'html'
	    }; 		
		
		$('#form_contact_content form').ajaxForm(options); 
        
        function showRequest(formData, jqForm, options){			
			var $flag = true;
			$('#form_contact_content input, #form_contact_content textarea').each(function(){
				if ($(this).val()==$(this).attr('default_text') || $(this).val()==''){
					var $o = $(this).parent().find('.error');
					
					$o.fadeIn().animate({top:'0'},1500,function(){ $o.fadeOut(); } );				
					$flag = false;
				}
			});
			
			if ($flag == false){
				return false;
			}
			$('#bt_submit').val('a enviar...').css('opacity','0.5');
			$('#form_contact_content .bt_submit a').css('opacity','0.7');
			return true;	
			
        }        
        
        function showResponse(result, statusText){		
        	$('#form_contact').slideUp('',function(){
        		$('.bio .success').slideDown();
        	});
        					                   
        }	
}

function getBanner(zoneId, obj){
	
	
	$.get('/banners/index/get/id/' + zoneId, function(result){
		$('#'+obj).html(result);
		//document.write(result);		
	});
}
