/*
* Section background fading
*/
function isIE6() 
{
  if( $.browser.msie == true && $.browser.version < 7 ) 
  {
    return true;
   }
   return false;
}
function getNewBgIndex(current_index,arr)
{
  var i;
  current_index = parseInt(current_index);
  if( current_index == 0 )
    i = 1;
  if( current_index < (arr.length - 1) )
    i = current_index+1;
  else
    i = 0;
  $.cookies.set('bgindex',i);
  return i;
} // end getIndex

function doFade(layer,newImg)
{
  // clone layer
  var new_layer = $('#'+layer).clone();

  $(new_layer).css('opacity','0');
  $(new_layer).css('background-image',"url('"+newImg+"')");
  $('#'+layer).after( new_layer );
  
  // animate original out
  $('#'+layer).animate({
      opacity: 0.0
    },1500);

  // animate original out
  $(new_layer).animate({
      opacity: 1.0
    },1500);
} // end doFade

$(document).ready(function() 
{
 if(_docID == '394')
 {
  $('div.top-nav-inner li.nav').hover(
      function () {
        if( $(this).find('ul').length > 0 ) {
          $(this).find('ul:first').css('visibility','visible');
        }
      }, 
      function () {
    	   if( $(this).find('ul').length > 0 ) {
            $(this).find('ul:first').css('visibility','hidden');
    	   }                
      });
  }

  // setup background images
  var dir = '/assets/templates/rce_business/images/backgrounds/';
  var imgs = new Array('Plants_II_062.jpg','Plants_II_064.jpg','Plants_II_065.jpg','Plants_II_067.jpg','Plants_II_095.jpg','Plants_III_041.jpg','Plants_III_051.jpg');
  var bgindex = 0;
  // get and set bgindex cookie
  if($.cookies.get('bgindex')) 
  { 
    bgindex = $.cookies.get('bgindex'); 
  }
  else
  {
    $.cookies.set('bgindex',bgindex);
  }  
  // setup original bg image
  $('#section-bg').css('background-image',"url('"+dir+imgs[bgindex]+"')");
  var new_bgindex = getNewBgIndex(bgindex,imgs); // hack 20090403
  if( ! isIE6()  )
  {

      setTimeout("doFade('section-bg','"+dir+imgs[new_bgindex]+"')",500);
  }

  // Setup lightbox for client resource center login
  $('a.iframe').fancybox({
      'hideOnContentClick': false,
      'padding': 0,
      'width': 440,
      'height': 325,
      'overlayShow': true,
      'overlayOpacity': 0.4,
      'overlayColor': '#5e7135'
  });
  

	// Lightbox for client success stories
	$("a.inline").fancybox({
		'padding':5,
		
		'hideOnContentClick': true,
		'autoDimensions': false,
		'width': 650,
		'height': 'auto',
		'overlayShow': true,
		'overlayOpacity': 0.4,
		'overlayColor': '#5e7135',
		'onComplete': function() {$.fancybox.resize();}
	});
	
	
	$('a.inline').hover(function() {
		//alert($(this).children(':first-child').attr('src'));
		
		$(this).children(':first-child').animate({
			top: -10,
			left: 75
		}, 200);
		
	}, function() {
		$(this).children(':first-child').animate({
			top: -5,
			left: 65
		}, 200);
	});
	

}); // end document ready

//client page logo rollover effect
function doLogoRollOver(ele) {
	ele.style.backgroundPosition = 'bottom';
}
function doLogoRollOut(ele) {
	ele.style.backgroundPosition = 'top';
}


/***  rollover crossfade functions **/

// create our transition as a plugin
$.fn.crossfade = function () {
  return this.each(function () { 
    // cache the copy of jQuery(this) - the start image
    var $$ = $(this);

    // get the target from the backgroundImage + regexp
    var target = $$.css('backgroundImage').replace(/^url|[\(\)]/g, '');

    // nice long chain: wrap img element in span
    $$.wrap('<span style="position: relative;"></span>')
      // change selector to parent - i.e. newly created span
      .parent()
      // prepend a new image inside the span
      .prepend('<img>')
      // change the selector to the newly created image
      .find(':first-child')
      // set the image to the target
      .attr('src', target);

    // position the original image
    $$.css({
      'position' : 'absolute', 
      'left' : 0, 
      // this.offsetTop aligns the image correctly inside the span
      'top' : this.offsetTop
    });

    // note: the above CSS change requires different handling for Opera and Safari,
    // <a href="http://jqueryfordesigners.com/downloads/jquery.crossfade.js">see the full plugin for this</a>.

    // similar effect as single image technique, except using .animate 
    // which will handle the fading up from the right opacity for us
    $$.hover(function () {
      $$.stop().animate({
          opacity: 0
      }, 100);
    }, function () {
      $$.stop().animate({
          opacity: 1
      }, 100);
    });
  });
};

// Not only when the DOM is ready, but when the images have finished loading,
// important, but subtle difference to $(document).ready();
$(window).bind('load', function () {
  // run the cross fade plugin against selector
  $('img.fade').crossfade();
});

/*** end rollover crossfade function **/

/**
* Footer image swaps
**/
var footer_img_vert = new Array('001_business_Leaf&Building.jpg','002_misc_3486244.jpg','002_pol_3151289.jpg','002_travel_851916.jpg','003_nature_3725739.jpg','003_wind_trimont1.jpg','004_nature_3629418.jpg','004_wind_tractor.jpg','005_nature_3227626.jpg','006_nature_3126685.jpg','007_wind_2106344.jpg','009_nature214381.jpg','012_wind_dv2012023.jpg');
var footer_img_horz = new Array('001_misc_4821375.jpg','001_nature_4608687.jpg','001_pol_8276806.jpg','001_solar_2241111.jpg','001_travel_3939776.jpg','001_wind_WNDF-0145.jpg','002_business_4664572.jpg','002_nature_3726308.jpg','002_wind_WNDF-0136.jpg','003_business_ForBusinessImage.jpg','003_pol_595921.jpg','003_travel_571898.jpg','004_travel_265313.jpg','005_wind_3694914.jpg','005_wind_topscreen.jpg','006_wind_3668880.jpg','007_nature_2658795.jpg','008_nature_1921014.jpg','008_wind_1540317.jpg','009_nature_276309.jpg','009_wind_1521469.jpg','010_wind_622709.jpg','011_wind_482016.jpg','013_wind_Corn&Turbine.jpg','014_wind_barn.jpg');
function doFooterImage(targ,arr,override)
{
  if(override=='blank.gif')
  {
    var dir = '/assets/templates/rce_business/images/footer_images/';
    var img = arr[ Math.floor(Math.random()*arr.length) ];
    $('#'+targ).attr('src',dir+img);
  }
}
/** end doFooterImages() */


/**
* Slide toggles a definition list, such as success stories
**/
function slideToggleDl(l)
{
 $('#dl_container dl').each( function(i) {
   if( l == $(this).find('dd:first').attr('id') ) // turn on
   {
     if( $(this).find('a:first').attr('class') != 'on' )
     {
       $(this).find('a:first').attr('class','on');
       $('#'+l).slideDown();
     }
     else
     {
       $(this).find('a:first').attr('class','');
       $('#'+l).slideUp();
     }
   }
   else // turn off
   {
       $(this).find('a:first').attr('class','');       
       $(this).find('dd:first').slideUp();     
   }
 });
 
}// end slideToggleDl()


function slideToggleDd(l)
{
 $('#dl_container dt').each( function(i) {
   if( l == $(this).next().attr('id') ) // turn on
   {
     $(this).find('a:first').attr('class','on');
     $('#'+l).slideDown('normal',function() {
       $('#dl_container').height( $(this).height() );
     });
   }
   else // turn off
   {
       $(this).find('a:first').attr('class','');       
       $(this).next().slideUp();     
   }
 });
 
}// end slideToggleDl()

/**
* contact form zipcode lookup verification
**/
function doZipcodeLookup(frm)
{
  // hide form if it's been previously shown
  $('#rep_form').css('display','none');
  // test values
  if( !frm.zip.value.match(/^[0-9]{5}$/gi) ) { alert('Please enter a 5 digit Zipcode'); frm.zip.focus(); return false; }
  if(frm.state.value == '' || frm.state.value == '--') { alert('Please select a State'); frm.state.focus(); return false; }
  // do ajax call
  $('#rep_info').html('<p><img src="assets/templates/rce_business/images/ajax-loader.gif"> Searching....</p>');
  $('#contact_rep_info').css('display','block');
  var url = '/sf-zipcode-lookup';
  var type = '';
  for (i = 0; i < frm.type.length; i++) {
    if (frm.type[i].checked) {
      type = frm.type[i].value
    }
  }
  var data = {zip: frm.zip.value, state: frm.state.value, type: type};
  
  $.get(url,data,function(returnData,textStatus) 
  {
    if(textStatus == 'success')
    {
      $('#rep_info').html( returnData );
      $('#rep_form').css('display','block');
    }
    else
    {
      $('#contact_rep_info').html('<p>An error occurred returning the information. Please try again.</p>');    
    }
    return false;
  });
  return false;
} // end doZipcodeLookup


// Read a page's GET URL variables and return them as an associative array.
function getUrlVars()
{
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');

    for(var i = 0; i < hashes.length; i++)
    {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }

    return vars;
}