$.require('dimensions');
$.require('livequery');
$.require('ajaxform');
$.require('history');
$.require('jCal');
$.require('highslide');


var loading = true;

function Votar( n , opcao ){
	$.post( $(n).attr('href') , { opcao : opcao } , function(){
		alert('Seu voto foi computado com sucesso!');
	});
	
	window.location.reload();

	return false;
}

function DarkWindow(){
    $('#jquery-overlay').livequery(function(){
        var offset = $(window).offsetParent();

        $('#jquery-overlay').css({
            backgroundColor: '#000',
            opacity: '0.8',
            width: offset.width(),
            height: offset.height(),
			zIndex : (hs.zIndexCounter++)
        }).fadeIn();

        $('#jquery-lightbox').css({
            top:  $(window).scrollTop() + ($(window).height() / 2) -  ($('#jquery-lightbox').height()/2),
            left: 0,
			zIndex : (hs.zIndexCounter++)
        }).show();
    });
}

function NormalWindow(){
    $('#jquery-overlay').livequery(function(){
        $('#jquery-lightbox').hide();
        $('#jquery-overlay').fadeOut(function(){
            $('#jquery-overlay').hide();
        });
    });
}

function calculaIMC( n ){
	if ($('#peso').val().indexOf(',')) {
		$('#peso').val($('#peso').val().replace(',', '.'));
	}

	if ($('#altura').val().indexOf('.')) {
		$('#altura').val($('#altura').val().replace('.', ''));
	}

	if ($('#altura').val().indexOf(',')) {
		$('#altura').val($('#altura').val().replace(',', ''));
	}

	if ($('#peso').val().length > 0 && isNaN(parseFloat($('#peso').val())) == false && $('#altura').val().length > 0 && isNaN(parseFloat($('#altura').val())) == false) {
		hs.htmlExpand( n , { objectType: 'ajax' , height : 750} );
		hs.Expander.prototype.onAfterExpand = function(){
			hs.Expander.prototype.onAfterExpand = function(){return true};
			$('#imc').html(Math.round(parseFloat($('#peso').val()) / Math.pow(parseInt($('#altura').val()), 2) * 1000000) / 100);
		}
		return true;
	}
	else {
		alert('Primeiro preencha corretamente os campos Peso e Altura.');
		return false;
	}
}

function calculaRCQ( n ){
	if ($('#cintura').val().indexOf(',')) {
		$('#cintura').val($('#cintura').val().replace(',', ''));
	}

	if ($('#cintura').val().indexOf('.')) {
		$('#cintura').val($('#cintura').val().replace('.', ''));
	}

	if ($('#quadril').val().indexOf('.')) {
		$('#quadril').val($('#quadril').val().replace('.', ''));
	}

	if ($('#quadril').val().indexOf(',')) {
		$('#quadril').val($('#quadril').val().replace(',', ''));
	}

	if ($('#cintura').val().length > 0 && isNaN(parseFloat($('#cintura').val())) == false && $('#quadril').val().length > 0 && isNaN(parseFloat($('#quadril').val())) == false) {
		hs.htmlExpand( n , { objectType: 'ajax' , height : 750} );
		hs.Expander.prototype.onAfterExpand = function(){
			hs.Expander.prototype.onAfterExpand = function(){return true};
			$('#rcq').html(Math.round(parseInt($('#cintura').val()) / parseInt($('#quadril').val()) * 100) / 100);
		};
		return true;
	}else{
		alert('Primeiro preencha corretamente os campos Cintura e Quadril.');
	}
}

$(function(){
	
	
	
	if( $("#slider").get(0) ){
		$.require('innerfade');
		$('#slider').livequery(function(){
			$(this).innerfade({
				speed: 'slow',
				timeout: 4000,
				type: 'sequence',
				containerheight: '421px'
			});
		});
	}

	hs.outlineType = 'rounded-white';
	hs.graphicsDir = $.scriptPath + '../../../img/graphics/';
	hs.align = 'center';
	hs.width = 0.7 * $(window).offsetParent().width();
	hs.addSlideshow({
		slideshowGroup: 'slideshow',
		interval: 5000,
		repeat: false,
		useControls: true,
		fixedControls: true,
		overlayOptions: {
			opacity: .6,
			position: 'bottom center',
			hideOnMouseOut: true
		}
	});

	hs.Expander.prototype.onAfterExpand = function(sender){
		$(sender.content).find('img').each(function(){
			$(this).hide();

			$(this).parent().parent().css({
				background: '#FFF url("../app/app_1/img/loading.gif") no-repeat 50% 50%',
				width: '200px',
				height: '150px'
			});

			$(this).load(function(){

				$(this).fadeIn(function(){
					$(this).parent().parent().css({
						background: 'none'
					});

					$(this).css('cursor', 'pointer');
				});
			}).attr('src', $(this).attr('src'));
		});
	};

	hs.transitions = ['expand', 'crossfade'];


	$.history.init(function(hash){
		if (hash) {
			$('#contents').load(hash + ' #contents > *', {
				history: true
			});
		}
	});

	$("a[rel='history']").livequery('click', function(){		
		if($(this).attr('rel') == 'history'){
			var hash = this.href;
			hash = hash.replace(/^.*#/, '');
			$.history.load(hash);
		}
		return false;
	});
	
	$('#categoriesContent ul li a').livequery('click',function(){														
		var ul = $(this).parent().find('ul:first');
		
		ul.find('li:last').css('backgroundImage','none');
		ul.slideToggle('slow');			
		
		return false;
	});

	$('#jquery-overlay').ajaxStart(function(){
		if (loading) {
			DarkWindow();
		}
	});

	$('#jquery-overlay').ajaxStop(function(){
		if (loading) {
			NormalWindow();
		}
	});

	$('#contato').livequery('submit',function(){
		$form = this;		
		if( $.trim($('input[name=de]').val()).length == 0 ){
			alert('O campo "De" deve ser preenchido.');
		}else if( $.trim($('input[name=demail]').val()).length == 0 ){
			alert('O campo "Email" deve ser preenchido.');
		}else if( $.trim($('input[name=assunto]').val()).length == 0 ){
			alert('O campo "Assunto" deve ser preenchido.');
		}else if( $.trim($('textarea[name=mensagem]').val()).length == 0 ){
			alert('O campo "Mensagem" deve ser preenchido.');
		}else{
			$($form).ajaxSubmit(function(){
				alert('Sua mensagem foi enviada com sucesso!');
				$form.reset();
			});
		}
		return false;
		
	});

	$('<img />').appendTo('body').attr('src', '../app/app_1/img/loading.gif').css('display', 'none');
	$('<img />').appendTo('body').attr('src', '../app/app_1/img/graphics/loader.gif').css('display', 'none');
	$('<img />').appendTo('body').attr('src', '../app/app_1/img/graphics/loader.white.gif').css('display', 'none');

	$('#CAL').jCal();

	


	$('#FormEnquetes').livequery('submit', function(){
		$DO = false;
		$('input[name="opcao"]').each(function(){
			if ($(this).attr('checked') === true) {
				$DO = true;
			}
		});

		if ($DO) {
			$form = this;
			$(this).ajaxSubmit(function(){
				alert('Seu voto foi computado com sucesso!');
				$form.reset();
				$('#votar').attr('disabled', 'disabled');
			});
		}
		else {
			alert('Primeiro selecione uma opção.');
		}
		return false;
	});
	
});
