var DEBUG = false;


/*
 * This function resizes the Browser window for given params
 * and centers it on the screen
 * param width: the content width
 * param height: the content height
 */
function resizePage(width, height) {

	if (jQuery.browser.msie) {
		var totalHeight = document.body.offsetHeight;
		var clientHeight = document.documentElement.clientHeight;
		var totalWidth = document.body.offsetWidth;
		var clientWidth = document.documentElement.clientWidth;
		window.resizeTo(width, height);
		width = width + (width - document.documentElement.clientWidth);
		height = height + (height - document.documentElement.clientHeight);
	} else {
		var totalHeight = window.outerHeight;
		var clientHeight = window.innerHeight;
		var totalWidth = window.outerWidth;
		var clientWidth = window.innerWidth;
		width = width + (totalWidth - clientWidth);
		height = height + (totalHeight - clientHeight);
	}
	var screenX = window.screen.availWidth;
	var screenY = window.screen.availHeight;
	window.moveTo((screenX - width)/2, (screenY - height)/2);
	window.resizeTo(width, height);
}



/*
 * This function rotates the flyers between given angles.
 * param lowerAngle: lower angle value
 * param upperAngle: upper angel value
 * param isAnimated: wheter the flyers should be animated rotated to their position or not
 */
function generateWall(lowerAngle, upperAngle, isAnimated) {
	$('.flyer>img').each(function(i) {
		var currentMenuItemId = '#'+$(this).attr("id");
		var deg = $.randomBetween(lowerAngle, upperAngle);
		debugAlert(currentMenuItemId + '|' + deg);
		if (isAnimated) {
			deg = (deg < 0) ? '-='+(deg*-1)+'deg' : '+='+deg+'deg';
			$(currentMenuItemId).animate({rotate: deg});
		} else {
			deg += 'deg';
			$(currentMenuItemId).css({rotate: deg});
		}
	});
}


/*
 * This function handles the animations if a user clicked on a flyer.
 * It also sends an AJAX-Request to contentProvider.php to get the question for this ID.
 * param obj: the clicked flyer
 * 
 */
function openContentFlyer(obj, status) {
	debugAlert("changeContent("+obj+")");

	// send AJAX-Request
	$.get("events/eventform", { id: $(obj).attr("id") }, function(data) {
		if (data != '') {
			if (!status) {
				$('.content_active_flyer_content_titlebar_header').html('Bitte beantworte die folgende Gewinnfrage:');
				$('.content_active_flyer_content_titlebar_header').show();
				$('.content_active_flyer_content_form').show();
				var eventId = $(obj).attr("id").replace("flyer", "");
				$('#EventEventId').val(eventId);
			} else {
				$('.content_active_flyer_content_titlebar_header').html('');
				$('.content_active_flyer_content_form').hide();
			}
			$('.content_active_flyer_content_text').html(data);
		}
	});

	var imgSrc = $(obj).attr("src");
	debugAlert('imgSrc' + imgSrc);
	$('.active_flyer_img').attr("src", imgSrc);
	$('.active_flyer_img').css({width: $(obj).css('width'), height: $(obj).css('height')});

	var width = $(obj).css('width').replace('px', '');
	width = (width*1.8)+'px';
	var height = $(obj).css('height').replace('px', '');
	height = (height*1.8)+'px';

	var position = $(obj).position();
	debugAlert(position.top + "|" + position.left);
	$('.content_active_flyer_flyer').attr('activeFlag', $(obj).attr('id'));
	$('.content_active_flyer_flyer').attr('activeFlagTop', position.top);
	$('.content_active_flyer_flyer').attr('activeFlagLeft', position.left);
	
	$('.content_active_flyer').show();
//	$('.content_active_flyer').fadeIn(1000);
	$('.content_active_flyer_flyer').fadeIn(1000);
	$('.content_active_flyer_content').fadeIn(1000);
	$('.side_box_content').fadeOut(1000);
	$('.flyers_box').fadeOut(1000);

	$('.active_flyer_img').css('position', 'absolute');
	$('.active_flyer_img').css('left', position.left);
	$('.active_flyer_img').css('top', position.top);
	// IE has problems with animated rotation and positioning
	if (jQuery.browser.msie) {
		// not animated rotation
		$('.active_flyer_img').animate({width: width, height: height, top: 123, left: 20}, 1000);
	} else {
		//animanted rotation
		$('.active_flyer_img').css('rotate', $(obj).css('rotate'));
		$('.active_flyer_img').animate({width: width, height: height, top: 123, left: 20, rotate: '0deg'}, 1000);
	}

}

/*
 * This function handles the animations if a user closes the flyer-content.
 */
function closeContentFlyer() {
	resetFormFields();
	if (jQuery.browser.msie) {
		$('.content_active_flyer').hide();
		$('.content_active_flyer_flyer').hide();
		$('.content_active_flyer_content').hide();
		$('.side_box_content').show();
		$('.flyers_box').show();
	} else {
		$('.content_active_flyer').fadeOut(1000);
		$('.content_active_flyer_flyer').fadeOut(1000);
		$('.content_active_flyer_content').fadeOut(1000);
		$('.side_box_content').fadeIn(1000);
		$('.flyers_box').fadeIn(1000);
	}
}



/*
 * This function handles the animations if a user clicks on a static content page.
 */
function openContentStatic(id) {
	// gtc || copyright || contact
	$('.content_static_content_text').hide();
	if (id != '') {
		$('.content_static_content_titlebar_header').html($('#'+id).attr('title'));
		$('#'+id).show();
	} else {
		$('.content_static_content_titlebar_header').html('');
		$('.content_static_content_text').html('');
	}
	$('.content_static_content').show();
	$('.content_static').fadeIn(1000);
}

function closeContentStatic() {
	$('.content_static').fadeOut(1000);
}


/*
 * jQuery Extensions
 */
jQuery.extend({
	random: function(X) {
	    return Math.floor(X * (Math.random() % 1));
	},
	randomBetween: function(MinV, MaxV) {
	  return MinV + jQuery.random(MaxV - MinV + 1);
	}
});

/*
 * Helpers
 */
function debugAlert(message) {
	if (DEBUG) alert(message);
}
