/*jQuery.preloadImages = function () {
    if (typeof arguments[arguments.length - 1] == 'function') {
        var callback = arguments[arguments.length - 1];
    } else {
        var callback = false;
    }
    if (typeof arguments[0] == 'object') {
        var images = arguments[0];
        var n = images.length;
    } else {
        var images = arguments;
        var n = images.length - 1;
    }
    var not_loaded = n;
    for (var i = 0; i < n; i++) {
        jQuery(new Image()).load(function() {
            if (--not_loaded < 1 && typeof callback == 'function') {
                callback();
            }
        }).attr('src', images[i]);
    }
}*/

$(document).ready(function () {
	var i = 1;
	$('#h1list>h1').each(function(index) {
		$(this).attr ('id', 'list' + i);
		i++;
	  });
	$('#h1list').prepend ('<div id="list"><table cellspacing="0" cellpadding="0" class="menu_inner" style="width: 600px; height: 32px;"><tbody><tr><td><ul></ul></td><td><ul></ul></td></tr></tbody></table></div>');
	var count = 0;
	$('#h1list>h1').each(function(index) {
		count++;
	});
	var first_col = Math.ceil (count / 2);
	var second_col = count - first_col;
	count = 0;
	$('#h1list>h1').each(function(index) {
		count++;
		if (count <= first_col)
			$('#list>table>tbody>tr>td:eq(0)>ul').append ('<li> <a href="#' + $(this).attr ('id') + '">' + $(this).text () + '</a></li>');
		else
			$('#list>table>tbody>tr>td:eq(1)>ul').append ('<li> <a href="#' + $(this).attr ('id') + '">' + $(this).text () + '</a></li>');
	});
    $('#h1list>h1').before ('<div class="line1 m0">&nbsp;</div>');
   	$("a[href*=#]").click(function() {
         var duration=500;
         var easing="swing";
         var newHash=this.hash;
         var target=$(this.hash).offset().top;
         var oldLocation=window.location.href.replace(window.location.hash, '');
         var newLocation=this;

         if(oldLocation+newHash==newLocation){
            $('html:not(:animated),body:not(:animated)')
               .animate({ scrollTop: target }, duration, easing, function() {
                  window.location.href=newLocation;
            });

            return false;

         }

   });
   
   if ( jQuery.browser.msie ){
			var radios = jQuery('input[type="radio"]');
			radios.each(function(){
				jQuery(this).parents('div:first').click(function(){
					var radioName = jQuery(this).find('input[type="radio"]:first').attr('name');
					if ( radioName ){
						var radioNameGroup = jQuery('input[name="'+radioName+'"]');
						jQuery(radioNameGroup).parents('div').removeClass('checked');
						jQuery(this).addClass('checked');
					}
				});
			});
		}
}); 

function SearchMoreLoad(li){
	var moreArray = $('.s_more_block');
		if($('#s_m_'+li).attr('class') == 's_more') {
			$('#s_m_'+li).fadeIn();
			$('#s_m_'+li+' a').text('Свернуть параметры');
			$('#s_m_'+li).removeClass('s_more');
			$('#s_m_'+li).addClass('s_moreA');
			$('#s_more_'+li).fadeIn();
		}
		else {
			$('#s_m_'+li+' a').text('Расширенный поиск');
			$('#s_m_'+li).removeClass('s_moreA');
			$('#s_m_'+li).addClass('s_more');
			$('#s_more_'+li).fadeOut();
		}
}

function gal_show(g,gal){
	$('div#popup').show();
	$('#g'+g).css("top" , 50 + "px");
	$('#g'+g).css("visibility" , "visible");
	$('#g'+g).hide();
	$('#g'+g).fadeIn();
}


function bg_yes_left(a){
	$(a).css("backgroundImage", "url(images/prev.png)");
	$(a).css("backgroundRepeat", "no-repeat");
	$(a).css("backgroundPosition", "0px 50%");
}

function bg_yes_right(a){
	$(a).css("backgroundImage", "url(images/next.png)");
	$(a).css("backgroundRepeat", "no-repeat");
	//$(a).animate({ backgroundImage: "images/top.png" });
	$(a).css("backgroundPosition", "100% 50%");
}

function bg_no(a){
	$(a).css("backgroundImage", "none");
	//$(a).animate({ backgroundImage: "images/top.png" });
}
	
function noteHide1(hide){
	$('div.note').hide();
	$(hide).css("backgroundPosition", "50% 0px");
}

function noteHide2(){
	$('div.notes').hide();
}
	
function noteHide3(note){
	$('div.notes').hide();
	$('#n'+note).hide();
}

function noteHide4(hide){
	$('div.note').hide();
	$(hide).css("backgroundPosition", "50% 0px");
}

function noteShow1(note){
	//alert($(note).parent('div').parent('td').children('div.notes').attr('class'));
	var n = $(note).children('div.note');
	$(note).css("backgroundPosition", "50% -66px");
	var nHeight = $(n).height();
	$(n).css("margin-left", "-114px");
	$(n).css("margin-top", -nHeight - 22 + "px");
	n.show();
}

function noteShow2(note,not){
	//alert($(note).parent('div').parent('td').children('div.notes').attr('class'));
	var nHeight = $('#n'+note).height();
	$('#n'+note).css("margin-left", $(not).offset().left - $('body').width()/2 + $('#n'+note).width() + "px");
	$('#n'+note).css("margin-top", $(not).offset().top - 660 + "px");
	$('#n'+note).show();
}

function noteShow4(note){
	//alert($(note).parent('div').parent('td').children('div.notes').attr('class'));
	var n = $(note).children('div.note');
	$(note).css("backgroundPosition", "50% -66px");
	var nHeight = $(n).height();
	$(n).css("margin-left", "-121px");
	$(n).css("margin-top", -nHeight + "px");
	n.show();
}

function bigShow(big){
	var b = $(big).parent('div.img').children('img.big_foto');
	var bH = $(b).height();
	var bW = $(b).width();
	$(b).css("top", -bH/4 + "px");
	$(b).css("left", -bW/4 + "px");
	b.show();
	$('div#popup').css({height : $('div#content').height()});
	$('div#popup').show();
}

function bigHide(){
	$('img.big_foto').hide();
	$('div#popup').hide();
}

var map, geoResult;

function init () {
    map = new YMaps.Map(document.getElementById("YMapsID"));
   map.setCenter(new YMaps.GeoPoint(37.617671,55.755768), 13,
   YMaps.MapType.MAP);map.addControl(new YMaps.TypeControl());
   map.addControl(new YMaps.ToolBar());
   map.addControl(new YMaps.Zoom());
   map.addControl(new YMaps.ScaleLine());
}

function showAddress (value) {
    map.removeOverlay(geoResult);
    var geocoder = new YMaps.Geocoder(value, {results: 1, boundedBy: map.getBounds()});

    YMaps.Events.observe(geocoder, geocoder.Events.Load, function () {
        if (this.length()) {
            geoResult = this.get(0);
            map.addOverlay(geoResult);
            map.setBounds(geoResult.getBounds());
        }else {
            alert("Адрес не найден")
        }
    });

   YMaps.Events.observe(geocoder, geocoder.Events.Fault,
   function (error) {alert("Произошла ошибка: " + error.message)});

}

function shedule (type)
{
	var month = $('#shedule_month').html ();
	var year = $('#shedule_year').html ();
	if (type == 'prev')
	{
		if (month == 1)
		{
			month = 12;
			year--;
		}
		else
		{
			month--;
		}
	}
	if (type == 'next')
	{
		if (month == 12)
		{
			month = 1;
			year++;
		}
		else
		{
			month++;
		}
	}
	$('#shedule').load ('/shedule_block?year=' + year + '&month=' + month);
	$('#shedule_year').html (year);
	$('#shedule_month').html (month);

	return false;
}

function change_price (id, price)
{
	if ($(id).attr ('checked'))
	{
		$('#total_price').html (parseInt ($('#total_price').html ()) + parseInt (price));
	}
	else
	{
		$('#total_price').html (parseInt ($('#total_price').html ()) - parseInt (price));
	}
}

