///////////////////////////////////////////////
// (c) Copyright by Gamer-Scene.de (Kevin.H) //
//          (r) All rights reserved          //
//             Coded by PCFreund             //
///////////////////////////////////////////////

// Define Variables
var alert_count = 0;
var content_load;
var reload_loginarea_load;
var load_img_count = 0;
var cloud_1_move;
var cloud_2_move;
var chat_plugin_id;
var chat_plugin_lastread;
var chat_plugin_use = new Array();
var chat_plugin_scroll = 'true';
var test = 0;

function alert_box(text)
{
	$("body").append('<div id="alert_'+alert_count+'"><p id="alert_text_'+alert_count+'">'+text+'</p></div>');
	$("#alert_"+alert_count).dialog({
		resizable: false,
		autoOpen: true,
		width: 400,
		height: 200,
		modal: true,
		position: 'center',
		buttons: {
			"Schließen": function() {
				$(this).dialog('close').remove();
			}
		}
	});
	alert_count++;
}

function say(title,text) {
	growl(title, text, 'http://static.gamer-scene.de/images/callsign.png');
}

function contentInsert(responseText,responseDiv) {
	if (responseDiv !== undefined && responseDiv !== null)
	{
		if (responseText == '404')
		{
			say('Modul nicht gefunden', 'Das angeforderte Modul konnte nicht gefunden werden.');
		}
		else
		{
			$("#"+responseDiv).html(responseText);
			window.setTimeout('setToolTip(); launchJavascript(\''+responseDiv+'\');', 50);
		}
	}
}

function request(url,post) {
	load_img(+1);
	cc(1);
	$.ajax({
		type: "POST",
		url: "/"+url,
		data: post,
		success: function(html) {
			responseText = html;
			load_img(-1);
			cc(0);
			contentInsert();
		},
		error: function(data) {
			cc(0);
		}
	});
}

function content(page) {
	var gender = '';
	var addreq = '';
	var actpage = window.location.hash.substr(3);
	if (page == 'passwordrequest')
	{
		
	}
	window.location.hash = '#!/'+page;
	document.getElementById('actpage').innerHTML = page;
	var mod1 = 'ajax.php';
	var mod2 = 'page='+urlencode(page)+addreq;
	if(content_load)
	{
		content_load.abort();
	}
	load_img(+1);
	cc(1);
	content_load = $.ajax({
		type: "POST",
		url: "/"+mod1,
		data: mod2,
		success: function(html) {
			contentInsert(html,'content');
			$("#login_username").val("Benutzername");
			$("#login_password").val("Passwort");
			load_img(-1);
			cc(0);
			reload_loginarea();
		},
		error: function(data) {
			if(data.status == 0)
			{
				
			}
			else if(data.status == 503)
			{
				window.location.href='http://gamer-scene.de/';
			}
			else
			{
				say('Server Fehler', 'Der Server konnte Deine Anfrage nicht bearbeiten. Versuche es später erneut!');
			}
			load_img(-1);
			cc(0);
		}
	});
}

function reload_loginarea() {
	var mod1 = 'ajax.php';
	var mod2 = 'page='+urlencode('modules:loginarea');
	if(reload_loginarea_load)
	{
		reload_loginarea_load.abort();
	}
	load_img(+1);
	cc(1);
	reload_loginarea_load = $.ajax({
		type: "POST",
		url: "/"+mod1,
		data: mod2,
		success: function(html) {
			load_img(-1);
			cc(0);
			reload_loginarea_now(html);
		},
		error: function() {
			load_img(-1);
			cc(0);
		}
	});
}

function reload_loginarea_now(responseText)
{
	if(responseText == '0')
	{
		$("#wrap_header_ucp").animate({marginTop: "-62px"}, 1000);
		$("#wrap_header_adhs").animate({marginTop: "-1px"}, {"easing": "easeOutBounce", "duration": 1000} , 1000);
	}
	else
	{
		$("#wrap_header_ucp").animate({marginTop: "0px"}, {"easing": "easeOutBounce", "duration": 1000} , 1000);
		$("#wrap_header_adhs").animate({marginTop: "-62px"}, 1000);
	}
}

function launchJavascript(responseText) {
	if(document.getElementById(responseText))
	var responseText = document.getElementById(responseText).innerHTML;
	var ScriptFragment = '(?:<script.*?>)((\n|.)*?)(?:</script>)';
	var match    = new RegExp(ScriptFragment, 'img');
	var scripts  = responseText.match(match);
	if(scripts) {
		var js = '';
		for(var s = 0; s < scripts.length; s++) {
			var match = new RegExp(ScriptFragment, 'im');
			js += scripts[s].match(match)[1];
		}
		eval(js);
	}
}

function relpage() {
	var page = '';
	if (window.location.hash.substr(0, 3) == '#!/')
	{
		page = window.location.hash.substr(3);
		if (page === '')
		{
			page = 'home';
		}
		if (document.getElementById('actpage').innerHTML != page)
		{
			content(page);
		}
	}
	else
	{
		content('home');
	}
}

function cloud_1() {
	time = rand(12000, 20000);
	timep = rand(0,5000);
	pos = rand(20, 878);
	$("#cloud_1").animate({marginLeft: pos+"px"}, time);
	cloud_1_move = window.setTimeout('cloud_1();', time+timep);
}

function cloud_2() {
	time = rand(12000, 20000);
	timep = rand(0,1000);
	pos = rand(20, 894);
	$("#cloud_2").animate({marginLeft: pos+"px"}, time);
	cloud_2_move = window.setTimeout('cloud_2();', time+timep);
}

function head_menu(entry) {
	
}

function setToolTip() {
	$("[title]").tooltip({
		effect: 'fade',
		fadeInSpeed: 200,
		fadeOutSpeed: 1000,
		position: "center right",
		offset: [-2, 10],
		opacity: 0.8
	});
}

function set_title(title) {
	if(document.getElementById('title').innerHTML != 'Gamer-Scene.de :: '+title)
	{
		remove_title(title);
	}
}

function remove_title(title) {
	var length = document.getElementById('title').innerHTML.length;
	var text = document.getElementById('title').innerHTML;
	var i_count = 0;
	for(i=length-1;i>=18;i--)
	{
		window.setTimeout('document.getElementById(\'title\').innerHTML = \''+text.substr(0,i)+'\';', i_count*100);
		i_count++;
	}
	window.setTimeout('add_title(\''+title+'\');', i_count*100);
}

function add_title(title) {
	length = document.getElementById('title').innerHTML.length;
	text = document.getElementById('title').innerHTML;
	newtext = text+title;
	i_count = 0;
	for(i=18;i<=newtext.length;i++)
	{
		window.setTimeout('document.getElementById(\'title\').innerHTML = \''+newtext.substr(0,i)+'\';', i_count*100);
		i_count++;
	}
}

function load_img(num)
{
	load_img_count = load_img_count+num;
	if(load_img_count <=0)
	{
		/* $('#ajax-loader').hide(); */
		$('#ajax-loader').stop().animate({opacity: "0.0"}, 500);
	}
	else
	{
		/* $('#ajax-loader').stop().show(); */
		$('#ajax-loader').stop().animate({opacity: "1.0"}, 250);
	}
}

function timebox()
{
	if(document.getElementById('cloud_1').innerHTML != date('H:i'))
	document.getElementById('cloud_1').innerHTML = date('H:i');
}

function module(modulename,moduleparameters,callback,hideli)
{
	cc(1);
	if(hideli == true)
	{
		$.ajax({
			type: "POST",
			url: "/modules.php?id="+modulename,
			data: moduleparameters,
			success: function(html) {
				cc(0);
				launchJavascript(html);
				eval(callback);
			},
			error: function() {
				cc(0);
				eval(callback);
			}
		});
	}
	else
	{
		load_img(+1);
		$.ajax({
			type: "POST",
			url: "/modules.php?id="+modulename,
			data: moduleparameters,
			success: function(html) {
				load_img(-1);
				cc(0);
				launchJavascript(html);
				eval(callback);
			},
			error: function() {
				load_img(-1);
				cc(0);
				eval(callback);
			}
		});
	}
}

function footer()
{
	setTimeout('module("footer", "", "footer();", true);', 100);
}

function growl(x1,x2,x3)
{
	var stayimg = '';
	if(x3 == '')
	{
		x3 = stayimg;
	}
	else if(x3 == null)
	{
		x3 = stayimg;
	}
	else if(x3 == 'undefined')
	{
		x3 = stayimg;
	}
	$.gritter.add({
		title: x1,
		text: x2,
		image: x3,
		sticky: false,
		time: '10000'
	});
	return true;
}

function set_title(title)
{
	document.getElementById('title').innerHTML = 'Gamer-Scene.de :: '+title;
	return true;
}

function get_menu(element)
{
	module("wrapmenu", "element="+element+"");
}

function synchronisize(state)
{
	if(state==1)
	{
		document.getElementById('synchronisizer').style.color='black';
	}
	else if(state==0)
	{
		document.getElementById('synchronisizer').style.color='white';
	}
}

function cc(num)
{
	if(num==1)
	{
		synchronisize(1);
		document.getElementById('connections').innerHTML=parseInt(document.getElementById('connections').innerHTML)+1;
		document.getElementById('connectionsmax').innerHTML=parseInt(document.getElementById('connectionsmax').innerHTML)+1;
	}
	else if(num==0)
	{
		next=parseInt(document.getElementById('connections').innerHTML)-1;
		if(next==0)
		{
			synchronisize(0);
		}
		document.getElementById('connections').innerHTML=parseInt(document.getElementById('connections').innerHTML)-1;
	}
}

function start_js()
{
	/*$("#cloud_1").draggable({ revert: true, containment: "parent", axis: "x" });
	$("#cloud_2").draggable({ revert: true, containment: "parent", axis: "x" });
	$("#cloud_1").mousedown(function() {
		$(this).stop();
		window.clearTimeout(cloud_1_move);
	}).mouseup(function() {
		cloud_1_move = window.setTimeout('cloud_1();', 500);
	});
	$("#cloud_2").mousedown(function() {
		$(this).stop();
		window.clearTimeout(cloud_2_move);
	}).mouseup(function() {
		cloud_2_move = window.setTimeout('cloud_2();', 500);
	});*/
	$("a", "#head_menu").button();
	window.setTimeout('$("#footer").animate({marginBottom: "0px"}, {"easing": "easeOutBounce", "duration": 1000} , 1000);', 2000);

	window.setTimeout('$("#menu_1").animate({opacity: "1.0"}, 1000);',  0);
	window.setTimeout('$("#menu_2").animate({opacity: "1.0"}, 1000);',  333.333);
	window.setTimeout('$("#menu_3").animate({opacity: "1.0"}, 1000);',  666.666);
	window.setTimeout('$("#menu_4").animate({opacity: "1.0"}, 1000);',  999.999);
	window.setTimeout('$("#menu_5").animate({opacity: "1.0"}, 1000);',  1333.332);
	window.setTimeout('$("#menu_6").animate({opacity: "1.0"}, 1000);',  1666.665);
	window.setTimeout('$("#menu_7").animate({opacity: "1.0"}, 1000);',  1333.332);
	window.setTimeout('$("#menu_8").animate({opacity: "1.0"}, 1000);',  999.999);
	window.setTimeout('$("#menu_9").animate({opacity: "1.0"}, 1000);',  666.666);
	window.setTimeout('$("#menu_10").animate({opacity: "1.0"}, 1000);', 333.333);
	window.setTimeout('$("#menu_11").animate({opacity: "1.0"}, 1000);', 0);

	window.setTimeout('$("#head_menu").animate({opacity: "0.25"}, 500); $("#head_menu").mouseover(function() { $("#head_menu").stop().animate({opacity: "1.0"}, 500); }).mouseout(function() { $("#head_menu").stop().animate({opacity: "0.25"}, 500); });', 2000);

	$("#toTop").scrollToTop({speed:1000});
	window.setTimeout('cloud_1(); cloud_2();', 3000);
	window.setInterval('relpage(); timebox();', 1000);
	relpage();
	reload_loginarea();
	timebox();
	module("footer", "", "footer();", true);
}

