/** 
* Scripts Contained Herein:
*   - Search Box Focus/Blur
*	- Ornamentation for Breadcrumbs and Footer Links
*	- Legal Links Footer Box
*	- All Services Tabs
*	- What's This? Drop Down 
*	- Remove Product Image Link
*   - Subscription Grid, Floating Info Boxes
**/

function update_total(count) {
		jQuery("#cart-total").html("View Your Cart (" + count + ")");
}


function add_to_cart_and_pop(title, url, nid, qty, attrs, status)
{
	var ats = "";

	if (attrs != null && attrs.length > 0) {
		for (attr in attrs)	{
			ats =  ats + '_a' + attr + 'o' + attrs[attr];
		}
	}
	
	jQuery(status).show();
	
	jQuery.get('/cart/add/p' + nid + '_q' + qty + ats, function(data) {
		jQuery(status).hide();
	  	tb_show(title, url, "images/cart.gif", "cart_popup");
	});
}

function popup_close( count) {
	jQuery("#icon_cart span").html("View Your Cart (" + count + ")");
}



jQuery(document).ready(function() {

		(function (){
			var search_box = jQuery("#search-field");
			var default_text = "Enter your search terms";
			var blur_color = "#999999";
			
			search_box.focus(function(){  
			   		if(jQuery(this).attr("value") == default_text)  { 
			   			jQuery(this).attr("value", "");
			   			jQuery(this).css("color", "#000000");	   				
			   		 };  
				});  
			search_box.blur(function(){  
			   		if(jQuery(this).attr("value") == "" || jQuery(this).attr("value") == default_text ) { 
			   			jQuery(this).attr("value", default_text);
			   			jQuery(this).css("color", blur_color);
			   		};  
			}); 
		})(jQuery);
		
				
		/*** ORNAMENTATION FOR BREADCRUMBS AND FOOTER LINKS ***/
		/*
		(function() {
			
			//Breadcrumbs
			jQuery(".crumb").each(function(index) {
					if ( !(jQuery(this).hasClass("first")) ) {
						if ( jQuery(this).hasClass("last") ) { jQuery(this).prepend("&#8226;&nbsp;&nbsp;"); } 
						else { jQuery(this).prepend("&#187;&nbsp;"); }	
					}
			});
			
			//Footer Links
			jQuery("#footer-content ul li").each(function(index) {
					if ( !(jQuery(this).hasClass("first")) ) {
						jQuery(this).prepend("&nbsp;&#8226;&nbsp;&nbsp;");
					}
			}); 	
			
		})(jQuery);
		*/
		
		
		/*** LEGAL LINKS FOOTER BOX ***/ 
		/*
		(function() {
			
			jQuery("#footer-content ul li a").each(function(index) {
				if ( jQuery(this).attr("href") == "/legal" ) {
					jQuery(this).parent("li").attr("id", "legal_link");
					jQuery(this).parent("li").append(jQuery("#legal_box"));
				
				}
			}); 
			
			jQuery("#footer-content li#legal_link").hover(
				function() {
					jQuery("#footer #legal_box").fadeIn("fast");
				},
				function() {
					jQuery("#footer #legal_box").fadeOut("fast");
				}
			); 
			
		})(jQuery);
		*/


		/*** ALL SERVICES TABS ***/
		/*
		(function() {
			jQuery("#all_services .tab_content").hide(); //Hide all content
			jQuery("#all_services ul.tabs li:first").addClass("active").show(); //Activate first tab
			jQuery("#all_services .tab_content:first").show(); //Show first tab content
	
			//On Click Event
			jQuery("#all_services ul.tabs li").click(function() {
		
				jQuery("#all_services ul.tabs li").removeClass("active"); //Remove any "active" class
				jQuery(this).addClass("active"); //Add "active" class to selected tab
				jQuery("#all_services .tab_content").hide(); //Hide all tab content
		
					
				var activeTab = jQuery(this).find("a").attr("href"); //Find the href attribute value to identify the active tab + content
				jQuery(activeTab).fadeIn(); //Fade in the active ID content				

				return false;
			});	
			
			jQuery("#all_services div.row").each( function() {
				if ( jQuery(this).children(".col").length == 1 ) {
					jQuery(this).children(".col.first").removeClass("first").addClass("single");
				}
			});
		
		
		})(jQuery);
		*/
		
		
		/*** WHAT'S THIS? DROP DOWN ***/
		
		( function() {
			
			jQuery('span.whats_this').click( function() {
				jQuery(this).parent('label').parent('.form-item').find('.whats_this_text').slideToggle('fast');
			});
						
		})(jQuery);
		
		
		
		/*** REMOVE PRODUCT IMAGE LINK ***/
		/*
		( function() {
			jQuery('.main-product-image > a img').unwrap();	
		})(jQuery);
		*/
		
        
		/*** FLOATING INFO FOR SUBSCRIPTION GRID ***/
		( function() {	
			
		jQuery(".floating-info").each( function() {
			var under = jQuery(this).hasClass('under');
			
			//wrap the contents of each "Floating info" div in the necessary divs to create surrounting borders
			var html = jQuery(this).html();
			
			
			// Wrap
			jQuery(this).html( '<div class="pointer"></div><div class="top-line"><div class="tl"></div><div class="top"></div><div class="tr"></div></div><div class="middle-line autoclear"><div class="left"></div><div class="middle">'
						   + html
						   + '</div><div class="right"></div></div><div class="bottom-line"><div class="bl"></div><div class="bottom"></div><div class="br"></div></div>' );


			//jQuery(this).parents("td").wrapInner('<div class="floating-info-wrapper" />');

			//Add the learn more button
			//jQuery(this).wrap('<span class="learn-more subscriptions-learn-more" />');
			jQuery(this).wrap('<span class="floating-info-wrapper" />');
			jQuery(this).parents('.floating-info-wrapper').append('<span class="learn-more subscriptions-learn-more">Learn&nbsp;More&nbsp(?)</span>');			
			//Get the vertical position of the pointer element
			var pointerTop = jQuery(this).height() / 2;
			//IE7 mysteriously adds 20px to the height, so we need to subtract half that.
			if ((jQuery.browser.msie) && ( parseFloat(jQuery.browser.version, 10) >= 7 ) && ( parseFloat(jQuery.browser.version, 10) < 8 )) { pointerTop = pointerTop - 10; }
			var pointerHeight = jQuery(this).find("div.pointer").height() / 2;
			pointerTop = pointerTop - pointerHeight;
			if (!under) { jQuery(this).find("div.pointer").css("top", pointerTop + "px"); }
			
			//set some border widths and heights
			jQuery(this).find("div.top-line div.top").width( jQuery(this).find("div.middle-line div.middle").outerWidth() );
			jQuery(this).find("div.bottom-line div.bottom").width( jQuery(this).find("div.middle-line div.middle").outerWidth() );
			jQuery(this).find("div.middle-line div.left").height( jQuery(this).find("div.middle-line div.middle").outerHeight() );
			jQuery(this).find("div.middle-line div.right").height( jQuery(this).find("div.middle-line div.middle").outerHeight() );
			
			jQuery(this).find("div.top-line").width( jQuery(this).find("div.top-line div.tl").width() +
												jQuery(this).find("div.top-line div.top").width() +
												jQuery(this).find("div.top-line div.tr").width() );
			jQuery(this).find("div.middle-line").width( jQuery(this).find("div.middle-line div.left").width() +
												jQuery(this).find("div.middle-line div.middle").outerWidth() +
												jQuery(this).find("div.middle-line div.right").width() );
			jQuery(this).find("div.bottom-line").width( jQuery(this).find("div.bottom-line div.bl").width() +
												jQuery(this).find("div.bottom-line div.bottom").width() +
												jQuery(this).find("div.bottom-line div.br").width() );
												
				
		});	
			
				
		//when mouse over the learn more button, move the div into view
		jQuery(".floating-info").parents("span.floating-info-wrapper").find(".learn-more").hover( 
			function() {
				if ( ! jQuery(this).parent("span.floating-info-wrapper").find(".floating-info").hasClass('under') ) {
					var topOffset = jQuery(this).parent("span.floating-info-wrapper").find(".floating-info").height() / 2;
					var topPosition = jQuery(this).parent("span.floating-info-wrapper").height() / 2 - topOffset;
					var leftPosition = jQuery(this).parent("span.floating-info-wrapper").width() + 10;
					
					jQuery(this).parent("span.floating-info-wrapper").find(".floating-info").css("top", topPosition + "px");
					jQuery(this).parent("span.floating-info-wrapper").find(".floating-info").css("left", leftPosition + "px");
				} else {
					var topPosition = jQuery(this).parent("span.floating-info-wrapper").height() + 10;
					var leftPosition = jQuery(this).parent("span.floating-info-wrapper").find("div.floating-info").width() - 30;
					jQuery(this).parent("span.floating-info-wrapper").find(".floating-info").css("top",  topPosition + "px");
					jQuery(this).parent("span.floating-info-wrapper").find(".floating-info").css("left", "-" + leftPosition + "px");
				}
				
				
				if ((jQuery.browser.msie) && ( parseFloat(jQuery.browser.version, 10) < 8 ) && (jQuery(this).siblings(".floating-info").hasClass('under')) ) {
				  $(this).addClass("hover");
				  $(this).parents("ul").find(".learn-more:not(.hover)").animate( {'opacity': 0}, 'fast');	
				}
				
			},
			function() {
				jQuery(this).parent("span.floating-info-wrapper").find(".floating-info").css("top", "-9999px");
				jQuery(this).parent("span.floating-info-wrapper").find(".floating-info").css("left", "-9999px");
				
				
				if ((jQuery.browser.msie) && ( parseFloat(jQuery.browser.version, 10) < 8 ) && (jQuery(this).siblings(".floating-info").hasClass('under')) ) {
				  $(this).parents("ul").find(".learn-more:not(.hover)").animate( {'opacity': 1}, 'fast');	
				  $(this).removeClass("hover");
				}
				
			}
		);
				
						
		})(jQuery);
		
		
		
		//MAIN NAV BUTTON LINKS
		(function() {
			
			jQuery('#main-nav .block').each( function() {
				var link = '/' + jQuery(this).find('h2').text().replace(/ /g,'-').replace('/','-');
				if ( jQuery(this).find('ul').children().length < 3 ) {
					jQuery(this).find('h2').css("position", "static").wrapInner('<div><a href="' + link.toLowerCase() + '" /></div>');
				} else {
					jQuery(this).find('h2').html('');
				}
			}); //each
			
			
			jQuery('#block-menu-menu-nav-in-home-consults .content').addClass('noShow');
			jQuery('#block-menu-menu-nav-on-call .content').addClass('noShow');
		})(jQuery);
		
		

		//MAIN NAV HOVERS
		
		(function() {
			jQuery('#main-nav .block').hoverIntent({
				sensitivity: 7, // number = sensitivity threshold (must be 1 or higher)
				interval: 50,   // number = milliseconds of polling interval
				over: function() { 
					if (! jQuery.browser.msie ) { jQuery(this).find(".subnav").fadeIn("fast"); }
					else {  
						jQuery(this).find(".subnav").show(); 
						if ( jQuery.browser.version >= 8 ) {
							jQuery(this).siblings().css("z-index", "12000");
							jQuery(this).css("z-index", "11000");
						}
					}
				},  
				timeout: 0,   // number = milliseconds delay before onMouseOut function call
				out: function() { 
					if (! jQuery.browser.msie ) { jQuery(this).find(".subnav").fadeOut("fast"); }
					else {  jQuery(this).find(".subnav").hide(); }
				}   
			});
			
			jQuery('#main-nav .menu li').hover( function() {
				$(this).addClass('hover');
				if ( $(this).hasClass('first') ) { $(this).addClass('firsthover'); }
			}, function() {
				$(this).removeClass('hover').removeClass('firsthover');
			}); //hover
		
		})(jQuery);
		
		
		//PHONE/CHAT BAR
		
		(function() {

			$(window).scroll(function(){						
				if ( $(window).scrollTop() > 260 ) { $('#chat-overlay').fadeIn(400); } else {  $('#chat-overlay').fadeOut(400); }

			});
						
		})(jQuery);
		
		


}); //document.ready
