(function($) {
	$.fn.renderIlike = function(options) {
		return this.each(function() {
			var ilikeContainer = $('<div />').attr("id", "ilike").html($(this).html());
			ilikeContainer.prependTo('body');
			$(this).remove();
			
			$('.our-sites').ilikeFoldOut();
			var news = [];
			var mag = [];
			var chars = [];
			var urls = [];
			var newsContainer = $('span#ilike-news-item');
			var marker = $('span#marker');
			var magzContainer = $('span#ilike-mag');
			var currChar, timer, charTimer, markerIntervall, startTimer;
			var newsIndex = 0; 
			var charIndex = 0;
			
			//next
			$('.ilike-next').click(function(){
				NextPrevItem(1);
				return false;
			});
			//prev
			$('.ilike-prev').click(function(){
				NextPrevItem(-1);
				return false;
			});
			
			//Set target attrubute on all links, due to html specs we do this in javascript
			$('#ilike a').each(function(){
				$(this).attr('target','_blank');
			});
		
			//If the client is safari: attach some safari search attributes
			if($.browser.safari) {
				var ilikesearch = $('#ilikesearch');
				$('#ilikesearch').attr('placeholder', 'Sökord').attr('autosave','iLike').attr('results','5');
				//small hack to make it work
				ilikesearch[0].type="search";
			}
			
			//fetch news from Ilike	
			//$.getJSON("/templates/iliketest/news.html",
			//$.getJSON("/hej_foo/ilike-bar/news.html", // alex
			$.getJSON("http://www.ilike.se/ilike-bar/news.html?callback=?",
			    function(data){
				$.each(data.items, function(i,item){
					var charstemp =  item.title.split("");
					chars[i] = charstemp;
					mag[i] = item.magazine;
					urls[i] = item.link;
				});
				
				clearTimeout(timer);
					
				timer = setTimeout(function() {
					newsContainer.empty();
					OutputNews(0, mag, 0, chars[0]);
				}, 1000);
					

		     });			 
			
			 var OutputNews = function(newsIndex, newsItemsArr, charIndex, charArr) {
				clearTimeout(timer);
				clearTimeout(charTimer);
				clearTimeout(startTimer);
				
				if(charIndex == 0) {
					//start marker
					markerIntervall = setInterval(function(){
						if(marker.css("display") == 'inline') {
							marker.css('display','none');
						}
						else {
							marker.css('display','inline');
						}
					},400);
					
					//set magazine
					newsContainer.empty();
					magzContainer.empty();
					magzContainer.html(newsItemsArr[newsIndex] + ": ");
					magzContainer.parent('a').attr('href',urls[newsIndex]+'?utm_source='+options.roxendomain+'&utm_medium=ilikebar').attr('target','_blank');
					
					//vänta lite före nyheten skrivs ut!
					startTimer = setTimeout(function(){
						//stop marker
						clearInterval(markerIntervall);
						marker.css('display','inline');
						
						charIndex=0;
						newsContainer.empty();
						
						//print first char then start looping
						newsContainer.text(newsContainer.text() + charArr[charIndex]);
				 		charIndex=1;
						OutputNews(newsIndex,newsItemsArr,charIndex,chars[newsIndex]);
					 	
					},1000);
				}
				else {
					if(charIndex < charArr.length) {
						newsContainer.text(newsContainer.text() + charArr[charIndex]);
				 		charIndex++;
						charTimer = setTimeout(function() {
							OutputNews(newsIndex,newsItemsArr,charIndex,chars[newsIndex]);
						}, 70);
				 	}
					else {
						//start marker
						markerIntervall = setInterval(function(){
							if(marker.css("display") == 'inline') {
								marker.css('display','none');
							}
							else {
								marker.css('display','inline');
							}
						},400);
							
						charIndex = 0;
						timer = setTimeout(function() {
							//stop marker
							clearInterval(markerIntervall);
							marker.css('display','inline');
							
							newsIndex++;
							if(newsIndex >= chars.length) {
								//start over!
								newsIndex=0;
							}
							
							OutputNews(newsIndex, newsItemsArr, charIndex,chars[newsIndex]);
						},4000);
						
					}
				}
			 }
			 
			 var NextPrevItem = function(nextprev) {
				charIndex = 0;
				clearTimeout(charTimer);
				clearTimeout(timer);
				clearInterval(markerIntervall);
				clearTimeout(startTimer);
				newsContainer.empty();
				
				newsIndex = newsIndex + nextprev;
				if(newsIndex >= chars.length) {
					//start over!
					newsIndex=0;
				}
				if(newsIndex < 0) {
					//go to last news item
					newsIndex=chars.length-1;
				}
				OutputNews(newsIndex, mag, charIndex,chars[newsIndex]);
			
			 }
			 
		
			
		});
	}
	
	
	$.fn.ilikeFoldOut = function(options) {
        $.extend({}, options);
        return this.bind('click', function(e) {
			if($('#ilike-list').css('display') == 'block') {
				$(this).removeClass('ilike-selected');
				$('#ilike-list').slideUp("normal");
			}
			else {
				$(this).addClass('ilike-selected');
				$('#ilike-list').slideDown("normal");	
			}
			return false;
		});
    }
})(jQuery);
