/*GET QUERY VARIABLE*/
function getQueryVariable(url, variable){
	var url = url.toLowerCase();
	var variable = variable.toLowerCase();
	var vars = url.replace('?', '&').split('&');
	
	for(var i = 0; i < vars.length; i++){
		if(vars[i].split("=")[0] == variable){
			return vars[i].split('=')[1];
		}
	}
}

/*INIT SIFR*/
function initSifr(){
	if(sIFR != null && sIFR.replaceElement != null){
		sIFR.replaceElement('.login h2.titulo', 'swf/helvetica_45_light.swf', '#FFFFFF', '#FFFFFF', '#FFFFFF', 'transparent', 0, 0, 0, 0);
		sIFR.replaceElement('.titulos h2.titulo', 'swf/helvetica_45_light.swf', '#FFFFFF', '#FFFFFF', '#FFFFFF', 'transparent', 0, 0, 0, 0);
		sIFR.replaceElement('h2.titulo', 'swf/helvetica_45_light.swf', '#222222', '#222222', '#222222', 'transparent', 0, 0, 0, 0);
		
		sIFR.replaceElement('.ultimas_declaracoes_postadas h3.subtitulo', 'swf/65_helvetica_medium.swf', '#222222', '#222222', '#222222', 'transparent', 0, 0, 0, 0);
		sIFR.replaceElement('.participantes_mais_votados h3.subtitulo', 'swf/65_helvetica_medium.swf', '#222222', '#222222', '#222222', 'transparent', 0, 0, 0, 0);
		sIFR.replaceElement('.lista_ganhadores h3.subtitulo', 'swf/65_helvetica_medium.swf', '#222222', '#222222', '#222222', 'transparent', 0, 0, 0, 0);
		sIFR.replaceElement('.titulos h3.subtitulo', 'swf/65_helvetica_medium.swf', '#FFFFFF', '#FFFFFF', '#FFFFFF', 'transparent', 0, 0, 0, 0);
		sIFR.replaceElement('h3.subtitulo', 'swf/65_helvetica_medium.swf', '#DC2330', '#DC2330', '#DC2330', 'transparent', 0, 0, 0, 0);
		sIFR.replaceElement('h3.titulo', 'swf/helvetica_45_light.swf', '#DC2330', '#DC2330', '#DC2330', 'transparent', 0, 0, 0, 0);
		
		$J('h2.titulo, h3.subtitulo').css('visibility', 'visible');
	}
}

/*VALUE FOCUS*/
function valueFocus(){
	var inputs = $J('input:text');
	var textareas = $J('textarea');
	
	function returnValue(obj){
		/*FOCUS*/
		obj.focus(function(){
			if($J(this).val() == $J(this).attr('title')){
				$J(this).val('');
			}
		})
		
		/*BLUR*/
		obj.blur(function(){
			if($J(this).val() == ''){
				$J(this).val($J(this).attr('title'));
			}
		})
	}
	
	/*INPUT*/
	for(var i = 0; i < inputs.length; i++){
		returnValue($J(inputs[i]));
	}
	
	/*TEXTAREA*/
	for(var i = 0; i < textareas.length; i++){
		returnValue($J(textareas[i]));
	}
}

/*LIMIT CARACTER*/
function limitCaracter(obj, counter, quant){
	var obj = $J(obj);
	var counter = $J(counter);
	
	key = function(){
		if(obj.val().length >= quant){
			counter.html('0 CARACTERES');
			obj.val(obj.val().substring(0, quant));
			
		}else{
			counter.html(quant - obj.val().length + ' CARACTERES');
		}
	}
	
	obj.keypress(function(){
		key();
	})
	
	obj.keyup(function(){
		key();
	})
	
	obj.keydown(function(){
		key();
	})
	
	obj.focus(function(){
		key();
	})
}

/*TOOLTIP*/
function tooltip(obj, txt){
	/*MOUSE OVER*/
	$J(obj).mouseover(function(e){
		if($J('.tooltip').is(':visible')){
			$J('.tooltip').remove();
		}
		
		$J('body').append('<div class="tooltip"><div class="tooltip_top">&nbsp;</div><div class="tooltip_content">' + txt + '</div><div class="tooltip_bottom">&nbsp;</div></div>');
		$J('.tooltip').css({top: e.pageY - $J('.tooltip').height() + 'px', left: e.pageX - $J('.tooltip').width() + 'px'});
	})
	
	/*MOUSE MOVE*/
	$J(obj).mousemove(function(e){
		if(!$J('.tooltip').is(':visible')){
			$J('body').append('<div class="tooltip"><div class="tooltip_top">&nbsp;</div><div class="tooltip_content">' + txt + '</div><div class="tooltip_bottom">&nbsp;</div></div>');
			$J('.tooltip').css({top: e.pageY - $J('.tooltip').height() + 'px', left: e.pageX - $J('.tooltip').width() + 'px'});
		}
		
		$J('.tooltip').css({top: e.pageY - $J('.tooltip').height() + 'px', left: e.pageX - $J('.tooltip').width() + 'px'});
	})
	
	/*MOUSE OUT*/
	$J(obj).mouseout(function(){
		$J('.tooltip').remove();
	})
	
	/*CLICK*/
	$J(obj).click(function(){
		return false;
	})
}

/*INIT TOLLTIP*/
function initTooltip(){
	var total = $J('[@tooltip"]');
	for(var i = 0; i < total.length; i++){
		tooltip(total[i], $J(total[i]).attr('tooltip'));
	}
}

/*IFRAME HEIGHT*/
function iframeHeight(){
	$J('#myiframe').css({height: '100%'});
	
	if($J.browser.msie){
		$J('#myiframe').height(window.frames['myiframe'].document.body.scrollHeight + 'px');
		
	}else{
		$J('#myiframe').height(document.getElementById('myiframe').contentDocument.getElementsByTagName('html')[0].scrollHeight + 'px');
	}
}

/*SCROLLER FLASH*/
var viewContent, urlComplete;

function scrollerFlash(url, pos, callback){
	if(!$J('#main').is(':visible')){
		$J('<div id="main"></div>').appendTo('#container');
	}
	
	if(viewContent == 'interna' || viewContent == undefined){
		window.location.href = url;
	
	}else{
		urchinTracker(url);
		
		$J('body').append('<img src="/img/load.gif" width="88" height="78" alt="Carregando..." class="load" /><div class="bg"></div>');
		$J('.bg').css('opacity', 0);
		
		$J('.bg').fadeTo('normal', 0.8, function(){
			$J.ajax({
				type: 'GET',
				url: url,
				dataType: 'html',
				success: function(returnHTML){
					$J('#main').html($J(returnHTML).find('.main').parent());
					
					$J('.load').remove();
					
					$J('.bg').fadeTo('normal', 0, function(){
						$J('.bg').remove();
						
						$J('#container').animate({marginTop: pos + 'px'}, 1000, function(){
							$J('#flash').remove();
							$J('#container').css({marginTop: 0});
						})
					})
					
					initParentHome();
				}
			})
		})
	}
	
	/*CALLBACK*/
	if(callback != undefined){
		$J('#main').ajaxComplete(function(){
			/*INIT*/
			init();
			
			/*RETURN CALLBACK*/
			callback();
		})
	}
}

/*FLASH HOME*/
function flashHome(noIntro){
	var flashHome = new SWFObject('swf/home.swf', 'home', '100%', '100%', '8', '#FFFFFF');
	flashHome.addParam('wmode', 'opaque');
	flashHome.addParam('menu', 'false');
	if(noIntro) flashHome.addVariable('noIntro', noIntro);
	flashHome.write('flash');
}

/*O PAIS*/
function oPais(){
	urchinTracker('/home');
	
	h = $J('html')[0].clientHeight;
	
	viewContent = 'home';
	
	$J('body').append('<img src="/img/load.gif" width="88" height="78" alt="Carregando..." class="load" /><div class="bg"></div>');
	$J('.bg').css('opacity', 0);
	
	if(!$J('#flash').is(':visible')){
		$J('#container').prepend(
			$J('<div id="flash"></div>').css({position: 'relative', marginTop: -h + 1 + 'px', height: h + 'px'})
		)
	}

	flashHome('true');
	
	$J('.bg').fadeTo('normal', 0.8);
}

/*O PAIS COMPLETE*/
function oPaisComplete(){
	$J('.load').remove();
	$J('.bg').fadeTo('normal', 0, function(){
		$J('.bg').remove();
			
		$J('#container').animate({marginTop: h + 'px'}, 1000, function(){
			$J('#container, #flash').css({top: 0, marginTop: 0});
			$J('#main').remove();
		})	
	})
}

/*SAUDADE*/
function saudade(){
	scrollerFlash('Saudade.aspx', -h, function(){
		viewContent = 'interna';
	})
}

/*ALEGRIA*/
function alegria(){
	scrollerFlash('Alegria.aspx', -h, function(){
		viewContent = 'interna';
	})
}

/*PAIXAO*/
function paixao(){
	scrollerFlash('Paixao.aspx', -h, function(){
		viewContent = 'interna';
	})
}

/*BOM HUMOR*/
function bomHumor(){
	scrollerFlash('BomHumor.aspx', -h, function(){
		viewContent = 'interna';
	})
}

/*ROTEIRO*/
function roteiro(){
	scrollerFlash('RoteiroDeViagem.aspx', -h, function(){
		viewContent = 'interna';
	})
}

/*VIDEOS CAMPANHA*/
function videosCampanha(){
	scrollerFlash('VideosCampanha.aspx', -h, function(){
		viewContent = 'interna';
	})
}

/*DIVULGUE AOS AMIGOS*/
function divulgueAosAmigos(){
	$J('.modal_divulgue_aos_amigos').show();
	$J('.modal_vote_nessa_declaracao').hide();
}

/*VOTE NESSA DECLARACAO*/
function voteNessaDeclaracao(){
	$J('.modal_vote_nessa_declaracao').show();
	$J('.modal_divulgue_aos_amigos').hide();
}

/*HAPPY TONES*/
function happyTones(title, mp3, size, download, id){
	var mp3_player = new SWFObject('swf/mp3_player.swf', id, '356', '26', '9', '#FAEED8');
	mp3_player.addParam('wmode', 'transparent');
	mp3_player.addVariable('title', title);
	mp3_player.addVariable('mp3', mp3);
	mp3_player.addVariable('size', size);
	mp3_player.addVariable('download', download);
	mp3_player.write(id);
}

/*INIT*/
function init(){
	/*SCROLL HEIGHT*/
	h = $J('html')[0].clientHeight;
	
	/*INIT TOOLTIP*/
	initTooltip();
	
	/*VALUE FOCUS*/
	valueFocus();
	
	/*INIT SIFR*/
	initSifr();
	
	/*CENTRALIZAR PAGINACAO*/
	if($J('.bom_humor .paginacao').is(':visible')){
		$J('.bom_humor .paginacao').css({width: $J('.paginacao').width(), float: 'none', margin: '0 auto'});
	}
	
	/*CENTRALIZAR PAGINACAO*/
	if($J('#flash').is(':visible')){
		$J('#flash').height(h);
	}
	
	/*BACKGROUND INPUT IMAGE*/
	$J('input:image').css('background', 'none');
}

/*INIT PARENT HOME*/
function initParentHome(){
	/*INIT MODAL*/
	initModal();
		
	/*ALPHA*/
	if($J('.alpha').is(':visible')){
		$J('.alpha').css('opacity', 0.9);
	}
	
	/*BORDER LAST UAU PAPERS*/
	if($J('.uau_papers ul ul').is(':visible')){
		$J('.uau_papers ul ul').find('li:last').css({border: 0, padding: 0});
	}
	
	/*LIMIT CARACTER*/
	if($J('.txt_texto_sobre_a_sua_paixao').is(':visible')){
		limitCaracter('.txt_texto_sobre_a_sua_paixao', '.limit_caracter', 255);
	}
	
	/*COPY HEIGHT*/
	if($J('.paixao_cadastro').is(':visible') && $J('.paixao_content').is(':visible')){
		if($J('.paixao_cadastro').height() > $J('.paixao_content').height()){
			$J('.paixao_cadastro, .paixao_content').height($J('.paixao_cadastro').height());
			
		}else{
			$J('.paixao_cadastro, .paixao_content').height($J('.paixao_content').height());
		}
	}
	
	/*CAROUSEL WALLPAPER*/
	carousel({obj: '.uau_papers', dimension: 'horizontal', scroll: 1, size: 4, animation: true});
	
	/*CAROUSEL HAPPYTONES*/
	carousel({obj: '.happy_tones', dimension: 'horizontal', scroll: 1, size: 1, animation: false});
	
	/*CAROUSEL TOSHICONS*/
	carousel({obj: '.semp_toshicons', dimension: 'horizontal', scroll: 1, size: 1, animation: false});
}

/*CHANGE PROCURAR*/
function changeProcurar(){
	$J('#txt_foto').val($J('.bt_buscar_imagem input:file').val());
}

/*RESIZE*/
function resize(){
	init();
}

/*CAROUSEL*/
function carousel(objParam){
	/*VARS*/
	var obj = objParam.obj;
	var dimension = objParam.dimension;
	var scroll = objParam.scroll;
	var size = objParam.size;
	var animation = objParam.animation;
	var timer = objParam.timer;
	var loop = objParam.loop;

	if($J(obj).is(':visible')){
		/*ARRAY POS CAROUSEL*/
		var pos = [];
		
		/*VIEW ARRAY POS*/
		var view = 0;
		
		/*OBJ CAROUSEL*/
		var obj = $J(obj);
		
		/*OBJCURRENT CAROUSEL*/
		var objCurrent = obj.find('.carousel_current');
		
		/*UL CAROUSEL*/
		var ul = objCurrent.find('ul:first');
		
		/*LI CAROUSEL*/
		var li = ul.find('li.item');
		
		/*LI*/
		li.css({float: 'left'});
		var liTotal = li.length;
		var liWidth = li[0].offsetWidth;
		var liHeigth = li[0].offsetHeight;
		
		/*WIDTH | HEIGHT UL*/
		var ulWidth = liWidth * liTotal;
		var ulHeight = liHeigth * liTotal;
	
		/*SCROLL TOP | LEFT*/
		var scrollLeft = liWidth * scroll;
		var scrollTop = liHeigth * scroll;
	
		/*WIDTH | HEIGTH OBJ CURRENT*/
		var objCurrentWidth = liWidth * size;
		var objCurrentHeight = liHeigth * size;
		
		/*BT NEXT*/
		var next = obj.find('.bt_next, .bt_proximo');
		var prev = obj.find('.bt_prev, .bt_anterior');
		
		/*SPEED CAROUSEL*/
		var speed = 500;
		
		/*VERIFICATION QUANT*/
		var quant = Math.ceil(liTotal/scroll);
		
		if(quant <= 0) var quant = 2;
		
		/*HORIZONTAL*/
		if(dimension == 'horizontal'){
			objCurrent.css({width: objCurrentWidth + 'px', overflow: 'hidden'});
			ul.css({width: ulWidth + 'px', height: liHeigth + 'px', overflow: 'hidden'});
			
			for(var i = 0; i < quant; i++){
				pos.push(-scrollLeft * i);
			}
		}
		
		/*VERTICAL*/
		if(dimension == 'vertical'){
			objCurrent.css({height: objCurrentHeight + 'px', overflow: 'hidden'});
			ul.css({height: ulHeight + 'px'});
			li.css({clear: 'both'});
			
			for(var i = 0; i < quant; i++){
				pos.push(-scrollTop * i);
			}
		}
		
		/*DISABLE NEXT | DISABLE PREV*/
		if(liTotal == size){
			next.addClass('disable');
			prev.addClass('disable');
		}
		
		/*NEXT*/
		next.click(function(){
			/*VERIFICATION*/
			if((scroll > size && view != pos.length - (-(pos.length - size))) || (scroll < size && view != pos.length - size) || (scroll == size && view != pos.length - 1)){
				view += 1;
				
				/*DIMENSION*/
				if(animation == undefined || animation == true){
					if(dimension == 'horizontal'){
						if(pos[view] != (objCurrentWidth - ulWidth)) ul.animate({marginLeft: pos[view]}, speed);
						else ul.animate({marginLeft: (objCurrentWidth - ulWidth)}, speed);
							
					}else{
						if(pos[view] != (objCurrentHeight - ulHeight)) ul.animate({marginTop: pos[view]}, speed);
						else ul.animate({marginTop: (objCurrentHeight - ulHeight)}, speed);
					}
					
				}else{
					if(dimension == 'horizontal'){
						if(pos[view] >= (objCurrentWidth - ulWidth)) ul.css({marginLeft: pos[view]}, speed);
						else ul.css({marginLeft: (objCurrentWidth - ulWidth)}, speed);
								
					}else{
						if(pos[view] >= (objCurrentHeight - ulHeight)) ul.css({marginTop: pos[view]}, speed);
						else ul.css({marginTop: (objCurrentHeight - ulHeight)}, speed);
					}
				}
				
			}else{
				if(timer != undefined && timer == true || loop != undefined && loop == true){
					clearInterval(intTimer);
					view = -1;
					next.click();
				}
			}
			
			return false;
		})
		
		/*PREV*/
		prev.click(function(){
			/*VERIFICATION*/
			if(view != 0){
				view -= 1;
	
				/*DIMENSION*/
				if(animation == undefined || animation == true){
					if(dimension == 'horizontal') ul.animate({marginLeft: pos[view]}, speed);
					else ul.animate({marginTop: pos[view]}, speed);
				
				}else{
					if(dimension == 'horizontal') ul.css({marginLeft: pos[view]}, speed);
					else ul.css({marginTop: pos[view]}, speed);
				}
			}else{
				if(loop != undefined && loop == true){
					if(scroll < size){
						view = pos.length - (size - 1);
					
					}else{
						view = pos.length;
					}
	
					prev.click();
				}
			}
			
			return false;
		})
		
		/*AUTO CAROUSEL*/
		if(typeof(timer) == 'number' && timer != undefined && timer != false){
			var intTimer;
			
			function timerCarosel(){
				intTimer = setInterval(function(){
					next.click();
				}, timer);
			}
			
			function clearTimerCarousel(){
				clearInterval(intTimer);
				timerCarosel();
			}
	
			timerCarosel();
			
			/*CLICK */
			next.click(function(){
				clearTimerCarousel();
			})
			
			prev.click(function(){
				clearTimerCarousel();
			})
		}
	}
}

/*VARS*/
var h;

/*ONLOAD*/
$J(document).ready(function(){
	window.setTimeout(function(){
		init();
		
		/*RESIZE*/
		$J('body').attr('onresize', 'resize();');
	
		/*INIT PARENT HOME*/
		initParentHome();
	}, 1)
})