// lightbox.js
// Version: 1.1.1.1

var Lightbox = {
	lightboxType : null,
	lightboxCurrentContentID : null,
	
	showBoxString : function(content, boxWidth, boxHeight){
    this.setLightboxDimensions(boxWidth, boxHeight);
		this.lightboxType = 'string';
		var contents = $('boxContents');
		contents.innerHTML = content;
		this.showBox();
		return false;
	},


	showBoxImage : function(href) {
		this.lightboxType = 'image';
		var contents = $('boxContents');
		var objImage = document.createElement("img");
		objImage.setAttribute('id','lightboxImage');
		contents.appendChild(objImage);
		imgPreload = new Image();
		imgPreload.onload=function(){
			objImage.src = href;
			Lightbox.showBox();
		}
		imgPreload.src = href;
		return false;
	},

	showBoxByID : function(id, boxWidth, boxHeight) {
		this.lightboxType = 'id';
		this.lightboxCurrentContentID = id;
		this.setLightboxDimensions(boxWidth, boxHeight);
		var element = $(id);
		var contents = $('boxContents');
		contents.appendChild(element);
		Element.show(id);
		this.showBox();
		return false;
	},

	showBoxByAJAX : function(href, boxWidth, boxHeight) {
		this.lightboxType = 'ajax';
		this.setLightboxDimensions(boxWidth, boxHeight);
		var contents = $('boxContents');
    var tmp = ajaxCM("getHTML", href);
		contents.innerHTML = tmp;
		this.showBox();
		return false;
	},
	
	setLightboxDimensions : function(width, height) {
		var windowSize = this.getPageDimensions();
		if(width) {
			if(width < windowSize[0]) {
				$('box').style.width = width + 'px';
			} else {
				$('box').style.width = (windowSize[0] - 50) + 'px';
			}
		}
		if(height) {
			if(height < windowSize[1]) {
				$('box').style.height = height + 'px';
			} else {
				$('box').style.height = (windowSize[1] - 50) + 'px';
			}
		}
	},


	showBox : function() {
    $('overlay').appear({duration: 0.5});
		this.center('box');
		new Draggable('box', {scroll: window,  handle: $$('ul.verticalMenu h1, div.lightboxMenu')[0] });
		return false;
	},
	
	
	hideBox : function(reloadContent) {		
    // if a text editor is opened so remove control of tiny mce
    if (Object.isElement($('textEditor')))
      tinyMCE.execCommand('mceRemoveControl', false, 'textEditor');
      
    // if observer for observing date changes is running
    if (window.observer != null) {
      window.observer.stop();
      window.observer = null;
    }
    
    var contents = $('boxContents');
		if(this.lightboxType == 'id') {
			var body = document.getElementsByTagName("body").item(0);
			Element.hide(this.lightboxCurrentContentID);
			body.appendChild($(this.lightboxCurrentContentID));
		}
		contents.innerHTML = '';
		$('box').style.width = null;
		$('box').style.height = null;
		Element.hide('box');
		Element.hide('overlay');
		
		if (reloadContent && Object.isElement($('content')))
      Content.reload();
		
		return false;
	},
	
	// taken from lightbox js, modified argument return order
	getPageDimensions : function(){
		var xScroll, yScroll;
	
		if (window.innerHeight && window.scrollMaxY) {	
			xScroll = document.body.scrollWidth;
			yScroll = window.innerHeight + window.scrollMaxY;
		} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
			xScroll = document.body.scrollWidth;
			yScroll = document.body.scrollHeight;
		} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
			xScroll = document.body.offsetWidth;
			yScroll = document.body.offsetHeight;
		}
		
		var windowWidth, windowHeight;
		if (self.innerHeight) {	// all except Explorer
			windowWidth = self.innerWidth;
			windowHeight = self.innerHeight;
		} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
			windowWidth = document.documentElement.clientWidth;
			windowHeight = document.documentElement.clientHeight;
		} else if (document.body) { // other Explorers
			windowWidth = document.body.clientWidth;
			windowHeight = document.body.clientHeight;
		}	
		
		// for small pages with total height less then height of the viewport
		if(yScroll < windowHeight){
			pageHeight = windowHeight;
		} else { 
			pageHeight = yScroll;
		}
	
		// for small pages with total width less then width of the viewport
		if(xScroll < windowWidth){	
			pageWidth = windowWidth;
		} else {
			pageWidth = xScroll;
		}
		arrayPageSize = new Array(windowWidth,windowHeight,pageWidth,pageHeight) 
		return arrayPageSize;
	},
	
	center : function(element){
		try{
			element = document.getElementById(element);
		}catch(e){
			return;
		}
		var windowSize = this.getPageDimensions();
		var window_width  = windowSize[0];
		var window_height = windowSize[1];
		
		$('overlay').style.height = windowSize[3] + 'px';
		
		element.style.position = 'absolute';
		element.style.zIndex   = 99;
	
		var scrollY = 0;
	
		if ( document.documentElement && document.documentElement.scrollTop ){
			scrollY = document.documentElement.scrollTop;
		}else if ( document.body && document.body.scrollTop ){
			scrollY = document.body.scrollTop;
		}else if ( window.pageYOffset ){
			scrollY = window.pageYOffset;
		}else if ( window.scrollY ){
			scrollY = window.scrollY;
		}
	
		var elementDimensions = Element.getDimensions(element);
		var setX = ( window_width  - elementDimensions.width  ) / 2;
		var setY = ( window_height - elementDimensions.height ) / 2 + scrollY;
	
		setX = ( setX < 0 ) ? 0 : setX;
		setY = ( setY < 0 ) ? 0 : setY;
	
		element.style.left = setX + "px";
		element.style.top  = setY + "px";
		Element.show(element);
	},
	
	updateContent: function(content) {
    var contents = $('boxContents');
		contents.innerHTML = content;
    new Draggable('box', {scroll: window,  handle: $$('ul.verticalMenu h1, div.lightboxMenu')[0] });    
  },
  
  updateContent_ajax: function(element, options) {
    var url = "element=" + element;
    switch (element) {
      case 'settings':
        url += "&setting=" + options.setting;
        if (options.setting == "userForm")
          url += "&userId=" + options.userId;
        break;
      case 'editModule':
        url += "&moduleType=" + options.moduleType + "&moduleId=" + options.moduleId;
        break;
    }
    var contents = $('boxContents');
    var tmp = ajaxCM("getHTML", url);
		contents.innerHTML = tmp;
    
    new Draggable('box', {scroll: window,  handle: $$('ul.verticalMenu h1, div.lightboxMenu')[0] });
    
    if (element == "settings" && options.setting == "settings_start")
      getBNNews();    
  },
	
	init : function() {
		var lightboxtext = '<div id="overlay" style="display:none"></div>';
		lightboxtext += '<div id="box" style="display:none">';
    lightboxtext += '<img id="close" src="' + urlBase + 'img/icons/cross.png" onClick="Lightbox.hideBox()" alt="Close" title="Close this window" />';
		lightboxtext += '<div id="errorBox"></div>';
    lightboxtext += '<div id="boxContents">';
		lightboxtext += '</div>';
    lightboxtext += '</div>';
		var body = document.getElementsByTagName("body").item(0);
		new Insertion.Bottom(body, lightboxtext);
	}
}

////////////////////////////////////////////
// Lightbox
/////////////////////////////////////////////
function showLightbox(key, options) {
  var height = 550;
  //var width = 455;
  var width = null;
  
  var url = "element=" + key;
  
  switch(key) {
    case 'settings':
      //height = 700;
      width = 950;
      url += "&setting=" + options.setting;
      break;
    case 'newModule':
    case 'editModule':
      switch (options.moduleType) {
        case "imagegallery":
          height = 600;
          width = 700;
          break;
        case "news":
          height = 600;
          width = 700;                     
          break;
        case "people":
          height = 600;
          width = 950;
          break;
        case 'mailchimp':
          height = 300;
          width = 550;
          break;
        case 'twitter':
        	height = null;
        	width = 505;
        	break;
				case 'video':
        	height = null;
        	width = 505;
        	break;
        default:
          height = 600;
          width = 900;
          break;
      }
      url += "&moduleId=" + options.moduleId + "&moduleType=" + options.moduleType;
      if (options.sidebar == 1)
        url += "&sidebar=1";
      else
        url += "&sidebar=0";
      break;
    case 'systemupdate':
      height = 330;
      width = 455;
      break;
    case 'pageinfo':
      url += "&pid=" + options.pid;
      break;
    case 'wizardPage_step1':
      width = 450;
      height = 550;
      url += "&parentPageId=" + options.parentPageId + "&menu=" + options.menu;
      break;
    case 'wizardTeaser_step2':
      width = null; //600
      height = 550;
      url += "&teaserId=" + options.teaserId;
      break; 
    case "switchTemplate":
      url += "&pageId=" + options.pid;
      break;
    case "peopleForm":
      url += "&moduleId=" + options.moduleId + "&pupilId=" + options.pupilId;
      break;
    case "marquee":
      width = 500;
      height = 200;
      break;
    case "countdown":
      width = 480;
      height = 300;
      break;
    case 'event':
      if (options.eventId)
        url += "&eventId=" + options.eventId;
      break;
    default:
      break;
  }
  
  Lightbox.showBoxByAJAX(url, width, height);
  
  switch(key) {
    case 'editModule':
    case 'newModule':
      if (Object.isElement($("textEditor")))
        tinyMCE.execCommand('mceAddControl', false, 'textEditor');      
      switch (options.moduleType) {
        case 'news':
          window.observer = new PeriodicalExecuter(function() {$('newsDate').innerHTML = $('newsDateInput').value; }, 1);
          break;
      }
      break;
    case 'settings':
      if (options.setting == "settings_start")
        getBNNews();
      break;
    case 'countdown':
      window.observer = new PeriodicalExecuter(function() {$('countdownDate').innerHTML = $('countdownDateInput').value; }, 1);
      break;
    case 'wizardTeaser_step2':
      if (Object.isElement($("textEditor")))
        tinyMCE.execCommand('mceAddControl', false, 'textEditor');
      break;
    default:
      break;
  }
}
