var report_error_message = '<p id="start_message"><strong>Hai notato un\'errore in questa pagina ?</strong> usa il campo di testo qui sotto per spiegarci brevemente il problema e correggeremo al pi&ugrave; presto!!</p>';

function submit_warning(submitter, cat, identifyer) {
	$('body').prepend('<div id="submit-warning-dialog"></div>');
	
	$('body').prepend('<div id="overlay"></div>');
	$('#submit-warning-dialog').append('<p id="close"><span><a href="#" id="close_button">chiudi</a></span></p>');
	var pageSizes = getPageSize();
	
	$('#close_button').bind('click', function() {
									$("#submit-warning-dialog").hide(500);
									$("#submit-warning-dialog").remove();
									$("#overlay").remove();
								});
								
	if(cat == 'events')
		$('#submit-warning-dialog').append(report_error_message);
		
	$('#submit-warning-dialog').append('<p id="end_message">La tua segnalazione &egrave; stata registrata.<strong> Grazie!!</strong></p>');
	$('#submit-warning-dialog').append('<p id="alert_message"><strong>Inserisci descizione del problema</strong></p>');
	$('#submit-warning-dialog').append('<textarea id="warning_message"></textarea>');
	$('#warning_message').focus( function() { $('#alert_message').hide(1000); } );
	
	$('#submit-warning-dialog').append('<input type="submit" id="submit_message" value="Invia Segnalazione" />');
	$('#submit_message').bind("click", {submitter:submitter, cat:cat, identifyer:identifyer}, send_warning ); 
	
	$('#submit-warning-dialog').css({ left:(pageSizes[2]/2) - ( parseInt($('#submit-warning-dialog').css('width')) /2 ) +"px", 
									top:(pageSizes[3]/2) - ( parseInt($('#submit-warning-dialog').css('height')) /2 ) +"px"
									});
	
}

function send_warning(event) {
	if( $('#warning_message').val() ) {
		$.post("http://www.skatemap.it/ajax/save_warning.php", { submitter:event.data.submitter, cat:event.data.cat, page:event.data.identifyer, error_message:$('#warning_message').val() }, warning_saved );
		$('#submit_message').attr("disabled","disabled");
		$('#warning_message').attr("disabled","disabled");	
	} else {
		$('#alert_message').show(1000);
	}
}

function warning_saved() {
	$('#alert_message').hide(500);
	$('#start_message').hide(1000);
	$('#end_message').show(1000);
}

function scrollingDetector(){
    if (navigator.appName == "Microsoft Internet Explorer") {
	return (document.documentElement.scrollTop?document.documentElement.scrollTop:document.body.scrollTop);
    } else {
        return window.pageYOffset;
    }
}

function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
    
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		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;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}

	arrayPageSize = new Array(pageWidth, pageHeight, windowWidth, windowHeight) 
	return arrayPageSize;
}
