//SCROLLABLE NAVIGATOR - custom
var titlesArray = new Array();
var linksArray = new Array();
var imagesArray = new Array();
var orderedImagesArray = new Array();
var imagesArticleLinkIndexArray = new Array();
var divImageID = new String();
var titleCounter = 0;
var linkCounter = 0;
var imageCounter = -1;
var selectedStoryArrow = "http://www.thejesusrehab.com/storage/carousel_arrow_selected.png";
var arrowPlaceHolder = "http://www.thejesusrehab.com/storage/spaceHolder.JPG";
var showStoryImageWidth = 130;
var showStoryImageHeight = 65;
 
$(document).ready(function()
{

	//if the cookie has not been set, the splash page will display
	checkCookie();

	$.ajax({
		type: "GET",
		url: "http://www.thejesusrehab.com/news/rss.xml",
		dataType: "xml",
		success: parseXml
	});
 
	$("#image0").click(function(){
		$("#selectedStoryImage").attr("src", orderedImagesArray[0]);
		$(".selectedStoryLink").attr("href",linksArray[0]);
		$("#selectedStoryTitle").html(titlesArray[0]);
		$("#arrow0").attr("src",selectedStoryArrow);		
		$("#arrow1").attr("src",arrowPlaceHolder);				
		$("#arrow2").attr("src",arrowPlaceHolder);						
		$("#arrow3").attr("src",arrowPlaceHolder);						
	});  

	$("#image1").click(function(){
		$("#selectedStoryImage").attr("src", orderedImagesArray[1]);
		$(".selectedStoryLink").attr("href",linksArray[1]);
		$("#selectedStoryTitle").html(titlesArray[1]);	
		$("#arrow1").attr("src",selectedStoryArrow);
		$("#arrow0").attr("src",arrowPlaceHolder);				
		$("#arrow2").attr("src",arrowPlaceHolder);						
		$("#arrow3").attr("src",arrowPlaceHolder);								
	});  

	$("#image2").click(function(){
		$("#selectedStoryImage").attr("src", orderedImagesArray[2]);
		$(".selectedStoryLink").attr("href",linksArray[2]);
		$("#selectedStoryTitle").html(titlesArray[2]);	
		$("#arrow2").attr("src",selectedStoryArrow);		
		$("#arrow1").attr("src",arrowPlaceHolder);				
		$("#arrow0").attr("src",arrowPlaceHolder);						
		$("#arrow3").attr("src",arrowPlaceHolder);								
	});  	

	$("#image3").click(function(){
		$("#selectedStoryImage").attr("src", orderedImagesArray[3]);
		$(".selectedStoryLink").attr("href",linksArray[3]);
		$("#selectedStoryTitle").html(titlesArray[3]);		
		$("#arrow3").attr("src",selectedStoryArrow);
		$("#arrow1").attr("src",arrowPlaceHolder);				
		$("#arrow2").attr("src",arrowPlaceHolder);						
		$("#arrow0").attr("src",arrowPlaceHolder);								
	});   
	
//NEXT SHOW FEED - http://feed2js.org/
	var rssItem = $(".rss-items").html();
	
	if(rssItem == null || rssItem == "")
	{
		$(".rss-items").html("We are chilling at home.<br>Check back here for show updates or sign up for our <a href=\"http://www.thejesusrehab.com/join-rehab\">mailing list</a> to get the latest info.");
	}	

//OVERLAYS - http://flowplayer.org/tools/overlay/index.html	
	$("div[rel]").overlay();
	$("img[rel]").overlay();	

//IMAGE ROTATOR - http://designm.ag/tutorials/image-rotator-css-jquery/
	//Show Banner
	$(".main_image .desc").show(); //Show Banner
	$(".main_image .block").animate({ opacity: 0.85 }, 1 ); //Set Opacity
 
	//Click and Hover events for thumbnail list
	$(".image_thumb ul li:first").addClass('active'); 
	$(".image_thumb ul li").click(function(){ 
		//Set Variables
		var imgAlt = $(this).find('img').attr("alt"); //Get Alt Tag of Image
		var imgTitle = $(this).find('a').attr("href"); //Get Main Image URL
		var imgDesc = $(this).find('.block').html(); 	//Get HTML of block
		var imgDescHeight = $(".main_image").find('.block').height();	//Calculate height of block	
		
		if ($(this).is(".active")) {  //If it's already active, then...
			return false; // Don't click through
		} else {
			//Animate the Teaser				
			$(".main_image .block").animate({ opacity: 0, marginBottom: -imgDescHeight }, 250 , function() {
				$(".main_image .block").html(imgDesc).animate({ opacity: 0.85,	marginBottom: "0" }, 250 );
				$(".main_image img").attr({ src: imgTitle , alt: imgAlt});
			});
		}
		
		$(".image_thumb ul li").removeClass('active'); //Remove class of 'active' on all lists
		$(this).addClass('active');  //add class of 'active' on this list only
		return false;
		
	}) .hover(function(){
		$(this).addClass('hover');
		}, function() {
		$(this).removeClass('hover');
	});
			
	//Toggle Teaser
	$("a.collapse").click(function(){
		$(".main_image .block").slideToggle();
		$("a.collapse").toggleClass("show");
	});	
	
	//Change Banner URL to prevent duplicate iFrames/Players
	$("h1#siteTitle > a").attr('href','http://www.thejesusrehab.com/home');
	
	//twitter feed
	$("#twitter").getTwitter({
		userName: "thejesusrehab",
		numTweets: 1,
		loaderText: "Loading tweets...",
		slideIn: false,
		slideDuration: 750,
		showHeading: false,
		headingText: "Latest Tweets",
		showProfileLink: false,
		showTimestamp: false
	});	
});
 
 
function parseXml(xml)
{
	titleCounter = 0;
	linkCounter = 0;
	$(xml).find("title").each(function()
	{
		var aTitle = $(this).text();
		if(aTitle != "News")
		{
			if(titleCounter < 4)
			{
				titlesArray[titleCounter] = aTitle;
			}

			titleCounter = titleCounter + 1;
		}

		$("#selectedStoryTitle").html(titlesArray[0]);			
	});

	$(xml).find("link").each(function()
	{
		var aLink = $(this).text();
		if(aLink != "http://www.thejesusrehab.com/news/")
		{
			if(linkCounter < 4)
			{
				linksArray[linkCounter] = aLink;
				getHTML(aLink);
			}

			linkCounter = linkCounter + 1;
		}
 
		$(".selectedStoryLink").attr("href",linksArray[0]);		
	});

}

 
function getHTML(journalEntryURL)
{
	$.ajax({
		type: "GET",
		url: journalEntryURL,
		dataType: "html",
		success: parseHtml
	});		
}

 
function parseHtml(html)
{
	imageCounter = imageCounter + 1;
	$(html).find(".journal-entry-text").each(function()
	{
		($(this).contents()).find("img:first").each(function()
		{
			imagesArray[imageCounter] = $(this).attr("src");
		});

		($(this).contents()).find(".journal-entry-navigation-current").each(function()
		{
			imagesArticleLinkIndexArray[imageCounter] = "http://www.thejesusrehab.com" + $(this).attr("href");
		});
	});

	if(imageCounter == 3)
	{
		for(i=0; i<linksArray.length; i++)
		{
			for(x=0; x<imagesArticleLinkIndexArray.length; x++)
			{
				if(linksArray[i] == imagesArticleLinkIndexArray[x])
				{
					divImageID = "#image" + i;			
					$(divImageID).attr("src", imagesArray[x]);
					$(divImageID).attr("width", showStoryImageWidth);
					$(divImageID).attr("height", showStoryImageHeight);					
					orderedImagesArray[i] = imagesArray[x];					
				}
			}
		}

		$("#selectedStoryImage").attr("src", orderedImagesArray[0]); 
		$("#imageZero").append(imagesArray[0]);
		$("#linkZero").append(linksArray[0]);
		$("#titlesZero").append(titlesArray[0]);
		$("#arrow0").attr("src",selectedStoryArrow);		
		$("#arrow1").attr("src",arrowPlaceHolder);				
		$("#arrow2").attr("src",arrowPlaceHolder);						
		$("#arrow3").attr("src",arrowPlaceHolder);			
	}
}

//SPLASH PAGE FUNCTIONS
function getCookie(c_name)
{
	if (document.cookie.length>0)
	  {
	  c_start=document.cookie.indexOf(c_name + "=");

	  if (c_start!=-1)
		{ 
			c_start=c_start + c_name.length+1 ;
			c_end=document.cookie.indexOf(";",c_start);
			if (c_end==-1) c_end=document.cookie.length
			return unescape(document.cookie.substring(c_start,c_end));
		} 
	  }
	return ""
}

function setCookie(c_name,value)
{
	document.cookie=c_name+ "=" +escape(value);
}

function checkCookie()
{
	overlayCookieFlag=getCookie('overlayCookie');

	if(overlayCookieFlag != "previously-displayed")
	{
		$("#splashpage").overlay({
			// custom top position
			top: 20,
			// some mask tweaks suitable for overlayBox-looking dialogs
			mask: {
				// you might also consider a "transparent" color for the mask
				color: '#ffffff',
				// load mask a little faster
				loadSpeed: 200,
				// very transparent
				opacity: 0.5
			},
		 
			// disable this for modal dialog-type of overlays
			closeOnClick: false,
			// load it immediately after the construction
			load: true
		});
		
		setCookie('overlayCookie',"previously-displayed");
	}
}
