//
//  Name:  base.js
//  Author:  drew.zimber@gmail.com
//  Description:  Core javascript file for application.
//

// method that drives the site
// specified page determines what's loaded
var LOADING_HTML = "<br/><br/><br/><br/><br/><br/><br/><br/><span class=\"title\">Loading....</span><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>";

//
//  INIT
// 
function init(page, id)
{   
    // get the nav and content objects and 
    // set to the defaults
    var homeNav = document.getElementById("homeNav");
    var trNav = document.getElementById("trNav");
    var newsNav = document.getElementById("newsNav");
    var weatherNav = document.getElementById("weatherNav");
    var rogNav = document.getElementById("rogNav");
    
    var footer = document.getElementById("footer");
    footer.style.display="none";
    
    var contentArea = document.getElementById("contentArea");
    contentArea.innerHTML = LOADING_HTML;
    contentArea.style.display = "none";
    
    var weatherList = document.getElementById("weatherList");
    weatherList.style.display = "none";
    
    var listArea = document.getElementById("listArea");
    listArea.style.display = "";
    
    homeNav.className = "nav";
    trNav.className = "nav";
    newsNav.className = "nav";
    weatherNav.className = "nav";
    rogNav.className = "nav";
    

    // based on the specified page,
    // load the data and set the table
    if ( page == "tr" )
    {    
        //alert("test:" + navigator.appName + "||" + navigator.appVersion);

        //getBlogInfo('1', '0', '0', '1', '3');
        //getBlogListInfo('1', '0', '0', '1', '100');
        getBlogInfoAndList('1', '0', '0', '1', '3', '100');
        
        trNav.className = "navon";
    }
    else if ( page == "news" )
    {
        //getBlogInfo('1', '0', '1', '0', '3');
        //getBlogListInfo('1', '0', '1', '0', '100');
        getBlogInfoAndList('1', '0', '1', '0', '3', '100');
        
        newsNav.className = "navon";
    }
    else if ( page == "weather" )
    {
        weatherList.style.display = "";
        listArea.style.display = "none";
        contentArea.style.display = "";
        
        showWeatherList('NH');
        
        weatherNav.className = "navon";
        getWeatherInfo('UA035', 'Mount Washington', 'NH');
    }
    else if ( page == "rog" )
    {
        //getBlogInfo('2', '1', '1', '1', '3');
        //getBlogListInfo('2', '1', '1', '1', '100');
        getBlogInfoAndList('2', '1', '1', '1', '3', '100');
        
        rogNav.className = "navon";
    }
    // default to the home page
    else
    { 
        if ( id != null )
        { 
            getBlogEntry(id);
            getBlogListInfo('1', '1', '1', '1', '100');
        }
        else
        {
            //getBlogInfo('1', '1', '1', '1', '3');
            getBlogInfoAndList('1', '1', '1', '1', '3', '100');
        }
        //getBlogListInfo('1', '1', '1', '1', '100');
        
        homeNav.className = "navon";
    } 
}


// get the weather feed from web service based on the 
// specified location code
function getWeatherInfo(locationCode, city, state)
{
    // begin JQUERY/XML logic
	$(document).ready(function()
	{		
	    var url = "service.php?a=0&lc=" + locationCode;
	//alert(url);
	    // call to the web service
		$.get(url, {}, function(xml)
		{	
			// weather-info is the high level node
			$("weather-info", xml).each(function(i)
			{
			    var count = 0;
			    var html = null;
			    var link = null;
			    var contentArea = document.getElementById("contentArea");
			    contentArea.innerHTML = "<br/>Loading...<br/>";
			    
			    $("item", $(this)).each(function(j)
				{
				    var title = $(this).attr("title");
				    var icon = $(this).attr("icon");
				    var desc = $(this).text();	
				    
				    if ( count == 0 )
				    {
				        link = $(this).attr("link");
				    }	    
				    
			        html = html + "<span class=\"field\">&nbsp;&nbsp;";
			        html = html + "<b>" + title + ":</b><br/>";
			        html = html + "&nbsp;&nbsp;" + icon + "&nbsp;&nbsp;" + desc;
			        html = html + "</span><br/>";
			        
			        count++;
				});
				
				// weather links
			    var weatherLinks = "<a href=\"" + link + "\" target=\"_blank\" class=\"fieldboldgold\">Detail at AccuWeather.com</a>";
				weatherLinks = weatherLinks + "&nbsp;&nbsp;&nbsp;";
				
				link = "javascript:goWeatherGov('" + city + "','" + state + "')";

				weatherLinks = weatherLinks + "<a href=\"" + link + "\" class=\"fieldboldgold\">Detail at Weather.gov (NOAA)</a></span>&nbsp;&nbsp;&nbsp;";
				
				link = "http://www.weather.com/weather/map/interactive/USNH0102?from=36hr_maps&zoom=8&interactiveMapLayer=radar";
				weatherLinks = weatherLinks + "<a href=\"" + link + "\" target=\"_blank\" class=\"fieldboldgold\">Radar on Weather.com</a></span>";
				weatherLinks = weatherLinks + "&nbsp;&nbsp;&nbsp;";
				
				link = "http://bestskiweather.com/newengland.php";
				weatherLinks = weatherLinks + "<a href=\"" + link + "\" target=\"_blank\" class=\"fieldboldgold\">Snow Depth at BestSkiWeather.com</a></span><br/><br/>";		
				
				// accuweather/noaa
				html = weatherLinks + html;

    			contentArea.innerHTML = contentArea.innerHTML + html;
    			contentArea.style.display = "";
    			buildFooterHtml();
			});
		});
	});
}

// display the weather list for the specified
// state
function showWeatherList(state)
{
    // step one whack all other lists
    var meList = document.getElementById("meweatherlist");
    var maList = document.getElementById("maweatherlist");
    var nhList = document.getElementById("nhweatherlist");
    var nyList = document.getElementById("nyweatherlist");
    var vtList = document.getElementById("vtweatherlist");
   
    meList.style.display = "none";
    maList.style.display = "none";
    nhList.style.display = "none";
    nyList.style.display = "none";
    vtList.style.display = "none";    
       
    if ( state == "ME" )
    {   
        meList.style.display = "inline";
    }
    else if ( state == "MA" )
    {
        maList.style.display = "inline";
    }
    else if ( state == "NH" )
    {
        nhList.style.display = "inline";
    }
    else if ( state == "NY" )
    {
        nyList.style.display = "inline";
    }
    else if ( state == "VT" )
    {
        vtList.style.display = "inline";
    }
}


// get blog info and blog list xml based on what kind of entries 
// should be returned, and how many.
// populate content area
function getBlogInfoAndList(blogId, blog, news, tr, count, listCount)
{
     // begin JQUERY/XML logic
	$(document).ready(function()
	{		
	    var url = "service.php?a=7&bid=" + blogId + "&bl=" + blog + "&ns=" + news + "&tr=" + tr + "&c=" + count + "&lc=" + listCount;
//alert(url);
	    // call to the web service
		$.get(url, {}, function(xml)
		{	
		    var html = null;
		    var contentArea = document.getElementById("contentArea");
			contentArea.innerHTML = "<br/>Loading...<br/>";
			
			// blog-info is the high level node
			$("blog-info", xml).each(function(i)
			{   
			    //
			    // build BLOG content
			    //
			    var html = "<div id=\"blog\" align=\"left\"><br/><br/><br/>";
			    html = html + "<code><pre>$(\"img\").lazyload({placeholder : \"images/grey.gif\", container: $(\"#contentArea\")});</pre></code>";
			    
			    $("item", $(this)).each(function(j)
				{
     				var title = $(this).attr("title");
	    		    var date = $(this).attr("date");
	    		    var content = $(this).text();
			    
		    	    html = html + "<br/><br/><span class=\"title\">";
		    	    html = html +  formatDate( date ) + "&nbsp;-&nbsp;" + title + "</span><br/>";
		    	    html = html + "<br/><span class=\"field\">" + content + "</span><br/>";
				});
				
				html = html + "</div>";

				contentArea.innerHTML = html;
				contentArea.style.display = "";
				
			    //
			    // build blog list
			    //

	    		html = "<br/><br/><br/><br/><br/>";
	    	    var listArea = document.getElementById("listArea");
	    		listArea.innerHTML = "<br/>Loading...<br/>";

                        

	    		$("blog-list", xml).each(function(i)
	    		{   
	    		    html += "<span class=\"fieldsmall\">";
			    
	    		    $("blogitem", $(this)).each(function(j)
	    			{
	    			    var id = $(this).attr("id");
         				var title = $(this).attr("title");
	        		    var date = $(this).attr("date");
			    
	    	    	    html = html +  "<br/><b><a href=\"javascript:getBlogEntry('" + id + "')\" class=\"fieldboldsmallgold\">" + formatDate( date ) + "</a></b><br/>" + title + "<br/>";
	    			});
				
	    			html = html + "</span><br/>";
	    	    }); 
		    
		        listArea.innerHTML = html;
				buildFooterHtml();
		    }); 
		});
    });
}


// get blog info xml based on what kind of entries 
// should be returned, and how many.
// populate content area
function getBlogInfo(blogId, blog, news, tr, count)
{
     // begin JQUERY/XML logic
	$(document).ready(function()
	{		
	    var url = "service.php?a=1&bid=" + blogId + "&bl=" + blog + "&ns=" + news + "&tr=" + tr + "&c=" + count;
//alert(url);
	    // call to the web service
		$.get(url, {}, function(xml)
		{	
		    var html = null;
		    var contentArea = document.getElementById("contentArea");
			contentArea.innerHTML = "<br/>Loading...<br/>";
		
			// blog-info is the high level node
			$("blog-info", xml).each(function(i)
			{   
			    var html = "<div id=\"blog\" align=\"left\"><br/><br/><br/>";
			    html = html + "<code><pre>$(\"img\").lazyload({placeholder : \"images/grey.gif\", container: $(\"#contentArea\")});</pre></code>";
			    
			    $("item", $(this)).each(function(j)
				{
     				var title = $(this).attr("title");
	    		    var date = $(this).attr("date");
	    		    var content = $(this).text();
			    
		    	    html = html + "<br/><br/><span class=\"title\">";
		    	    html = html +  formatDate( date ) + "&nbsp;-&nbsp;" + title + "</span><br/>";
		    	    html = html + "<br/><span class=\"field\">" + content + "</span><br/>";
				});
				
				html = html + "</div>";

				contentArea.innerHTML = html;
				contentArea.style.display = "";
				buildFooterHtml();
		    }); 
		});
    });
}

// get blog info xml for single entry, by id
function getBlogEntry(Id)
{
     // begin JQUERY/XML logic
	$(document).ready(function()
	{		
	    var url = "service.php?a=4&id=" + Id;

	    // call to the web service
		$.get(url, {}, function(xml)
		{	
		    var html = null;
		    var contentArea = document.getElementById("contentArea");
			contentArea.innerHTML = "<br/>Loading...<br/>";
		
			// blog-info is the high level node
			$("blog-info", xml).each(function(i)
			{   
			    var html = "<div id=\"blog\" align=\"left\"><br/><br/><br/>";
			    html = html + "<code><pre>$(\"img\").lazyload({placeholder : \"images/grey.gif\", container: $(\"#contentArea\")});</pre></code>";
			    
			    $("item", $(this)).each(function(j)
				{
     				var title = $(this).attr("title");
	    		    var date = $(this).attr("date");
	    		    var content = $(this).text();
			    
		    	    html = html + "<br/><span class=\"title\">";
		    	    html = html +  formatDate( date ) + "&nbsp;-&nbsp;" + title + "</span><br/>";
		    	    html = html + "<br/><span class=\"field\">" + content + "</span><br/><br/>";
				});
				
				html = html + "</div>";
				
				contentArea.innerHTML = html;
				contentArea.style.display = "";
				buildFooterHtml();
		    }); 
		});
    });
}

// get the blog list XML object based on entry types
// and how many
function getBlogListInfo(blogId, blog, news, tr, count)
{
     // begin JQUERY/XML logic
	$(document).ready(function()
	{		
	    var url = "service.php?a=3&bid=" + blogId + "&bl=" + blog + "&ns=" + news + "&tr=" + tr + "&c=" + count;
//alert(url);	    
	    // call to the web service
		$.get(url, {}, function(xml)
		{	
		    var html = null;
		    var listArea = document.getElementById("listArea");
			listArea.innerHTML = "<br/>Loading...<br/>";
		
			// blog-list is the high level node
			$("blog-list", xml).each(function(i)
			{   
			    html = "<span class=\"fieldsmall\">";
			    
			    $("item", $(this)).each(function(j)
				{
				    var id = $(this).attr("id");
     				var title = $(this).attr("title");
	    		    var date = $(this).attr("date");
			    
		    	    html = html +  "<br/><b><a href=\"javascript:getBlogEntry('" + id + "')\" class=\"fieldboldsmallgold\">" + formatDate( date ) + "</a></b><br/>" + title + "<br/>";
				});
				
				html = html + "</span><br/>";
		    }); 
		    
		    listArea.innerHTML = html;
		});
    });
}

// get the blog list XML object based on entry types
// and how many
function getSteepAndCheapInfo()
{
     // begin JQUERY/XML logic
	$(document).ready(function()
	{		
	    var url = "service.php?a=6";
	    
	    // call to the web service
		$.get(url, {}, function(xml)
		{	
		    var html = null;
		    var listArea = document.getElementById("contentArea");
			listArea.innerHTML = "<br/>Loading...<br/>";
		
			// blog-list is the high level node
			$("steep-and-cheap", xml).each(function(i)
			{   
			    $("item", $(this)).each(function(j)
				{
				    var link = $(this).attr("link");
     				var title = $(this).attr("title");
			    
		    	    html = html +  "<br/><b><a href=\"" + link + "\">" + title + "</a></b><br/>";
				});
		    }); 
		    
		    listArea.innerHTML = html;
		});
    });
}

function buildFooterHtml()
{
    var html = "<br/><span class=\"fieldbold\">Copyright 2011 Eastern Backcountry  |  <a class=\"fieldboldgold\" href=\"mailto:admin@easternbackcountry.com\">Contact</a> |  <a class=\"fieldboldgold\" href=\"http://easternbackcountry.com/rss.php?blog=main\" target=\"_blank\">RSS</a></span><br/><br/><br/>";
    var footer = document.getElementById("footer");
    
    footer.innerHTML = html;
    footer.style.display = "";
}

function goWeatherGov(city, state)
{
    var url = "http://forecast.weather.gov/zipcity.php?inputstring=" + city + "%2C" + state + "&Go2=Go";
    url = url.replace(" ", "+");
    
    //window.location.href = url;
    window.open(url);

}

function randomNumber ( n )
{
  return ( Math.floor ( Math.random ( ) * n + 1 ) );
}

// expects yyyy-mm-dd
function formatDate( dateIn )
{
    var dateTemp = new Date();
    var dateParts = dateIn.split("-");
    
    dateTemp.setFullYear(dateParts[0], (dateParts[1]-1), dateParts[2]);
    
    return dateFormat(dateTemp, "mmm dd, yyyy");
}

