﻿jQuery.noConflict();

// -----------------------------------------------------------------------------------------------------
//  Foxbright CMS commonly-used functions v1.0
//  Created: 2009-09-10 (AW)
// -----------------------------------------------------------------------------------------------------


// --[ displayCurrentYear() ]---------------------------------------------------------------------------

  // Params: None
  // Return vals: current 4-digit year
  // Notes: Typically used for copyright year

  function displayCurrentYear(){
    var currentDate = new Date();
    var currentYear = currentDate.getYear();
    currentYear = (currentYear < 1000) ? currentYear += 1900 : currentYear;
    document.write(currentYear);
  }



// --[ openPageSelectWindow() ]-------------------------------------------------------------------------

  // Params: output_field
  // Return vals:
  // Notes: Unknown usage

  function openPageSelectWindow(output_field){
    var url = '/core/admin/page/PageSelectDialog.htm';
    var window_name = 'Select_A_Page';
    var attributes = 'width=425,height=400';

    window.urlreturnparam = output_field;
    window.open( url, window_name, attributes );
  }


// --[ removeCharacters() ]-------------------------------------------------------------------------

  // Params:
  //   Required: words (string)
  //   Required: character (string to remove)
  // Return vals: words (string with character removed)
  // Notes: Remove all instances of character from words

  function removeCharacters(parameters){
    words = parameters['words'];
    character = (typeof(parameters['character']) == 'undefined') ? ' ' : parameters['character'];
    var chars = words.length;
    for(var x = 0; x<chars; x++){
     words = words.replace(character, "");
    }
    return words;
  }


// --[ removeCharacters() ]-------------------------------------------------------------------------

  // Params:
  //   None.
  // Return vals: true if this page is an editor, false otherwise.

  function isInEditor(){
    if (jQuery('#editor').length > 0){
      return true;
    }
    return false;
  }

// --[ html Encode/Decode ]-------------------------------------------------------------------------
  // Params:
  //    text - the text to encode/decode.
  // Returns:
  //    the text, encoded or decoded.

    function fb_htmlEncode(text)
    {
        return jQuery('<div/>').text(text).html();
    }//end fb_htmlEncode

    function fb_htmlDecode(text)
    {
        return jQuery('<div/>').html(text).text();
    }//end fb_htmlDecode

// --[ hideIfEmpty() ]-------------------------------------------------------------------------

  // Params:
  //   Required: content_selectors = jQuery Selector for elements to search for content.
  //                e.g. "#rightCol .contentArea"
  //   Optional: hide_selectors = jQuery Selector for elements to hide.
  //                (If null, the content selectors will be used.) e.g. "#rightCol .contentArea"
  //   Optional: hide_behavior = 'all'|'any'|'each'
  //                all - hide the hide_selector elements iff ALL of the content_selector elements are empty.
  //                        [default if the hide_selector parameter is present ]
  //                any - hide the hide_selector elements if ANY of the content_selector elements are empty.
  //                each - hide each content_selector element if it is found to by empty. Ignores the
  //                        ignores the hide_selector parameter.
  //                        [default if no hide_selector parameter is present ]
  // Return vals: None
  // Notes: Hides empty elements

    function hideIfEmpty(parameters){

        if ( isInEditor() )
        {
            return;
        }

        if(typeof(parameters['content_selectors']) == 'undefined'){
            return;
        }

        var content_selectors = parameters['content_selectors'];
        var hide_behavior = (typeof(parameters['hide_behavior']) == 'undefined') ? 'all' : parameters['hide_behavior'];
        if (typeof(parameters['hide_selectors']) == 'undefined')
        {
            // if there is no hide_selectors parameter, default behavior to 'each'
            hide_behavior = 'each';
        }
        var hide_selectors = (typeof(parameters['hide_selectors']) == 'undefined') ? content_selectors : parameters['hide_selectors'];

        var hide_stuff = false;

        for (var i = 0; i < content_selectors.length; ++i)
        {
            if(jQuery(content_selectors[i]).length == 0)
            {
                continue;
            }

            var contentAreasArr = jQuery(content_selectors[i]);
            for (var idx = 0; idx < contentAreasArr.length; idx++)
            {
                if ( jQuery(contentAreasArr[idx]).html().replace(/\s*/g, '').length == 0 )
                {
                    hide_stuff = true;
                    if (hide_behavior == 'any')
                    {
                        break;
                    }
                    else if (hide_behavior == 'each')
                    {
                        jQuery(contentAreasArr[idx]).hide();
                    }
                }
                else if (hide_behavior == 'all')
                {
                    hide_stuff = false;
                    break;
                }
            }//endfor

            if (hide_behavior == 'any' && hide_stuff == true)
            {
                break;
            }
            else if (hide_behavior == 'all' && hide_stuff == false)
            {
                break;
            }
        }//endfor

        if (hide_stuff && hide_behavior != 'each')
        {
            for (var i = 0; i < hide_selectors.length; ++i)
            {
                jQuery(hide_selectors[i]).hide();
            }//endfor
        }
    } //end hideIfEmpty


// --[ addSearchBoxTriggers() ]-------------------------------------------------------------------------

  // Example calls:
  //    addSearchBoxTriggers({});
  //    addSearchBoxTriggers({ fieldId : 'myField', defaultText : 'enter search criteria' });
  // Params:
  //    Optional: ID of search field
  //    Optional: Default text
  //    Optional: CSS class for empty field
  // Return vals: None
  // Notes: Clear search field on focus if empty / return to default state on blur if empty

  function addSearchBoxTriggers(parameters){

    fieldId = (typeof(parameters['fieldId']) == 'undefined') ? 'search_string' : parameters['fieldId'];
    defaultText = (typeof(parameters['defaultText']) == 'undefined') ? '' : parameters['defaultText'];
    emptyClass = (typeof(parameters['emptyClass']) == 'undefined') ? 'empty' : parameters['emptyClass'];

    jQuery("#"+fieldId).focus(function(){
      jQuery(this).removeClass(emptyClass);
      if(jQuery(this).val() == defaultText){
        jQuery(this).val('');
      }
    }).blur(function() {
      if(removeCharacters({ words : jQuery(this).val() }) == ''){
        jQuery(this).addClass('empty');
        jQuery(this).val(defaultText);
      }
    });

    // Ensure empty class is removed if page is refreshed while search field contains text
    if(jQuery("#"+fieldId).val() != defaultText){
      jQuery("#"+fieldId).removeClass(emptyClass);
    }

  }


// --[ oldBrowserWarning() ]-------------------------------------------------------------------------

  // Params: force (optional) - Set this to any value to force display even in modern browser (for testing purposes)
  // Return vals: None
  // Notes: Display warning if browser is old

  function oldBrowserWarning(force){
    
    var BV = parseFloat(navigator.appVersion.indexOf("MSIE")>0?navigator.appVersion.split(";")[1].substr(6):navigator.appVersion);
    var IE=(navigator.appName.indexOf('Explorer')!=-1);
    if((IE && BV < 7) || typeof force != 'undefined'){
      var oldBrowserWarningHTML = "";
      oldBrowserWarningHTML += "<div id='FB_oldBrowserWarning'>";
        oldBrowserWarningHTML += "<div id='FB_oldBrowserWarning_close'></div>";
        oldBrowserWarningHTML += "<div id='FB_oldBrowserWarningContent'>";
          oldBrowserWarningHTML += "<div id='FB_oldBrowserWarningText'>";
            oldBrowserWarningHTML += "<h1>You are using an outdated browser</h1>";
            oldBrowserWarningHTML += "<h2>For a better experience using this site, please upgrade to a modern web browser.</h2>";
          oldBrowserWarningHTML += "</div>";
          oldBrowserWarningHTML += "<div id='FB_oldBrowserWarningBrowserIcons'>";
            oldBrowserWarningHTML += "<a title='Get Internet Explorer 7+' href='http://www.browserforthebetter.com/download.html' target='_blank'></a>";
            oldBrowserWarningHTML += "<a title='Get Firefox 3+' href='http://www.firefox.com/' target='_blank'></a>";
            oldBrowserWarningHTML += "<a title='Get Safari 3+' href='http://www.apple.com/safari/download/' target='_blank'></a>";
            oldBrowserWarningHTML += "<a title='Get Google Chrome 2+' href='http://www.google.com/chrome' target='_blank'></a>";
          oldBrowserWarningHTML += "</div>";
        oldBrowserWarningHTML += "</div>";
      oldBrowserWarningHTML += "</div>";

      var cssLink = jQuery('<link />').appendTo(jQuery('head'));
      cssLink.attr({
        rel: "stylesheet",
        href: "/core/oldBrowserWarning/oldBrowserWarning.css"
      });
      jQuery('body').prepend(oldBrowserWarningHTML);

      jQuery('#FB_oldBrowserWarning_close').click(function(){
        jQuery('#FB_oldBrowserWarning').remove();
      });
    }

  }


// --[ ADMIN: oldBrowserWarning() ]-------------------------------------------------------------------------

    // Requires: /core/js/BrowserDetect.js is run first
  // Params: None
  // Return vals: None
  // Notes: Display warning (in admin) if browser is old

    function admin_oldBrowserWarning(){

        var validBrowser = "fail";

        switch(BrowserDetect.browser.toLowerCase()){
            case 'firefox':
              if(eval(BrowserDetect.version) >= 3.5){
                    validBrowser = "pass";
                }
                break;
            case 'explorer':
              if(eval(BrowserDetect.version) >= 7){
                    validBrowser = "pass";
                }
                break;
            default:
                // validBrowser = "fail";
        }

        if(validBrowser != "pass"){ // negative test as IE6 comes up with "undefined"

            var unsupportedBrowserHTML = "";

            unsupportedBrowserHTML += "<div style='border: 1px solid #F7941D; text-align: center; clear: both; height: 65px; position: relative; background: #FEEFDA url(/core/img/ie6nomore-warning.jpg) 100% 0 no-repeat;'>";
                unsupportedBrowserHTML += "<div style='text-align: center; padding: 0; overflow: hidden; color: black;'>";

                    unsupportedBrowserHTML += "<div style='font-family: Arial, sans-serif;'>";
                        unsupportedBrowserHTML += "<div style='font-size: 14px; font-weight: bold; margin-top: 12px;'>PLEASE NOTE: You are accessing the Admin Panel using an unsupported browser/version</div>";
                        unsupportedBrowserHTML += "<div style='font-size: 12px; font-weight: bold; margin-top: 6px; line-height: 12px;'>Please refer to documentation for admin panel browser support</div>";
                    unsupportedBrowserHTML += "</div>";
                unsupportedBrowserHTML += "</div>";
            unsupportedBrowserHTML += "</div>";

            jQuery("body").prepend(unsupportedBrowserHTML);

        }

  }

// --[ Truncate text ]-------------------------------------------------------------------------

  jQuery.fn.truncate = function( max, settings ) {
    settings = jQuery.extend( {
      chars: /\s/,
      trail: [ "...", "" ]
    }, settings );
    var myResults = {};
    var ie = jQuery.browser.msie;
    function fixIE( o ) {
      if ( ie ) {
        o.style.removeAttribute( "filter" );
      }
    }
    return this.each( function() {
      var jQuerythis = jQuery(this);
      var myStrOrig = jQuerythis.html().replace( /\r\n/gim, "" );
      var myStr = myStrOrig;
      var myRegEx = /<\/?[^<>]*\/?>/gim;
      var myRegExArray;
      var myRegExHash = {};
      var myResultsKey = jQuery("*").index( this );
      while ( ( myRegExArray = myRegEx.exec( myStr ) ) != null ) {
        myRegExHash[ myRegExArray.index ] = myRegExArray[ 0 ];
      }
      myStr = jQuery.trim( myStr.split( myRegEx ).join( "" ) );
      if ( myStr.length > max ) {
        var c;
        while ( max < myStr.length ) {
          c = myStr.charAt( max );
          if ( c.match( settings.chars ) ) {
            myStr = myStr.substring( 0, max );
            break;
          }
          max--;
        }
        if ( myStrOrig.search( myRegEx ) != -1 ) {
          var endCap = 0;
          for ( eachEl in myRegExHash ) {
            myStr = [ myStr.substring( 0, eachEl ), myRegExHash[ eachEl ], myStr.substring( eachEl, myStr.length ) ].join( "" );
            if ( eachEl < myStr.length ) {
              endCap = myStr.length;
            }
          }
          jQuerythis.html( [ myStr.substring( 0, endCap ), myStr.substring( endCap, myStr.length ).replace( /<(\w+)[^>]*>.*<\/\1>/gim, "" ).replace( /<(br|hr|img|input)[^<>]*\/?>/gim, "" ) ].join( "" ) );
        } else {
          jQuerythis.html( myStr );
        }
        myResults[ myResultsKey ] = myStrOrig;
        jQuerythis.html( [ "<div class='truncate_less'>", jQuerythis.html(), settings.trail[ 0 ], "</div>" ].join( "" ) )
        .find(".truncate_show",this).click( function() {
          if ( jQuerythis.find( ".truncate_more" ).length == 0 ) {
            jQuerythis.append( [ "<div class='truncate_more' style='display: none;'>", myResults[ myResultsKey ], settings.trail[ 1 ], "</div>" ].join( "" ) )
            .find( ".truncate_hide" ).click( function() {
              jQuerythis.find( ".truncate_more" ).css( "background", "#fff" ).fadeOut( "normal", function() {
                jQuerythis.find( ".truncate_less" ).css( "background", "#fff" ).fadeIn( "normal", function() {
                  fixIE( this );
                  jQuery(this).css( "background", "none" );
                });
                fixIE( this );
              });
              return false;
            });
          }
          jQuerythis.find( ".truncate_less" ).fadeOut( "normal", function() {
            jQuerythis.find( ".truncate_more" ).fadeIn( "normal", function() {
              fixIE( this );
            });
            fixIE( this );
          });
          jQuery(".truncate_show",jQuerythis).click( function() {
            jQuerythis.find( ".truncate_less" ).css( "background", "#fff" ).fadeOut( "normal", function() {
              jQuerythis.find( ".truncate_more" ).css( "background", "#fff" ).fadeIn( "normal", function() {
                fixIE( this );
                jQuery(this).css( "background", "none" );
              });
              fixIE( this );
            });
            return false;
          });
          return false;
        });
      }
    });
  };


// --[ MM_findObj() ]---------------------------------------------------------------------------

  // Params: element ID
  // Return vals: object
  // Notes: Legacy. MM_findObj() was originally a (Macromedia) Dream Weaver function which
  //        dealt with browser inconsistencies.  Replaced contents with document.getElementById,
  //        which is now standard, but function name is still referenced in older scripts.

  function MM_findObj(obj){
    return document.getElementById(obj);
  }


// --[ jQuery Timer Plugin ]---------------------------------------------------------------------------

  /*
   * jQuery Timer Plugin
   * http://www.evanbot.com/article/jquery-timer-plugin/23
   *
   * @version      1.0
   * @copyright    2009 Evan Byrne (http://www.evanbot.com)
   */

  jQuery.timer = function(time,func,callback){
    var a = {timer:setTimeout(func,time),callback:null}
    if(typeof(callback) == 'function'){a.callback = callback;}
    return a;
  };

  jQuery.clearTimer = function(a){
    clearTimeout(a.timer);
    if(typeof(a.callback) == 'function'){a.callback();};
    return this;
  };


// --[ setQuickJump() ]---------------------------------------------------------------------------


  function setQuickJump(parameters){

    // Example calls:
    //    setQuickJump({});
    //    setQuickJump({ quickJumpSpeed : 'slow', quickJumpDelay : 300 });
    // Params:
    //    Optional: ID of quick jump button
    //    Optional: ID of quick jump drop menu
    //    Optional: miliseconds before QJD hides
    //    Optional: miliseconds QJD hide/show transition takes
    // Return vals: none
    // Notes: Adds show/hide triggers to quick jump menu
    // Requires: jQuery.timer, jQuery.clearTimer

    // Defaults
    var quickJumpButton = (typeof(parameters['quickJumpButton']) == 'undefined') ? 'quickJumpButton' : parameters['quickJumpButton'];
    var quickJumpDrop = (typeof(parameters['quickJumpDrop']) == 'undefined') ? 'quickJumpDrop' : parameters['quickJumpDrop'];
    var quickJumpDelay = (typeof(parameters['quickJumpDelay']) == 'undefined') ? 300 : parameters['quickJumpDelay'];
    var quickJumpSpeed = (typeof(parameters['quickJumpSpeed']) == 'undefined') ? 0 : parameters['quickJumpSpeed'];
    var quickJumpTimer = null;

    jQuery("#"+quickJumpButton).click(
      function(){

        if(jQuery("#"+quickJumpDrop).is(':hidden')){
          jQuery("#"+quickJumpDrop).slideDown(quickJumpSpeed);
        }
        else{
          jQuery("#"+quickJumpDrop).slideUp(quickJumpSpeed);
        }
      }
    );

    jQuery("#"+quickJumpButton).hover(
      function(){
        if(quickJumpTimer != null){jQuery.clearTimer(quickJumpTimer);}
      },
      function(){
        quickJumpTimer = jQuery.timer(quickJumpDelay,function(){
          jQuery("#"+quickJumpDrop).slideUp(quickJumpSpeed);
        });
      }
    );

    jQuery("#"+quickJumpDrop).hover(
      function(){
        if(quickJumpTimer != null){jQuery.clearTimer(quickJumpTimer);}
      },
      function(){
        quickJumpTimer = jQuery.timer(quickJumpDelay,function(){
          jQuery("#"+quickJumpDrop).slideUp(quickJumpSpeed);
        });
      }
    );

  }


var fb_js_debug = false;

function debug_out()
{
    if (fb_js_debug == true)
    {
        if (window.console && window.console.log)
        {
            window.console.log(Array.prototype.join.call(arguments,', '));
        }
    }
};

/*
==========================================================================================
*/

  function coreJS(){
  }

  coreJS.isIE = function(){
    if (jQuery.browser.msie) {
      return parseInt(jQuery.browser.version, 10);
    }
    else{
      return false;
    }
  }


  coreJS.isHome = function(){
    if(jQuery('body.home').length > 0) {
      return true;
    }
    else{
      return false;
    }
  }


/*
==========================================================================================
*/


/*
======[RUN DEBUG IF REQUESTED]============================================================
*/

jQuery('document').ready(function () {

  if (typeof runDBug != 'undefined') { // if var runDBug is defined
    if(runDBug == true){ // if debug is set to run
      jQuery('<script language="javascript" type="text/javascript" src="/core/js/debug.js"></script>').appendTo(jQuery('head'));
    } // if debug is true
  } // if debug is defined

  // Run debug() when debug.js is loaded
  DBug_wait();

});

function DBug_wait() {
  if (typeof DBugLoaded == 'undefined') {  
    window.setTimeout(DBug_wait,100);
  }
  // If debug.js is loaded
  else {
    // Make sure body is loaded
    jQuery('document').ready(function () {
      var t=setTimeout("debugTests()",3000);
    });
  }
}

/*
======[END RUN DEBUG IF REQUESTED]========================================================
*/

