﻿
function e(id) {return document.getElementById(id);}

function getWinHeight() {if (window.self && self.innerHeight) {return self.innerHeight;}if (document.documentElement && document.documentElement.clientHeight) {return document.documentElement.clientHeight;}return 0;}

function getWinWidth() {if (window.self && self.innerWidth) {return self.innerWidth;}if (document.documentElement && document.documentElement.clientWidth) {return document.documentElement.clientWidth;}return 0;}

function popup(url, name, height, width, scrollbars)
{
    var popwin;
    var opts = "toolbar=no,status=no,location=no,menubar=no,resizable=yes,height=" + height + ",width=" + width + ",scrollbars=" + scrollbars;
    popwin = window.open("", name, opts);
    popwin.focus();
    popwin.location = url;
    self.name = "opener";
    return false;
}

function closePopup() {timer = setTimeout('window.close();', 2);}

function process() {document.getElementById("processing").style.display = "inline";}

function clearSearch(fld, origValue) {
    if (fld) {
        if (fld.value) {
            if (fld.value == origValue) {
                fld.value = ''
            }
        }
    }
}

function toggleHelp(helpToggleLink) {
    var Dom = YAHOO.util.Dom;
    var helpNode = Dom.getFirstChildBy(Dom.getAncestorByClassName(helpToggleLink, "componentFrame"), function (el) {return Dom.hasClass(el, "componentHelp");});
    if (helpNode) {
        (Dom.hasClass(helpNode, "vis")) ? Dom.replaceClass(helpNode, "vis", "invis") : Dom.replaceClass(helpNode, "invis", "vis");
    }
}

function setupRatings() {
    $("input.star2").rating({
        callback: function (value, link, name) {

            if (name && name.length > 0)
                name = name.replace("_rating", "");
            else
                return;

            $.post("/services/RateIt.aspx", {
                c: name,
                r: value,
                p: window.location.pathname
            },
                function (data) {
                    if (data == -4) {
                        alert("You have already submitted a rating for this component. To change your rating you must first click the Cancel Rating button.");
                    }
                    else if (data != 1)
                        alert("Sorry, but there was an error saving your rating score. Please try again later.");
                }                
            );            
        }
    });
    $("#star-rating a").tooltip({
        delay: 0,
        showURL: false,
        fade: 250
    });    
}

if (window.jQuery) {
    $(function () {
        if (jQuery().rating) {
            // add the stars
            $(".componentHeader:not(.norate) h2,.toolHeader:not(.norate) h2").each(function () {
                var comp = $(this).text();
                var name = comp.replace(/ /g, "_") + "_rating";

                if (/^(?:Description|Go to a Specific Topic)$/.test(comp))
                    return;

                $(this).before("<div style='float: right; width: 120px;' id='star-rating'><input name='" + name + "' class='star2' value='1' title='Click to rate the usefulness of this component: Very Poor' type='radio' /><input name='" + name + "' class='star2' value='2' title='Click to rate the usefulness of this component: Poor' type='radio' /><input name='" + name + "' class='star2' value='3' title='Click to rate the usefulness of this component: OK' type='radio' /><input name='" + name + "' class='star2' value='4' title='Click to rate the usefulness of this component: Good' type='radio' /><input name='" + name + "' class='star2' value='5' title='Click to rate the usefulness of this component: Very Good' type='radio' /><a href='/forms/comment.asp' title='Comment'><img style='padding-top:2px' src='/images/images_standard/comment.png' /></a></div>");
            });

            // call the plugin
            setupRatings();

            // select previous ratings
            $(".componentHeader:not(.norate) h2,.toolHeader:not(.norate) h2").each(function () {
                var comp = $(this).text().replace(/ /g, "_");
                var name = comp + "_rating";

                if (/^(?:Description|Go to a Specific Topic)$/.test(comp))
                    return;

                $.post("/services/RateIt.aspx", {
                    c: comp
                },
					function (data) {
					    if (data > 0) {
					        $("input.star2[name='" + name + "']").rating("cancelCallback", true).rating("select", data).rating("cancelCallback", false);
					    }
					}
				);
            });
        }
    });
}