/*
    Document   : script.js
    Created on : Jan 20, 2011
    Copyright  : David Obdržálek, dayvee.cz
*/

var Web = {

 include: function(src,dir,async) {
  var e = document.createElement('script');
  e.src = dir ? src : ('/_pages/script/'+src);
  e.type = 'text/javascript';
  var t = document.createTextNode('');
  e.appendChild(t);
  if (async)
   e.async = true;
  document.getElementsByTagName('head')[0].appendChild(e);
 },

 load: function() {
//  Web.include('swfobject.js');
//  Web.include('prototype.js');
//  Web.include('scriptaculous.js?load=effects,builder');

 },
 
 dayKey: {
  stack: new Array(),
  index: 0,
  push: function(fce) {
   Web.dayKey.stack[Web.dayKey.index] = window.onkeypress;
   Web.dayKey.index++;
   window.onkeypress = fce;
 //  alert(Web.dayKey.index);
  },
  pop: function() {
   Web.dayKey.stack[Web.dayKey.index] = function() {};
   Web.dayKey.index--;
   if (Web.dayKey.index > -1)
    window.onkeypress = Web.dayKey.stack[Web.dayKey.index];
   else
    Web.dayKey.index = 0;
 //  alert(Web.dayKey.index);
  }
 },
 
 photoSwitch: {
  
  load: function() {   
   $('photo_switch_left').onclick = function() {
	Web.photoSwitch.move(-1);
	return false;
   }
   $('photo_switch_right').onclick = function() {
	Web.photoSwitch.move(1);
	return false;
   }
   $('photo_switch_left').show();
   $('photo_switch_right').show();
   
   preload1 = new Image();preload1.src = Web.photoSwitch.base+Web.photoSwitch.set[1];
   Web.photoSwitch.time = setTimeout("Web.photoSwitch.move(1);",10*1000);
   return false;
  },
  
  move: function(m) {
   set = Web.photoSwitch.set;
   i = ( Web.photoSwitch.index + m ) % set.length;
   if ( i < 0) i+= set.length;
   Web.photoSwitch.index = i;
   
   $('photo_switch_left').hide();
   $('photo_switch_right').hide();
   clearTimeout(Web.photoSwitch.time);
   
   var img0 = $$('#photo img')[0];
   var img1 = new Element('img', {src:Web.photoSwitch.base+set[i], alt:''});
   if (m < 0) {
	img1.style.marginLeft = '-906px';
	new Insertion.Before(img0,img1);
	moveX = +906;	
   }else {
	new Insertion.After(img0,img1);
	moveX = -906;
   }
   new Effect.Move(img0, {x:moveX, y:0, duration:.5});
   new Effect.Move(img1, {x:moveX, y:0, duration:.5, afterFinish:function() {
	 img0.remove();
	 img1.style.left='0px';
	 img1.style.marginLeft='0px';
	 Web.photoSwitch.load();
   }});
  
   /* preload */
   i0 = ( Web.photoSwitch.index -1 ) % set.length;
   if ( i0 < 0) i0+= set.length;
   i1 = ( Web.photoSwitch.index +1 ) % set.length;
   if ( i1 < 0) i1+= set.length;
   preload1 = new Image();preload1.src = Web.photoSwitch.base+set[i0];
   preload2 = new Image();preload2.src = Web.photoSwitch.base+set[i1];
   
   
  },
  
  base: '',
  index: 0,
  set: [],
  time: false
  
 },
 
 galerie: {
 
  ac: new Element('a',{href:'#',id:'gal_switch_close'}),  
  ap: new Element('a',{href:'#',id:'gal_switch_left'}),
  an: new Element('a',{href:'#',id:'gal_switch_right'}),
  
  load: function() {
   ass = $$('.gal .slider a');
   for(i=0;i<ass.length;i++) {
	a = ass[i];
	a.onclick = function() {
	 s = this.select('img')[0].src.replace(/\.thumb/,'');
	 if ($$('.gal .slider a.active').length)
	  $$('.gal .slider a.active')[0].removeClassName('active');
	 this.addClassName('active');
	 Web.galerie.show(this,s);
	 return false;
	}
   }
   gps = $$('.gal .photo');
   for(i=0;i<gps.length;i++) {
	gps[i].hide();
   }
   Web.galerie.ac.update('&nbsp;');
   Web.galerie.ac.onclick = function() {Web.galerie.close();return false;}
   Web.galerie.ap.update('&nbsp;');
   Web.galerie.ap.onclick = function() {Web.galerie.move(-1);return false;}
   Web.galerie.an.update('&nbsp;');
   Web.galerie.an.onclick = function() {Web.galerie.move(+1);return false;}
  },
  
  show: function(o,s) {
   gs = $$('.gal');
   for(i=0;i<gs.length;i++) {
	g = gs[i];
	if (g == o.parentNode.parentNode) {
	 new Effect.Opacity(g, {to:1});
	 img = new Element('img', {src:s});
	 gp = g.select('.photo')[0];
	 if (!gp.visible()) {
	  new Effect.BlindDown(gp);
	 }
	 gp.update(Web.galerie.ac);
	 gp.insert(Web.galerie.ap);
	 gp.insert(Web.galerie.an);
	 gp.insert(img);
	 
	 sl = g.select('.slider')[0];
	 rows = Math.ceil(sl.select('a').length / 6);
	 h = rows * (72+12) +12 ;
	 new Effect.Morph(sl, {style:'height:'+h+'px;'});
	 
	} else {
	 new Effect.Opacity(g, {to:.35});
	 gpp = g.select('.photo')[0];
	 if (gpp.visible()) {
	  new Effect.BlindUp(gpp, {afterFinish:function() {
		gpp.update();
	  }})
	 }
	 new Effect.Morph(g.select('.slider')[0], {style:'height:72px;'});
	}
   }
   
   window.onkeyup = function(e) {
	c = e.keyCode;
	switch (c) {
	 case 39:
	  Web.galerie.move(+1);
	  break;
	 case 37:
	  Web.galerie.move(-1);
	  break;
	 case 27:
	  Web.galerie.close();
	  break;
	}
   }
  },
  
  close: function() {
   gs = $$('.gal');
   for(i=0;i<gs.length;i++) {
	g = gs[i];
	new Effect.Opacity(g, {to:1});
	gpp = g.select('.photo')[0];
	if (gpp.visible()) {
	 new Effect.BlindUp(gpp, {afterFinish:function() {
	   gpp.update();
	 }})
	}
	new Effect.Morph(g.select('.slider')[0], {style:'height:72px;'});
   }
   Web.dayKey.pop();
  },
  
  move: function(m) {
   gs = $$('.gal .photo');
   for (i=0; i<gs.length; i++)
	if (gs[i].visible())
	 g = gs[i].parentNode;

   if (!g)
	return;
   
   index = 0;
   set = g.select('.slider a');
   for (i=0;i<set.length;i++) {
	if (set[i].hasClassName('active'))
	 index = i;
   }
   
   i = ( index + m ) % set.length;
   if ( i < 0) i+= set.length;
   
   if (set[i])
	set[i].onclick();
  }
  
 },

 finalSets: function() {
  var ass = $$('a');
  for (i=0; i<ass.length; i++) {
   a = ass[i];
   a.onfocus = function() {this.blur();}
   switch (a.getAttribute('rel')) {
    case 'new':
     a.onclick = function() {
      var nw = window.open(this.href);
      return false;
     }
     break;
    default:break;
   }
  }

  var forms = $$('form');
  for (i=0; i<forms.length; i++) {
   f = forms[i];
   f.onsubmit = function() {
    inps = this.select('input');
    for(j=0;j<inps.length;j++) {
     inp = inps[j];
     if (inp.value == inp.getAttribute('title'))
      inp.value = '';
     if ( (inp.hasAttribute('required') || inp.hasClassName('required') ) && !inp.value) {
      inp.focus();
      new Effect.Highlight(inp, {startcolor:'#990000',endcolor:'#221111',resetcolor:'#221111', duration:0.2, queue:'end'} );
      new Effect.Highlight(inp, {startcolor:'#990000',endcolor:'#221111',resetcolor:'#221111', duration:0.2, queue:'end'} );
      return false;
     }
    }
    return true;
   }
  }

 }

};

Web.load();
window.onload = function() {
 Web.finalSets();
}
