$(document).ready(function() {
	$("#sort-table, #sort-table-2, #sort-table-3").tablesorter();
	$('#map').lightBox();
	$('#flag').lightBox();
	$('#gerb').lightBox();
	$('#projection-link, #projection-close').click(function(){$('#projection-body').slideToggle(300); return false;});
	$('#what-time-link, #what-time-close').click(function(){$('#what-time-body').slideToggle(300); return false;});
	if ($('#compare-list')) {
		$('#compare-list').change(function(){
			var current_id = $('#current-country').attr('value');
			var compare_id = $('#compare-list :selected').attr('value');
			if (compare_id != 0 && compare_id != current_id) {
				$('body').append('<div id="fog" class="fog" style="display: none;"></div>');
				$('body').append('<div id="compare-div" class="compare-div" style="display: none;"></div>');
				$('#compare-div').append('<a href="javascript:void(0);" id="compare-close" class="compare-close"></a>');
				$.ajax({
					url: 'ajax/compare.php?id1='+current_id+'&id2='+compare_id,
					dataType : "html",
					success: function (data) {
						if (data != 'error') {
							$('#compare-div').append('<div id="compare-data">'+data+'</div>');
							$('#fog').fadeIn(300);
							$('#compare-div').fadeIn(300);
							$('#fog, #compare-close').click(function(){
								$('#compare-div').fadeOut(300); 
								$('#fog').fadeOut(300);
								$('#compare-list option[value=0]').attr('selected', 'selected');
								setTimeout(function(){
									$('#compare-data, #compare-div, #fog').remove()
								}, 1000);
							});
						}
					}
				});
									
			}	
		});
	}
	//fun shit
	function sound(s) {
		if (s == 1) {
			text = 'Your clothes... give them to me, now';
			time = 4000;
		}
		if (s == 2 || s == 3 || s == 4) text = time = 0;
		if (s == 5) {
			text = 'I need a vacation';
			time = 1700;
		}		
		if (s == 6) {
			text = 'Fuck you, asshole';
			time = 1700;
		}
		var player = document.getElementById('player-'+s);
		player.play();
		if (text != 0){
			$('#subtitles').text(text);
			$('#subtitles').fadeIn('slow');
			setTimeout(function(){$('#subtitles').fadeOut('slow');}, time);	
		}	
	}
	function shooting(){
		if (fun_count < 0) {
			$('#fun').addClass('red-eye');
			clearInterval(f);
			f = setInterval(function(){$('#fun').fadeTo(1000, 1).fadeTo(1000, 0);}, 2000);	
			fun_count_plus = 1;
		}	
		if (fun_count == 0) {
			$('#page').append('<div id="subtitles"></div>');
			sound(1);
			fun_count_plus = 1;
		}
		if (fun_count == 1) {
			sound(2);
			fun_count_plus = 1;
		}			
		if (fun_count > 1 && fun_count <= 1 + clip_size) {
			shots = Math.random() * (1);
			max_x = $(window).width();
			max_y = $(window).height();
			//burst
			if (shots > 0.2 && fun_count < clip_size - 2) {
				shots = 4;
				shots_arr = new Array(shots);				
				y0 = Math.floor(max_y / 100 * (Math.random() * (78) + 2));
				y1 = Math.floor(max_y / 100 * (Math.random() * (78) + 2));
				var tmp = Math.random() * (1);
				var tmp1 = Math.floor(max_x / 100 * (Math.random() * (20) + 70));
				var tmp2 = Math.floor(max_x / 100 * (Math.random() * (20) + 2));
				if (tmp > 0.5) {x0 = tmp1;	x1 = tmp2;}	
				else {x0 = tmp2; x1 = tmp1;}	
				x_step = (x1 - x0) / shots;				
				y_step = (y1 - y0) / shots;				
				shots_arr[0] = new Array(2)
				shots_arr[0]['x'] = x0;
				shots_arr[0]['y'] = y0;
				for (i = 1; i < shots; i++) {
					x0 = Math.floor(x0 + x_step);
					y0 = Math.floor(y0 + y_step);
					shots_arr[i] = new Array(2)
					shots_arr[i]['x'] = x0;
					shots_arr[i]['y'] = y0;
				}	
			}
			else {					//1 shot
				shots = 1;
				shots_arr = new Array(shots);
				for (i = 0; i < shots; i++) {
					x = Math.floor(max_x / 100 * Math.floor(Math.random() * (78)) + 2);
					y = Math.floor(max_y / 100 * Math.floor(Math.random() * (78)) + 2);
					shots_arr[i] = new Array(2)
					shots_arr[i]['x'] = x;
					shots_arr[i]['y'] = y;
				}
			}
			//bullet holes
			for (i = 0; i < shots; i++) {
				var shot_delay = speed * i + 200;
				b_h = Math.floor((Math.random() * 3) + 1);
				$('#page').append('<div class="bullet_hole b_h_'+b_h+'" id="id_'+shots_arr[i]['x']+'_'+shots_arr[i]['y']+'" style="left: '+shots_arr[i]['x']+'px; top: '+shots_arr[i]['y']+'px"></div>');
				var id = '#id_'+shots_arr[i]['x']+'_'+shots_arr[i]['y'];
				$(id).fadeTo(shot_delay, 0).fadeTo(1,1);
			}
			if (shots == 1) sound(3);
			else  sound(4);
			fun_count_plus = shots;
		}
		if (fun_count == clip_size + 2) {
			sound(5);
			fun_count_plus = 1;
		}			
		if (fun_count > clip_size + 2) {
			sound(6);
			fun_count_plus = 1;
		}			
		fun_count = fun_count + fun_count_plus;
	}	
	$('#page').append('<span id="fun"></span>');
	$('#fun').fadeTo(0, 0);
	var f = setInterval(function(){$('#fun').fadeTo(1000, 1).fadeTo(1000, 0);}, 5000);	
	var fun_count = -1;
	var fun_count_plus = text = time = 0;
	var speed = 70; //rate of fire
	var clip_size = 30; 
	if (!($.browser.msie && $.browser.version < 9)) {	
		$('#fun').click(function(){
			if ($('#fun_div').length) shooting();
			else {
				$('#page').append('<div id="fun_div" style="display: none;"></div>');
				$('#fun_div').load('../includes/fun.php');
				var current_id = $('#current-country').attr('value');
				$.ajax({
					url: 'ajax/fun_count.php?id='+current_id,
					dataType : "html",
					success: function (data){}
				});				
				shooting();
			}	
		});
	}
	else {
		$('#fun').click(function(){
			$('#page').append('<div id="ie-must-die"></div>');
			$('#ie-must-die').fadeIn('slow');
		});	
	}	

});
function weather(id) {
	$('#media').append('<h5>Погода в столице</h5>');
	$('#media').append('<a href="http://www.gismeteo.ru/towns/'+id+'.htm"><img src="http://informer.gismeteo.ru/'+id+'-10.GIF" alt="" /></a>');
}
