// these script functions require the jquery library
var prefix = "";
var HOUSE_INFORMATION_AJAX_CONTENT_PAGE = prefix + "/AjaxContentPages/HouseSessionInformation.aspx";
var BILLS_BEFORE_THE_HOUSE_AJAX_CONTENT_PAGE = prefix + "/AjaxContentPages/BillsBeforeTheHouseContent.aspx";
var REPRESENTATIVE_INFORMATION_AJAX_CONTENT_PAGE = prefix + "/Handlers/RepresentativeInformationHandler.ashx?";

function ShowHideSearchBox(RelatedElement, SearchBoxDiv, HideClass, topoffset, leftoffset) {

    //the nub that points to the link that has been clicked has to be adjusted based 
    // on the browser.  Version of IE before 8 need it position lower.  Look for 
    // all the elements with the nub class which should only be the image nub
    // elements and position them depending on which browser is being used.
    if ($.browser.msie && $.browser.version < 8) {
        $(".searchBoxNub").each(function(i) {
            if (this.style.top != "10px") {
                this.style.top = "10px";
            }
        });
    } else {
        $(".searchBoxNub").each(function(i) {
            if (this.style.top != "5px") {
                this.style.top = "5px";
            }
        });
    }

    // look to see if the element that was passed in is being displayed.
    // if it is then hide that element else display it
    if ($(jq(SearchBoxDiv.id)).css("display") != 'none') {

        $(jq(SearchBoxDiv.id)).slideUp("slow");
    } else {

        //before displaying the searchbox for the clicked link
        // hide any of the searchboxes that are currently displayed
        HideClass = "." + HideClass;
        $(HideClass + "[display!=none]").slideUp("slow");

        // this section calculates where to display the box relative to the
        //  related element passed in
        var curleft = curtop = 0

        if (RelatedElement.offsetParent) {
            curleft = RelatedElement.offsetLeft
            curtop = RelatedElement.offsetTop

            while (RelatedElement = RelatedElement.offsetParent) {
                curleft += RelatedElement.offsetLeft
                curtop += RelatedElement.offsetTop
            }
        }

        // position the offsets based on what was passed in plus the offset
        //  calculated from
        // the related element.
        SearchBoxDiv.style.top = (curtop + topoffset) + "px";
        SearchBoxDiv.style.left = (curleft + leftoffset) + "px";

        //display the searchbox with a slow slide down transition
        $(jq(SearchBoxDiv.id)).slideDown("slow");
    }
}


//adjust an id to replace special characters with something that jquery
// will recongnize
function jq(myid)
{ return '#' + myid.replace(/:/g, "\\:").replace(/\./g, "\\."); }


//Hides the Div if it exists.
function HideRepInfoBox(repID) {
    RepDivName = "#RepInfoDiv";
    if ($(RepDivName).length) {
        $(RepDivName).hide();
    }
}

//this function uses the representative id to create a div to associate to the
// representative.  If it already exists, it checks to see if has the default div 
// html which would mean that for some reason the information for the div didn't 
// load previously or this is the first time the div has been loaded.  Try to 
// load the div information if it wasn't loaded before.
function ShowRepInfoBox(repID, e) {

    RepDivName = "#RepInfoDiv"

    if ($(RepDivName).length == 0) {
        //create the div right after the first instance of the form element.
        $("form:first").after("<div class=\"RepInfoDiv\" onmouseout=\"Javascript: ToolTipHoverMouseOutEvent(" + repID + ")\"  id=\"RepInfoDiv\"><big>LOADING ... </big> </div>");
    }

    // call the handler to retrieve the html to display within the div
    //upon the call back place the html within the rep div.
    //    if ($(RepDivName).html().search("LOADING ...") != -1) {
    $.post(REPRESENTATIVE_INFORMATION_AJAX_CONTENT_PAGE,
    {
        RepresentativeID: repID
    }, function(response) {
        $(RepDivName).html(response);
        $(RepDivName).show();

    });
    //    }

    //Get the window height and representative div height   
    var winH = $(window).height();
    var repDivH = $(RepDivName).height();

    //make sure that if the popup is displayed below the  mouse cursor that the bottom
    // of the popup won't go below the window bottom, if it does than display the popup
    // above the mouse cursor

    if ((mouseY(e) + repDivH) > ((document.documentElement.scrollTop ?
   document.documentElement.scrollTop :
   document.body.scrollTop) + winH)) {
        //set the position of the rep div at the same cordinates as the
        // mouse.  MouseY and MouseX functions compensate for the webpage being scrolled.
        // the representative div height is subtracted to make the div appear above the 
        // mouse cursor
        $(RepDivName).css("top", (mouseY(e) - repDivH - 5) + "px");
        $(RepDivName).css("left", (mouseX(e) + 10) + "px");
    } else {

        //set the position of the rep div at the same cordinates as the 
        // mouse.  MouseY and MouseX functions compensate for the webpage being scrolled.
        $(RepDivName).css("top", (mouseY(e) + 5) + "px");
        $(RepDivName).css("left", (mouseX(e) + 10) + "px");
    }
}

function mouseX(evt) {
    if (evt.pageX) return evt.pageX;
    else if (evt.clientX)
        return evt.clientX + (document.documentElement.scrollLeft ?
   document.documentElement.scrollLeft :
   document.body.scrollLeft);
    else return null;
}
function mouseY(evt) {
    if (evt.pageY) return evt.pageY;
    else if (evt.clientY)
        return evt.clientY + (document.documentElement.scrollTop ?
   document.documentElement.scrollTop :
   document.body.scrollTop);
    else return null;
}

function formSubmit(event, button) {
    if (event.keyCode == 13) {
        document.getElementById(button).click();
        return false;
    }
    return true;
}

function NextField(event, textfield1, textfield2, MaxLength) {
    if (document.getElementById(textfield1).value.length == MaxLength &&
            event.keyCode != 37) {
        document.getElementById(textfield2).focus();
    }
}

function PreviousField(event, textfield1, textfield2) {

    if (((event.keyCode == 37 || event.keyCode == 8) &&
            (document.getElementById(textfield1).value.length == 0)) ||
            (caretPos(textfield1) == 1)) {
        document.getElementById(textfield2).focus();
        document.getElementById(textfield2).value = document.getElementById(textfield2).value;
    }

}

function caretPos(textfield1) {

    var i = document.getElementById(textfield1).value.length + 1;
    if (document.getElementById(textfield1).createTextRange) {
        theCaret = document.selection.createRange().duplicate();
        while (theCaret.parentElement() == document.getElementById(textfield1)
&& theCaret.move("character", 1) == 1) --i;
    }
    return i == document.getElementById(textfield1).value.length + 1 ? -1 : i;
}



// This javascript will take a hidden panel on a html page and when a certain link is
// clicked it shows the panel in the middle of the screen with the rest of the screen
// grayed out and disabled.  The modal panel is to be used for any warnings about
// leaving the current website and will have a link to the website the orginally
// clicked link was set for
//
// linkid is the id of the hyperlink that when clicked will trigger the modalpanel
// to show up
// 
// modalpanelID is the id of the panel to be shown when the link is clicked.
//  this function needs to be set up on the document load using a script like this.
//
// <script type="text/javascript" language="JavaScript">
//    $(document).ready(function() {
//        var GoogleMapPanelID = 'pnlGoogleMap';
//        var districtOneMapLink = '<%= hlDistrict1MapDrivingDirections.ClientID %>';
//        ShowModalLinkForwardingPanel(districtOneMapLink, GoogleMapPanelID);        
//    });
//  </script>
//
// somewhere in the page a hidden div needs to be place with a id of "mask"
// <div id="mask"></div>
//  the css needs to be set to include these attributes
// #mask {   
//  position:absolute;   
//  z-index:9000;   
//  background-color:#000; 
//  display:none;
//}
//
// the css of the div (modal panel) to be shown should be include these attributes
//     
//#modalpanel {   
//  position:absolute;   
//  display:none;   
//  z-index:9999;
//}
//
// the link to be forwarded to should have the class "ForwardedLink"
// any button or link that will cancel the modal panel should have the class "close"
function ShowModalLinkForwardingPanel(linkid, modalpanelID) {

    var s;

    //select all the a tag with name equal to modal
    $(jq(linkid)).click(function(e) {
        //Cancel the link behavior   
        e.preventDefault();
        //Get the A tag   
        var id = $(jq(modalpanelID));
        $('.ForwardedLink').attr("href", $(jq(linkid)).attr("href"));

        //Get the screen height and width   
        var maskHeight = $(document).height();
        var maskWidth = $(window).width();

        //Set height and width to mask to fill up the whole screen
        $('#mask').css({ 'width': maskWidth, 'height': maskHeight, 'top': 0, 'left': 0 });


        //transition effect        
        $('#mask').fadeIn(500);
        $('#mask').fadeTo("fast", 0.5);

        //Get the window height and width   
        var winH = $(window).height();
        var winW = $(window).width();

        //Set the popup window to center   
        $(id).css('top', winH / 2 - $(id).height() / 2);
        $(id).css('left', winW / 2 - $(id).width() / 2);

        //transition effect
        $(id).fadeIn(2000);

        s = setInterval("scrollingModalLinkForwardingPanelDetector(" + modalpanelID + ")", 500);

    });

    //if close button is clicked
    $('#' + modalpanelID + ' .close').click(function(e) {
        //Cancel the link behavior   
        e.preventDefault();
        $('#mask').hide();
        $(jq(modalpanelID)).hide();
        clearInterval(s);
    });

    //if mask is clicked   
    $('#mask').click(function() {
        $(this).hide();
        $(jq(modalpanelID)).hide();
        clearInterval(s);
    });

};

// sets the elements top/left position to place it in the center of the browser
//  no matter the size or where the scroll is on the page.
function scrollingModalLinkForwardingPanelDetector(modalpanelID) {
    if ($(jq(modalpanelID.id)).css("display") != 'none') {
        var id = $(jq(modalpanelID.id));

        //Get the window height and width   
        var winH = $(window).height();
        var winW = $(window).width();

        var scrOfX = 0, scrOfY = 0;
        if (typeof (window.pageYOffset) == 'number') {
            scrOfY = window.pageYOffset; scrOfX = window.pageXOffset;
        } else if (document.body && (document.body.scrollLeft ||
             document.body.scrollTop)) {
            scrOfY = document.body.scrollTop; scrOfX = document.body.scrollLeft;
        } else if (document.documentElement && (document.documentElement.scrollLeft ||
                 document.documentElement.scrollTop)) {
            scrOfY = document.documentElement.scrollTop; scrOfX = document.documentElement.scrollLeft;
        }


        $(id).css('top', winH / 2 - $(id).height() / 2 + scrOfY);
        $(id).css('left', winW / 2 - $(id).width() / 2 + scrOfX);
    }
}





function printFrame() {
    var DocIFrame = frames['<%= ifrSearchDoc.ClientID %>'];
    DocIFrame.focus();
    DocIFrame.print();
}

function getDocument(URL) {
    SearchListDiv.style.visibility = 'hidden';
    SearchListDiv.style.display = 'none';
    DocumentDiv.style.visibility = 'visible';
    DocumentDiv.style.display = 'inline';
    SearchDocIFrm.location.href = URL;
    SearchDocIFrm.location.reload();
}

function getFolder(URL) {
    window.parent.location.href = URL;
}

function showSearchResultsWindow() {
    SearchListDiv.style.visibility = 'visible';
    SearchListDiv.style.display = 'inline';
    DocumentDiv.style.visibility = 'hidden';
    DocumentDiv.style.display = 'none';
}

function hideSearchResultsWindow() {
    SearchListDiv.style.visibility = 'hidden';
    SearchListDiv.style.display = 'none';
    DocumentDiv.style.visibility = 'visible';
    DocumentDiv.style.display = 'inline';
}

function ToolTipHoverMouseOutEvent(repid) {
    HideRepInfoBox(repid);
}
function setupSessionInformationTimer(timerinterval) {
    setInterval(loadsessionInformation, timerinterval);
}
function setupBillsBeforeTheHouseTimer(timerinterval) {
    setInterval(loadBillsBeforeTheHouseInformation, timerinterval);
}

function loadsessionInformation() {
    var contentPanel = '#toplayer';
    $.ajax({
        type: "POST",
        url: HOUSE_INFORMATION_AJAX_CONTENT_PAGE, //path to external content
        async: true,
        error: function(ajaxrequest) {
            $(contentPanel).html('Information Temporarily Unavailable')
        },
        success: function(content) {
            $(contentPanel).html($(content).find(contentPanel).html());
        }
    })
}
function loadBillsBeforeTheHouseInformation() {
    var contentPanel = '#NewsVertical';
    $.ajax({
        type: "POST",
        url: BILLS_BEFORE_THE_HOUSE_AJAX_CONTENT_PAGE, //path to external content
        async: true,
        error: function(ajaxrequest) {
        $(contentPanel).html('Information Temporarily Unavailable')
        },
        success: function(content) {
        $(contentPanel).html($(content).find(contentPanel).html());
        }
    })
}

