// http://davidwalsh.name/dwclickable-entire-block-clickable-mootools-12
// dwClickable: Entire Block Clickable Using MooTools 1.2

var dwClickables = new Class({
	
	//implements
	Implements: [Options],

	//options
	options: {
		elements: $$('div'),
		selectClass: '',
		anchorToSpan: false
	},
	
	//initialization
	initialize: function(options) {
		//set options
		this.setOptions(options);
		//set clickable
		this.doClickables();
	},
	
	//a method that does whatever you want
	doClickables: function() {
		
		//for all of the elements
		this.options.elements.each(function(el) {
			
			//get the href
			var anchor = el.getElements('a' + (this.options.selectClass ? '.' + this.options.selectClass : ''))[0];
			
			//if we found one
			if($defined(anchor)) {
				
				//add a click event to the item so it goes there when clicked. 
				this.setClick(el,anchor.get('href'));
				
				//modify anchor to span if necesssary
				if(this.options.anchorToSpan) {
					var span = new Element('span',{
						text: anchor.get('text')
					}).replaces(anchor);
				}
			}
			
		},this);
	},
	
	//ads the click event
	setClick: function(element,href) {
		
		element.addEvent('click', function() {
			window.location = href;
		});
	}
	
});

// JavaScript Document

var Site = {
	  
		init: function(){
			
			  this.container = $('main-content');
			  
        /* target="_blank" replacement for all elements (not only a) */
        $$('._blank').each(function(element){
				    element.addEvent('click', function(event){
						    event.stop();
								window.open(element.get('href'));
						});
				});
				
				/* Giant link */
				$('giant').getElement('span').setStyle('cursor', 'pointer').addEvent('click', function(event){
				    event.stop()
						window.open('http://www.giant.cz');
				});
				
				/* Clickable boxes */
				this.clickableBoxes = $$('.gallery-categories li');
				if (this.clickableBoxes){
					  this.clickableBoxes.setStyle('cursor', 'pointer');
						this.clickableBoxes.each(function(element){
								element.addEvent('click', function(event){
										window.location = element.getElement('a').get('href');
								});
						});
				}
				
				/* SmoothScroll */
				new SmoothScroll({
					duration: 700,
				  transition: Fx.Transitions.Quint.easeInOut
			  });
				
				
				
				/* Homepage Flash */
				if ($('flashx')){
					$('flash-replacement').destroy();
				//	new Swiff('flash-hp.v2.swf', {
					new Swiff('flash-hp.v2.swf', {
						id: 'homepage-flash',
						container: 'flash',
						width: 980,
						height: 270,
						params: {
								wmode: 'opaque'
						}
					});
				}
				
				/* Flash Koulovačka hlavní sekce */

				if ($('flash-koulovacka-sekce')){
					new Swiff('/flash-hp.v19.swf', {
						id: 'dzs-koulovacka-sekce-swf',
						container: 'flash-koulovacka-sekce',
						width: 540,
						height: 149,
						params: {
								wmode: 'opaque'
						}
					});
				}				
				
				/* Flash Koulovačka podstránky */
				if ($('flash-koulovacka-podstranky')){
					new Swiff('/flash/jarni-akce-180x170-3.swf', {
						id: 'dzs-koulovacka-podstranky-swf',
						container: 'flash-koulovacka-podstranky',
						width: 180,
						height: 170,
						params: {
								wmode: 'opaque'
						}
					});
				}					
			
				/* Flash prohlidka */
				if ($('flash-prohlidka')){
					$('get-flash').destroy();
					new Swiff('/prohlidka/prohlidka.swf', {
						id: 'flash-prohlidka-swf',
						container: 'flash-prohlidka',
						width: 740,
						height: 460,
						params: {
								wmode: 'opaque',
								allowfullscreen: 'true'
						}
					});
				}
				
				/* Flash Dny zdraveho spani */
				if ($('flash-zdrave-spani')){
					new Swiff('/dny-zdraveho-spani/dzs-banner.swf', {
						id: 'dzs-banner-swf',
						container: 'flash-zdrave-spani',
						width: 172,
						height: 172,
						params: {
								wmode: 'opaque'
						}
					});
				}
				
				/* Otevirani dveri */
				if ($('flash-nk-pantyx')){
					new Swiff('/vestavene-skrine/otevirani/flash/oteviraci-nk-dvere.swf', {
					  container: 'flash-nk-panty',
						width: 238,
						height: 389,
						params: {
							  wmode: 'transparent'
						}
					});
					$('flash-nk-panty').setStyle('margin', '0px auto');
				}
				
				if ($('posuvne-dvere-v-ramux')){
					new Swiff('/vestavene-skrine/otevirani/flash/posuvne-dvere-v-ramu.swf', {
					  container: 'posuvne-dvere-v-ramu',
						width: 238,
						height: 389,
						params: {
							  wmode: 'transparent'
						}
					});
					$('posuvne-dvere-v-ramu').setStyle('margin', '0px auto');
				}
				
				if ($('otocne-dvere-v-ramux')){
					new Swiff('/vestavene-skrine/otevirani/flash/otocne-dvere-v-ramu.swf', {
					  container: 'otocne-dvere-v-ramu',
						width: 238,
						height: 389,
						params: {
							  wmode: 'transparent'
						}
					});
					$('otocne-dvere-v-ramu').setStyle('margin', '0px auto');
				}
				
				// Sample room list
				if ($('sample-rooms')){
					var sampleRoomFx = new Fx.Slide('sample-rooms-list', { duration: 500, link: 'cancel', transition: Fx.Transitions.Quad.easeInOut });
					sampleRoomFx.hide();
					$('sample-rooms').setStyles({
					  'text-decoration': 'underline',
						'cursor': 'pointer'
					});
					$('sample-rooms').addEvent('click', function(){
						sampleRoomFx.toggle();
					});
				}
				
				// toggler
				if ($$('.toggler').length){
					this.togglers = this.container.getElements('.toggler');
					this.slides = this.container.getElements('.toggled');
					
					this.togglers.each(function(element,index){
					 var slideFx = new Fx.Slide(this.slides[index], { duration: 500, link: 'cancel', transition: Fx.Transitions.Quad.easeInOut }).hide();
					 element.getElement('a').addEvent('click', function(event){ event.preventDefault(); });
					 element.addEvent('click', function(){slideFx.toggle()});
					},this);
				}

		}
};

var Zoomer = {
	  init: function(){
			  ReMooz.assign('.img-table a, .remooz, .gallery a, .gallery-caption a', {
					'origin': 'img',
					'shadow': 'onOpen', // fx is faster because shadow appears after resize animation
					'resizeFactor': 0.9, // resize to maximum 80% of screen size
					'cutOut': false, // don't hide the original
					'opacityResize': 0, // opaque resize
					'dragging': true, // disable dragging
					'centered': true // resize to center of the screen, not relative to the source element
				});
		}
};




var FormValidation = {
	  init: function(){
			  forms = $$('#form-contact, #form-order', '#form-designer');
				forms.each(function(element){
				  element.addEvent('submit', function(event){
					    event.stop();
							var err = new Array();
							
							var name  = $('form-name');
							var phone = $('form-phone');
							var email = $('form-email');
							var city  = $('form-city');
							
							var fields = new Array();
							if (name.value  == '') fields.include('Jméno');
							if (phone.value == '') fields.include('Telefon');
							if (email.value == '') fields.include('E-mail');
							if (city.value  == '') fields.include('Město');
							
							if (fields.length > 0)
							{
								  err.include('Je nutné vyplnit následující pole:\n' + fields.join(', '));
							}
							
							if (email.value)
							{
								  var mail = email.value.replace(/^([a-zA-Z0-9])+([\.a-zA-Z0-9_-])*@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-]+)+/, 'valid');
									if (mail != 'valid')
									{
										  err.include('Zadaná e-mailová adresa je neplatná');
									}
							}
							
							if (phone.value)
							{
								  var tel = phone.value.replace(' ', '').replace(/\+?[0-9]{9,20}/, 'valid');
									if (tel != 'valid')
									{
										  err.include('Zadané telefonní číslo je neplatné');
									}
							}
							
							if (err.length > 0)
							{
								  alert(err.join('\n\n'));
							} else {
								  element.submit();
							}
					});
			  });
		}
 };

var Mapa = {
	
	  init: function(){
			 
		   
			 var field = $('mapa-cr-wrapper');
			 
			 field.getElement('img').setOpacity(0.001);
			 


			 
			 // kraje
			 var areas = $('mapa-cr-large').getElements('area');
			 
			 var kraje = new Hash({
						'zlinsky'         : [1, 2],
						'praha'           : [2, 1],
						'kralovehradecky' : [3, 2],
						'vysocina'        : [1, 0],
						'jihocesky'       : [0, 2],
						'karlovarsky'     : [2, 0],
						'plzensky'        : [3, 1],
						'jihomoravsky'    : [0, 3],
						'moravskoslezsky' : [2, 3],
						'olomoucky'       : [1, 3],
						'pardubicky'      : [2, 2],
						'liberecky'       : [0, 1],
						'ustecky'         : [3, 0],
						'stredocesky'     : [1, 1]
			 });
			 
			 var bubbles = new Hash({
					  'zlinsky'         : [317, 151],
						'praha'           : [52,  57],
						'kralovehradecky' : [217,  35],
						'vysocina'        : [198, 121],
						'jihocesky'       : [116, 142],
						'karlovarsky'     : [ 34,  27],
						'plzensky'        : [ 53,  84],
						'jihomoravsky'    : [251, 157],
						'moravskoslezsky' : [339,  92],
						'olomoucky'       : [288, 109],
						'pardubicky'      : [233,  80],
						'liberecky'       : [172,  -6],
						'ustecky'         : [105,   4],
						'stredocesky'     : [163,  54]
			 });
			 
			 var bubbleFxs =  [];

       areas.each(function(element){
          var kraj = element.getProperty('href').replace('#', '').replace('-kraj', '');
								 // bubble
	 			  var bubble = new Element('p', { 'class' : 'bubble' });
				  //bubble.setHTML('<span>' + element.getProperty('title').replace('|', '<br />') +'</span>');
					bubble.set('html', '<span>' + element.getProperty('title').replace('|', '<br />') +'</span>');
					// remove title and alt atributes
					element.removeProperties('title', 'alt');
					bubble.setStyles({
					    'left' : bubbles.get(kraj)[0],
							'top'  : bubbles.get(kraj)[1]
					});
				  var bubbleFx = new Fx.Tween(bubble, { property: 'opacity', duration: 200, wait: false });
				  bubbleFx.set(0);
				  bubble.inject(field);
					bubbleFxs.push(bubbleFx);
					
					element.addEvent('mouseenter', function(event){
					    Mapa.move(kraje.get(kraj), field);
							bubbleFx.start(1);
					});
					
					element.addEvent('mouseleave', function(event){
							Mapa.move([0, 0], field); 
							bubbleFx.start(0);
					});
			 });
			 
			 var selectedRow = '';
			
		},
		
		move: function(coords, field){
			var col = coords[0];
			var row = coords[1];
			var width = 422;
			var height = 259;
			field.setStyle('background-position', (-422*col) + 'px ' + (-259*row) + 'px');
		}
		
};

window.addEvent('domready', function(){
	
	Site.init();
	
	Zoomer.init();
	
	FormValidation.init();
	
	var clix = new dwClickables({
		elements: $$('.clickable'),
		anchorToSpan: false
	});
	
	
	if($('srotovne')) { 
		$('srotovne').addEvent('mouseover', function(event){
			$('srot-logo').addClass('hover-srot');
		});
		$('srotovne').addEvent('mouseout', function(event){
			$('srot-logo').removeClass('hover-srot');
		});
	}
	if($('block-srotovne')) { 
		$('block-srotovne').addEvent('mouseover', function(event){
			$('srot-logo').addClass('hover-srot');
		});
		$('block-srotovne').addEvent('mouseout', function(event){
			$('srot-logo').removeClass('hover-srot');
		});
	}
	
	if ($('mapa-cr-wrapper')) Mapa.init();

});
