$(function() { 
	newsUpdate.init();
});

var first = 1;
var newsUpdate = {
	counter: 0,
	now: '',
	interval: 5000,
	dTitle: '',
	newsHtml: '',
	
	cWrap: "#news-update-container",
	cTitle: "#news-update-head h2",
	cBody: "#news-update-body ul",
	cNotif: "#news-update-notifier",
	cClose: "#news-update-close",
	
	//Initialize newsUpdate
	init: function() {
		
		newsUpdate.dTitle = $(document).attr("title");
		
		var time = $.ajax({
  			url: "http://www.transindex.ro/ujdesign_resources/time.php",
  			async: false
 			}).responseText;
		var d = new Date(time);

		newsUpdate.now = d.getTime();
		
		//Check for news
		setTimeout(newsUpdate._fetch, newsUpdate.interval);
		
		//Show newsUpdate content on click on notifier
		$(newsUpdate.cNotif).click(function() {
			newsUpdate._display();
		});
		
		$(newsUpdate.cClose).click(function() {
			newsUpdate._close();
		});
	},
	
	//Fetch news via ajax
	_fetch: function() {
		$.ajax({
		  url: "http://www.transindex.ro/transindex-frissules.php", 
		  data: { since:newsUpdate.now },
		  success: function(data) {
				//alert(newsUpdate.now);
				var nr = data.split('<li>');
  				var nCount = nr.length-1;

				nHtml = [];
				elem = '';
			
			if (nCount > 0) {
				//Update document title
				newsUpdate._setTitle("(" + nCount + ") ");
				//Hide newsUpdate title
				$(newsUpdate.cTitle).hide();
				//Update notifier
				$(newsUpdate.cNotif).html("<a href='javascript:;'><span>" + nCount + "</span> új cikkel frissültünk.</a>").show();
				//Populate newsUpdate content
				nHtml.push(data);
				newsUpdate.newsHtml = nHtml.join('');
			}
			
			//Check for news
			setTimeout(newsUpdate._fetch, newsUpdate.interval);
		}
		});
	},

	
	//Display news
	_display: function() {
				
		if (newsUpdate.newsHtml != '') {
			//Hide notifier
			$(newsUpdate.cNotif).html('').hide();
			//Show newsUpdate title
			$(newsUpdate.cTitle).show();
			//Show newsUpdate content
			$(newsUpdate.cBody).prepend(newsUpdate.newsHtml);
			//Reset document title
			newsUpdate._setTitle("");
			
			newsUpdate.newsHtml = '';
			var time = $.ajax({
  				url: "http://www.transindex.ro/ujdesign_resources/time.php",
  				async: false
 			}).responseText;
			var d = new Date(time);
			newsUpdate.now = d.getTime();
			if(first == 1)
			{
					$(newsUpdate.cClose).html("<a href='javascript:;'>[x] bezár</a>").show();
			}
			first = 0;
		}
	},
	
	//Close news
	_close: function() {
		var time = $.ajax({
  				url: "http://www.transindex.ro/ujdesign_resources/time.php",
  				async: false
 			}).responseText;
		var d = new Date(time);
		
		newsUpdate.now = d.getTime();
		first = 1;
		$(newsUpdate.cClose).html("").hide();
		$('#news-update-body').html("<ul></ul>");
	},
	
	//Set document title
	_setTitle: function(text) {
		$(document).attr('title', text + newsUpdate.dTitle);
	}
}

function torol(g,text) {
	if (g.value==text) g.value='';
}

function kitolt(g,text) {
	if (g.value=='') g.value=text;
}
