var ibLightBox = {
  
  timmerId : 0,
  loadingFrame : 0,
  targetX : 0,
  targetY : 0,
  
  
  init: function()
  {
    $$('a.lightbox').each(function(s){ s.onclick = function () { ibLightBox.showLightBox(this);return false; }});
    $$('a.modalbox').each(function(s){ s.onclick = function () { ibLightBox.showModalBox(this);}});
  },

  showModalBox: function (element, width, height,content)
  {
    try{
      element = $(element);
    }catch(e) {
      return;
    }
    try{
      ibLightBox.element = element = $(element);
    }catch(e) {
      return;
    }
    
    // prep objects
    var objLink = element;
  	var objOverlay = $('ibOverlay');
  	var objLightbox = $('fancy_content');
  	var objCaption = $('fancy_title');
  	var objImage = document.getElementById('fancy_img');
  	var objLoadingImage = document.getElementById('fancy_loading');
    
  	var queue = Effect.Queues.get('boxscope');
    queue.each(function(effect) { effect.cancel(); });

    $('fancy_bg').classNames().each(function(s){$('fancy_bg').removeClassName(s)});
    $('fancy_bg').addClassName(content);
    $$('#fancy_inner_content .lightToggle').each(function(s){s.hide()});
    $(content).show();
  	
    Effect.Appear('ibOverlay',{to:0.5,duration: 0.5});
  		
    objLightbox.show();
    $('box').show();
    $('box').setStyle('opacity:1');
    $('fancy_close').show();
  
    $('fancy_outer').setStyle({
                 top: '0px',
                 left: '0px',
                 width: element.getWidth()+10+'px', 
                 height: element.getHeight()+10+'px' 
    });
	    
    $('fancy_content').setStyle({width: width+95 + 'px', height: height+70 + 'px'});
	    
    var arrayPosition = ibLightBox.center('fancy_content');

    ibLightBox.targetX = element.cumulativeOffset().left - arrayPosition[0] - 20;
    ibLightBox.targetY = element.cumulativeOffset().top - arrayPosition[1] - 10;
	    
    $('fancy_content').setStyle({left: arrayPosition[0] + 'px', top: arrayPosition[1] + 'px'});
    $('fancy_outer').setStyle({left: ibLightBox.targetX + 'px', top: ibLightBox.targetY + 'px'});
	    
    new Effect.Morph('fancy_outer',{
          style: {
              width: width+55 + 'px', 
              height: height+70 + 'px',
              top: '0px',
              left: '0px'
          },
          transition: Effect.Transitions.sinoidal,
          duration: 0.3
        });
      
  		
		if(objLink.getAttribute('title')){
			objCaption.style.display = 'block';
			objCaption.down('#fancy_title_main div').update(objLink.getAttribute('title'));
		} else {
			objCaption.style.display = 'none';
		}
  		
		// A small pause between the image loading and displaying is required with IE,
		// this prevents the previous image displaying for a short burst causing flicker.
		if (navigator.appVersion.indexOf("MSIE")!=-1){
			ibLightBox.pause(250);
		}
   
    return false;
  },
  
  showLightBox: function (element)
  {
    
    try{
      ibLightBox.element = element = $(element);
    }catch(e) {
      return false;
    }
    
    // prep objects
    var objLink = element;
  	var objOverlay = $('ibOverlay');
  	var objLightbox = $('fancy_content');
  	var objCaption = $('fancy_title');
  	var objImage = $('fancy_img');
  	var objLoadingImage = $('fancy_loading');
  
  	// center loadingImage if it exists
  	if (objLoadingImage) {
  	  var arrayPosition = ibLightBox.center(objLoadingImage);
  	  objLoadingImage.setStyle({left:arrayPosition[0]+'px',top:arrayPosition[1]+'px',display:'block'});
  	  
  	  clearInterval(ibLightBox.loadingTimer);
  		ibLightBox.loadingTimer = setInterval(ibLightBox.animateLoading, 66);
  	}
    
    Effect.Appear('ibOverlay',{to:0.5,duration: 0.5});
 
  	// preload image
  	imgPreload = new Image();
  
  	imgPreload.onload=function()
  	{
  	  var isIE = false;
  	  if (parseInt(navigator.userAgent.substring(navigator.userAgent.indexOf("MSIE")+5))==6) isIE = true;
  	  
    		objImage.src = imgPreload.src;

    		var width = imgPreload.width;
        var height = imgPreload.height;
        
        if(isIE) { objImage.setStyle({width: '95%'}); height += 25; }
        
        objLightbox.show();
        $$('#fancy_inner_content .lightToggle').each(function(s){s.hide()});
        $('lightImg').show();
    		
        $('fancy_close').show(); 
        $('fancy_outer').setStyle({
                     top: '0px',
                     left: '0px',
                     width: element.getWidth()+10+'px',
                     height: element.getHeight()+10+'px' 
        });
        $('fancy_content').setStyle({top: '0px', left: '0px', width: width + 95 + 'px', height: height + 70 + 'px'});
        
        var arrayPosition = ibLightBox.center('fancy_content');
        arrayPosition[0] = (arrayPosition[2] - width - 95) / 2;
        arrayPosition[1] = (arrayPosition[3] - height - 70) / 2 + arrayPosition[4];
        
        ibLightBox.targetX = element.cumulativeOffset().left - arrayPosition[0] - 20;
        ibLightBox.targetY = element.cumulativeOffset().top - arrayPosition[1] - 10;
        $('fancy_content').setStyle({left: arrayPosition[0] + 'px', top: arrayPosition[1] + 'px'});
        $('fancy_outer').setStyle({left: ibLightBox.targetX + 'px', top: ibLightBox.targetY + 'px'});

        new Effect.Morph('fancy_outer',{
              style: {
                  width: width+55 + 'px', 
                  height: height+95 + 'px',
                  top: '0px',
                  left: '0px'
              },
              transition: Effect.Transitions.sinoidal,
              duration: 0.3
            });
          
    		    		
    		if(objLink.getAttribute('title')){
    			objCaption.show();
    			objCaption.down('#fancy_title_main div').update(objLink.getAttribute('title'));
    		} else {
    			objCaption.hide();
    		}
    
    		if (objLoadingImage) { objLoadingImage.hide() }
    
    		// Hide select boxes as they will 'peek' through the image in IE
    		$$("select").each(function(s){s.setStyle({visibility:'hidden' })});
    
    		return false;
    	}
    
    	imgPreload.src = element.href;
      
    	$('box').show();
    	$('box').setStyle('opacity:1');
  
      return false;
    },
    
  pause: function(numberMillis) {
  	var now = new Date();
  	var exitTime = now.getTime() + numberMillis;
  	while (true) {
  		now = new Date();
  		if (now.getTime() > exitTime)
  			return;
  	}
  },
  
  animateLoading: function() {
		if (!$('fancy_loading').visible())
		{
			clearInterval(ibLightBox.loadingTimer);
			return;
		}

		$('fancy_loading').down('div').setStyle({'top': (ibLightBox.loadingFrame * -40) + 'px'});

		ibLightBox.loadingFrame = (ibLightBox.loadingFrame + 1) % 12;
  },

  hideBox: function ()
  {
    $('fancy_close').hide();
    
    new Effect.Morph('fancy_outer',{
              style: {
                  width: ibLightBox.element.getWidth()+'px', 
                  height: ibLightBox.element.getHeight()+'px',
                  top: ibLightBox.targetY+'px',
                  left: ibLightBox.targetX+'px'
              },
              transition: Effect.Transitions.sinoidal,
              duration: 0.3
            });
    new Effect.Fade('box',{
        duration: 0.3,
        queue: { position: 'end', scope: 'boxscope' }
    });
    
    $('ibOverlay').hide();
    
    return false;
  },

  center: function (element){
    try{
      element = $(element);
    }catch(e){
      return;
    }

    var my_width  = 0;
    var my_height = 0;

    if ( typeof( window.innerWidth ) == 'number' ){
      my_width  = window.innerWidth;
      my_height = window.innerHeight;
    }else if ( document.documentElement &&
    ( document.documentElement.clientWidth ||
    document.documentElement.clientHeight ) ){
      my_width  = document.documentElement.clientWidth;
      my_height = document.documentElement.clientHeight;
    }
    else if ( document.body &&
    ( document.body.clientWidth || document.body.clientHeight ) ){
      my_width  = document.body.clientWidth;
      my_height = document.body.clientHeight;
    }

    //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 = ( my_width  - elementDimensions.width  ) / 2;
    var setY = ( my_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.style.display  = 'block';
    
    return [setX,setY,my_width, my_height, scrollY];
    
  }
}

document.observe("dom:loaded", ibLightBox.init);
