jQuery.noConflict();

// Создадим свое пространство имен для глобальных переменных,
// дабы не захламлять все пространство.
var space = new Object();

jQuery(document).ready( function() {

    if (jQuery("#instructors").length) {
        jQuery("#instructors")
            .css('display', 'block')
            .slideshow({
                width:     230,
                height:    100,
                effect:    "fade",
                playframe: false,
                panel:     false,
                title:     true,
                play:      true
            });
    }

    jQuery('input.search_form_text')
        .focus( function() {
            var text = new Array(
                jQuery.trim(jQuery(this).val()),
                jQuery.trim(jQuery(this).attr('rel'))
            );
            if (text[0] == '' || text[0] == text[1]) {
                jQuery(this).val('');
            }
        })
        .blur( function() {
            var text = new Array(
                jQuery.trim(jQuery(this).val()),
                jQuery.trim(jQuery(this).attr('rel'))
            );
            if (text[0] == '' || text[0] == text[1]) {
                jQuery(this).val(text[1]);
            }
        });
    
    jQuery('#link_search_types, #search_form span.link_arrow').click( function() {
        jQuery('#search_form_types').toggle();
        return false;
    });
    
    jQuery('#search_form_types_list a').click( function() {
        jQuery('#search_form_type').val(jQuery(this).attr('rel'));
        jQuery('#link_search_types').text(jQuery('#link_search_types').attr('rel')+' '+jQuery(this).text());
        jQuery('#search_form_types').toggle();
        return false;
    });
    
    jQuery('#search_types_btn_close').click( function() {
        jQuery('#search_form_types').toggle();
        return false;
    });
    
    jQuery('#cities input').hide();
    jQuery('#cities select')
    	.attr('disabled', false)
    	.change( function() {
			window.location.href = jQuery(this).val();
		    //changeCity();
		    return false;
		});

    jQuery('#line select[name=city]').change( function() {
        searchChangePlaces();
        return false;
    });

    jQuery('#line select[name=place]').change( function() {
        searchChangePlace();
        return false;
    });

    jQuery(window).scroll( function() {
        var w = jQuery(window);
        //var top = (w.height()-jQuery('div.popup_window').height())/2+w.scrollTop();
        //top = top - (top/2);
        
        jQuery('div.popup_window').animate(
            {top: (w.height()-jQuery('div.popup_window').height())/2+w.scrollTop()+'px'},
            //{top: top+'px'},
            {duration: 500, queue: false}
        );
    });

    jQuery('a.popup_window_close_btn').click( function() {
        jQuery(this).parent().parent().fadeOut();
        blankLayoutHide();
        jQuery('div.popup_window_messages').fadeOut();
        return false;
    });
    
    jQuery('a.tooltip').mousemove( function(e) {
        jQuery(this).after('<div class="tooltip">'+jQuery(this).attr('rel')+'</div>');
        jQuery('div.tooltip').css({
            top: e.pageY+20,
            left: e.pageX+20
        });
    });
    jQuery('a.tooltip').mouseout( function() {
        jQuery('div.tooltip').remove();
    });
});

function changeCity()
{
    jQuery.post(
        jQuery('#cities').attr('action'),
        {
            cityId: jQuery('#cities option:selected').val()
        },
        function(result) {
            switch (result) {
                case 1:
                    window.location.reload();
                    break
            }
        },
        'json'
    );
}

function blankLayoutShow()
{
    jQuery('#blank_layout').remove();
    jQuery('body').append('<div id="blank_layout"></div>');
    jQuery('#blank_layout')
        .css({
            display:         'none',
            position:        'absolute',
            zIndex:          '998',
            top:             '0px',
            left:            '0px',
            width:           '100%',
            height:          jQuery(document).height(),
            backgroundColor: '#ccc',
            opacity:         0.4
        })
        .fadeIn();
}

function blankLayoutHide()
{
    jQuery('#blank_layout').fadeOut();
}

function popupWindowShow(id)
{
    var w = jQuery(window);
    //var top = (w.height()-jQuery('#'+id).height())/2+w.scrollTop();
    //top = top - (top/2);

    blankLayoutShow();
    jQuery('#'+id)
        .css({
            top:  (w.height()-jQuery('#'+id).height())/2+w.scrollTop()+'px',
            //top:  top+'px',
            left: (w.width()-jQuery('#'+id).width())/2+w.scrollLeft()+'px'
        })
        .fadeIn();
}

function captchaRefresh()
{
    var date = new Date();
    var captcha = jQuery('img.captcha');
    captcha.attr('src', captcha.attr('src')+'?_='+date.getTime());
}

function searchChangePlaces()
{
    //jQuery('#line select[name=place]').hide().after('<img src="/assets/img/loading/loading19tr.gif" alt="" />');

    /*
    jQuery.post(
        jQuery('#line select[name=place]').attr('rel'),
        {
            cityId: jQuery('#line select[name=city] option:selected').val()
        },
        function(response) {
            switch (response) {
                case 0:
                    alert('Ошибка при получении мест выбранного города');
                    break;
                default:
                    jQuery('#line select[name=place]')
                        .empty()
                        .append('<option value="0">Все Места</option>');

                    jQuery.each(response, function(i, item) {
                        jQuery('#line select[name=place]').append('<option value="'+item.id+'">'+item.name+'</option>');
                    })
            }

            //jQuery('#line select[name=place]').show().next('img').remove();
            searchChangePlace();
        },
        'json'
    );
    */
    
    searchChangePlace();
}

function searchChangePlace()
{
    //jQuery('#line select[name=undeground]').hide().after('<img src="/assets/img/loading/loading19tr.gif" alt="" />');

    if (jQuery('#line select[name=city] option:selected').attr('rel') == 0) {
        /*
        jQuery.post(
            jQuery('#line select[name=undeground]').attr('rel'),
            {
                cityId:  jQuery('#line select[name=city] option:selected').val(),
                placeId: jQuery('#line select[name=place] option:selected').val()
            },
            function(response) {
                switch (response) {
                    case 0:
                        alert('Ошибка при получении станций метро для выбранных мест');
                        break;
                    default:
                        jQuery('#line select[name=undeground]')
                            .empty()
                            .append('<option value="0">Все станции метро</option>');
                        
                        if (response.length > 0) {
                            jQuery.each(response, function(i, item) {
                                jQuery('#line select[name=undeground]').append('<option value="'+item.id+'">'+item.name+'</option>');
                            });
                            jQuery('#line select[name=undeground]').show();
                        } else {
                            jQuery('#line select[name=undeground]').hide();
                        }
                }

                //jQuery('#line select[name=undeground]').show().next('img').remove();
            },
            'json'
        );
        */
        jQuery('#line select[name=undeground]').show();
    } else {
        jQuery('#line select[name=undeground]').hide();
    }
    
}

function print_r(obj) {
    if(obj.constructor == Array || obj.constructor == Object) {
        document.write("<ul>");
        for(var key in obj) {
            if(obj[key].constructor == Array || obj[key].constructor == Object) {
                document.write("<li>["+key+"] => "+typeof(obj)+"</li>");
                document.write("<ul>");
                print_r(obj[key]);
                document.write("</ul>");
            } else {
                document.write("<li>["+key+"] => "+obj[key]+"</li>");
            }
        }
        document.write("</ul>");
    } else {
        document.write(obj);
    }
}

