$(function(){
	/* rhomb */
	$('i.rhomb').click(function(){
		
		switch ($(this).attr('class')) {
			case 'rhomb': $(this).addClass('rhomb1'); break;
			case 'rhomb rhomb1': $(this).addClass('rhomb2'); break;
			case 'rhomb rhomb1 rhomb2': $(this).addClass('rhomb3'); break;
			case 'rhomb rhomb1 rhomb2 rhomb3': $(this).addClass('rhomb4'); break;
			case 'rhomb rhomb1 rhomb2 rhomb3 rhomb4': $(this).attr('class','rhomb'); $('div.header blockquote p').text('Иди работай ;)'); break;
		}
	});
	/* menu-ext */
	$('div.menu-ext').addClass('menu-ext-js').width($('div.menu').width()-90);
	$('div.menu h3').click(function(){
		$('div.menu-ext').slideToggle('fast');
	});
	$('div.menu-ext ul li h4').click(function(){
		$('div.menu-ext ul li h4').removeClass('active').siblings().hide();
		$(this).addClass('active').siblings().show();
	});
	$(window).bind('resize', function(){
		$('div.menu-ext').width($('div.menu').width()-90);
	});
	$('div.main, div.header').bind('click', function(e){
		$('div.menu-ext').slideUp('fast');
	});
	/* cat-arch */
	$('div.navigation dl.cat-arch dt span').click(function(){
		$('div.navigation dl.cat-arch dt').removeClass('active');
		$('div.navigation dl.cat-arch dd').removeClass('active');
		$(this).parent('dt').addClass('active').next('dd').addClass('active');
	});
	/* file-types */
	$('a[href$=".pdf"], a[href$=".doc"], a[href$=".zip"], a[href$=".xls"]').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="i/file-'+type+'.gif" width="16" height="16" alt="'+type+'" title="'+type+'" class="file-icon" /> <span class="file-size">'+length+' '+lengthUnits+'</span>');
				link.addClass(type);
			}
		});
	});
	/* columns */
	$('div.navigation').height($('div.main').height());
	$('div.extra').height($('div.main').height());
});
