﻿$(document).ready(function () {
    // display the Industry Tips popup
    if ($("#industry-tips2").length > 0) {
        $("#industry-tips2").each(function () {
            $("#industry-tips2").bind("click", function (e) {
                $.linkPOS = $("#industry-tips2");
                $.pos = $.linkPOS.offset();
                //alert($.pos.top)
                if ($("#industry-tips-pop").hasClass("show")) {
                    //alert('in')
                    $("#industry-tips-pop").removeClass("show");
                }
                else { // adjust the window position and display
                    //alert('not');
                    $("#industry-tips-pop").css("top", ($.pos.top - 200)).css("left", $.pos.left - 0);
                    $("#industry-tips-pop").addClass("show").bind("click", function () {
                        $("#industry-tips-pop").removeClass("show");
                    });
                }
                e.stop;
                return false;
            });
        });
    }

    //  display the Under Review Alert for this Business DIV window
    if ($("#underreview-link").length > 0) {
        $("#underreview-link").each(function () {
            $("#underreview-link").bind("click", function (e) {
                $.linkPOS = $("#underreview-link");
                $.pos = $.linkPOS.offset();
                //alert($.pos.top)
                pageTracker._trackEvent('Pop-up', 'Under Review', $('h1.business-title').text());
                if ($("#underreview-pop").hasClass("show")) {
                    $("#underreview-pop").removeClass("show");
                }
                else {
                    $("#underreview-pop").css("top", ($.pos.top + 35)).css("left", $.pos.left);
                    $("#underreview-pop").addClass("show").bind("click", function () {
                        $("#underreview-pop").removeClass("show");
                    });
                }
                e.stop;
                return false;
            });
        });
    }

    //PopUps
    $('.blueReviews').popupWindow({
        centerBrowser: 1, width: 675, height: 480, scrollbars: 1, resizable: 1
    });
    $('.blueQuote').popupWindow({
        centerBrowser: 1, width: 675, height: 480, scrollbars: 1, resizable: 1
    });
    $('.couponLink').popupWindow({
        centerBrowser: 1, width: 640, height: 420, scrollbars: 1, resizable: 1
    });
    $('.suggestAB').popupWindow({
        centerBrowser: 1, width: 640, height: 420, scrollbars: 1, resizable: 1
    });

    //Dialogs
    //  display the COMPLAINT TRENDS for this Business
    var cmpltrend = 0;
    if ($("#complaint-trends-link").length > 0) {
        $("#complaint-trends-link").each(function () {
            $("#complaint-trends-link").bind("click", function (e) {
                $.linkPOS = $("#complaint-trends-link");
                $.pos = $.linkPOS.offset();
                //alert($.pos.top)
                if ($("#complaint-trends").hasClass("show")) {
                    $("#complaint-trends").removeClass("show");
                }
                else {
                    $("#complaint-trends .close-win a").bind("click", function (e) {
                        $("#complaint-trends").removeClass("show");
                        e.preventDefault();
                        e.stopPropagation();
                        return false;
                    });
                    pageTracker._trackEvent('Business Review', 'Complaint Trends', $('h1.business-title').text());
                    if (cmpltrend == 0) {
                        //alert($(this).attr("href"));
                        $.get($(this).attr("href"), function (data) {
                            $("#complaint-trends-chart").html(data);
                            $("#complaint-trends").css("top", ($(window).height() - $("#complaint-trends").height()) / 2 + $(window).scrollTop() + "px").css("left", ($(window).width() - $("#complaint-trends").width()) / 2 + $(window).scrollLeft() + "px");
                            $("#complaint-trends").addClass("show");
                            cmpltrend = 1;
                            return false;
                        });
                    } else {
                        $("#complaint-trends").css("top", ($(window).height() - $("#complaint-trends").height()) / 2 + $(window).scrollTop() + "px").css("left", ($(window).width() - $("#complaint-trends").width()) / 2 + $(window).scrollLeft() + "px");
                        $("#complaint-trends").addClass("show");
                    }
                }
                e.stop;
                return false;
            });
        });
    }
    var revtrend = 0;
    if ($(".review-trends-link").length > 0) {

        $(".review-trends-link").bind("click", function (e) {
            if ($("#review-trends").hasClass("show")) {
                $("#review-trends").removeClass("show");
            }
            else {
                $("#review-trends .close-win a").bind("click", function (e) {
                    $("#review-trends").removeClass("show");
                    e.preventDefault();
                    e.stopPropagation();
                    return false;
                });
                //Show Chart
                pageTracker._trackEvent('Business Review', 'Customer Review Trends', $('h1.business-title').text());
                if (revtrend == 0) {
                    //alert($(this).attr("href"));
                    $.get($(this).attr("href"), function (data) {
                        $("#review-trends-chart").html(data);
                        revtrend = 1;
                        $("#review-trends").css("top", ($(window).height() - $("#review-trends").height()) / 2 + $(window).scrollTop() + "px").css("left", ($(window).width() - $("#review-trends").width()) / 2 + $(window).scrollLeft() + "px");
                        $("#review-trends").addClass("show");
                        return false;
                    });
                } else {
                    $("#review-trends").css("top", ($(window).height() - $("#review-trends").height()) / 2 + $(window).scrollTop() + "px").css("left", ($(window).width() - $("#review-trends").width()) / 2 + $(window).scrollLeft() + "px");
                    $("#review-trends").addClass("show");
                }
            }
            e.stop;
            return false;
        });

    }
    // photos/videos tab
    $("#left .media-module .video-thumbs li").hover(function () {
        $(this).css({ 'z-index': '10' }); /*Add a higher z-index value so this image stays on top*/
        $(this).find('div.video-thumb-wrapper').stop() /* Add class of "hover", then stop animation queue buildup*/
            .animate({
                //top: '-16px', /* The next 4 lines will vertically align this image */
                //left: '-18px',
                //width: '80px', /* Set new width */
                //height: '80px', /* Set new height */
                padding: '0px'
            }, 200); /* this value of "200" is the speed of how fast/slow this hover animates */

    }, function () {
        $(this).css({ 'z-index': '0' }); /* Set z-index back to 0 */
        $(this).find('div.video-thumb-wrapper').stop()  /* Remove the "hover" class , then stop animation queue buildup*/
            .animate({
                top: '0',
                left: '0'
                //,width: '63px', /* Set width back to default */
                //height: '63px' /* Set height back to default */
            }, 300);
    }).click(function (evt) {
        $("#left .media-module .video-thumbs li.selected").removeClass("selected");
        $(this).addClass("selected");
        var relArr, embed, caption, count, vendor;
        relArr = $(this).find('a').attr('rel').split('|');
        vendor = relArr[0];
        embed = relArr[1];
        caption = relArr[2];
        count = relArr[3];
        document.getElementById('bbbplayer').setQueryStringParameters({ embedCode: embed, autoplay: 1 });
        $("#currentVideo-caption").text(caption);
        $("#video-count").text(count);
        //document.getElementById('bbbplayer').playMovie();
        evt.preventDefault();
    });

    $("#index-coupons").bind("click", function (e) {
        if ($("#disc-coupons-pop").hasClass("show")) {
            //alert('in')
            $("#disc-coupons-pop").removeClass("show");
        }
        else { // adjust the window position and display
            $("#disc-coupons-pop").css("top", ($(window).height() - $("#disc-coupons-pop").height()) / 2 + $(window).scrollTop() + "px").css("left", ($(window).width() - $("#disc-coupons-pop").width()) / 2 + $(window).scrollLeft() + "px");
            $("#disc-coupons-pop").addClass("show").bind("click", function () {
            });
            pageTracker._trackEvent('Business Review', 'Discount Coupons: Overview', $('h1.business-title').text());
        }
        e.stop;
        return false;
    });
    $("#disc-coupons").bind("click", function (e) {
        pageTracker._trackEvent('Business Review', 'Discount Coupons: Right Rail', $('h1.business-title').text());
    });

});

function getBlueDirections() {
    var map, geocoder, address, address2;

    address = $("#toAddress")[0].value + ", " + $("#toCity")[0].value + ", " + $("#toState")[0].value + " " + $("#toZip")[0].value;
    if (($("#toLng")[0].value != "") && ($("#toLat")[0].value != "")) {
        address = $("#toLat")[0].value + "," + $("#toLng")[0].value
    }
    address2 = $("#fromAddress")[0].value + ", " + $("#fromCity")[0].value + ", " + $("#fromState")[0].value + " " + $("#fromZip")[0].value;

    if ((address.replace(/,/g, "").replace(/ /g, "") == "") || (address2.replace(/,/g, "").replace(/ /g, "") == "")) {
        alert('Please enter a starting point & destination in order to retrieve directions.');
        return false
    } else {
        /*
        map = new GMap2(document.getElementById("gmaps-container"));
        map.setUIToDefault();
        var directionsPanel = document.getElementById("directionsroute");
        var directions = new GDirections(map, directionsPanel);
        directions.load("from: " + address2 + " to: " + address);
        */
        var request = {
            origin: address2,
            destination: address,
            travelMode: "DRIVING"
        };
        var directionsService = new google.maps.DirectionsService();
        directionsService.route(request, function (response, status) {
            if (status == google.maps.DirectionsStatus.OK) {
                var directionsDisplay = new google.maps.DirectionsRenderer();
                var myOptions = {
                    mapTypeId: google.maps.MapTypeId.ROADMAP
                }
                var map = new google.maps.Map(document.getElementById("gmaps-container"), myOptions);
                directionsDisplay.setMap(map);
                directionsDisplay.setPanel(document.getElementById("directionsroute")); 
                directionsDisplay.setDirections(response);
            }
        });
    }
    return false;
}

function setupLocationLinks() {
    $(".findlocation").click(function (event) {
        event.preventDefault();
        $.get(this.href, function (data) {
            $("#addition-location-body").html(data);
            setupLocationLinks();
            return false;
        });
    });
}

function doLocationSearch() {
    //alert('search');
    var zip = $("#zipBox").val();
    //alert(zip);
    var action = $("#additional-location-search").attr('action');
    //alert(action);
    if (zip != '') {
        //$("#additional-locations-count").html("Locations near " + zip);
        action = action + "&zip=" + zip;
        $("#tmp-locations").html(zip);
        $.get(action, function (data) {
            $("#addition-location-body").html(data);
            setupLocationLinks();
            return false;
        });
    } else {
        //$("#additional-locations-count").html("&#160;");
        $.get(action, function (data) {
            $("#addition-location-body").html(data);
            setupLocationLinks();
            return false;
        });
    }
    return false;
}

function receiveOoyalaEvent(playerId, eventName, eventArgs) {

}

//Complaint Detail
var cmplDetailShown = new Array();
function showCmplDetail(lnk, pkid) {
    if ($.inArray(pkid, cmplDetailShown) == -1) {
        var action = $(lnk).attr("href")
        $.get(action, function (data) {
            $("#cmplDetail_" + pkid).html(data);
            setupDetailLinks("#cmplDetail_" + pkid);
            return false;
        });
        trackClick(lnk, '');
        cmplDetailShown.push(pkid);
    }
    pageTracker._trackEvent('Business Review', 'Complaint Detail: Read', $('h1.business-title').text());
    return false;
}

function setupDetailLinks(dv) {
    $(dv + " .PagingDetail").click(function (event) {
        event.preventDefault();
        trackClick(this, '');
        $.get(this.href, function (data) {
            $(dv).html(data);
            setupDetailLinks(dv);
            return false;
        });
    });
    if ($("a.popMoreInfo").length > 0) {
        $("a.popMoreInfo").bind("click", function (e) {
            e.stop;

            if ($(this).parent("td.complaint-detail").siblings("td.date").children("a").hasClass("open")) {
                //alert("close");			
                $(this).parent("td.complaint-detail").siblings("td.date").children("a").removeClass("open");
                $(this).parent("td.complaint-detail").children(".additional-notes").removeClass("open");
            }
            else {
                $(this).parent("td.complaint-detail").siblings("td.date").children("a").addClass("open");
                $(this).parent("td.complaint-detail").children(".additional-notes").addClass("open");
            }
            return false;
        });
    }
    // bind close fucntion to the more info close button
    if ($(".additional-notes .close-win").length > 0) {
        $(".additional-notes .close-win").bind("click", function () {
            $(this).parent(".additional-notes").removeClass("open");
            $(this).parent(".additional-notes").parent("td.complaint-detail").prev("td.date").children("a").removeClass("open");
            return false;
        });
    }
}

//Review Detail
var cmplReviewsShown = new Array();
function showReviewDetail(lnk, pkid) {
    if ($.inArray(pkid, cmplReviewsShown) == -1) {
        var action = $(lnk).attr("href")
        $.get(action, function (data) {
            $("#reviewDetail_" + pkid).html(data);
            setupDetailLinks("#reviewDetail_" + pkid);
            return false;
        });
        trackClick(lnk, '');
        cmplReviewsShown.push(pkid);
    }
    switch (pkid) {
        case '-1':
            pageTracker._trackEvent('Business Review', 'Customer Review: Read Negative Reviews', $('h1.business-title').text());
            break;
        case '0':
            pageTracker._trackEvent('Business Review', 'Customer Review: Read Neutral Reviews', $('h1.business-title').text());        
            break;
        case '1':
            pageTracker._trackEvent('Business Review', 'Customer Review: Read Positive Reviews', $('h1.business-title').text());
            break;
    }
    return false;
}

$.fn.preventDoubleSubmit = function () {
    $(this).submit(function () {
        if (this.beenSubmitted)
            return true;
        else
            this.beenSubmitted = true;
    });
};
