/*
+---------------------------------------------------------------------------
|   Editions Libellules Website v2.0 Final
|   ========================================
|   by Alban COQUOIN
|   (c) 2009 Editions Libellules
|   http://www.editionslibellule.com/
|   ========================================
|   "EditionsLibellule.Com" as "Editions Libellule"
|   to Thomas Gaudron - Editions Libellule
|   ========================================
|   Coded by Alban COQUOIN - DNA Web
|   Web: http://www.dnaweb.fr/
|   Email: contact@dnaweb.fr
|   Siret: 512-112-772 00011
+---------------------------------------------------------------------------
|
|   > Javascript Script
|   > Date started: Wen, 24th July 2009
|
+---------------------------------------------------------------------------
*/

function pad( num ) {
	num = num + '';
	while( num.length < 4 ) {
		num = '0' + num;
	}
	return num;
}

function file_exists( url ) {
    var req = this.window.ActiveXObject ? new ActiveXObject( 'Microsoft.XMLHTTP' ) : new XMLHttpRequest();
    if ( !req ) {
		throw new Error( 'XMLHttpRequest not supported' );
    }
    req.open( 'HEAD', url, false );
    req.send( null );
    if ( req.status == 200 || req.status == 304 ) {
        return true;
    }
    return false;
}

function view( id ) {
	if ( id == 'close' ) {
		document.getElementById("overlay").style.display = "none";
		document.images['view_map'].src = 'images/design/blank.gif';
	} else if ( id == 'next' || id == 'previous' ) {
		document.getElementById("overlay").style.display = "block";
		var match = document.images['view_map'].src.match(/([0-9]{0,4})_b.png/i);
		var this_id = parseFloat( match[0].replace( '_b.png', '' ) );
		var id = ( id == 'next' ) ? this_id + 1 : this_id - 1;
		view( file_exists( 'images/chars/' + pad( id ) + '_b.png' ) ? pad( id ) : 'close' );
	} else {
		document.getElementById("overlay").style.display = "block";
		document.images['view_map'].src = 'images/chars/' + id + '_b.png';
	}
}

function view_admin( id, type, letter, x, y ) {
	if ( file_exists( 'images/' + type + '/' + id + ( letter != '' ? '_' + letter : letter + '.jpg' ) ) ) {
		document.getElementById("overlay").style.display = "block";
		document.images['view_admin'].src = 'images/' + type + '/' + id + ( letter != '' ? '_' + letter : letter + '.jpg' );
		document.images['view_admin'].style.marginLeft = '-' + Math.round( x / 2 ) + 'px';
		document.images['view_admin'].style.marginTop = '-' + Math.round( y / 2 ) + 'px';
		document.images['view_admin'].setAttribute( 'onclick', 'document.images["view_admin"].src = "images/design/blank.gif";document.getElementById("overlay").style.display = "none";' );
	}
}

function rotate( i, t, m, a ) {
	var tmp = new Array( 'e', 'f', 'g', 'h' );
	var a = ( a > 2 || a == undefined ) ? 0 : a + 1;
	var m = ( m == 'books' || m == undefined ) ? 'books' : 'boxes';
	if ( file_exists( 'images/' + m + '/' + pad( i ) + '_' + tmp[a] + '.jpg' ) ) {
		document.images['view_rotate'].src = 'images/' + m + '/' + pad( i ) + '_' + tmp[a] + '.jpg';
	}
	tempo = setTimeout( 'rotate( ' + i + ', ' + t + ', "' + m + '", ' + a + ' );', t * 1000 );
}

function popup( u,w, h ) {
	var t = ( screen.height - h ) / 2;
	var l = (screen.width - w ) / 2;
	window.open( u, '', 'top=' + ( ( screen.height - h ) / 2 ) + ',left=' + ( ( screen.width - w ) / 2 ) + ',width=' + w + ',height=' + h + 'toolbar=0,location=0,directories=0,status=0,scrollbars=1,resizable=1,copyhistory=0,menuBar=0');
}

function rotate_upload( id, n, t, e, f, g, h, s_e, s_f, s_g, s_h  ) {
	if( document.getElementById( id ).innerHTML == '' ) {
		var s_e = ( s_e != 1 ) ? '' : 1; 
		var s_f = ( s_f != 1 ) ? '' : 1; 
		var s_g = ( s_g != 1 ) ? '' : 1; 
		var s_h = ( s_h != 1 ) ? '' : 1; 
		e = ( e == '1' ) ? '<a href="javascript:view_admin( \'' + n + '\', \''+ t +'\', \'e\', 710, 530 );">Voir l\'image associée</a> - <input type="hidden" name="suppr_e" value="' + s_e + '" /><a href="javascript:;" onclick="document.forms[\'admin_' + t + '\'].suppr_e.value = ( document.forms[\'admin_' + t + '\'].suppr_e.value == 1 ) ? \'\' : 1;this.innerHTML = ( document.forms[\'admin_' + t + '\'].suppr_e.value == 1 ) ? \'Ne pas supprimer\' : \'Supprimer\';">Supprimer</a>' : '';
		f = ( f == '1' ) ? '<a href="javascript:view_admin( \'' + n + '\', \''+ t +'\', \'f\', 710, 530 );">Voir l\'image associée</a> - <input type="hidden" name="suppr_f" value="' + s_f + '" /><a href="javascript:;" onclick="document.forms[\'admin_' + t + '\'].suppr_f.value = ( document.forms[\'admin_' + t + '\'].suppr_f.value == 1 ) ? \'\' : 1;this.innerHTML = ( document.forms[\'admin_' + t + '\'].suppr_f.value == 1 ) ? \'Ne pas supprimer\' : \'Supprimer\';">Supprimer</a>' : '';
		g = ( g == '1' ) ? '<a href="javascript:view_admin( \'' + n + '\', \''+ t +'\', \'g\', 710, 530 );">Voir l\'image associée</a> - <input type="hidden" name="suppr_g" value="' + s_g + '" /><a href="javascript:;" onclick="document.forms[\'admin_' + t + '\'].suppr_g.value = ( document.forms[\'admin_' + t + '\'].suppr_g.value == 1 ) ? \'\' : 1;this.innerHTML = ( document.forms[\'admin_' + t + '\'].suppr_g.value == 1 ) ? \'Ne pas supprimer\' : \'Supprimer\';">Supprimer</a>' : '';
		h = ( h == '1' ) ? '<a href="javascript:view_admin( \'' + n + '\', \''+ t +'\', \'h\', 710, 530 );">Voir l\'image associée</a> - <input type="hidden" name="suppr_h" value="' + s_h + '" /><a href="javascript:;" onclick="document.forms[\'admin_' + t + '\'].suppr_h.value = ( document.forms[\'admin_' + t + '\'].suppr_h.value == 1 ) ? \'\' : 1;this.innerHTML = ( document.forms[\'admin_' + t + '\'].suppr_h.value == 1 ) ? \'Ne pas supprimer\' : \'Supprimer\';">Supprimer</a>' : '';
		document.getElementById( id ).innerHTML = 'Image 1 <input type="file" name="rotate_e" value="" />'+e+'<br />Image 2 <input type="file" name="rotate_f" value="" />'+f+'<br />Image 3 <input type="file" name="rotate_g" value="" />'+g+'<br />Image 4 <input type="file" name="rotate_h" value="" />'+h+'<br /><i>Fichiers servant à la prévisualiation par rotation automatique.<br /><u>Spécification :</u> Format JPG/JPEG - 100ko max - 710x530px.</i>';
	} else {
		document.getElementById( id ).innerHTML = '';
	}
}