var started			= false;
var activePromoID	= 0;
var promoList		= [];

var Promos	= {
	
	init	: function ( )
	{
		// Load the xml.
		$.ajax({
			url: "http://ajax.googleapis.com/ajax/services/feed/load?v=1.0&num=100&callback=?&q=" + encodeURIComponent("http://www.padevavra.com/rss.php?action=newblogs&type=rss"),
			dataType: "json",
			success: function ( data )
			{
				
				// Get the list of entries.
				var entryList	= data.responseData.feed.entries;
				var id			= 0;
				
				
				// Loop through and create press vo.
				for ( var i = 0; i < entryList.length; ++i )
				{ 
					var entry				= entryList[i];
					
					if ( entry.title.indexOf("promo") >= 0 )
					{
						
						var promoVO			= {};
						
						promoVO.id			= id;
						promoVO.sortOrder	= 100;
						
						id++;
						
						
						// Get the content.	
						var c					= entry.content;
						
						
						$(c).find("img").each( function ( )
						{
							
							promoVO.image	= $(this).attr("src");
									
						}); // end find images.
						
						var temp		= "<temp>" + entry.content + "</temp>";
						// Get sort order.
						$(temp).find("p").each( function ( )
						{
							var so	= $(this).text( );
							if (so.indexOf("sort order:") >= 0 )
							{
								
								promoVO.sortOrder	= so.substr( so.indexOf(":") + 1, 2 );
								//alert( "so: " + promoVO.sortOrder );	
							}
						});	
						
						
						promoVO.linkURL	= $(temp).find("p").first( ).text( );
						
						promoList.push( promoVO );
					}
				
				} // end for.
				
				promoList.sort( function ( a, b ) 
								{
									var a1 = a.sortOrder, b1 = b.sortOrder;
									if ( a1 == b1 ) return 0;
									return a1 > b1 ? 1 : -1;
								});
				
				Promos.displayPromo( );
				
			}, // end success.
			
			error: function ( x, t )
			{
				alert( "error" );
			} // end error.
				
		}); // end ajax.
		
	},
	
	displayPromo : function ( )
	{
		var vo			= promoList[activePromoID];
		activePromoID	= ( activePromoID >= promoList.length ) ? 0 : activePromoID + 1;
		var linkURL	= "http://padevavra.com/products/" + vo.linkURL + ".html";
		linkURL		= linkURL.replace(/ /g, "-");
		$("#product a").attr('href', linkURL);
		
		// Check if one is displayed.
		if ( started )
		{
			
			$("#product img").attr('src', vo.image);
			$("#product img").fadeIn(400).delay(10000).fadeOut(400, function( )
			{
				Promos.displayPromo( );
			});
		}
		
		else
		{
			
			started = true;
			$("#product img").attr('src', vo.image).delay(10000).fadeOut(400, function( )
			{
				Promos.displayPromo( );
			});
		}
	},
	
	promoClick	: function ( event )
	{
		
			
	},
	
		
};

$(document).ready( function( )
{
	Promos.init();
});
