jQuery.fn.get_pos = function() {
	var x = 0, y = 0;
    var el = this.get(0);
	while(el) {
		x += el.offsetLeft || 0;
		y += el.offsetTop || 0;
		el = el.offsetParent;
	}
	return {x:x, y:y};
}

$(document).ready(function() {
	$('a[href^=#map]').click(function() {
        var o = $(this);
        $('a[href^=#map]').removeClass('act');
		o.addClass('act');
        $('div[id^=map]').hide();
        $(o.attr('href')).show();
        return false;
    });
	
	$('a[href^=#expand-]').click(function() {
        var obj = $(this);
        var id = obj.attr('href');
        var expand = $(id);
        if(expand.css('display').toLowerCase() == 'none')
			expand.animate({height:'show'});
			//expand.show();
        else
			expand.animate({height:'hide'});
			//expand.hide();
        return false;
    });
	
    $('a[href^=#refresh_captcha]').click(function() {captcha.refresh();});

    data = new Array();
    $('.manager_info').each(function(i, n) {
        data[i] = $(n).html();
    });
    tooltip = new tooltip(data);
    
    $('input.stxt').focus(function() {
        var val = $(this).val();
        var i = $(this);
        if (val == 'Поиск' || val == 'поиск') {
            i.val('');
        }
    });
    
    $('input.stxt').blur(function() {
        var val = $(this).val();
        var i = $(this);
        if (val == '' || val == 'поиск') {
            i.val('Поиск');
        }
    });

    if($('#msg_form_obj').length > 0) {
        $('.sub_f').addClass('disabled').attr('disabled', true);

        $('#message, #captcha').keyup(function() {
            if($('#message').val() && $('#captcha').val()) {
                $('.sub_f').removeClass('disabled').removeAttr('disabled');
            } else {
                $('.sub_f').addClass('disabled').attr('disabled', true);
            }
        });

        $('#msg_form_obj').submit(function() {
            var options = {
                dataType: 'json',
                success: function(data) {
                    $('.error, .success').fadeOut();
                    if(data.success) {
                        $('#success').fadeIn();
                        setTimeout(function(){$('#success').fadeOut();}, 4500);
                    } else {
                        $.each(data.errors, function(i, n) {
                            $('#'+i+'_error').fadeIn();
                        });
                    }
                }
            }

            $(this).ajaxSubmit(options);
            return false;
        });
    }

    if($('#contact_form_div').length > 0) {
        $('#contact_form').submit(function() {
             var options = {
                dataType: 'json',
                success: function(data) {
                    $('.error, .success').hide();
                    if(data.success) {
                        $('#form_success').fadeIn();
                        setTimeout(function(){
                            $('#form_success').fadeOut();
                            if(!$('#contact_form_div').hasClass('static')) {
                                $('#black_body').fadeIn();
                                $('#contact_form_div').fadeIn();
                            };
                        }, 4500);

                    } else {
                        $('#error').fadeIn();
                        $.each(data.errors, function(i, n) {
                            $('#'+i+'_error').show();
                        });
                        setTimeout(function(){$('#error').fadeOut();}, 2500);
                    }
                }
            }

            $(this).ajaxSubmit(options);
            return false;
        });
    }

    if($('#order_form_div').length > 0) {
        $('a[href*=order_price]').click(function() {
            $('#black_body').fadeIn();
            $('#order_form_div').fadeIn();
        });

        $('#price_order_form').submit(function() {
             var options = {
                dataType: 'json',
                success: function(data) {
                    $('.error, .success').hide();
                    if(data.success) {
                        $('#oform_success').fadeIn();
                        setTimeout(function(){
                            $('#price_order_form').clearForm();
                            $('#ocaptcha_img').attr('src', '/captcha/'+(new Number(new Date())));
                            $('#oform_success').fadeOut();
                            if(!$('#order_form_div').hasClass('static')) {
                                $('#black_body').fadeIn();
                                $('#order_form_div').fadeIn();
                            }
                        }, 4500);

                    } else {
                        $('#oerror').fadeIn();
                        $.each(data.errors, function(i, n) {
                            $('#o'+i+'_error').show();
                        });
                        setTimeout(function(){$('#oerror').fadeOut();}, 2500);
                    }
                }
            }

            $(this).ajaxSubmit(options);
            return false;
        });
    }

    $('#black_body').click(function() {
        $('div[id*=_form_div]').fadeOut();
        $('form[id*=_form]').clearForm();
        $(this).fadeOut();
    });

    $('#mail_us').click(function() {
        $('#black_body').fadeIn();
        $('#contact_form_div').fadeIn();
        return false;
    });
});

/* компонент всплывающей подсказки */
tooltip = function(info) {
    this.info = info;
    this.shown = false;
    this.obj = false;
    this.link = null;
    
    this.init();
}

tooltip.prototype.init = function() {
    var container = document.createElement("div");
    var div = document.createElement("div");
    $(div).addClass('tooltip');
    this.obj = div;
    $(container).attr('id', 'tooltip_helper').append(div);
    $('body').append(container);
    
    var tooltip = this;
    
    $(document).ready(function() {
        $(this).click(function(e){
            var link = e.originalTarget ? e.originalTarget : e.srcElement;
            if (tooltip.link && link != tooltip.link) {
                tooltip.hide();
            }
        });
    });
}

tooltip.prototype.show = function(e, n) {
    var tt = this;
    var link = e.originalTarget?e.originalTarget:e.srcElement;
    this.link = link;
    var pos = $(link).get_pos();
    if(!tt.obj || tt.obj === undefined) return false;

    var jt = $(tt.obj);

    if (this.shown !== false) {
        $(this.obj).hide(function() {
            var shown_n = tt.shown;
            tt.shown = false;
            
            if (shown_n !== n) {
                jt.html(tt.info[n]);
                jt.css({
                    top: (pos.y - 62),
                    left: (pos.x + $(link).width())
                }).show(function(){
                    tt.shown = n;
                });
            }
        });
        return false;
    }
    
    
    jt.html(tt.info[n]);
    jt.css({
        top: (pos.y - 62),
        left: (pos.x + $(link).width())
    }).show(function(){
        tt.shown = n;
    });
}

tooltip.prototype.hide = function(){
    var tooltip = this;
    $(this.obj).hide(function() {
        tooltip.shown = false;
    });
}

captcha = {
    refresh: function(alias) {
        if(!alias || alias === undefined) alias = '';
        if(alias) alias = '/'+alias;
        $('#captcha_img').attr('src', alias+'/captcha/'+(new Number(new Date())));

        return false;
    }
}
