/*=============================================================================================
INITATION FILE FOR WWW.PETINSURANCE.COM/NEWSLETTER (the Companion)

Developer(s):		Zeke F.

Dependencies:		jquery_1_3_2_min.js

Update History:	03-13-11 - ?v1.1 - Updated file for v7i4 - ZF
								MM-DD-YY - Change Comments - Intials
								
Notes:					Make sure to change /js/fancybox.pack.js to /js/v3/fancybox.pack.js
=============================================================================================*/
$(document).ready(function(){
	//Detect if JavaScript is enabled
	$('html').addClass('js');
	
	//If you selecting something more then twice think about making it a variable	
	var vpiContent = $('#content');
	
	//Add in unsemantic span for icon placement within macroNav items
	$('#macroNav a').not('#VPIHeader_portalLink a').prepend('<span class="siteIcon" />').wrapInner('<span />');
	
	//Companion drop-down menus
	$('#compPrimaryNav').removeClass('noJS');
	$.xLazyLoader({
		img:['/~/media/img/newsletter/graphics/menuHoverBG.ashx']
	});
	$('.menuTitle').hoverIntent(
	{
		over:expandNav,
		timeout:250,
		out:doNothing
	});
	$('.menuCategory').hoverIntent(
	{
		over:doNothing,
		timeout:250,
		out:contractNav
	});
	function doNothing(){}
	function expandNav()
	{
		var menuTitle = $(this).parent();
		menuTitle.addClass('over');
		menuTitle.find('.subCategory').show();
		menuTitle.find('.menuItems').slideDown(200);
	}
	function contractNav()
	{
		if ($(this).hasClass('over'))
		{
   		$(this).find('.menuItems').slideUp(100, function()
   			{
					$(this).parent().removeClass('over');
   			}
   		);
   		$(this).find('.subCategory').hide();
		}
	}
	//Initiate Tabs on Perks Page
	var perksPage = $('#perks');
	if (perksPage.length > 0)
	{
		$('#tabNav').show();
		$('#tabContainerNoFade').tabs();
		$('#navPerks .menuItems a').eq(0).click(function()
		{
			$('#tabContainerNoFade').triggerTab(1);
		});
		$('#navPerks .menuItems a').eq(1).click(function()
		{
			$('#tabContainerNoFade').triggerTab(2);
		});
	}
	//For v7i4 featured section
	var companionIndex = $('#companionIndex');
	if (companionIndex.length > 0)
	{
		$('#compFeatured').append('<p id="compPlus">+</p>')
	}
	//EASTER EGG (see companionCore.css)
	$('#featureCTA').click(
		function()
		{
			$('#compPlus').addClass('rotate');
		}
	);
	$('#featureCTA').mouseout(
		function()
		{
			$('#compPlus').removeClass('rotate');
		}
	);
	//iOS friendly number
	var	isiPad = navigator.userAgent.match(/iPad/i) != null,
			isiPhone = navigator.userAgent.match(/iPhone/i) != null,
			isiPod = navigator.userAgent.match(/iPod/i) != null;
	if(isiPod || isiPhone || isiPad)
	{
		if (document.cookie.indexOf("iphone_redirect=false") == -1) 
		{
			var easyDial = $('#header .tel').attr('title');
			if (easyDial != "")
			{
				$('#header .tel').html('<a href="tel:'+easyDial+'">'+easyDial+'</a>');
			}
		}
	}
	//Check if fancybox.pack.js is loaded
	if($.isFunction($.fn.fancybox))
	{
		//Fancybox is loaded. Setup the modals
		initModals();
	}
	else //If it isn't then load it
	{
		//loads script and then call the function to setup the modals
		$.getScript('/js/v3/fancybox.pack.js',initModals);
		//Make sure the path is /js/v3/fancybox.pack.js for sitecore
	}
	//Initiate Modals
	function initModals()
	{
		var modal = vpiContent.find('.modal'),
				youTubeModal = $('#youTubeModal'),
				playPUG = $('#playPUG'),
				aahaModal = $('.aahaModal');

		//Image modals
		if (modal.length > 0)
		{
			$(modal).fancybox({
				'overlayColor':'#f2f1f0',
				'overlayOpacity':0.8,
				'speedIn':400,
				'speedOut':550,
				'type':'image',
				'hideOnContentClick':true
			});
		}
		//youTube modals
		if (youTubeModal.length > 0)
		{
			$('#youTubeModal').click(function()
			{
				$.fancybox({
					'overlayOpacity':.5,
					'overlayColor':'#534741',
					'padding':'0',
					'autoScale':false,
					'transitionIn':'none',
					'transitionOut':'none',
					'width':627,
					'height':495,
					'href':this.href.replace(new RegExp("watch\\?v=", "i"), 'v/'),
					'type':'swf',
					'swf':{
						'wmode':'transparent',
						'allowfullscreen'	: 'true'
					}
				});
				return false;
			});
		}
		//playPUG Fancybox Init
		if (playPUG.length > 0)
		{
			$(playPUG).fancybox({
				'scrolling':'no',
				'autoDimensions':false,
				'height':530,
				'width':940
			});
		}
		//AAHA Fancybox Init
		if (aahaModal.length > 0)
		{
			$(aahaModal).fancybox({
				'type':'iframe',
				'scrolling':'no',
				'height':312,
				'width':580,
				'titleShow':false
			});
		}
	}
	//Article Specific Stuff
	var heathyPetArticle = $('#healthyPet_v8i1'),
			pug_v8i1 = $('#pug_v8i1');
	if (heathyPetArticle.length > 0)
	{
		$('#secondaryContent').append('<div id="tableBiter" />');
		$('#tableBiter').hover(function()
		{
			$(this).stop().animate({
				'right':'166px'
			},500);
		},function()
		{
			$(this).stop().animate({
				'right':'46px'
			},350)
		});
	}
	if (pug_v8i1.length > 0)
	{
		$('#playPUG').fadeTo(0,.7).hover(
			function()
			{
				$(this).stop().fadeTo(300,1);
			},
			function()
			{
				$(this).stop().fadeTo(300,.7);
			}
		);
	}
});






