
var sUrlC = "/tickets/lib/calendar.php";
var sUrlTimesteps = "/tickets/lib/timesteps.php";

showBox = false;
calendarBox = null;
clipHeight = 0;
clipWidth = 0;	
finalWidth = 0;
finalHeight =0;
ratio = 0;

function zeroPad(num,count)
{
   var numZeropad = num + '';
   while(numZeropad.length < count){
      numZeropad = "0" + numZeropad;
   }
   return numZeropad;
}


function hideProgressive() 
{
  reachHeight = (clipHeight >= 0);
  reachWidth = (clipWidth <= finalWidth);
  
  if (reachHeight || reachWidth)
  {
    if (reachHeight)
		clipHeight -= 10;

    if (reachWidth)
		clipWidth += Math.round(10 * ratio);
	
	calendarBox.style.clip = "rect(0px," + finalWidth + "px," + clipHeight + "px," + clipWidth + "px)";
    setTimeout(hideProgressive,1); 
  }
}

function showProgressive() {

	calendarBox.style.visibility = "visible";

	reachHeight = (clipHeight < finalHeight);
	reachWidth = (clipWidth > 0);
  
  if (reachHeight || reachWidth)
  {
    if (reachHeight)
		clipHeight += 10;

    if (reachWidth)
		clipWidth -= Math.round(10 * ratio);
	
	calendarBox.style.clip = "rect(0px," + finalWidth + "px," + clipHeight + "px," + clipWidth + "px)";
    setTimeout(showProgressive,1); 
  }
}


function toggleClip()
{
	if (showBox) {
		hideProgressive(); 
		showBox = false;
		document['reservation_form_0'].timesteps.style.visibility = 'visible';
		
	} else {	
		showProgressive(); 
		showBox = true;
		document['reservation_form_0'].timesteps.style.visibility = 'hidden';
		
	}
}


function showCalendar(response) {

	calendarBox.innerHTML = response;
	
	
	if (!showBox) {
	
		var iconCalendar = document.getElementById("iconCalendar");

		XY = findPos(iconCalendar);
	
		finalWidth = calendarBox.offsetWidth;
		finalHeight = calendarBox.offsetHeight + 50; 
		
		
		ratio = finalWidth / finalHeight;
		X1 = XY[0] - finalWidth - 4;
		Y1 = XY[1] + iconCalendar.offsetHeight + 4;
		//calendarBox.style.position = 'relative';
		calendarBox.style.left = X1;
		calendarBox.style.top = Y1;
		calendarBox.style.zIndex = 100;
		
		//box.style.clip = "rect(0px," + finalWidth + "px,0px,0px)";
		clipHeight = 0;
		clipWidth = finalWidth;	
		toggleClip();
	}
	
}


function display_timesteps(response) {
	var id_timesteps = document.getElementById('id_timesteps');
	if (id_timesteps!=null) {
		id_timesteps.innerHTML = response;
	}
}


var handleSuccessC = function(o){
	if(o.responseText !== undefined){
		showCalendar(o.responseText);
	}
};

var handleFailureC = function(o){
	if(o.responseText !== undefined){
		alert("Connection Error");
	}
};

var handleSuccessT = function(o){
	if(o.responseText !== undefined){
		display_timesteps(o.responseText);
	}
};

var handleFailureT = function(o){
	if(o.responseText !== undefined){
		alert("Connection Error");
	}
};

var callbackC =
{
  success:handleSuccessC,
  failure:handleFailureC,
  argument:['foo','bar']
};

var callbackT =
{
  success:handleSuccessT,
  failure:handleFailureT,
  argument:['foo','bar']
};


function setForm(day,month,year,museum) {

	if (document['reservation_form_0']) {

		for (i=0; i < document['reservation_form_0'].year.length; i++) {
			if (document['reservation_form_0'].year[i].value == year) {
				document['reservation_form_0'].year.selectedIndex = i;
			}
		}
		for (i=0; i < document['reservation_form_0'].month.length; i++) {
			if (document['reservation_form_0'].month[i].value == month) {
				document['reservation_form_0'].month.selectedIndex = i;
			}
		}
		
		 document['reservation_form_0'].day.selectedIndex = day - 1; 
	}

	if (showBox) {
		toggleClip();
	}

	params = "idmuseum=" + encodeURIComponent(museum);
	params += "&year=" + encodeURIComponent(year);
	params += "&month=" + encodeURIComponent(month);
	params += "&day=" + encodeURIComponent(day);
	var request = YAHOO.util.Connect.asyncRequest('POST', sUrlTimesteps, callbackT, params);
	
}

function setTimesteps(museum) {

	if (document['reservation_form_0']) {
	
		year = document['reservation_form_0'].year[document['reservation_form_0'].year.selectedIndex].value;
		month = document['reservation_form_0'].month[document['reservation_form_0'].month.selectedIndex].value;
		day = document['reservation_form_0'].day[document['reservation_form_0'].day.selectedIndex].value;
	
		params = "idmuseum=" + encodeURIComponent(museum);
		params += "&year=" + encodeURIComponent(year);
		params += "&month=" + encodeURIComponent(month);
		params += "&day=" + encodeURIComponent(day);
		var request = YAHOO.util.Connect.asyncRequest('POST', sUrlTimesteps, callbackT, params);	

	}

}


function callCalendar(museum,when,lang,setform) {
	if (setform) {
		if (document['reservation_form_0']) {
			year = parseInt(when.substring(0,4));
			month = parseInt(when.substring(5,7));
			day = parseInt(when.substring(8));

			for (i=0; i < document['reservation_form_0'].year.length; i++) {
				if (document['reservation_form_0'].year[i].value == year) {
					document['reservation_form_0'].year.selectedIndex = i;
				}
			}
			for (i=0; i < document['reservation_form_0'].month.length; i++) {
				if (parseInt(document['reservation_form_0'].month[i].value) == month) {
					document['reservation_form_0'].month.selectedIndex = i;
				}
			}
			
						
			checkYear(document['reservation_form_0'].year, document['reservation_form_0'].month);
			
			// document['reservation_form_0'].day.selectedIndex = day - 1; 
			
		}		
	}
	params = "idmuseum=" + encodeURIComponent(museum);
	params += "&when=" + encodeURIComponent(when);
	params += "&lang=" + encodeURIComponent(lang);
	var request = YAHOO.util.Connect.asyncRequest('POST', sUrlC, callbackC, params);
}

function getCalendar(museum, form, toggle) {

	when = form.year.options[form.year.selectedIndex].value + '-' + zeroPad(form.month.options[form.month.selectedIndex].value,2) + '-' + zeroPad(form.day.options[form.day.selectedIndex].value,2);

	lang = form.lang.value;

	calendarBox = document.getElementById("calendarBox");
	
	if ((!toggle) && showBox) {
		callCalendar(museum,when,lang,false);
	} else if (toggle) { 
		if(!showBox) {
			callCalendar(museum,when,lang,false);
		} else {
			toggleClip();
		}
	}

}

