document.observe('dom:loaded', function(){
	$$('.banner-under-gallery').each(function(el) {
		if (el.getHeight() < 15) el.setStyle({'display': 'none'});
		console.log (el);
		console.log (el.getHeight());
	});
});



var ComboBoxMaker = {
		
	work: function() {
	$$('label.mm-select').each(function (el) {
		var select = el.down('select');
		//vytvorim potrebne elementy
		input = new Element('span', {'class': 'input'}).insert(new Element('input', {
			'id'   : select.id,
			'name' : select.name,
			'value': select.value,
			'type' : 'hidden',
			'class' : 'h'
		})).insert(new Element('span', {'class': 'data'}).update(select.options[select.selectedIndex].text));
		var button = new Element('span', {'class': 'button'});
		
		//vytvorim list ktory bude v tipe
		var list = new Element('span', {'class': 'list'});
		$w(select.className).each(function (className) {list.addClassName(className)});
		select.childElements().each(function (opt) {
			var option = new Element('span').update(opt.innerHTML);
			if (opt.selected) option.addClassName('selected');
			$w(opt.className).each(function (className) {option.addClassName(className)});
			
			// nastavenie hoveru
			option.observe('mouseover', function (e) {
				e.stop();
				this.addClassName('active');
			}.bindAsEventListener(option));
			option.observe('mouseout', function (e) {
				e.stop();
				this.removeClassName('active');
			}.bindAsEventListener(option));
			option.onclick = opt.ondblclick;
			
			// nastavenie clicku -> skrytie tipu, nastavenie hodnoty inputu
			option.observe('click', function (e, value) {
				e.stop();
				var target = e.element();
				this.prototip.hide();
				document.stopObserving('click', this.prototip.fx);
				this.down('span.input').down('input').value = value;
				this.down('span.input').down('span.data').update(target.innerHTML);
				target.up().childElements().invoke('removeClassName', 'selected');
				target.addClassName('selected');
			}.bindAsEventListener(el, opt.value));
			
			list.insert(option);
		});
		
		list.insert('<b></b>');
		
		Element.remove(select);
		el.insert(input).insert(button);
		
		new Tip(el, list, {
			hook: {target: 'bottomLeft', tip: 'topLeft'},
			showOn: 'click',
			hideOn: false,
			width: 'auto',
			offset: { x: 0, y: 2}
		});
		el.prototip.combobox = true;
		
		el.removeClassName('mm-select');
		
	});
	
	}
}


Event.observe(window, 'load', function() {
	mmList();
	mmDropdown();
	mmCenterise();
	ComboBoxMaker.work();
	
	document.observe('prototip:shown', function(event) {
		var tip = event.element().prototip;
		if (tip.combobox == true){
			event.stop();
			tip.fx = function(event) {
				event.stop();
				this.prototip.hide();
				document.stopObserving('click', tip.fx);
			}.bindAsEventListener(event.element());
			document.observe('click', tip.fx);
		}
	});	
});


function mmCenterise() {
	$$('.mm-centerise').each(function(element) {

		_width = (element.up().getWidth() - element.getWidth()) / 2; 
		_height = (element.up().getHeight() - element.getHeight()) / 2;

		element.setStyle({
			left: _width + 'px', 
			top: _height + 'px'
		});
	});

	$$('.mm-centerise-2').each(function(element) {

		_width = (element.up().up().getWidth() - element.getWidth()) / 2; 
		_height = (element.up().up().getHeight() - element.getHeight()) / 2;

		element.setStyle({
			left: _width + 'px', 
			top: _height + 'px'
		});
	});
}

function mmList() {
	$$('.b-list .i, #b-user-list .i').each(function(element) {
		Event.observe(element, 'mouseover', function(){
			element.addClassName('active');
		}); 

		Event.observe(element, 'mouseout', function(){
			element.removeClassName('active')
		}); 
	});
}

Event.observe(window, 'load', function() {
	$$('.dropdown-list a').each(function(element) {
		element.observe('click', function() {
			mmList();
		});
	});
});

function mmClear() {
	Tips.hideAll();

	$$('.b-list .i, #b-user-list .i').each(function(element) {
		element.removeClassName('active');
	});
}

function mmDropdown() {
	$$('.i-dropdown').each(function(element) {
		new Tip(element, element.down('.dropdown-list'), {
			showOn: 'click',
			/*hideOn: 'click',*/
			hideOn: { element: 'body', event: 'click' },
			/*hideAfter: 1,*/
			hook: { target: 'bottomRight', tip: 'topRight' },
			offset: { x: 4, y: -3 },
			hideOthers: true,
			width: 'auto'
		});
	});

	$$('.i-dropdown').each(function(element) {
		element.removeClassName('i-dropdown-show');

		element.observe('prototip:shown', function() {
			$$('.b-list .i, #b-user-list .i').each(function(element2) {
				element2.stopObserving();
				document.onclick = mmClear;
			}); 
		}); 

		element.observe('prototip:hidden', function() {
			mmList();
		}); 
	}); 
}

	Event.observe(window, 'load', function() {
	$$('.new-combo').each(function(element) {
		new ComboBox(element, {
			id: 'share-service',
			title: 'skuska',
			empty: false
		});
	});
/*
	var calendar = new Calendar();
	calendar.getData();
	calendar.displayData();
	calendar.renderCalendar();
	*/
});

var ComboBox = Class.create({
	root: null,
	label: null,
	ul: null,
	list: [],
	attrs: [],
	
	initialize: function(element, attrs) {
		this.root = element;
		if (!Object.isUndefined(attrs)) {
			this.attrs = attrs;
		}
		this.attrs.title = Object.isUndefined(this.attrs.title) ? '' : this.attrs.title;
		this.attrs.id = Object.isUndefined(this.attrs.id) ? '' : this.attrs.id;
		this.attrs.empty = Object.isUndefined(this.attrs.empty) ? false : this.attrs.empty;
		this.generateList();
		this.generateLabel();
		this.root.insert({
			'after': this.ul
		});
		this.root.insert({
			'after': this.label
		});
	//		this.root.remove();
	},
	generateList: function() {
		options = this.root.getElementsByTagName('option');
		for (i = 0; i < options.length; i++) {
			this.list[i] = {
				key: options[i].getAttribute('value'),
				value: options[i].textContent,
				click: options[i].getAttribute('onDblClick')
			};
		}
		ul = new Element('ul');
		for (i = 0; i < this.list.length; i++) {
			item = new Element('li', {
				id: this.list[i].key,
				onDblClick: this.list[i].click
			}).update(this.list[i].value);
			item.observe('click', this.select.bindAsEventListener(this));
			ul.appendChild(item);
		}
		ul.hide();
		this.ul = ul;
	},
	generateLabel: function() {
		label = new Element('label', {
			id: this.attrs.id
		});

		input = new Element('input', {
			type: 'text',
			title: this.attrs.title,
			name: this.root.getAttribute('name')
		});
		label.appendChild(input);

		img = new Element('img', {
			src: 'img/input-icon.png',
			alt: this.attrs.title,
			title: this.attrs.title
		});
		img.observe('click', this.toggle.bindAsEventListener(this));
		label.appendChild(img);
		
		if (!this.attrs.empty) {
			label.down('input').value = this.list.first().value;
		}
		this.label = label;
	},
	select: function(event) {
		element = event.findElement();
		this.label.down('input').value = element.textContent;
		this.hide();
	},
	show: function() {
		this.ul.show();
	},
	hide: function() {
		this.ul.hide();
	},
	toggle: function() {
		if (this.ul.visible()) {
			this.hide();
		}
		else {
			this.show();
		}
	}
});

var Calendar = Class.create({
	date: new Date(),
	data: null,
	program: null,
	renderCalendar: function() {
		startDate = new Date(this.date.getFullYear(), this.date.getMonth(), 1);
		endDate = new Date(this.date.getFullYear(), this.date.getMonth() + 1, 0);
		if (startDate.getDay() == 0) {
			startDate.setDate(startDate.getDate() - 6);
		}
		else if (startDate.getDay() != 0) {
			startDate.setDate(startDate.getDate() - startDate.getDay() + 1);
		}
		weeks = Math.ceil((endDate - startDate) / (7 * 86400 * 1000));
		rows = [];
		for (i = 0; i < weeks; i++) {
			cells = [];
			for (j = 0; j < 7; j++) {
				if (this.program[startDate.getFullYear() + '-' + ('0' + (startDate.getMonth() + 1)).substr(-2) + '-' + ('0' + startDate.getDate()).substr(-2)]) {
					children = Builder.node('a', startDate.getDate());
					children.observe('click', this.showDayItems.bindAsEventListener(this, startDate));
				}
				else {
					children = startDate.getDate();
				}
				attribute = null;
				if ([0, 6].indexOf(startDate.getDay()) !== -1) {
					attribute = {
						'class': 'end'
					};
				}
				cells.push(Builder.node('td', attribute, children));
				startDate = new Date((startDate * 1) + 86400000);
			}
			rows.push(Builder.node('tr', cells));
		}
		thead = Builder.node('thead', [Builder.node('th', 'Po'), Builder.node('th', 'Ut'), Builder.node('th', 'St'), Builder.node('th', 'Št'), Builder.node('th', 'Pi'), Builder.node('th', 'So'), Builder.node('th', 'Ne'), ])
		$('calendar').appendChild(Builder.node('table', {
			summary: 'calendar'
		}, [thead, Builder.node('tbody', rows)]));
	},
	getData: function() {
		url = 'feed.php';
		new Ajax.Request(url, {
			method: 'get',
			asynchronous: false,
			onComplete: function(xmlHttp) {
				Calendar.data = xmlHttp;
			}
		});
	},
	displayData: function() {
		this.data = Calendar.data;
		days = this.data.responseXML.getElementsByTagName('day');
		programs = [];
		for (i = 0; i < days.length; i++) {
			key = days[i].getAttribute('date');
			programs[key] = days[i].childNodes;
		}
		this.program = programs;
	},
	showDayItems: function(event, date) {
		items = this.program[date.getFullYear() + '-' + ('0' + (date.getMonth() + 1)).substr(-2) + '-' + ('0' + date.getDate()).substr(-2)];
		lists = [];
		for (i = 0; i < items.length; i++) {
			lists[i] = Builder.node('li', items[i].textContent);
		}
		Builder.node('ul', {
			id: 'program-list'
		}, lists);
	}
});


//
//	Trieda pre mazanie defaultnych textov v inputoch
//	 pri zanechani prazdneho inputu sa naspet vrati hodnota na podovnu
//
var InputCleaner = Class.create({
	inputId: '',
	input: null,
	oldValue: '',
	initialize: function(inputId) {
		this.inputId = inputId;
		this.input = $(this.inputId);
		if (this.input != null) {
			this.oldValue = this.input.getValue();
			this.input.observe('focus', this.onActivate.bindAsEventListener(this));
			this.input.observe('blur', this.onDeactivate.bindAsEventListener(this));
		}
	},
	onActivate: function(event) {
		if (this.input.getValue() == this.oldValue) {
			this.input.setValue('');
		}
	},
	onDeactivate: function(event) {
		if (this.input.getValue() == '') {
			this.input.setValue(this.oldValue);
		}
	}
});

//
//  Trieda pre nastavenie akcie inputu ktora umozni po klinkuti na input oznacit cely jeho obash
//  pouziva sa pri inputoch kde je embed kod videa, alebo url
//
var InputSelector = Class.create({
	inputId: '',
	intput: null,
	initialize: function(inputId) {
		this.inputId = inputId;
		this.input = $(this.inputId);
		if (this.input != null) {
			this.input.observe('click', this.onActivate.bindAsEventListener(this))
		}
	},
	onActivate: function(event) {
		this.input.focus();
		this.input.select();
	}
});


var SameSizeResizer = Class.create({
	initialize: function(items) {
		var maxSize = 0;
		for (i = 0; i < items.length; i++) {
			var dim = items[i].getDimensions();
			if (maxSize < dim.height) maxSize = dim.height;
		}
		for (i = 0; i < items.length; i++) {
			var dim = items[i].getDimensions();
			var rozdiel = maxSize - dim.height;
			
			if (rozdiel > 0) {
				var padding = items[i].getStyle('padding-bottom');
				alert(padding);
				items[i].setStyle({
					'padding-bottom': padding + rozdiel
				});
			}
		//items[i].setStyle({height: maxSize + 'px'});
		}
	}
});

setInterval("settime()", 1000);

function settime ()
{
	var curtime = new Date();

	var curhour = curtime.getHours();
	var curmin = curtime.getMinutes();
	var cursec = curtime.getSeconds();
	var time = "";

	//if(curhour == 0) curhour = 12;
	time = (curhour == 0 ? "00" : curhour) + ":" +
	(curmin < 10 ? "0" : "") + curmin + ":" +
	(cursec < 10 ? "0" : "") + cursec;

	if ($('jstime')) $('jstime').innerHTML = time;
}

function compareBoxes(box1, box2)
{
	var obj1 = $(box1).down('ul');
	var obj2 = $(box2).down('ul');
	if (!obj1 || !obj2) return;
	if (obj1.getHeight() != obj2.getHeight())
	{
		var objHeight = Math.max(obj1.getHeight(), obj2.getHeight());
		obj1.style.height = objHeight + 'px';
		obj2.style.height = objHeight + 'px';
	}
}


// Funkcia ktora sa pusta ked chce niekto vstupit do kategorie nad 18 rokov
// pre foto alebo video a ked "potvrdi" ze ma 18+

function controlAge(cn, cv, domain)
{
    if (!domain) domain = '';
    var jar = new CookieJar({
		expires: 0,
		path: '/',
		domain: domain
	});
	
	jar.appendString = '';
	jar.put(cn, cv);
	

    return true;
}


var RegionWeather = Class.create({
	areaMaps: [],
	actualRegion: 1,
	actualHoverRegion: 0,
	initialize: function() {
		this.areaMaps = $$('#map area');
		var i = 0;
		for (i = 0; i < this.areaMaps.length; i++) {
			var area = this.areaMaps[i];
			Event.observe(area, 'click', this.areaClick.bindAsEventListener(this));
			Event.observe(area, 'mouseover', this.areaIn.bindAsEventListener(this));
		}
		Event.observe($('map'), 'mouseout', this.areaOut.bindAsEventListener(this));
		
		// ak ma nastavenu cookies tak vyberie automaticky kraj
		var cj = new CookieJar();
		var actualRegion = cj.get('rg');
		if (actualRegion) this.selectRegion(actualRegion);

		this.hideUlElem();
	},
	hideUlElem: function (){
		var aRegion = this.actualRegion;
		var ulEl = $$('.weather');
		var compRegion = 'weather-region-' + aRegion;
		ulEl.each(function(item){
			var compUlId = item.id;
			if (compRegion != compUlId)
			{
				item.style.display = 'none';
			} else {
				item.style.display = 'block';
			}
		})
	},
	areaIn: function(event) {
		var area = Event.findElement(event, 'area');
		this.actualHoverRegion = area.id.replace("r", "");
		this.hoverRegion(this.actualHoverRegion);
		return false;
	},
	areaOut: function(event) {
		this.hoverRegion(0);
		return false;
	},
	areaClick: function(event) {
		var area = Event.findElement(event, 'area');
		Event.stop(event);
		this.actualRegion = area.id.replace("r", "");
		this.selectRegion(this.actualRegion);

		// zmeni zobrazeny prehlad pocasia podla daneho kraja
		this.hideUlElem();

		return false;
	},
	hoverRegion: function(region) {
		var imagesubMap = $('imagesubmap');
		for (i = 1; i <= this.areaMaps.length; i++) {
			imagesubMap.removeClassName('r' + i);
		}
		imagesubMap.addClassName('r' + region);
	},
	selectRegion: function(region) {
		// vyznacime na mape
		// zobrazime pocasie a clanky
		var imageMap = $('imagemap');
		for (i = 1; i <= this.areaMaps.length; i++) {
			imageMap.removeClassName('r' + i);
			var regionArticles = $('region-articles-' + i);
			if (regionArticles) regionArticles.style.display = 'none';
			var regionArticle = $('region-articles-' + region);
			if (regionArticle) regionArticle.style.display = 'block';
		}
		imageMap.addClassName('r' + region);

		this.areaOut();

		// zmeni header
		var regionLabel = $('region-label-' + region);
		if (regionLabel) $('region-label').update(regionLabel.innerHTML);
		// zmeni href odkazu na pocasie daneho kraja
		var regionLink = $('region-link-' + region);
		if (regionLink) $('region-link').setAttribute('href',regionLink.innerHTML);

		var cj = new CookieJar();
		cj.put('rg', region);
	}
});

var Vote = {
	add : function(channel, clip)
	{
		new Ajax.Request('services/ClipVote.php', {
			method: 'get',
			parameters: { 'channel' : channel, 'clip' : clip }, 
			onSuccess : function(t) {
				try {
					eval(t.responseText);
					alert(_da['message']);
				}
				catch (error) {}
			}
		})
	}
};
