// identify the browser
var isIE = (navigator.userAgent.indexOf("MSIE") >= 0);
// compatibility tasks
var cssRules = new Array();
if (isIE) {
	cssRules = document.styleSheets[1].rules;
} else {
	cssRules = document.styleSheets[1].cssRules;
}

// Set/reset display property of css selectors. This can be used to show/hide elements.
// Parameters:
//   clsArray - Array of selectors.
//   index1, ..., index N - indices to show, others will be hidden
//
function showHideCSS(clsArray) {
	var showRules = "";
	var hideRules = "";
	for (i=1; i<showHide.arguments.length; i++) {
		showRules += clsArray[showHide.arguments[i]] + ",";
	}
	//alert("show: " + showRules);
	for (i=0; i<clsArray.length; i++) {
		if (showRules.indexOf(clsArray[i]) < 0) {
			hideRules += clsArray[i] + ",";
		}
	}
	//alert("hide: " + hideRules);
	//alert(cssRules.length);
	for (i=0; i<cssRules.length; i++) {
		//alert(cssRules[i].selectorText);
		if (showRules.indexOf(cssRules[i].selectorText) >= 0) {
			cssRules[i].style.display = "";
			if (cssRules[i].selectorText == "#ic_open") {
				Calendar.setup(
					{
						flat         : "calendar-container", // ID of the parent element
						flatCallback : dateChanged,          // our callback function
						cache        : false
					}
				);
			}
		} else if (hideRules.indexOf(cssRules[i].selectorText) >= 0) {
			if (cssRules[i].selectorText == "#ic_open") {
				var cc = document.getElementById("calendar-container");
				if (cc.firstChild) {
					cc.removeChild(cc.firstChild);
				}
			}
			cssRules[i].style.display = "none";
		}
	}
	//alert("done");
	//window.status = "done: " + showHide.arguments[1] + "," + showHide.arguments[2] + "," + showHide.arguments[3];
}

// Show/hide elements.
// Parameters:
//   idArray - Array of IDs.
//   index1, ..., index N - indices to show, others will be hidden
//
function showHide(idArray) {
	var showRules = "";
	var hideRules = "";
	for (i=1; i<showHide.arguments.length; i++) {
		showRules += idArray[showHide.arguments[i]] + ",";
		var obj = document.getElementById(idArray[showHide.arguments[i]]);
		if (obj) {
			obj.style.display = "";
			/*if (obj.id == "ic_open") {
				Calendar.setup(
					{
						flat         : "calendar-container", // ID of the parent element
						flatCallback : dateChanged,          // our callback function
						cache        : false
					}
				);
			}*/
		}
	}
	//alert("show: " + showRules);
	for (i=0; i<idArray.length; i++) {
		if (showRules.indexOf(idArray[i]) < 0) {
			hideRules += idArray[i] + ",";
			var obj = document.getElementById(idArray[i]);
			if (obj) {
				obj.style.display = "none";
				/*if (obj.id == "ic_open") {
					var cc = document.getElementById("calendar-container");
					if (cc.firstChild) {
						cc.removeChild(cc.firstChild);
					}
				}*/
			}
		}
	}
	//alert("hide: " + hideRules);
	//alert("done");
	//window.status = "done: " + showHide.arguments[1] + "," + showHide.arguments[2] + "," + showHide.arguments[3];
}

// show/hide selector arrays (should go in prog specific js)
var shProgrammeCSS = new Array(
	"#ic_closed",
	"#ic_open",
	"#sc_closed",
	"#sc_open",
	"#tc_closed",
	"#tc_open"
	);

// show/hide id arrays (should go in prog specific js)
var shProgramme = new Array(
	"ic_closed",
	"ic_open",
	"sc_closed",
	"sc_open",
	"tc_closed",
	"tc_open",
	"tc_open_fo" // this is a view of the form when flights only selected, not a separate form
	);

function dateChanged(calendar) {
  alert("date");
  // Beware that this function is called even if the end-user only
  // changed the month/year.  In order to determine if a date was
  // clicked you can use the dateClicked property of the calendar:
  if (calendar.dateClicked) {
    // OK, a date was clicked, redirect to /yyyy/mm/dd/index.php
    var y = calendar.date.getFullYear();
    var m = calendar.date.getMonth();     // integer, 0..11
    var d = calendar.date.getDate();      // integer, 1..31
    // redirect...
    window.location = "/" + y + "/" + m + "/" + d + "/index.php";
  }
};
      
function findXY(obj) {
  var x = 0, y = 0;
  while (obj) {
    x += obj.offsetLeft;
    y += obj.offsetTop;
    obj = obj.offsetParent;
  }
  return {x: x, y: y};
}

var heightDelta = 0;
var timerId = 0;

function trItinMove() {
  //var trYC = window.frames[0].document.getElementById("trYC");
  var trYC = window.document.getElementById("trYC");
  var trYCXY = findXY(trYC);
  //var tableYC = window.frames[0].document.getElementById("tableYC");
  var tableYC = window.document.getElementById("tableYC");
  var tableYCXY = findXY(tableYC);
  //window.status = 
  //  "heightDelta: " + heightDelta
  //  + " / "
  //  + "trYC.realTop: " + trYCXY.y
  //  + " / "
  //  + "tableYC.realTop: " + tableYCXY.y;
  if (heightDelta == 0) {
    clearInterval(timerId);
    return;
  }
  //  var imgItinTop = window.frames[0].document.getElementById("imgItinTop");
  var imgItinTop = window.document.getElementById("imgItinTop");
  if (heightDelta > 0) {
    if ((tableYCXY.y - trYCXY.y) < 52) {
      heightDelta = 0;
      clearInterval(timerId);
      return;
    }
    heightDelta -= 10;
    imgItinTop.height += 10;
  } else {
    heightDelta += 10;
    imgItinTop.height -= 10;
  }
}

function scrollItinBox(e) {
  if (heightDelta != 0) {
    clearInterval(timerId);
  }
  if (!e) e = event;
  //var imgItinTop = window.frames[0].document.getElementById("imgItinTop");
  var imgItinTop = window.document.getElementById("imgItinTop");
  if (!imgItinTop){ return; }
  var imgItinTopXY = findXY(imgItinTop);
  //var trItin = window.frames[0].document.getElementById("trItin");
  var trItin = window.document.getElementById("trItin");
  var trItinXY = findXY(trItin);
/*  window.status = 
      "document.body.scrollTop: " + document.body.scrollTop
    + " / "
    + "imgItinTop.height: " + imgItinTop.height
    + " / "
    + "trItin.realTop: " + trItinXY.y;*/
  // move down/up
  if (document.body.scrollTop > imgItinTopXY.y) {
    imgItinTopNewHight = document.body.scrollTop - imgItinTopXY.y;
  } else {
    imgItinTopNewHight = 10;
  }
  heightDelta = Math.round((imgItinTopNewHight - imgItinTop.height) / 10) * 10;
  //alert(heightDelta + "/" + imgItinTop.height + "/" + imgItinTopNewHight);
  if (heightDelta != 0) {
    timerId = setInterval(trItinMove, 1);
  }
}


