var menu_hover = new Image();
menu_hover.src = rootUrl + 'imgs/others/menu-arrow-hover.gif'; //carrega imagem que sera chamada no over do menu

$(document).ready( function() {
	$("a[@rel*=external]").attr({target:"_blank", title:this.title + ' (Este enlace abre una ventana nueva)'});
	$("a[@rel*=back]").click( function(){ history.back(); return false });
	$("a[@rel*=print]").click( function(){ print(); })
	
	if (typeof document.body.style.maxHeight === "undefined") {// ie 6
		ieMinHeight($('#wrapper'),$('html')); // implementa min-height(por javascript apenas para IE6)  no elemento wrapper se ele for menor que elemento html
		ieFlickerFix();
	}
	
	if($('.mask').length != 0) { // mascaras de formulario
		
		/* +date */
		$('.date_day').setMask('39');
		$('.date_month').setMask('19');
		$('.date_year').setMask('9999');
		/* end date */
		
		/* +identifier */
		$('.identifier').setMask('99999999999999999999');
		/* end identifier */
		
		/* +phone */
		$('.ddd').setMask('999');
		$('.phone_number').setMask('9999999999');
		/* end phone */
	}

	if($('#store_locator_form').length != 0) {
		
		/*
		if(!$('#province_field')[0]) {
			$('#store_locator_form fieldset').append('<div id="province_field" class="clearfix"><label for="province" class="bold text_right clear">Seleccione la provincia:</label><select id="province" name="province" class="left"><option value="selecione">Seleccione...</option></select></div>');
			$('#store_locator_form fieldset #province').attr("disabled","disabled");
		}
		*/
		
		if(!$('#city_field')[0]) {
			$('#store_locator_form fieldset').append('<div id="city_field" class="clearfix"><label for="city" class="bold text_right clear">Seleccione la ciudad:</label><select id="city" name="city" class="left"><option value="selecione">Seleccione...</option></select></div>');
			$('#store_locator_form fieldset #city').attr("disabled","disabled");
		}
		
		var ok_button = $('#store_locator_form fieldset input.ok').clone();
		$('input.ok').remove();
		ok_button.appendTo('#city_field');
			
		$('#country').find('option:first').attr('selected','selected')
		
		$('#country').change(
			function() {
				$('#store_locator_form fieldset #city option').remove();
				$('#store_locator_form fieldset #city').attr("disabled","disabled").prepend('<option value="">Cargando...</option>');
				
				$.get('city.asp',{country:$('#country option:selected').val()},function(result){
					$('#city').html(result);
					$('#store_locator_form fieldset #city').removeAttr('disabled');
				});
			}
		);
		
		/*
		$('#country').change(
			function() {
				$('#store_locator_form fieldset #province option').remove();
				$('#store_locator_form fieldset #city option').remove();
				$('#store_locator_form fieldset #city').append('<option value="">Seleccione...</option>')
				$('#store_locator_form fieldset #province').attr("disabled","disabled").prepend('<option value="">Cargando...</option>');
				
				$.get('province.asp',{country:$('#country option:selected').val()},function(result){
					$('#province').html(result);
					$('#store_locator_form fieldset #province').removeAttr('disabled');
				});
			}
		);
		
		$('#province').change(
			function() {
				$('#store_locator_form fieldset #city option').remove();
				$('#store_locator_form fieldset #city').attr("disabled","disabled").prepend('<option value="">Cargando...</option>');
				
				$.get('city.asp',{country:$('#country option:selected').val(), province:$('#province option:selected').val()},function(result){
					$('#city').html(result);
					$('#store_locator_form fieldset #city').removeAttr('disabled');
				});
			}
		);
		*/
	}
	
	if($('.default_form').length != 0) {
		$('.default_form').submit( // validacao de forms
			function() {
				if($('input').val() == '' || $('select').val() == '' || $('textarea').val() == '') {
					if($('.error_message').length != 0)
						$('.error_message').remove();
					
					$('<div id="form-message" class="error_message"><ul></ul></div>').insertBefore(this);
					
					if($(this).find('#name').length != 0 && $(this).find('#name').val() == '')
						$(this).prev('.error_message').find('ul').append(formWarning('fill', 'nombre', '<li><em>','</em></li>'));
					
					if($(this).find('#date_day').length != 0 && $(this).find('#date_day').val() == '' || $(this).find('#date_month').length != 0 && $(this).find('#date_month').val() == '' || $(this).find('#date_year').length != 0 && $(this).find('#date_year').val() == '')
						$(this).prev('.error_message').find('ul').append(formWarning('fill', 'fecha de nacimiento', '<li><em>','</em></li>'));	
					
					if($(this).find('#city').length != 0 && $(this).find('#city').val() == '')
						$(this).prev('.error_message').find('ul').append(formWarning('fill', 'ciudad', '<li><em>','</em></li>'));
					
					if($(this).find('#complete-address').length != 0 && $(this).find('#complete-address').val() == '')
						$(this).prev('.error_message').find('ul').append(formWarning('fill', 'dirección completa', '<li><em>','</em></li>'));
					
					if($(this).find('#identifier').length != 0 && $(this).find('#identifier').val() == '')
						$(this).prev('.error_message').find('ul').append(formWarning('fill', 'documento de identidad', '<li><em>','</em></li>'));
					
					if($(this).find('#email').length != 0 && $(this).find('#email').val() == '')
						$(this).prev('.error_message').find('ul').append(formWarning('fill', 'e-mail', '<li><em>','</em></li>'));
					
					if($(this).find('#country').length != 0 && $(this).find('#country').val() == '')
						$(this).prev('.error_message').find('ul').append(formWarning('select', 'país', '<li><em>','</em></li>'));
					
					if($(this).find('#phone-ddd').length != 0 && $(this).find('#phone-ddd').val() == '' || $(this).find('#phone-number').length != 0 && $(this).find('#phone-number').val() == '')
						$(this).prev('.error_message').find('ul').append(formWarning('fill', 'teléfono', '<li><em>','</em></li>'));
					
					if($(this).find('#post').length != 0 && $(this).find('#post').val() == '')
						$(this).prev('.error_message').find('ul').append(formWarning('fill', 'La estación ya posee una tienda', '<li><em>','</em></li>'));
					
					if($(this).find('#becoming-a-franchisee').length != 0 && $(this).find('#becoming-a-franchisee').val() == '')
						$(this).prev('.error_message').find('ul').append(formWarning('fill', '?Por qué desea ser una franquicia?', '<li><em>','</em></li>'));
					
					if($(this).find('#what-you-know').length != 0 && $(this).find('#what-you-know').val() == '')
						$(this).prev('.error_message').find('ul').append(formWarning('fill', '?Que conoce de la marca Spacio1?', '<li><em>','</em></li>'));
					
					if($(this).find('#subject').length != 0 && $(this).find('#subject').val() == '')
						$(this).prev('.error_message').find('ul').append(formWarning('select', 'asunto', '<li><em>','</em></li>'));
					
					if($(this).find('#message').length != 0 && $(this).find('#message').val() == '')
						$(this).prev('.error_message').find('ul').append(formWarning('fill', 'mensaje', '<li><em>','</em></li>'));
										
					document.location = "#" + 'form-message';
					return false;
				}
			}
		);
	}
});

/* +functions */
function formWarning(type, field, before, after) { // type = select(para campos de multiplas escolhas) or fill (para campos de preenchimento ex: input text) - mensagem de campos de formularios nao(ou mal) preenchidos
	if(type != 'select')
		var sentence = 'Complete el campo "' + field + '"';
	else
		var sentence = 'El campo "' + field + '" necesita tener una opción seleccionada';
	
	return before + sentence + after;
}

function ieMinHeight(elm,elm_compare) { // min-height para ie6
	if($(elm).height() > $(elm_compare).height())
		$(elm).css("height","auto");
	else
		$(elm).css("height","100%");
}

function ieFlickerFix(){
	try {
		document.execCommand("BackgroundImageCache", false, true);

	} catch(err) {}
}

String.prototype.isEmail = function() { // validacao de e-mail
	var rx = new RegExp("\\w+([-+.\']\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*");

	var matches = rx.exec(this);
	return (matches != null && this == matches[0]);
}
/* end functions */
