/*
	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'))
							new _uiSlideShow(e);
					});
					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
	
	fnOnChange: null,

  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);
		  if(this.fnOnChange) this.fnOnChange(this.e);
		}.bind(this));

		this.e.setStyle('visibility', 'visible');
  },


  useWrapped: function(size)
  {
		var size = this.capsule.getSize();
		
		this.e.addEvent('change', function()
		{
		  if(this.fnOnChange) this.fnOnChange(this.e);
		}.bind(this));



		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.e.fireEvent('change');
					  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');

		window.addEvent('click', function()
		{
		   this.fxList.slideOut();
		}.bind(this));
  },
  
  onChange: function(fn)
  {
		this.fnOnChange = fn;
  }

});




// ---------------------------------------------------------------------------------------------------
// 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();

		this.e.setStyle('display', 'none');
    this.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,

	capsule: null,
	box: null,
	check: null,

	inst: null,

  initialize: function(e)
  {
   	this.e     = e;
    var parent = e.getParent();
    var size   = e.getSize();

		this.e.setStyle('display', 'none');
    this.e.addClass('ui-wrapped');

    if(!_uiRadio.inst) _uiRadio.inst = new Array();
    if(_uiRadio.inst[this.e.get('name')]) _uiRadio.inst[this.e.get('name')] = new Array();
    _uiRadio.inst[this.e.get('name')].push(this);

    this.capsule = new Element('div',
		{
			'class': 'ui-radio',
			styles:
			{
			  position: 'relative',
				width: size.x,
				height: size.y
			}
		});

		this.box = new Element('div',
		{
		  'class': 'rBox',
		  styles:
		  {
		    position: 'absolute',
		    cursor: 'pointer'
		  },
		  events:
		  {
		    click: function()
		    {
		      this.toggle();
		    }.bind(this)
		  }
		});
		this.capsule.adopt(this.box);

		this.check = new Element('div',
		{
		  text: '●',
		  'class': 'rCheck',
		  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('rLabel');
		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  = '_uiRadio'+_uiRadio.inst.length;

			this.e.set('id', id);
			eval(this.e.get('onclick').replace(new RegExp('this+', 'g'), '$(\''+id+'\')'));
			this.e.set('id', sid);
		}
  }

});




// ---------------------------------------------------------------------------------------------------
// Tree
// ---------------------------------------------------------------------------------------------------
var _uiTreeView = new Class(
{
  initialize: function(e)
  {
    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');

					  wrapper = new Element('div',
					  {
							styles:
							{

							}
					  });

					  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, wrapper: wrapper}));

					  if(!active) toggler.retrieve('fxToggle').hide();

					  // Dont know really why, but with this style IE7 dont gets crazy
					  if(Browser.ie7)
					  	wrapper.setStyle('position', 'relative');

					  wrapper.adopt(child);
					  item.adopt(wrapper);
					  item.adopt(toggler);
					  this.browse(child);
					}
				}

			}.bind(this));
		}
	}
});




// ---------------------------------------------------------------------------------------------------
// SlideShow
// ---------------------------------------------------------------------------------------------------
var _uiSlideShow = new Class(
{
	Implements: [Options],

	h: null,
	size: null,

	options:
	{
		images: true,
		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,
		onlyWidescreen: false,
		autoplay: true,

		position: {x: 'center', y: 'center'},
		menu: {enabled: false, title: false, clickable: false},

		effects:
		{
			transition: {enabled: true, els: null, morphs: null},
			slide: {enabled: false},
			move: {enabled: false, zoom: 2},
			stripes: {enabled: false, els: null, morphs: null, count: 5, width: '10%', background: '#fff', opacity: 0.3},
			bricks: {enabled: false, els: null, morphs: null, background: '#fff', opacity: 1, size: {x: 10, y: 10}}
		},

		controls:
		{
			proggress: false,
			timing: false
		},
		loading:
		{
			visible: true,
			delay: 500,
			progress: true,
			bgColor: '#fff'
		}
	},

	eDisplay: null,
	eMenu: null,
	eDescPos: null,
	eDesc: null,
	eDescInner: 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
	effects: null,


	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.options.images ? this.imgLoad(imgs[i]) : this.contentLoad(imgs[i]);

		if(this.options.menu.enabled) this.iniMenu();

		this.loadingStart();
		this.current = 0;
		if(this.options.images) this.display();

		this.timein	 = 0;
		this.timeout = this.options.delay/1000;

		if(this.images.length>1 && this.options.autoplay)
			this.hPeriod = this.autoplay.periodical(1000, this);
	},

	// ------------------------------------------------------------------------------------------------------------------------
	// Slideshow
	display: function()
	{
		if(this.options.menu.enabled)
		{
			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.eDescInner.set('text', this.images[this.current].title);
				this.eDesc.retrieve('fxMorph').start({opacity: [0,0.7]});
				this.eDesc.retrieve('fxSlide').slideIn();
			}.delay(600, this));
		}

		this.dLoadingStop = this.loadingStop.delay(this.options.loading.delay*2, this);

		if(!this.options.loading.visible) this.beginEffects();
		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(false);
		}
		else
		{
			this.timein++;
			this.timeout--;
		}

		this.controlsSetTiming();
	},
	next: function(manual)
	{
		this.endEffects();

		if(this.images.length>1 && this.options.description && this.images[this.current].title)
		{
			this.eDesc.retrieve('fxMorph').start({opacity: [0.7,0]});
			this.eDesc.retrieve('fxSlide').slideOut();
		}

		this.current++;
		if(this.current==this.count)
			this.current = 0;

		if(manual)
		{
			this.timein	= 0;
			this.timeout = this.options.delay/1000;
		}

		this.controlsSetTiming();
		this.display();
	},
	prev: function(manual)
	{
		this.endEffects();

		if(this.images.length>1 && this.options.description && this.images[this.current].title)
		{
			this.eDesc.retrieve('fxMorph').start({opacity: [0.7,0]});
			this.eDesc.retrieve('fxSlide').slideOut();
		}

		this.current--;
		if(this.current<0)
			this.current = this.count-1;

		if(manual)
		{
			this.timein	= 0;
			this.timeout = this.options.delay/1000;
		}

		this.controlsSetTiming();
		this.display();
	},
	setCurrent: function(current)
	{
		this.endEffects();

		if(this.images.length>1 && this.options.description && this.images[this.current].title)
		{
			this.eDesc.retrieve('fxMorph').start({opacity: [0.7,0]});
			this.eDesc.retrieve('fxSlide').slideOut();
		}

		this.current = current;

		this.timein	= 0;
		this.timeout = this.options.delay/1000;


		this.controlsSetTiming();
		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'
		});

		for(var i=0; i<this.count; i++)
		{
			var li = new Element('li');

			var a = new Element('a',
			{
				text: this.options.menu.title ? this.images[i].title : '',
				rel: i
			});

			if(this.options.menu.clickable)
			{
			   a.set('href', '#');
				a.addEvent('click', function(el)
				{
					this.setCurrent(el.get('rel').toInt());

					return false;
				}.bind(this, a));
			}

			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',

					'z-index': 100
				}
			});

			this.eDesc = new Element('div',
			{
				'class': 'desc'
			});

			this.eDescInner = new Element('div',
			{
				'class': 'descInner'
			});

			this.eDesc.adopt(this.eDescInner);
			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, mode: 'horizontal', 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/_uiConstrols/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()
	{
	   this.effects   = new Object();
	   var effectName = '';

		// Transition
		if(this.options.effects.transition.enabled)
		{
		   var efTransition    = this.options.effects.transition;
			efTransition.els    = new Array();
			efTransition.morphs = new Array();

		   effectName = 'transition';
		   this.effects[effectName] = new Object();

			this.effects[effectName].fnB = function()
			{
			   var efTransition = this.options.effects.transition;

				this.images[this.current].h.setStyle('opacity', 0);
				efTransition.morphs[this.current] = new Fx.Morph(this.images[this.current].h, {duration: this.options.loading.delay*3});
				efTransition.morphs[this.current].start({opacity: [0,1]});
			}.bind(this);


			this.effects[effectName].fnE = function()
			{
            var current = this.current;

			   if(this.images.length>1)
			   {
					var efTransition = this.options.effects.transition;

					if(efTransition.morphs.length)
					{
						efTransition.morphs[current].start({opacity: [1,0]});
						//this.images[current].h.setStyle('z-index', this.images[current].h.getStyle('z-index').toInt()+1);

						(function()
						{
							this.dispose();
							//this.setStyle('z-index', 0);
						}.delay(this.options.loading.delay*3, this.images[current].h));
					}
				}

			}.bind(this);
		}


		// Slide
		if(this.options.effects.slide.enabled)
		{
		   effectName = 'slide';
		   this.effects[effectName] = new Object();


			this.effects[effectName].fnB = 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].h.width: this.images[this.current].width, this.images[this.current].left]
				});

			}.bind(this);
		}


		// Move
		if(this.options.effects.move.enabled)
		{
         effectName = 'move';
		   this.effects[effectName] = new Object();

			this.effects[effectName].fnB = function()
			{
				if(this.images.length>1)
				{
				   var isImgSmallerV = this.images[this.current].h.width<(this.size.x*this.options.effects.move.zoom);
				   var isImgSmallerH = this.images[this.current].h.height<(this.size.y*this.options.effects.move.zoom);

				   var multiply = isImgSmallerV || isImgSmallerH;

					var w = this.images[this.current].h.width*(multiply ? 1 : this.options.effects.move.zoom);
					var h = this.images[this.current].h.height*(multiply ? 1 : this.options.effects.move.zoom);

	 				this.images[this.current].h.setStyles(
					{
						width: w,
						height: h
					});

					var centerV = this.size.x/2-w/2;
					var centerH = this.size.y/2-h/2;

					// check performance on this
					new Fx.Morph(this.images[this.current].h, {duration: this.options.delay+(this.options.loading.delay*5)}).start({
						top: isImgSmallerH ? [centerH, centerH] : [-this.rnd(0,h-this.size.y), -this.rnd(0,h-this.size.y)],
						left: isImgSmallerV ? [centerV, centerV] : [-this.rnd(0,w-this.size.x), -this.rnd(0,w-this.size.x)]
					});
				}
			}.bind(this);
		}

		// Stripes
		if(this.options.effects.stripes.enabled)
		{
		   effectName       = 'stripes';

			var efStripes    = this.options.effects.stripes;
			efStripes.els    = new Array();
			efStripes.morphs = new Array();

			this.effects[effectName] = new Object();

		   for(var i=0; i<efStripes.count; i++)
		   {
		      efStripes.els[i] = new Element('div',
				{
				   styles:
				   {
				      position: 'absolute',
				      top: 0,
				      bottom: 0,
				      width: efStripes.width,
				      left: this.rnd(0,this.size.x),

				      background: efStripes.background,
				      opacity: efStripes.opacity,
				      'z-index': 10
				   }
				});

				this.eDisplay.adopt(efStripes.els[i]);

				efStripes.morphs[i] = new Fx.Morph(efStripes.els[i], {duration: this.options.delay+this.options.loading.delay});
			}

			this.effects[effectName].fnB = function()
			{
			   var efStripes = this.options.effects.stripes;
			   var l         = efStripes.els.length;

			   for(var i=0; i<l; i++)
			   {
					efStripes.morphs[i].start(
					{
						left: [efStripes.els[i].getStyle('left').toInt(), this.rnd(0, this.size.x)]
					});
			   }

			}.bind(this);
		}

		// Bricks
		if(this.options.effects.bricks.enabled)
		{
		   effectName = 'bricks';
		   this.effects[effectName] = new Object();

			var efBricks    = this.options.effects.bricks;
			efBricks.els    = new Array();
			efBricks.morphs = new Array();

			var w = this.size.x/efBricks.size.x;
			var h = this.size.y/efBricks.size.y;

			var i = 0;
			var offset = 0;
		   for(var x=0; x<efBricks.size.x; x++)
		   {
			   for(var y=0; y<efBricks.size.y; y++)
			   {
			      efBricks.els[i] = new Element('div',
					{
					   styles:
					   {
					      position: 'absolute',
					      top: y*w,
					      width: w,
					      height: h,
					      left: x*h,

					      background: 'no-repeat -'+(x*h)+'px -'+(y*w)+'px',
					      opacity: efBricks.opacity,
					      'z-index': 10
					   }
					});

					this.eDisplay.adopt(efBricks.els[i]);

					efBricks.morphs[i] = new Fx.Morph(efBricks.els[i], {duration: this.options.loading.delay+550+(x*this.rnd(0, 100))});

					i++;
				}
			}

			this.effects[effectName].fnB = function()
			{
			   var efBricks = this.options.effects.bricks;
			   var l         = efBricks.els.length;

			   for(var i=0; i<l; i++)
				{
				   efBricks.els[i].setStyle('background-image', 'url(\''+this.images[this.current].h.src+'\')');
					efBricks.morphs[i].start({opacity: [0, efBricks.opacity]});
				}

			}.bind(this);

			this.effects[effectName].fnE = function()
			{

			   var efBricks = this.options.effects.bricks;
			   var l         = efBricks.els.length;

			   for(var i=0; i<l; i++) efBricks.morphs[i].start({opacity: [efBricks.opacity, 0]});
			}.bind(this);
		}
	},

	beginEffects: function()
	{
		for(var i in this.effects)
			if(this.effects[i].fnB) this.effects[i].fnB();
	},

	endEffects: function()
	{
		for(var i in this.effects)
			if(this.effects[i].fnE) this.effects[i].fnE();
	},


	// ------------------------------------------------------------------------------------------------------------------------
	// 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
			}
		});

		this.images.push(data);

		data.h.addEvent('load', function(image)
		{
			this.loaded++;
			this.controlsSetProgress();

			//var image = this.images[i];

			if(this.options.onlyWidescreen)
			{
			   if(image.h.width<image.h.height)
			   {
				   image.h.destroy();
				   this.images.erase(image);
				   this.count = this.images.length;

				   return;
			   }
			}


			image.width	= image.h.width;
			image.height = image.h.height;


			// Adjust size to boxing element
			if(this.options.adjustSize)
			{
				image.ratio = image.h.width>image.h.height ? image.h.width/this.size.x : image.h.height/this.size.y;

				image.h.width	/= image.ratio;
				image.h.height /= image.ratio;
			}

			// Center both vertical and horizontal or other
			switch(this.options.position.x)
			{
			   case 'center':	image.left = this.size.x/2-image.h.width/2; break;
			   case 'left':	image.left = 0; break;
			   case 'right':	image.left = this.size.x-image.h.width; break;
			   default:			image.left = this.options.position.x; break;
			}
			switch(this.options.position.y)
			{
			   case 'center':	image.top = this.size.y/2-image.h.height/2; break;
			   case 'top':		image.top = 0; break;
			   case 'bottom':	image.top = this.size.y-image.h.height; break;
			   default:			image.top = this.options.position.y; break;
			}
			
			image.h.setStyles(
			{
				top: image.top,
				left: image.left
			});

			if(this.options.clickToFullSize)
			{
				image.h.setStyle('cursor', 'pointer');
				image.h.addEvent('click', function()
				{
					Mediabox.open(this.h.src, this.h.alt, 'mediabox['+this.width+' '+this.height+']');
				}.bind(image));
			}

			//document.id('debug').innerHTML += '<p>'+JSON.encode(image)+'</p>';
			image.completed = true;
		}.bind(this, this.images[this.images.length-1]));

		/*
		data.h.addEvent('mousewheel', function()
		{
			new Request({url: this.src}).send();
		});
		*/
	},


	// ------------------------------------------------------------------------------------------------------------------------
	// Content loading
	contentLoad: function(content)
	{
		var data =
		{
			h: null,
			width: 0,
			height: 0,
			top: 0,
			left: 0,
			ratio: 0,
			title: content.title,
			hMenu: null,
			completed: true
		};

		data.h = content;

		var size = content.getSize();

		data.width  = size.x;
		data.height = size.y;

		content.setStyles(
		{
			position: 'absolute',
			top: 0,
			bottom: 0,
			left: 0,
			right: 0,
			'z-index': 0
		});

		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;
		    }
		});
  }
});



