function openPopupWindow(url) {
  	popupWin = window.open(url, "Popup", "directories=no,toolbar=no,menubar=no,scrollbars=yes,resizable=yes,width=400,height=400,left=5,top=10");
}

function openPopupWindow(url, width, height) {
  	popupWin = window.open(url, "Popup", "directories=no,toolbar=no,menubar=no,scrollbars=yes,resizable=yes,width=" + width + ",height=" + height + ",left=5,top=10");
}

function resizeWindowWidthImg(addWidth, addHeight) {
	var img = new Image();
	img.src = window.document.images[0].src;

	var goodWidth = img.width + 100 + addWidth;
	var goodHeight = img.height + 100 + addHeight;

	if (goodWidth > screen.availWidth)
		goodWidth = screen.availWidth-5;

	if (goodHeight > screen.availHeight)
		goodHeight = screen.availHeight-10;
		
	if (goodWidth < 190)
		goodWidth = 190;

	if (goodHeight < 30)
		goodHeight = 30;

	window.resizeTo(goodWidth, goodHeight);
}

/*#################
# SPECIFIC
#################*/

var imageIndex = 0;
function switchImage(){
	$('image-top-tmp').childElements().invoke('hide');
	$('image-top-tmp').childElements()[imageIndex].show();
	$('image-top').update($('image-top-tmp').innerHTML);
	imageIndex++;
	if (imageIndex >= $('image-top-tmp').childElements().length)
		imageIndex = 0;
}

function doRequest(uri, mode, documentUri) {
	new Ajax.Updater('zone-chargement' , 'ajaxRequest', {
		method : 'get',
		requestHeaders : ['Cache-Control', 'no-cache', 'Pragma', 'no-cache', 'Content-Type', 'text/html;charset=UTF-8'],
		evalScripts : true,
		parameters : { 
			uri : uri,
			documentUri : documentUri,
			mode : mode,
			template : 'ajax/files',
			nocache : 'true'
		}
	});
}

function doGetNbPlaces(documentUri, position) {
	new Ajax.Updater(documentUri+'-representation'+position+'-nbplaces-abonne', 'ajaxRequest', {
		method : 'get',
		requestHeaders : ['Cache-Control', 'no-cache', 'Pragma', 'no-cache', 'Content-Type', 'text/html;charset=UTF-8'],
		evalScripts : true,
		parameters : {
			documentUri : documentUri,
			position : position,
			template : 'backoffice/ajax/places_back',
			nocache : 'true'
		}
	});
}

function doBilletterie (abonne, documentUri, nbrepresentations, firstDate, lastDate) {
	new Ajax.Updater('zone-billetterie' , 'ajaxRequest', {
		method : 'get',
		evalScripts : true,
		requestHeaders : ['Cache-Control', 'no-cache', 'Pragma', 'no-cache'],
		parameters : { 
			uri : documentUri,
			template : 'ajax/billetterie',
			abonne : abonne,
			nbrepresentations : nbrepresentations,
			firstDate : firstDate,
			lastDate : lastDate,
			nocache : 'true'
		}/*,
		onSuccess: function (){
			if (abonne == 'false'){
				$$('input.nbplaces_representation').invoke('hide');
				$$('input.select_representation').invoke('show');
			}
		}*/
	});
	$('zone-resultat-reservation').hide();
}

function doReservation (documentUri, libelle_de_spectacle) {
	new Ajax.Updater('zone-resultat-reservation', 'ajaxRequest', {
		method : 'post',
		requestHeaders : ['Cache-Control', 'no-cache', 'Pragma', 'no-cache'],
		parameters : 
			$('form_billetterie').serialize() + 
			'&documentUri=' + documentUri +
			'&documentName=' + libelle_de_spectacle +
			'&template=ajax/reservation' +
			'&nocache=true',
		onCreate: function(transport) {
			$('zone-resultat-reservation').show();
			$('zone-resultat-reservation').update('<div>Veuillez patienter pendant le traitement de l\'op&eacute;ration...</div>');
		},
		onComplete: function(transport) {
			$('zone-resultat-reservation').show();
			$('zone-resultat-reservation').update(transport.responseText.gsub('&amp;', '&'));
		}
	});
}