// JavaScript Document

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'         : [238,  80],
						'praha'           : [ 95,  27],
						'kralovehradecky' : [176,   3],
						'vysocina'        : [135,  58],
						'jihocesky'       : [ 82,  65],
						'karlovarsky'     : [ 24,   0],
						'plzensky'        : [ 48,  27],
						'jihomoravsky'    : [177,  75],
						'moravskoslezsky' : [222,  42],
						'olomoucky'       : [203,  37],
						'pardubicky'      : [144,  35],
						'liberecky'       : [129, -20],
						'ustecky'         : [ 87, -18],
						'stredocesky'     : [117,  15] 
			 });

       areas.each(function(element){
          //var kraj = element.getProperty('href').replace('/obrazovky/', '').replace('-kraj', '');
					var kraj = element.get('id');
				  // bubble
	 			  var bubble = new Element('p', { 'class' : 'bubble' });
				  bubble.set('html', '<span>' + element.getProperty('title') +'</span>');
					bubble.setStyles({
					    'left' : bubbles.get(kraj)[0],
							'top'  : bubbles.get(kraj)[1]
					});
				  var bubbleFx = new Fx.Tween(bubble, { property: 'opacity', duration: 200, link: 'cancel' });
				  bubbleFx.set(0);
				  bubble.inject(field);
					
					element.addEvent('mouseenter', function(event){
					    Mapa.move(kraje.get(kraj), field);
							bubbleFx.start(0.8);
					});
					
					element.addEvent('mouseleave', function(event){
							Mapa.move([0, 0], field); 
							bubbleFx.start(0);
					});
					
					element.addEvent('click', $lambda(false));
			 });
			
		},
		
		move: function(coords, field){
			var col = coords[0];
			var row = coords[1];
			var width = 275;
			var height = 170;
			field.setStyle('background-position', (-width*col) + 'px ' + (-height*row) + 'px');
		}
		
};

var SlideChanger = {
	
	init: function(){
		
		this.durationFx = 500;
		
		this.container = $('slide-changer');
		this.main = this.container.getElement('#main-slide');
		this.mainFx = new Fx.Morph(this.main, { duration: this.durationFx, link: 'cancel' });
		this.mainFx.set({'left' : 0, 'opacity' : 1 });
		
		this.slides = this.container.getElements('.slide');
		this.links = this.main.getElements('.more a');
		this.slideFx = new Hash();
		this.slides.each(function(slide){
			slide.setStyle('display', 'block');
      var id = slide.get('id');
			var fx = new Fx.Morph(slide, { duration: this.durationFx, link: 'cancel' });
			fx.set({'left': 500, 'opacity': 0});
			this.slideFx.set(id, fx);
			var backBtn = slide.getElement('.back a');
			if (backBtn){
				backBtn.addEvent('click', function(event){
				  event.stop();
					this.hide(id);
				}.bind(this));
			}
		}, this);
		
		this.links.each(function(link){
		  var id = link.get('href').replace('#','');
			var fx = this.slideFx.get(id);
			link.addEvent('click', function(event){
			  event.stop();
				this.show(id);
			}.bind(this));
		}, this);
		
		// ReMooz
		$$('.gallery a').each(function(link){
			new ReMooz(link, {
				resizeOptions : {
					duration: 400,
					transition: Fx.Transitions.Quart.easeOut
				},
				origin: link.getElement('img'),
				opacityResize: 0,
				cutOut: false,
				centered: true
			});	
		});
		
		
	},
	
	show: function(id){
		var fx = this.slideFx.get(id);
		fx.set({'left' : 200, 'opacity': 0});
    fx.start({ 'left' : 0, 'opacity' : 1});
	  this.mainFx.start({ 'left' : -200, 'opacity' : 0});
	},
	
	hide: function(id){
		var fx = this.slideFx.get(id);
		fx.start({ 'left' : 200, 'opacity' : 0});
	  this.mainFx.start({ 'left' : 0, 'opacity' : 1});
	}
	
};

var XLMedia = {
	
	init: function(){
		
		if ($('mapa-cr-wrapper')) Mapa.init();
		
		// Homepage flash
		if ($('homepage-flash')){
			var flashOff = Cookie.read('flashOff');
			//var flashOff = false;
			if (!flashOff){
				this.hide();
				new Swiff('/flash/homepage.swf',{
					container: 'homepage-flash-source',
					width: 2000,
					height: 1250
				});
				if (Browser.Plugins.Flash.version){
					this.show();
				}
        Cookie.write('flashOff','true', document.URL.replace('http://','').replace('/',''));
			} else {
				$('homepage-flash').dispose();
				this.show();
			}
		}
		
		// antispam
		if ($('form-contact')){
			this.formContact = $('form-contact');
			this.antiSpamWrapper = this.formContact.getElement('p.anti').destroy();
			this.antiSpamField = new Element('input', { type: 'hidden', value: 'xlmedia', name: 'anti' }).inject(this.formContact, 'top');
			$('cRef').value = $defined(cRef) ? cRef : '';
			$('cUtmSource').value = $defined(cUtmSource) ? cUtmSource : '';
			$('cUtmMedium').value = $defined(cUtmMedium) ? cUtmMedium : '';
			$('cUtmCampaign').value = $defined(cUtmCampaign) ? cUtmCampaign : '';
		}
		
		if ($('slide-changer')) SlideChanger.init();
		
		// fake emails
		$$('.erplc').each(function(email){
			var value = email.get('text').split('').reverse().join('');
			var mailto = new Element('a', { 'href' : 'mailto:'+value, 'text': value }).replaces(email);
		});
		
		// flash replacement
		if ($('flash-rplc')){
			new Swiff('/flash/main.swf',{
			  container: 'flash-source',
				width: 500,
				height: 502
			});
		}
		// flash replacement
		if ($('video-rplc')){
			new Swiff('/flash/video-player.swf',{
			  container: 'hp-video',
				width: 430,
				height: 300,
				params: {
        	flashvars: $H(flashvars).toQueryString(),
					scale: "noscale",
        	allowfullscreen: "true",
					salign: "tl",
					bgcolor: "111111"
    		}
			});
		}
		
		if ($('logo')){
			new Swiff('/flash/logo'+((Math.random()*6).toInt()+1).toString()+'.swf',{
				container: 'logo',
			  width: 188,
				height: 140
			});
		}
		
		if ($('image')){
			$('image').setStyle('background-image', 'url(/img/bg0'+((Math.random()*6).toInt()+1).toString()+'.jpg)');
		}
		
		/* GIANT */
		if ($('footer')){
			var footer = $('footer');
			if ($(document.body).getSize().x < 1230){
				footer.addClass('narrow');
			}
			var giantLink = footer.getElement('a');
			var giantLogo = footer.getElement('img');
			giantLogo.addEvent('click', function(event){
			  window.location = giantLink.get('href');
			}).setStyle('cursor', 'pointer');
		}
		
		this.show.delay(2000);
		
		
	},
	
	show: function(){
		$('page-wrapper').setStyle('visibility', 'visible');
		$(document.body).removeClass('plain');
	},
	hide: function(){
		$('page-wrapper').setStyle('visibility', 'hidden');
		$(document.body).addClass('plain');
	}
	
};


window.addEvent('domready', function(){
  XLMedia.init();
});

/* flash intro communication */
function introDeactivator() {
	if ($('homepage-flash')) {		
		$('homepage-flash').setStyle('display','none');
	}
}
