/*
	About:
	  Author: Michal Schejbal
	  Version: 1.0

		- Based on mootools 1.3


	Changelog (only vital changes):
		1.1:
		  -

	Usage:
		Type these to element class attribute
		1. Selectors:
		  ui-make-[ui control class name]

		  Otherwise you can alter _uiConstrols.set function select mechanism

		example:
		  ui-make-scroller  // Will find all elements with this class and make them scrollers

		2. Special parameters:
		  - ui-omit         // This element will be omitted
		  - ui-wrapped      // This element was already wraped


		Styling via css
		1. Available class names:
		  Use these class names to style ui elements
		  - ui-htmlButton
		  - ui-scroller
		  - ui-select
		  - ui-checkbox
		  - ui-radio
		  - ui-treeView
		  - ui-slideShow

	    - ui-make-overinput   // mootools implementation

*/


var _uiControls = new Class(
{
  //  ['button', 'submit', 'scroller', 'select', 'checkbox', 'radio', 'treeView', 'slideShow', 'overinput']
	set: function(types)
	{
		types.each(function(item)
		{
			switch(item)
			{
			  case 'submit':
			    $$('input[type="submit"]').each(function(e)
					{
					  if(!e.hasClass('ui-wrapped') && !e.hasClass('ui-omit'))
							new _uiHtmlButton(e);
					});
					break;
			  case 'button':
			    $$('input[type="button"], .ui-make-htmlButton').each(function(e)
					{
					  if(!e.hasClass('ui-wrapped') && !e.hasClass('ui-omit'))
							new _uiHtmlButton(e);
					});
					break;

			  case 'scroller':
			    $$('input.ui-make-scroller').each(function(e)
					{
					  if(!e.hasClass('ui-wrapped') && !e.hasClass('ui-omit'))
							new _uiScroller(e);
					});
					break;

			  case 'select':
			    $$('select').each(function(e)
					{
					  if(!e.hasClass('ui-wrapped') && !e.hasClass('ui-omit'))
							new _uiSelect(e);
					});
					break;

			  case 'checkbox':
			    $$('input[type="checkbox"]').each(function(e)
					{
					  if(!e.hasClass('ui-wrapped') && !e.hasClass('ui-omit'))
							new _uiCheckbox(e);
					});
					break;

			  case 'radio':
			    $$('input[type="radio"]').each(function(e)
					{
					  if(!e.hasClass('ui-wrapped') && !e.hasClass('ui-omit'))
							new _uiRadio(e);
					});
					break;

			  case 'treeView':
			    $$('.ui-make-treeView').each(function(e)
					{
					  if(!e.hasClass('ui-wrapped') && !e.hasClass('ui-omit'))
							new _uiTreeView(e);
					});
					break;

			  case 'slideShow':
			    $$('.ui-make-slideShow').each(function(e)
					{
					  if(!e.hasClass('ui-wrapped') && !e.hasClass('ui-omit'))
					  {
					    var tmp  = e.getElements('img');
					    var imgs = new Array();
					    
					    for(var i=0; i<tmp.length; i++)
					      imgs.push({src: tmp[i].src, title: tmp[i].title ? tmp[i].title : tmp[i].alt});

							e.empty();
							new _uiSlideShow(e, imgs, JSON.decode(e.title));
							e.removeProperty('title');
						}
					});
					break;

			  case 'overinput':
			    $$('input[alt], ui-make-overinput').each(function(e)
					{
						if(e.alt!=null && !e.hasClass('ui-wrapped') && !e.hasClass('ui-omit'))
						{
							new OverText(e, {wrap: false, poll: true});
							e.addClass('ui-wrapped');
						}
					});
			    break;
			}
		});
	},

	setBrowserInfo: function()
	{
	  $('body').addClass(Browser.name+' '+Browser.name+Browser.version+' flash'+Browser.Plugins.Flash.version+' '+Browser.Platform.name);
	}
});

var controls = new _uiControls();




// ---------------------------------------------------------------------------------------------------
// Button
// ---------------------------------------------------------------------------------------------------
var _uiHtmlButton = new Class(
{
	e: null,

  initialize: function(e)
  {
    this.e     = e;
    var parent = e.getParent();
    var size   = e.getSize();

    e.setStyle('display', 'none');
    e.addClass('ui-wrapped');

    var capsule = new Element('div',
		{
			'class': 'ui-htmlButton',
			styles:
			{
				width: size.x
			}
		});

    var link = new Element('a',
		{
			text: e.get('value') ? e.get('value') : e.get('text'),
			title: e.get('title'),
			'class': 'link',
			styles:
			{
			  cursor: 'pointer'
			},
			events:
			{
			  click: function()
			  {
			    if(e.get('tag')!='a')
			    {
				    var form = this.e.getParent('form');
				    if(form) this.e.getParent('form').submit();
				    else this.e.fireEvent('click');
			    }

					return true;
			  }.bind(this)
			}
		});

		if(e.get('tag')=='a') link.set('href', e.get('href'));

		capsule.adopt(link);


    // Borders
    capsule.adopt(new Element('div', {'class': 'border _top'}));
    capsule.adopt(new Element('div', {'class': 'border _left'}));
    capsule.adopt(new Element('div', {'class': 'border _bottom'}));
    capsule.adopt(new Element('div', {'class': 'border _right'}));

    parent.adopt(capsule);
    capsule.adopt(e);
  }
});




// ---------------------------------------------------------------------------------------------------
// Scroller
// ---------------------------------------------------------------------------------------------------
var _uiScroller = new Class(
{
  Implements: [Options],

	e: null,
	options:
	{
	  negValues: false
	},

  initialize: function(e, opts)
  {
   	this.e  = e;
    var parent = e.getParent();
    var size   = e.getSize();

    this.setOptions(opts);
    e.addClass('ui-wrapped');

    var capsule = new Element('div',
		{
			'class': 'ui-scroller',
			styles:
			{
			  position: 'relative',
				width: size.x,
				height: size.y
			}
		});

    var plus = new Element('a',
		{
			text: '+',
			href: '',
			'class': 'plus',
			styles:
			{
			  position: 'absolute'
			},
			events:
			{
			  click: function()
			  {
			    this.e.set('value', parseInt(this.e.get('value'))+1);
					return false;
			  }.bind(this)
			}
		});
		capsule.adopt(plus);

    var minus = new Element('a',
		{
			text: '-',
			href: '#',
			'class': 'minus',
			styles:
			{
			  position: 'absolute'
			},
			events:
			{
			  click: function()
			  {
			    var value = parseInt(this.e.get('value'))-1;
			    if(this.options.negValues || value>=0) this.e.set('value', value);

					return false;
			  }.bind(this)
			}
		});
		capsule.adopt(minus);

		e.addEvent('keydown', function(ev)
		{
			switch(ev.code)
			{
			  case 38:  // up
			    this.e.set('value', parseInt(this.e.get('value'))+1);
			    break;
			  case 40:  // down
			    var value = parseInt(this.e.get('value'))-1;
			    if(this.options.negValues || value>=0) this.e.set('value', value);
			    break;
			}
		}.bind(this));

    e.inject(capsule, 'top');
    parent.adopt(capsule);
  }
});




// ---------------------------------------------------------------------------------------------------
// Select
// ---------------------------------------------------------------------------------------------------
var _uiSelect = new Class(
{
  Implements: [Options],

	e: null,
	options:
	{
	  dropdownTime: 200,
		useWrapped: true,
		arrowText: '»'
	},

	capsule: null,
	select: null,
	arrowOver: null,
	arrow: null,
	wrapper: null,
	list: null,
	fxList: null,

	inst: null,      // Static

  initialize: function(e, opts)
  {
   	this.e  = e;
    var parent = e.getParent();
    var size   = e.getSize();

    this.setOptions(opts);

    if(!_uiSelect.inst) _uiSelect.inst = new Array();
    _uiSelect.inst.push(this);

    e.addClass('ui-wrapped');
    e.setStyles({position: 'absolute', 'z-index': 4, opacity: 0});

    this.capsule = new Element('div',
		{
			'class': 'ui-select',
			styles:
			{
			  position: 'relative',
				width: size.x,
				height: size.y
			}
		});

    this.select = new Element('div',
		{
			text: e.options[e.selectedIndex].text,
			'class': 'select',
			styles:
			{
			  position: 'absolute',
			  top: 0,
			  left: 0,
			  width: size.x,
			  height: size.y,

			  'line-height': size.y,
			  overflow: 'hidden',
			  'white-space': 'nowrap',
			  cursor: 'pointer',
			  'z-index': 2
			},
			events:
			{
			  click: function()
			  {
			    if(this.fxList) this.fxList.toggle();
			  }.bind(this)
			}
		});
		this.capsule.adopt(this.select);


    this.arrowOver = new Element('div',
		{
			'class': 'arrowOver',
			styles:
			{
			  position: 'absolute',
			  cursor: 'pointer',
			  'z-index': 3
			},
			events:
			{
			  click: function()
			  {
			    if(this.fxList) this.fxList.toggle();
			  }.bind(this)
			}
		});

    this.arrow = new Element('div',
		{
			text: this.options.arrowText,
			'class': 'arrow',
			styles:
			{
			  cursor: 'pointer'
			},
			events:
			{
			  click: function()
			  {
			    if(this.fxList) this.fxList.toggle();
			  }.bind(this)
			}
		});

		this.arrowOver.adopt(this.arrow);
		this.capsule.adopt(this.arrowOver);
    this.capsule.adopt(e);
    parent.adopt(this.capsule);

		this.options.useWrapped ? this.useWrapped() : this.useDefailt();
  },


  useDefault: function()
  {
		this.e.addEvent('change', function()
		{
		  this.select.set('text', this.e.options[this.e.selectedIndex].text);
		}.bind(this));

		this.e.setStyle('visibility', 'visible');
  },


  useWrapped: function(size)
  {
		var size = this.capsule.getSize();

		this.wrapper = new Element('div',
  	{
			'class': 'listWrapper',
  	  styles:
  	  {
			  position: 'absolute',
			  top: size.y+1,
			  left: 0,
			  width: size.x,

			  'z-index': 1
			}
		});

    this.list = new Element('ol',
    {
      'class': 'list',
			styles:
			{
			  position: 'absolute',
			  top: 0,
			  width: (size.x-2)+'px',
			  'max-height': '300px',
			  margin: 0,

				'overflow-y': 'auto',
				'overflow-x': 'hidden',

				'z-index': 1
			}
    });

    for(var i=0; i<this.e.options.length; i++)
    {
	    this.list.adopt(new Element('li',
	    {
	      'class': (!(i%2) ? 'odd ' : 'even ') + (!i ? 'first' : '') + (i==this.e.options.length-1 ? 'last' : ''),
	      text: this.e.options[i].text,
	      title: this.e.options[i].text,
	      styles:
				{
				  cursor: 'pointer',
				  'white-space': 'nowrap'
				},
	      events:
	      {
					click: function(arg)
					{
					  this.select.set('text', this.e.options[arg].text);
					  this.e.selectedIndex = arg;
					  this.fxList.toggle();
					}.bind(this,i)
	      }
	    }));
    }

    this.wrapper.adopt(this.list);
    this.capsule.adopt(this.wrapper);
    this.fxList = new Fx.Slide(this.list,
		{
			duration: this.options.dropdownTime,
			transition: 'quad:out',
			wrapper: this.wrapper
		}).hide();

		this.fxList.addEvent('start', function()
	  {
	    for(var i=0; i<_uiSelect.inst.length; i++)
	    {
	      if(_uiSelect.inst[i]!=this)
	        _uiSelect.inst[i].fxList.hide();
	    }

	    if(!this.fxList.open)
				this.wrapper.setStyle('z-index',parseInt(this.wrapper.getStyle('z-index'))+5);
	  }.bind(this));

		this.fxList.addEvent('complete', function()
	  {
			if(!this.fxList.open)
				this.wrapper.setStyle('z-index',parseInt(this.wrapper.getStyle('z-index'))-5);
	  }.bind(this));

    this.e.setStyle('visibility', 'hidden');
  }

});




// ---------------------------------------------------------------------------------------------------
// Checkbox
// ---------------------------------------------------------------------------------------------------
var _uiCheckbox = new Class(
{
	orig: null,

	capsule: null,
	box: null,
	check: null,

	inst: null,

  initialize: function(e)
  {
   	this.e     = e;
    var parent = e.getParent();
    var size   = e.getSize();

		e.setStyle('display', 'none');
    e.addClass('ui-wrapped');

    if(!_uiCheckbox.inst) _uiCheckbox.inst = new Array();
    _uiCheckbox.inst.push(this);


    this.capsule = new Element('div',
		{
			'class': 'ui-checkox',
			styles:
			{
			  position: 'relative',
				width: size.x,
				height: size.y
			}
		});

		this.box = new Element('div',
		{
		  'class': 'cbBox',
		  styles:
		  {
		    position: 'absolute',
		    cursor: 'pointer'
		  },
		  events:
		  {
		    click: function()
		    {
		      this.toggle();
		    }.bind(this)
		  }
		});
		this.capsule.adopt(this.box);

		this.check = new Element('div',
		{
		  //text: '●',
		  'class': 'cbCheck',
		  styles:
		  {
		    position: 'absolute',
		    cursor: 'pointer'
		  },
		  events:
		  {
		    click: function()
		    {
		      this.toggle();
		    }.bind(this)
		  }
		});
		this.capsule.adopt(this.check);

		label = parent.getElement('label[for="'+this.e.get('id')+'"]');

		label.removeProperty('for');
		label.addClass('cbLabel');
		label.setStyle('position', 'absolute');
		label.addEvent('click', function()
		{
		  this.toggle();
		}.bind(this));

		parent.adopt(this.capsule);
		this.capsule.adopt(this.e);
		this.capsule.adopt(label);

		if(this.e.checked) this.check.toggleClass('checked');
  },

  toggle: function()
  {
    this.e.fireEvent('click');
    this.e.checked = !this.e.checked;
    this.check.toggleClass('checked');

    if(this.e.get('onclick'))
    {
      var sid = this.e.get('id');
      var id  = '_uiCheckbox'+_uiCheckbox.inst.length;

			this.e.set('id', id);
			eval(this.e.get('onclick').replace(new RegExp('this+', 'g'), '$(\''+id+'\')'));
			this.e.set('id', sid);
		}
  }

});




// ---------------------------------------------------------------------------------------------------
// Radio
// ---------------------------------------------------------------------------------------------------
var _uiRadio = new Class(
{
	orig: null,

	inst: null,

  initialize: function(e)
  {
   	this.e     = e;
    var parent = e.getParent();
    var size   = e.getSize();

    this.setOptions(opts);

    if(!_uiRadio.inst) _uiRadio.inst = new Array();
    _uiRadio.inst.push(this);

    e.addClass('ui-wrapped');
    e.setStyles({position: 'absolute', 'z-index': 4, opacity: 0});

    this.capsule = new Element('div',
		{
			'class': 'ui-select',
			styles:
			{
			  position: 'relative',
				width: size.x,
				height: size.y
			}
		});
	}
});




// ---------------------------------------------------------------------------------------------------
// Tree
// ---------------------------------------------------------------------------------------------------
var _uiTreeView = new Class(
{
  initialize: function(e)
  {
    // Doesnt behave good
    if(Browser.ie7) return;

    e.addClass('ui-treeView ui-wrapped');

    this.browse(e);

  /*
  	list = e.getChildren('ul');
   	for(var i=0; i<list.length; i++)
			this.browse(list[i]);
	*/
  },

	browse: function(list)
	{
	  if(list)
	  {
		  list.getChildren('li').each(function(item)
			{
			  item.setStyle('position', 'relative');


				var active = item.hasClass('active');
				var childen  = item.getChildren('ul');

				for(var i=0; i<childen.length; i++)
   			{
					var child = childen[i];

				  if(child)
				  {
				    item.setStyle('background', '0');

					  toggler = new Element('a',
					  {
					    text: active ? '-' : '+',
							'class': 'toggler',
							href: '',
					    events:
					    {
					      click: function()
					      {
		              this.set('text', this.retrieve('fxToggle').open ? '+' : '-');
		              this.retrieve('fxMorph').start({opacity: this.retrieve('fxToggle').open ? [1,0] : [0,1]});
		              this.retrieve('fxToggle').toggle();

		              return false;
					      }
					    }
					  });

					  toggler.store('fxMorph', new Fx.Morph(child, {duration: 500}));
					  toggler.store('fxToggle', new Fx.Slide(child, {duration: 500, transition: 'quad:out', resetHeight: true}));

					  if(!active) toggler.retrieve('fxToggle').hide();

					  item.adopt(toggler);
					  this.browse(child);
					}
				}

			}.bind(this));
		}
	}
});




// ---------------------------------------------------------------------------------------------------
// SlideShow
// ---------------------------------------------------------------------------------------------------
var _uiSlideShow = new Class(
{
  Implements: [Options],

	h: null,
	size: null,
	
	options:
	{
	  delay: 6000,
	  random: false,
	  adjustSize: false,   				// Adjust size to boxing element
		clickToFullSize: false,     // This setting uses mediabox advanced
		description: false,         // Show title of the image

	  menu: false,
	  
	  effects:
	  {
	    transition: true,
	    slide: false,
	    move: false
	  },

		controls:
		{
		  proggress: false,
		  timing: false
		},
		loading:
		{
		  visible: true,
		  delay: 500,
		  progress: true,
		  bgColor: '#fff'
		}
	},

	eDisplay: null,
	eMenu: null,
	eDescPos: null,
	eDesc: null,

	// Controls
	controls:
	{
		h: null,    // Controls window
		progress:
		{
		  enabled: false,
		  h: null,
		  hDone: null,
		  hCount: null
		},
		timing:
		{
		  enabled: false,
		  h: null,
		  hText: null,
		  hGraph: null
		}
	},


	// Images
	images: new Array(),
  loaded: 0,    						// Preload status
  count: 0,     						// Img count
	current: 0,
	timeout: 0,     					//  Time
	timein: 0,
	dImgCheckLoad: null, 			// Delay before check


	// Loading
	eLoading: null,
	dLoadingStart: null,
	dLoadingStop: null,


	// Slides
	hPeriod: null,
	
	// Effects
	efFns: new Array(),


  initialize: function(e, imgs, opts)
  {
		this.h = e;
		this.h.setStyle('position', 'relative');
		this.h.setStyle('z-index', 0);

		e.addClass('ui-slideShow');

		this.size = this.h.getSize();
		this.setOptions(opts);
		
		this.options.delay = Math.round(this.options.delay/1000)*1000;	// Makes sure that only whole second were inputed

		this.iniDisplay();
		this.iniControls();
		this.iniEffects();
		this.iniDescription();

    if(this.options.random) imgs.sort(this.aRnd);
	  this.count = imgs.length;
	  for(var i=0; i<this.count; i++)
	    this.imgLoad(imgs[i]);

		if(this.options.menu) this.iniMenu();

    this.loadingStart();
    this.current = 0;
	  this.display();

	  this.timein   = 0;
    this.timeout  = this.options.delay/1000;

		if(this.images.length>1)
	  	this.hPeriod = this.autoplay.periodical(1000, this);
  },

	// ------------------------------------------------------------------------------------------------------------------------
	// Slideshow
  display: function()
  {
    if(this.options.menu)
    {
	    for(var i=0; i<this.images.length; i++)
	    {
	      if(this.current==i) this.images[i].hMenu.addClass('active');
	      else this.images[i].hMenu.removeClass('active');
	    }
    }

    if(this.dImgCheckLoad)
    {
			clearInterval(this.dImgCheckLoad);
			this.dImgCheckLoad = null;
		}

		clearInterval(this.dLoadingStart);
		if(this.images.length>1)
    	this.dLoadingStart = this.loadingStart.delay(this.options.delay-this.options.loading.delay, this);

		if(!this.images[this.current].completed)
		{
			this.eLoading.setStyles(
			{
				'background-image': 'url("/img/common/loading.gif")',
				'background-position': (this.size.x/2-16)+'px '+(this.size.y/2-16)+'px',
				'background-repeat': 'no-repeat'
			});

		  this.dImgCheckLoadProgress = true;
		  this.dImgCheckLoad         = this.display.delay(75, this);
			return;
		}
		
		if(this.options.description && this.images[this.current].title)
		{
			(function()
			{
			  this.eDesc.set('text', this.images[this.current].title);
				this.eDesc.retrieve('fxMorph').start({opacity: [0,0.7]});
				this.eDesc.retrieve('fxSlide').slideIn();
			}.delay(this.options.loading.delay, this));
			
			if(this.images.length>1)
			{
				(function()
				{
					this.eDesc.retrieve('fxMorph').start({opacity: [0.7,0]});
					this.eDesc.retrieve('fxSlide').slideOut();
				}.delay(this.options.delay-this.options.loading.delay, this));
			}
		}

		this.dLoadingStop = this.loadingStop.delay(this.options.loading.delay*2, this);
		
		if(!this.options.loading.visible) this.runEffects();
    else this.eDisplay.empty();

    	
		this.eDisplay.adopt(this.images[this.current].h);
  },


	autoplay: function()
	{
		  if(this.timein==this.options.delay/1000)
		  {
	      this.timein  = 0;
	      this.timeout = this.options.delay/1000;

	    	this.next();
	    }
	    else
	    {
	      this.timein++;
	      this.timeout--;
	    }

	    this.controlsSetTiming();
	},
  next: function()
  {
    this.current++;
    if(this.current==this.count)
      this.current = 0;

	  this.display();
  },
  prev: function()
  {
    this.current--;
    if(this.current<0)
      this.current = this.count-1;

	  this.display();
  },

	// ------------------------------------------------------------------------------------------------------------------------
	// Elements
	iniDisplay: function()
	{
		this.eDisplay = new Element('div',
		{
		  'class': 'display',
			styles:
			{
			  position: 'absolute',
        top: 0,
        left: 0,
        width: this.size.x+'px',
        height: this.size.y+'px',

				'text-align': 'center',
				overflow: 'hidden'
			},
			events:
			{
			  click: function()
			  {

			  }.bind(this)
			}
		});

		this.h.adopt(this.eDisplay);
	},

	iniMenu: function()
	{
		this.eMenu = new Element('ul',
		{
		  'class': 'menu',
			styles:
			{
			  position: 'absolute'
			}
		});

		for(var i=0; i<this.count; i++)
		{
			var li = new Element('li');

			var a = new Element('a',
			{
			  //text: i,
			  href: '',
			  rel: i
			});

	    a.addEvent('click', function(el)
	    {
        for(var i=0; i<this.images.length; i++)
        {
          if(this.images[i].hMenu==el)
					{
					  // Stop all
            clearInterval(this.hPeriod);
            clearTimeout(this.dLoadingStart);
            clearTimeout(this.dLoadingStop);

            this.loadingStart();
            this.dLoadingStop = this.loadingStop.delay(this.options.loading.delay, this);
				    this.current = i;
					  this.display.delay(this.options.loading.delay, this);

					  this.timein  = 0;
				    this.timeout = this.options.delay/1000;
				    this.controlsSetTiming();
					  this.hPeriod = this.autoplay.periodical(1000, this);
						break;
					}
				}

				return false;
	    }.bind(this, li));

	    li.adopt(a);


			this.images[i].hMenu = li;
			this.eMenu.adopt(li);
		}

		this.h.adopt(this.eMenu);
	},

	iniControls: function()
	{
	  this.controls.progress.enabled = this.options.controlsProgress;
	  this.controls.timing.enabled   = this.options.controlsTiming;


	  this.controls.h = new Element('div',
	  {
	    'class': 'controls',
	    styles:
	    {
	      position: 'absolute',
	      'z-index': '1000'
	    }
	  });

	  // Progress
	  if(this.controls.progress.enabled)
	  {
			this.controls.progress.h = new Element('span',
		  {
		    'class': 'progress'
		  });
		  this.controls.h.adopt(this.controls.progress.h);
	  }

	  // Timing
	  if(this.controls.timing.enabled)
	  {
			this.controls.timing.h = new Element('span', {'class': 'timing'});

			this.controls.timing.hText = new Element('label');
			this.controls.timing.h.adopt(this.controls.timing.hText);

			this.controls.timing.hGraph = new Element('span');
			this.controls.timing.h.adopt(this.controls.timing.hGraph);

		  this.controls.h.adopt(this.controls.timing.h);
	  }

		this.h.adopt(this.controls.h);
	},
	
	iniDescription: function()
	{
	  if(this.options.description)
	  {
		  this.eDescPos = new Element('div',
			{
			  'class': 'descPos',
				styles:
				{
				  position: 'absolute',
				  top: 0,
				  width: '100%',

					'z-index': 1000
				}
			});

		  this.eDesc = new Element('div',
			{
				'class': 'desc'
			});
			
			this.eDescPos.adopt(this.eDesc);
			this.h.adopt(this.eDescPos);
			
			this.eDesc.store('fxMorph', new Fx.Morph(this.eDesc, {duration: 500}));
		  this.eDesc.store('fxSlide', new Fx.Slide(this.eDesc, {duration: 500, transition: 'quad:out', resetHeight: true}));
			this.eDesc.retrieve('fxMorph').set({opacity: 0});
			this.eDesc.retrieve('fxSlide').hide();
		}
	},


	// ------------------------------------------------------------------------------------------------------------------------
	// Loading
  loadingStart: function()
  {
    if(!this.eLoading)
    {
	    this.eLoading = new Element('div',
	    {
	      styles:
	      {
	        position: 'absolute',
	        top: 0,
	        left: 0,
	        width: this.size.x,
	        height: this.size.y
	      }
	    });

	    if(this.options.loading.progress)
				this.eLoading.setStyle('background', 'url("/img/common/loading.gif") no-repeat '+(this.size.x/2-16)+'px '+(this.size.y/2-16)+'px');
	    if(this.options.loading.visible)
				this.eLoading.setStyle('background-color', this.options.loading.bgColor);



	    this.fxLoading = new Fx.Morph(this.eLoading, {duration: this.options.loading.delay}).set({opacity: 0});
	    this.fxLoading.start({opacity: [0,1]});

	    this.h.adopt(this.eLoading);
    }
  },
  loadingStop: function()
  {
    if(this.eLoading)
    {
      this.fxLoading.start({opacity: [1,0]});

	    (function()
			{
			  if(this.eLoading)
			  {
				  this.eLoading.dispose();
					this.fxLoading = null;
				  this.eLoading  = null;
			  }
			}.delay(this.options.loading.delay, this));
		}
  },
  
	// ------------------------------------------------------------------------------------------------------------------------
	// Effects
  iniEffects: function()
  {
    // Transition
    if(this.options.effects.transition)
    {
    	this.efFns.push(function()
		  {
        if(this.images.length>1)
        {
					(function(current)
					{
						new Fx.Morph(this.images[current].h, {duration: this.options.loading.delay*5}).start({opacity: [1,0]});
						this.images[current].h.setStyle('z-index', parseInt(this.images[current].h.getStyle('z-index'))+1);


							(function()
							{
								this.dispose();
								this.setStyle('z-index', 0);
							}.delay(this.options.loading.delay*5, this.images[current].h));

					}.delay(this.options.delay, this, this.current));
				}

        this.images[this.current].h.setStyle('opacity', 0);
				new Fx.Morph(this.images[this.current].h, {duration: this.options.loading.delay*5}).start({opacity: [0,1]});
		  }.bind(this));
    }
  
  
    // Slide
    if(this.options.effects.slide)
    {
    	this.efFns.push(function()
		  {
		    // Should prevents from blinking when position changing
		    this.images[this.current].h.setStyle('left', this.images[this.current].h.getStyle('width'));

				new Fx.Morph(this.images[this.current].h, {duration: this.options.loading.delay*4}).start(
				{
					left: [this.images[this.current].h.width, this.images[this.current].left]
				});


		  }.bind(this));
    }

    
    // Move
    if(this.options.effects.move)
    {
    	this.efFns.push(function()
		  {
		    if(this.images.length>1)
		    {
	 			  this.images[this.current].h.setStyles(
					{
					  width: this.images[this.current].h.width*2,
					  height: this.images[this.current].h.height*2
				  });

				  new Fx.Morph(this.images[this.current].h, {duration: this.options.delay+(this.options.loading.delay*5)}).start(
					{
						top: [-this.rnd(0,this.images[this.current].h.height), -this.rnd(0,this.images[this.current].h.height)],
						left: [-this.rnd(0,this.images[this.current].h.width), -this.rnd(0,this.images[this.current].h.width)]
					});
				}
		  }.bind(this));
    }
  },
  
  runEffects: function()
  {
    for(var i=0; i<this.efFns.length; i++)
			this.efFns[i]();
  },


	// ------------------------------------------------------------------------------------------------------------------------
	// IMG loading
  imgLoad: function(img)
  {
    if(!img) return false;
    if(img.src==null) return false;

    var data =
    {
      h: null,
      width: 0,
      height: 0,
      top: 0,
      left: 0,
      ratio: 0,
      title: img.title,
      hMenu: null,
      completed: false
    };

    data.h = new Element('img',
    {
      src: img.src,
      alt: img.title,
			//morph: {duration: this.options.loading.delay*5},
      styles:
      {
        position: 'absolute',
        top: 0,
        left: 0,

        'z-index': 0
      }
    });

    data.h.addEvent('load', function(el)
    {
        this.loaded++;
        this.controlsSetProgress();

        for(var i=0; i<this.images.length; i++)
        {
          if(this.images[i].h==el)
					{
				    this.images[i].width  = this.images[i].h.width;
				    this.images[i].height = this.images[i].h.height;


					  // Adjust size to boxing element
					  if(this.options.adjustSize)
					  {
					    this.images[i].ratio = this.images[i].h.width>this.images[i].h.height ? this.images[i].h.width/this.size.x : this.images[i].h.height/this.size.y;

					    this.images[i].h.width  /= this.images[i].ratio;
						  this.images[i].h.height /= this.images[i].ratio;
					  }

					  // Center both vertical and horizontal
					  this.images[i].left = this.size.x/2-this.images[i].h.width/2;
					  this.images[i].top  = this.size.y/2-this.images[i].h.height/2;

					  this.images[i].h.setStyles(
					  {
					    top: this.images[i].top,
					    left: this.images[i].left
					  });

					  if(this.options.clickToFullSize)
					  {
					    this.images[i].h.setStyle('cursor', 'pointer');
							this.images[i].h.addEvent('click', function()
							{
								Mediabox.open(this.h.src, this.h.alt, 'mediabox['+this.width+' '+this.height+']');
							}.bind(this.images[i]));
						}

						//$('debug').innerHTML += '<p>'+JSON.encode(this.images[i])+'</p>';

						this.images[i].completed = true;
						break;
					}
				}
    }.bind(this, data.h));
    
    /*
    data.h.addEvent('mousewheel', function()
    {
      new Request({url: this.src}).send();
    });
    */


    this.images.push(data);
  },


  // Controls functions
  controlsSetProgress: function()
  {
		if(this.controls.progress.enabled)
		{
		  this.controls.progress.h.set('text', 'Nahráno '+this.loaded+' z '+this.count+' obrázků');
		}
  },
  controlsSetTiming: function()
  {
		if(this.controls.timing.enabled)
		{
//		  this.controls.timing.hText.set('text', 'Další obrázek za '+this.timeout+'s.');
		  var fx = new Fx.Morph(this.controls.timing.hGraph, {duration: 700, unit: '%'});


		  fx.start({width: (this.timeout/(this.options.delay/1000)*100)+'%'});
		}
  },


	aRnd: function()
	{
		return (Math.floor(Math.random()*5));
	},
	rnd: function(min, max)
	{
		return Math.random()*(max - min);
	}

});






// ---------------------------------------------------------------------------------------------------
// Form Valid
// ---------------------------------------------------------------------------------------------------
var _uiFormValidator = new Class(
{
	orig: null,

  initialize: function(e)
  {
		var formValidator = new Form.Validator(e,
		{
			evaluateFieldsOnBlur: false,
			evaluateFieldsOnChange: true
		});

		formValidator.addEvent('formValidate', function(passed, form, ev)
		{
		  if(!passed)
		  {
		    alert('Formulář není správně vyplňen');
		  }
		});

		formValidator.add('cc_atleast1',
		{
		    errorMsg: 'Vyplňte toto pole',
		    test: function(el)
				{
		        if(!el.value.length) return false;
		        else return true;
		    }
		});
		formValidator.add('cc_atleast4',
		{
		    errorMsg: 'Vyplňte aspoň 4 znaky',
		    test: function(el)
				{
		        if(el.value.length<4) return false;
		        else return true;
		    }
		});
		formValidator.add('cc_pass',
		{
		    errorMsg: 'Hesla nesouhlasí',
		    test: function(el)
				{
		        if(el.value.length<4) return false;
		        else return true;
		    }
		});
		formValidator.add('cc_email',
		{
		    errorMsg: 'Špatně zadaný formát emailu',
		    test: function(el)
				{
		        if(el.value.match(new RegExp('^\\w(\\w|-|\\.)*@(\\w|-|\\.)+\\.\\w+$'))==null) return false;
		        else return true;
		    }
		});
		formValidator.add('cc_phone',
		{
		    errorMsg: 'Špatně zadaný formát telefonního čísla',
		    test: function(el)
				{
		        if(el.value.match(new RegExp('^((\\+|00){0,1}\\d{3} {0,1}){0,1}\\d{3} {0,1}\\d{6}$'))==null) return false;
		        else return true;
		    }
		});
		formValidator.add('cc_phone_short', //bez predcisli
		{
		    errorMsg: 'Špatně zadaný formát telefonního čísla',
		    test: function(el)
				{
		        if(el.value.match(new RegExp('^\\d{9}$'))==null) return false;
		        else return true;
		    }
		});
		formValidator.add('cc_postal',
		{
		    errorMsg: 'Špatně zadaný formát poštovního čísla',
		    test: function(el)
				{
		        if(el.value.match(new RegExp('^\\d{3} {0,1}\\d{2,3}$'))==null) return false;
		        else return true;
		    }
		});
		formValidator.add('cc_login',
		{
		    errorMsg: 'Špatně zadaný login',
		    test: function(el)
				{
		        if(el.value.match(new RegExp('^\\w*$'))==null) return false;
		        else return true;
		    }
		});
  }
});



