/* FUNCTIONS */

/* recurring function to move the text in div#movingtext by the width of the first element and then resetting its position to 0 */
function moveText(textwidth) {
	if(window.AnimateText == true) {
		var time = window.AnimationSpeed * textwidth;
		$('div#movingtext').css('margin-left', '0px').animate({marginLeft: '-'+textwidth}, time, 'linear').promise().done(function() { setTimeout('moveText('+textwidth+')',0); });	
	}
}
/* retriggers the scaling process */
function retrigger_scaling() {
	$(window).trigger('resize');
}
/* retracts the debug menu */
function debug_retract() {	
	var debugdiv = $('div#debug');
	debugdiv.removeClass('retract').unbind('click');
	var debugy = debugdiv.outerHeight();
	var headery = debugdiv.children('.header').outerHeight();
	var fontsize = parseInt($('body').css('font-size'));
	
	var emdestination = (debugy - headery) / (fontsize + (fontsize / 5));
	emdestination = Math.round(emdestination*100) / 100;
	
	debugdiv.clearQueue().animate({marginTop: '-'+emdestination+'em'}).promise().done(function() {
		$('div#debug .header').clearQueue().addClass('expand').bind('click', function() { debug_expand(); });
	});
	
}
/* expand the debug menu */
function debug_expand() {
	var debugdiv = $('div#debug');
	debugdiv.removeClass('expand').unbind('click');
	
	debugdiv.clearQueue().animate({marginTop: '0em'}).promise().done(function() {
		$('div#debug .header').clearQueue().addClass('retract').bind('click', function() { debug_retract(); });
	});
}
/* change the modifier for automatic scaling */
function change_scalingmod() {
	var inputfield = $('INPUT#scalingmod');
	var mod = parseInt(inputfield.val());
	if(!isNaN(mod)) {
		window.ScalingModificator = mod;
		$(window).trigger('resize');
	} else {
		inputfield.val(window.ScalingModificator);	
	}
}
/* manual scaling instead of automatic scaling */
function manual_scaling() {
	if(window.AutomaticScaling == false) {
	var docwidth = $(window).width();
	var docheight = $(window).height();
	var scale = parseFloat($('input#manualscale').val());
	if(isNaN(scale)) {
		scale = 1;
		$('input#manualscale').val(1);
	}
	if(scale < window.minimumScale) { scale = window.minimumScale; }
	if(scale > window.maximumScale) { scale = window.maximumScale; }
	$('body').css('font-size', Math.round(scale*10)+'px');
	$('div#debug P.scaling_info').text(docwidth + ' * ' + docheight + ' (scale '+ scale +')');
	}
}
/* hide all rotatable elements and show only those of a certain index */
function rotateImage(target) {
	$('div#background IMG.rotate').addClass('hidden').eq(target).removeClass('hidden');
	$('div#bottom P.rotate SPAN').addClass('hidden').eq(target).removeClass('hidden');
	$('div#bottom IMG.selector').attr('src','img/frame_empty.png').eq(target).attr('src', 'img/frame_massive.png');
	window.coverRotation = target;
}
/* call rotateImage on arrow click, depending if this is possible */
function rotateNavigate(dir) {
	var i = window.coverRotation;
	if(dir == 'Back') {
		i = i - 1;
		if(i < 0) { i = $('div#bottom IMG.selector').length - 1; }	
	}
	if(dir == 'Forward') {
		i = i + 1;
		if(i >= $('div#bottom IMG.selector').length) { i = 0; }	
	}
	rotateImage(i);
}
/* move the project slider */
function moveSlider(target) { 
	if(window.sliderBusy == true) { return; }

	var destination = window.sliderFrameWidth * (target * window.sliderMovePerClick);
	var fontsize = parseInt($('body').css('font-size'));
	$('div#slider div.static').animate({marginLeft: '-'+destination+'em'}, window.sliderSpeed).promise().done(function() { window.sliderBusy = false; })
	
	$('div#slider_control IMG.selector').attr('src', 'img/frame_empty.png').eq(target).attr('src', 'img/frame_massive.png');
	
	window.sliderState = target;	
}
/* call moveSlider on arrow click, depending if this is possible */
function sliderNavigate(dir) {
	var i = window.sliderState;
	if(dir == 'Back') {
		i = i - 1;
		if(i < 0) { i = $('div#slider_control IMG.selector').length - 1; }	
	}
	if(dir == 'Forward') {
		i = i + 1;
		if(i >= $('div#slider_control IMG.selector').length) { i = 0; }	
	}
	moveSlider(i);
}
function detailSelect(target) {
	$('div#details_menu SPAN').removeClass('main').eq(target).addClass('main');
	$('div.details').hide().eq(target).show();
}
function changeImage(target) {
	$('div#big_image IMG').hide().eq(target).show();
	$('div#image_control IMG.selector').attr('src','img/frame_empty.png').eq(target).attr('src', 'img/frame_massive.png');
	window.imageState = target;
}
function navigateImage(dir) {
	var i = window.imageState;
	if(dir == 'Back') {
		i = i - 1;
		if(i < 0) { i = $('div#image_control IMG.selector').length -1; }	
	}
	if(dir == 'Forward') {
		i = i + 1;
		if(i >= $('div#image_control IMG.selector').length ) { i = 0; }	
	}
	
	changeImage(i);
}
function changePlans(target) {
	$('div#images_plans IMG').hide().eq(target).show();
	$('div#plans_control IMG.selector').attr('src','img/frame_empty.png').eq(target).attr('src', 'img/frame_massive.png');

	window.plansState = target;
}
function navigatePlans(dir) {
	var i = window.plansState;
	if(dir == 'Back') {
		i = i - 1;
		if(i < 0) { i = $('div#plans_control IMG.selector').length -1; }	
	}
	if(dir == 'Forward') {
		i = i + 1;
		if(i >= $('div#plans_control IMG.selector').length ) { i = 0; }	
	}
	
	changePlans(i);
}
function changeVideo(target) {
	$('div#videos_window DIV.embed').hide().eq(target).show();
	$('div#videos_control IMG.selector').attr('src','img/frame_empty.png').eq(target).attr('src', 'img/frame_massive.png');
	window.videosState = target;
}
function navigateVideos(dir) {
	var i = window.videosState;
	if(dir == 'Back') {
		i = i - 1;
		if(i < 0) { i = $('div#videos_control IMG.selector').length -1; }	
	}
	if(dir == 'Forward') {
		i = i + 1;
		if(i >= $('div#videos_control IMG.selector').length ) { i = 0; }	
	}
	
	changeVideo(i);
}

/* BINDINGS ON RUNTIME */

/* bind on resize: scaling of page */
$(window).resize(function() {
	var docwidth = $(window).width();
	var docheight = $(window).height();
	if(window.AutomaticScaling == true) {
		var scale = Math.round(docwidth / 100);
		scale = scale - window.ScalingModificator;
		if(scale < 6) {
			scale = 6;
		}
		$('body').css('font-size', scale+'px');
		$('div#debug INPUT#manualscale').val(scale / 10);
	} else {
		scale = $('div#debug INPUT#manualscale').val() * 10;	
	}
	
	$('div#debug P.scaling_info').text(docwidth + ' * ' + docheight + ' (scale '+ ( scale / 10 ) +')');
});
/* bind on resize: positioning of bottom bar - if height of browser window is too little div#bottom gets a fixed position */
$(window).resize(function() {
	var docheight = $(window).height();
	var fontsize = parseInt($('body').css('font-size'));
	if(docheight / fontsize <= 60) {
		$('div#bottom').addClass('smallscreen');
	} else {
		$('div#bottom').removeClass('smallscreen');	
	}
});
/* bind on resize: prepare div#movingtext for marquee effect. duplicate content as much as needed and initiate animation */
$(window).resize(function() {

	if($('div#movingtext')) {
	var fullwidth = parseInt($('div#movingtext').width());
	var textwidth = parseInt($('div#movingtext SPAN:first').width());
	var htmlbit = $('div#movingtext SPAN:first').html();
	htmlbit = '<span>'+htmlbit+'</span>';
	var repeats = 2;
	var puthtml = htmlbit + htmlbit;
	
	/*IE SAVE */
	if(textwidth > 0 ) {
	while(textwidth * repeats <= fullwidth + textwidth) {
		puthtml = puthtml + htmlbit;
		repeats++;
	}
	}
	
	if(window.AnimateText == true) {
	$('div#movingtext').stop().clearQueue().css('margin-left', '0px');
	$('div#movingtext').html(puthtml);
	moveText(textwidth);
	}
}
});
/* bind on resize: var1 background should use 100% of the more dominant side */
$(window).resize(function () {
	if($('div#background.var1')) {
		if($(window).width() > $(window).height()) {
			$('div#background.var1 IMG').css('width', '100%').css('height', 'auto');
		} else {
			$('div#background.var1 IMG').css('width', 'auto').css('height', '100%');	
		}
	}
});
/* bind on resize: var2 background should behave so that it always covers the full window */
$(window).resize(function () {
	if($('div#background.var2')) {
		var docx = parseInt($(window).width());
		var docy = parseInt($(window).height());
		var imgx = 1004;
		var imgy = 765;
		if(docx >= docy) {
			if(imgx >= imgy) {
				$('div#background.var2 IMG').css('width', '100%').css('height', 'auto');	
			} else {
				$('div#background.var2 IMG').css('width', '100%').css('height', 'auto');	
			}
			
		} else {
			if(imgx >= imgy) {
				$('div#background.var2 IMG').css('width', 'auto').css('height', '100%');	
			} else {
				$('div#background.var2 IMG').css('width', 'auto').css('height', '100%');	
			}		
		}
		
		var actualx = parseInt($('div#background.var2 IMG:first').width());
		var actualy = parseInt($('div#background.var2 IMG:first').height());
		if(actualx < docx && actualx != 0) {
			$('div#background.var2 IMG').css('width', '100%').css('height', 'auto');
		}
		if(actualy < docy && actualy != 0) {
			$('div#background.var2 IMG').css('width', 'auto').css('height', '100%');
		}
		if(actualx == 0) {
			setTimeout('retrigger_scaling()', 2500);	
		}
	}
});
/* bind on resize: var3 background compares image height with window height and ties to height if necessary */
$(window).resize(function () {
	if($('div#background.var3')) {
		var docx = parseInt($(window).width());
		var docy = parseInt($(window).height());
		var imgx = 1004;
		var imgy = 765;
		if(docy >= imgy) {
			$('div#background.var3 IMG').css('width', 'auto').css('height', '100%');			
		} else {
			$('div#background.var3 IMG').css('width', '100%').css('height', 'auto');	
		}

	}
});

/* bind image rotation */
$(function() {
	$('div#bottom IMG.selector').each(function(index) {
		$(this).bind('click', function() {
			rotateImage(index);
		});
	});
	$('div#bottom SPAN.arrows IMG').each(function() {
		var dir = $(this).attr('alt');
		$(this).bind('click', function() {
			rotateNavigate(dir);
		});	
	});
});
/* bind slider controls */
$(function() {
	$('div#slider_control IMG.selector').each(function(index) {
		$(this).bind('click', function() {
			moveSlider(index);
		
		});
	});
	$('div#slider_control SPAN.arrows IMG').each(function() {
		var dir = $(this).attr('alt');
		$(this).bind('click', function() {
			sliderNavigate(dir);
		});	
	});	
});
/* bind detail menu controls */
$(function () {
	$('div#details_menu SPAN').each(function(index) {
		$(this).bind('click', function() {
			detailSelect(index);
		});
	});	
});
/* bind big image controls */
$(function() {
	$('div#image_control IMG.selector').each(function(index) {
		$(this).bind('click', function() {
			changeImage(index);
		});
	});
	$('div#image_control SPAN.arrows IMG').each(function() {
		var dir = $(this).attr('alt');
		$(this).bind('click', function() {
			navigateImage(dir);
		});	
	});
	$('div#image_control IMG.selector:first').attr('src','img/frame_massive.png');
	$('div#big_image IMG').bind('click', function() { navigateImage('Forward') });
});
/* bind plans controls */
$(function() {
	$('div#plans_control IMG.selector').each(function(index) {
		$(this).bind('click', function() {
			changePlans(index);
		});
	});
	$('div#plans_control SPAN.arrows IMG').each(function() {
		var dir = $(this).attr('alt');
		$(this).bind('click', function() {
			navigatePlans(dir);
		});	
	});
	$('div#plans_control IMG.selector:first').attr('src','img/frame_massive.png');
	$('div#images_plans IMG').bind('click', function() { navigatePlans('Forward') });
});
/* bind video controls */
$(function() {
	$('div#videos_control IMG.selector').each(function(index) {
		$(this).bind('click', function() {
			changeVideo(index);
		});
	});
	$('div#videos_control SPAN.arrows IMG').each(function() {
		var dir = $(this).attr('alt');
		$(this).bind('click', function() {
			navigateVideos(dir);
		});	
	});
	$('div#videos_control IMG.selector:first').attr('src','img/frame_massive.png');
});

/* bind debug text animation stop and go commands */
$(function() {
	var stop = $('span#textmovementstop');
	var go = $('span#textmovementgo');
	
	if(window.AnimateText == true) {
		stop.show(); go.hide();	
	} else {
		stop.hide(); go.show();	
	}
	
	stop.children('a').bind('click', function() {
		window.AnimateText = false;
		stop.hide(); go.show();	
		$('div#movingtext').clearQueue().stop();
	});
	go.children('a').bind('click', function() {
		window.AnimateText = true;
		stop.show(); go.hide();
		$(window).trigger('resize');
	});
});
/* fill animation speed and bind function */
$(function() {
	var inputfield = $('INPUT#textmovementspeed');
	inputfield.val(window.AnimationSpeed);
	
	inputfield.bind('change', function() {
		var inputfield = $('INPUT#textmovementspeed');
		var speed = parseInt(inputfield.val());
		if(isNaN(speed)) {
			inputfield.val(window.AnimationSpeed);
			speed = window.AnimationSpeed;	
		}
		if(speed < 5) {
			speed = 5;
			inputfield.val(speed);
		}
		window.AnimationSpeed = speed;
	});
});
/* bind debug scaling commands */
$(function() {
	var stop = $('span#autoscalingstop');
	var go = $('span#autoscalinggo');
	var auto = $('p.auto_scaling');
	var manual = $('p.manual_scaling');
	
	if(window.AutomaticScaling == true) {
		stop.show(); go.hide();	auto.show(); manual.hide();
	} else {
		stop.hide(); go.show();	auto.hide(); manual.show();
	}
	
	$('input#scalingmod').val(window.ScalingModificator);
	
	stop.children('a').bind('click', function() {
		window.AutomaticScaling = false;
		stop.hide(); go.show(); auto.hide(); manual.show()
	});
	go.children('a').bind('click', function() {
		window.AutomaticScaling = true;
		stop.show(); go.hide(); auto.show(); manual.hide();
		$(window).trigger('resize');
	});
	
	$('p.auto_scaling INPUT[type=submit]').bind('click', function() { change_scalingmod(); });
	$('p.manual_scaling INPUT[type=submit]').bind('click', function() { manual_scaling(); });
});
/* fill autoscaling control and bind functions */
/*$(function() {
	if(window.AutomaticScaling == true) {
		stop.show(); go.hide();	
	} else {
		stop.hide(); go.show();	
	}
	
	stop.children('a').bind('click', function() {
		window.AutomaticScaling = false;
		stop.hide(); go.show();
	});
	go.children('a').bind('click', function() {
		window.AutomaticScaling = true;
		stop.show(); go.hide();
		$(window).trigger('resize');
	});
});*/

/* Shoot off resize-event on page initiation, just to be sure */
$(function() {
	$(window).trigger('resize');
});
/* Shoot off debug retract on page initation */
$(function() {
	debug_retract();
});

/* ENVIRONMENTAL VARIABLES */
var ScalingModificator = 5;

var AutomaticScaling = true;
var minimumScale = 0.8;
var maximumScale = 3.2;

var AnimateText = true;
var AnimationSpeed = 50;

var coverRotation = 0;

var sliderState = 0;
var sliderFrameWidth = 41.2;
var sliderMovePerClick = 3;
var sliderBusy = false;
var sliderSpeed = 500;

var imageState = 0;

var plansState = 0;

var videosState = 0;
