/* 
Alere
Global Scripts [global.js]

Thomas Gainar [thomas.gainar@acquitygroup.com]

Copyright (c) 2010 Acquity Group LLC

	AUTHOR / LOG:
	TG 8/22/10, Initial creation

TOC:

	1) IE6 Global Navigation Rollover Controller
	2) Footer SEE ALL LINKS Controller
	3) Custom Select Box skin and checkbox Skin
	4) Reset Link for Sub Nav Filters
	5) Product Detail Tabs
	5a) Global Navigation drop downs
	5b) Country selection menu drop down
	6) Product Image Switcher
	7a) Shows the hidden fields on the Contact Page from URL string.
	7b) This function will encode the HREF before being sent to the URL.
	8) Country Select Dropdown Controller
	9) FAQs Hide/Show Controller
	10) Check All functionality
	11) General Content Hide/Show Controller
	12) Window Popup Controller

*/

//Add in the Library for the menus.
document.write('<scr'+'ipt type="text/javascript" src="/common/scripts/jquery.hoverIntent.minified.js" ></scr'+'ipt>');

// To avoid conflicts with other libraries.
var $j = jQuery;

/* 1) IE6 :hover Rollover Controller (because IE6 ONLY allows :hover on <A> elements) */
$j(document).ready(function() { 
	 if(navigator.appVersion.indexOf("MSIE")==-1){return;}
	 var i,k,g,lg,r=/\s*hvr/,nn='',c,cs='hvr',bv='globalNav';
	 for(i=0;i<10;i++){g=document.getElementById(bv+nn);if(g){
	 lg=g.getElementsByTagName("LI");if(lg){for(k=0;k<lg.length;k++){
	 lg[k].onmouseover=function(){c=this.className;cl=(c)?c+' '+cs:cs;
	 this.className=cl;};lg[k].onmouseout=function(){c=this.className;
	 this.className=(c)?c.replace(r,''):'';};}}}nn=i+1;}
});

/* 2) Footer SEE ALL LINKS Controller */
$j(document).ready(function() {
	if ($j('.content').length) $j('.content').attr('id', 'content');
		// Get out of this function if there is no footer present
	if (($j('#footer').is('*'))==false) {return;}
		// Initialize collapsed footer since JavaScript is enabled
	$j('#linkContainer').css({height:'125px'});
	$j('#seeAllLinks').parent().show();
		// Now show full footer link set on click
	$j('#seeAllLinks').click(function(event) {
		event.preventDefault();
		$j('#linkContainer').css({'height':'auto'});
		$j(this).parent().hide();
		$j('#collapseAllLinks').parent().show();
	});
		// Now collapse full footer link set on click
	$j('#collapseAllLinks').click(function(event) {
		event.preventDefault();
		$j('#linkContainer').css({height:'125px'});
		$j(this).parent().hide();
		$j('#seeAllLinks').parent().show();
	});
});

/* 3) Custom Select Box skin and checkbox Skin */
$j(document).ready(function() {
	if($j('input[type=checkbox]').length)
	{
		$j('input:checkbox:not([safari])').checkbox();
	}
	
	if($j('form.contact').length)
	{
		$j('#select_title').modmac();
		getURLvars();// preloads product data to input fields
	}
});

/* 4) Reset Link for Sub Nav Filters */
$j(document).ready(function() {
	if($j('a.reset').length) {
		$j('a.reset').click(function(event) {
			event.preventDefault();
			$j('.sub_nav input[type=checkbox]').removeAttr('checked');
		});
	}
});

/* 5) Product Details Tabs */
$j(document).ready(function() {
	if($j('div.product_tabs').length)
	{
		$j('ul.tabs li a').click(
			function() 
			{
				$j('ul.tabs li a').removeClass('selected');
				$j('ul.tabs li').removeClass('active');
				$j(this).addClass('selected');
				$j(this).parent().addClass("active");
				switchIt(this.id);
				return false;
			}
		);
	}
});

/* 5a) Global Navigation drop downs */
$j(document).ready(function() {	
	$j(".menu_body").each(function(index) {
	    var existingText = $j(this).prev('h2').html();
	    var newText = existingText + " <img src='/common/images/arrow_close.png' class='menu_image'>";
	    $j(this).prev('h2').html(newText);
	});
	
	function expand(){	
		$j(this)
				.next("div.menu_body")
				.slideDown(500, function(){
					$j(this).css("zoom", "1");
				})
				.siblings("div.menu_body")
				.slideUp(2000, function(){
					$j(this).css({"overflow": "hidden"});
				});
			
		    $j(this).siblings().children('.menu_image').attr("src", "/common/images/arrow_close.png" );			
		    $j(this).children('.menu_image').attr("src", "/common/images/arrow_open.png" );

		    return false;			
	}	
	
	$j(".mega_menu h2").hoverIntent(expand, expand)
});

/* 5b) Country selection menu drop down */
$j(document).ready(function() {	

	$j('.country_selector .mega_menu h2').each(function(index) {
		 
		var test = $j(this).next('dl').children().html();	
			
	    var existingText = $j(this).html();
	    var newText = existingText + " <img src='/common/images/arrow_close.png' class='country_menu_image'>";
	    $j(this).html(newText);
	});

	$j('#header .mega_menu dl').css('display', 'none');
	
	function expandSection(){	
		$j(this).next('dl')
				.slideDown(500, function(){
					$j(this).css("zoom", "1");
				})
				.siblings('dl')
				.slideUp(2000, function(){
					$j(this).css({"overflow": "hidden"});
				});
			
		    $j(this).siblings().children('.country_menu_image').attr("src", "/common/images/arrow_close.png" );			
		    $j(this).children('.country_menu_image').attr("src", "/common/images/arrow_open.png" );

		    return false;			
	}	
	
	$j(".country_selector .mega_menu h2").hoverIntent(expandSection, expandSection)
});

/* 5c) Left Rail menu drop down */
$j(document).ready(function() {
	
	function expandItem(){	
		$j(this).children('ul')
			.slideDown(500, function(){
			$j(this).css({"zoom":"1"});
			$j(this).prev().children('a').css('background-position','left -100px');					
		});
		    return false;			
	}
	
	function collapseItem(){
		$j(this).parents("div.sub_nav").find("div").not(".item.expanded").children("ul")
			.slideUp(2000, function(){
			$j(this).css({"overflow": "hidden"});
			$j(this).prev().children('a').css('background-position','left 0px');
		});
		return false;
	}
	
	$j(".left_rail .sub_nav .item").hoverIntent(expandItem, collapseItem)	
	
});

function switchIt(tabName){
	var activeTab = tabName.slice(5);
	$j('div.sub_content').addClass('hidden');
	$j('#'+ activeTab).removeClass('hidden');
}

/* 6) Product Image Switcher */
$j(document).ready(function() {
	if($j('div.product_images img ').length > 1)
	{
		var imageCount = $j('div.product_images img').length;
		var controlHtml = "";
		$j('ul.image_controls').removeClass('hidden');
		for(i = 0; i < imageCount; i++)
		{
			// image id is set to start from one by default.
			if(i == 0)
			{
				controlHtml += '<li id='+ (i + 1) + ' class="selected">'+ (i + 1) + '</li>';//first image is default and therefore selected class applied.
			}
			else
			{
				controlHtml += '<li id='+ (i + 1) + '>'+ (i + 1) + '</li>';
			}
				$j('ul.image_controls').html(controlHtml);
		}
		// click controls.
		$j('ul.image_controls li').click(
			function() 
			{
				$j('ul.image_controls li').removeClass('selected');
				$j('.product_images img').addClass('hidden');
				$j(this).addClass('selected');
				$j('.product_images img').eq(this.id-1).removeClass('hidden');
			}
		);
	}
});


/* 7a) This function will populate and show the hidden fields on the contact page after decoding the Product name.*/
/* Contact-product URL vars */
function getURLvars(){
	var vars = [], hash;
	var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
	
	if($j(hashes).length > 1)// will only run functions if variables are passed.
	{
		for(var i = 0; i < hashes.length; i++)
		{
			hash = hashes[i].split('=');
	    	vars.push(hash[1]);
			if(hash[0] == 'product')
			{
				vars[i]=(decodeURIComponent(vars[i]));
				vars[i]=vars[i].replace(/<[^>]*>?/g,'');
				$j('fieldset#product_field input').val(vars[i]);
				$j('fieldset#product_field').removeClass('hidden');
			}
			else if(hash[0] == 'id')
			{
				vars[i]=(decodeURIComponent(vars[i]));
				vars[i]=vars[i].replace(/<[^>]*>?/g,'');
				$j('fieldset#catalog_field input').val(vars[i]);
				$j('fieldset#catalog_field').removeClass('hidden');
			}
	 	}
	}
}
/* 7b) This function will encode the HREF before being sent to the URL. */
$j(document).ready(function() {
	if(($j('#prodContact').is('*'))==false) {return;}
	var myURL = (encodeURI($j('#prodContact').attr('href'))+ "");

	$j('#prodContact').attr('href',myURL);
});

/* 8) Country Select Dropdown Controller */
$j(document).ready(function() {
		// Get out of this function if there is no Country Select Dropdown present
	if (($j('#countrySelector').is('*'))==false) {return;}
	$j('#countrySelector').click(function() {
		$j('#countrySelector #countryBtn').removeAttr('href');
		$j('#countrySelector #countryBtn').addClass('hvr');
		$j('#countrySelector .mega_menu').toggle();
	});
	$j('#countrySelector').mouseleave(function() {
		$j('#countrySelector #countryBtn').removeClass('hvr');
		$j('#countrySelector .mega_menu').hide();
	 });
});

/* 9) FAQs Hide/Show Controller */
$j(document).ready(function() {
  // Get out of this function if there is no FAQ listing present
 if (($j('.faq_listing').is('*'))==false) {return;} 
  // Initialize to all collapsed
 $j('.faq_listing').addClass('active_faq_listing');
 $j('.faq_listing dl').addClass('collapsed');
  // Toggle classes to show or hide
 $j('.faq_listing dt').click(function(event) {
  event.preventDefault();
  $j(this).parent("dl").toggleClass('collapsed');
 });
});
 

/* 10) Check All functionality */
$j(document).ready(function() {			   
	$j('.checkall').click(function () {
		checkboxObj = $j(this).parents('.filters:eq(0)').find(':checkbox');			   
		if 	 (checkboxObj.attr('checked')) {checkboxObj.removeAttr("checked");}
		else {checkboxObj.attr('checked','true');}
		if 	 ($j(this).attr('checked')) {$j(this).removeAttr("checked"); $j(this).siblings('span.jquery-checkbox').removeClass('jquery-checkbox-checked');}
		else {$j(this).attr('checked','true');$j(this).siblings('span.jquery-checkbox').addClass('jquery-checkbox-checked');}
	});
});

/* 11) General Content Hide/Show Controller */
$j(document).ready(function() {	
		// Get out of this function if there is no Show More present
	if (($j('.expandable_area').is('*'))==false) {return;}
	areaObj = $j('.expandable_area');
		// Initialize for this JavaScript user.
	areaObj.addClass('is_collapsed');
	areaHeight = areaObj.height();
		// Click Event
	$j('.expandable_area_controller a').click(function (event) {
		event.preventDefault(); $jthis=$j(this);
			// Swap Link Display Copy
		var replacementText = $jthis.attr( 'rel' );
		var linkText = $jthis.html();
		$jthis.attr('rel', linkText);
		$jthis.html(replacementText);
			
		$jthis.toggleClass('expanded');
		if ($jthis.hasClass('expanded')) {
			$jthis.parents('.expandable_area').height('100%');
		}
		else {
			$jthis.parents('.expandable_area').height(areaHeight);
		}
	});
});

/* 12) Window Popup Controller */
$j(document).ready(function() {
		// Get out of this function if there are no videoPopup links present
	if (($j('.popup_link').is('*'))==false) {return;} else {videoPopupObj = $j('.popup_link');}
	videoPopupObj.click(function (event) {
	event.preventDefault();
	videoURL = videoPopupObj.attr("href"); 
	window.open(videoURL,'VideoPlayer','menubar=no,width=670,height=390,toolbar=no,resize=1');
	});
});
