﻿//******************************** jQuery INITIALIZATIONS BLOCK *****************************************
//
//

$(document).ready( function() {
//******************************** jQuery Amber Alerts Initialization *****************************************
//
//

//Fetch Amber Alert JSON and activate alert on the portal if there are active ones.
	var showAmberAlert = false;
	$.getJSON("http://pipes.yahoo.com/pipes/pipe.run?_id=0d5996bc643ba6e12aa130a8028665aa&_render=json&num=1&_callback=?",
		function(data) {
			$.each(data.value.items, function(i, item) {
	    		showAmberAlert = true;
	    		if(!(document.getElementById("portalBarAmberAlert"))) {
	    			$("<div class='portalBarAmberContainer'><ul id='portalBarAmberAlert'></ul></div>").appendTo(".portalBarAlertContainer");
	    		}
	    		$("#portalBarAmberAlert").append("<li class='amberAlert'>An Amber Alert has been issued for Kentucky: <a href='" + item.link + "' target='offSite'>" + item.title + "</a></li>");
		    });
		    if (showAmberAlert) {
		    	if ($("#portalBarAmberAlert li").size() > 0) {
		    		$(".portalBarAmberContainer").append("<a href='#' class='closeButton'>Close</a>");
		    	}
		    	if ($("#portalBarAmberAlert li").size() > 1) {
		    		$('#portalBarAmberAlert').cycle({ 
						fx:    'fade', 
						speed: 1000
					});
		    	}
		    	$(".portalBarAmberContainer .closeButton").click ( function() {
		    		$(this).parent().slideUp("slow");
		    		return false;
		    	});
		    	$(".portalBarAmberContainer").slideDown("slow");
		    }
		});
		
		
		
//******************************** jQuery Severe Weather Alerts Initialization *****************************************
//
//

//Fetch Weather Alert JSON and activate alert on the portal if there are active ones.
		var showWeatherAlert = false;
		$.getJSON("http://pipes.yahoo.com/pipes/pipe.run?_id=rmvZfolM3hGwzolRbbsjiw&_render=json&num=1&_callback=?",
		function(data) {
			$.each(data.value.items, function(i, item) {
	    		if (data.value.items.length > 0) showWeatherAlert = true;
	    		if(!(document.getElementById("portalBarWeatherAlert"))) {
	    			// Adjust verbage to correctly reflect the number of alerts
	    			if (data.value.items.length > 1) { 
	    				$("#weatherBox h3").after("<div class='portalBarWeatherContainer'><p>Severe Weather Alerts have been issued for Kentucky <a href='#' class='weather-alerts-link'>Show Alerts</a></p><ul id='portalBarWeatherAlert'></ul></div>");
	    			} else {
	    				$("#weatherBox h3").after("<div class='portalBarWeatherContainer'><p>A severe Weather Alert has been issued for Kentucky <a href='#' class='weather-alerts-link'>Show Alerts</a></p><ul id='portalBarWeatherAlert'></ul></div>");
	    			};
	    		}
	    		
	    		var titleSegment = "";//parse out issuing office and link info.
	    		if (item.title.indexOf("Issued by") != -1) {
	    			titleSegment = item.title.substr(0, item.title.indexOf("Issued by") ); 
	    		} else {
	    			titleSegment = item.title.substr(0, item.title.indexOf("by") );
	    		}
	    		var counties = item.areaDesc.split(";");  //Get area affected info and parse into individual counties
	    		var countySegment = " for ";
	    		if (counties.length == 1) {														//One county named
	    			countySegment += counties[0] + " County";
	    		} else if (counties.length == 2) {												//Two counties named
	    			countySegment += counties[0] + " and " + counties[1] + " Counties";
	    		} else if (counties.length > 2){												//More than two counties named
		    		for (var i=0; i < counties.length; i++) {
		    			if (i == (counties.length - 1)) {
		    				countySegment += "and " + counties[i] + " Counties";
		    			} else {
		    				countySegment += counties[i] + ", ";
		    			}
		    		}
	    		} else {																		//Just in case no counties are listed in the alert
	    			countySegment = "";
	    		}
	    		
	    			    		
	    		$("#portalBarWeatherAlert").append("<li class='weatherAlert'><a href='" + item.link + "' target='offSite'>" + titleSegment + countySegment + "</a></li>");
		        
		    });
		    if (showWeatherAlert) {
			    $(".weather-alerts-link").toggle (
		    		function () {
		    			$("#portalBarWeatherAlert").slideDown("fast");
		    			$(this).text("Hide Alerts");
		    		},
		    		function () {
		    			$("#portalBarWeatherAlert").slideUp("slow");
		    			$(this).text("Show Alerts");
		    		}		    	
		    	);
		    	$(".portalBarWeatherContainer").slideDown("fast");
		    }
		   
		});




//******************************** jQuery Tabs Initialization *****************************************
//
//
//apply jQuery Tabs plugin to Online Services section
	$(".tabs").tabs( { fx: { opacity: 'toggle' } } );




//******************************** jQuery Cycle Initialization for Kentucky's Scrapbook *****************************************
//
//
//Build the control panel and attach the jQuery Cycle plugin to the Kentucky's Scrapbook and Kentucky Spotlight.	
//Create image navigation and control panel
	$('.homeFlickrPhotos ul').after('<div id="controlPanelScrapbook">');
	$('#homeKentuckySpotlight > ul').after('<div id="controlPanelSpotlight">');


//Build the control panel for Scrapbook and Spotlight
	var $prev = "<a href=\"#\" id=\"prevImgScrapbook\"><img src=\"/SiteCollectionImages/homepage/prevbutton.png\" alt=\"Go to previous image\" border=\"0\" /></a>";
	var $play = "<a href=\"#\" id=\"playImgScrapbook\"><img src=\"/SiteCollectionImages/homepage/playbutton.png\" alt=\"Play slideshow\" border=\"0\" /></a>";
	var $pause = "<a href=\"#\" id=\"pauseImgScrapbook\"><img src=\"/SiteCollectionImages/homepage/pausebutton.png\" alt=\"Pause slideshow\" border=\"0\" /></a>";
	var $next = "<a href=\"#\" id=\"nextImgScrapbook\"><img src=\"/SiteCollectionImages/homepage/nextbutton.png\" alt=\"Go to next image\" border=\"0\" /></a>"
	$('#controlPanelScrapbook').append($prev).append($play).append($pause).append($next);
	
	var $prev = "<a href=\"#\" id=\"prevImgSpotlight\"><img src=\"/SiteCollectionImages/homepage/prevbutton.png\" alt=\"Go to previous image\" border=\"0\" /></a>";
	var $play = "<a href=\"#\" id=\"playImgSpotlight\"><img src=\"/SiteCollectionImages/homepage/playbutton.png\" alt=\"Play slideshow\" border=\"0\" /></a>";
	var $pause = "<a href=\"#\" id=\"pauseImgSpotlight\"><img src=\"/SiteCollectionImages/homepage/pausebutton.png\" alt=\"Pause slideshow\" border=\"0\" /></a>";
	var $next = "<a href=\"#\" id=\"nextImgSpotlight\"><img src=\"/SiteCollectionImages/homepage/nextbutton.png\" alt=\"Go to next image\" border=\"0\" /></a>"
	$('#controlPanelSpotlight').append($prev).append($play).append($pause).append($next);

//Attach Play and Pause functionality to control buttons
	$('#playImgScrapbook').click( function() {
		$('#playImgScrapbook').hide();
		$('#pauseImgScrapbook').show();
		$('.homeFlickrPhotos ul').cycle('resume');
		return false;
	});
	$('#pauseImgScrapbook').click( function() {
		$('#pauseImgScrapbook').hide();
		$('#playImgScrapbook').show();
		$('.homeFlickrPhotos ul').cycle('pause');
		return false;
	});
	
	$('#playImgSpotlight').click( function() {
		$('#playImgSpotlight').hide();
		$('#pauseImgSpotlight').show();
		$('#homeKentuckySpotlight ul').cycle('resume');
		return false;
	});
	$('#pauseImgSpotlight').click( function() {
		$('#pauseImgSpotlight').hide();
		$('#playImgSpotlight').show();
		$('#homeKentuckySpotlight ul').cycle('pause');
		return false;
	});


//Attach opacity to control panel via jQuery for ease and consistency, and set the initial opacity
//DON'T delete this. We may repurpose it.
	/*$('#controlPanel').hover( function() {
			$(this).fadeTo(0,1);
		}, function() {
			$(this).fadeTo(0,0.4);
	});
	$('#controlPanel').fadeTo(0,0.4);*/

			
//Set Cycle customizations
	$('.homeFlickrPhotos ul').cycle({ 
		fx:    'fade', 
		speed: 500,
		timeout: 7000, 
		next: '#nextImgScrapbook',
		prev: '#prevImgScrapbook'
	 });
	 
	 $('#homeKentuckySpotlight ul').cycle({ 
		fx:    'fade', 
		speed: 500,
		timeout: 10000, 
		next: '#nextImgSpotlight',
		prev: '#prevImgSpotlight'
	 });



//******************************** jQuery Accordion Initialization *****************************************
//
//
//accordion
	$(function() {
		$(".accordion").accordion({
			autoHeight: false
		});
	});


});

