// JavaScript Document

var ViewChange = {
	
	  init: function(){
			    
					var box = $('box');
					
					// Cookie
					if (Cookie.get('viewStatus') == 'list-view') {
						if (!box.hasClass('list-view')) { box.addClass('list-view'); }
						$$('#view-switch a').toggleClass('selected');
					} else {
						if (box.hasClass('list-view')) { box.removeClass('list-view'); }
					}
					
					// switch actions
					$('box-view').onclick = function(event){
						event = new Event(event);
						event.preventDefault();
						if (box.hasClass('list-view')) { box.removeClass('list-view'); }
						$$('#view-switch a').toggleClass('selected');
						Cookie.set('viewStatus','box-view', { path: '/' });
					}
					
					$('list-view').onclick = function(event){
						event = new Event(event);
						event.preventDefault();
						if (!box.hasClass('list-view')) { box.addClass('list-view'); }
						$$('#view-switch a').toggleClass('selected');
						Cookie.set('viewStatus','list-view', { path: '/' });
					}
		}

};

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, 169],
						'praha'           : [132,  65],
						'kralovehradecky' : [217,  53],
						'vysocina'        : [198, 139],
						'jihocesky'       : [116, 160],
						'karlovarsky'     : [ 34,  45],
						'plzensky'        : [ 53, 102],
						'jihomoravsky'    : [251, 175],
						'moravskoslezsky' : [339, 110],
						'olomoucky'       : [288, 127],
						'pardubicky'      : [233,  98],
						'liberecky'       : [172,  12],
						'ustecky'         : [105,  22],
						'stredocesky'     : [163,  72] 
			 });

       areas.each(function(element){
          var kraj = element.getProperty('href').replace('/kontakty/autorizovani-prodejci-', '').replace('-kraj', '');
								 // bubble
	 			  var bubble = new Element('p', { 'class' : 'bubble' });
				  bubble.setHTML('<span>' + element.getProperty('title') +'</span>');
					bubble.setStyles({
					    'left' : bubbles.get(kraj)[0],
							'top'  : bubbles.get(kraj)[1]
					});
				  var bubbleFx = new Fx.Style(bubble, 'opacity', { duration: 200, wait: false });
				  bubbleFx.set(0);
				  bubble.injectInside(field);
					
					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);
					});
			 });
			
		},
		
		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');
		}
		
};

var BoxShow = new Class({
	
	initialize: function(container){
		this.container = container;
		this.box = this.container.getElements('div')[1];
		if (this.box){
			this.boxFx = new Fx.Style(this.box, 'opacity', { duration: 1000, wait: true });
			this.status = 1;
			this.opacityToggle.periodical(6000, this);
		}
	},

  opacityToggle: function(){
		if (this.status){
			this.boxFx.start(0);
			this.status = 0;
		} else {
			this.boxFx.start(1);
			this.status = 1;
		}
	}
});

var Video = {
	
	  init: function(){
				
				var divs = $$('#box .box');
				divs.setStyle('cursor', 'pointer'); 
				divs.each(function(element, index){
				    var divId = element.getProperty('id').replace('flash-','');
						element.addEvent('click', function(event){
						  var event = new Event(event);
							event.preventDefault();
							var so = new SWFObject(divId+'.swf', 'flash-'+divId, '320', '284', '8.0.23', '', true);
							so.addParam('wmode','transparent');
		          so.write('video-wrapper');
						});
				});

		}
};

/*var BoxShow = new Class({
	
	initialize: function(container){
		this.container = container;
		this.box = this.container.getElements('div')[1];
		if (this.box){
			this.boxFx = new Fx.Style(this.box, 'opacity', { duration: 1000, wait: true });
			this.status = 1;
			this.opacityToggle.periodical(10000, this);
		}
	},

opacityToggle: function(){
		if (this.status){
			this.boxFx.start(0);
			this.status = 0;
		} else {
			this.boxFx.start(1);
			this.status = 1;
		}
	}
});*/

var BoxShow = new Class({
	
	initialize: function(container){
		this.container = container;
		this.boxes = this.container.getElements('div');
		this.count = this.boxes.length;
		if (this.count < 2) return null;
		
		this.boxesFx = new Array();
		this.boxes.each(function(box, index){
		  this.boxesFx[index] = new Fx.Style(box, 'opacity', {  duration: 1000, link: 'chain' });
			this.boxesFx[index].set(0);
			box.setStyle('z-index', 0);
		}, this);
		
		// display first box
		this.current = this.count - 1;
		this.boxes[this.current].setStyle('z-index', 0);
		this.boxesFx[this.current].set(1);
		
		this.repeater = this.opacityToggle.periodical(10000, this);
	},

  opacityToggle: function(){
		var next = (this.current + 1)%this.count;
		this.boxes[next].setStyle('z-index', 1);
		this.boxesFx[next].start(1).chain(function(){
		  this.boxesFx[this.current].set(0);
			this.boxes[next].setStyle('z-index', 0);
			this.current = next;
		}.bind(this));
	}
});

var PdfTargetBlank = {
	  init: function(){
			  var links = $$('.product-docs a');
				links = links.filter(function(item){
				 return item.getProperty('href').test('(.*)pdf$');
				});
				links.each(function(element){
				  element.addEvent('click', function(event){
					  var event = new Event(event);
						event.stop();
						window.open(element.getProperty('href'));
					});
				});
		}
};

function hideBanner() {
	$('flash-banner').setStyle('display','none');	
}

window.addEvent('domready', function(){
	if ($('view-switch')) { ViewChange.init(); }
	
	if ($('video-wrapper')) { Video.init(); }
	
	if ($('mapa-cr-large'))  Mapa.init(); 
	
  if ($$('.boxshow')){
		$$('.boxshow').each(function(element, index){
		  (function(){ new BoxShow(element); }).delay(index*300);
		});
	}
	
  $$('.clickable').each(function(element){
	  element.setStyle('cursor', 'pointer');
		element.addEvent('click', function(event){
			var event = new Event(event);
		  event.stop();
			window.location = element.getElement('a').getProperty('href');
		});
		element.getElement('a').addEvent('click', function(event){
		  var event =  new Event(event);
			event.stopPropagation();
		});
	});
	
	PdfTargetBlank.init();
	
});

