$().ready(function() {	
	var interval = setInterval(function() {
		
		var curdate = new Date();				
		$('#busca #time').html(curdate.getHours() + ':' + curdate.getMinutes() + 'H');
		
	}, 1000);
		
	$('#form-busca').submit(function() {
		
		var search = $('#form-busca input[name=busca]');
		if(search.val() == 'Busca' || search.val() == '') {
			search.focus();
			return false;
		}
		
		var opt = '';
		$('#form-busca input[name=opt]:checked').each(function() {
			if(opt != '') { opt += ','; }
			opt += $(this).val();
		});
		
		if(opt == '') {
			alert("Selecione o tipo de busca:\nno site | nas edições");
			return false;
		}
				
		var b = search.val().replace(' ', '-');
		window.location = Zend().baseUrl() + '/site/busca?search=' + b + '&opt=' + opt;
		
	})
});

function initTwitter()
{
	$(".tweet").tweet({
        join_text: "auto",
        username: "revistaflashvip",        
        count: 5,
        auto_join_text_default: "",
        auto_join_text_ed: "",
        auto_join_text_ing: "",
        auto_join_text_reply: "",
        auto_join_text_url: "",
        loading_text: "carregando tweets..."
    });
}

var Edicoes = {
		
	atual:0,
	
	total:0,
	
	itens:null,
	
	init:function()
	{
		this.itens = new Array();
		
		$('#edicoes div.item').each(function(idx) {
			if(idx) {
				$(this).hide();
			}
			
			Edicoes.itens.push(this);
			Edicoes.total++;
		});
				
		var prev = $('#edicoes .setas .left');		
		prev.click(function() { Edicoes.prev(); });
		
		var next = $('#edicoes .setas .right');		
		next.click(function() { Edicoes.next(); });
		
	},
	
	prev:function()
	{
		this.atual--;
		if(this.atual < 0) {
			this.atual = this.total-1;
		}
		
		Edicoes.update();
	},
	
	next:function()
	{
		this.atual++;
		if(this.atual > this.total-1) {
			this.atual = 0;
		}
		
		Edicoes.update();
	},
	
	update:function()
	{
		$(this.itens).each(function() { $(this).fadeOut(); });
		$(this.itens[this.atual]).fadeIn();
	}
}
function Tritoq_Download(id, file)
{
	var url	  = Zend().baseUrl() + '/' + Zend().module() + '/' + Zend().controller() + '/download';
	
	if(file && id) {
		var TFrame;
		if($('#Tritoq_Frame_Download').length <= 0) {
			TFrame = $(document.createElement('iframe'));
			TFrame.attr('name','TritoqFrameDownload');
			TFrame.attr('id',  'Tritoq_Frame_Download');
			TFrame.attr('style','height:0px; width:0px; border:0; display:none;');
		} else {
			TFrame = $('#Tritoq_Frame_Download');
		}
		
		// Formulário
		var TFile		= $(document.createElement('input'));
		TFile.attr('type', 'hidden').attr('name', 'Tritoq_File').attr('value' ,file);
						
		var TIdFile		= $(document.createElement('input'));
		TIdFile.attr('type', 'hidden').attr('name', 'Tritoq_IdFile').attr('value' ,id);

		var TSection	= $(document.createElement('input'));
		TSection.attr('type', 'hidden').attr('name', 'Tritoq_Section').attr('value' , Zend().action());
		
		var TForm;
		if($('#Tritoq_Form_Download').length <= 0) {
			TForm = $(document.createElement('form'));
			TForm.attr('action', url).attr('method', 'POST').attr('target', 'TritoqFrameDownload').attr('id', 'Tritoq_Form_Download');
		} else {			
			$('#Tritoq_Form_Download input').remove();
			TForm = $('#Tritoq_Form_Download');
		}
		
		// Apends
		$(TForm).append(TFile);
		$(TForm).append(TIdFile);
		$(TForm).append(TSection);
		$('body').append(TFrame);
		$('body').append(TForm);
		
		// Submit Form
		$(TForm).submit();
	}
}


function initBox(form) {

	var elements;

	$('#' + form).map(function() {
		elements = this.elements;
	});

	for ( var i = 0; i < elements.length; i++) {
		$(elements[i]).map(
			function() {
				this.initvalue = this.value;
				if ((this.type == 'text' || this.type == 'textarea')
					&& this.alt != 'telefone') {
						this.title = this.value;
						$(this).focus(function() {
							if (this.value == this.initvalue) {
								this.value = '';
							}	
					});
					
					 $(this).blur(function() 
					 { 
						 if(this.alt != 'telefone')
						 {
							 if (this.value == '') {
								 this.value = this.initvalue; 
							 }
						 }
					 });
				}
		});
	}

	$('input[alt*=telefone]').each(function() {
		$(this).focus(function() {
			if(!this.masked)
			{
				this.value = '';
				$(this).mask('(99)9999-9999');
				this.masked = true;
			}
		});

	});
	
	$('#' + form).submit(function ()
	{
		for(var i=0; i < this.elements.length; i++)
		{
			var obj = this.elements[i];
			if(obj.type == 'text')
			{
				if(obj.initvalue == obj.value)
				{
					obj.value = '';
				}
			}
		}	
	});

	$('#' + form).validate( {
		rules : {
			nome : 'required',
			email : {
				email : true,
				required : true
			},
			telefone : 'required'			
		},
		
		messages : {
			nome: '*',
			email: '*',
			telefone: '*'			
		}	
	});
}

function openBox (tg, form)
{	
	if($(tg).css('display') == 'none')
	{
		
	}
	initBox(form);	
	showHideAnuncie (tg);
	
	if(!$('#'+form).attr('added'))
	{
			$('#'+form).attr('added', true);
			$('#'+form).submit(function () {
				
				if(!$('#'+form).attr('s'))
				{	
					var nome = $('#'+form+" #nome").val();
					var email = $('#'+form+" #email").val();
					var telefone = $('#'+form+" #telefone").val();
					var local = $('#'+form+" #local").val();
					
					$('#'+form+" #btsend").val('Enviando ...');
					
					$('#'+form).attr('s' , true);
					
					$.post(Zend().currentPath()+'/anuncie/send', {'nome':nome, 'email':email, 'telefone':telefone,'local': local},
						function (r) {				
							$('#'+form+" #nome").val($('#'+form+" #nome").attr('initvalue'));
							$('#'+form+" #email").val($('#'+form+" #email").attr('initvalue'));
							$('#'+form+" #telefone").val($('#'+form+" #telefone").attr('initvalue'));
							
							$('#'+form+" #btsend").val('Cadastro efetuado');
							
							
							$('#'+form).attr('s' , false);
							
							setTimeout(function () {$('#'+form+" #btsend").val('Enviar');} , 6000);
						}	
					);
					
				}
				return false;
			  });
		}
}

function showHideAnuncie (target)
{
	target = '#'+target;
	if($(target).css('display') == 'none')	
	{
		$(target).fadeIn('fast');
	}
	else
	{
		$(target).fadeOut('fast');
	}
}
