$(document).ready(function(){
	/**
	 * Png Fix
	 */
	$(document).pngFix();
		
	/**
	 * Main content
	 */
	sendContent( 'index.php?engine=site.hotspot&__UNITY__=1', '', 'areaConteudo' );
	
	/**
	 * MP3 player
	 */
	_requestContent( 'GET', '/skin/tpl/mp3player_iguatemi.html', '', 'mp3player', 'carregandoPlayer' );
	
	/**
	 * Menu destino
	 */
	$('#destino, #destino_jurere, #destino_iguatemi, #selecione_destino').fadeIn( 1300 );
	$('#destino_iguatemi').unbind( 'click' );
	$('#destino_iguatemi').bind( 'click', function(){ $('#menu_iguatemi').slideDown(300); } );
	$('#destino_jurere').unbind( 'click' );
	$('#destino_jurere').bind( 'click', destino_jurere );

	/**
	 * Form newsletter
	 */
	$('#formNewsletter input[name="str_nome"], #formNewsletter input[name="str_email"]').focus(function(){
		if ( $(this).attr( 'value' ) == 'Nome' || $(this).attr( 'value' ) == 'E-mail' ) {
			$(this).attr( 'value', '' );
		}
	});

	$('#formNewsletter input[name="str_nome"]').blur(function(){
		if ( $(this).attr( 'value' ) == '' ) {
			$(this).attr( 'value', 'Nome' );
		}
	});
	
	$('#formNewsletter input[name="str_email"]').blur(function(){
		if ( $(this).attr( 'value' ) == '' ) {
			$(this).attr( 'value', 'E-mail' );
		}
	});
	
	$('#formNewsletter').submit(function(){
		$(this).attr( 'target', 'areaConteudo' );
		post( '#formNewsletter' );
		return false;
	});

	$('.paging a').each(function(){
		var str_href = $(this).attr( 'href' );
		$(this).attr( 'href', '#' );
		$(this).unbind( 'click' );
		$(this).click(function(){
			sendContent( str_href, '', 'areaConteudo' );
		});
	});
	
	/**
	 * Preaload images
	 */
	preloadImages('skin/img/comum/fundo_jurere.jpg', 'skin/img/comum/redes_socias_jurere.gif', 'skin/img/botoes/ok_jurere.gif', 'skin/img/comum/newsletter_jurere.gif', 'skin/img/comum/fundo_campo_jurere.jpg', 'skin/img/comum/selecione_destino_jurere.gif', 'skin/img/comum/carregando_jurere.gif', 'skin/img/comum/menu_furere.gif', 'skin/img/comum/destino_jurere_jurere.gif', 'skin/img/comum/destino_iguatemi_jurere.gif', 'skin/img/comum/logo_jurere.png');
});

/**
 * Preload images
 * @return void
 */
function preloadImages() {
	var a = ( typeof arguments[0] == 'object' ) ? arguments[0] : arguments;
	for( var i = a.length -1; i >= 0; i--) {
		$("<img>").attr("src", a[i]);
	}
}

function destino_iguatemi() {
	destino( 'iguatemi', 'jurere', 1 );
}

function destino_jurere() {
	destino( 'jurere', 'iguatemi', 4 );
}

/**
* Muda as imagens e cores do skin para o modelo Jurere
* 
* @param string str_destino
* @return void
*/
function destino( str_destino, str_current, int_unity ){
	$('#areaConteudo').html('');
	$('.fullscreen').animate({
			opacity : '0'
		}, {
			duration : 650,
			complete : function() {
				$('#style').attr( 'href', 'skin/css/styles_' + str_destino + '.css' );
				$('#menu_' + str_current ).hide();
				
				$('#destino_' + str_destino).unbind( 'click' );
				$('#destino_' + str_current).unbind( 'click' );
				$('#destino_' + str_current).bind( 'click', eval( 'destino_' + str_current ) );
				
				/**
				 * MP3 player
				 */
				_requestContent( 'GET', '/skin/tpl/mp3player_' + str_destino + '.html', '', 'mp3player', 'carregandoPlayer' );
			}
		}
	);
	
	$('.fullscreen').animate({
		opacity : '1'
		}, {
			duration : 650,
			complete : function() {
				$('#menu_' + str_destino).slideDown(300);
				sendContent( 'index.php?engine=site.hotspot&__UNITY__=' + int_unity, '', 'areaConteudo' );
			}
		}
	);
}

/**
 * Faz requisição ajax via GET
 * @param string str_url
 * @param string str_data
 * @param string str_target
 * @return void
 */
function sendContent( str_url, str_data, str_target ) {
	return _requestContent( 'GET', str_url, str_data, str_target, '#carregando' );
}

/**
* Faz requisição ajax via POST
* @param string str_url
* @param string str_data
* @param string str_target
* @return void
*/
function postContent( str_url, str_data, str_target ) {
	return _requestContent( 'POST', str_url, str_data, str_target, '#carregando' );
}

/**
* Faz requisição ajax
* @param string str_type
* @param string str_url
* @param string str_data
* @param string str_target
* @param string str_loading
* @return void
*/
function _requestContent( str_type, str_url, str_data, str_target, str_loading ) {
	$('#' + str_target).html( '' );
	$(str_loading).show();
	$.ajax({
		dataType: "html",
		async: true,
		type: str_type,
		url: str_url,
		data: str_data,
		complete: function(){
			$(str_loading).hide();
		},
		success: function(html){
			$(str_loading).hide();
			$('#' + str_target).html( html );
		},
		error: function(){
			//
		}
	});
}

/**
 * Faz post de formulário
 * @param string str_id
 * @return void
 */
function post( str_id ) {
	var str_form = str_id;
	var str_post_attributes = ', { ';
	var str_post_separator  = '';
	var str_get_attributes  = '';
	var str_get_separator   = '';

	$(str_form).find('input, textarea, select').each(function () {
		var str_key = $(this).attr('name');
		var str_value = '';
		if ( $(this).attr('type') != "button" && $(this).attr('disabled') != true ) {
			if ( $(this).attr('type') == 'radio' ) {
				str_value = ( $(this).attr('checked') ? 1 : 0 );
			} else if ( $(this).attr('type') == 'checkbox' ) {
				str_value = ( $(this).attr('checked') ? 1 : 0 );
			} else {
				str_value = $(this).attr('value');
			}
			
			str_value = encodeURI( str_value );
			str_post_attributes += str_post_separator + '"' + str_key + '": "' + str_value + '"';
			str_get_attributes  += str_get_separator + str_key + '=' + str_value;
			str_post_separator = ', ';
			str_get_separator  = '&';
		}
	}).get().join(", ");
	
	str_post_attributes += '}';

	var str_form_method = $(str_form).attr( 'method' );
	var str_form_action = $(str_form).attr( 'action' );
	var str_form_target = $(str_form).attr( 'target' );
	
	var str_query_separator = ( str_form_action.indexOf( '?' ) != -1 ) ? '&' : '?';
	
	if ( str_form_method.toLowerCase() == 'post' ) {
		postContent( str_form_action, str_get_attributes, str_form_target );
	} else {
		sendContent( str_form_action, str_get_attributes, str_form_target );
	}
}