var ll_labels = {
	en: {
		days: 'days',
		hours: 'hours',
		minutes: 'minutes',
		seconds: 'seconds',
		postfix: 'ago'
	},
	de: {
		days: 'tage',
		hours: 'stunden',
		minutes: 'minuten',
		seconds: 'sekunden',
		postfix: 'vor'
	},
	ru: {
		days: 'дней',
		hours: 'часов',
		minutes: 'минут',
		seconds: 'секунд',
		postfix: 'назад'
	}
}

var currentLang = metalang.split('-')[1];

EventTicker = function(el, url, labels) {
	this.data = false;
	this.el = el;
	this.url = url;
	this.getData(this);
	this.labels = labels;
	var self = this;
	$(el).hide();
	setInterval(function() {
		self.getData(self)
	}, 60000);
	setTimeout(function() {
		self.showRandom(self)
	}, 1000);
	setInterval(function() {
		self.showRandom(self)
	}, 10000);
	
}

EventTicker.prototype = {

	showRandom: function(o) {
		if (!o.data) {
			return false;
		}
		var n = Math.round(Math.random()*(o.data.length-1));
		var now = new Date();
		var ts = new Date((o.data[n].timestamp) * 1000);
		var diff = (now - ts + now.getTimezoneOffset())/1000;
		var seconds=Math.floor(diff % 60);
		    diff=diff/60;
		var minutes=Math.floor(diff % 60);
		    diff=diff/60;
		var hours=Math.floor(diff % 24);
		    diff=diff/24;
		var days=Math.floor(diff);
		var timeStr = '';
		//if (days) {
		//	timeStr += days + ' ' + o.labels['days'] + ' ';
		//}
//		if (hours) {
//			timeStr += hours + ' ' + o.labels['hours'] + ' ';
//		}
//		if (minutes) {
//			timeStr += minutes + ' ' + o.labels['minutes'] + ' ';
//		}
		if (seconds) {
			timeStr += seconds + ' ' + o.labels['seconds'] + ' ';
		}
		
		timeStr += o.labels['postfix'];
//		console.log(timeStr);
				
		$(o.el).text('++ ' + timeStr + ' ' + o.data[n].description + ' ++');
		$(o.el).fadeIn(500, function() {
			setTimeout(function(){
				$(o.el).fadeOut(500);
			}, 5000);
		});
	},

	getData: function(self) {
		//var self = this;
		$.getJSON(this.url, function(obj) {
			self.data = obj;
		});
	}
}

$(document).ready(function () {
	clock();
	$("#packages").tabs({
		selected: 3,
		containerTag: 'ul',
		tabTag: 'li'		
	});
	
	$('#packages-home').tabs({
		selected: 1,
		containerTag: 'tr',
		tabTag: 'td'
	});
	$("#btn_go").click(function(){
		$("#CloginForm").hide();
		$("#si_preloader").show();
		$("#loginform").submit(); return false; 
	    }); 
	$("#btn_fg").click(function(){
		var data = $("#forgotform").serialize();
		$.ajax({
			type: "GET",
			url: "/secure/mandators/change_password_email/?format=js",
			data: data,
			success: fgRespond,
		});
		return false;
	});
	$("#loadLoginForm").click(function() {$("#CforgotForm").hide(); $("#CloginForm").show(); return false;})
	$("#loadForgotForm").click(function() {$("#CloginForm").hide(); $("#CforgotForm").show(); return false;});
	$(".more").click(function() {$("#langDDMore").toggle();});						
	$('#feedback_send').click(feedbackSend);
	$('textarea').focus(function() {$(this).val('');$(this).unbind('focus');$(this).css('color','#515151');});
	$('input').focus(function() {$(this).val('');$(this).unbind('focus');$(this).css('color','#515151');});
	//var ticker = new EventTicker('#event-ticker', 'http://tikktakk.com/fileadmin/templates/javascript/events_data_'+metalang+'.js', ll_labels[currentLang]);
	var ticker = new EventTicker('#event-ticker', 'fileadmin/templates/javascript/events_data_'+metalang+'.js', ll_labels[currentLang]);
});

function fgRespond(resp) {
	var response = eval("(" + resp + ")");
	if(response['error'] == false) $('#pw_content').html('<p style="text-align:center; padding-top:15px; width:220px;">' + response['message'] + '</p>');
	if(response['error'] == true) $('#pw_content').html('<p style="text-align:center; padding-top:15px; width:220px;color:#FF0000;">' + response['message'] + '</p>');
	$("#btn_fg").unbind();
	return false;
}

function feedbackSend() {
	$(".feedback_form").hide();
	$(".feedback_preloader").show();
	var data = $("#feedback_form").serialize();
	var url = document.location;
	$.ajax({
		type: "GET",
		url: "index.php?id=5&type=600",
		data: data,
		success: feedbackRespond
	});
	return false;
}

function feedbackRespond(resp) {
	var response = eval("(" + resp + ")");
	if(response['error'] == 1) {
		$(".feedback_form").show();
		$('#errorMsg').html(response['errorMsg']);
	}
	if(response['error'] == 0) {
		$(".mailform").css("margin-left", 0 + "px");
		$('#mailform').html(response['resp']);
		if(response['formType'] == 'left') {
			setTimeout(function() {
				$('#mailform').fadeOut(2000);
			}, 1000);
		}
	}
	$(".feedback_preloader").hide();
}

function formSubmit(fid) {
    $('#' + fid ).submit();
}


function clock() {
	time = new Date();
	cH=time.getHours();
	cM=time.getMinutes();
	cS=time.getSeconds(); 

	s1 = Math.floor(cS/10) * -26;
	s2 = cS % 10 * -26;	
	$('.s1').css("background-position", "0px "+s1+"px");
	$('.s2').css("background-position", "0px "+s2+"px");
	
	m1 = Math.floor(cM/10) * -26;
	m2 = cM % 10 * -26;	
	$('.m1').css("background-position", "0px "+m1+"px");
	$('.m2').css("background-position", "0px "+m2+"px");


	h1 = Math.floor(cH/10) * -26;
	h2 = cH % 10 * -26;	
	$('.h1').css("background-position", "0px "+h1+"px");
	$('.h2').css("background-position", "0px "+h2+"px");

	//if((cS%2) == 0) $('.colon').css("visibility", "visible");
	//else $('.colon').css("visibility", "hidden");
	
	setTimeout("clock()",1000);
}



