
var newline = "\n";
var tab 	= "\t";

var LandingPage = function () {
	/**
	 * INIT
	 */
	this.initTitle();
	this.initScale();
	this.initMachines();
};

LandingPage.scaleStart 		= 0;
LandingPage.scaleEnd 		= 100;
LandingPage.machineNumber 	= 0;
LandingPage.widthMaxChart 	= 431;
LandingPage.currentSerie 	= '';

LandingPage.prototype.initTitle = function () {
	this.LP_title = new LP_Title($('table.LP_header'));
};

LandingPage.prototype.initScale = function () {
	LandingPage.scaleStart 	= $('td.LP_scale_start').html();
	LandingPage.scaleEnd 	= $('td.LP_scale_end').html();
};

LandingPage.prototype.initMachines = function () {
	this.series = new Array();
	
	var that = this;
	$('table.LP_configuration tr.LP_serie').each(function(i){
		that.series[i] = new LP_Serie($(this));
	});
};

LandingPage.prototype.addEvents = function () {
	LandingPage.currentSerie = '';
	
	var that = this;
	$('td.LP_serie').hover(function(){
		var serieName = $(this).parents('table.LP_series').find('tr:first-child td.LP_serie span').html();
		if (serieName != LandingPage.currentSerie) {
			$(this).css('backgroundColor', '#b6b6b6');
			LandingPage.currentSerie = serieName;
			$.each(that.series, function(i){
				if (serieName == this.name) {
					$('div.LP_rollover').html(that.series[i].rollover);
					$('div.LP_rollover').css('height', ($('div.LP_charts').height() - 1));
					$('div.LP_rollover').show();
				}
			});
		}
	}, function () {
		$(this).css('backgroundColor', 'transparent');
		LandingPage.currentSerie = '';
		$('div.LP_rollover').html('');
		$('div.LP_rollover').hide();
	});
	$('td.LP_serie').click(function(){
		var serieName = $(this).parents('table.LP_series').find('tr:first-child td.LP_serie span').html();
		$.each(that.series, function(i){
			if (serieName == this.name) {
				window.location.href = that.series[i].link;
			}
		});
	});
};

LandingPage.prototype.display = function () {
	var output = '';
	
	/**
	 * PREPARING DATA TO DISPLAYING
	 */
	output += this.LP_title.getOutput();
	$.each(this.series, function(i, serie){
		output += serie.getOutput();
	});
	
	/**
	 * DISPLAYING
	 */
	$('div.LP_loading').hide();
	$('div.LP_ui').html(output);
	
	/**
	 * APPLYING CSS
	 */
	$('table.LP_series').each(function(){
		$(this).find('tr.LP_serie_row:first').find('td').each(function(){
			if ($(this).hasClass('LP_serie') || $(this).hasClass('LP_model') || $(this).hasClass('LP_chart')) {
				$(this).css('padding-top', '5px');
			}
			if ($(this).hasClass('LP_chart')) {
				$(this).css('height', '21px');
			}
		});
		$(this).find('tr.LP_serie_row:last').find('td').each(function(){
			if ($(this).hasClass('LP_serie') || $(this).hasClass('LP_model') || $(this).hasClass('LP_chart')) {
				$(this).css('padding-bottom', '5px');
			}
			if ($(this).hasClass('LP_chart')) {
				$(this).css('height', '21px');
			}
		});
		
		$(this).find('tr.LP_serie_title_row').find('td').each(function(){
			if ($(this).hasClass('LP_title_serie')) {
				$(this).css('padding-bottom', '5px');
				$(this).css('padding-top', '5px');
			}
			if ($(this).hasClass('LP_chart')) {
				$(this).css('height', '21px');
			}
		});
		
	});
	
	$('div.LP_ipm').each(function(){
		var divChart = $(this).parents('td.LP_chart').find('div:first');
		
		// GET INT LEFT CHART
		var leftChart = parseInt(divChart.css('left').substring(0, divChart.css('left').length - 2));
		
		$(this).css('left', (leftChart) + 'px');
	});
	
	$('div.LP_comment').each(function(){
		if ($(this).html() != '') {
			var divChart 	= $(this).parents('td.LP_chart').find('div:first');
			var divIPM 		= $(this).parents('td.LP_chart').find('div.LP_ipm');
			
			// GET INT LEFT CHART
			var leftChart = parseInt(divChart.css('left').substring(0, divChart.css('left').length - 2));
			
			// GET INT WIDTH CHART
			var widthChart = parseInt(divChart.css('width').substring(0, divChart.css('width').length - 2));
			
			// GET INT WIDTH COMMENT
			var widthComment = parseInt($(this).width());
			
			// GET INT WIDTH IPM
			var widthIPM = 0;
			if (divIPM.length == 1) {
				widthIPM = parseInt(divIPM.width());
			}
			
			if ($(this).hasClass('LP_comment_align_right')) {
				// RIGHT ALIGN
				$(this).css('left', (leftChart + 5) + 'px');
			}
			else {
				// LEFT ALIGN
				$(this).css('left', (leftChart - widthChart - widthComment - widthIPM - 5) + 'px');
			}
		}
	});
	/**/
};
