// JavaScript Document

$(document).ready(function(){

//footer stick
positionFooter(); 
	function positionFooter(){
		if($("#outer").height()+$("#footer-second-level").height() < $(window).height()){ 
			//$("#footer").css({position: "absolute",top:($(window).scrollTop()+$(window).height()-$("#footer").height())+"px"})
			//alert($(window).scrollTop()+$(window).height()-$("#footer-second-level").height())
			//var height = ($("outer").height() > $(window).height())? $("#outer").height():$(window).height();
			
			
			
			$("#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")
		
		});
		
		
// 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 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 );*/
			
			$(slide).fadeOut('fast');
			
			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).fadeOut('fast', function() { $(slide2).fadeIn('fast'); });
			
			// slide two
			//$(slide2).animate({ left:620 },0);
			//$(slide2).show();
			/*$(slide2).animate({ 
				left: 0
			}, 750 );*/
			
			//$(slide2).fadeIn('fast');
			
			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
	});	
	
	
	
	
	// free day or night
	$(".free-day").click(function() { launchModalWindow('/free-day/',580,270) } );
	$(".free-night").click(function() { launchModalWindow('/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/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();
  		});

		
	}
	
	var openAnswer = "";
	
	$('.faq_question').click( function() {
		
		if(openAnswer!="") $(openAnswer).css("display","none");
		
		var myIndex = $('.faq_question').index(this);
		var myAnswer = $('.faq_answer').get(myIndex);
		
		if($(myAnswer).is(":visible")) {
			$(myAnswer).css("display","none");
			openAnswer = "";
			//positionFooter();
			//$("#footer-second-level").css({position: "absolute",top:($(window).scrollTop()+$(document).height()-$("#footer-second-level").height())+"px"}) 
		} else {
			$(myAnswer).css("display","block");
			//$("#footer-second-level").css({position: "absolute",top:($(window).scrollTop()+$(document).height())+"px"}) 
			openAnswer = $(myAnswer);
		}
		
		//var block = $("#faq_btm .right");
		var block = $("#outer");
		//alert($(block).height());
		
		
		

		
	});
	
	$('.faq_close').click( function() {
		
		// close the open div
		var myIndex = $('.faq_close').index(this);
		var myAnswer = $('.faq_answer').get(myIndex);
		$(myAnswer).css("display","none");
		
		//$("#footer-second-level").css({position: "absolute",top:($(window).scrollTop()+$(document).height()-$("#footer-second-level").height())+"px"}) 
		//$("#footer-second-level").css({position: "absolute",top:($(window).scrollTop()+$(window).height()-$("#footer-second-level").height())+"px"})
		//positionFooter();
		
		//$("#footer-second-level").css({position: "absolute",top:($(window).scrollTop()+$("#outer").height())+"px"}) 
		
	});
	
	
	$('.fanmail-page').click( function(e) {
		
		e.preventDefault();
		e.stopPropagation();

		
		// iterate through the page numbers to de-activate
		$('.fanmail-page').each(function(key, value) {
 			if($(this).hasClass('active')) $(this).removeClass('active');
		});
		// activate this number
		$(this).addClass('active');
		
		var showPosts = $('#showPosts').val();
		var myPageNum = $(this).attr('rel'); // which page number we're on
		var rowCount = 0;
		var pageCount = 1;
		
		$('.fan-mail-row').each(function(key, value) { 
 			
			//if($(this).is(":visible")) $(this).hide(); 
			
			if(rowCount==showPosts) { // another page
				pageCount++
				rowCount = 0;
			}
			
			if(pageCount==myPageNum) {
				$(this).show();
			} else {
				$(this).hide(); 
			}
			
			rowCount++;
			
			
		});
		
		return false;
		
		
	});
	
	
	$('.article-page').click( function(e) {
		
		e.preventDefault();
		
		// iterate through the page numbers to de-activate
		$('.article-page').each(function(key, value) {
 			if($(this).hasClass('active')) $(this).removeClass('active');
		});
		// activate this number
		$(this).addClass('active');
		
		var showPosts = $('#showPosts').val();
		var myPageNum = $(this).attr('rel'); // which page number we're on
		var rowCount = 0;
		var pageCount = 1;
		
		var len = $('.article-row').length;
		
		for(i=0;i<len; i++) {
			
			var row = $('.article-row').get(i);
			
			if(rowCount==showPosts) { // another page
				pageCount++;
				rowCount = 0;
			}
			
			if(pageCount==myPageNum) { 
				$(row).show();
			} else {
				$(row).hide();
			}
			
			rowCount++;
			
		}
		
		return false;
		
		
	});
	
	
	/* FAN MAIL PAGINATION */
	
	var fm_startIndex = 10;
	var fm_endIndex = 20;
	var lastPageLinkNum = 0;
	
	$('.more-fan-mail').click( function() {
		
		var count = 0;
		var pages = $(".fanmail-page").length;
		
		$('.fanmail-page').each( function(key,val) {
			
			if($(this).is(":visible")) {
				$(this).hide();
			}
			
			var index = $(this).index();
				
			if(index>fm_startIndex && index<fm_endIndex && count<10) {
				
				$(this).show();
				count++;
				fm_startIndex++;
				fm_endIndex++;
				
				if($(this).attr("rel") == pages) {
					$(".more-fan-mail").hide();
				} else {
					$(".more-fan-mail").show();
				}
				
				if(index>9) {
					$(".prev-fan-mail").show();
				}
				
			}
			
	
		});
		
		lastPageLinkNum = count;
		
	});
	
	
	$('.prev-fan-mail').click( function() {
		
		var count = 0;
		var pages = $(".fanmail-page").length;
		
		var start = fm_startIndex-(20-(10-lastPageLinkNum));
		var end = start + 11; //alert(end)
		
		$('.fanmail-page').each( function(key,val) {
			
			if($(this).is(":visible")) {
				$(this).hide();
			}
			
			var index = $(this).index();
			
			if(index>start && index<end && count<10) {
				
				$(this).show();
				count++;
				fm_startIndex--;
				fm_endIndex--;
				
				if($(this).attr("rel") == 1) {
					$(".prev-fan-mail").hide();
				}
				
				
			}
			
	
		});
		
		 fm_endIndex+= (10-lastPageLinkNum);
		 fm_startIndex+= (10-lastPageLinkNum);
		 
		 lastPageLinkNum = count;
		
		
		$(".more-fan-mail").show();
		
	});
	
	
	
	/* PET RESOURCES PAGINATION */
	
	var pr_startIndex = 10;
	var pr_endIndex = 20;
	var lastPRPageLinkNum = 0;
	
	$('.more-articles').click( function() {
		
		var count = 0;
		var pages = $(".article-page").length;
		
		$('.article-page').each( function(key,val) {
			
			if($(this).is(":visible")) {
				$(this).hide();
			}
			
			var index = $(this).index();
				
			if(index>pr_startIndex && index<pr_endIndex && count<10) {
				
				$(this).show();
				count++;
				pr_startIndex++;
				pr_endIndex++;
				
				if($(this).attr("rel") == pages) {
					$(".more-articles").hide();
				} else {
					$(".more-articles").show();
				}
				
				if(index>9) {
					$(".prev-articles").show();
				}
				
			}
			
	
		});
		
		lastPRPageLinkNum = count;
		
	});
	
	
	$('.prev-articles').click( function() {
		
		var count = 0;
		var pages = $(".article-page").length;
		
		var start = pr_startIndex-(20-(10-lastPRPageLinkNum));
		var end = start + 11; //alert(end)
		
		$('.article-page').each( function(key,val) {
			
			if($(this).is(":visible")) {
				$(this).hide();
			}
			
			var index = $(this).index();
			
			if(index>start && index<end && count<10) {
				
				$(this).show();
				count++;
				pr_startIndex--;
				pr_endIndex--;
				
				if($(this).attr("rel") == 1) {
					$(".prev-articles").hide();
				}
				
				
			}
			
	
		});
		
		 pr_endIndex+= (10-lastPRPageLinkNum);
		 pr_startIndex+= (10-lastPRPageLinkNum);
		 
		 lastPRPageLinkNum = count;
		
		
		$(".more-articles").show();
		
	});



});
