﻿/*
** Timer global.
*/
var t = new Array();

/*
** Used to set a maximum length for textarea input fields
*/
function textLimit(textboxname, textbox, maxlength) {
    if (textbox.value.length > maxlength) {
        textbox.value = textbox.value.substring(0, maxlength);
        alert(textboxname + " text box exceeds " + maxlength + " characters.\nInput has been truncated.");
    }
};

/*
** Used to set the page title in Hosted Display Mode pages
*/
function setPageTitle(pageTitle) {
    //escape characters
    pageTitle = pageTitle.replace("&rsquo;", "\'"); //** ' -> \'
    pageTitle = pageTitle.replace("&#092;", "\\");  //** \ -> \\
    document.title = pageTitle;
};

/*
** Used to rotate photos on the front page 
*/
function photoRotator() {
    $(document).ready(function() {
        $(".main_image .desc").show(); //Show Banner
        $(".main_image .desc_block").animate({ opacity: 1.00 }, 1); //Set Opacity, was 0.85, changed to be solid but kept code intact

        $(".image_thumb ul li").click(function() {
            //list item selected, stop all timers and stay on selected list item
            for (i=0;i<t.length;i++) {
                clearTimeout(t[i]);
            }
            activatePhoto(this, $(this).prevAll().length)    
        }).hover(function() { //Hover effects on list-item
            $(this).addClass('hover'); //Add class "hover" on hover
            $(this).find('small').addClass('hover'); //Add class "hover" on hover 
        }, function() {
            $(this).removeClass('hover'); //Remove class "hover" on hover out
            $(this).find('small').removeClass('hover'); //Remove class "hover" on hover out
        });
        
        indexedSelect(0);
        sequential();
    });    
};

/*
** Used to sequentially rotate through an unordered lists children one-time through
*/
function sequential() {
    $(document).ready(function() {
        var childCount = $(".image_thumb").find('ul').children().size();
        var time = 10000;
        for (i=1;i<=childCount;i++) {
            //set a staggered timeout for all list items
            t[i-1] = setTimeout('indexedSelect(\''+i+'\')', time*i);
        }
    });
};

/*
** Used to select a child list item by it's zero based index to trigger activatePhoto() on
*/
function indexedSelect(index) {
    $(document).ready(function() {
        activatePhoto($(".image_thumb").find('ul').find('li:eq('+index+')'), index);
        //if at the end, repeat timer processes all over again
        if ( index == $(".image_thumb").find('ul').children().size() ) {
            indexedSelect(0);
            sequential();
        }
    });
};

function activatePhoto(photo, index) {
    $(document).ready(function() {
        //Set Variables
        var imgAlt = $(photo).find('img').attr("alt"); //Get Alt Tag of Image
        var imgTitle = $(photo).find('a').attr("href"); //Get Main Image URL
        var imgDesc = $(photo).find('.desc_block').html();  //Get HTML of the "desc_block" container
        var imgDescHeight = $(".main_image").find('.desc_block').height(); //Find the height of the "desc_block"

        if ($(photo).is(".active")) {  //If the list item is active/selected, then...
            return false; // Don't click through - Prevents repetitive animations on active/selected list-item
        }
        else { //If not active then...
            //Animate the Description
            $(".main_image .desc_block").animate({ opacity: 0, marginBottom: -imgDescHeight }, 250, function() { //Pull the block down (negative bottom margin of its own height)
                $(".main_image .desc_block").html(imgDesc).animate({ opacity: 1.00, marginBottom: "0" }, 250); //swap the html of the desc_block, then pull the desc_block container back up and set opacity, was 0.85, changed to be solid but kept code intact
                $(".main_image img").attr({ src: imgTitle, alt: imgAlt }); //Switch the main image (URL + alt tag)
            });
        }
        //Show active list-item
        $(".image_thumb ul li").removeClass('active'); //Remove class of 'active' on all list-items
        $(".image_thumb small").removeClass('active'); //Remove class of 'active' on all small elements
        $(photo).addClass('active');  //Add class of 'active' on the selected list
        $(photo).find('small').addClass('active');  //Add class of 'active' on the small element

        //Show all inactive images
        $(".image_thumb ul li").find("img.thumb_active").hide();
        $(".image_thumb ul li").find("img.thumb_inactive").show();

        //Show active image for this list-item
        $(photo).find("img.thumb_inactive").hide();
        $(photo).find("img.thumb_active").show();

        return false;
    });
};

/*
** Used to swap between regular and rollover .gif images
*/
function swap() {
    $(document).ready(function() {
        $(".swap").hover(function() {
            $(this).attr("src", $(this).attr("src").slice(0, -4) + "_rollover.gif");
        }, function() {
            $(this).attr("src", $(this).attr("src").slice(0, -13) + ".gif");
        });
    });
};

/*
** Used to swap between regular and rollover .gif images
*/
function swapAsync() {
    $(document).ready(function() {
        $(".async").find(".swap").hover(function() {
            $(this).attr("src", $(this).attr("src").slice(0, -4) + "_rollover.gif");
        }, function() {
            $(this).attr("src", $(this).attr("src").slice(0, -13) + ".gif");
        });
    });
};

/*
** Used to show and animate search results
*/
function showResults() {
    $(document).ready(function() {
        $(".blockFull").show("slow");
    });
};

/*
** Clears the default text from a textbox, places default text back in if text box is blank when focus is changed
*/
function defaultTextBox(element) {
    if (element.value==element.defaultValue) {
        element.value = "";
        return;
    }
    if (element.value=="")
        element.value = element.defaultValue;
};

/*
** Rewrites the Enterprise Portal's global federated search with our own interal 
** global search that searches programs/publications/forms/events/legal library entries
*/
function hostedDisplayGlobalSearch() {
    var gc;
    gc = "<td valign=\"middle\">";
    gc += "<form name=\"search\" action=\"http://www.portal.state.pa.us/portal/server.pt/gateway/PTARGS_0_2_24476_10297_0_43/AgWebsite/Search.aspx?navid=31&parentnavid=0&searchq=\" onsubmit=\"location.href = this.action + this.searchq.value.replace(/ /g, '+') + '&'; return false;\">";
    gc += "<input class=\"formTextBoxText\" type=\"text\" title=\"please enter search criteria\" name=\"searchq\" maxlength=\"50\" size=\"15\" value=\" --search AG--\" onfocus=\"defaultTextBox(this);\" onblur=\"defaultTextBox(this);\" />";
    gc += "<input name=\"searchq\" id=\"searchq\" type=\"image\" title=\"go\" style=\"vertical-align: bottom;\" src=\"http://www.portal.state.pa.us/imageserver/plumtree/portal/custom/enterprise/btnGo.gif\" />";
    gc += "</form></td>";
    return gc;
};

/*** Below functions not being used currently. ***/
/*
** Used to randomly choose the index of elements in an element
*/
function randomFilter() {
    $(document).ready(function() {
        jQuery.jQueryRandom = 0;
        jQuery.extend(jQuery.expr[":"],
        {
            random: function(a, i, m, r) {
                if (i == 0) {
                    jQuery.jQueryRandom = Math.floor(Math.random() * r.length);
                };
                return i == jQuery.jQueryRandom;
            }
        });
    });
};

/*
** Used to randomly select a list-item to trigger the click event on
*/
function randomSelect() {
    $(document).ready(function() {
        $(".image_thumb").find('ul').find('li:random').trigger('click');
    });
};
