// JavaScript Document
var mousex = 0;
var mousey = 0;

var PageModeType = { STANDARD:0, EXTENDED:1, GALLERY:2, SLIDESHOW:3 };
var ThumbsScrollDirection = { HORIZONTAL:0, VERTICAL:1 };

var AUTOEXPAND="none";
var isHiddenInfo = false;
var PAGEMODE;

jQuery(document).ready(function() {
  $("a.zoom").fancybox({
	overlayOpacity: 0.7,
	overlayColor: '#000'
  });
			
  $("#inline").fancybox({
    'titlePosition'		: 'inside',
    'transitionIn'		: 'none',
    'transitionOut'		: 'none'
  });
    
  $("#guestbook").click(function() {
	$.fancybox([
		'../guestbook/guestbook_1.jpg',
		'../guestbook/guestbook_2.jpg',
		'../guestbook/guestbook_3.jpg',
		'../guestbook/guestbook_4.jpg',
		'../guestbook/guestbook_5b.jpg',
		'../guestbook/guestbook_6b.jpg',
		'../guestbook/guestbook_7.jpg',
		'../guestbook/guestbook_8.jpg',
		'../guestbook/guestbook_9.jpg',
		'../guestbook/guestbook_10.jpg'
	], {
		'padding'			: 0,
		'transitionIn'		: 'fade',
		'transitionOut'		: 'fade',
		'type'              : 'image',
		'changeFade'        : 0
	});
  });    
});


/**
 *
 */
function dtOnLoad(pageMode) {
  initDHTMLAPI();
  dtResetElements(pageMode);
  
  if( AUTOEXPAND != 'none' )
    display( AUTOEXPAND);
}

/**
 *
 */
function dtOnResize(pageMode) {
  //dtRestrictWindowSize();
  dtResetElements(pageMode);
}

/**
 *
 */
function dtRestrictWindowSize() {
  winWidth = getInsideWindowWidth();
  winHeight = getInsideWindowHeight();
  
  document.getElementById("infoText").innerHTML=winWidth+"x"+winHeight;
  
  resize = false;
  
  if( winWidth < 1024 ) {
    winWidth = 1024;
	resize = true;
  }
  
  if( winHeight < 600 ) {
    winHeight = 600;
	resize = true;
  }
  
  if( resize)
    window.resizeTo( winWidth, winHeight );
}

/**
 *
 */
function dtResetElements(pageMode) {
  dtResizeImageToWin('mainImgLayer','mainImage',pageMode);
  
  toUp('layerGalleryThumbsContainer');
  toLeft('layerThumbsContainer');
  dtHideLayers();
  dtCenterThem(pageMode);  
  dtShowLayers(pageMode);
}

/**
 *
 */
function dtToggleInfo( lang ) {
		
  if( findPosX('menuLayer') == 0 ) {
    isHiddenInfo = true;
    dtHideInfo( lang );
  }
  else {
    isHiddenInfo = false;
	dtCenterThem(PAGEMODE);
    dtShowInfo( lang );
  }
}

/**
 *
 */
function dtHideInfo(lang) {
  shiftTo( 'menuLayer', -210, 0 );
  shiftTo( 'layerThumbsContainer', 36, getInsideWindowHeight()-19-65 );
  hide('contentLayer');
  hide('layerScrollContent');
  hide('layerScrollHolder');
  //hide('layerThumbsContainer');
  hide('layerLang');
    
  if( lang == 'en' )
    document.getElementById("btn_hide_info").src="assets/templates/ostraco/images/btn_show_info.gif";
  else if( lang == 'el' )
	document.getElementById("btn_hide_info").src="assets/templates/ostraco/images/btn_show_info_el.gif";

  
  if( countChildElements('layerThumbs','table') > 0 )
    document.getElementById("layerThumbsContainer").style.backgroundColor="#79715c";
}

/**
 *
 */
function dtShowInfo( lang ) {
  shiftTo( 'menuLayer', 0, 0 );
  show('Navigation');
  show('contentLayer');
  show('layerScrollContent');
  show('layerScrollHolder');
  show('layerThumbsContainer');
  show('layerLang');
  
    if( lang == 'en' )
    document.getElementById("btn_hide_info").src="assets/templates/ostraco/images/btn_hide_info.gif";
  else if( lang == 'el' )
	document.getElementById("btn_hide_info").src="assets/templates/ostraco/images/btn_hide_info_el.gif";

  document.getElementById("layerThumbsContainer").style.backgroundColor="";
}

/**
 *
 */
function dtResizeImageToWin( layer, img, pageMode ) {
  var winWidth = getInsideWindowWidth();
  var winHeight = getInsideWindowHeight();
	
  var imgWidth = 995;
  var imgHeight = 562;
  
  if( pageMode == PageModeType.GALLERY || pageMode == PageModeType.SLIDESHOW ) {
	imgWidth = 769;
    winWidth = winWidth - 226;
	shiftTo( layer, 226, 0 );
  }
  
  imgRatio = imgWidth/imgHeight;
  winRatio = winWidth/winHeight;
  
  var w, h, foo;
  
  if( imgRatio > winRatio ) {
	foo=1;
    h = winHeight;
    w = h*imgWidth/imgHeight;
  }
  else {
	foo=2;
    w = winWidth;
	h = w*imgHeight/imgWidth;
  }
  
  //document.getElementById("infoText").innerHTML="imgRatio="+imgRatio+", winRatio="+winRatio+", winWidth="+winWidth+", winHeight="+winHeight+", w="+w+", h="+h+", foo="+foo;
  
  imgStyle = document.getElementById( img ).style;
  layerStyle = document.getElementById( layer ).style;
  
  imgStyle.width= w+'px';
  imgStyle.height= h+'px';
  layerStyle.width= winWidth+'px';
  layerStyle.height= winHeight+'px';

  if( pageMode == PageModeType.SLIDESHOW ) {
  }
}

/**
 *
 */
function dtHideLayers() {
  hide('mainImgLayer');
  hide('menuLayer');
  hide('Navigation');
  hide('contentLayer');
  hide('layerScrollContent');
  hide('layerScrollHolder');
  hide('layerThumbsContainer');
  hide('layerGalleryThumbsContainer');
  hide('layerLang');
}

/**
 *
 */
function dtShowLayers(pageMode) {
  show('mainImgLayer');
  show('menuLayer');
  
  if( ! isHiddenInfo ) {
	show('layerLang');
	
	if( pageMode == PageModeType.STANDARD ) {
	  show('layerThumbsContainer');
	}
	
	if( pageMode == PageModeType.STANDARD || pageMode == PageModeType.EXTENDED || pageMode == PageModeType.SLIDESHOW ) {
	  show('Navigation');
	}
	
	if( pageMode == PageModeType.STANDARD || pageMode == PageModeType.EXTENDED ) {
	  show('contentLayer');
	  show('layerScrollContent');
	  show('layerScrollHolder');
	}
	
	if( pageMode == PageModeType.GALLERY )  {
	  show('layerGalleryThumbsContainer');
	}
  }
  else
    show('layerThumbsContainer');
}

/**
 *
 */
function dtCenterThem(pageMode) {
  
  $('#layerScrollContent').jScrollPaneRemove();
   
  var winWidth = getInsideWindowWidth();
  var winHeight = getInsideWindowHeight();
  var contentOffsetWidth = 266;
  var contentOffsetX = 246;
  var contentOffsetY = 199;
  
  if( pageMode == PageModeType.EXTENDED ) {
    //contentOffsetWidth = 400;
	//contentOffsetX = 350;
	contentOffsetY = winHeight - 20;
	
	contentHeight = (winHeight-2*20);
	
	document.getElementById("contentLayer").style.height=contentHeight+'px';
    document.getElementById("layerScrollHolder").style.height=(contentHeight-100)+'px';
    document.getElementById("layerScrollContent").style.height=(contentHeight-100)+'px';
  }

  var contentWidth = winWidth - contentOffsetWidth;
  var textWidth = contentWidth - 30;
  var contentTop = winHeight - contentOffsetY;
    
  shiftTo( 'contentLayer', contentOffsetX, contentTop );
  shiftTo( 'layerScrollHolder', contentOffsetX+18, contentTop + 71 );
  shiftTo( 'layerLang', 226-35-57, winHeight-19-10 );

  if( isHiddenInfo ) {
    shiftTo( 'layerThumbsContainer', 36, winHeight-19-65 );
}
  else {
    shiftTo( 'layerThumbsContainer', contentOffsetX+contentWidth-235-15, contentTop+5 );
  }
  
  document.getElementById("contentLayer").style.width=contentWidth+'px';
  document.getElementById("menuLayer").style.height=winHeight+'px';
  document.getElementById("cellHideInfo").style.height=winHeight+'px';
  document.getElementById("layerScrollContent").style.width=textWidth+'px';
  document.getElementById("layerScrollHolder").style.width=textWidth+'px';
  document.getElementById("layerGalleryThumbsContainer").style.height=(winHeight-150-19-30)+'px';
	  
  $('#layerScrollContent').jScrollPane({showArrows:true,scrollbarWidth:8,scrollbarMargin:10});

  
}

/**
 *
 */
function display (category) {
		
	dtCloseAllCategories(category);
	
	var whichcategory = document.getElementById(category);
	if (whichcategory.className=="show") {
		whichcategory.className="hide";
	} else {
		whichcategory.className="show";
	}
}

/**
 *
 */
function dtCloseAllCategories(category) {
  if( category != "Hotel" )
    document.getElementById("Hotel").className='hide';
  if( category != "Suites" )
    document.getElementById("Suites").className='hide';
  if( category != "Folegandros" )
    document.getElementById("Folegandros").className='hide';
  if( category != "media" )
    document.getElementById("media").className='hide';
  if( category != "reservations" )
    document.getElementById("reservations").className='hide';
}

/**
   * Retrieve the coordinates of the given event relative to the center
   * of the widget.
   *
   * @param event
   *   A mouse-related DOM event.
   * @param reference
   *   A DOM element whose position we want to transform the mouse coordinates to.
   * @return
   *    A hash containing keys 'x' and 'y'.
   */
  function getRelativeCoordinates(event, reference) {
    var x, y;
    event = event || window.event;
    var el = event.target || event.srcElement;

    if (!window.opera && typeof event.offsetX != 'undefined') {
      // Use offset coordinates and find common offsetParent
      var pos = { x: event.offsetX, y: event.offsetY };

      // Send the coordinates upwards through the offsetParent chain.
      var e = el;
      while (e) {
        e.mouseX = pos.x;
        e.mouseY = pos.y;
        pos.x += e.offsetLeft;
        pos.y += e.offsetTop;
        e = e.offsetParent;
      }

      // Look for the coordinates starting from the reference element.
      var e = reference;
      var offset = { x: 0, y: 0 }
      while (e) {
        if (typeof e.mouseX != 'undefined') {
          x = e.mouseX - offset.x;
          y = e.mouseY - offset.y;
          break;
        }
        offset.x += e.offsetLeft;
        offset.y += e.offsetTop;
        e = e.offsetParent;
      }

      // Reset stored coordinates
      e = el;
      while (e) {
        e.mouseX = undefined;
        e.mouseY = undefined;
        e = e.offsetParent;
      }
    }
    else {
      // Use absolute coordinates
      var pos = getAbsolutePosition(reference);
      x = event.pageX  - pos.x;
      y = event.pageY - pos.y;
    }
    // Subtract distance to middle
    return { x: x, y: y };
  }
  
  function getAbsolutePosition(element) {
    var r = { x: element.offsetLeft, y: element.offsetTop };
    if (element.offsetParent) {
      var tmp = getAbsolutePosition(element.offsetParent);
      r.x += tmp.x;
      r.y += tmp.y;
    }
    return r;
  };

/**
 *
 */
function getMouseXY(e) // works on IE6,FF,Moz,Opera7
{ 
  if (!e) e = window.event; // works on IE, but not NS (we rely on NS passing us the event)
 
  if (e)
  { 
    if (e.pageX || e.pageY)
    { // this doesn't work on IE6!! (works on FF,Moz,Opera7)
      mousex = e.pageX;
      mousey = e.pageY;
      algor = '[e.pageX]';
      if (e.clientX || e.clientY) algor += ' [e.clientX] '
    }
    else if (e.clientX || e.clientY)
    { // works on IE6,FF,Moz,Opera7
      // Note: I am adding together both the "body" and "documentElement" scroll positions
      //       this lets me cover for the quirks that happen based on the "doctype" of the html page.
      //         (example: IE6 in compatibility mode or strict)
      //       Based on the different ways that IE,FF,Moz,Opera use these ScrollValues for body and documentElement
      //       it looks like they will fill EITHER ONE SCROLL VALUE OR THE OTHER, NOT BOTH 
      //         (from info at http://www.quirksmode.org/js/doctypes.html)
      mousex = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
      mousey = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;
      algor = '[e.clientX]';
      if (e.pageX || e.pageY) algor += ' [e.pageX] '
    }
  }
}
  
/**
 *
 */
function dtThumbsControl( containerName, event, scrollDirection ) {
	
  scrollStep = 1;
 
  var thumbsLayerWidth=getObjectWidth(containerName);
  var thumbsLayerHeight=getObjectHeight(containerName);

  getMouseXY(event);
  pos_x=mousex-document.getElementById(containerName).offsetLeft;
  pos_y=mousey-document.getElementById(containerName).offsetTop;
	
  vecX = thumbsLayerWidth/2-pos_x;
  vecY = thumbsLayerHeight/2-pos_y;
  
  //document.getElementById("infoText").innerHTML=thumbsLayerHeight+"/"+pos_y+"/"+vecY;

  if( ThumbsScrollDirection.HORIZONTAL == scrollDirection ) {
	if( vecX < -20 ) {
	  scrollDivLeftStep(containerName,scrollStep);
	}
	else if( vecX > 20 ) {
	  scrollDivRightStep(containerName,scrollStep);
	}
  }
  else {
	if( vecY < -40 ) {
	  scrollDivUpStep(containerName,scrollStep);
	}
	else if( vecY > 40 ) {
	  scrollDivDownStep(containerName,scrollStep);
	}
  }  
}

/**
 *
 */
function countChildElements(parent, child) {
  var parent = document.getElementById(parent);
  var childCount = parent.getElementsByTagName(child).length;
  return childCount;
}

/**
 *
 */
function processForm( errorMessage ){
  if( document.contantForm.email.value == '' ||
	  document.contantForm.message.value == '' ) {
    alert( errorMessage );
    return false;
  }
  
  return jcap();
}

