﻿// these script functions require the jquery library
var HOUSE_INFORMATION_AJAX_CONTENT_PAGE = prefix + "AjaxContentPages/HouseSessionInformation.aspx";
var REPRESENTATIVE_INFORMATION_AJAX_CONTENT_PAGE = prefix + "Handlers/RepresentativeInformationHandler.ashx?";


//Hides the Div if it exists.
function HideRepInfoBox(repID) {
    var RepInfoDivContainer = "#RepInfoDivContainer";
    if ($(RepInfoDivContainer).length > 0) {
        $(RepInfoDivContainer).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) {

    var RepDivContainerName = "#RepInfoDivContainer";
    var RepDivName = "#RepInfoDiv"

    if ($(RepDivContainerName).length == 0) {
        //create the div right after the first instance of the form element.
        $("body").append("<div class=\"RepInfoDivContainer\"  id=\"RepInfoDivContainer\" ><img class=\"repInfoBoxNubUp\" style=\"position: relative; left: 20px; top: 8px;\" alt=\"\" src=\"" + prefix + "Images/Nub.png\" /><div class=\"RepInfoDiv\" onmouseout=\"Javascript: ToolTipHoverMouseOutEvent(" + repID + ")\"  id=\"RepInfoDiv\"><big>LOADING ... </big> </div><img class=\"repInfoBoxNubDown\" style=\"position: relative; display: none; left: 20px; bottom: 8px;\" alt=\"\" src=\"" + prefix + "Images/NubDown.png\" /></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);
        $(RepDivContainerName).show();

    });
    //    }

    //Get the window height and representative div height   
    var winH = $(window).height();
    var repDivH = $(RepDivContainerName).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
    var repInfoBoxNubDownName = ".repInfoBoxNubDown";
    var repInfoBoxNubUpName = ".repInfoBoxNubUp";
    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
        $(RepDivContainerName).css("top", (mouseY(e) - repDivH - 5) + "px");
        $(RepDivContainerName).css("left", (mouseX(e) + 10) + "px");
        $(repInfoBoxNubDownName).show();
        $(repInfoBoxNubUpName).hide();
    } 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.
        $(RepDivContainerName).css("top", (mouseY(e) + 5) + "px");
        $(RepDivContainerName).css("left", (mouseX(e) + 10) + "px");
        $(repInfoBoxNubDownName).hide();
        $(repInfoBoxNubUpName).show();
    }
}

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 setupSessionInformationTimer(timerinterval) {
    loadsessionInformation();
    setInterval(loadsessionInformation, timerinterval);
}

function loadsessionInformation() {
    var contentPanel = '#SessionLiveStatus';
    $.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("#toplayer").html());
   
            try {
                GrabCashedWatchLiveValue();
            } catch (e) { alert(e); }
        }
    })
}
function GrabCashedWatchLiveValue() {
    var contentPanel = '#SessionLiveStatus';
    if ($(contentPanel).find("#ctrlContentBox_hlWatchNowURL").length == 0) return;
    $.ajax({
        type: "POST",
        url: $(contentPanel).find("#ctrlContentBox_hlWatchNowURL").attr("href") + "&JSON=1", //path to external content
        async: true,
        dataType: "text/html",
        error: function(ajaxrequest) {
            //alert($(contentPanel).find("#ctrlContentBox_hlWatchNowURL").attr("href") + "&JSON=1")
            //alert("error: " + ajaxrequest.responseText);
            //$(contentPanel).html('Information Temporarily Unavailable')
        },
        success: function(contentValue) {
            var returnedVal = null;
            try {
                returnedVal = eval(contentValue);
                $(contentPanel).find("#ctrlContentBox_hlWatchNowURL").attr("href", returnedVal.StreamLink);
            } catch (e) { }
            
            try {
                if (returnedVal.UsePlayer == "0") {
                    $("#ctrlContentBox_hlWatchNowURL").click(function(e) {
                        var href = $(this).attr("href");
                        e.preventDefault();
                        window.open(href, 'VideoPlayer', 'width=560,height=400,toolbar=no,location=no,menubar=no,scrollbars=yes');
                    });
                }
            } catch (e) { }
            
        }
    });
}
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, "\\."); }


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;
}


function showForwardingModalMessagePanel(e, anchor) {
    e.preventDefault();

    //Get the screen height and width   
    var maskHeight = $(document).height();
    var maskWidth = $(window).width();
    var MaskID = "FowardingModalMessageMaskID";
    var MessageTextDivID = "pnlLeavingMyFloridaHouse";
    $("#" + MessageTextDivID + " .ForwardedLink").attr("href", $(anchor).attr("href"));

    if ($.Id(MaskID).length == 0) {
        var maskhtml = $.supplant('<div id="{maskid}" style="position:absolute; z-index:9950; background-color:#000; display:none;"></div>', {
            maskid: MaskID
        });
        $("body").append(maskhtml);
    }



    var id = $.Id(MessageTextDivID);
    id.css("display", "table");

    //Set height and width to mask to fill up the whole screen
    $.Id(MaskID).css({
        'width': maskWidth,
        'height': maskHeight,
        'top': 0,
        'left': 0
    });


    $.Id(MaskID).show();
    $.Id(MaskID).css("opacity", "0.5");

    //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);


    //transition effect
    $(id).show();

    //if close button is clicked
    $('#' + MessageTextDivID + ' .close').click(function(e) {
        //Cancel the link behavior   
        e.preventDefault();
        $.Id(MaskID).hide();
        $.Id(MessageTextDivID).hide();
    });

    var returnToPage = function() {
        $(this).hide();
        $.Id(MessageTextDivID).hide();
    }
    //if mask is clicked   
    $.Id(MaskID).click(returnToPage);

};



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);
}

$(document).ready(function(e) {

    var loc = window.location.href;

    var MFHRootURL = getBaseURL();
    var prefixExclusions = [];
    prefixExclusions.push("http://billinfo.leg.state.fl.us");
    prefixExclusions.push("http://billinfo");
    prefixExclusions.push("http://199.44.254.194");
    prefixExclusions.push("http://addthis.com");
    prefixExclusions.push("http://www.addthis.com");
    prefixExclusions.push("http://www.myfloridahouse.gov");
    prefixExclusions.push("http://myfloridahouse.gov");
    prefixExclusions.push(MFHRootURL);
    prefixExclusions.push("http://www.leg.state.fl.us");
    prefixExclusions.push("http://streams.leg.state.fl.us");
    prefixExclusions.push("http://m.myfloridahouse.gov");
    prefixExclusions.push("http://mfhmobile.gov");
    prefixExclusions.push("http://mfhmobile.com");
    prefixExclusions.push("http://www.mfhmobile.com")

    var MFHRootURL = getBaseURL();
    MFHRootURL = MFHRootURL.substring(0, MFHRootURL.length);
    var finalselector = function(e) {
        var anchor = $(this);
        if ($.IsUndefinedNullOrEmptyString(anchor.attr("href"))) return;
        var exclude = false;
        if (anchor.attr("href").startsWith("http")) {
            $(prefixExclusions).each(function() {
                if (anchor.attr("href").toUpperCase().indexOf(this.toUpperCase()) > -1) {
                    exclude = true;
                    return;
                }
            });
            if ((loc.toUpperCase().indexOf('/SECTIONS/VIDEOS/VIDEOARCHIVE.ASPX') > 0 && anchor.attr('class') == "videoPageLink") ||
                (loc.toUpperCase().indexOf('/SECTIONS/INTHENEWS/INTHENEWSARCHIVE.ASPX') > 0 && anchor.attr('class') == "InTheNewsLink") ||
                anchor.attr('class') == "ForwardedLink") {
                exclude = true;
            }
            if (!exclude) showForwardingModalMessagePanel(e, anchor);
        }
    }
    $("body").delegate('a', "click", finalselector);

    $("body").delegate('a', "click", finalselector);
    var WebTrendsSelector = function(e) {
        var anchor = $(this);
        var href = anchor.attr("href");
        if (href.indexOf(".pdf") > -1) {
            dcsMultiTrack('DCS.dcsuri',
                     href,
                      'WT.ti',
                      href)
        }
    }
    $("body").delegate('a', "click", WebTrendsSelector);



});



