

			
			
/***********************************************************
************                                     ***********
************  Generated @ 26.07.2010 - 10:41:57  ***********
************                                     ***********
***********************************************************/

			



			
			
/***********************************************************
*********************                  *********************
*********************  GameDetails.js  *********************
*********************                  *********************
***********************************************************/

			

function rebindBuildingDetails(){
	$('.buildingDetail').unbind();
	$('.buildingDetail').bind('click', function(event) {
		event.preventDefault();
		$.getJSON('/GameDetails/getBuildingDetails/' + $(this).attr('href'),function(json){
			if(json.success){
				$('#dialog').html(json.feedback);				
				dialog.dialog('open');
			}
			else{
				$('#okdialog').html(json.feedback);				
				okdialog.dialog('open');
			}
		});
	});
}

function rebindUnitDetails(){
	$('.unitDetail').unbind();
	$('.unitDetail').bind('click', function(event) {
		event.preventDefault();
		$.getJSON('/GameDetails/getUnitDetails/' + $(this).attr('href'),function(json){
			if(json.success){
				$('#dialog').html(json.feedback);				
				dialog.dialog('open');
			}
			else{
				$('#okdialog').html(json.feedback);				
				okdialog.dialog('open');
			}
		});
	});
}

function rebindResearchDetails(){
	$('.researchDetail').unbind();
	$('.researchDetail').bind('click', function(event) {
		event.preventDefault();
		$.getJSON('/GameDetails/getResearchDetails/' + $(this).attr('href'),function(json){
			if(json.success){
				$('#dialog').html(json.feedback);				
				dialog.dialog('open');
			}
			else{
				$('#okdialog').html(json.feedback);				
				okdialog.dialog('open');
			}
		});
	});
}

$(function(){
	rebindResearchDetails();
	rebindUnitDetails();
	rebindBuildingDetails();
});

			
			
/***********************************************************
************************             ***********************
************************  castle.js  ***********************
************************             ***********************
***********************************************************/

			

function adjustRessToJsonResponse(jsonObject){
	$('#currentCastleRess1').html(jsonObject.curCastleRes1);
	$('#currentCastleRess2').html(jsonObject.curCastleRes2);
	$('#currentCastleRess3').html(jsonObject.curCastleRes3);
	$('#currentCastleRess4').html(jsonObject.curCastleRes4);
}

			
			
/***********************************************************
************************             ***********************
************************  clocks.js  ***********************
************************             ***********************
***********************************************************/

			

function loadClocks(items,identifier){
	$(document).stopTime("clocks_"+identifier);
	var hours = null;
	var minutes = null;
	var seconds = null;
	$(document).everyTime("1s", "clocks_"+identifier,function(i) {
		items.each(function(){
	 		secsRemaining = $(this).attr('title');
	 		if(secsRemaining === null || secsRemaining == '')
	 			return;
	 		if(secsRemaining > 359999){
	 			$(this).find('span.aClockTooBig').show();
	 			$(this).find('span.aClockFinishedText').hide();
	 			$(this).find('span.aClockCountdown').hide();
	 		}
	 		else if(secsRemaining <= 0){
	 			$(this).find('span.aClockTooBig').hide();
	 			$(this).find('span.aClockFinishedText').show();
	 			$(this).find('span.aClockCountdown').hide();
	 		}
	 		else {
	 			hours = Math.floor(secsRemaining / (60*60));
	 			minutes = Math.floor((secsRemaining - hours*60*60) / 60);
	 			seconds = Math.floor(secsRemaining - hours*60*60 - minutes*60);
	 			if(hours < 10)
	 				hours = '0' + hours;
	 			if(minutes < 10)
	 				minutes = '0' + minutes;
	 			if(seconds < 10)
	 				seconds = '0' + seconds;
	 			
	 			$(this).find('span.aClockTooBig').html(hours + ':' + minutes + ':' + seconds);
	 			$(this).find('span.aClockTooBig').show();
	 			$(this).find('span.aClockFinishedText').hide();
	 			$(this).find('span.aClockCountdown').hide();
	 		}
	 		$(this).attr('title',secsRemaining-1);
	 	});
	 });
}

			
			
/***********************************************************
************************             ***********************
************************  common.js  ***********************
************************             ***********************
***********************************************************/

			

$(function(){
	rebindHelpTexts();
});


function rebindHelpTexts(){
	$('.helpTextsDialog').unbind();
	$('.helpTextsDialog').bind('click', function(event) {
		event.preventDefault();
		$.getJSON('/HelpText/' + $(this).attr('href'),function(json){
			$('#okdialog').html(json.feedback);				
			okdialog.dialog('open');
		});
	});
}


function Numsort (a, b) {
  return a - b;
}

			
			
/***********************************************************
***********************              ***********************
***********************  dialogs.js  ***********************
***********************              ***********************
***********************************************************/

			

var okdialog = null;
var dialog = null;
var reloaddialog = null;
var confirmdialog = null;
var redirectdialog = null;
var logindialog = null;

var redirectURL = null
var externalLinkHREF = null;
var externalLinkDialog = null;


var confirmdialogCallbackFunc = null;
var confirmdialogParamArray = new Array();
var confirmReadyStateURL = null;
var cancelReadyStateURL = null;


$(function(){
	okdialog = $("#okdialog").dialog({
			bgiframe: true,
			modal: true,
			buttons: {
				Ok: function() {
					$(this).dialog('close');
				}
			},
			width:400,
			height:282,
			autoOpen: false,
			dialogClass: 'caGrungeDialog400',
			resizable: false
		});
		
	logindialog = $("#logindialog").dialog({
			bgiframe: true,
			modal: true,
			width:400,
			height:282,
			autoOpen: false,
			dialogClass: 'caGrungeDialog400',
			resizable: false
			
		});
	/*$('#okdialog').bind('dialogopen', function(event, ui) {
		$('#okdialog').dialog('option', 'width', 500);
		$('#okdialog').attr('title', '');
	});*/
		
	dialog = $("#dialog").dialog({
			bgiframe: true,
			modal: true,
			width:400,
			height:282,
			autoOpen: false,
			dialogClass: 'caGrungeDialog400',
			resizable: false
			
		});
	/*$('#dialog').bind('dialogopen', function(event, ui) {
		$('#dialog').dialog('option', 'width', 500);
		$('#dialog').attr('title', '');
	});	*/
		
	reloaddialog = $("#reloaddialog").dialog({
			bgiframe: true,
			modal: true,
			buttons: {
				Ok: function() {
					$(this).dialog('close');
					window.location.reload();
				}
			},
			width:400,
			height:282,
			autoOpen: false,
			dialogClass: 'caGrungeDialog400',
			resizable: false
		});
	/*$('#reloaddialog').bind('dialogopen', function(event, ui) {
		$('#reloaddialog').dialog('option', 'width', 500);
		$('#reloaddialog').attr('title', '');
	});*/
	
	confirmdialog = $("#confirmdialog").dialog({
			bgiframe: true,
			modal: true,
			buttons: {
				Ok: function() {
					$(this).dialog('close');
					if(confirmdialogCallbackFunc !== null)
						confirmdialogCallbackFunc();
				},
				Cancel: function(){
					$(this).dialog('close');
				}
			},
			width:400,
			height:282,
			autoOpen: false,
			dialogClass: 'caGrungeDialog400',
			resizable: false
		});	
	/*$('#confirmdialog').bind('dialogopen', function(event, ui) {
		$('#confirmdialog').dialog('option', 'width', 500);
		$('#confirmdialog').attr('title', '');
	});*/
	
	redirectdialog = $("#redirectdialog").dialog({
			bgiframe: true,
			modal: true,
			buttons: {
				Ok: function() {
					$(this).dialog('close');
					if(redirectURL !== null)
						window.location.href = redirectURL;
				}
			},
			width:400,
			height:282,
			autoOpen: false,
			dialogClass: 'caGrungeDialog400',
			resizable: false
		});
		
	externalLinkDialog = $("#externalLinkDialog").dialog({
			bgiframe: true,
			modal: true,
			buttons: {
				Ok: function() {
					$(this).dialog('close');
					if(externalLinkHREF !== null)
						window.open(externalLinkHREF,'_blank');
				},
				Cancel: function(){
					$(this).dialog('close');
				}
			},
			width:400,
			height:282,
			autoOpen: false,
			dialogClass: 'caGrungeDialog400',
			resizable: false
		});
});


			
			
/***********************************************************
*************************          *************************
*************************  map.js  *************************
*************************          *************************
***********************************************************/

			

var TopScrollCoords = null;
var BottomScrollCoords = null;
var LeftScrollCoords = null;
var RightScrollCoords = null;
var MapWidthPx = null;
var MapHeightPx = null;
var msAnimationSpeed = 500;

var currentlyAnimatingMap = false;

function rebindSingleMapEntries(){
	$('.singleMapEntry').unbind();
	$('#currentMapContainer .singleMapEntryContainer').bind('mouseenter',function(event){
		$(this).find('.singleMapEntry').css({'background-color': '#00AA00', opacity: 0.1});							
		HighlightSingleMapEntryFadeIn($(this).find('.singleMapEntry'));
	});
	
	$('#currentMapContainer .singleMapEntryContainer').bind('mouseleave',function(event){
		$(this).find('.singleMapEntry').stop().css({'background-color': 'transparent', opacity: 0});	
	});
	
	$('#currentMapContainer .singleMapEntryContainer .MapCastleMarker').bind('click',function(event){
		event.preventDefault();
		$.getJSON('/Karte/LoadCastleDetailData/' + $(this).find("span").html(), function(json){
			okdialog.dialog('open');
			$('#okdialog').html(json.feedback);	
		});
	});
}

function HighlightSingleMapEntryFadeIn(element){
	$(element).animate({
		opacity: 0.6			
	}, msAnimationSpeed, function() { 
		HighlightSingleMapEntryFadeOut(element)
	});				
}

function HighlightSingleMapEntryFadeOut(element){
	$(element).animate({
		opacity: 0.1
	}, msAnimationSpeed, function() { 
		HighlightSingleMapEntryFadeIn(element)
	});				
}

function loadNewMapSection(newX,newY,newDirection){
	if(currentlyAnimatingMap)
		return;	
	currentlyAnimatingMap = true;		
	$('#mapLoadingAnimation').show();		
	$('#mapLoadingAnimation').css("opacity", 0.8);
	
	$.post('/Karte/LoadMapSection', { x: newX, y: newY }, function(json){
		if(json.success){
			$('#MapLocatorFormX').val(json.currentX);
			$('#MapLocatorFormY').val(json.currentY);
			
			TopScrollCoords = json.top_href;
			if(TopScrollCoords == null)
				$('.mapScrollBar[alt="top"]').hide();
			else
				$('.mapScrollBar[alt="top"]').show();
			BottomScrollCoords = json.bottom_href;
			if(BottomScrollCoords == null)
				$('.mapScrollBar[alt="bottom"]').hide();
			else
				$('.mapScrollBar[alt="bottom"]').show();
			LeftScrollCoords = json.left_href;
			if(LeftScrollCoords == null)
				$('.mapScrollBar[alt="left"]').hide();
			else
				$('.mapScrollBar[alt="left"]').show();
			RightScrollCoords = json.right_href;
			if(RightScrollCoords == null)
				$('.mapScrollBar[alt="right"]').hide();
			else
				$('.mapScrollBar[alt="right"]').show();

			$('#reloadedMapContainer').html(json.feedback);
			$('#mapLoadingAnimation').animate({opacity:0},200,function(){
				$('#mapLoadingAnimation').hide();
				if(newDirection == 'right'){
					$('#reloadedMapContainer').css({'top' : 0, 'left' : MapWidthPx, opacity: 1});
					$('#currentMapContainer').animate({
						opacity: 0,
						top: 0,
						left: '-' + MapWidthPx					
					}, msAnimationSpeed);
				}
				if(newDirection == 'left'){
					$('#reloadedMapContainer').css({'top' : 0, 'left' : '-' + MapWidthPx, opacity: 1});
					$('#currentMapContainer').animate({
						opacity: 0,
						top: 0,
						left: MapWidthPx					
					}, msAnimationSpeed);
				}
				if(newDirection == 'top'){
					$('#reloadedMapContainer').css({'top' : '-' + MapHeightPx, 'left' : 0, opacity: 1});
					$('#currentMapContainer').animate({
						opacity: 0,
						top: MapHeightPx,
						left: 0					
					}, msAnimationSpeed);
				}
				if(newDirection == 'bottom'){
					$('#reloadedMapContainer').css({'top' : MapHeightPx, 'left' : 0, opacity: 1});
					$('#currentMapContainer').animate({
						opacity: 0,
						top: '-' + MapHeightPx,
						left: 0					
					}, msAnimationSpeed);
				}
				if(newDirection == null){
					$('#reloadedMapContainer').css({'top' : 0, 'left' : 0, opacity: 1});
					$('#currentMapContainer').css({'top' : 0, 'left' : 0, opacity: 0});
					reloadMapContainerLoaded();
				}
				else{
					$('#reloadedMapContainer').animate({
						top: 0,
						left: 0					
					}, msAnimationSpeed, reloadMapContainerLoaded);
				}			
			});
		}
		else{
			okdialog.dialog('open');
			$('#okdialog').html(json.feedback);
		}
	},"json");	
}

function reloadMapContainerLoaded(){
	currentlyAnimatingMap = false; 
	$('#reloadedMapContainer').attr('id','tmpMapContainer');
	$('#currentMapContainer').attr('id','reloadedMapContainer');
	$('#tmpMapContainer').attr('id','currentMapContainer');
	rebindSingleMapEntries();
}

			
			
/***********************************************************
*************************          *************************
*************************  msg.js  *************************
*************************          *************************
***********************************************************/

			

function beginMsgUnreadMarkerHighlight(){
	if($('#msgUnreadMarker').length == 1){
		element = $('#msgUnreadMarker');
		element.animate({
				color: "#FFFFFF"		
			}, 1000, function() { 
				MsgUnreadMarkerHighlightReverse(element)
			}
		);				
	}
}

function MsgUnreadMarkerHighlightReverse(element){
	element.animate({
			color: "#800000"			
		}, 1000, function() { 
			beginMsgUnreadMarkerHighlight(element)
		}
	);				
}

var messagesCurrentPage = null;

function loadMailFolder(id,mailFolder,page){
	messagesCurrentPage = page;
	$('#' + id + ' .loadingContainer').show();
	$('#' + id + ' .msgFolderContainer').hide();
	$.getJSON('/Nachrichten/loadFolder/' + mailFolder + '/' + page,function(json){
		if(json.success){
			$('#' + id + ' .loadingContainer').hide();				
			$('#' + id + ' .msgFolderContainer').html(json.feedback);
			$('#' + id + ' .msgFolderContainer').show();
			$('.openMailHref').unbind();
			$('.openMailHref').bind('click', function(event) {
				event.preventDefault();
				$.getJSON('/Nachrichten/loadMail/' + $(this).attr('href'),function(json){
					okdialog.dialog('open');
					$('#okdialog').html(json.feedback);
					if(json.markAsUnread){
						$('a[href="' + json.mailID + '"]').parentsUntil('table').removeClass('MsgItemUnread');
					}
				});
			});
			
			
			$('#' + id + ' .msgFolderContainer .loadPageHref').unbind();
			$('#' + id + ' .msgFolderContainer .loadPageHref').bind('click', function(event) {
				event.preventDefault();
				loadMailFolder($(this).parent().parent().attr('id'),$(this).parent().parent().attr('title'),$(this).html());
			});
		}
		else{
			okdialog.dialog('open');
			$('#okdialog').html(json.feedback);
		}
	});
}

			
			
/***********************************************************
**********************                **********************
**********************  rangliste.js  **********************
**********************                **********************
***********************************************************/

			

function loadRangliste(page,mode){
	$('#ranglisteLoadingContainer').show();
	$('#ranglisteContainer').hide();
	$('#ranglisteContainerPages').hide();
	$.getJSON('/Rangliste/loadPage' + mode + '/' + page, function(json){
		if(json.success){
			$('#ranglisteContainer').html(json.feedback);
		}
		else{
			okdialog.dialog('open');
			$('#okdialog').html(json.feedback);
		}
		$('#ranglisteLoadingContainer').hide();
		$('#ranglisteContainer').show();
		$('#ranglisteContainerPages').show();
	});
}

			
			
/***********************************************************
*************************           ************************
*************************  ress.js  ************************
*************************           ************************
***********************************************************/

			

function calculateRessPerDragger(){
			var left = $('#ress-dragger').css('left').substr(0,$('#ress-dragger').css('left').length - 2);
			var top = $('#ress-dragger').css('top').substr(0,$('#ress-dragger').css('top').length - 2);

			var totalWidth = $('#dragging-res-border').width() - $('#ress-dragger').width();
			res1Way = totalWidth - Math.sqrt(left*left + top*top);
			res2Way = totalWidth - Math.sqrt((totalWidth-left)*(totalWidth-left) + top*top);
			res3Way = totalWidth - Math.sqrt(left*left + (totalWidth-top)*(totalWidth-top));
			res4Way = totalWidth - Math.sqrt((totalWidth-left)*(totalWidth-left) + (totalWidth-top)*(totalWidth-top));
			res1Way = res1Way > totalWidth ? totalWidth : (res1Way < 0 ? 0: res1Way);
			res2Way = res2Way > totalWidth ? totalWidth : (res2Way < 0 ? 0: res2Way);
			res3Way = res3Way > totalWidth ? totalWidth : (res3Way < 0 ? 0: res3Way);
			res4Way = res4Way > totalWidth ? totalWidth : (res4Way < 0 ? 0: res4Way);
			totalWay = res1Way + res2Way + res3Way + res4Way;
			res1Percentage = res1Way / totalWay;
			res2Percentage = res2Way / totalWay;
			res3Percentage = res3Way / totalWay;
			res4Percentage = res4Way / totalWay;
			res1workers = Math.floor(totalWorkers * res1Percentage);
			res2workers = Math.floor(totalWorkers * res2Percentage);
			res3workers = Math.floor(totalWorkers * res3Percentage);
			res4workers = Math.floor(totalWorkers * res4Percentage);
			toAdd = totalWorkers - (res1workers + res2workers + res3workers + res4workers);
			while(toAdd<0)
			{
				if(res1workers > 0) res1workers--;
				if(res2workers > 0) res2workers--;
				if(res3workers > 0) res3workers--;
				if(res4workers > 0) res4workers--;
				toAdd = totalWorkers - (res1workers + res2workers + res3workers + res4workers);
			}
			if(toAdd != 0){
				var roundingErrors = new Array(
						totalWorkers * res1Percentage - res1workers,
						totalWorkers * res2Percentage - res2workers,
						totalWorkers * res3Percentage - res3workers,
						totalWorkers * res4Percentage - res4workers);
				roundingErrors.sort(Numsort);
				for(i = 0; i<toAdd; i++){
					if(totalWorkers * res1Percentage - res1workers == roundingErrors[i]) res1workers++;
					else if(totalWorkers * res2Percentage - res2workers == roundingErrors[i]) res2workers++;
					else if(totalWorkers * res3Percentage - res3workers == roundingErrors[i]) res3workers++;
					else if(totalWorkers * res4Percentage - res4workers == roundingErrors[i]) res4workers++;
				};
			}
			$("input[name='worker_res1']").val(res1workers);
			$("input[name='worker_res2']").val(res2workers);
			$("input[name='worker_res3']").val(res3workers);
			$("input[name='worker_res4']").val(res4workers);
			$('#ress_res1_per_hour').html(res1workers * worker1perHour);
			$('#ress_res2_per_hour').html(res2workers * worker2perHour);
			$('#ress_res3_per_hour').html(res3workers * worker3perHour);
			$('#ress_res4_per_hour').html(res4workers * worker4perHour);
		}
		function saveWorkerCount(){
			$.post('/Rohstoffe/updateWorkerCount',$('#saveRessWorkersForm').serialize(),function(json){
				if(json.success)
					$('#saveWorkerCountFeedback').html(json.feedback);
				else{
					okdialog.dialog('open');
					$('#okdialog').html(json.feedback);
				}
			},'json');
		}
		
		function prepareWorkerSaving(){
			$('#saveWorkerCountFeedback').html($('#saveWorkerLoading').html());
			$(document).stopTime("ressUpdateClock");
			$(document).oneTime(100, "ressUpdateClock", function() {
			    saveWorkerCount();
			});
		}

			
			
/***********************************************************
*************************           ************************
*************************  user.js  ************************
*************************           ************************
***********************************************************/

			

$(function(){
	$('#userpanel-login-form').bind('submit',function(event){
		event.preventDefault();	
		$.post('/Session/login',$(this).serialize(),function(json){
			if(json.success){
				window.location.reload();
			}
			else{
				okdialog.dialog('open');
				$('#okdialog').html(json.feedback);
			}
		},'json');
	});
	
	$('#logout-btn').bind('click',function(event){
		event.preventDefault();	
		$.getJSON('/Session/logout',function(json){
			if(json.success){
				redirectURL = '/';
				redirectdialog.dialog('open');
				$('#redirectdialog').html(json.feedback);
			}
			else{
				okdialog.dialog('open');
				$('#okdialog').html(json.feedback);
			}
		});
	});
		
	var curtime = new Date();
	var sesskey = null;
	var lastSessionPoll = curtime.getTime();
	if($.cookies.get('SERVER1[polltime]') !== null){
		var polltime = $.cookies.get('SERVER1[polltime]');
	}
	else{
		var polltime = 60;
		$.cookies.set('SERVER1[polltime]',polltime);
	}
		
		
	var curGP = null;
	/** SESSION ALIVESIGN **/
	
	
	function startAliveSign(seconds){
		$(document).everyTime(seconds + "s", "aliveSign",function(i) {
			curtime = new Date();
			sesskey = $.cookies.get('SERVER1[sesskey]');
			lastpoll = $.cookies.get('SERVER1[lastpoll]');
			if(sesskey == null || sesskey.length < 10){
				stopAliveSign();
				return;
			}
			if(lastpoll == null || curtime.getTime() - lastpoll > seconds * 1000 - 50){
			 	$.getJSON('/Session/aliveSign',function(json){
			 		if(json.success){
			 			$.cookies.set('SERVER1[lastpoll]',curtime.getTime());
			 			if(typeof(json.polltime) != 'undefined'){
			 				adjustPollTime(json.polltime);
			 			}	
			 		}
			 		else{
			 			stopAliveSign();
			 			window.location.reload();
			 		}
			 	});
			}
		});
	}
	
	function adjustPollTime(newPollTime){
		if(newPollTime == polltime)
			return;
		polltime = newPollTime;
		$(document).stopTime("aliveSign");
		$.cookies.set('SERVER1[polltime]',newPollTime);
		startAliveSign(polltime);
	}
	
	function stopAliveSign(){
		// Timeout erreicht oder abgemeldet
		$.cookies.del('SERVER1[lastpoll]');
		$.cookies.del('SERVER1[sesskey]');
		$(document).stopTime("aliveSign");
		
	}
	if($.cookies.get('SERVER1[sesskey]') != null)
		startAliveSign(polltime);
});
