var current_menu_active;
var $li;
var site = {
	global: {
		init: function() {
			try {
				$("table.table2 tr").addClass("white");
				$("table.table2 tr:nth-child(odd)").addClass("grey");
			}
			catch ( e ) {}

			
			Cufon.replace('h1 span, .menu li a span');
			
			$("#menu li").hover(function(){
				current_menu_active = $("#menu li.hsm.active");
				$li.removeClass("current");
				$("#menu li.hsm").removeClass("active");
				$(this).addClass("current");
				
			}, function(){
				$li = $(this);
				current_menu_active.addClass("active");
				setTimeout('$li.removeClass("current")', 500);
			});
		}
	},
	inline_editor: {
		init: function(){
			$(".inline-editor .edit-button").click(function() {
				var $editor = $(this).parents(".inline-editor");
				
				$editor.find(".editor").each(function(){
					var value = $(this).text();
					var name = $(this).attr("rel");
					
					if ($(this).hasClass("date") || $(this).hasClass("text")) {
						$editor.data(name, $(this).replaceWith('<input class="text" type="text" name="'+name+'" value="'+value+'" />').html());			
					}
					if ($(this).hasClass("textarea")) {
						$editor.data(name, $(this).replaceWith('<textarea class="textarea" style="width:320px; height:100px;" name="'+name+'">'+value+'</textarea>').html());
					}
					 
				});
				
				$(this).hide();
				$editor.find(".edit-control").show();
				
				return false;
			});
			
			$(".inline-editor .save-button").click(function(){
				$(this).parents("form").submit();
				
				return false;
			});
			
			$(".inline-editor .cancel-button").click(function(){
				var $editor = $(this).parents(".inline-editor");
				
				$editor.find(":input").each(function(){
					var name = $(this).attr("name");
					if ($(this).hasClass("text")) {
						$(this).replaceWith('<span class="editor text" rel="'+name+'">'+$editor.data(name)+'</span>');
					}
					if ($(this).hasClass("textarea")) {
						$(this).replaceWith('<span class="editor textarea" rel="'+name+'">'+$editor.data(name)+'</span>');
					}
				});
				
				$(this).parents(".edit-control").hide();
				$editor.find(".edit-button").show();
				return false;
			});

		}
	},
	index: {
		init: function() {
			$("#voting-1").submit(function(){
				$.ajax({
					type: "post",
					dataType: "text",
					url: '/votings/1/',
					data: $("#voting-1").serialize(),
					success: function(data){
						$("#voting-1").html('Ваш голос учтен');
					}
				});
				
				return false;
			});
			
			$("#voting-2").submit(function(){
				$.ajax({
					type: "post",
					dataType: "text",
					url: '/votings/2/',
					data: $("#voting-2").serialize(),
					success: function(data){
						$("#voting-2").html('<div style="padding:5px; color:#fff">Ваш голос учтен</div>');
					}
				});
				
				return false;
			});
			
			$("#actions-ctrl a").hover(function(){
		        var el = $(this);
		        $("#actions-ctrl img.selected").hide();
		        $("#actions-ctrl img.default").show();
		        
		        $("img.default", el).hide();
		        $("img.selected", el).show();
		        
		        $("#actions .action").hide();
		        //setTimeout($("#actions .action-"+el.attr("rel")).show(), 1000);
		        $("#actions .action-"+el.attr("rel")).show()
		        
		        current_action = $("#actions-ctrl a").index(this);
		    }, function(){
		    });
		    
		    $("#actions-ctrl img.default:eq(0)").hide();
		    $("#actions-ctrl img.selected:eq(0)").show();
		    
		    $("#play-ctrl a.play-pause").toggle(function(){ 
				$(this).removeClass("pause"); $(this).addClass("play");
				clearInterval(actions_timer);
				actions_timer = null;
		    }, function(){
		        $(this).removeClass("play"); $(this).addClass("pause");
		        actions_timer = setInterval(showNextAction, actions_interval);
		    });
		    
		    $("#play-ctrl .prev").click(function(){
		    	if ( !actions_timer ) {
		    		current_action--;
		    		showNextAction();
		    	}
		        return false;
		    });
		    
		    $("#play-ctrl .next").click(function(){
		    	if ( !actions_timer ) {
		    		current_action++;
		    		showNextAction();
		    	}
		        return false;
		    });

		}
	},
	
	gallery: {
		params: {
			photos_count: 0,
			groups_count: 0,
			current_photo: 1,
			current_group: 1,
			current_year: 1,
			years_count: 0,
			interval: null
		},
		
		load_know: function(photo_id) {
			$.ajax({
				type: "get",
				dataType: "html",
				url: '/gallery/get-know/'+photo_id+'/',
				data: {},
				success: function(data){
					$("#get-know").html(data);
				}
			});

		},
		
		init: function() {
			site.gallery.params.photos_count = $("#gallery #photos-holder a").length;
			site.gallery.params.groups_count = Math.ceil(site.gallery.params.photos_count / 7);
			
			$("#know").click(function(){
				var $a = $(this);
				$.ajax({
					type: "get",
					dataType: "text",
					url: $a.attr("href"),
					data: {},
					success: function(data){
						site.gallery.load_know($a.attr("rel"));
					}
				});
				
				return false;
			});
			
			$("#gallery #photos-holder a").click(function(){
				site.gallery.params.current_photo = parseInt($(this).attr("rel"))+1;
				
				var buf = new Image();
				var a = $(this);
				buf.onload = function() {
					$("#gallery-big-photo").css('background-image', 'url("'+buf.src+'")');
					$("#gallery #photos-holder a").removeClass("current");
					a.addClass("current");
					
					if (parseInt(buf.width) < 634)
						$("#photo-title").css({width: buf.width, left:'50%', 'margin-left':'-'+((buf.width)/2)+'px'});
					else
						$("#photo-title").css({width: 634, left:'50%', 'margin-left':'-'+((634)/2)+'px'});
					
					if ( a.attr("title").toString().length )
						$("#photo-title div").text(a.attr("title"));
				}
				buf.src = $(this).attr("href");	
				
				var photo_id = a.attr("id").replace("photo-","");
				
				$("#know").attr("href", '/gallery/know/'+photo_id+'/');
				$("#know").attr("rel", photo_id);
				site.gallery.load_know(photo_id);
				
				return false;
			});
			
			$("#gallery #photos-holder a:eq(0)").click();
			
			$("#gallery .buttons a").click(function(){
				var type = $(this).attr("rel");
				if (type == 'slideshow') {
					if (!$(this).hasClass("pause")) {
						site.gallery.runSlideshow();
						$(this).addClass("pause");
					}
					else {
						site.gallery.stopSlideshow();
						$(this).removeClass("pause");
					}
					
					return false;
				}
				if (type == 'next') {
					site.gallery.params.current_photo++;
				}
				if (type == 'prev') {
					site.gallery.params.current_photo--;
				}
				if (type == 'first' || site.gallery.params.current_photo > site.gallery.params.photos_count) {
					site.gallery.params.current_photo = 1;
					site.gallery.params.current_group = 2;
				}
				if (type == 'last' || site.gallery.params.current_photo < 1) {
					site.gallery.params.current_photo = site.gallery.params.photos_count;
					site.gallery.params.current_group = site.gallery.params.groups_count-1;
				}
				
				var left_limit = site.gallery.params.current_photo <= (site.gallery.params.current_group-1)*7;
				var right_limit = site.gallery.params.current_photo > site.gallery.params.current_group*7;
				if (left_limit || right_limit) {
					if (left_limit)
						site.gallery.params.current_group--;
					if (right_limit)
						site.gallery.params.current_group++;						
				}
				
				$("#photos-holder").animate({marginLeft: '-'+((site.gallery.params.current_group-1)*469)+'px'});
				
				$("#gallery #photos-holder a:eq("+(site.gallery.params.current_photo-1)+")").click();
				
				return false;
			});
		},
		runSlideshow: function() {
			$("#gallery .buttons a.next").click();
			site.gallery.params.interval = setInterval('$("#gallery .buttons a.next").click()', 4000);
		},
		stopSlideshow: function() {
			clearInterval(site.gallery.params.interval);
		},
		years: function() {
			site.gallery.params.years_count = $("#galleries-holder a").length;
			
			$("#galleries-navigation .arrow").click(function(){
				var type = $(this).attr("rel");
				if (type == 'right')
					site.gallery.params.current_year++;
				else
					site.gallery.params.current_year--;
					
				if (site.gallery.params.current_year < 1)
					site.gallery.params.current_year = site.gallery.params.years_count-3;
				if (site.gallery.params.current_year > site.gallery.params.years_count-3)
					site.gallery.params.current_year = 1;
					
				$("#galleries-holder").animate({marginLeft: '-'+((site.gallery.params.current_year-1)*130)+'px'});
				
				return false;
			});
		}
	},
	matches: {
		init: function() {
			$("table.matches-table tr.match").hover(function(){
				$(this).addClass("current");
			}, function(){
				$(this).removeClass("current");
			})
			.click(function(){
				/*$.ajax({
					type: "post",
					dataType: "html",
					url: ,
					beforeSend: function() {
					},
					success: function(data){
					},
					error: function() {
					}
				});*/
			});
			
			$("#matches-type-switcher a").click(function(){
				var $a = $(this);
				$("#matches-type-switcher a").removeClass("current");
				$("#matches .match-type").hide();
				$("#"+$a.attr("rel")).fadeIn();
				$a.addClass("current");
				
				return false;
			});
			
			$("#match-info-switcher a").click(function(){
				var $a = $(this);
				$("#match-info-switcher a").removeClass("current");
				$("#matches .match-info").hide();
				$("#"+$a.attr("rel")).fadeIn();
				$a.addClass("current");
				
				//return false;
			});
			
			if ( location.hash ) {
				$("#match-info-switcher a[href='"+location.hash+"']").click();
			}
			
			var timer = null;
			
			if (1 == readCookie('refresh-online')) {
				
				timer = setInterval('location.reload(true);', 1000*60*3);
				$("#refresh-online").text("отключить обновление");
			}
			
			
			$("#refresh-online").click(function() {
				if (0 == readCookie('refresh-online')) {
					createCookie('refresh-online', 1, 1);
					$(this).text("отключить обновление");
					setInterval('location.reload(true);', 1000*60*3);
				}
				else {					
					createCookie('refresh-online', 0, 1);
					$(this).text("обновлять каждые 3 минуты");
					clearInterval(timer);
				}
				
				return false;
			});
		}
	}
}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

$(document).ready(site.global.init);