// JavaScript Document
$move_by = 948;
$frame_left = 0;
$frame_no = 1;
$start_middle = 0;
maxItemCount = 12;

function getFeed(url) {
		    $j.get("../proxy.cfm?source="+url, function(xml) {
				$j(xml).find('item').each(function(index) {
				if (index < maxItemCount) { 
				//name the current found item this for this particular loop run
				var $item = $j(this);
				// grab image
				var image = $item.find('media\\:thumbnail').attr('url');
				// original image src sometimes has duplicated host
				if (image) { var validImage = image.toString().replace(/http:\/\/news.discovery.comhttp:\/\/news.discovery.com/g, "http://news.discovery.com") }
				// grab the post title
				var title = $item.find('title').text();
				// grab the post's URL
				var link = $item.find('link').text();
				// next, the description
				var description = $item.find('description').text();
				description = description.substring(0, 100);
				//don't forget the pubdate
				var pubDate = $item.find('pubDate').text();
				// now create a var 'html' to store the markup we're using to output the feed to the browser window
				var html = "<li><a href=\""+link+"\" title=\""+title+"\" class=\"img-link\">";
				if (image != undefined) { html += "<img src=\""+validImage+"\" alt=\""+escape(title)+"\" width=\"106\" height=\"80\" /></a>";} else { html += "<img src=\"/images/dot.gif\" width=\"1\" height=\"1\" /></a>"; } 
				html += "<div class=\"\">";
				html += "<h4>"+title+"</h4>";
				html += "<p>"+description+"...</p>";
				html += "<a href=\""+link+"\" title=\"\" class=\"read-more\" target=\"_blank\">Read more<span class=\"icon-arrow\"></span></a>";
				html += "</div></li>";
				$j('#image_container').append($j(html));
				}
				});
				$j(".ajax-loading-bar").fadeOut(500, function () { 
					$j(this).addClass("hide");
				});
				setUpScroll();
		}, "xml");
	}
	
	function setUpScroll() {
	// need to find correct clicks if odd number
	$j('#image_container').fadeIn(1000);
	$max_clicks = $j("#image_container").children().size() / 3;
	$imgCont = $move_by * $max_clicks;
	$j("#image_container").css({width : $imgCont});

	if($start_middle == 1)
		{
			// Get the middle frame, according to the $max_clicks
			$new_frame_no = ($max_clicks/2).toFixed(0)
			
			// Adjust the frame position
			$new_left = -($new_frame_no * $move_by);						
			$new_left_attr = $new_left+"px";
			
			// Do the move
			$j("#image_container").stop().animate({left: $new_left_attr}, 800 );
			
			// Save the new values
			$frame_left = $new_left;
			$frame_no = ($new_frame_no/1 + 1);
		}
	$j(".next").click(function() {
			/* Set the new position & frame number */
			$new_frame_no = (($frame_no/1)+1);
			$new_left = (($frame_left/1) - $move_by);
			/* Check if we're moving too far over */
			if($new_frame_no > $max_clicks)
				{		
					/* Move all the way right, to the beginning*/
					$new_left = 0;
					$new_frame_no = 1;
				}				
			$new_left_attr = $new_left+"px";
			$j("#image_container").stop().animate({left: $new_left_attr}, 800 );
			$frame_left = $new_left;
			$frame_no = $new_frame_no;
			return false;
		});	
	$j(".prev").click(function() {
			/* Set the new position & frame number */
			$new_frame_no = (($frame_no/1)-1);
			$new_left = (($frame_left/1) + $move_by);
			/* Check if we're moving too far over */
			if($new_frame_no <= 0)
				{
					/* Move the images all the way left, minus one frame */
					$new_left = -($move_by*$max_clicks)+$move_by;
					$new_frame_no = $max_clicks;
				}
			$new_left_attr = $new_left+"px";
			$j("#image_container").stop().animate({left: $new_left_attr}, 800 );
			$frame_left = $new_left;
			$frame_no = $new_frame_no;
			return false;
		});
	
	}

