//[d] включаю на странице "Где купить"
//[d] два параметра категория (город или партнер) и id точек
function route(category,points) {
	//[d] включаю таблицу (по умолчанию города)
	if (category==undefined) {
		if (window.location.href.split("=")[1]!=undefined) {
			table=(window.location.href.split("=")[1]).split("&")[0]			
		}else {
			table='city';			
		}		
	} else {
		table=category;		
	}
	
	//[d] нажали "назад"
	if (points!=-1) {
		//[d] если пустая переменная смотрим, может дали прямой ссылкой
		if (points==undefined)
			points=parseInt(window.location.href.split("=")[2])>0?parseInt(window.location.href.split("=")[2]):undefined;		
	} else {
		points=undefined;
	}

	//[d] если точек нету выводим города или партнеры
	if (points==undefined) {
		$.post('/buy/category',{table:table},function(data){
			$('#method').html(data);
			$('#title_buy').hide();
			$('#method').show();
			$('#points').hide();
		});	 
	} else {
		$.post('/buy/points',{table:table,id:points},function(data){
			$('#points').html(data);
			if ((window.location.href.split("=")[1]).split("&")[0]=='city') {
				$.post('/buy/name',{table:table,id:points},function(name){			
					title="Список точек продаж закуски хумус в городе "+name+":";	
					$('#title_buy').html("<h4>"+title+"</h4><br/>");			
					$('#title_buy').show();
					$('#method').hide();
					$('#points').show();	
				});
			} else {
				$.post('/buy/name',{table:table,id:points},function(name){	
					title="Список точек продаж закуски хумус у партнера &#171;"+name+"&#187;:";		
					$('#title_buy').html("<h4>"+title+"</h4><br/>");			
					$('#title_buy').show();
					$('#method').hide();
					$('#points').show();
				});
			}	
		});	 
	}
	
}

$(document).ready(function(){
	$(document).ready(function(){
		$('span.showadrs').live('click',function(){
			var t = $(this);
			t.next('div').slideToggle('fast');
			if (t.hasClass('opened')) {
				t.removeClass('opened').text('посмотреть');
			}
			else {
				t.addClass('opened').text('скрыть');						
			}
		});
	});
	//[d] если прямой link на страницу
	if (window.location.href.split("=")[1]!=undefined) {
		//[d] если в прямой ссылке есть категория
		if ((window.location.href.split("=")[1]).split("&")[0]!=undefined) {
			//[d] если эта категория другая, чем по умолчанию сделать swap
			switch ((window.location.href.split("=")[1]).split("&")[0]) {
				case 'city':
					$('#sub_menu li a').eq(0).attr("class","sel");
					$('#sub_menu li a').eq(1).attr("class","");	
					$('#sub_menu li a').eq(2).attr("class","");	
				break;	
				case 'partners':
					$('#sub_menu li a').eq(0).attr("class","");
					$('#sub_menu li a').eq(1).attr("class","sel");	
					$('#sub_menu li a').eq(2).attr("class","");	
					break;
				case 'market':
					$('#sub_menu li a').eq(0).attr("class","");
					$('#sub_menu li a').eq(1).attr("class","");	
					$('#sub_menu li a').eq(2).attr("class","sel");	
					break;					
			}
		}
	}
	
	// [d] на странице "Где купить?" переключение под меню
	$('#second').click(function() {
		//[d] обновляю роутер
		route($(this).children('a').attr("rel"),-1);
		//[d] под меню редактирую
		$('#sub_menu li a').eq(0).attr("class","");
		$('#sub_menu li a').eq(1).attr("class","sel");	
		$('#sub_menu li a').eq(2).attr("class","");
	});	

	// [d] на странице "Где купить?" переключение под меню
	$('#first').click(function() {
		//[d] обновляю роутер
		route($(this).children('a').attr("rel"),-1);
		//[d] под меню редактирую
		$('#sub_menu li a').eq(0).attr("class","sel");
		$('#sub_menu li a').eq(1).attr("class","");	
		$('#sub_menu li a').eq(2).attr("class","");
	});

	// [d] на странице "Где купить?" переключение под меню
	$('#third').click(function() {
		//[d] под меню редактирую
		$('#sub_menu li a').eq(0).attr("class","");
		$('#sub_menu li a').eq(1).attr("class","");	
		$('#sub_menu li a').eq(2).attr("class","sel");
		//[d] обновляю роутер
		window.location='#method=market';
		window.location.reload(window.location='#method=market');			
	});
		
	//[d] если я нажимаю на выбранную ссылку в категории
	$('#category_list li a').live("click", function(){
		category=$(this).attr('rel').split("/")[0];
		points=$(this).attr('rel').split("/")[1];
		route(category,points);
	});
	
	//[d] если я нажимаю на выбранную ссылку в категории
	$('#buy_prev').live("click", function(){
		category=$(this).attr('rel');
		route(category,-1);
	});
	
	//[d] select на главной
	$('#select_city').change(function() {
		window.location = "/buy.html#method=city&id="+$('#select_city').val()+"";
	});
	

	//[d] Дистрибьюция
	$('#list_partners li a').click(function() {
		$.post('/partner/id',{id:$(this).attr('rel')},function(data){
			var html='<p><a href="/partner.html">Назад</a></p><br/>';
			if (data.name!='') {
				html +='<b>Название:</b> '+data.name+'<br/><br/>';
			}
			if (data.additional!='') {
				html +='<b>Дополнительная информация:</b> '+data.additional+'<br/><br/>';
			}	
			html +='<a href="/buy.html#method=partners&id='+data.id+'">Посмотреть список магазинов</a>';
			
			$('#list').hide();
			$('#description').html(html);
			$('#description').show();
		}, "json");	  
	}); 
	
	$('.next').click(function() {
		$('#list').show();
		$('#description').hide();
	}); 
	
    $('#foot-curra #box').hide();
    
	$('#foot-curra').hover(
        function(){
            $('#foot-curra #box').stop(true, true).slideDown(400);
            return false;
        },
        function(){
            $('#foot-curra #box').stop(true, true).slideUp(400);
            return false;
        }
    )

    $('section.inner tbody tr:odd td').css('background-color', '#2f902f');

   jQuery('#mycarousel').jcarousel({
      // Configuration goes here
   });

   jQuery('#videocarousel').jcarousel({
      scroll:1
   });

   //[d] забираю кол-во видео
   var cnt_video=$('#videogallery ul li').size();
   var now_video=0;
   //[d] если видео есть, то вывожу текст к нему
   if (cnt_video>0) {
	   $('#video_text').html($('#videogallery ul li').eq(now_video).attr("rel")); 
   }
   //[d] меняю текст видео
   $('.jcarousel-prev').click(function() {
	   now_video--; 
	   $('#video_text').html($('#videogallery ul li').eq(now_video).attr("rel"));    
   }); 
   $('.jcarousel-next').click(function() {
	   now_video++;
	   $('#video_text').html($('#videogallery ul li').eq(now_video).attr("rel")); 
   });
   
   // lightbox
	$('#b-gallery a').lightBox({
            txtImage: 'изображение',
            txtOf: 'из'
        }); 

})

