
/* methods */

/* fixing fades in IE by over-writing the std jquery functions */

jQuery.fn.fadeIn = function(speed, callback) { 
    return this.animate({opacity: 'show'}, speed, function() { 
        if (jQuery.browser.msie)  
            this.style.removeAttribute('filter');  
        if (jQuery.isFunction(callback)) 
            callback();  
    }); 
}; 
 
jQuery.fn.fadeOut = function(speed, callback) { 
    return this.animate({opacity: 'hide'}, speed, function() { 
        if (jQuery.browser.msie)  
            this.style.removeAttribute('filter');  
        if (jQuery.isFunction(callback)) 
            callback();  
    }); 
}; 
 
jQuery.fn.fadeTo = function(speed,to,callback) { 
    return this.animate({opacity: to}, speed, function() { 
        if (to == 1 && jQuery.browser.msie)  
            this.style.removeAttribute('filter');  
        if (jQuery.isFunction(callback)) 
            callback();  
    }); 
}; 

/* end of fade fix */

jQuery.fn.swapClass = function(outclass,inclass){
  return this.each(
    function(){
      if ($(this).hasClass(outclass)) {
        $(this).removeClass(outclass) ;
	 if ( !$(this).hasClass(inclass) ) { $(this).addClass(inclass) }
      }
    }
  );
};

jQuery.fn.setHoverClass = function(hvrclass){
    return this.each(
      function(){
        $(this).hover(
          function(){ $(this).addClass(hvrclass) ; },
          function(){ $(this).removeClass(hvrclass) ; }
        );
      }
    ) ;
};

jQuery.fn.setHoverFade = function(){
    return this.each(
      function(){
        $(this).hover(
          function(){ $(this).fadeTo('slow',.65); },
          function(){ $(this).fadeTo('slow',1); }
        );
      }
    ) ;
};

jQuery.fn.setTabSlide = function(){
    return this.each(
      function(){
	     var thisId = '#' + this.id ;
        var tabid = '#tab-' + thisId.substr(4) ;
        $(thisId).toggle(
          function(){
        	 var togmarker = thisId + ' span.accordinator' ;
                $(tabid).slideUp('normal') ;
                $(togmarker).text('+') ;
          },	     
          function(){
        	 var togmarker = thisId + ' span.accordinator' ;
                $(tabid).slideDown('normal') ;
                $(togmarker).html('&ndash;') ;
          }	     
        );

      }
	 ) ;
};

jQuery.fn.setFocusNoSee = function(){
  return this.each(
    function(){
      $(this).click(
        function(){
          if ('hidefocus' in this) {
            this.hidefocus = true ;
          } else {
            $(this).blur() ;
          }
        }
      );
    }
  ) ;
} ;

jQuery.fn.labelFor = function(){
  return this.each(
    function() {
      if ( $('input',this).attr('type') == 'radio' || $('input',this).attr('type') == 'checkbox' ) {
        $(this).attr('for', $('input',this).attr('id') ) ;
      }
    }
  );
};

jQuery.fn.pngIE = function(reinit) {
   
  return this.each(
    function(){
     var $this = $(this) ;
     if (reinit == true) {
       var reimg = new Image() ;
	reimg.src = $this.data('osrc') ;
       this.src = reimg.src ;
	this.width = reimg.width ;
     }
      $this.data('osrc',this.src) ;
      $this.data('owidth',this.width) ;
      this.filters.item(gp.alphaloader).src = $this.data('osrc') ;
      if ( $this.hasClass('ieScale') ) { this.filters.item(gp.alphaloader).sizingMethod = 'scale' ; }
      this.src = gp.ieimgsub ;
      this.width = $this.data('owidth') ;
    }
  );
};

jQuery.fn.showAtPointer = function(ev){
  var evt = (ev) ? ev : window.event ;
  
  return this.each(
    function() {
      var popX = evt.pageX ;
      var popY = evt.pageY ;
      alert(popX) ;
      $(this).css({'top': popY, 'left' : popX}) ;
    }
  );
};

jQuery.fn.infoPop = function(ev){
   var evt = (ev) ? ev : window.event ;

  return this.each(
    function() {

      var ipopitemid = $(this).attr('name') ;
      var $ipopitem = $('#' + ipopitemid + 'ip') ;
      var $ipopshell = $('#infopop') ;

      if ( $('#infopop').is(':visible') ) {
        $('#infopop').hide() ;
	 if ( $ipopshell.data('previpop') == ipopitemid ) { return }
      }
      
      var $ipopfill = $('#infopopfill') ;
      var popX = evt.pageX ;
      var popY = evt.pageY ;
      var offX = ( $.isProp(gp.ipopOffsetWidth) ) ? gp.ipopOffsetWidth : $ipopshell.outerWidth() ;
      
      if ( popX + offX > $(window).width() ) { popX -= offX }
      
      $ipopfill.html( $ipopitem.clone(true) ) ;
      $ipopshell.data('previpop',ipopitemid) ;
      $('.ipopfill .hwrp').attr('title','Exit').css('cursor','pointer').click( function(){ $('#infopop').hide(); } );

      $ipopshell.css({'top': popY, 'left' : popX}).show('normal') ;
    }
  );
};

jQuery.fn.setClickInfo = function(displayid){
  return this.each(function(){
    $(this).click(
      function(){
        var itemid = this.id ;
        var infoselector = '#' + itemid + '-info' ;
        var loc = $(this).offset() ;
        var wid = $(this).outerWidth() ;
        var cssobj = {left:loc.left+wid, top:loc.top-12}
        $(displayid + ' div.tbfill').html( $(infoselector).html() ) ;
        if ( !$.support.opacity ) {
          $(displayid).css(cssobj).show() ;
        } else {
          $(displayid).css(cssobj).show('normal') ;
        }
      }
    );
  });
};

jQuery.fn.addSetBG = function() {
  return this.each(function(){
    this.setBG = function(fname){
      if (this.style.backgroundImage != $.cssurlwrap(gp.bgdir + fname)) { this.style.backgroundImage = $.cssurlwrap(gp.bgdir + fname) } ;
    }
  });
};

(function($){
  jQuery.fn.lightBox = function() {
    return $(function(){
      $('body').append('<div class="lightbox">&nbsp;</div>') ;
      $('div.lightbox').css({height:$(window).height(),width:$(window).width()}).show() ;
    });
  };
})(jQuery);

(function($){
  jQuery.fn.placement = function() {
    return this.each(function(){
      var args = jQuery.fn.placement.arguments ;
      var pos = {top:'',left:''}
      var defaults = {top:'',left:''}
      defaults.left = Math.round( ($(window).width() - $(this).width())/2 ) ;
      defaults.top = Math.round( ($(window).height() - $(this).height())/2 ) ;
      if ( defaults.top < 0 ) { defaults.top = 24 }
      
      switch ( args.length ) { 
        case 0:
          pos.top = defaults.top ;
          pos.left = defaults.left ;
          break;
        case 1:
          pos.top = args[0] ;
          pos.left = defaults.left ;
          break;
        case 2:
          pos.top = args[0] ;
          pos.left = args[1] ;
          break;
        default:
          pos.top = args[0] ;
          pos.left = args[1] ;
          break;
      }
      
      $(this).css(pos) ;
    });
  };
})(jQuery);

jQuery.fn.wipe = function() {
  $('div.lightbox').remove();
  return this.each(function(){
    $(this).empty().hide() ;
  });
};


/* functions */

jQuery.switchBG = function(el) {
  if ( typeof(gp.bgindex) == 'undefined' ) { 
    gp.bgindex = 0 ;
    for (var i = 0; i < gp.bg.length; i++) {
      if ( gp.bg[i] == cC.crumbs.currentBG.value ) {
        gp.bgindex = i ;
        break ;
      }
    }
  }
  if (el.value == 'next' || el.value == '&gt;') { gp.bgindex ++ ; }
  if (el.value == 'prev' || el.value == '&lt;') { gp.bgindex -- ; }

  if (gp.bgindex >= gp.bg.length) { gp.bgindex = 0 ; }
  if (gp.bgindex < 0) { gp.bgindex = gp.bg.length-1 ; }

  cC.crumbs.currentBG.update(gp.bg[gp.bgindex]) ;
};

jQuery.cssurlwrap = function(fname) {
  return "url(" + fname + ")" ;
} ;

jQuery.isProp = function(objprop) { 
  if ( (typeof objprop == 'function') || (typeof objprop == 'undefined') ) { return false } ;
  return true ;
};
   

jQuery.popblock = function(e) {
  var popobjid = 'tab-' + e.id.substr(e.id.indexOf('-')+1) ;
  if (e.childNodes) {
    var accordiansymbol = (e.childNodes[0].className == 'accordinator') ? e.childNodes[0] : false ;
  } else {
    var accordiansymbol = false ;
  }
  var ee = document.getElementById(popobjid) ;
  if (ee.style.display == '') {ee.style.display = 'none'}
  if (ee.style.display == 'none') {
   ee.style.display = 'block' ;
   if (accordiansymbol) { accordiansymbol.innerHTML = "&ndash;" }
  } else {
   ee.style.display = 'none' ;
   if (accordiansymbol) { accordiansymbol.innerHTML = "+" }
  }
};


jQuery.popblock2 = function(e) {
  var popobjid = 'tab-' + e.id.substr(e.id.indexOf('-')+1) ;
  var ee = document.getElementById(popobjid) ;
  if (ee.style.display == '') {
    if (ee.offsetHeight > 0) {ee.style.display = 'block'} else {ee.style.display = 'none'} ;
  }
  if (ee.style.display == 'none') {
   ee.style.display = 'block' ;
  } else {
   ee.style.display = 'none' ;
  }
};





jQuery.usd = {

  usd : function(dollars){
    return dollars.toFixed(2) ;
  },

  fCalc : function(dollars,rate){
    return ( parseFloat( dollars ) * parseFloat( rate ) ).toFixed(2) ;
  },

  iCalc : function(dollars,quantity){
    return ( parseFloat( dollars ) * parseInt( quantity ) ).toFixed(2) ;
  },

  sum : function(){
    var args = jQuery.usd.sum.arguments ;
    var total = 0 ;
    if (args.length) {
      for (var i = 0; i < args.length; i++) {
        total += parseFloat(args[i]) ;
      }
    }
    return total.toFixed(2) ;
  },

  less : function(){
    var args = jQuery.usd.less.arguments ;
    var total = (args.length) ? args[0] : 0 ;
    if (args.length > 1) {
      for (var i = 1; i < args.length; i++) {
        total -= parseFloat(args[i]) ;
      }
    } else if (args.length == 1) {
       total = args[0] ;
    } else {
       total = 0 ;
    }
    return total.toFixed(2) ;
  }
  
};

jQuery.insertAtCaret = function(areaId,text) { 
  var txtarea = document.getElementById(areaId);
  //alert(txtarea.tagName) ;
  var scrollPos = txtarea.scrollTop; 
  var strPos = 0;
  var br = ((txtarea.selectionStart || txtarea.selectionStart == '0') ? "ff" : (document.selection ? "ie" : false ) );
  if (br == "ie") { 
    txtarea.focus();
    var range = document.selection.createRange();
    range.moveStart ('character', -txtarea.value.length);
    strPos = range.text.length;
  } else if (br == "ff") {
    strPos = txtarea.selectionStart;
  }
  
  var front = (txtarea.value).substring(0,strPos);
  var back = (txtarea.value).substring(strPos,txtarea.value.length);
  txtarea.value=front+text+back;
  strPos = strPos + text.length;
  
  if (br == "ie") { 
    txtarea.focus();
    var range = document.selection.createRange();
    range.moveStart ('character', -txtarea.value.length);
    range.moveStart ('character', strPos);
    range.moveEnd ('character', 0);
    range.select();
  } else if (br == "ff") { 
    txtarea.selectionStart = strPos;
    txtarea.selectionEnd = strPos;
    txtarea.focus();
  } 
  
  txtarea.scrollTop = scrollPos;
  
};
