//Include codeToHTML script
document.write('<script type="text/javascript" src="http://www.uis.edu/php-app/codeToHtml/codeToHtml.js"></script>');


$(function(){
    MenuActiveLink();
    HomepageMods();
    
    //fade and rotate
    //FadeAndRotateInit();
    setInterval('FadeAndRotate()',8000);
});


function FadeAndRotateInit()
{
	$('.FadeAndRotate ":empty"').remove();
    $('.FadeAndRotate span:not(":first")').addClass('hide');
}
function FadeAndRotate()
{
	
    var next = $('.FadeAndRotate span:not(".hide")').next().length==0?$('.FadeAndRotate span:first'):$('.FadeAndRotate span:not(".hide")').next();
    
    $('.FadeAndRotate span:not(".hide")').animate({opacity:0}, function(){
            $('.FadeAndRotate span').addClass('hide');
            next.css({opacity:0}).removeClass('hide').animate({opacity:1});
        });
}



//Homepage modifications
function HomepageMods()
{
    //Homepage mods
	if($('body').hasClass('homepage'))
	{
		//NPR Feed truncate
		$('#homeContentNPR .nprModText').each(function(){
			var html = $(this).text().replace(/\s+/g, " "); //This will remove all extra white spaces in the string, not just trailing and leading.
			$(this).html(truncate(html, 160));
		});
		
		//NPR Feed truncate
		$('#homeContentNPR .nprModHead strong').each(function(){
			var html = $(this).text().replace(/\s+/g, " "); //This will remove all extra white spaces in the string, not just trailing and leading.
			$(this).html(truncate(html, 35));
		});
		
		
		
		//WUIS News feed truncate
		$('#homeContentWUISNews .RSStoHTML p').each(function(){
				       $(this).html(truncate($(this).text(),130));
					});
		$('#homeContentWUISNews .RSStoHTML h2 a').each(function(){
				       $(this).html(truncate($(this).text(),50));
					});
	    $('#homeContentWUISNews').removeClass('height-1px');
		
		
		//Twitter Feed URL
		$('ul.twitterFeed a').each(function(){
										$(this).attr('href','http://twitter.com/wuis');
										$(this).html($(this).html().replace(/wuis:/i,'')); //Remove WUIS prefix
								});
		
        //Load homepage banners
        HomepageBanners();
		
		//Taglinks after everything on homepage is loaded
		//This is done after yql query, in HomepageBanners()
	}
	//End of Homepage mods
	else
	{
		//Else taglinks whenever this func is called
		if(typeof tagLinks == 'function') tagLinks();
	}
}
//End of HomepageMods





//Homepage Banners - YQL
//http://illinois.edu/lb/rss/4093/text.xml
function HomepageBanners()
{
    //Homepage Banners Left
    var bannersFeed = "http://query.yahooapis.com/v1/public/yql?q=select%20title%2C%20category%2C%20link%2C%20description%2C%20enclosure%20from%20rss%20where%20url%3D'http%3A%2F%2Fillinois.edu%2Flb%2Frss%2F4093%2Ftext.xml'&format=json&callback=?";
		
    $.getJSON(bannersFeed, function(data) {
                        var htmlArea1 = "";
                        var htmlArea2 = "";
                        var htmlArea3 = "";
                        $.each(data.query.results.item,function(key, value){
                            if(value.enclosure)
                            {
                                var html = "";
                                if(value.description) html += "<div class='desc'>"+value.description+"</div>";
                                if(value.link) html += "<a href='"+value.link+"'><img src='"+value.enclosure.url+"' alt='"+value.title+"'></a>";
                                else  html += "<img src='"+value.enclosure.url+"' alt='"+value.title+"'>";
                                
                                switch(value.category)
                                {
                                    case 'Area-1':  htmlArea1 += html; break;
                                    case 'Area-2':  htmlArea2 = html; break;
                                    case 'Area-3':  htmlArea3 = html; break;
                                }
                            }
                        });
						
                        $('#homepage_banners_left_area').html(htmlArea1);
                        $('#homeBannersRightTop .banner-1').html(htmlArea2);
                        $('#homeBannersRightTop .banner-2').html(htmlArea3);
                        
                        //Init fade and rotate again.
                        FadeAndRotateInit();
						//Taglinks
						if(typeof tagLinks == 'function') tagLinks();
                    });
}







//Main Menu current class, for the current page
//Top links current link always links to index.html, so search if "index.html" or "../index.html" or "../../index.html" is any of the links. It goes only 4 levels deep
function MenuActiveLink()
{
    $('#menuLinks a').each(function(){
        if($(this).attr('href')=="index.html") $(this).addClass('current');
        if($(this).attr('href')=="../index.html") $(this).addClass('current');
        if($(this).attr('href')=="../../index.html") $(this).addClass('current');
        if($(this).attr('href')=="../../../index.html") $(this).addClass('current');
        if($(this).attr('href')=="../../../../index.html") $(this).addClass('current');
    });

    //If current path is not one of these, then remove current class from the first link, which is home
    var currentPath = window.location.pathname;
    if(currentPath != "/" && currentPath != "/index.html")
        $('#menuLinks a:first').removeClass('current');
}






/**
 * Author: Andrew Hedges, andrew@hedges.name
 * License: free to use, alter, and redistribute without attribution
 */

/**
 * Truncate a string to the given length, breaking at word boundaries and adding an elipsis
 * @param string str String to be truncated
 * @param integer limit Max length of the string
 * @return string
 */
var truncate = function (str, limit) {
	var bits, i;
	/*
	if (STR !== typeof str) {
		return '';
	}*/
	bits = str.split('');
	if (bits.length > limit) {
		for (i = bits.length - 1; i > -1; --i) {
			if (i > limit) {
				bits.length = i;
			}
			else if (' ' === bits[i]) {
				bits.length = i;
				break;
			}
		}
		bits.push('...');
	}
	return bits.join('');
};
// END: truncate






		/* OLD Code*/
		/*
		$('#homeContentWUISNews .RSStoHTML').each(function(){
			var html = "";
			var playNow = "";
			$(this).find('p').each(function(){
											if($(this).find('a').size()==0)
											{
												html += $(this).text();
											}
											else //For play now links
											{
												var playnowLink = $(this).find('a:first');
												playNow = "<a href='"+playnowLink.attr('href')+"'>"+playnowLink.text()+"</a>"
											}
											$(this).remove();
										  });
			$(this).find('h2').after("<p>" + truncate($.trim(html), 130) + "</p>" + "<p class='playNow'>" + playNow + "</p>");
		});
		$('#homeContentWUISNews').removeClass('height-1px');
		*/ /*OLD CODE END*/
