$(function(){
	/* print-link */
	$('div.results-stat h1 small.print').click(function(){
		window.print();
		if ($('div.results-stat select#form_search_contest').length > 0) {
			var contest_name = $('select#form_search_contest option:selected').text();
			var contest_src = '';
			switch (contest_name){
				case 'Британский бульдог':
					contest_src = 'contests-bulldog';
					break;
				case 'Зимние Интеллектуальные Игры':
					contest_src = 'contests-snowman';
					break;
				case 'Золотое руно':
					contest_src = 'contests-runo';
					break;
				case 'Кенгуру':
					contest_src = 'contests-kenguru';
					break;
				case 'Кенгуру выпускникам':
					contest_src = 'contests-kenguru-grad';
					break;
				case 'КИТ':
					contest_src = 'contests-kit';
					break;
				case 'Моя Удмуртия':
					contest_src = 'contests-myudm';
					break;
				case 'Леонардо':
					contest_src = 'contests-leonardo';
					break;
				case 'ЕНОТ':
					contest_src = 'contests-enot';
					break;
				default:
					contest_src = 'contests-snowman';
				}
			$('form#form_search').after('<p class="print-only"><img src="/assets/images/contests/'+contest_src+'.gif" width="134" height="134" alt="'+contest_name+'" /></p>');
		} else {
			$('form#form_search').after('<p class="print-only"><img src="/assets/images/contests/contests-snowman.gif" width="134" height="134" alt="Зимние Интеллектуальные Игры" /></p>');
		}
	});
	/* corners */
	if ($('p.important').length > 0) {
		$('p.important').corners('5px');
	}
	/* tablesort */
	if ($('div.contests-list table.contests').length > 0) {
		$('div.contests-list table.contests').tablesorter();
	}
	if ($('div.results-stat table.contests').length > 0) {
		$('div.results-stat table.contests').tablesorter();
	}
	/* contests-list */
	if ($('div.contests-list table.all-contests').length > 0) {
		$('div.contests-list table.all-contests').hide();
	}
	$('div.contests-list ul.switch li.active').live('click', function(){
		$('div.contests-list ul.close-contests').toggle();
		$('div.contests-list table.all-contests').toggle();
		$('div.contests-list ul.switch li:not(.active)').addClass('active');
		$(this).removeClass('active');
	});
	/* results-stat */
	if ($('div.results-stat ul.switch').length > 0) {
		$('div.results-stat div.search').hide();
		$('div.results-stat ul.switch li.active').live('click', function(){
			$('div.results-stat div.search').toggle();
			$('div.results-stat ul.switch li:not(.active)').addClass('active');
			$(this).removeClass('active');
		});
	}
	/* toggle */
	if ($('div.toggle').length > 0) {
		$('div.toggle').hide();
		$('h2.toggle>span').click(function(){
			$(this).parent('h2').next('div.toggle').slideToggle('fast');
		});
	}
	/* qa */
	if ($('dl.qa dd').length > 0) {
		$('dl.qa dd').hide();
		$('dl.qa dt span').click(function(){
			$(this).parent('dt').next('dd').slideToggle('fast');
		});
	}
	/* regions */
	$('div.regions div.russia>ul>li>a').click(function(){
		if (!$(this).parent('li').children('div.desc').is('.active')) {
			$('div.regions div.russia ul li div.desc.active').slideUp('fast').removeClass('active');
			$('div.regions div.russia ul li.active').removeClass('active');
			$(this).parent('li').addClass('active');
			$(this).parent('li').children('div.desc').slideDown('fast').addClass('active');
		} else {
			$(this).parent('li').toggleClass('active');
			$(this).parent('li').children('div.desc').slideToggle('fast').toggleClass('active');
		}
		return false;
	});
	/* links */
	$('a[href$=".pdf"], a[href$=".doc"], 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="i/file-'+type+'.gif" width="24" height="17" alt="'+type+'" title="'+type+'" class="file-icon" /> <span class="file-size">'+length+' '+lengthUnits+'</span>');
				link.addClass(type);
			}
		});
	});
	/* map-popup */
	$('div.extra div.roadmap p a').click(function() {
		if(!$('div.popup').length) {
			$('body').append('<ins class="popup-bg"></ins>').append('<div class="popup popup-map"><div class="popup-content"><h3>Схема проезда</h3><p><img src="p/roadmap.png" width="800" height="622" alt="Схема проезда" title="Схема проезда" /></p></div><i class="t"></i><i class="tl"></i><i class="tr"></i><i class="b"></i><i class="bl"></i><i class="br"></i><i class="l"></i><i class="r"></i><ins class="close" title="Закрыть"></ins></div>');
			$('select').hide();
		}
		var closer = function() {
			$('ins.popup-bg').remove();
			$('div.popup').remove();
			$('select').show();
		}
		$('ins.popup-bg').click(function(){
			closer();
		});
		$('div.popup').click(function(){
			closer();
		});
		$(document).keydown(function(e) {
			if (e.which==27) closer();
		});
		return false;
	});
	/* form-popup */
	if ($('div.popup').length > 0) {
		$('div.popup').removeClass('popup-no').hide();
		if ($('div.popup p.error').length > 0) {
			$('div.footer').after('<ins class="popup-bg"></ins>');
			$('div.popup').show();
			var closer = function() {
				$('ins.popup-bg').remove();
				$('div.popup').hide();
			}
			$('ins.popup-bg').click(function(){
				closer();
			});
			$(document).keydown(function(e) {
				if (e.which==27) closer();
			});
		}
	}
	$('div.extra div.contest-links ul li.member a').click(function() {
		$('div.footer').after('<ins class="popup-bg"></ins>');
		$('div#popup-participation.popup').show();
		var closer = function() {
			$('ins.popup-bg').remove();
			$('div#popup-participation.popup').hide();
		}
		$('ins.popup-bg').click(function(){
			closer();
		});
		$(document).keydown(function(e) {
			if (e.which==27) closer();
		});
		return false;
	});
	$('div.extra div.contest-links ul li.partner a').click(function() {
		$('div.footer').after('<ins class="popup-bg"></ins>');
		$('div#popup-partner.popup').show();
		var closer = function() {
			$('ins.popup-bg').remove();
			$('div#popup-partner.popup').hide();
		}
		$('ins.popup-bg').click(function(){
			closer();
		});
		$(document).keydown(function(e) {
			if (e.which==27) closer();
		});
		return false;
	});
	$('div.contests-list a[href$=#popup-participation]').click(function() {
		if ($('div#popup-participation.popup p.error').length == 0) {
			$('div#popup-participation.popup h3').html('Заявка на участие в конкурсе &laquo;<span>'+$(this).attr('title')+'</span>&raquo;');
			$("div#popup-participation.popup input[name^='form_participation_contest']").val($(this).attr('title'));
		}		
			
		$('div.footer').after('<ins class="popup-bg"></ins>');
			
		$('div#popup-participation.popup').show();
		var closer = function() {
			$('ins.popup-bg').remove();
			$('div#popup-participation.popup').hide();
		}
		$('ins.popup-bg').click(function(){
			closer();
		});
		$(document).keydown(function(e) {
			if (e.which==27) closer();
		});
		return false;
	});
	$('div.popup ins.close').click(function() {
		$('ins.popup-bg').remove();
		$(this).parent('div').hide();
	});
});
