// JavaScript Document
	var original_image;
	var img_container = document.createElement('div');;
	var new_image;
	
	window.onload = background;
	window.onresize = resize;

function get_scroll(){
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  return scrOfY;
}
function show_picker(control){
	current_item = control;
	var picker = document.getElementById('color_picker');
	document.getElementById('color_value').value = get_current_color();
	picker.style.top = 470 + get_scroll() + 'px';
	picker.style.left = '180px';
	picker.style.display = 'inline';
}
function hide_picker(){
	document.getElementById('color_picker').style.display = 'none';
}
function get_current_color(){
	var controls = current_item.getElementsByTagName('input');
	return controls[0].value;
}
function set_current_color(color) {
	var controls = current_item.getElementsByTagName('input');
	controls[0].value = color;
}
function choose_color(){
	var picker = document.getElementById('color_picker');
	var color = document.getElementById('color_value').value;
	current_item.style.background = color;
	set_current_color(color);
	hide_picker();
}
function set_bg(){
	var container_block = document.getElementById('principale');
	var content_block = document.getElementById('principale_content');
	container_block.style.height = content_block.offsetHeight + 'px';
	
}
function background(){
	//this function creates an img tag inside a div tag
	if(document.body.background != ''){
		original_image = new Image()
		original_image.src = document.body.background;
		new_image = new Image();
		new_image.onload = resize;
		new_image.src = document.body.background;
		img_container.setAttribute('id', 'background_img');
		img_container.setAttribute('name', 'background_img');
		img_container.style.position = "absolute";
		img_container.style.top = 0;
		img_container.style.left = 0;
		
		img_container.appendChild(new_image);
		document.body.appendChild(img_container);
	}
}
function resize(){
	var ratio;
	var imgWidth = original_image.width;
	var imgHeight = original_image.height;
	if(document.body.background != ''){
		if(document.documentElement && document.documentElement.clientHeight){
			var scrWidth = document.documentElement.offsetWidth;
			var scrHeight = document.documentElement.clientHeight;
		}else if(document.body){
			var scrWidth = document.body.clientWidth;
			var scrHeight = document.body.clientHeight;
		}
		
		if((scrWidth - imgWidth) > (scrHeight - imgHeight)){
			ratio = scrWidth / imgWidth;
			if(Math.ceil(ratio * imgHeight) < scrHeight){
				ratio =scrHeight / imgHeight;
			}
		}else{
			ratio = scrHeight / imgHeight;
			if(Math.ceil(ratio * imgWidth) < scrWidth){
				ratio = (scrWidth /  imgWidth);
			}
		}
		
		transWidth = Math.ceil(imgWidth * ratio);
		transHeight = Math.ceil(imgHeight * ratio);
		
		img_container.style.width = scrWidth + 'px';
		img_container.style.height = scrHeight + 'px';
		new_image.style.width = transWidth + 'px';
		new_image.style.height = transHeight + 'px';
		//alert('screen:' + scrWidth + ' x ' + scrHeight + '\noriginale image: ' + imgWidth + ' x ' + imgHeight + '\nnew image size: ' + transWidth + ' x ' + transHeight);
	}
}
function open_calendar() {
	var cal_container = document.createElement('div');
	var cal_iframe = document.createElement('iframe');
	
	cal_container.setAttribute('id', 'cal_container');
	cal_container.style.zIndex = '10';
	cal_container.style.position = 'absolute';
	cal_container.style.top = '100px';
	cal_container.style.left = '200px';
	//cal_container.style.margin = '0 auto 0 auto';
	cal_container.style.width = '300px';
	cal_container.style.height = '200px';
	
	cal_iframe.setAttribute('id', 'cal_iframe');
	cal_iframe.setAttribute('src', './includes/calendar.php');
	cal_iframe.style.width = '100%';
	cal_iframe.style.height = '100%';
	
	cal_container.appendChild(cal_iframe);
	document.body.appendChild(cal_container);
}
