var siteInitialized     = false;
var jFocusButton        = null;
var focusCheckID        = null;
var jCurrentPageFrameDIV= null;

var MIN_PAGE_HEIGHT       = 375;
var BANNER_AND_NAV_HEIGHT = 162;

$(document).ready(
  function()
  {
    if(!siteInitialized)    
      initPage(false); 
  }
)
function initPage(nested,firstPageID)
{
  if(!isSet(firstPageID))
    firstPageID = '4bb3e087711a79_47079598'; //is prive eerste pagina
  
  if(!isSet(nested))
    nested = false;
  
  $(window).resize(
    function(e)
    {
      doResize();  
    }
  )
  doResize();
  
  jCurrentPageFrameDIV = $('div#oliphant_stub_page_div');
  $('div.oliphant_navbar_button_div').hover(
    function hoverOverNavBarButton()
    {
      var jThis = $(this);
      var md = jThis.metadata(); 
      
      var jSubMenu = $('#submenu_' + md.id); 
      if(jSubMenu.length>0)
      {
        displayMenu(jThis,jSubMenu,true,true);
      }       
      
      setFocusButton(jThis);
      jThis.addClass('oliphant_navbar_hover_div');
    },
    function hoverOutNavBarButton()
    {
      var jThis = $(this);
      loseFocusButton(jThis);
      
      jThis.removeClass('oliphant_navbar_hover_div');
    }
  ).click(
    function()
    {
      var jThis = $(this); 
      switch(jThis.attr('id'))
      {
        case 'button_back_to_home':
          location.href = '/';
          break;  
        default:
          {
            var md = jThis.metadata();
            if(subMenuExists(md.id))
            {
             // $(this).hover();
            }
            else
              goPage(md.id);
            //openPage(md.id);
            //alert('click the button: ' + md.id + ',' + $(this).contents());  
          }
          break;
      }
              
    }
  )
  
  $('div.oliphant_submenu_div').hide();
  
  $('.oliphant_submenu_button_div').hover(
    function()
    {
      var jThis = $(this);
      
      var md = jThis.metadata();
      
      var jSubMenu = $('#submenu_' + md.id); 
      if(jSubMenu.length>0)
      {
        displaySubMenu(jThis,jSubMenu,true);
      } 
      
      setFocusButton(jThis);
      jThis.addClass('oliphant_submenu_button_hover_div');
    },
    function()
    {
      var jThis = $(this);
      loseFocusButton(jThis);
      jThis.removeClass('oliphant_submenu_button_hover_div');
    }
  ).click(
    function()
    {
      var md = $(this).metadata();
      //openPage(md.id);
      if(subMenuExists(md.id))
      {
        //$(this).hover();
      }
      else
        goPage(md.id);
      //alert('click the button: ' + md.id + ',' + $(this).contents());        
    }
  )
  
  
  
  //jStartPhoto.hide();

  
  if(!nested)
  {
    var jStartPhoto = $('div.oliphant_main_photo_div');
    
    jStartPhoto.css({'top':'-' + jStartPhoto.height() + 'px'});
    jStartPhoto.animate({
        top: '20'
      }, 600,'easeOutSine', function() {
  //        alert('ready');
      });
    
    $('#oliphant_logo_div').animate({
      left: '30px'
    }, 2000,'easeOutSine', function() {
      // Animation complete.
    });
  }
  
  goPage(firstPageID,nested);
  
  addShadowsToImages($(document));
}
function subMenuExists(id)
{
  var jSubMenu = $('div#submenu_' + id);
  if(jSubMenu.length>0)
    return true;
  else
    return false;  
}

function doResize()
{
  var totalHeight = $(this).height();
  var totalWidth = $(this).width();
        
  var minPageHeight = (totalHeight-BANNER_AND_NAV_HEIGHT-1) +'px';
  
  $('div.oliphant_main_frame_div').css({'min-height':minPageHeight});
  
  $('div#oliphant_image_viewer_background_div').css({'height':totalHeight,'width':totalWidth});  
}
function showPhotoInViewer(imageId)
{
  $('div#oliphant_image_viewer_image_div').html(loadingHTML).load('load_image_for_viewer.php',{image_id:imageId},viewerImageLoaded);
}
function viewerImageLoaded()
{
  displayPhotoViewer(true);
  
  var jImage       = $('div#oliphant_image_viewer_image_div img');
  
  var imageWidth   = jImage.width();
  var imageHeight  = jImage.height();
	var imageLeft    = ($(window).width()-imageWidth)/2;
	var imageTop     = ($(window).height()-imageHeight)/2;
	
	$('div#oliphant_image_viewer_image_div').css({left:imageLeft,top:imageTop,width:imageWidth,height:imageHeight});
  $('div#oliphant_image_viewer_close_div').css({left:imageLeft+imageWidth-$('div#oliphant_image_viewer_close_div').width(),top:imageTop}).unbind('hover').hover(
    function(e)
    {
      $(this).addClass('oliphant_image_viewer_close_hover');      
    },
    function(e)
    {
      $(this).removeClass('oliphant_image_viewer_close_hover');      
    }
  );
  //alert('image loaded');
}
function displayPhotoViewer(state)
{
  var visState = '';
  if(state)
    visState = 'visible';
  else
    visState = 'hidden';
  
  $('div#oliphant_image_viewer_background_div').css({'visibility':visState}).unbind('click').click(
    function(e)
    {
      displayPhotoViewer(false);      
    }
  );
}
function displayMenu(jParent,jMenu,navbar,alterPosition)
{
  if(!isSet(navbar))
    navbar = true;
  
  if(!isSet(alterPosition))
    alterPosition = false;
  
  if(alterPosition)
  {
    //alert('called from parent with id: ' + jParent.metadata().id);
    var headerOffset = $('div#oliphant_navbar_div').position();
    var parentOffset = null;
    var parentMenuOffset = null;
    
    if(navbar)
    {
      parentOffset = jParent.position();
      jMenu.css({left:parentOffset.left,top:headerOffset.top + parentOffset.top+20});
    }
    else
    {
      var jParentMenu = jParent.closest('div.oliphant_submenu_div');
      parentMenuOffset = jParentMenu.position();
      parentOffset     = jParent.position();
      //alert('parent: ' + parentOffset.left + ',' + parentOffset.top);
      jMenu.css({left:parentMenuOffset.left+jParent.width(),top:parentMenuOffset.top+parentOffset.top});
    }
  }
  jMenu.show();
}
function displaySubMenu(jParent,jSubMenu,alterPosition)
{
  displayMenu(jParent,jSubMenu,false,alterPosition);
}
function hideSubMenus()
{
  $('div.oliphant_submenu_div').hide();
}

function setFocusButton(jButton)
{
  if(focusCheckID!=null)
    clearTimeout(focusCheckID);
    
  if(jButton==jFocusButton)
    return;
    
  if(jFocusButton!=null)
  {
    //alert('focusButton was nog niet leeg ' + jFocusButton.metadata().id);    
  }
  
  jFocusButton = jButton;
  
  checkFocus();
}
function loseFocusButton(jButton)
{
  if(jFocusButton.metadata().id==jButton.metadata().id)
  {
    jFocusButton = null;
  }
  
  focusCheckID = setTimeout('checkFocus()',1000);
}
function checkFocus()
{
  hideSubMenus();
  
  if(jFocusButton!=null)
  {
    var md = '';
    
    //displayMenu(jFocusButton,$('div#submenu_' + md.id));
    
    var tempParentID = '';

    var jTempButton  = jFocusButton;
    var jParentMenu = null; 
    do {
      md = jTempButton.metadata();
      
      displayMenu(jTempButton,$('div#submenu_' + md.id),jTempButton.hasClass('oliphant_navbar_button_div'),false);
      
      jParentMenu = jTempButton.closest('div.oliphant_submenu_div');
      if(jParentMenu.length>0)
      {
        tempParentID = jParentMenu.metadata().super_id;
        jTempButton = $('div#button_' + tempParentID);
      }        
    } while(jParentMenu.length>0 && jTempButton.length>0)
    
    //jFocusButton.parents('div.oliphant_submenu_div').add('#submenu_' + md.id).show();
  }
  
  focusCheckID = null;
}
function goPage(id,instant)
{
  window.open('pageloader.php?page_id=' + id + '&instant=' + (instant?'1':'0'),'oliphant_loader_iframe');  
}
function openPage(id,instant)
{
  //instant should only be used on the first one
  if(!isSet(instant))
    instant = 0;
    
  hideSubMenus();
  
  //page method
  var pageName = 'oliphant_page_' + id;
  var jPage = $('div#' + pageName);
  if(jPage.length==0)
  {
    //page method
    $('div.oliphant_main_frame_div').append('<div id="' + pageName + '" class="oliphant_main_frame_content_div"></div>'); 
    
    //page method
    var jPage = $('div#' + pageName);
    jPage.hide();
    
    
    jPage.load('pagecontent.php',{'superId':id},
      function()
      {
        //page method
        
        var jThis = $(this);
               
        jThis.show();
        addShadowsToImages(jThis);
        jThis.hide();
        
        showPage(id,pageName,instant); 
        
        jThis.find('div.oliphant_side_photo_div').click(
          function(e)
          {
            var md = $(this).metadata();
            
            showPhotoInViewer(md.id);
          }
        );
        
        if(instant)
        {
          moveAwayFrontPageStuff();
        }
      }
    );
  }
  else
  {
    //direct animeren
    //frame method
    //showPage(id,pageFrameName);
    
    //page method
    showPage(id,pageName,instant);      
  }
  
  //alert('of moeten de pagina\'s vanuit javascript gebouwd worden?');
  //$('div.oliphant_main_frame_content_div').html(loadingHTML).load('pagecontent.php',{'superId':id},pageLoadReady);  
}
function showPage(id,pageFrameName,instant)
{
  if(!isSet(instant))
    instant = 0;
  
  var jPageFrame = $('div#' + pageFrameName);
  //alert(pageFrameName +',' + jCurrentPageFrameDIV.attr('id') + ',' +jPageFrame.attr('id')); 
  if(jCurrentPageFrameDIV.attr('id')==jPageFrame.attr('id'))
    return;
  
  
  currentPageHeight = jCurrentPageFrameDIV.height();
  if(currentPageHeight<MIN_PAGE_HEIGHT)
    currentPageHeight = MIN_PAGE_HEIGHT;
  
  if(instant)
  {
    jCurrentPageFrameDIV.hide();
    jCurrentPageFrameDIV = jPageFrame;
    jCurrentPageFrameDIV.show();
    jPageFrame.css({top:'20px'});
  }
  else
  {
    jCurrentPageFrameDIV.animate({
        top: '-=' + currentPageHeight
      }, 300,'easeInSine', function() {
        // Animation complete.
        $(this).hide();
        
        jCurrentPageFrameDIV = jPageFrame;
        
        var pageHeight = jPageFrame.height();
        if(pageHeight<MIN_PAGE_HEIGHT)
          pageHeight = MIN_PAGE_HEIGHT;
        
        jPageFrame.show();
        jPageFrame.css({top:'-'+pageHeight+'px'});
        
        
        jPageFrame.animate({
          top: '20'
        }, 300,'easeOutSine', function() {
  //        alert('ready');
        });
      });
  }
}

function addShadowsToImages(jCollection)
{
  var jImages = jCollection.find('div.oliphant_main_photo_div,div.oliphant_side_photo_div,div.oliphant_inline_image_div');
 
  jImages.map(addShadowsToImageDIV);
}
function addShadowsToImageDIV(index,element)
{
  var jElement = $(element);
  var refPos = jElement.find('div').position();
  //alert('refpos: ' + refPos.left + ',' + refPos.top);
  
  var START_ELEMENT_WIDTH   = 9;
  var START_ELEMENT_HEIGHT  = 9;

  //horizontal shadow (including start)
  jElement.append('<div class="oliphant_image_shadow_h_start" style="left:' + refPos.left + 'px;top:' + (refPos.top+jElement.height()) + 'px"></div>');
  jElement.append('<div class="oliphant_image_shadow_h" style="width:' + (jElement.width()-START_ELEMENT_WIDTH) + 'px;top:' + (refPos.top +jElement.height()) + 'px;left:' + (refPos.left+START_ELEMENT_WIDTH) + 'px;"></div>');
  
  //vertical shadow (including start)
  jElement.append('<div class="oliphant_image_shadow_v_start" style="top:' + refPos.top + 'px;left:' + (refPos.left+jElement.width()) + 'px"></div>');
  jElement.append('<div class="oliphant_image_shadow_v" style="height:' + (jElement.height()-START_ELEMENT_HEIGHT) + 'px;left:' + (refPos.left +jElement.width()) + 'px;top:' + (START_ELEMENT_HEIGHT+refPos.top) + 'px;"></div>');
  
  //bottom right corner shadow
  jElement.append('<div class="oliphant_image_shadow_corner_outside" style="top:' + (refPos.top +jElement.height()) + 'px;left:' + (refPos.left +jElement.width()) + 'px"></div>');
  
  //also put image centered in its container
  
  var jChild = jElement.find('img');
   
  if(jChild.width()>jElement.width())
    jChild.css({left:((jElement.width()-jChild.width())/2) + "px"});
    
  if(jChild.height()>jElement.height())
    jChild.css({top:((jElement.height()-jChild.height())/2) + "px"});
    
  
}