$(function(){
		/**
		* pretty checkboxes
		*--------------------------------------------------------------------------*/
		if($.fn.checkbox !== undefined){
				var matched = function() {
					$(this).checkbox();
				};
				$('#taxcontent input.pretty, #arcocontent input.pretty, #opinion input.pretty, #attentioncontent input.pretty, #sign_up input.pretty').livequery(matched);

				// custom css just for hide .pretty radios
				var s = document.createElement("link")
				s.rel = "stylesheet";
				s.type = "text/css";
				s.media = "screen";
				s.href = "/OficinaVirtual/webdirecto/genesis/stylesheets/genesis.css";
				document.body.appendChild(s);
		}

		// /**
		//     * ajax history para tarificador
		//     *--------------------------------------------------------------------------*/
		//     if($.historyInit !== undefined){
		//         function stepload(hash) {
		//             if(hash.length > 0){
		//                 $("#taxcontent, #arcocontent").load(hash + ".html");
		//             }
		//         }
		//         $.historyInit(stepload);
		//     }


		/**
		* validator
		*--------------------------------------------------------------------------*/
		if($.validator !== undefined){
				$.validator.addMethod(
						"regex",
						function(value, element, regexp) {
								var check = false;
								var re = new RegExp(regexp);
								return this.optional(element) || re.test(value);
						},
						"Please check your input."
				);
				$.validator.addMethod(
						"maxDaysFromNow",
						function(value, element, days) {
								// $date = $(element).val().split("/");
								// $checkDate = new Date;
								// $checkDate.setFullYear( parseInt($date[2]), parseInt($date[1])-1, parseInt($date[0]) );
								// $daysfromnow = ($checkDate - new Date)/86400000;  // days to milliseconds
								// return ($checkDate.getMonth() == $date[1]-1 && $daysfromnow < days && $daysfromnow > 0)
								return false;
						},
						"&nbsp;"
				);
				$.validator.addMethod(
						"minYearsFromNow",
						function(value, element, years) {
								$date = $(element).val().split("/");
								$checkDate = new Date;
								$checkDate.setFullYear( parseInt($date[2]), parseInt($date[1])-1, parseInt($date[0]) );
								$yearsfromnow = (new Date - $checkDate)/31536000000;  // years to milliseconds
								return ($checkDate.getMonth() == $date[1]-1 && $yearsfromnow > years)
						},
						"&nbsp;"
				);

				$.validator.addMethod(
						"valid_date",
						function(value, elementyears) {
								$date = $(element).val().split("/");
								$checkDate = new Date;
								$checkDate.setFullYear( parseInt($date[2]), parseInt($date[1])-1, parseInt($date[0]) );
								return ($checkDate.getMonth() == $date[1]-1)
						},
						"&nbsp;"
				);

				$.validator.addMethod(
						 "dateExists",
						 function(value, element) {
								 date = value.split('/');
								 date[1]--
								 var oDate = new Date(date[2],date[1],date[0]);
								 if (date[2] != oDate.getFullYear() ||  date[1] != oDate.getMonth() || date[0] != oDate.getDate()){
										return false;
								 }
								 return true;
						 },
						 "&nbsp;"
				 );

				 $.validator.addMethod(
							 "checkNoFuture",
							 function(value, element) {
								 date = value.split('/');
								 date[1]--
								 var nDate = new Date(date[2],date[1],date[0]);
								 var today = new Date();

								 if (nDate > today) {
									 return false;
								 };

								 return true;
							 },
							 "&nbsp;"
					 );
		}

		// Validates date
		$.prototype.minYears = function (years) {
			$date = $(this).val().split("/");
			$checkDate = new Date;
			$checkDate.setFullYear( parseInt($date[2]), parseInt($date[1])-1, parseInt($date[0]) );
			$yearsfromnow = (new Date - $checkDate)/31536000000;  // years to milliseconds
			return ($checkDate.getDate() == parseInt($date[0]) && $checkDate.getMonth() == $date[1]-1 && $yearsfromnow > years)
		};

		// Validates date
		$.prototype.maxYears = function (years) {
			$date = $(this).val().split("/");
			$checkDate = new Date;
			$checkDate.setFullYear( parseInt($date[2]), parseInt($date[1])-1, parseInt($date[0]) );
			$yearsfromnow = ($checkDate-new Date)/31536000000;  // years to milliseconds
			return ($checkDate.getDate() == parseInt($date[0]) && $checkDate.getMonth() == $date[1]-1 && $yearsfromnow < years)
		};

		// Validates date
		$.prototype.valid_date = function () {
			$date = $(this).val().split("/");
			$checkDate = new Date;
			$checkDate.setFullYear( parseInt($date[2]), parseInt($date[1])-1, parseInt($date[0]) );
			return ($checkDate.getDate() == parseInt($date[0]) && $checkDate.getMonth() == $date[1]-1)
		};

		/**
		* tooltips configuration
		*--------------------------------------------------------------------------*/
		if($.fn.qtip !== undefined) {
				$("a[class^='tooltip']").live("click", function(e){e.preventDefault(); $('.qtip').qtip('hide');});
				$(".tooltip_close").live("click", function(e){e.preventDefault(); $('.qtip').qtip('hide');})
				$.fn.qtip.styles.genesis = {
						width: 250,
						background: '#ffffff',
						color: '#333333',
						textAlign: 'left',
						padding: '10px 10px',
						border: {
								width: 1,
								radius: 0,
								color: '#333333'
						}
				}
				tooltip_click_config = {
						style: 'genesis',
						show: { when: { event: 'click' } },
						hide: { when: { event: 'click' } },
						position: {
								corner: { target: 'bottomLeft', tooltip: 'topLeft' },
								adjust: { y: 10, x: -10 }
						}
				}
		}



/**
* live action for close lightboxes.
*--------------------------------------------------------------------------*/
if($.fn.colorbox !== undefined){
	$(".closelightbox").live('click', function(e){
		e.preventDefault();
		parent.$.fn.colorbox.close();
		return false;
	});


	$(".recover_box a").colorbox({
		opacity: 0.5,
		overlayClose: false,
		iframe: true,
		width: "770px",
		height: "335px"
	});

	/**
	* live action for open modal window "LLamanos"
	*--------------------------------------------------------------------------*/
	$(".call_you").live('click', function(e) {
		e.preventDefault();
		$(this).colorbox({
			opacity: 0.5,
			overlayClose: false,
			open: true,
			iframe: true,
			width: "760px",
			height: "500px"
		});
	});

	/**
	* open modal window with online office login
	*--------------------------------------------------------------------------*/
	$(".oi").colorbox({
		iframe: true,
		width: "790px",
		height: "380px"
	});
}


/**
* File replacement
*--------------------------------------------------------------------------*/
$.fn.file_replacement = function(opts){

	var defaults = {
		placeholder: "Adjuntar un nuevo archivo"
	};
	var options = $.extend(defaults, opts||{});

	return this.each(function(){
		var self = this;
		var wrapper = $("<div>")
					.css({
						'width' : '100%',
						'overflow' : 'hidden'
					});
		var filename = $('<span class="file">' + options.placeholder + '</span>')
					.addClass($(self).attr("class"))
					.css({
						'position' : 'absolute',
						'z-index' : '0'
					});

		$(self).before(filename);
		$(self).wrap(wrapper);

		$(self).css({
			'opacity' : '0.0',
			'height' : $(filename).height() + 5 + "px",
			'width' : $(filename).width() + 5 + "px",
			'font-size' : '60px',
			'cursor' : 'pointer',
			'position' : 'relative',
			'z-index' : '1'
		});

		$(self).bind("change", function() {
			$(filename).html($(self).val());
			$(self).css({
				'height' : $(filename).height() + 5 + "px",
				'width' : $(filename).width() + 5 + "px"
			});
		});
	});
}


/**
* Example inputs
*--------------------------------------------------------------------------*/
if ($.fn.example !== undefined) {
	$('.login_form #login').example('Usuario');
	$('.login_form #password').example('Contraseña');
	$('.brand_type #car_index').example('Escribe aqui la marca');

};


/**
* Footer position at bottom
*--------------------------------------------------------------------------*/
var footer_position = {
	init: function(){
		var footer = $('#footer .home_legal'),
		body = $('body'),
		win = $(window);
		win
			.bind('footer_position', function(){
			if (win.height() > body.height()) {
				footer.css('bottom', -(win.height()-body.height())+'px');
			}else{
				footer.css('bottom', '0px');
			}
			})
			.trigger('footer_position')
			.bind('resize', function(){
				win.trigger('footer_position');
			})
	}
}
footer_position.init();



});
