function bind_shop_actions()
{
	$("#system_buttons > div").unbind('click').click(function(){
		var button = $(this).attr('rel');
		$("#system_buttons > div").removeClass('active');
		$(this).addClass('active');
		$("#content_panel > div").removeClass('active hovered');
		$("#content_panel > #"+button).addClass('active');
	});
	
	$("#system_buttons div").unbind('mouseover').mouseover(function(){
		var button = $(this).attr('rel')+"_hover";
		
		if ($("#content_panel > #intro").hasClass('active'))
		{
			$("#content_panel > div").removeClass('active');
			$("#content_panel > #"+button).addClass('active');
			
			$("#content_panel > #intro").addClass('hovered');
		}
	});
	
	$("#system_buttons div").unbind('mouseout').mouseout(function(){
		var button = $(this).attr('rel')+"_hover";
		
		if ($("#content_panel > #intro").hasClass('hovered'))
		{
			$("#content_panel > div").removeClass('active');
			$("#content_panel > #intro").addClass('active').removeClass('hovered');
		}
	});

	$("#content_panel .next").unbind('click').click(function(){
		$(this).parent().submit();
	});
	
	$("#content_panel .back").unbind('click').click(function(){
		var active_panel = $('#content_panel > div.active').attr('id');
		$('#content_panel > #'+active_panel).load('/shop #'+active_panel+'>form', function(data){
			bind_shop_actions();
		});
	});
	
	$("input#idideverything").change(function(){
		if ($(this).attr('checked'))
		{
			$(".go_to_payment").addClass('active');
		} else
		{
			$(".go_to_payment").removeClass('active');
		}
	});
	
	$("#system_buttons #qiwi").click(function(){
		$("#content_panel > div").removeClass('active');
		$("#content_panel > #intro").addClass('active');

		$.openDOMWindow({  
			width:900,
			windowSourceID:'#qiwi_instructions',
			windowBGColor:'transparent',
			modal:true,
			overlayOpacity:'30'
		});
	});
	
	$("#terms_button").openDOMWindow({  
		width:500, 
		eventType:'click',
		windowSourceID:'#terms',
		windowBGColor:'transparent',
		modal:true,
		overlayOpacity:'50'
	});
	
	$(".close_dom_window").closeDOMWindow({eventType:'click'}); 
	
	$(".go_to_payment").click(function(){
		if ($("input#idideverything").attr('checked'))
		{
			$.closeDOMWindow();
			$("#content_panel > div").removeClass('active');
			$("#content_panel > #qiwi").addClass('active');

		}
	});

	$("#content_panel form").unbind('submit').submit(function(){
		if ($(this).attr('rel') == 'outer') return true;

		var form = $(this);
		var action = form.attr('action');
		var method = form.attr('method').toLowerCase();
		var selector = form.attr('rel');
		var args = form.serialize();

		if (method == '') method = 'post';
		if (action == '') action = '/shop/submit';

		if (method == 'post')
		{
			$.post(action, args, function(data){
				form.parent().html(data);
				bind_shop_actions();
			});
		}
		
		if (method == 'get')
		{
			if (selector != 'undefined') action = action+' '+selector;

			form.parent().load(action+' '+selector, function(data){
				bind_shop_actions();
			});
		}
		return false;
	});
}

function show_splash()
{
	$.openDOMWindow({  
		width:500,
		windowSourceID:'#reserve_text',
		windowBGColor:'transparent',
		modal:true,
		overlayOpacity:'60'
	});
	
	$('#DOMWindow .reserve_text form').submit(function(){
		var args = $('#DOMWindow .reserve_text form').serializeArray();
		$('#DOMWindow .reserve_text .messages').hide();
		$('#DOMWindow .reserve_text form').hide();
		$('#DOMWindow .submit_loader').show();

		$.post('/shop/reserve', args, function(response){
			if (response.substr(0, 4) == 'err:')
			{
				$('#DOMWindow .error .message').html(response.substr(4));
				$('#DOMWindow .error').show();
			} else
			{
				$('#DOMWindow .ok .message').html(response.substr(4));
				$('#DOMWindow .ok').show();
			}
			$('#DOMWindow .submit_loader').hide();
		});
		return false;
	});
	
	$('#DOMWindow .try_again').click(function(){
		$('#DOMWindow .reserve_text .messages').show();
		$('#DOMWindow .reserve_text form').show();
		$('#DOMWindow .error').hide();
		$('#DOMWindow .ok').hide();
	});
}

$(document).ready(function(){
	if ($("#shop #reserve_text").length > 0)
	{
		show_splash();
	} else
	{
		bind_shop_actions();
	}
});
