$(function(){

	/* input digits only */
	$('input[name=SeatsCount].tiny').keypress(function(e) {
		if(e.which!=8 && e.which!=0 && (e.which<48 || e.which>57)) {
			return false;
		}
	});
	/* mobilephone format 
	if ($('input[name=REGISTER[PERSONAL_MOBILE]]').length > 0) {
		$('input[name=REGISTER[PERSONAL_MOBILE]]').setMask'(999) 999-99-99');
	};
	
	if ($('input[name=REGISTER[PERSONAL_BIRTHDAY]]').length > 0) {
		$('input[name=REGISTER[PERSONAL_BIRTHDAY]]').setMask('99.99.9999');
	};
*/
	if ($("input[id='date']").length > 0) {
		$("input[id='date']").setMask('99.99');
	};
if ($("input[id='DATE']").length > 0) {
		$("input[id='DATE']").setMask('99.99');
	};
	/* qa */
	if ($('dl.qa dd').length > 0) {
		$('dl.qa dd').hide();
		$('dl.qa dt span').click(function(){
			$(this).parent('dt').next('dd').slideToggle('fast');
		});
	};

	/* lightbox */
	if ($('div.extra div.photo ul li a').length > 0) {
		$('div.extra div.photo ul li a').lightBox();
	};
	/* drivers-date format */
	if ($('div.content div.form dl.form dd input.date').length > 0) {
		$('div.content div.form dl.form dd #date').setMask('99.99.9999');
	};
	/* links */
	$('a[href$=".pdf"], a[href$=".doc"], a[href$=".rtf"], a[href$=".xls"], a[href$=".zip"], a[href$=".jpg"]').each(function(){
		var link = $(this);
		var bits = this.href.split('.');
		var type = bits[bits.length -1];
		
		var url= "http://json-head.appspot.com/?url="+encodeURIComponent (this.href)+"&callback=?";

		// then call the json thing and insert the size back into the link text
		 $.getJSON(url, function(json){
			if(json.ok && json.headers['content-length']) {
				var length = parseInt(json.headers['content-length'], 10);
				
				// divide the length into its largest unit
				var units = [
					[1024 * 1024 * 1024, 'ГБ'],
					[1024 * 1024, 'МБ'],
					[1024, 'КБ'],
					[1, 'Б']
				];
				
				for(var i = 0; i < units.length; i++){
					
					var unitSize = units[i][0];
					var unitText = units[i][1];
					
					if (length >= unitSize) {
						length = length / unitSize;
						// 1 decimal place
						length = Math.ceil(length * 10) / 10;
						var lengthUnits = unitText;
						break;
					}
				}
				
				// insert the text directly after the link and add a class to the link
				// note: if you want to insert the size into the link rather than after it change the following 'after' to 'append'
				link.after(' <img src="/assets/images/ico/'+type+'.gif" width="24" height="17" alt="'+type+'" title="'+type+'" class="file-icon" /> <span class="file-size">'+length+' '+lengthUnits+'</span>');
				link.addClass(type);
			}
		});
	});
});

