function update_header(url) {
	new Ajax.Updater(
		'header',
		url,
		{
			evalScripts: true,
			onLoading:
				function(request, json) {
					$('article_numbers_selected').update('');
					$('article_numbers_lbl').update('');
					$('article_numbers_count').update('');

					Element.hide('divide');
					Element.show('divide_indicator');

					Element.hide('show');
					Element.show('show_indicator');

					Element.hide('list');
					Element.show('list_indicator');

					Element.hide('header');
					Element.show('header_indicator');
				},
			onComplete:
				function(request, json) {
					Element.hide('header_indicator');
					Element.show('header');
					new Effect.Highlight('header', {});
				}
		}
	);
}

function update_list(url) {
	new Ajax.Updater(
		'list',
		url,
		{
			evalScripts: true,
			onLoading:
				function(request, json) {
					$('article_numbers_selected').update('');
					$('article_numbers_lbl').update('');
					$('article_numbers_count').update('');

					Element.hide('divide');
					Element.show('divide_indicator');

					Element.hide('show');
					Element.show('show_indicator');

					Element.hide('list');
					Element.show('list_indicator');
				},
			onComplete:
				function(request, json) {
					Element.hide('list_indicator');
					Element.show('list');
					new Effect.Highlight('list', {});
				}
		}
	);
}

function update_show(url) {
	new Ajax.Updater(
		'show',
		url,
		{
			evalScripts: true,
			onLoading:
				function(request, json) {
					Element.hide('divide');
					Element.show('divide_indicator');

					Element.hide('show');
					Element.show('show_indicator');
				},
			onComplete:
				function(request, json) {
					Element.hide('divide_indicator');
					Element.show('divide');

					Element.hide('show_indicator');
					Element.show('show');
					new Effect.Highlight('show', {});
				}
		}
	);
}

var new_max_width = 0;
var new_max_height = 0;

function resize_elements(is_page_news) {
	var min_width = 1000;
	var min_height = 558;

	arrayWindowSize = get_window_size();
	if (min_width > arrayWindowSize[0]) arrayWindowSize[0] = min_width;
	if (min_height > arrayWindowSize[1]) arrayWindowSize[1] = min_height;
	if ((new_max_width != arrayWindowSize[0]) || (new_max_height != arrayWindowSize[1])) {
		if (new_max_width != arrayWindowSize[0]) {
			new_max_width = arrayWindowSize[0];
			change_calculate_width(is_page_news);
		}
		if (new_max_height != arrayWindowSize[1]) {
			new_max_height = arrayWindowSize[1];
			change_calculate_height(is_page_news);
		}
	}
}

var list_height = 0;
var show_height = 0;
var start_drag_y = 0;
var end_drag_y = 0;

var divide_height = 0;

function body_onload(aParams) {
	is_page_news = aParams['is_page_news'];
	var url = '_header.php?is_page_news=' + is_page_news;
	if (aParams['IdArticolo']) url += '&IdArticolo=' + aParams['IdArticolo'];
	if (aParams['Grouped']) url += '&Grouped=' + aParams['Grouped'];
	if (aParams['IdCategoria']) url += '&IdCategoria=' + aParams['IdCategoria'];
	if (aParams['Data']) url += '&Data=' + aParams['Data'];
	if (aParams['Keywords']) url += '&Keywords=' + aParams['Keywords'];

	update_header(url);

	if (is_page_news) {
		new Draggable(
			'divide',
			{
				constraint:'vertical',
				onStart: function(draggableElement, event) {
					start_drag_y = Event.pointerY(event);

					list_height = Element.getHeight('list');
					show_height = Element.getHeight('show');

					divide_height = Element.getHeight('divide');
				},
				onEnd: function(draggableElement, event) {
					end_drag_y = Event.pointerY(event);

					var new_show_height = show_height;
					var new_list_height = list_height;

					if (end_drag_y > start_drag_y) {
						var delta_y = end_drag_y - start_drag_y;
						if ((show_height - delta_y) < divide_height) {
							new_show_height = divide_height;
						}
						else {
							new_show_height = show_height - delta_y;
						}
					}

					if (end_drag_y < start_drag_y) {
						var delta_y = start_drag_y - end_drag_y;
						if ((list_height - delta_y) < divide_height) {
							new_list_height = divide_height;
						}
						else {
							new_list_height = list_height - delta_y;
						}
					}

					if (new_show_height < show_height) {
						$('show').style.height = new_show_height + 'px';
						$('list').style.height = (list_height + (show_height - new_show_height)) + 'px';
					}

					if (new_list_height < list_height) {
						$('list').style.height = new_list_height + 'px';
						$('show').style.height = (show_height + (list_height - new_list_height)) + 'px';
					}

					if ((new_show_height < show_height) || (new_list_height < list_height)) {
						$('show_indicator').style.height = Element.getHeight('show') + 'px';
						$('list_indicator').style.height = Element.getHeight('list') + 'px';
					}

					$('divide').setStyle({top: '0px'});
				},
				revert:false
			}
		);
	}

	resize_elements(is_page_news);
	window.onresize = function() {
		resize_elements(is_page_news);
	}
}

var id_main_form_timeout;

function refresh_contents() {
	if (id_main_form_timeout) {
		clearTimeout(id_main_form_timeout);
	}
	id_main_form_timeout = setTimeout('submit_main_form()', 1000 * 60 * 5);
}

function submit_main_form() {
	new Ajax.Updater(
		'header',
		'_header.php',
		{
			evalScripts: true,
			onLoading:
				function(request, json) {
					$('article_numbers_selected').update('');
					$('article_numbers_lbl').update('');
					$('article_numbers_count').update('');

					Element.hide('divide');
					Element.show('divide_indicator');

					Element.hide('show');
					Element.show('show_indicator');

					Element.hide('list');
					Element.show('list_indicator');

					Element.hide('header');
					Element.show('header_indicator');
				},
			onComplete:
				function(request, json) {
					Element.hide('header_indicator');
					Element.show('header');
					new Effect.Highlight('header', {});
				},
			parameters:Form.serialize('main_form')
		}
	);

	refresh_contents();
}

function update_list_from_params(IdArticolo, Grouped, IdCategoria, Anno, Mese, Giorno, Keywords) {
	update_list('_list.php?IdArticolo=' + IdArticolo + '&Grouped=' + Grouped + '&IdCategoria=' + IdCategoria + '&Anno=' + Anno + '&Mese=' + Mese + '&Giorno=' + Giorno + '&Keywords=' + Keywords);
}

function trigger_categorie(category_list) {
	if (category_list && (category_list.selectedIndex >= 0)) {
		document.main_form.IdCategoria.value = category_list.options[category_list.selectedIndex].value;
		submit_main_form();
	}
}

function trigger_keywords(theForm) {
	document.main_form.Keywords.value = theForm.Keywords.value;
	submit_main_form();
}

function trigger_prev_next(Anno, Mese, Giorno) {
	document.main_form.Anno.value = Anno;
	document.main_form.Mese.value = Mese;
	document.main_form.Giorno.value = Giorno;
	submit_main_form();
}

function change_article(IdArticolo, Grouped) {
	if (IdArticolo) {
		document.main_form.IdArticolo.value = IdArticolo;
		document.main_form.Grouped.value = Grouped;
		if (id_selected_article) {
			Element.toggleClassName('news_id_' + id_selected_article, 'selected');
		}
		id_selected_article = IdArticolo;
		Element.toggleClassName('news_id_' + id_selected_article, 'selected');
		$('article_numbers_selected').update(document.articles_order['hidden_news_id_' + IdArticolo].value);
		update_show('_show.php?IdArticolo=' + IdArticolo + '&Grouped=' + Grouped);
	}
}

function go_to_next_article(IdArticolo) {
	if (document.articles_order['hidden_news_id_' + IdArticolo]) {
		current_order = document.articles_order['hidden_news_id_' + IdArticolo].value;
		if (document.articles_order['hidden_order_' + (parseInt(current_order) + 1)]) {
			newIdArticolo = document.articles_order['hidden_order_' + (parseInt(current_order) + 1)].value;
			change_article(newIdArticolo, 'false');
			$('news_id_' + newIdArticolo).focus();
		}
	}
}

function go_to_prev_article(IdArticolo) {
	if (document.articles_order['hidden_news_id_' + IdArticolo]) {
		current_order = document.articles_order['hidden_news_id_' + IdArticolo].value;
		if (document.articles_order['hidden_order_' + (parseInt(current_order) - 1)]) {
			newIdArticolo = document.articles_order['hidden_order_' + (parseInt(current_order) - 1)].value;
			change_article(newIdArticolo, 'false');
			$('news_id_' + newIdArticolo).focus();
		}
	}
}

function go_to_grouped_article(IdArticolo) {
	if (document.articles_order['hidden_news_id_' + IdArticolo]) {
		change_article(IdArticolo, 'true');
		$('news_id_' + IdArticolo).focus();
	}
}

var new_height_percent = 0;

function change_whole_height(percent, is_page_news) {
	new_height_percent = percent;
	var min = 768;
	var max = 1200;
	var increment = ((max - min) / 100).toFixed() * percent;

	new_max_height = min + increment;

	change_calculate_height(is_page_news);
}

function change_calculate_height(is_page_news) {
	var deltay = 20;

	$('outer_cnt').setStyle({height: (new_max_height - deltay) + 'px'});
	$('content').setStyle({height: (new_max_height - (deltay + 100)) + 'px'});

	if (is_page_news) {
		divide_height = Element.getHeight('divide');

		list_height = ((new_max_height - (deltay + 100) - divide_height) / 2).toFixed();
		show_height = (new_max_height - (deltay + 100) - divide_height - list_height);

		$('list_indicator').setStyle({height: list_height + 'px'});
		$('list').setStyle({height: show_height + 'px'});

		$('show_indicator').setStyle({height: list_height + 'px'});
		$('show').setStyle({height: show_height + 'px'});
	}
}

var new_width_percent = 0;

function change_whole_width(percent, is_page_news) {
	new_width_percent = percent;
	var min = 1024;
	var max = 1600;
	var increment = ((max - min) / 100).toFixed() * percent;

	new_max_width = min + increment;

	change_calculate_width(is_page_news);
}

function change_calculate_width(is_page_news) {
	$('outer_cnt').setStyle({width: (new_max_width - 74) + 'px'});
	$('header_indicator').setStyle({width: (new_max_width - 76) + 'px'});
	$('header').setStyle({width: (new_max_width - 76) + 'px'});
	$('content').setStyle({width: (new_max_width - 76) + 'px'});

	resize_header_right();

	if (is_page_news) {
		$('list_indicator').setStyle({width: (new_max_width - 76) + 'px'});
		$('list').setStyle({width: (new_max_width - 76) + 'px'});
		$('show_indicator').setStyle({width: (new_max_width - 76) + 'px'});
		$('show').setStyle({width: (new_max_width - 76) + 'px'});
	}
}

function resize_header_right() {
	if (new_max_width) {
		var new_right_width = new_max_width - 327;
		if ($('header_indicator_right')) {
			var old_right_width = Element.getHeight('header_indicator_right');
			if (old_right_width != new_right_width) $('header_indicator_right').setStyle({width: new_right_width + 'px'});
		}
		if ($('header_right')) {
			var old_right_width = Element.getHeight('header_right');
			if (old_right_width != new_right_width) $('header_right').setStyle({width: new_right_width + 'px'});
		}
	}
}

function get_window_size() {
	var windowWidth, windowHeight;

	if (self.innerHeight) {	// all except Explorer
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth; 
		} else {
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}

	arrayWindowSize = new Array(windowWidth, windowHeight);
	return(arrayWindowSize);
}

var list_char_size = 12;
var list_char_height = 17;

function increase_list_char_size() {
	if (list_char_size < 20) {
		list_char_size++;
		list_char_height++;

		set_list_char_size();
	}
}

function decrease_list_char_size() {
	if (list_char_size > 8) {
		list_char_size--;
		list_char_height--;

		set_list_char_size();
	}
}

function set_list_char_size() {
	if (list_char_size && list_char_height) {
		var row_divs = $$('#list div.row');
		for (var i=0; i<row_divs.length; i++) {
			row_div = row_divs[i];
			row_div.setStyle({height: list_char_height + 'px', lineHeight: list_char_height + 'px', fontSize: list_char_size + 'px'});
		}

		var row_anchors = $$('#list div.row a');
		for (var i=0; i<row_anchors.length; i++) {
			row_anchor = row_anchors[i];
			row_anchor.setStyle({height: list_char_height + 'px'});
		}
	}
}

var show_data_size = 10;
var show_title_size = 12;
var show_title_height = 30;
var show_text_size = 12;
var show_text_height = 15;

function increase_show_char_size() {
	if (show_data_size < 20) {
		show_data_size++;
		show_title_size++;
		show_title_height++;
		show_text_size++;
		show_text_height++;

		set_show_char_size();
	}
}

function decrease_show_char_size() {
	if (show_data_size > 8) {
		show_data_size--;
		show_title_size--;
		show_title_height--;
		show_text_size--;
		show_text_height--;

		set_show_char_size();
	}
}

function set_show_char_size() {
	if (show_data_size && show_title_size && show_text_size) {
		$$('#show div.top div.data')[0].setStyle({fontSize: show_data_size + 'px'});
		$$('#show div.title')[0].setStyle({height: show_title_height + 'px', lineHeight: show_title_height + 'px', fontSize: show_title_size + 'px'});
		$$('#show div.text')[0].setStyle({lineHeight: show_text_height + 'px', fontSize: show_text_size + 'px'});
	}
}

function salva_articolo() {
	IdArticolo = document.main_form.IdArticolo.value;
	Grouped = document.main_form.Grouped.value;
	if ((parseInt(IdArticolo) + 0) > 0) {
		document.location.href = 'salva_articolo.php?IdArticolo=' + IdArticolo + '&Grouped=' + Grouped;
	}
}

function esporta_articolo() {
	IdArticolo = document.main_form.IdArticolo.value;
	Grouped = document.main_form.Grouped.value;
	if ((parseInt(IdArticolo) + 0) > 0) {
		document.location.href = 'esporta_articolo.php?IdArticolo=' + IdArticolo + '&Grouped=' + Grouped;
	}
}

function stampa_articolo() {
	IdArticolo = document.main_form.IdArticolo.value;
	Grouped = document.main_form.Grouped.value;
	if ((parseInt(IdArticolo) + 0) > 0) {
		window.open('stampa_articolo.php?IdArticolo=' + IdArticolo + '&Grouped=' + Grouped, 'newsstampaarticolo' + IdArticolo, 'toolbar=yes,scrollbars=yes,menubar=yes,width=700,height=500,resizable=yes');
	}
}

function showSWF(swf_file, div_id, width, height, bgcolor) {
	html = '';
	html += '<object' + "\n";
	html += 'classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000"' + "\n";
	html += 'codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0"' + "\n";
	html += 'width="' + width + '"' + "\n";
	html += 'height="' + height + '"' + "\n";
	html += 'id="' + div_id + '_fileswf_id">' + "\n";
	html += '<param name="bgcolor" value="' + bgcolor + '" />' + "\n";
	html += '<param name="allowScriptAccess" value="sameDomain" />' + "\n";
	html += '<param name="movie" value="' + swf_file + '" /><param name="quality" value="high" /><embed' + "\n";
	html += 'src="' + swf_file + '"' + "\n";
	html += 'bgcolor="' + bgcolor + '"' + "\n";
	html += 'quality="high"' + "\n";
	html += 'width="' + width + '"' + "\n";
	html += 'height="' + height + '"' + "\n";
	html += 'name="' + div_id + '_fileswf_name"' + "\n";
	html += 'allowScriptAccess="sameDomain"' + "\n";
	html += 'type="application/x-shockwave-flash"' + "\n";
	html += 'pluginspage="http://www.macromedia.com/go/getflashplayer" />' + "\n";
	html += '</object>' + "\n";

	$(div_id).update(html);
}

