var LOADER_IMG = '<img src="../images/loader.white.gif" alt="" class="loaderImg" />';
 
google.load("feeds", "1");
 
$(function(){
		
 	/* Accordion */
 	$("ul.accordion").each(function(){
 		$(this).css('overflow-y', 'hidden');
 		/** Actueel column has a fixed height */
 		if(!$(this).is('.KaftTeasers')){
 			$(this).css('height', $(this).outerHeight());
 		}
 	});
   $("li.KaftItem > a.KaftItemLink").bind('mouseover', actueelHover);
   
   
   /** Sites filtering based on category */
   Puddlepuff.FrontPage.Sites.setup({
   		categories : $('ul.KaftSitesCategories'),
   		sites : $('table.KaftSitesIndex')
   });
   Puddlepuff.FrontPage.Sites.enableCategoryFilter();
  
   /** Fill up untill 6 items */
   var numNews = 6 - $('ul.KaftTeasers > li').length;
   if(numNews > 0){ 
	   Puddlepuff.FrontPage.Feeder.fetch({
	   		
	   		url: "http://moblog.net/feeds/blog/puddlepuff",
	   		numResults: numNews,
	   		callback: function(result){
	   	
		   		if(result.feed){  			
		   			if(result.feed.entries.length > 0){
		   				  				
		   				var after = $('ul.KaftTeasers > li.teaserbelowten:last');
		   				if( after.length == 0){
		   					after = $('ul.KaftTeasers > li.teaseraboveten:first');
		   					if(after.length == 0){
		   						after = $('ul.KaftTeasers');
		   					}
		   				}
		   				 
		   				$(result.feed.entries).each(function(){
		   					   				
		   					var li = $('<li class="KaftItem teaserfeed"></li>');
		   					var a = $('<a href="'+ this.link +'" class="KaftItemLink" target="new"></a>');
		   					var span = $('<span class="Text"></span>');
		   					var h = $('<h2 class="Title">'+ this.title +'</h2>');
		   					var p = $('<p class="Body">'+ this.contentSnippet +'</span>');
		   					
		   					/** look for an image */
		   					if(/<img[^<>]+>/.test(this.content)){
		   						var img = $(this.content.match(/<img[^<>]+>/)[0]);
		   						var imgSpan = $('<span class="Image" style="overflow:hidden;position:relative"></span>');
		   							imgSpan.append('<img src="'+ img.attr('src') +'" width="180" alt="" style="position:absolute;top:0px;left:0px;z-index:0" />');
		   							imgSpan.append('<img src="images/transparant.gif" width="28" height="26" align="right" hspace="4" vspace="6" border="0" class="ArrowMoblog" style="position:absolute;right:0px;top:0px;z-index:1">');
		   						a.append(imgSpan);	
		   					}
		   					
		   					span.append(h);
		   					span.append(p);
		   					a.append(span);
		   					li.append(a);
		     				
		     				if(after.is('.teaserbelowten') || after.is('.teaserfeed') ){
		     					after.after(li); 
		     				}else if(after.is('.teaseraboveten')){
		     					after.prepend(li);        				
		     				}else if(after.is('ul')){
		     					after.append(li); 
		     				} 					   					
		   					after = $('ul.KaftTeasers > li.teaserfeed:last');
		   					after.find('> a').bind('mouseover', actueelHover); /** enable mouse hovering */
		   				});		
		   			}			
		   		}
		   	}
	   });
	} 
});

function actueelHover(){
	var parent = $(this).parents('ul');
   if(!$(this).is('.KaftItemLinkSelected')){
   	/** close all open and deselect them after animating */
   	parent.find('a.KaftItemLinkSelected').each(function(){
   		$(this).animate({height: 57}, 320, function(){
   			$(this).removeClass('KaftItemLinkSelected'); 
   		});
   	});
   	
   	/** open me en select me */
   	$(this).animate({height: 175}, 320);
   	$(this).addClass('KaftItemLinkSelected');
   }
}

function scrollToTime(container, time){
	var found = false;
	var cpos = container.position();
	
	if(($.browser.msie) && parseInt($.browser.version) <= 6){
		return;/** i'm sorry */
	}
	
	container.find('tr').each(function(){
		if(!found){
			t = parseFloat($(this).find('td').eq(0).find('a').text().split(':').join(''));
			if(t <= 600){ t+=2400; }
			if(t >= time){
				var prevPos = (t == time)? $(this).position() : $(this).prev('tr').position();
				if(typeof prevPos != "undefined"){ 
					var pos = ((typeof prevPos.top != "undefined")? prevPos.top : 0) - cpos.top;
					container.scrollTop(pos);
				}
				found = true;
			}
		}
	});
}

function padTime(int) {
	if(int < 10){ return '0'+ int; }
	return int;
}

/** 
 * FrontPage
 */
var Puddlepuff = {
	
	FrontPage : {
		
		/**
		 * Create an Accordion animation list.
		 * Currently specific for the FrontPage 
		 */
		/**
		 * @param config {Hash}
		 *		@option container {jQuery} Container of accordion items
		 *		@option items {jQuery} Array of accordion items
		 *		@option activeClass {String} 
		 */
		Accordion : function(config){
			
			var ac = {				

				items : new Array(),
				
				init : function(cf){
					
					this.container = cf.container;
					this.activeClass = cf.activeClass;
					
					var il = cf.items.length;
					for(var i=0; i<il; i++){
						
						/** whoops, FrontPage specific */
						var id = $(cf.items[i]).parents('li.KaftItem').attr('id');
						this.items[id] = {
							playing : false,
							item : cf.items[i]
						};
					}
					this.enable();
					return this;			
				},
				
				enable : function(){
					
					var ref = this;
					for(var item in this.items){						
							item.bind('mouseover', function(){
							   	var id = this.parents('li.KaftItem').attr('id');
							    if(!$(this).is('.'+ ref.activeClass) && !ref.items[id].playing ){
									/*								
							    	parent.find('a.KaftItemLinkSelected').each(function(){
							    		$(this).animate({height: 57}, 320, function(){
							    			$(this).removeClass('KaftItemLinkSelected');
							    		});
							    	});
							    	
							    	$(this).animate({height: 175}, 320);
							    	$(this).addClass('KaftItemLinkSelected');
							    	*/
							    }						
							});						
					}				
				}
			};
			return ac.init(config);		
		},
		
		Sites : {
		
			INITED : false,
			
			/**
			 * @param config.categories {jQuery} category container
			 * @param config.sites {jQuery} sites container
			 */
			setup : function(config){
				this.categories		= config.categories;
				this.sites			= config.sites;
				this.INITED 		= true;
			},
		
			/** filter sites based on chosen category */
			enableCategoryFilter : function(){
				if(this.INITED){
					
					var ref = this;
					this.categories.find('a').bind('click', function(){						
						if(!$(this).is('.Selected')){
							ref['deselectCategory'].apply(ref);
							ref['selectCategory'].apply(ref, [$(this)]);
						}
						$(this).blur();												
						window.focus();						
						return false;
					});
				}				
			},
			
			deselectCategory : function(){
				this.categories.find('.Selected').removeClass('Selected');
				this.sites.find('.Selected').removeClass('Selected');
			},
			
			selectCategory : function(cat){				
				cat.parent().addClass('Selected');
				this.sites.find( 'li.'+ cat.parent().attr('id') ).addClass('Selected');	
			}
		},
		
		Feeder : {
			/**
				@param options Hash of options
					@option url
					@option callback 
					@option numResults {Integer}
					@option returnXML {Boolean} Instead of JSON format use XML as returned format
			*/
			fetch : function(options){
				/** use Google */
				if(typeof options.url == "undefined"){ return; }
				
				var feed = new google.feeds.Feed(options.url);
				if(typeof options.returnXML != "undefined" && (options.returnXML)){
					feed.setResultFormat(google.feeds.Feed.XML_FORMAT);
				}
				feed.setNumEntries( (options.numResults)? options.numResults : 6 );
					feed.load(function(result){
						if(typeof result.error == "undefined"){
							options.callback(result);
						}					
					});				
			}
		}
	}
};
