// JavaScript to interpolate random images into a page.

function pickRandom(range) {
	if (Math.random)
		return Math.round(Math.random() * (range-1));
	else {
		var now = new Date();
		return (now.getTime() / 1000) % range;
	}
}

//set up arrays for data
var gift1_img = new Array(); // Array to hold filepaths of images

var gift1_url = new Array(); //Hold URLs for each image

var gift1_dsc = new Array();	//Hold Image Description

var gift_link = [];
gift_link[0] = "/site/apps/nl/rss2.asp?c=ovIXIhN0JuE&b=5692909"; //rss feed col1

// pickRandom - Return a random number in a given range. If we're running
// on an older browser that doesn't support 'Math.random()', we can fake
// it by using the current time. This isn't ideal for mission-critical
// security applications, but it's fine here. Note that we divide the
// current time by 1000 to get rid of the milliseconds which Navigator
// doesn't seem to take into account.

$.event.add(window, 'load', function() {
	getImages();
});

function pickImage(gift, pic) {
	var selhref = $(".pickImage"+gift);
	for (x=0;x<selhref.length;x++) {
		selhref[x].className = "pickImage"+gift;
		if ((x+1) == pic) selhref[x].className += " imgsel";
	}
	$("#gift1").attr("src",gift1_img[(pic-1)]);
	$("#gift1dsc").html(gift1_dsc[(pic-1)]);
	var html = '';
	if (pic>1) html += "<a class='pickImage1' style='color:#999999; text-decoration:none' href='javascript:void(0)' onclick='pickImage(1,"+(pic-1)+")'>&laquo;</a>&nbsp;&nbsp;";
	else html += "&laquo;&nbsp;&nbsp;";
	if (pic<(gift1_img.length)) html += "<a class='pickImage1' style='color:#999999; text-decoration:none' href='javascript:void(0)' onclick='pickImage(1,"+(pic+1)+")'>&raquo;</a>";
	else html += "&raquo;";
	$('#col1count').html(html);
}
	
function getImages() {
	$.get(gift_link[0], function(data) {
			//find each 'item' in the file and parse it
			var count = 1;
			$(data).find('item').each(function() {
				//name the current found item this for this particular loop run
				var $item = $(this);
				for (i=0; $item.length>i; i++)
				{
					var dayval = '', daylen = 0;
					
					for (k=0; $item[i].childNodes.length>k; k++)
					{
						if($item[i].childNodes[k].firstChild != null) {
							 switch($item[i].childNodes[k].tagName) {
								 case "description" : gift1_dsc[(count-1)] = $item[i].childNodes[k].firstChild.data;
												break;
								 case "k:feature_image" :
								 case "feature_image" : gift1_img[(count-1)] = $item[i].childNodes[k].firstChild.data;
												break;
							}
						} 
					 }
				}
				//$('#col1count')[0].innerHTML += "<a class='pickImage1' style='color:#289ab8' href='#' onClick='pickImage(1,"+count+")'>"+count+"</a> &nbsp; &nbsp;";
				count++;
			});
			pickImage(1, (pickRandom(count-1)+1) );
	});
}