if(jQuery.browser.mozilla && /^1\.8/.test(jQuery.browser.version)) {
	var headID = document.getElementsByTagName('head')[0];
	var cssNode = document.createElement('link');
	cssNode.type = 'text/css';
	cssNode.rel = 'stylesheet';
	cssNode.media = 'screen';
	cssNode.href = PinkAd.CSS_URL+'ff2.css';
	headID.appendChild(cssNode);
}

PinkAd.Menu = {
	TIMEOUT: 0.5,
	ActiveSubmenu: null,
	ActiveSection: null,
	CurrentTimeout: null,
	init: function() {
		var obj = this;
		this.hoverConfig = {
			sensitivity: 1,
			interval: 50,
			over: function() {
				obj = PinkAd.Menu;
				var section = $(this).attr('id').slice(5);
				if($('#submenu-'+section).length == 0) {
					return;
				}
				if(obj.ActiveSubmenu !== null && obj.ActiveSection == section) {
					return;
				}
				if(obj.ActiveSubmenu !== null) {
					obj.hideActive();
				}
				obj.show(section);
			},
			out: function() {}
		}
		$('#header div.top #menu a.item').hoverIntent(this.hoverConfig);
	},
	show: function(section) {
		this.ActiveSubmenu = $('#submenu-'+section)[0];
		this.ActiveSection = section;
		$(this.ActiveSubmenu).fadeIn('fast');
	},
	hideActive: function() {
		$(this.ActiveSubmenu).fadeOut('fast');
		this.ActiveSubmenu = null;
		this.ActiveSection = null;
	}
}

$(document).ready(function() {
	$('h3.neophyte, a.neophyte').facelift({path: PinkAd.FLIR_URL,font:'y2k_neophyte'});

	PinkAd.Menu.init();

	$('textarea[name="comentarios"]').autogrow();
	$('input[type="text"].numbers').inputfilter(/\d/);
	$('input[name="telefone_ddd"]').autotab($('input[name="telefone"]')[0]);

	$('a.alt-submit').live('click',function() {
		$(this).closest('form').trigger('submit');
	});

	var attributes = {};
	var flashvars = {};
	var params = {
		allowFullScreen: 'true',
		allowscriptaccess: 'always',
		wmode: 'transparent'
	};
	swfobject.embedSWF(
		PinkAd.FLASH_URL+'flash_home.swf',
		'flash_home', '460', '345', '9.0.0',
		PinkAd.FLASH_URL+'expressInstall.swf',
		flashvars, params, attributes
	);

	$('form#contato_anunciante, form#contato_parceiro').ajaxForm({
		dataType: 'json',
		beforeSubmit: function(data,form,options) {
			var errors = [];
			$(':input',form).each(function() {
				var $this = $(this);
				if($this.val().length == 0 && $this.hasClass('required')) {
					$this.addClass('error');
					errors.push('O campo '+($this.attr('title')||$this.attr('name'))+' é obrigatório');
					return;
				}
				switch($this.attr('name')) {
					case 'telefone_ddd':
						if(!$this.val().match(/^\d{2}$/)) {
							$this.addClass('error');
							errors.push('Este DDD não é válido');
							return;
						}
						break;
					case 'telefone':
						if(!$this.val().match(/^\d{8}$/)) {
							$this.addClass('error');
							errors.push('Este telefone não é válido');
							return;
						}
						break;
					case 'email':
						if(!$.trim($this.val()).match(/^(?:[a-zA-Z0-9_\-\+]+\.)*[a-zA-Z0-9_\-\+]+@(?:[a-zA-Z0-9]+)(?:\.\w{2,4})+$/)) {
							$this.addClass('error');
							errors.push('Este email não é válido');
							return;
						}
						break;
				}
				$this.removeClass('error');
			});
			if(errors.length > 0) {
				alert(errors.join("\n"));
				return false;
			}
			$('div.form_container').hide();
			$('div.form_loading').show();
		},
		success: function(response) {
			$('div.form_loading').hide();
			$('div.form_complete').show();
		}
	});
});
