/* common.js */

//slideshow
$(function() {
    $('#slideshow').cycle({
		timeout: 3000,
		delay: 1,
    speed: 500,
		prev:   '#prev',
    next:   '#next',
		before: onBefore,
		pager:      '#nav', 
    pagerEvent: 'mouseover', 
    fastOnEvent: true,
    pause: 1
		});
    function onBefore() {
        $('#title').html(this.alt);
    }
});

  
$('#pauseButton').click(function() { 
    $('#slideshow').cycle('pause'); 
});
$('#resumeButton').click(function() { 
    $('#slideshow').cycle('resume', true); 
});


//stand-alone trim functions
function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,'');
}
function ltrim(stringToTrim) {
	return stringToTrim.replace(/^\s+/,'');
}
function rtrim(stringToTrim) {
	return stringToTrim.replace(/\s+$/,'');
}

//validate email address
function emailAddr_Test( emailAddr, alertTxt ){
  var email_regex = new RegExp( '^[a-zA-Z0-9_+\-.]+@[a-zA-Z0-9_+\-.]+[.][a-zA-Z]{2,4}$' );
  with( emailAddr ){
   if( ! email_regex.test( trim( emailAddr ) ) ){
    alert( alertTxt );
    return false;
   }
   else {
   return true;
   }
  }
 }

function getKudos(){
 //do not run Kudos for IE6 users
 if ( /msie\s6/.test( navigator.userAgent.toLowerCase() ) ) { return false; }
 
 //send height info to ajax kudo handler
 heightInfo = {};
 heightInfo[ "CONTAINER" ] = $( '#container' ).height();
 
 var altHeight = parseInt( $( 'input[name=CONTAINERHEIGHT]' ).val() );
 
 if ( altHeight > 0 ) { heightInfo[ "CONTAINER" ] = altHeight; }
 
 heightInfo[ "NAVBAR" ] = $( '#navBar' ).height();

 $.post( '/ajax/kudoH.cfm', heightInfo, kudosReady, 'json' );
}

function kudosReady( kudoData ){
 $( '#kudos' ).html( kudoData['TEXT'] );
}

var parseID = function( idToParse ) { 
//var uPos = $.inArray( '_', idToParse );  this jquery function did not work in IE 8
var uPos = idToParse.indexOf( '_' );
 if( uPos > 0 ){
  return idToParse.substr( 0, uPos );
 }
 else { return; }
}

var toggleContainer = function( toggleOptID ) {
 //alert( 'toi = ' + toggleOptID );
 var moreImg = 'more.gif';
 var hideImg = 'hide.gif';
 if ( $( '#' + toggleOptID + '_moreHide' ).attr( 'class' ).match( /recommended/ ) ) {
    moreImg = 'recommended_show.gif';
    hideImg = 'recommended_hide.gif';
 }
 $( '#' + toggleOptID + '_container' ).slideToggle( 'slow' );
  if ( $( '#' + toggleOptID + '_moreHide' ) ){
   var src = ( $( '#' + toggleOptID + '_moreHide' ).attr( 'src' ) === '/images/zz_images/' + moreImg ) ? '/images/zz_images/' + hideImg : '/images/zz_images/' + moreImg ;
   $( '#' + toggleOptID + '_moreHide' ).attr( 'src', src );
  }
}

var toggleBasicContainer = function( toggleOptID ) {
 $( '#' + toggleOptID + '_container' ).slideToggle( 'slow' );
}

/* put all onloads here */
$(document).ready( function() { 
     //alert( 'mode = ' + document.compatMode );
     var is_chrome = /chrome/.test( navigator.userAgent.toLowerCase() );
     var is_ff2 = /firefox\/2/.test( navigator.userAgent.toLowerCase() );
     var is_ie6 = /msie\s6/.test( navigator.userAgent.toLowerCase() );
     
     if( is_ff2 ) { $( '#container' ).css( { 'clear': 'both' } ); }
     
     if ( ! is_ie6 ) { $( '.dropShadow' ).dropShadow(); }
     $( '.colorBox' ).colorbox();
     $( '.colorBoxIframe' ).colorbox( {width:"40%", height:"80%", iframe:true} );
     $( '.colorBoxLink' ).colorbox( {iframe:true, innerWidth:720, innerHeight:600} );
     $( '.colorBoxMed' ).colorbox( {iframe:true, innerWidth:550, innerHeight:600} );
     $( '.colorBoxSmall' ).colorbox( {iframe:true, innerWidth:450, innerHeight:550} );
     $( '.colorBoxTiny' ).colorbox( {iframe:true, innerWidth:350, innerHeight:350} );
     $( '.colorBoxThin' ).colorbox( {iframe:true, innerWidth:350, innerHeight:600} );
     //$( '.boxPreview' ).colorbox( { onOpen:function() { $( '#cboxLoadedContent' ).css( { 'margin-bottom':'1px' } ); }, width:910, innerHeight:470, iframe:true } );
     // onOpen, onLoad, and onComplete didn't seem to do anything here.     
     $( '.boxPreview' ).colorbox( { width:910, innerHeight:470, iframe:true, overlayClose:false } );
     $( '.tooltip' ).tooltip( {
	               delay: 0,
	             showURL: false,
	         bodyHandler: function() { return $($(this).attr("href")).html(); }
     });
     $( '.trigger' ).click( function(){
        var optID = parseID( $( this ).attr( 'id' ) );
        toggleContainer( optID );
     });
     $( '.basicTrigger' ).click( function(){
        var optID = parseID( $( this ).attr( 'id' ) );
        toggleBasicContainer( optID );
     });
     //menus  these colors assume both top and side menus use the same color scheme if they are different, these will need to be overwritten
     var hoverColor = $( '#mainNav' ).css( 'color' );
     var hoverBGColor = $( '#mainNav li.topCategory ul' ).css( 'background-color' );
     var orgColor = $( '#mainNav a' ).css( 'color' ); 
     
     
     //dont show the menus for ie6
     if ( ! is_ie6 ) {
       
       /* top menus */
       $( '#mainNav li.topCategory' ).hover(
          function() {
             var p = $( this ).position();
             var h = $( this ).height();
             var pb = parseInt( $( this ).css( 'paddingBottom' ).replace( /[^0-9]/g, '' ) );   
             var lh = parseInt( $( this ).css( 'line-height' ).replace( /[^0-9]/g, '' ) );
             var fs = parseInt( $( this ).css( 'font-size' ).replace( /[^0-9]/g, '' ) );
             var et = p.top + h + pb  + ( lh - fs )/2 - 1;  /* I have no idea why the -1 has to be there but it works the same in FF, IE, & SF */
             $( this ).find( 'ul:first' ).css( {  'display': 'block', 'visibility': 'visible', 'left': p.left + 'px', 'top': et + 'px' } ); //corners don't seem to work right in IE
             if( jQuery.support.opacity && ! is_chrome && ! is_ff2 ) {
                /* rounded corners for FF & Safari */
                $( this ).corners( 'top' );
                $( this ).find( 'ul:first' ).corners( 'top-right bottom' );
             }
             //show the top item as though it were hovered
             $( this ).find( 'a' ).css( { 'color': hoverColor, 'background': hoverBGColor, 'text-shadow': 'none' } );
          },
          function() {
             $( this ).find( 'ul:first' ).css( { 'visibility': 'hidden' } );
             $( this ).find( 'a' ).css( { 'color': orgColor, 'background': 'transparent' } );
          }
       );
       
       /* side menus */
       $( '#navBar li.topCategory' ).hover(
          function() {  
             //mouseover function
             var p = $( this ).position();
             pTop = p.top;
             if ( ! jQuery.support.opacity ) { //fudge for IE
                if ( p.top != 0 ) {
                   pTop = p.top - 6;
                }
             }
             var w = $( this ).width();          
  
             //alert( 'color = ' + $( this ).find( 'a' ).css( 'color' ) );
             var orgColor = '#FFF';
             $( this ).find( 'ul:first' ).css( { 'display': 'block', 'visibility': 'visible', 'left': ( p.left + w ) + 'px', 'top': ( pTop ) + 'px' } ); 
             $( this ).find( 'a' ).css( { 'color': hoverColor, 'background': hoverBGColor } );
             if( jQuery.support.opacity && ! is_chrome && ! is_ff2 ) {
                /* rounded corners for FF & Safari */
                $( this ).corners( 'left' );
                $( this ).find( 'ul:first' ).corners( 'right bottom-left' );
             }
          },
          function() { 
             //mouseout function
             $( this ).find( 'ul:first' ).css( { 'visibility': 'hidden' } );
             $( this ).find( 'a' ).css( { 'color': orgColor, 'background': 'transparent' } );
        });
     }
      

     
     $('.countChars').keyup(function(){
        var txtField = $(this).attr('id');
        if ( $( this ).is( 'textarea' ) ) {
           var maxLen = parseInt( $( this ).attr( 'title' ).match( /[0-9]+/ ) );
           $('#'+ txtField +'_counter').html($(this).val().length + ' of ' + maxLen + ' characters.');
        } else {
           $('#'+ txtField +'_counter').html($(this).val().length + ' of ' + $(this).attr('maxlength') + ' characters');
        }
     });
     
     $('#cartLink').hover( 
       function() {
          //mouseover function
          var p = $( this ).offset();
          var w = $( '#miniCart' ).width(); /* doesn't have a width yet */
          w = 200;
          //determine right edge of #container, if p.left + w / 2 > right, shift the minicart leftward
          
          var h = $( this ).height();
          $( '#miniCart' ).css( {  'display': 'block', 'visibility': 'visible', 'left': ( p.left - w / 2 ) + 'px', 'top': ( p.top + h ) + 'px' } ).hover(
             function() { $( this ).show(); },
             function() { $( this ).hide(); }
          );
       }, 
       function() {
          $( '#miniCart' ).hide();
      });
     
     if( jQuery.support.opacity )( $( '.pGrid li p.box' ).corners( 'top' ) );

     
});
