$(document).ready(function()
{
	$dialog = $('<div id="contacts"></div>').dialog({
		autoOpen: false,
		height: 590,
		width: 780,
		modal: true,
		resizable: false,
		show: "blind",
		hide: "explode"
	});
	$("div.menu_titles").click(function() {
		if ($(this).attr("id"))
		{
			var title = '';
			var id = $(this).attr("id");
			var html = jQuery('<div>', {
			    id: 'inwindow'
			});
			switch(id) 
			{
				case 'profile':
					title = "ПРОФИЛЬ (в разработке)";
					$.post("index.php", {mode: "general", submode: "ajax", action: "profile"}, function(res){
						html.html(res);
					});
					html.css({height: "425px", width: "540px"});
					$dialog.dialog({height: 500, width: 600});
					break;
				case 'about':
					title = "О ГРУППЕ";
					$.post("index.php", {mode: "general", submode: "ajax", action: "about"}, function(res){
						html.html(res);
					});
					html.css({height: "520px"});
					$dialog.dialog({height: 590, width: 780});
					break;
				case 'contacts':
					title = "КОНТАКТЫ";
					$.post("index.php", {mode: "general", submode: "ajax", action: "contacts"}, function(res){
						html.html(res);
					});
					html.css({height: "520px"});
					$dialog.dialog({height: 590, width: 780});
					break;
				case 'archive':
					title = "АРХИВ КОНЦЕРТОВ";
					$.post("index.php", {mode: "general", submode: "ajax", action: "archive"}, function(res){
						html.html(res);
					});
					html.css({height: "520px"});
					$dialog.dialog({height: 590, width: 780});
					break;
			}
			$dialog.html(html).dialog({title: title}).dialog("open");
		}
	});
	
//	return;
	$('table.listing > tr, table.listing > tbody > tr').filter(function(i){
		if (i % 2 == 0)
			return true;
		return false;
	}).addClass('bg');
	$('table.listing > tr, table.listing > tbody > tr').hover(function(e){
		$(this).addClass('hover');
	}, function(e){
		$(this).removeClass('hover');
	});
	$('input[autofocus="true"], textarea[autofocus="true"], select[autofocus="true"]').eq(0).focus();
	$('.show_options input.autoupdate[type="checkbox"]').each(function(){
		var el = $(this);
		var id = el.attr('id');
		if ($.cookie(id))
			el.attr('checked', 'checked');
		else
			el.removeAttr('checked');
		el.change(function(e){
			if ($(this).attr('checked'))
				$.cookie(id, 1, {expires: 7});
			else
				$.cookie(id, null);
			refresh_page();
		});
	});
	$('.show_options select.autoupdate').each(function(){
		var el = $(this);
		var id = el.attr('id');
		if ($.cookie(id))
			el.find('option[value="'+$.cookie(id)+'"]').attr('selected', 'selected');
		el.change(function(e){
			$.cookie(id, $(this).val(), {expires: 7});
			refresh_page();
		});
	});
	$('.show_options input.autoupdate[type="text"]').each(function(){
		var el = $(this);
		var id = el.attr('id');
		if ($.cookie(id))
			el.val($.cookie(id));
		el.keydown(function(e){
			if ((e.keyCode || e.which) != 13)
				return;
			$.cookie(id, $(this).val(), {expires: 7});
			refresh_page();
		});
	});
//	$('img.vkontakte').bind('mouseover', main.vk_auth_show);
//	$('img.vkontakte').bind('mouseout', main.vk_auth_hide);
//	$('div#vk_auth').bind('mouseover', main.vk_show);
//	$('div#vk_auth').bind('mouseout', main.vk_hide);
});


function go(href)
{
	location = href;
}

function refresh_page()
{
	window.location.reload(true);
}

function stop_page()
{
	if ($.browser.msie) 
		document.execCommand("Stop"); 
	else 
		window.stop();
}

function ajax_href(el, container, callback)
{
	var href = $(el).attr('href');
	if (!href)
		return false;
	href += '&ajax=';
	var cb = null;
	if (callback instanceof Function)
		cb = function(data, result){
			callback(el, data, result);
		};
	if (container)
		$(container).load(href, cb);
	else
		$.get(href, cb);
	return false;
}

function bool_invert(el)
{
	return ajax_href(el, '', _bool_invert);
}

function _bool_invert(el, data, result)
{
	var el = $(el);
	var text = el.text();
	text = text == 'yes' ? 'no' : 'yes';
	el.text(text);
	return false;
}

function make_clone(el)
{
	return ajax_href(el, '', _make_clone);
}

function _make_clone(el, data, result)
{
	if (!data)
		return false;
	el = $(el);
	el.unbind('click');
	var new_href = el.attr('new_href');
	new_href = new_href.replace('%new_id%', data);
	el.attr('href', new_href).removeAttr('new_href');
	el.find('img').eq(0).attr('src', '/i/admin/ok.png');
	return false;
}

function drop_cache(el)
{
	return ajax_href(el, '', _drop_cache);
}

function _drop_cache(el, data, result)
{
	alert(result);
	return false;
}

function coef()
{
	return coef;
}
coef.jFORM = null;
coef.FORM = null;

coef.show_panel = function(data)
{
	coef.jFORM = $('#sd_container form.coef');
	coef.FORM = coef.jFORM.get(0);
	coef.restore_data();
	if (typeof data == 'object')
		for (name in data)
		{
			if (name == 'autofocus')
			{
				$('#coef_' + data[name]).focus();
				continue;
			}
			var value = '';
			if (data[name].substr(0, 1) == '#')
				value = $(data[name]).val();
			else
				value = data[name];
			$('#coef_' + name).val(value);
		}
	coef.evaluate();
	return false;
};

coef.evaluate = function()
{
	var sum = 1;
	sum *= parseInt(eval(coef.FORM.minutes.value));
	sum *= parseInt(coef.FORM.unit_count.value);
	sum *= parseFloat(coef.FORM.building_speed.value);
	sum *= parseInt(coef.FORM.unit_speed.value);
	sum *= parseFloat(coef.FORM.user_speed.value);
	sum /= 60;
	sum = Math.floor(sum);
	coef.show_result(sum);
	coef.save_data();
	return false;
};

coef.restore_data = function()
{
	var els = coef.FORM.elements;
	for (i = 0; i < els.length; ++i)
	{
		var el = $(els[i]);
		var name = el.attr('name');
		if (!name) continue;
		var value = $.cookie('coef_field_'+name);
		if (isNaN(value) || value == '')
			continue;
		el.val(value);
	}
};

coef.save_data = function()
{
	var els = coef.FORM.elements;
	for (i = 0; i < els.length; ++i)
	{
		var el = $(els[i]);
		var name = el.attr('name');
		if (!name)
			continue;
		var value = el.val();
		$.cookie('coef_field_'+name, value, {expires: 7});
	}
};

coef.show_result = function(result)
{
	coef.jFORM.find('.result').find('span').text(result);
};

function main()
{
	return main;
};

main.vk_auth_show = function(event)
{
	var auth = $('div#vk_auth');
	auth.css("top", 20);
	auth.css("left", event.pageX - 100);
	auth.css("display", "block");
};

main.vk_auth_hide = function(event)
{
	var auth = $('div#vk_auth');
	auth.css("top", 20);
	auth.css("left", event.pageX - 100);
	auth.css("display", "none");
};

main.vk_show = function(event)
{
	var auth = $('div#vk_auth');
	auth.css("display", "block");
};

main.vk_hide = function(event)
{
	var auth = $('div#vk_auth');
	auth.css("display", "none");
};


