$(document).ready(function(){
	initSOsearch();
});


function sortMainpageSo(typeId){
	
	$("#main_page_types").find("li").each(function(i,li){
		li.className = "" ;
	});	
	$('#so_type_href_' + typeId).parent().addClass( "active" );
	
	
	$.ajax({
        url: '/ajax.php?'+Math.random(),
        type: 'POST',
        data: { element: 'MAINPAGE',  method: 'doSoSort', type: typeId },
        success: function(response){
       		
			var json = jQuery.parseJSON(response);
			var html = '';
			$.each(json.offers, function(index, country){
				html += '<li class="">';
                html += '    <h3> ' + index + '</h3>';
				
				
				$.each(country, function(index, offer){
					html += ' 	<div class="offer">';
	                html += '     <div class="description">';
	                html += '      <dl><dt>';
					html += '        <a href="'+ offer.url+ '">' + offer.promo_title + '</a>';
	                html += '                    </dt><dd>';
	                html += 			offer.second_str_main;
	                html += '                    </dd>';
	                html += '                </dl>';
	                html += '            </div>';
	                html += '            <div class="price">';
	                html += '                <b> ';
	                html += 		offer.price + ' ' + offer.currency +'</b><br/>';
	                html += '                   <? } ?>';
	                html += '                <span class="discount">' + offer.discount + '</span>';
					html += '            </div></div>';
				});
				  
				
				html += '    <div class="top"></div>';
         		html += '    <div class="bottom"></div>';
                html += '</li>';
				
			});
			
			
			$('#special-offers-list').html(html);
			
			$('.special-offers-list>li').hover(
				function(){
					$(this).addClass('hover');
					$(this).parent().find('li').removeClass('online-booking-hover');
					},
					function(){
					$(this).removeClass('hover');
					}
			); 
        }
    });
	
}




function initSOsearch(){
	$.ajax({
        url: '/ajax.php?'+Math.random(),
        type: 'POST',
        data: { element: 'MAINPAGE',  method: 'initSoSearch' },
        success: function(response){
       		var json = jQuery.parseJSON(response);
            var typesHTML = '';
			typesHTML += '<li><a href="#"  id="so_type_href_all" onclick="sortMainpageSo(\'all\');return false;" >Все</a></li>';
            $.each(json.types, function(index, element){
                typesHTML += '<li><a href="#"  id="so_type_href_' + element.id + '" onclick="sortMainpageSo(' + element.id + ');return false;" >' + element.name + '</a></li>';
            });
			
			$('#main_page_types').html(typesHTML);
        }
    });
}

