﻿// Common routines - (used by root site, purchase, etc.)

$.fn.extend({
    pepHelp: function() {
        var $content = $(this);

        $content.append('<table cellpadding="0" cellspacing="0" border="0" width="100%">' +
							'<tr><td class="pepHelpLeftTop"></td><td class="pepHelpTop"></td><td class="pepHelpRightTop"></td></tr>' +
							'<tr><td class="pepHelpTitleLeft"></td><td class="pepHelpTitle" id="helpTitle"></td><td  class="pepHelpTitleRight"></td></tr>' +
							'<tr><td class="pepHelpLeft"></td><td class="pepHelpDescription" id="helpDescription"></td><td class="pepHelpRight"></td></tr>' +
							'<tr><td class="pepHelpLeftBottom"></td><td class="pepHelpBottom"></td><td class="pepHelpRightBottom"></td></tr>' +
							'</table>');

        var dialogObj = {
            $dialog: null,
            $openerElement: null,
            open: function(btnHelp, helpId) {
                this.$openerElement = btnHelp;
                PEP.Purchase.AppCode.PurchaseServices.GetHelpText(helpId, this.getHelpText);

                return false;
            },
            getHelpText: function(helpTextObject) {
                $('#helpTitle').html(helpTextObject.Title);
                $('#helpDescription').html(helpTextObject.Description);
                var helpWidth = helpTextObject.PixelWidth;
                if (helpWidth == null || helpWidth == 0)
                    helpWidth = 240;

                if (dialogObj.$dialog)
                    dialogObj.$dialog
		            	.dialog('option', 'position', 'center')
		            	.dialog('option', 'width', helpWidth)
		            	.dialog('open');
                else {
                    dialogObj.$dialog = $content.dialog({
                        dialogClass: 'pepDialog pepHelp',
                        resizable: false,
                        width: helpWidth,
                        title: '',
                        minHeight: 0
                    });
                }

                dialogObj.alignToElement(dialogObj.$openerElement);
            },
            close: function() {
                this.$openerElement = null;
                if (this.$dialog) this.$dialog.dialog('close');
                return false;
            },
            alignToElement: function(element) {
                var pos = Sys.UI.DomElement.getLocation(element),
            		doc = $(document),
					pTop = pos.y - doc.scrollTop(),
					pLeft = pos.x - doc.scrollLeft(),
                    pos = [pLeft, pTop],
                    forceLeft = $(element).hasClass('forceLeft');

                var h = $content.outerHeight()
						, w = $content.outerWidth()
						, winW = $(window).width()
						, x = pLeft + ((forceLeft || (pLeft + w > winW)) ? 19 - w : 0)
						, y = pTop + (((pTop - h) < 0) ? 15 : -h)
						, className = 'pepHelp' + (x < pLeft ? "Right" : "Left") + (y < pTop ? "Bottom" : "Top");

                $.each(['pepHelpLeftBottom', 'pepHelpRightBottom', 'pepHelpLeftTop', 'pepHelpRightTop'], function(i, n) {
                    $content.find('.' + n).removeClass(n + 'Ptr');
                });

                $content.find('.' + className)
						.addClass(className + "Ptr")

                this.$dialog.dialog('option', 'position', [x, y]);
            },
            handleWindowResize: function() {
                if (typeof (dialogObj) != "undefined" && dialogObj.$openerElement != null)
                    dialogObj.alignToElement(dialogObj.$openerElement);
            }
        };

        $(window).resize(dialogObj.handleWindowResize);

        return dialogObj;
    }
});

$.fn.extend({
    addHover: function() {
        this.hover(
            function() {
                if ($(this).hasClass('disableHover')) return;
                $(this).css('background-position',
                    '0 -' + parseInt($(this).css('height')) + 'px');
            },
            function() {
                if ($(this).hasClass('disableHover')) return;
                $(this).css('background-position', '0 0px')
            })
        this.click(
            function() {
                $(this).css('background-position',
                    '0 -' + (parseInt($(this).css('height')) * 2) + 'px');
            });        
    }
});

//set start up values
$(function() {

    // NOTE: Due to the nature of the Font Size control buttons, their hover state 
    // is handled seperately, within "/scripts/pemco.com.rootsite.js"

    $('.btnHover').addHover();

    $('div.listItemTitle').hover(
        function() {
            $(this).prev().trigger('mouseenter');
        },
        function() {
            $(this).prev().trigger('mouseout');
        }
    );

    $('div[id$=_listItemImage]').hover(
    	function() {
    	    // cannot trigger mouse events for div.listItemTitle because of recursion
    	    $(this).next().find('a').css('color', '#e7832d')
    	},
    	function() {
    	    $(this).next().find('a').css('color', '')
    	}
    );

    $(document).bind("contextmenu", function(e) {
        return false;
    });

    $('#FooterCopyRight').text('©2010, PEMCO Mutual Insurance Company, Seattle, WA');
    $('#FooterCopyRightLife').text('©2010, PEMCO Life Insurance Company, Seattle, WA');
});

String.prototype.HtmlEncode = function() {
    return this.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
};

String.prototype.HtmlDecode = function() {
    return this.replace(/&amp;/g, '&').replace(/&lt;/g, '<').replace(/&gt;/g, '>');
};

try {document.domain = "pemco.com";} catch(ex){};

function ratingsDisplayed(totalReviewsCount, avgRating, ratingsOnlyReviewCount, recommendPercentage, productID) {
    if (totalReviewsCount == 0) {
        var bvRevCntr = document.getElementById("BVReviewsContainer");
        var bvSVPLink = document.getElementById("BVSVPLinkContainer");

        if (bvRevCntr) { bvRevCntr.style.display = "none"; }
        if (bvSVPLink) { bvSVPLink.style.display = "none"; }
    }
}

function pageChanged(pageName, pageStatus) {
}

function BVHandleSummaryResults(jsonData) {
    var reviewsSummaryContent = document.getElementById('ReviewsSummaryContent_' + jsonData.subjectID);
    if (reviewsSummaryContent) {
        if (jsonData.totalReviews > 0 && jsonData.reviewsUrl.length > 0) {
            var readReviewStr = "Read " + jsonData.totalReviews + " Reviews";
            reviewsSummaryContent.innerHTML += "Average Rating: " + jsonData.averageRating.toFixed(2) + "<br />";
            reviewsSummaryContent.innerHTML += readReviewStr.link(jsonData.reviewsUrl);
        }
    }
}

function getQuerystringValue(key, defaultValue) {
    if (defaultValue == null) defaultValue = "";
    key = key.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regex = new RegExp("[\\?&]" + key + "=([^&#]*)");
    var qs = regex.exec(window.location.href);
    if (qs == null)
        return defaultValue;
    else
        return qs[1];
}