// JavaScript Document
function file(fichier)  {
  if(window.XMLHttpRequest) {
    xhr_object = new XMLHttpRequest();
  }
  else if(window.ActiveXObject) {
      xhr_object = new ActiveXObject("Microsoft.XMLHTTP");
    }
  else {
    return(false);
  }
  xhr_object.open("GET", fichier, false);
  xhr_object.send(null);
  if(xhr_object.readyState == 4) {
    return(xhr_object.responseText);
  }
  else {
    return(false);
  }
}	

function fct_ajax(div, function_name)  {
  //text = file("http://127.0.0.1/causette/includes/traitement_ajax.php?ajax_action="+function_name);
  text = file(cfg_address+"includes/traitement_ajax.php?ajax_action="+function_name);
  document.getElementById(div).innerHTML = text;
}

function save_thumbnail () {
	var folder = document.getElementById('folder').value;
	var size_max = document.getElementById('size_max').value;
  var width = document.getElementById('width_photo').value;
  var height = document.getElementById('height_photo').value;
  var width_temp = document.getElementById('width_photo_temp').value;
  var height_temp = document.getElementById('height_photo_temp').value;  
	var width_croop = document.getElementById('width_croop').value;
  var height_croop = document.getElementById('height_croop').value;            
  fct_ajax('aff_uploaded_photo', 'save_thumbnail;'+document.getElementById('name_photo').value+';1;'+$('#x1').val()+';'+$('#y1').val()+';'+$('#w').val()+';'+$('#h').val()+';'+width+';'+height+';'+width_temp+';'+height_temp+';'+width_croop+';'+height_croop+';'+size_max+';'+folder+'');
  document.getElementById('sortie_creation').innerHTML = '';
  document.getElementById('border1').style.display='none';
  document.getElementById('border2').style.display='none';
  document.getElementById('outbottom').style.display='none';
  document.getElementById('outtop').style.display='none';
  document.getElementById('outleft').style.display='none';
  document.getElementById('outright').style.display='none';
  document.getElementById('area').style.display='none';                  
  document.getElementById('crooper').innerHTML = '<h3>Image enregistr&eacute;e avec succ&egrave;s</h3>';                 
}

function preview(img, selection) { 
	var width_croop = document.getElementById('width_croop').value / 2;
	var height_croop = document.getElementById('height_croop').value / 2;
	var scaleX = width_croop / selection.width; 
	var scaleY = height_croop / selection.height;        	
	$('#uploaded_file + div > img').css({ 
		width: Math.round(scaleX * document.getElementById('width_photo_temp').value) + 'px', 
		height: Math.round(scaleY * document.getElementById('height_photo_temp').value) + 'px',
		marginLeft: '-' + Math.round(scaleX * selection.x1) + 'px', 
		marginTop: '-' + Math.round(scaleY * selection.y1) + 'px' 
	});
	$('#x1').val(selection.x1);
	$('#y1').val(selection.y1);
	$('#x2').val(selection.x2);
	$('#y2').val(selection.y2);
	$('#w').val(selection.width);
	$('#h').val(selection.height);
} 

function crooper(ratio){
	$('#uploaded_file').imgAreaSelect({ aspectRatio: '1:'+ratio, onSelectChange: preview }); 
}

function aff_photo(name_photo) {
	document.getElementById('view_photo').innerHTML = '<img src="photos/uploaded_photos/'+name_photo+'" />';
}

function aff_div(old_div, new_div){
	if (new_div==""){
		$("#"+old_div).removeClass();
	}
}

function update_img(){
	$("#upload_photo").removeClass();
	$("#view_photo").addClass("hidden");
	$("#upload_photo").addClass("visible");
}

function update_file(){
	$("#upload_swf").removeClass();
	$("#view_swf").addClass("hidden");
}

function set_form_rubrique(rubrique_id){
	$("#form_rubrique_"+rubrique_id).removeClass();
	$("#form_rubrique_"+rubrique_id).addClass("rubrique");
	$("#rubrique_"+rubrique_id).addClass("hidden");
	old_rubrique_id = document.getElementById('rubrique_id').value;
	if (old_rubrique_id != "") {
		$("#rubrique_"+old_rubrique_id).removeClass();
		$("#rubrique_"+old_rubrique_id).addClass("rubrique");
		$("#form_rubrique_"+old_rubrique_id).addClass("hidden");		
	}
	document.getElementById('rubrique_id').value = rubrique_id;
}

function cancel_form_rubrique(rubrique_id){
	$("#rubrique_"+rubrique_id).removeClass();
	$("#rubrique_"+rubrique_id).addClass("rubrique");
	$("#form_rubrique_"+rubrique_id).addClass("hidden");
}

function confirm_message (message, url) {
	check = confirm(message);
	if(check == true) window.location.href = url;
}

function open_same_window(url){
	window.open(url, "_self");
}	

$(document).ready( function(){ 
$('#news').innerfade({ animationtype: 'slide', speed: 750, timeout: 2000, type: 'random', containerheight: '1em' }); 
$('#list_articles').innerfade({ speed: 'slow', timeout: 8000, type: 'sequence', containerheight: '120px' }); 
$('.fade').innerfade({ speed: 'slow', timeout: 1000, type: 'sequence', containerheight: '1.5em' }); } ); 

