$(function () 
{
    var show_tabs = function (content) 
    {
        $("div.panel").hide();
        $(content).show('fast');
        $("#detail_tabs a.current").removeClass('current');
        if (content.selector == '#gmap' && AMD.map.blockInit !== true) {
            setTimeout(function ()
            {
                AMD.map.init()
            }, 300);
            AMD.map.blockInit = true;
        }
    }
    $("input.zip").focus();
    $("#frm-find-shop").submit(function ()
    {
        if ($("#input-zip").val() == '') {
            alert("Please enter location");
            $("#input-zip").focus();
            return false;
        }
        else if ($('#input-make').val() == '') {
            alert("Please select a make");
            $("#input-make").focus();
            return false;
        }
        else 
        {
            var url_get = site_url + '/service_shop/check_valid_zipcode/';
            var retval = false;
            $.ajax(
            {
                url : url_get, type : 'get', data : 'zipcode=' + $("#input-zip").val(), dataType : 'json', 
                async : false,
                success : function (data) 
                {
                    if (data) 
                    {
                        if (data.response) {
                            retval = true;
                        }
                        else {
                            alert("Sorry. We couldn't find your address. Please enter ZIP code.");
                        }
                    }
                }
            });
            return retval;
        }
    });
    var catsToggle = function () 
    {
        $(this).toggleClass('hide-cats');
        $('#fsform-services').slideToggle();
    };
    $('#cats-list').click(catsToggle);
    $('#cats-list-lb').click(catsToggle);
    $("#request-appointment").click(function ()
    {
        var shopname = escape($("#shopname").text());
        var bid = $("#remoteid").val();
        $.ubox(site_url + "/service_shop/set_appointment?sname=" + shopname + "&bid=" + bid);
    });
    $("a.btn-shop-feedback").click(function ()
    {
        $("#shop-feedback").ubox();
        $.render_captcha();
    });
    $('#sf-form-description').live('keyup', function (e)
    {
        $.char_limit_generic('#sf-form-description', '#desc-limit', 250, 'reached limit.');
    });
    $('p.amd-verify').hover(function () 
    {
        var p = $(this);
        var offset = p.offset();
        var loffset = offset.left - 150;
        var toffset = offset.top + 12;
        $('.review-hover').css({
            position : 'absolute', top : toffset, left : loffset, display : 'block', zIndex : 1003
        }).appendTo($("body"));
    },
    function () 
    {
        $('.review-hover').css('display', 'none');
    } );
    $('.review-hover').hover(function ()
    {
        $('.review-hover').css('display', 'block');
    },
    function ()
    {
        $(this).css('display', 'none') 
    });
    $('#sort_by').change(function () 
    {
        $('#frm-find-shop').submit();
    });
    $('.services-list dd input').click(function () 
    {
        if ($("#input-zip").val() == '') {
            alert("Please enter location");
            $("#input-zip").focus();
            return false;
        }
        else if ($('#input-make').val() == '') {
            alert("Please select a make");
            $("#input-make").focus();
            return false;
        }
        $('.services-list').block(
        {
            message : '<img src="/images/loading-small.gif" alt="Loading" />', css : {
                border : '1px solid #90C6E7', background : '#fff', padding : '1px' 
            }
        });
        $('#frm-find-shop').submit();
    }); 
    $('.services-list dd a').click(function () 
    {
        if ($("#input-zip").val() == '') {
            alert("Please enter location");
            $("#input-zip").focus();
            return false;
        }
        else if ($('#input-make').val() == '') {
            alert("Please select a make");
            $("#input-make").focus();
            return false;
        }
        var ix = $('.services-list dd a').index($(this));
        var eCheck = $('.services-list dd input:eq(' + ix + ')');
        if (eCheck.is(':checked')) {
            eCheck.removeAttr('checked');
        }
        else {
            eCheck.attr('checked', 'checked');
        }
        $('.services-list').block(
        {
            message : '<img src="/images/loading-small.gif" alt="Loading" />', css : {
                border : '1px solid #90C6E7', background : '#fff', padding : '1px' 
            }
        });
        $('#frm-find-shop').submit();
    }); 
    if ($('#scrolldiv').length > 0 )
    {
        $(window).scroll(function () 
        {
	        var pos = $(this).scrollTop();
	        var container_top_pos = $("#shop-homepage-content").position().top;
	        var container_bottom_pos = container_top_pos + $("#shop-homepage-content").height();
	        
            if (pos <= container_top_pos) {
                pos = container_top_pos;
            }
            else if (pos + 620 >= container_bottom_pos) {
                pos = container_bottom_pos - 710;
            }
            else {
                pos = pos - 60
            }
            $('#scrolldiv').animate({
                top : pos + "px" 
            },
            {
                queue : false, duration : 100
            });
        });
    }
    $("#get-directions").click(function ()
    {
        var loc = $("#user-location").val();
        if (loc == "") {
            alert("Please enter a location");
        }
        else {
            AMD.map.get_route(loc, AMD.shop_loc);
            AMD.map.blockInit = true;
            return false;
        }
        $("#user-location").focus();
    });
    if ($('.input_star_rate').length > 0) 
    {
        $('.input_star_rate').rating();
        $('.input_star_rate').click(function ()
        {
            $("#rating").val( $(this).text());
        });
    }
    $('div.user_display_rate').each(function ()
    {
        $('.user_posted_rate', $(this)).rating({
            readOnly : true
        });
    });
    $("#frm-shop-feedback").live('submit', function ()
    {
        var data = $(form).serialize();
        var email = $("#sf-form-email");
        var err = false;
        if (!validate_email($(email).val()) || $(email).val() == '')
        {
            $(email).bubbleTip({
                fade : true, message : 'Please enter a valid email address'
            });
            err = true;
        }
        else 
        {
            var emailSplit = email.val();
            var arrEmailSplit = emailSplit.split('@');
            if (arrEmailSplit[0].length > 64 || arrEmailSplit[1].length > 255) 
            {
                $(email).bubbleTip({
                    fade : true, message : 'Please enter a valid email address'
                });
                err = true;
            }
        }
        if ($("#sf-form-description").val() == '')
        {
            $("#sf-form-description").bubbleTip({
                fade : true, message : 'Please describe incorrect information.'
            });
            err = true;
        }
        if (err) {
            return false
        }
        $.post(form.action, data, function (data)
        {
            if (data.response == 'success') {
                $.ubox(site_url + "/default/success?option=5");
                return false;
            }
            else if (data.response == 'Invalid security code') 
            {
                $("#sf-feedback-captcha").bubbleTip({
                    message : 'Your text does not match the image.'
                }).val('').focus();
                $.render_captcha();
                return false;
            }
            else {
                alert(data.response);
                return false;
            }
        },
        "json");
        return false;
    });
    $('a.change-v').click(function ()
    {
        var arrow = $('#vehicle span.arrow');
        arrow.css({
            zIndex : '10000'
        });
        var URL = document.location.href;
        var x = $("#all").offset().left + 10;
        var y = $("#all").offset().top + 126;
        var html = $("#vehicle-select").html;
        $("#overlay").show();
        $("#vehicle-select").slideDown('fast').css({
            zIndex : 2000, top : y + 'px', right : x + 'px'
        });
        $(arrow).attr('id', 'open');
        $(arrow).attr('id', 'open');
    });
    $("#post-review-preview").click(function ()
    {
        var job = $("#job").val();
        var rate_per_hour = $("#hourly-rate").val();
        var total_cost = $("#total-cost").val();
        var review = $("#review-field").val();
        var error = false;
        if ($(".star-rating-on").length <= 0)
        {
            $("#rating_tmp").bubbleTip({
                message : 'Rating is required.', timeout : 5000
            }).focus();
            error = true;
        }
        if ($("#job").val() == '') 
        {
            $("#job").bubbleTip({
                message : 'Job title is required.', timeout : 5000
            }).focus();
            error = true;
        }
        if ($("#hourly-rate").val() == '') 
        {
            $("#hourly-rate").bubbleTip({
                message : 'Hourly rate is required.', timeout : 5000
            }).focus();
            error = true;
        }
        if ($("#total-cost").val() == '') 
        {
            $("#total-cost").bubbleTip({
                message : 'Total cost is required.', timeout : 5000
            }).focus();
            error = true;
        }
        if ($("#review-field").val() == '') 
        {
            $("#review-field").bubbleTip({
                message : 'Review is required.', timeout : 5000
            }).focus();
            error = true;
        }
        if (error) {
            return false;
        }
        else
        {
            $("#preview-job").html(job);
            $("#preview-rate-per-hour").html('$' + rate_per_hour);
            $("#preview-total-cost").html('$' + total_cost);
            $("#preview-review").html(review);
            $.ubox("#preview-review-c");
        }
    });
    $("#submit-reviews").click(function ()
    {
        var form = $("#commentForm");
        var post = $(form).serialize();
        sitecatalyst_eval = "var scevar=[];scevar['eVar8']='event12';var scevent = [];sclinkCode(s_account, 'Question Posted', scevent, scevar);";
        if ($("#userid").val() == '') 
        {
            redirect = 'http://' + window.location.hostname + '/service_shop/detail?branch_id=' + $('input#remoteid', 
            $('#commentForm')).val() + '&tab=reviews';
            ajax_data = post;
            ajax_url = $(form).attr('action');
            $.show_login();
            return;
        }
        $.post($(form).attr('action'), post, function (data)
        {
            alert(data.response);
            eval(sitecatalyst_eval);
            window.location.reload();
        },
        "json");
    });
    $("a.tabs").click(function ()
    {
        var container = $('#' + $(this).attr('rel'));
        show_tabs(container);
        return false;
    });
    $("#preview-cancel").click(function ()
    {
        $("#preview-review-c").hide();
        return false;
    });
    $("a.btn-review-shop").click(function ()
    {
        var container = $('#shop-details');
        if ($(container).css('display') == 'none') {
            $("div.panel").hide();
            $(container).show('fast');
        }
    });
    $("div.rating-cancel").remove();
    $('.btn-claim').click(function ()
    {
        var bid = $(this).attr('id');
        a = confirm ('Please confirm this is your shop.');
        if (a) 
        {
            if (!AMD.logged_in) 
            {
                redirect = site_url + '/user/shop/?branch_id=' + bid;
                ajax_data = 'branch_id=' + bid + '&do=ajax';
                ajax_url = '/user/associate_shop/';
                $.show_login();
            }
            else {
                return true;
            }
        }
        return false;
    });
});