//////////////////////////////////////////////////////////
//
//  File: Global Javascript
//  Author: Craig Nelson / Classic Labs Development
//
//

    // external links
    function setExternalLinks(anchors) {
        anchors.each(function (e) {
            if (e.getAttribute("rel") == "external") {
                e.target = "_blank";
            }
            
            if (e.getAttribute('rel') == 'no-click') {
                Event.observe(e, "click", function (event) {
                   event.preventDefault(); 
                });
            }
        });
        
        return;
    }
    // --external links
    
    /* ratings */
    function setRatingSelectorBackground(stars, current) {
        stars.each(function (e, index) {
            if (index <= current) {
                e.style.backgroundPosition = (e.style.backgroundPosition.match(/-21/g)) ? 0 : -21 + "px";
            }
        });
    }
    
    function ratingSelector(stars) {
        stars.each(function (e, index) {
            Event.observe(e, "mouseover", function (event) {
                setRatingSelectorBackground(stars, index);
            });
            
            Event.observe(e, "mouseout", function (event) {
                setRatingSelectorBackground(stars, index);
            });
            
            Event.observe(e, "click", function (event) {
                this.blur();
                var notice;
                
                switch (index) {
                    case 0:
                        notice = "1 Star Rating";
                        break;
                    case 1:
                        notice = "2 Star Rating";
                        break;
                    case 2:
                        notice = "3 Star Rating";
                        break;
                    case 3:
                        notice = "4 Star Rating";
                        break;
                    case 4:
                        notice = "5 Star Rating";
                        break;
                }
                
                alert(notice);
                event.preventDefault();
            });
        });
        
    }
    /* --ratings */

    // events
    document.observe("dom:loaded", function () {
        $$("input.guide").each(function (e) {
            var v = e.value; // initial value
            
            Event.observe(e, "focus", function (event) {
                if (this.value == v) {
                    this.value = "";
                }
            });
            
            Event.observe(e, "blur", function (event) {
                this.value = (e.value == "") ? v : e.value;
            });
        });
    }); // document.observe

    Event.observe(window, "load", function (event) {
        setExternalLinks($$("a"));
        var stars = $$("ul.stars a");
        if (stars.length !== 0) { // are we on a page that has rating selectors
            ratingSelector(stars);
        }
    }); // window load
    // --events
