﻿
var keywords;
function OperapaediaKeywords() {
    if (keywords == undefined) {
        $.getJSON("/json/OperapaediaKeywords/",
            function(data) {
                keywords = data;
                if (data.length > 0) {
                    for (k = 0; k < data.length; k++) {
                        $("p", $("#col1")).linkKeywords(keywords[k]);
                        $("li", $("#col1")).linkKeywords(keywords[k]);
                        $("dl", $("#col1")).linkKeywords(keywords[k]);
                        $("p", $("#mainoperapane")).linkKeywords(keywords[k]);
                        $("li", $("#mainoperapane")).linkKeywords(keywords[k]);
                    }
                }
            });
        } else {
        if (keywords.length > 0) {
            for (k = 0; k < keywords.length; k++) {
                $("p", $("#col1")).linkKeywords(keywords[k]);
                $("li", $("#col1")).linkKeywords(keywords[k]);
                $("dl", $("#col1")).linkKeywords(keywords[k]);
                $("p", $("#mainoperapane")).linkKeywords(keywords[k]);
                $("li", $("#mainoperapane")).linkKeywords(keywords[k]);                
            }
        }
    }
}


jQuery.fn.linkKeywords = function(pat) {
    function innerLink(node, pat) {
        var skip = 0;
        if (node.nodeType == 3) {
            var pos = node.data.toUpperCase().indexOf(pat);

            if (pos >= 0) {


                var spannode = document.createElement('a');
                spannode.className = 'keyword';
                spannode.target = "_blank";
                spannode.href = "/Operapaedia/" + pat.toLowerCase();
                var middlebit = node.splitText(pos);
                var endbit = middlebit.splitText(pat.length);
                var middleclone = middlebit.cloneNode(true);
                spannode.appendChild(middleclone);
                middlebit.parentNode.replaceChild(spannode, middlebit);
                skip = 1;
            }
        }
        else if (node.nodeType == 1 && node.childNodes && !/(script|style|a|h1|q|select)/i.test(node.tagName)) {
            for (var i = 0; i < node.childNodes.length; ++i) {
                i += innerLink(node.childNodes[i], pat);

            }
        }
        return skip;
    }
    return this.each(function() {
        innerLink(this, pat.toUpperCase());
    });
};


$.fn.vjustify = function() {
    var maxHeight = 0;
    $(".resize").css("height", "auto");
    this.each(function() {
        if (this.offsetHeight > maxHeight) {
            maxHeight = this.offsetHeight;
        }
    });
    this.each(function() {
        $(this).height(maxHeight);
        if (this.offsetHeight > maxHeight) {
            $(this).height((maxHeight - (this.offsetHeight - maxHeight)));
        }
    });
};

$.fn.hoverClass = function(classname) {
    return this.hover(function() {
        $(this).addClass(classname);
    },
    function() {
        $(this).removeClass(classname);
    });
};

function ToggleText(mode) {
    if (mode == 1) {
        $("body").addClass("text-large");
        $.cookie("text-size", "large", {"path": "/"});
    } else {
    $("body").removeClass("text-large");
    $.cookie("text-size", null,{"path": "/"});
    }
}

function GenerateTimer() {
    $.getJSON("/json/GetTicketExpiration/" + dateFormat(new Date(), "yymmdd_hhMMss"),
    function(json) {
        if (json.d != "TESSITURA_SEAT_LOCKING_EXCEPTION") {
            var expiration = parseInt(json);
            var sCartTimer = "";
            var secondsRemaining = expiration;
            //alert(expiration + ' seconds remaining');
            var timeRemaining = Math.floor(secondsRemaining / 60);
            if (timeRemaining < 30) {
                if (timeRemaining > 1) {
                    $("div.timer").html("You have " + timeRemaining + " minutes remaining to complete your order").show();
                    setTimeout("UpdateTimer(" + timeRemaining + ")", 60000);

                }
                else {
                    window.location = '/Account/SessionExpired/';
                }
            }
        }
    });

}

function UpdateTimer(currentTime) {
    currentTime -= 1;
    if (currentTime > 0) {
        $("div.timer").html("You have " + currentTime + " minutes remaining to complete your order");
        setTimeout("UpdateTimer(" + currentTime + ")", 60000);
    }
    else {
        window.location = '/Account/SessionExpired';
    }
}
function FormEffects() {
    //radio hotspots
    $("input[type=radio]", $(".radio-hotspot")).click(radioClickHandler);

    $(".radio-hotspot").removeClass("selected").attr("sel", "false");
    $(".radio-hotspot").each(function(ind, el) {
        if ($("input[type=radio]:checked", $(el)).length > 0) {
            $(el).addClass("selected").attr("sel", "true");
        }
        $(el).attr("customtype", "radio-hotspot");
    });
    //checkbox hotspots
    $("input[type=checkbox]", $(".checkbox-hotspot")).click(checkboxClickHandler);

}

function radioClickHandler(event) {
    //event.stopPropagation();
    $(".radio-hotspot").removeClass("selected").attr("sel", "false");
    $(".radio-hotspot").each(function(ind2, el2) {
        if ($("input[type=radio]:checked", $(el2)).length > 0) {
            $(el2).addClass("selected");
            $(el2).attr("sel", "true");
        }
    });
}

function stopAllAudio() {
     $(".audioplayer").each(function(ind) {
     //alert($(this).attr("id"));
                try{
     $(this).stopAudio();
            }catch(err){}});
            $(".audio_title").animate({ width: "0px" }, { duration: 2000, easing: 'linear' });
}

function stopAudioJS(src) {
    alert("src: "+ src);
    $("object", $(".audio")).each(function() {
        if ($(this).attr("id") != src) {
            try {
                thisMovie($(this).attr("id")).haltAudio();
            }
            catch (err) { alert(err.description); }
        }
    });
    $("embed", $(".audio")).each(function() {
        if ($(this).attr("name") != src) {
            try {
                thisMovie($(this).attr("name")).haltAudio();
            }
            catch (err) { }
        }
    });
}

function Search(q) {
    $.getJSON("/json/Search/?q=" + escape(q),
            function(data) {
                var content = "<div class=\"search-results\">";
                    if (data.length > 0) {
                    for (i = 0; i < data.length; i++) {
                        content += "<div class=\"search-result\">";
                        content += "<h3><a href=\"" + data[i].Url + "\" target=\"_blank\">" + data[i].Title + "</a></h3>";
                        content += "<p>" + data[i].Summary + "</p></div>";
                    }
                    
                } else {
                content += "<div>Your search '" +q+ "' did not return any valid results.</div>";
                }
                content += "</div>";

                var searchboxy = new Boxy(content, {
                    "title": "San Diego Opera Search Results for '" + q + "'",
                    modal: true,
                    unloadOnHide: true
                });
            });
}

function thisMovie(movieName) {
    if (navigator.appName.indexOf("Microsoft") != -1) {
        return document.getElementById(movieName);
    }
    else {
        return document[movieName];
        //return document.getElementById(movieName);
    }
}

function checkboxClickHandler(event) {
    //event.stopPropagation();
    $(event.target).parent(".checkbox-hotspot").toggleClass("selected");
}

function padRight(str, len) {
    var s = str;
    var numspaces = len - str.length;
    if (numspaces > 0) {
        for (i = 0; i < numspaces; i++) {
            s += '&nbsp;';
        }
    }
    return s;
}
function padLeft(str, len) {
    var s = str;
    var numspaces = len - str.length;
    if (numspaces > 0) {
        for (i = 0; i < numspaces; i++) {
            s = '&nbsp;'+s;
        }
    }
    return s;
}


