jQuery.noConflict();
(function($){

	function callOnDomReady() {

		// load facebox on page homepage load
		if (
			($('body').attr('id') === 'home') &&
			!($.cookie('mckessonbop_name') )
		) {
			$.facebox(function() {
		        $.get('/wp-content/themes/mckesson-bop/choice/', function(data) {
		            $.facebox(data, false, "facebox_choice");
		        });
		    });
			$("#facebox_overlay,a.close").click(function(){
				$.cookie('mckessonbop_name', 'OTHER', { path: '/', expires: 14 });
			});
		};

		/*********************************************
		 * Validate forms on newsletter signup pages
		 *********************************************/
		if($('body').attr('id') === 'consultation' || $('body').attr('id') === 'readings' || $('body').attr('id') === 'join') {
			$.tools.validator.addEffect("wall", function(errors, event) {

				// get the message wall
				var wall = $(this.getConf().container).fadeIn();

				// remove all existing messages
				wall.find("p").remove();

				// add new ones
				$.each(errors, function(index, error) {
					wall.append(
						"<p>" +error.messages[0]+ "</p>"
					);
				});

			// the effect does nothing when all inputs are valid
			}, function(inputs)  {

			});

			$('#consulting_form').validator({
				effect: 'wall',
   				container: '#errors',
			   // do not validate inputs when they are edited
			   errorInputEvent: null
			});

			$('input[name="contact"]').change(function(target){
				if(this.value == 'Other Person'){
					$('#other-contact').slideDown();
					$('input[name="other_contact_name"]').focus();
				}else{
					$('#other-contact').slideUp();
				}
			})
		}
		
		// Custom Stuff for Alternative Site Pharmacy Page
		if($("body").attr("id") === "alternatesite"){
			// Slider
			var slides = $('div#slider div.post');
			slides.each(function(){
				$(this).css({"position":"absolute", "top":"0", "left":"0"});
			});
			slides.first().addClass("active");
			var sliderNav = $("ul#slidernav li a");
			sliderNav.click(function(e){
				sliderNav.removeClass("active");
				var linkTarget = $(this).attr("href");
				slides.removeClass("active");
				$(linkTarget).addClass("active");
				$(this).addClass("active");
				e.preventDefault();
			}).first().addClass("active");
			
			// Internal slider nav links
			var slidesNav = $("div.post a.slidesnav");
            slidesNav.click(function(e){
				sliderNav.removeClass("active");
				var linkTarget = $(this).attr("href");
				$('ul#slidernav li a[href='+ linkTarget + ']').addClass("active");
				slides.removeClass("active");
				$(linkTarget).addClass("active");
				e.preventDefault();
			}).first().addClass("active");
			
			
			// Format category titles
			$("div.asrx-section h2").each(function(){
				var theHTML = $(this).html();
				theHTML = theHTML.replace("Your", "<br /><em>Your");
				theHTML = theHTML + "</em>";
				$(this).html(theHTML);
			});
			
			// Set up ASRx video lightbox
			function loadAsrxVideo(){
				jQuery.facebox({ div: "#playvideo" }); // Open lightbox
				// Set up Flowplayer
				$("div#facebox div#playvideo a").flowplayer({src: "/wp-content/themes/mckesson-bop/scripts/flowplayer-3.2.7.swf", wmode: "opaque"},
					{
					version: [10, 0],
					expressInstall: "swf/expressInstall.swf",
					cachebusting: true,
					onLoad: function()  {
						// if you need to do something on load, put code here
					},
					onUnload: function() {
						// if you need to do something on unload, put code here
					},
					clip: {
						scaling: 'fit',
						onLastSecond: function() {},
						onPause: function() {},
						onResume: function() {},
						// if screen is hidden, show it upon startup
						onStart: function(clip) {
							this.getScreen().css({opacity: 1});
							// Put onstart analytics here
						},
						// when movie is done, unload movie
						onFinish: function() {
							//this.getScreen().animate({opacity: 0.5}, 3000);
							this.unload();
							if(this.isFullscreen() && jQuery.browser.msie){
								document.body.focus();
							}
							// Put onfinish analytics here
						},
						// when movie is stopped, unload movie
						onStop: function() {
							if(this.isFullscreen() && jQuery.browser.safari){
								this.toggleFullscreen();
								document.body.focus();
								unload = this.unload;
								setTimeout(function(){eval("unload()")},50);
							}else{
								this.unload();
								document.body.focus();
							}
						}
					},
					play: {
						opacity: 0
					},
					plugins: {
						controls: { url: '/wp-content/themes/mckesson-bop/scripts/flowplayer.controls-3.2.5.swf'}
					}
				})
			};
			// Call video lightbox on pageload
			if(location.search.indexOf('playvideo=true') != -1){
				loadAsrxVideo();
			}
			// Call video lightbox on click
			$("a.playvideo").click(function(e){
				loadAsrxVideo();
				$f().play(); // Autoplay on click
				e.preventDefault();
			});
			
		}

		// external links
		$('a[rel~="external"]').click(function() {
			this.target = "_blank";
		});

		$('table.form-table td.checked button').mousedown(function() {
		    $("#linkedinid").val("");
		});

		$('a[rel*="facebox"]').facebox();
		$('a[rel="facebox[.fbform]"]').each(function() {		    
			if ( this.href.indexOf("feedback") > 1 ) {
				this.href = this.href + '&width=500&height=500';
			} else if ( this.href.indexOf("contact") > 1 ) {
				this.href = this.href + '&width=500&height=560';
			} else if ( this.href.indexOf("newsletter") > 1 ) {
				this.href = this.href + '&width=500&height=300';
			} else if ( this.href.indexOf("get-started-form") > 1 ) {
				this.href = this.href + '?width=500&height=665';				
			} else if ( this.href.indexOf("get-started") > 1 ) {
				this.href = this.href + '&width=500&height=665';				
			};
			this.href = this.href + '&ajax=true';
		});

		// add a value to the search boxes
		var searchValue = "SEARCH";
		$('#searchform input[type="text"]').each(function() {
			// set value initially if none is specified
			if(this.value == '') {
				this.value = searchValue;
			} else {
				return;
			};
		});

		// remove values on focus
		$('#searchform input[type="text"]').focus(function() {
			$(this).parents("#searchform").addClass("focusField");
			if( this.value == searchValue ) this.value='';
		});

		// place values back on blur
		$('#searchform input[type="text"]').blur(function() {
			$(this).parents("#searchform").removeClass("focusField");
			if(this.value==searchValue || this.value=='') this.value=searchValue;
		});

		// solution and services dropdown
		$("#header li.solutions-services a.top-level").click(function() {
			$(this).parent().siblings("li").toggleClass("not-active");
			$(this).parent().toggleClass("active");
			$("#submenu").slideToggle(200);
			return false;
		});

		// login form
		$("#login a.login").click(function() {
			$(this).hide();
			$(this).siblings("#loginform").slideToggle(500);
			return false;
		});
		$("#login a.close").click(function() {
			$(this).parents("#loginform").slideToggle(500);
			$(this).parents("#loginform").siblings("a.login").show();
		});
		// add a value to the search boxes
		$('#login input[type="text"]').focus(function() {
			$(this).addClass("focusField");
			if (this.value == this.defaultValue) this.value = '';
		});

		$('#login input[type="text"]').blur(function() {
			$(this).removeClass("focusField");
			if (this.value == '') this.value = (this.defaultValue ? this.defaultValue : '');
		});

		// share this
		$("li.sharethis a.top-level").click(function() {
			$(this).siblings("ol.social").slideToggle(200);
			return false;
		});

		var $scrollables = $('.scrollable').scrollable({
			circular: true,
			speed: 300
		});

		$scrollables.each(function() {
			var $itemsToClone = $(this).scrollable().getItems().slice(1);
			var $wrap = $(this).scrollable().getItemWrap();
			var clonedClass = $(this).scrollable().getConf().clonedClass;
			$itemsToClone.each(function() {
				$(this).clone(true).appendTo($wrap)
					.addClass(clonedClass + ' hacked-' + clonedClass);
			})
		});
		
		$('a[rel~="show-get-started"]').click(function(e) {
		    e.preventDefault();
		    var href = $('#get-started-button').attr('href');
		     
		    if ($('#get-started-button').hasClass('show-frame')) {
		        $('#get-started-button').removeClass('show-frame').css({top: 175});
		        $('#get-started-frame').html('').hide();		        
		    } else {
		        
		        var xScroll, yScroll;
                if (self.pageYOffset) {
                  yScroll = self.pageYOffset;
                  xScroll = self.pageXOffset;
                } else if (document.documentElement && document.documentElement.scrollTop) {	 // Explorer 6 Strict
                  yScroll = document.documentElement.scrollTop;
                  xScroll = document.documentElement.scrollLeft;
                } else if (document.body) {// all other Explorers
                  yScroll = document.body.scrollTop;
                  xScroll = document.body.scrollLeft;
                }
		        
                // $('#get-started-frame')
		        
		        $('#get-started-frame').css({ top: yScroll + 10}).html('<iframe src="' + href + '" frameborder="0" marginheight="0" marginwidth="0"></iframe>').show();
		        
    		    $('#get-started-button').addClass('show-frame').css({ top: yScroll + 35});
    		    
		    }
 		});
		

	};

		// external links
		$('a.external, a[rel~="external"], a[rel~="rss"]').click(function() {
		
			var link_name = $(this).attr('href');
			var link_type = "exit";
			
			if($(this).parent().attr('class') == 'linkedin' || $(this).parent().attr('class') == 'youtube') {
				link_type = "social";
			}
			
			ntptEventTag('ev=link&linkType='+link_type+'&lk=1&link='+link_name);
		});
		
		$('a[href*=".pdf"],a[href*=".doc"],a[href*=".ppt"]').click(function() {
			var ext = $(this).attr('href').split('.').pop().toLowerCase();
			ntptEventTag('ev=download&download='+$(this).attr('href')+'&downloadFormat='+ext);
		});
		
		$('a[href*="mailto:"]').click(function() {
			ntptEventTag('ev=link&linkType=email&link='+$(this).attr('href'));
		});
		
		$('a[rel~="chapter"], a#masthead').click(function() {
			var link_name = $(this).attr('href');
			ntptEventTag('ev=link&linkType=featured&link='+link_name);
		});
		


	$(callOnDomReady);
})(jQuery);

