/* 
Care Fusion
Global Scripts [global.js]

Thomas Gainar [thomas.gainar@acquitygroup.com]

Copyright (c) 2010 Acquity Group LLC

TOC:

	1) Preloader
	2) Global Navigation Controller for IE6
	3) Global Navigation Menu Drop Adjuster
	4) Input Field Instructional Copy Hide/Show
	5) Accordian Functionality
	6) Product Detail Tab Controller
	7) Print This Page Functionality

*/


// Preloader Function for Global Navigation.
(function($) {
  var cache = [];
  // Arguments are image paths relative to the current page.
  $.preLoadImages = function() {
    var args_len = arguments.length;
    for (var i = args_len; i--;) {
      var cacheImage = document.createElement('img');
      cacheImage.src = arguments[i];
      cache.push(cacheImage);
    }
  }
})(jQuery)

/* Preload nav images (sprite could not be used due to png transparency overlaps */
jQuery.preLoadImages("/media/images/bg_nav_top_3col.gif", "/media/images/bg_nav_top_2col.gif", "/media/images/bg_nav_top_1col.gif", "/media/images/bg_nav_row.gif", "/media/images/bg_nav_current_first.gif", "/media/images/bg_nav_current.gif", "/media/images/bg_nav_bot_3col.png", "/media/images/bg_nav_bot_2col.png", "/media/images/bg_nav_bot_1col.png", "/media/images/bg_nav_active_right.gif", "/media/images/bg_nav_active_left_first.gif", "/media/images/bg_nav_active_left.gif");	


// Top navigation menu control for IE6. Inturprets ".over" found inside CSS so that it responds like ":hover"  It also hides all <select>
// elements to get around a bug in the IE6 browsers.
$(document).ready(function() {	
	if ($('#nav').is('*') && ($.browser.version == 6.0)) {
		cssdropdownRoot = document.getElementById('nav').getElementsByTagName('li');
		for (x=0; x<cssdropdownRoot.length; x++) {
			node = cssdropdownRoot[x];
			if (node.nodeName=="LI") {
				node.onmouseover=function() {this.className+=" over"; $('select').css({visibility: "hidden"});}
				node.onmouseout=function() {this.className=this.className.replace(" over", "");  $('select').css({visibility: "visible"});}
			}
		}
	}
});

// Adjusts the Global Nav Drop Menus so they do not float off to the right of the content area.
$(document).ready(function() {
	if ($('#nav').is('*')) {
			// Get total nav bar width
		var navWidth = $('#nav').width(); 
			// Set the tally to subtract from later
		var tallyWidth = navWidth;
		var n = $("#nav > LI");
		n.each(function(idx) {
		   var subItem = $(this).find(".item");
		   var subWidth = subItem.width();
		   if (subWidth > tallyWidth) {
				tallyWidth = tallyWidth - $(this).width();
				subItem.css("left", "auto").css("right", "-" + tallyWidth + "px");
		   }
		   else tallyWidth = tallyWidth - $(this).width(); 
        }); 
	}
});
// Clear field value function: removes the default value onfocus, and adds back if nothing entered 
// 	INPUT: Element's Object
function fieldClear(obj) {
	if(obj.Val) {
		if (obj.value == '') { 
			obj.value = obj.Val;
			obj.Val = null;
			obj.first = null;
		} 
		else {
			obj.Val = null;
		}
	} else if (!obj.first) { 
		obj.Val = obj.value;
		obj.value = ''; 
		obj.first = 'true';
	} 
}
$(function(){ $('.globalSearch .input input[type="text"]').focus(function() { fieldClear(this); }).blur(function() { fieldClear(this); });});
$(function(){ $('.psearch .input input[type="text"]').focus(function() { fieldClear(this); }).blur(function() { fieldClear(this); });});

// Accordian effect on Left Navigation
function initMenus() {
	$('ul.productMenu ul').hide();
	$.each($('ul.productMenu'), function(){
		$('#' + this.id + '.expandfirst ul:first').show();
		$('#' + this.id + '.expandfirst li:first h4 a').addClass('active');
	});
	$('ul.productMenu h4 a').click(
		function(event) {
			event.preventDefault();
			$('ul.productMenu h4 a').removeClass('active'); // removes active class from all
			//if ( $(this).hasClass('active') )
									 
			$(this).addClass('active');// adds back active state for the current one
			var checkElement = $(this).parent().next(); // returns the next UL group
			var parent = this.parentNode.parentNode.parentNode.id; // returns #ourProducts
			
			
			if((checkElement.is('ul')) && (checkElement.is(':visible'))) {
				if($('#' + parent).hasClass('collapsible')) {
					$('#' + parent + ' ul:visible').slideUp('normal');
					$(this).removeClass('active');
				}
				return false;
			}
			if((checkElement.is('ul')) && (!checkElement.is(':visible'))) {
				$('#' + parent + ' ul:visible').slideUp('normal');
				checkElement.slideDown('normal');
				return false;
			}
		}
	);
	// Expands and Collapses All Brands in the Left Navigation
	$('#allBrands a').click( function(event) {
		event.preventDefault();
		$this=$(this);
		// If already expanded
		if ($this.hasClass('activated')) {
			$this.parent().siblings('.shown').removeClass('shown').addClass('hidden');
			$this.removeClass('activated');
			$this.parent().css({'margin-bottom': '0'})
			$this.css({'background-position': 'right 5px'});
		} else {		// If already collapsed
			$this.parent().siblings('.hidden').removeClass('hidden').addClass('shown');
			$this.addClass('activated');
			$this.parent().css({'margin-bottom': '15px'})
			$this.css({'background-position': 'right -5px'});
		}
		// Use 'rel' attribute to save expanded/collapsed copy states
		var linkState = $this.parent().html();
		var linkText = $this.html();
		var replacementText = $this.attr( 'rel' );
		$this.attr('rel', linkText);
		$this.html(replacementText);
	}); 
}
// Initialize Accordian effect, provided it is present on the page.
$(document).ready(function() {if ($('#ourProducts').is('*')) initMenus();});

// Manages sub tab control for hide/shows
jQuery.fn.tabSelect = function(options) {
	var settings = jQuery.extend({
		cont_group: "*",
		classe: "active"
	}, options);
	var tab_group = $(this);
	$(this).click( function(event) {
		event.preventDefault();
		$(tab_group).removeClass(settings.classe);
		$(this).addClass(settings.classe);
		var indx = $(tab_group).index($(this));
		$(settings.cont_group).removeClass(settings.classe);
		$(settings.cont_group).eq(indx).addClass(settings.classe);
	} );
}

// Controls TAB hide and show states 
$(document).ready(function() {
	$("#detail_tabs a").tabSelect({
		cont_group: "#detail_tabs_content .detail_tab"
	});
});
// Controls Print This Page functionality 
$(document).ready(function() {
	$("#printPage").click(
		function(event) {
			event.preventDefault();
			window.print();
		}
	);
});

// Left Promo Rollover
$(document).ready(function(){
	$('.promoShow').mouseover(function(){
		$(this).addClass('promoHide');
		$(this).closest('div').siblings().removeClass('promoHide');
	});
	$('.promoRollover').mouseout(function(){
		$(this).addClass('promoHide');
		$(this).closest('div').siblings().removeClass('promoHide');
	});
});

