var vu = 'ajax-vote.html'; // vote service url 

var msgAjaxError = 'Vzdialená strana neodpovedá. Skúste znova.';
var msgUploadError = 'Video sa nepodarilo odoslať. Skúste znova.';
var msgAnalError = 'Chýbaju dáta!';
var msgCommentError = 'Musíte vyplniť text komentára!';
var msgLoginError = 'Musíte byť prihlásený!';
var msgConfirmDelete = 'Naozaj chcete zmazať video?';

var dbgFlag = false; // magic debugging

var documenttitle;

	document.observe('dom:loaded', function(){
	documenttitle = document.title;
	centerInit(true);											// vertical center
	submitInit();											// form blocker
	scrollInit();											// custom scroll
	playlistInit();										// check for cookies
	tabInit();												// tabs
	galleryInit();										// loading fullsize from thumbnails
	voteInit(); 											// comment voting
	
});

Event.observe(window, 'load', function() { 
	centerInit(false);											// vertical center
});



function dbg(output) {
	/*
	if (dbgFlag) console.log(output);
*/
}
function log(output) {
}

function onUploadError(flashID, reply) {
	alert('Chyba pri odosielaní!');
}

function onUploadComplete(flashID, reply) {
	if (window.DOMParser) {
		parser = new DOMParser();
		xmlDoc = parser.parseFromString(reply, "text/xml");
	} else { // Internet Explorer
		xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
		xmlDoc.async="false";
		xmlDoc.loadXML(reply);
	} 

	var path = xmlDoc.getElementsByTagName("result")[0].attributes.getNamedItem("url").value;
	
	$('post_video_code').value = path;
	$$('.submit').each(function(el) { el.removeClassName('submit-disabled'); });
}

function onUploadStart(flashID) {
	$$('.submit').each(function(el) { el.addClassName('submit-disabled'); });
}

function onFileSelect(flashID) {
	$$('.submit').each(function(el) { el.addClassName('submit-disabled'); });
}

function submitInit () {
	dbg('submitInit');

	$$('.b-registration .submit').each(function(el){
		el.observe('click', function(ev){
			el = ev.element();

			if (el.hasClassName('submit-disabled')) { 
				ev.stop();
			}
		})
	});

	dbg('submitInit');
}

function setCookie(cn, cv, ce) {
	var ex = new Date();
	ex.setDate(ex.getDate() + ce);
	document.cookie=cn+'='+escape(cv)+((ce==null) ? '' : ';expires='+ex.toGMTString())+';path=/';
}

function getCookie(cn) {
	if (document.cookie.length > 0) {
		ca = document.cookie.indexOf(cn + '=');

		if (ca != -1) {
			ca = ca + cn.length + 1;
			cb = document.cookie.indexOf(';', ca);
			if (cb == -1) cb = document.cookie.length;
			return unescape(document.cookie.substring(ca, cb));
		}
	}

	return '';
}

function playlistInit() {
	dbg('playlistInit');

	if($$('.playlist-play').size()) {
		$$('.playlist-play').first().observe('click', function(ev) {
			el = ev.element();
			el.hide();
			$$('.playlist-stop').first().show();
			setCookie('playingthelist', '', 365);
			window.location = $$('.j-playlist-load').first().readAttribute('href');
		});
		
		$$('.playlist-stop').first().observe('click', function(ev) {
			el = ev.element();
			el.hide();
			$$('.playlist-play').first().show();
			setCookie('playingthelist', '1', 365);
		});
		
		if (getCookie('playingthelist') == '') {
				$$('.playlist-play').first().hide();
		} else {
				$$('.playlist-stop').first().hide();
		}	
	}

	dbg('playlistInit');
}

function onPlaybackStart(id) {
	dbg('onPlaybackStart'); 

	id = Number(id);
	el = $('j-playlist-'+id);

	el.up('.i').addClassName('active');
	
	dbg('onPlaybackStart');
}

function onPlaybackFinish(id) {
	dbg('onPlaybackFinish');

	id = Number(id);
	pl = $$('.j-playlist-load');
	el = $('j-playlist-'+id);
	
	if((el != pl.last()) && (getCookie('playingthelist') != '1')) {
		window.location = el.up('li').next('li').down('.j-playlist-load').readAttribute('href');
	} else {
		setCookie('playingthelist', '1', 365);
	}

	dbg('onPlaybackFinish');
}

function voteInit() {
	dbg('voteInit');

	$$('.i-voting').each(function(el){
		if (!el.hasClassName('i-voting-disabled')) {
			el.down('.up').observe('click', function(ev){
				ev.stop();
				voteAjax(el, 1);
			});
			el.down('.down').observe('click', function(ev){
				ev.stop();
				voteAjax(el, -1);
			});
		} else {
			el.down('.up').observe('click', function(ev){ ev.stop(); });
			el.down('.down').observe('click', function(ev){ ev.stop(); });
		}
	});

	dbg('voteInit');
}

function galleryUpdate(ls, el, move) {
	dbg('galleryUpdate');
	if (!el.hasClassName('j-gallery-load')) el = el.up();
	if (move != 0) el = ls.detect(function(el) { if (el.hasClassName('active')) return el; });
	if (move == -1) {
		if (!el.up().previous()) {
			imageObjects = (document.getElementsByClassName('j-gallery-load'));
			el = imageObjects[imageObjects.length-1];
		} else {
			el = el.up().previous().down();
		}
	}
	if (move == 1) {
		if (!el.up().next()) {
			imageObjects = (document.getElementsByClassName('j-gallery-load'));
			el = imageObjects[0];
		} else {
			el = el.up().next().down();
		}
	}
	galleryManage(ls, el);
	ls.invoke('removeClassName', 'active');
	el.addClassName('active');
	src = el.readAttribute('href');
	tit = el.readAttribute('title');
	ids = el.readAttribute('id');
	$('gallery-player').writeAttribute('src', src);
	$('gallery-player').writeAttribute('title', tit);
	$('gallery-title').update(tit);
	
	document.title = tit + ' | ' + documenttitle;
	
	dbg('galleryUpdate');
}

function galleryManage(gl, el) {
	
	/*
	bp = $$('#photo .previous_button').first();
	bn = $$('#photo .next_button').first();

	if (el.up().previous()) bp.removeClassName('previous_button_disabled');
	else bp.addClassName('previous_button_disabled');
	
	if (el.up().next()) bn.removeClassName('next_button_disabled');
	else bn.addClassName('next_button_disabled');
	*/
	
	$$('#photo .m-move small').first().update(gl.indexOf(el) + 1);
}
	
function galleryInit() {
	dbg('galleryInit');
	
	imageObjects = (document.getElementsByClassName('j-gallery-load'));
	if (imageObjects.length == 0) {
		return;
	}

	if (imageObjects.length == 1) {
		return;
	} else {
		document.getElementById('gallery-player').style.cursor = 'pointer';
	}
	
	if ($$('.j-gallery-load').length) {
		gl = $$('.j-gallery-load');
		
		wl = window.location.href;
		if(wl.replace('#photo-', '') != wl) {
			idec = 'photo-'+wl.split('#photo-').last();

			if ($(idec) != undefined) galleryUpdate(gl, $(idec).down(), 0);
			else galleryUpdate(gl, $$('.j-gallery-load.active').first(), 0);
		}

		$$('#photo .previous_button').first().observe('click', function(ev){
			el = ev.element();
			ev.stop();
			if (!el.hasClassName('previous_button_disabled')) galleryUpdate(gl, $$('#gallery-gallery .l .active').first(), -1);
		});
		
		$$('#gallery-player').first().observe('click', function(ev){
			el = ev.element();
			ev.stop();
			if (!el.hasClassName('next_button_disabled')) galleryUpdate(gl, $$('#gallery-gallery .l .active').first(), 1);
		});
		

		$$('#photo .next_button').first().observe('click', function(ev){
			el = ev.element();
			ev.stop();
			if (!el.hasClassName('next_button_disabled')) galleryUpdate(gl, $$('#gallery-gallery .l .active').first(), 1);
		});
		
		gl.each(function(el){
			
			el.observe('click', function(ev){
				el = ev.element();
				ev.stop();
				galleryUpdate(gl, el, 0);
			});
		});
		
	}

	dbg('galleryInit');
}

function centerInit(load) {
	dbg('centerInit');

	if (load) {
		$$('.j-center').invoke('addClassName', 'hideout');
	} else {
		$$('.j-center').each(function(el){
			_top = (el.up().getHeight() - el.getHeight()) / 2;
			_left = (el.up().getWidth() - el.getWidth()) / 2;
			el.removeClassName('hideout');
			el.setStyle({ 'top': _top+'px', 'left': _left+'px' });
		});
	}

	dbg('centerInit');
}

function tabInit() {
	dbg('tabInit');

	$$('.j-tab').each(function(el){
		if (el.down('.active') != undefined) {
			cn = el.down('.active').down('a').readAttribute('href').split('=').last();
			ok = true;
		} else ok = false;

		new Control.Tabs(el);

		if (ok) Control.Tabs.findByTabId(cn).setActiveTab(cn);  
	});

	dbg('tabInit');
}

function scrollInit() {
	dbg('scrollInit');

	bd = $$('.j-scroll');
	bd.each(function(el){
		CSBfleXcroll(el.identify());
		el.removeClassName('hideout');
		if (!el.hasClassName('active')) el.hide();
	});

	dbg('scrollInit');
}

function voteAjax(el, vt) {
	dbg('voteAjax');

	new Ajax.Request(vu, {
		method: 'post',
		parameters: {
			comment: el.up('.i').identify(),
			vote: vt
		},
		onCreate: function() { el.addClassName('i-voting-spell'); },
		onFailure: function() { el.removeClassName('i-voting-spell'); alert(msgAjaxError); },
		onSuccess: function (rq) {
			el.removeClassName('i-voting-spell');
			el.addClassName('i-voting-disabled');
			el.update(rq.responseText);
			[el.down('.up'), el.down('.down')].each(function(el){
				el.observe('click', function(ev){
					ev.stop();
				});
			});
		}
	});

	dbg('voteAjax');
}


document.observe('dom:loaded', function() {
	$$('#s-menu .item').each(function(el) {
		if (el.down('.tip-target') != undefined) {
			new Tip(el.down('.tip-target'), el.down('.tip-popup'), {
				hook: { tip: 'topLeft', target: 'bottomLeft' },
				offset: { x: 0, y: 0 },
				showOn: 'mouseover',
				hideOn: { element: 'tip', event: 'mouseleave' },
				hideAfter: 1,
				hideOthers: true,
				border: 0,
				radius: 0,
				delay: 0,
				width: 454
			});

			el.down('.tip-target').observe('prototip:shown', function(el) {
				el.element().addClassName('selected');
			});

			el.down('.tip-target').observe('prototip:hidden', function(el) {
				el.element().removeClassName('selected');
			});
		}

		el.observe('mouseover', function(el) {
			//Tips.hideAll();
		});
	});

	$$('#send-target').each(function(el) {
		new Tip(el, $('send-popup'), {
			hook: { tip: 'bottomLeft', target: 'topLeft' },
			offset: { x: 0, y: -10 },
			showOn: 'click',
			hideOn: { element: '.close', event: 'click' },
			hideAfter: false,
			hideOthers: true,
			border: 0,
			radius: 0,
			delay: 0,
			width: 454
		});
	});
});


function onUploadError(flashID, reply) {
	alert('Chyba pri odosielaní!');
}

function onUploadComplete(flashID, reply) {
	if (window.DOMParser) {
		parser = new DOMParser();
		xmlDoc = parser.parseFromString(reply, "text/xml");
	} else { // Internet Explorer
		xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
		xmlDoc.async="false";
		xmlDoc.loadXML(reply);
	} 

	var path = xmlDoc.getElementsByTagName("result")[0].attributes.getNamedItem("url").value;
	
	$('post_video_code').value = path;
	
	/*
	var videoExt = path.split('.');
	videoExt = videoExt[videoExt.length-1];

	path = path.split('/');
	var dir = path[path.length-2]; 

	var originalFilename = xmlDoc.getElementsByTagName("result")[0].attributes.getNamedItem("originalFilename").value;

	document.getElementById('videoTitle').value = originalFilename;
	document.getElementById('uploadUrl').value = dir;
	document.getElementById('videoExt').value = videoExt;

	$$('.flash-uploader, .flash-,essage')each(function(el){ el.toggle(); });
	*/
	
	$$('.submit').each(function(el){ el.removeClassName('submit-disabled'); });
}

function onUploadStart(flashID) {
	$$('.submit').each(function(el){ el.addClassName('submit-disabled'); });
}

function onFileSelect(flashID) {
	$$('.submit').each(function(el){ el.addClassName('submit-disabled'); });
}

function submitInit () {
	dbg('submitInit');

	$$('.submit').each(function(el){
		el.observe('click', function(ev){
			el = ev.element();

			if (el.hasClassName('submit-disabled')) { 
				ev.stop();
			}
		})
	});

	dbg('submitInit');
}


