// JavaScript Document

$(document).ready(function(){

//footer stick
positionFooter(); 
	function positionFooter(){  //alert($(window).height());
		if($("#outer").height()+$("#footer-second-level").height() < $(window).height()){ 
			//$("#footer").css({position: "absolute",top:($(window).scrollTop()+$(window).height()-$("#footer").height())+"px"})
			$("#footer-second-level").css({position: "absolute",top:($(window).scrollTop()+$(window).height()-$("#footer-second-level").height())+"px"})
		}	
	}

	$(window)
		.scroll(positionFooter)
		.resize(positionFooter)
		
		
		
		
// img swap
$(".img-swap").hover(
	
		function(){ this.src = this.src.replace("_off","_over") },
		function(){ this.src = this.src.replace("_over","_off")
		
		});
		
		
		
// home page slideshow

	
	$(".paging a:first").addClass("active");
		
	var slide,lead,blurb,play;
	var currentSlide;
	var currentSlideIndex = 1;
	var slideIndex = 0;
	
	///Paging + Slider Function
	rotate = function(index){
		
		if(index==undefined || (index!=undefined && index!=currentSlideIndex)) {
		
			if(slideIndex==$(".slide-img").length) {
				slideIndex = 0;
			}
			
			$(".paging a").removeClass('active'); //Remove all active class
			$active.addClass('active'); //Add active class (the $active is declared in the rotateSwitch function)
			
			slide = $(".slide-img").get(slideIndex);
			blurb = $(".slide-blurb").get(slideIndex);
			lead = $(".slide-header").get(slideIndex);
			
			// slide one
			$(slide).animate({ 
				left: -625
			}, 750 );
			
			var nextSlide = (slideIndex+1==$(".slide-img").length)? 0:slideIndex+1;
			nextSlide = (index!=undefined)? index-1:nextSlide;
			currentSlideIndex = (index!=undefined)? index:slideIndex+2;
			
			var blurb2 = $(".slide-blurb").get(nextSlide);
			var lead2 = $(".slide-header").get(nextSlide);
			
			// caption swap
			$(blurb).fadeOut('fast', function() { $(blurb2).fadeIn('fast') } );
			
			// header swap
			$(lead).fadeOut('fast', function() { $(lead2).fadeIn('fast') } );
			
			var slide2 = $(".slide-img").get(nextSlide);
			
			// slide two
			$(slide2).animate({ left:620 },0);
			$(slide2).show();
			$(slide2).animate({ 
				left: 0
			}, 750 );
			
			slideIndex = nextSlide;
		
		}
		
	};
	
	//Rotation + Timing Event
	rotateSwitch = function(){		
		play = setInterval(function(){ //Set timer - this will repeat itself every 3 seconds
			$active = $('.paging a.active').next();
			if ( $active.length === 0) { //If paging reaches the end...
				$active = $('.paging a:first'); //go back to first
			}
			rotate(); //Trigger the paging and slider function
		}, 9000); //Timer speed in milliseconds (8.5 seconds)
	};
	
	rotateSwitch(); //Run function on launch
	
	//On Hover
	$("#hp-slideshow-locations-frame").hover(function() {
		clearInterval(play); //Stop the rotation
	}, function() {
		rotateSwitch(); //Resume rotation
	});	
	
	//On Click
	$(".paging a").click(function() {
		$active = $(this); //Activate the clicked paging
		//Reset Timer
		clearInterval(play); //Stop the rotation
		rotate($(this).attr("rel")); //Trigger rotation immediately
		rotateSwitch(); // Resume rotation
		return false; //Prevent browser jump to link anchor
	});	
	
	
	// header FREE burst swap
	var myBurst = $(".burst").first();
	var myBurst2 = $(".burst").last();
	var firstSwap = true;
	
	function swapBurst() {
		
		var b1, b2;
		
		if(firstSwap) {
			
			firstSwap = false;
			b1 = myBurst;
			b2 = myBurst2;
			
		} else {
			
			firstSwap = true;
			b1 = myBurst2;
			b2 = myBurst;
			
		}
		
		$(b1).fadeOut("slow")
		$(b2).fadeIn("slow")
		
	}
	
	rotateBurst = function() {
		burst = setInterval( function() { swapBurst() }, 3000 );
	}
	
	rotateBurst();
	
	

	// HOME PAGE PHOTO TOUR
	$(".hp-tour-right").click(function() {
		
		// take the visible image and move it left
		var slide1 = $(".photo-tour-img:visible").eq(0)//.addClass("first"); 
		
		//alert(slide1.attr("id"))
		
		// is slide1 the last slide?
		var slide2 = ( slide1.equals($(".photo-tour-img").last()) )? $(".photo-tour-img").first():$(slide1).next();
		
		$(slide2).animate({ left: $(slide2).width() }, 0 );
		$(slide2).show();
		
		$(slide1).animate({ 
				left: $(slide1).width()*-1
			}, 500, function() { $(this).hide() } );
			
		$(slide2).animate({ 
				left: 0
			}, 500 );
			
		// get the index
		var index1 = slide1.index();
		var index2 = slide2.index();
		
		// swap the blurbs
		var blurb1 = $(".photo-tour-blurb").get(index1)
		var blurb2 = $(".photo-tour-blurb").get(index2)
		
		$(blurb1).fadeOut('fast', function() { $(blurb2).fadeIn('fast') });
		
		var pID = $(slide2).attr("id");
		
		// swap addthis url
		$(".addthis_button").attr("addthis:url",$(".site_url").html()+"photo-share/?pID="+pID);	
		$(".addthis_button").attr("addthis:description", $(blurb2).html());
		   window.addthis.ost = 0;
		   window.addthis.ready();		
		
	});
	
	$(".hp-tour-left").click(function() {
		
		// take the visible image and move it right
		var slide1 = $(".photo-tour-img:visible").eq(0) //.addClass("first");
		
		// is slide1 the last slide?
		var slide2 = ( slide1.equals($(".photo-tour-img").first()) )? $(".photo-tour-img").last():$(slide1).prev();
		
		$(slide2).animate({ left: $(slide2).width() *-1 }, 0 );
		$(slide2).show();
		
		$(slide1).animate({ 
				left: $(slide1).width()
			}, 500, function() { $(this).hide() } );
			
		$(slide2).animate({ 
				left: 0
			}, 500 );
			
		// get the index
		var index1 = slide1.index();
		var index2 = slide2.index();
			
		// swap the blurbs
		var blurb1 = $(".photo-tour-blurb").get(index1)
		var blurb2 = $(".photo-tour-blurb").get(index2)
		
		$(blurb1).fadeOut('fast', function() { $(blurb2).fadeIn('fast') });
		
		var pID = $(slide2).attr("id");
		
		// swap addthis url
		$(".addthis_button").attr("addthis:url",$(".site_url").html()+"photo-share/?pID="+pID);  
		$(".addthis_button").attr("addthis:description", $(blurb2).html());
		   window.addthis.ost = 0;
		   window.addthis.ready();
		   
		
	});
	
	
	
	// launch pet cam modal
	/*$(".pet-cams").change( function() {
		if( $(this).attr("selectedIndex") > 0 ) {
			
			var href = $(this).val();
			window.open(href);
			
			//launchModalImage($(this).val());
		}
	});*/
	
	
	// launch pet video modal
	$(".pet-videos").change( function() {
		if( $(this).attr("selectedIndex") > 0 ) {
			// split string to get width
			var myStr = $(this).val();
			myStr = myStr.split("~");
			
			var href = myStr[0];
			window.open(href);
			
			//launchModalVideo(myStr[0],myStr[1]);
			
		}
	});
	
	
	// free day or night
	$(".free-day").click(function() { launchModalWindow('/norwalk-ct/free-day/',580,270) } );
	$(".free-night").click(function() { launchModalWindow('/norwalk-ct/free-night/',580,230) } );
	
	
	function launchModalWindow(url,width,height) {
		
		var frameW = width + 6;
		var frameH = height + 6
		
		// scroll to top
		$('html, body').animate({scrollTop: '0px'}, 800);
		
		$('<div id="blanket"></div>').css({
			"background-color" : "#111",
			"opacity" : "0.85",
			"position": "fixed",
			"z-index" : "9001",
			"top" : "0px",
			"left" : "0px",
			"width" : "100%",
			"height" : "250%"}).appendTo("#outer");
		
		$('<div id="imgwindow"></div>').css({
			"position" : "absolute",
			"width" : frameW+"px",
			"height" : frameH+"px",
			"z-index" : "9002",
			"top" : "200px",
			"overflow" : "hidden",
			"left" : (($(document).width() - width) / 2)}).appendTo("#outer");
			
		$('<div id="closewindow"><a id="closeMe"><img src="/wp-content/themes/bfpc-locations/images/closeX.png" width="29" height="29" border="0" alt="Close Window" /></a></div>').css({
				"position" : "absolute",
				"left" : (($(document).width() / 2) + (width/2 - 9) ),
				"top" : "185px",
				"width" : "29px",
				"height" : "29px",
				"display" : "block",
				"cursor" : "pointer",
				"z-index" : "9003"}).appendTo("#outer");
		
		
		$('#imgwindow').html('<iframe id="myIframe" ALLOWTRANSPARENCY="true" frameborder="0" scrolling="no" width="'+width+'" height="'+height+'" src="'+url+'" style="border:3px solid #01a89e; border-radius:3px; -moz-border-radius:3px; -webkit-border-radius:3px; background:#fff"></iframe>');
		
		$('#closeMe').click(function(){
    		$('#imgwindow').remove();
			$('#closewindow').remove();
    		$('#blanket').remove();
  		});

		
	}
	
	function launchModalVideo(url,width) {
		
		var height = (width==640)? 480:240;
		var closeOffset = (width==640)? 245:82;
		
		var frameW = parseInt(width) + 6;
		var frameH = parseInt(height) + 6;
		
		// scroll to top
		$('html, body').animate({scrollTop: '0px'}, 800);
		
		$('<div id="blanket"></div>').css({
			"background-color" : "#111",
			"opacity" : "0.85",
			"position": "fixed",
			"z-index" : "9001",
			"top" : "0px",
			"left" : "0px",
			"width" : "100%",
			"height" : "250%"}).appendTo("#outer");
		
		$('<div id="imgwindow"></div>').css({
			"position" : "absolute",
			"width" : frameW+"px",
			"height" : frameH+"px",
			"z-index" : "9002",
			"top" : "230px",
			"overflow" : "hidden",
			"left" : (($(document).width() - frameW) / 2)}).appendTo("#outer");
			
		$('<div id="closewindow"><a id="closeMe" style="text-decoration:none; font-family:Lucida Sans Unicode, Lucida Grande, Arial, sans-serif; font-size:10px; color:#01a89e">CLOSE WINDOW</a></div>').css({
				"position" : "absolute",
				"left" : ($(document).width() / 2) + closeOffset,
				"top" : "210px",
				"width" : "80px",
				"height" : "20px",
				"display" : "block",
				"cursor" : "pointer",
				"z-index" : "9003"}).appendTo("#outer");
		
		
		$('#imgwindow').html('<iframe id="myIframe" ALLOWTRANSPARENCY="true" frameborder="0" scrolling="no" width="'+width+'" height="'+height+'" src="'+url+'" style="border:3px solid #01a89e; border-radius:3px; -moz-border-radius:3px; -webkit-border-radius:3px;"></iframe>');
		
		$('#closeMe').click(function(){
    		$('#imgwindow').remove();
			$('#closewindow').remove();
    		$('#blanket').remove();
  		});

		
	}
	
	$(".leave").click( function() {
		
		// go to outside site
		var href = $(this).attr("rel");
		
		// confirm with user
		if(confirm("You will now be taken to the Best Friends Corporate Website in a new window.")) {
			
			window.open(href);
			return true;
		
		}
	});
	
	
	
	
	
	$.fn.equals = function(compareTo) {
			if (!compareTo || !compareTo.length || this.length!=compareTo.length)
	{
					return false;
			}
			for (var i=0; i<this.length; i++) {
					if (this[i]!==compareTo[i]) {
							return false;
					}
			}
			return true;
	
	} 



});

// prevent jQuery from appending cache busting string to the end of the URL
var cache = jQuery.ajaxSettings.cache;
jQuery.ajaxSettings.cache = true;
jQuery.getScript('http://s7.addthis.com/js/250/addthis_widget.js');
   
// Restore jQuery caching setting
jQuery.ajaxSettings.cache = cache;
