//var base_url = location.protocol + '//' + location.host + location.pathname;

$.History.bind(function(state)
{
	$('#products').hide();
	$('#browse .loading').show();
	
	var id = state.substr(1);
	
	$('#products').load('shop/browse/category', { category:id }, function()
	{
		var name = $('#category_'+id+' a').text();
	
		$('#browse .loading').hide();
		$('#category_name').html(name);
		$('#products').show();

		$('.submenu li').removeClass('current');
		$('#category_'+id).addClass('current');
	});
});

$(document).ready(function()
{
	// Defaultna kategoria
	
	if($.History.getHash() == '')
	{
		var url	= $('#submenu').find('li.default').children('a').attr('href');
		
		$.History.setHash(url);
	}

	// Submenu

	$('#submenu_2').hide();

	$('#category_eat').click(function()
	{
		$('#categories').removeClass('drink').addClass('eat');
		$('#submenu_2').hide();
		$('#submenu_1').show();

		var url	= $('#submenu_1').find('li.default').children('a').attr('href');
		
		$.History.setHash(url);
	});
	
	$('#category_drink').click(function()
	{
		$('#categories').removeClass('eat').addClass('drink');
		$('#submenu_1').hide();
		$('#submenu_2').show();

		var url	= $('#submenu_2').find('li.default').children('a').attr('href');
		
		$.History.setHash(url);
	});
	
	
	// Pridavanie do kosiku
	
	$('.order').live('click', function()
	{
		if(!$(this).hasClass('configure'))
		{
			id	= $(this).attr('id');
			
			$.post('shop/cart/add', { id:id }, function(html)
			{
				$('#basket_content').html(html);
			});
		}
	});
	
	
	// Plus Minus v kosiku
	
	$('.plus').live('click', function()
	{
		id	= $(this).parent().parent().attr('id');
		qty	= $(this).prev().html();
		qty = parseInt(qty) + 1;

		$.post('shop/cart/update', { rowid:id, qty:qty }, function(html)
		{
			$('#basket_content').html(html);
		});
	});
	
	$('.minus').live('click', function()
	{
		id	= $(this).parent().parent().attr('id');
		qty	= $(this).next().html();
		qty = parseInt(qty) - 1;
		
		$.post('shop/cart/update', { rowid:id, qty:qty }, function(html)
		{
			$('#basket_content').html(html);
		});
	});


	// Scrolling basket
	
	var msie6	= $.browser == 'msie' && $.browser.version < 7;

	if(!msie6)
	{
		if($('#products_wrap').length)
		{
			$(window).scroll(function(event)
			{
				var top	= $('#browse').offset().top - 20;
				var y	= $(this).scrollTop();
				var hp	= $('#products_wrap').innerHeight();
				var hb	= $('#basket').outerHeight();
				var hw	= $(window).height();
	
				if (y >= top && hp > hb && hb < hw)	$('#basket').addClass('fixed');
				else								$('#basket').removeClass('fixed');
			});
		}
	} 

	$('.go_to_login').live('click', function()
	{
		var top	= $('#browse').offset().top - 20;

		$('#login_registration').slideDown('normal', function()
		{
			$('#login_email').focus();
		});

		$('#basket').removeClass('fixed');
		
		return false;
	});
	
	// Additives
	
	$('#add_additive').live('click', function()
	{
		var select	= $(this).prev("select");
		var value	= $(select).val();
		var text	= $(select).find(':selected').text();
		
		if(!$('#selected').find('input[value="'+value+'"]').length && value != 0)
		{
			$('#selected').append('<li class="additive"><span class="remove">x</span><input type="hidden" name="additive[]" value="'+value+'" />+ '+text+'</li>');
		}
		
		$('#add_additive_info').hide().removeClass('firstTime');
		
		return false;
	});
	
	$('.remove').live('click', function()
	{
		$(this).parent().remove();
	});
	

	// Streets

	$('input#street').placeholder();
	
	var city = $('select#city').val();
	
	autocomplete_streets(city);
	
	function autocomplete_streets(city)
	{
		$('.jsonSuggestResults').remove();
		$('input#street').addClass('loading');
		
		$.getJSON('/data/streets_with_prices/'+city, function(data)
		{
			$('input#street').removeClass('loading');
			$('input#street').jsonSuggest(data, { maxResults:5, wildCard:'', highlightMatches:false, onSelect:callback });
		});
	}
	
	function callback(item)
	{
		$('#hotline_phone').hide().html(item.phone).fadeIn('slow');
		$('ul.delivery li.day strong').hide().text(item.price_day+' €').fadeIn('slow');
		$('ul.delivery li.night strong').hide().text(item.price_night+' €').fadeIn('slow');
		$('ul.delivery li span.night_hour').text(item.night_hour);
	}
	
	$('#city_wrap .jqTransformSelectWrapper ul li a').live('click', function()
	{
		var city = $('select#city').val();

		window.location = '/city/set_by_id/'+city;

	//	$('input#street').val('').removeClass('placeholder');		
	//	autocomplete_streets(city);
	});
	
/*
	$('select#city').change(function()
	{
		var city = $('select#city').val();
		$('input#street').val('');
		
		autocomplete_streets(city);
	});
*/
});
