var used = false;
var last = 0;
var hday, hmon, hyear, highlighted
var loc = new String(location);
if (loc.contains('www.')) {
	var root = "http://www.book-a-steve.de/";
} else {
	var root = "http://book-a-steve.de/";	
}
window.addEvent('domready', function() {

 	var div = $$('body').setStyles({
		position: 'absolute',
		width:	  '100%',
		height:		'100%',
		'text-align': 'center',
		overflow: 'hidden'
	});
	var div = $$('#page').setStyles({
		height:		'100%',
		width:		'920px',
		'text-align':	'center',
		margin:		'0 auto'
	});
	
	var div = $$('#outer').setStyles({
		display:	'table',
		height:		'100%',
		'#position':'relative',
		overflow:	'hidden',
		width:		'100%'
	});
	var div = $$('#middle').setStyles({
		'#position':'absolute',
		'#top':		'50%',
		display:	'table-cell',
		'vertical-align':	'middle'
	});
	var div = $$('#inner').setStyles({
		'#position':'relative',
		'#top':		'-50%'
	});
	var div = $$('#content').setStyles({
		height:		'420px',
		overflow:	'hidden'
	});
	/*
	var steveCon = new Fx.Elements($$('#content'));
	steveCon.start({
    	'0': { 
        	'height': '340px'
    	}
	});*/
	
});
window.addEvent('load', function() {
	var content_height = $('scroll_outer').getSize().size.y-420;
	if (content_height > 0) {
		var scroll = new Fx.Scroll('content', {
			wait: false,
			fps:   50,
			duration: 1000,
			offset: {'x': 0, 'y': 0},
			transition: Fx.Transitions.Quad.easeInOut
		});			
		var div = $$('#slider').setStyles({
			'float':	'left',	
			width:		'10px',
			height:		'420px',
			background:	'#f0f0f0',
			'margin-left':'-190px'	
		});
		var div = $$('#knob').setStyles({
			width:		'10px',
			height:		'50px',
			background:	'#ccc'	
		});
		var steveSlide = new Slider($('slider'), $('knob'), {	
			steps: content_height,	
			mode: 'vertical',	
			onChange: function(step){
					scroll.scrollTo(0,step);	
			}
		}).set(0);

		Element.Events.extend({
			'wheelup': {
				type: Element.Events.mousewheel.type,
				map: function(event){
					event = new Event(event);
					if (event.wheel >= 0) this.fireEvent('wheelup', event)
					}
			},
 
			'wheeldown': {
				type: Element.Events.mousewheel.type,
				map: function(event){
					event = new Event(event);
					if (event.wheel <= 0) this.fireEvent('wheeldown', event)
					}
			}
		});

		$('content').addEvents({
			'wheelup': function(e) {
				e = new Event(e).stop();
 				steveSlide.set(steveSlide.step-20);
			},
 
			'wheeldown': function(e) {
				e = new Event(e).stop();
 				steveSlide.set(steveSlide.step+20);
			}
		});
	};
});

function galleryLinks(newset) {
	var tmbs = $ES('a','tmbinner'); //thumbnails
  
  if (newset==null || newset==false) {
  	var tmb_id = new String($$('#firsttmb a').getProperty('id')); 
    last = tmb_id.split("_")[1];//slice(2,-(1-1));
	  new Fx.Style('tmb'+last, 'width',  {duration: 100, transition: Fx.Transitions.linear}).start(0);
	}
	if ($('tmb'+last)) {
    new Fx.Style('tmb'+last, 'width',  {duration: 100, transition: Fx.Transitions.linear}).start(0);
  }
	var old;
	tmbs.each(function(tmb){
		var thistmb_id = new String(tmb.getProperty('id'));
		tmb.setProperty('href','#');
		tmb.addEvent('click', function(e) {
			if (used == false ) {
			e = new Event(e).stop();
			used = true;
			$('loadbox').setStyle('display','inline');
			new Ajax(root + "preload_img.php?img=" + (thistmb_id.split('_')[1]), {
          method: 'get',
          onComplete: function(request) {
              new Asset.images([request], {
              onComplete:  function() {
                $('loadbox').setStyle('display','none');
                newImage.request();
                }
                });
          }
      }).request();
      
			var url = root + "ajax_img.php?img=" + (thistmb_id.split("_")[1]);
			var newImage = new Ajax(url, {
					method:	'get',
					onComplete: function(request) {
							$('nextview').setHTML(request);
              				
							v.start(0);
              s.start(-580);				
						}
					})
				
			var s = new Fx.Style('full', 'margin-right',  {fps:60,duration: 400, transition: Fx.Transitions.linear});
			var v = new Fx.Style('nextview', 'left', {fps:60,duration: 400, 
													transition: Fx.Transitions.linear, //.easeIn, 
													onComplete: function(){
													  if ($('tmb'+last)) {
														  new Fx.Style('tmb'+last, 'width',  {duration: 100, transition: Fx.Transitions.linear}).start(44);
														}
														last = thistmb_id.split("_")[1];
														new Fx.Style('tmb'+last, 'width',  {duration: 100, transition: Fx.Transitions.linear}).start(0);
														//$('tmb'+last).setStyle('display','none');
														$('fullview').remove();
														$('nextview').setProperty('id','fullview');
														nextimg = new Element('div',{
															'id':'nextview'
														});
														nextimg.injectInside($E('body'));
														$('nextview').injectBefore('fullview');
														used = false;														
													}
								});
			}		
		});
	});
	swthumbs();
	
};

function showImg(img, e) {
	e = new Event(e).stop();	
}

function swthumbs() {
  var navi = $ES('a','imgnav');
	navi.each(function(nav){
    nav.setProperty('href','#');
    nav.addEvent('click', function(e){
      e = new Event(e).stop();
      var url = root + "req_thumbs.php?thumbs=" + nav.getProperty('class');
      new Ajax(url, {
          method: 'get',
          update: 'thumbnails',
          onComplete: function(){
            galleryLinks(true);
            
          }
          }).request();
    });
  });
}

function swcal(cd, cm, cy) {
  if(cd) {
    highlighted = cd+"_"+cm+"_"+cy;
  }
  var navi = $ES('a','calnav');
	navi.each(function(nav){
    nav.setProperty('href','#');
    nav.addEvent('click', function(e){
      e = new Event(e).stop();
      var url = root + "req_cal.php?dmy=" + nav.getProperty('class');
      new Ajax(url, {
          method: 'get',
          update: 'calendar',
          onComplete: function(){
            swcal();
          }
          }).request();
    });
  });
  swDays();
}


function swDays() {
  var navi = $ES('a','cal');
	navi.each(function(nav){
    nav.setProperty('href','#');
    if (nav.getProperty('class')==highlighted) {
      nav.getParent().setProperty('class','chosen');
    }
    nav.addEvent('click', function(e){
      e = new Event(e).stop();
      var url = root + "req_entry.php?dmy=" + nav.getProperty('class') + "&js=true";
      highlighted = nav.getProperty('class');
      entry_content = new Ajax(url + "&content=text", {
          method: 'get',
          update: 'scroll_outer',
          onComplete: function(){
            updateCal(nav.getParent().getProperty('id'));
          }
          });
      new Ajax(url + "&content=title", {
          method: 'get',
          update: 'entry_title',
          onComplete: function() {
            entry_content.request();
          }
          }).request();
    });
  });
}

function updateCal(highlight) {
  $$('.chosen').setProperty('class','dfilled');
  $(highlight).setProperty('class','chosen');
}

function aboutImages() {
  $ES('a','aboutimg_nav').each(function(nav){
    nav.setProperty('href','#');
    nav.addEvent('click', function(e) {
      e = new Event(e).stop();
      $$('.act').setProperty('class','');
      nav.setProperty('class','act');
      $('aboutsteve').setProperty('src',root+'img/about'+nav.getText()+'.jpg');
    });
  });  
}
