﻿
    
function isIE6() {
	if ($.browser.msie && $.browser.version.substr(0, 1) < 7)
	    return true;
	return false;
}

function toggleFade(jEl, callback) {
	if (jEl.is(":visible"))
	    if ( isIE6() )
	        jEl.hide();
	    else
		    jEl.fadeOut(300, callback);
	else
	    if ( isIE6() )
	        jEl.show();
	    else
		    jEl.fadeIn(400, callback);
}

    
// online publication index display in content
function putArrowsOnIndex(rootExpr, ShowIndexExpanded) {
    if ( $(rootExpr).size() == 0 )
        return;
    function ifIE6fixPngImg(elem) {
      //to be replaced with something better
    }
    //use images
    var arrows = $(rootExpr+" ul.level li.name");// img.arrow");
    arrows.each(function() {
        var jEl = $(this);
        var arrow = jEl.find("img.arrow:first");
        var children = jEl.find("+li.children");
        if (children.length > 0) {
            arrow.click(function() {
                arrow.toggleClass("arrow2");
                //alert("img class: "+arrow.attr("class")+" and has arrow2? "+arrow.hasClass("arrow2"));
                if (arrow.hasClass("arrow2"))
                    arrow.attr("src", "/common/images/arrow_expanded.png");
                else
                    arrow.attr("src", "/common/images/arrow_collapsed.png");
                ifIE6fixPngImg(arrow);
                toggleFade(children);
            });
            if ( ShowIndexExpanded ) {
                arrow.addClass("arrow2");
                arrow.attr("src", "/common/images/arrow_expanded.png");
                ifIE6fixPngImg(arrow);
                children.show();
            } else { 
                arrow.removeClass("arrow2");
                arrow.attr("src", "/common/images/arrow_collapsed.png");
                ifIE6fixPngImg(arrow);
                children.hide();
            } 
        } else {
            arrow.addClass("noarrow");
            arrow.attr("src", "/common/images/spacer.gif");
            ifIE6remPngFilter(arrow);
            children.hide();
        }
    });
    //expand first level
    var first = $(rootExpr+" ul.level:first li.name:first");
    first.addClass("name_first");
    var children = first.find("+li.children");
    var arrow = first.find("img.arrow:first");
    if ( children.length>0 ) {
        arrow.attr("src", "/common/images/arrow_expanded.png");
        arrow.addClass("arrow2");
        ifIE6fixPngImg(arrow);
        children.show();
        //expand second level
        var lvl2 = children.find("> ul.level > li.name").each(function(){
            var jEl = $(this);
            var arrow = jEl.find("img.arrow:first");
            var children = jEl.find("+li.children");
            if (children.length > 0) {
                arrow.attr("src", "/common/images/arrow_expanded.png");
                arrow.addClass("arrow2");
                ifIE6fixPngImg(arrow);
                children.show();
            }
        });
    }
}

//breadcrumbs script: show breadcrumbs full only if on one row, otherwise 
//show less and have option to popup the rest
$.fn.limitBreadcrumbs = function(o){
  o = o || {};
	return this.each(function() {
    var ul = $(this);
    var lis = ul.find("li").toArray().reverse();
    var ulWidth = ul.width();
    var liWidth = 0;
    var isTooLong = false;
    for( i=0; i<lis.length; i++) {
      liWidth += $(lis[i]).width();
      if ( liWidth > ulWidth ) {
        //should hide some of them
        isTooLong = true;
        break;
      }
    }
    //alert("is too long? "+isTooLong);
    if ( isTooLong ) {
      var liStart = ul.find("li.start");
      var liEtc = ul.find("li.etc");
      liEtc.show();
      var etcLinks = "";
      liWidth = liStart.width()+liEtc.width();
      var bDel = false;
      for ( i=0; i<lis.length; i++ ) {
        var li = $(lis[i]);
        if ( li.is(".etc") || li.is(".start") )
          continue;
        if ( !bDel && liWidth + li.width() > ulWidth ) {
          bDel = true;
          if ( li.is(".sep") && i>0 )
            $(lis[i-1]).hide();
        }
        if ( bDel ) {
          li.hide();
          //also add the hidden list to the click on the etc
          etcLinks = li.html()+etcLinks;
        } else
          liWidth+=li.width();
      }
      liEtc.qtip({
         content: etcLinks,
         position: {
            corner: {
               target: 'bottomLeft',
               tooltip: 'topLeft'
            }
         },
         hide: {
            fixed: true // Make it fixed so it can be hovered over
         },
         style: { 
            name: 'light' // Inherit from preset style
         }

      });
    }
  });
}  

var gsa_search = null;
gsa = function(o) {
    o = o||{};
    var SearchURL = o.SearchURL||"/common/Popups/GSASearch.aspx";
    var GSAHiddenBaseURL = o.GSAHiddenBaseURL || "#GSAHiddenBaseURL";
    $("div.GSABox").each(function(){
      var box = $(this);
      var GSALoading = box.find("div.GSALoading");
      var res = box.find("div.GSASearchResults");
      var searchInput = box.find("div.GSASearchBox input.GSASearchInput");
      var searchButton = box.find("div.GSASearchBox input.GSASearchButton");
      function activateLinks(responseText, textStatus, XMLHttpRequest) {
          GSALoading.fadeOut(100);
          var list = res.find("ul.GSAResultsList");
          if (list.size()==0) {
              res.html("No results found");
              return;
          }
          res.find("div.GSAPagingControls").each(function(){
            var GSABaseURL = getValue(GSAHiddenBaseURL);
            function getUrl(startIndex,pageSize) {
              return GSABaseURL+"&start="+startIndex+"&num="+pageSize;
            }
            var root = $(this);
            var startIndex = parseInt(root.attr("startindex"));
            var endIndex = parseInt(root.attr("endindex"));
            var totalCount = parseInt(root.attr("totalcount"));
            var pageSize = parseInt(root.attr("pagesize"));
            var prevStart = startIndex-1-pageSize;
            if ( prevStart<0 ) prevStart = 0;
            var lastStart = parseInt(totalCount/pageSize)*pageSize;
            var nextStart = startIndex-1+pageSize;
            if ( nextStart>totalCount ) nextStart = lastStart;
            root.find("a.GSAFirstPage").click(function(){ loadUrl2(getUrl(0,pageSize)); });
            root.find("a.GSAPrevPage").click(function(){ loadUrl2(getUrl(prevStart,pageSize)); });
            root.find("a.GSANextPage").click(function(){ loadUrl2(getUrl(nextStart,pageSize)); });
            root.find("a.GSALastPage").click(function(){ loadUrl2(getUrl(lastStart,pageSize)); });
            root.find("select.GSAPageSizeCombo").change(function(){
              var newPageSize = parseInt(this.options[this.selectedIndex].value);
              loadUrl2(getUrl(0,newPageSize));
            });
            root.find("select.GSAPageIndexCombo").change(function(){
              var pageIndex = parseInt(this.options[this.selectedIndex].value);
              loadUrl2(getUrl(pageIndex*pageSize,pageSize));
            });
          });
          
      }
      function doSearch() {
          var text = searchInput.val();
          if (text) {
              GSALoading.fadeIn(100);
              res.load(SearchURL+"?searchq=" + escape(text), activateLinks);
          }
      }
      gsa_search = doSearch;
      function hasValue(elem) {
          if (getValue(elem))
              return true;
          return false;
      }
      function getValue(elem) {
          var n = res.find(elem);
          if (n.size() > 0)
              n = n[0].value;
          else
              n = false;
          return n;
      }
      function loadUrl(elem) {
          var n = getValue(elem);
          if (n) {
              GSALoading.fadeIn(100);
              res.load(SearchURL, { searchurl: n }, activateLinks);
          }
      }
      function loadUrl2(url) {
          GSALoading.fadeIn(100);
          res.load(SearchURL, { searchurl:url }, activateLinks);
      }    
      function checkForEnter(event) {
          if (event.keyCode == 13) {
              doSearch();
              event.preventDefault();
              return false;
          }
      }
      searchInput.keydown(checkForEnter);
      searchButton.click(doSearch);
      doSearch();
    });
}
$.fn.advancedFilters = function(d) {
    if (d == 'expand') {
        $('a.expand-filters').css("display", "none");
        $('a.collapse-filters').css("display", "block");
        setSessionCookie("directoryadvfilters", "1");
    }
    else {
        $('a.expand-filters').css("display", "block");
        $('a.collapse-filters').css("display", "none");
        setSessionCookie("directoryadvfilters", "0");
    }
}
$.fn.applyTextDefault = function(tooltip) {
    var default_value = tooltip;
    if (tooltip == this[0].value)
        $(this).addClass("idleField");
       
    $(this).focus(function() {
        if (this.value == default_value) {
            this.value = '';
            $(this).removeClass("idleField");
        }
    });
    $(this).blur(function() {
        if (this.value == '') {
            $(this).addClass("idleField");
            this.value = default_value;
        }
    });
}
//check if any part of the element is in viewport
$.fn.isInView = function() {
    function getViewport() {
        var myWidth = 0, myHeight = 0;
        if( typeof( window.innerWidth ) == 'number' ) {
        //Non-IE
            myWidth = window.innerWidth;
            myHeight = window.innerHeight;
        } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
            //IE 6+ in 'standards compliant mode'
            myWidth = document.documentElement.clientWidth;
            myHeight = document.documentElement.clientHeight;
        } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
            //IE 4 compatible
            myWidth = document.body.clientWidth;
            myHeight = document.body.clientHeight;
        }
        var scrOfX = 0, scrOfY = 0;
        if( typeof( window.pageYOffset ) == 'number' ) {
            //Netscape compliant
            scrOfY = window.pageYOffset;
            scrOfX = window.pageXOffset;
        } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
            //DOM compliant
            scrOfY = document.body.scrollTop;
            scrOfX = document.body.scrollLeft;
        } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
            //IE6 standards compliant mode
            scrOfY = document.documentElement.scrollTop;
            scrOfX = document.documentElement.scrollLeft;
        }
        return {
            width: myWidth,
            height: myHeight,
            top: scrOfY,
            left: scrOfX
        }
    }
    
    var el = this.get(0);
    var top = el.offsetTop;
    var left = el.offsetLeft;
    var width = el.offsetWidth;
    var height = el.offsetHeight;
    //alert("1. top="+top+" left="+left+" width="+width+" height="+height);
    while(el.offsetParent) {
        el = el.offsetParent;
        top += el.offsetTop;
        left += el.offsetLeft;
    }
    //alert("2. top="+top+" left="+left+" width="+width+" height="+height);
    var view = getViewport();
    //alert("3. window.pageXOffset="+view.left+" window.pageYOffset="+view.top
    //    +" window.innerHeight="+view.height+" window.innerWidth="+view.width)
    return (
        top < (view.top + view.height) &&
        left < (view.left + view.width) &&
        (top + height) > view.top &&
        (left + width) > view.left
    );
}

//mechanism to authorize downloading of a file
function doSubscribePopup(id) {
    $.colorbox({ width: "378px", height: "385px", href: "/common/Popups/SubscribeForm.aspx?id=" + id, iframe:true, title:"Movie Download List" });
}
