var states = new Array();
var upperiodical;
var uploadid;
window.addEvent('load', function()
{

if ($$('.cmtp')) { var Tipz = new Tips($$('.cmtp'), { className: 'cmtpx', fixed: true }); }
if ($$('.cmtm')) { var TipzM = new Tips($$('.cmtm'), { className: 'cmtmx', fixed: true }); }
if ($$('.marktip')) { var TipzMark = new Tips($$('.marktip'), { className: 'markx', fixed: true }); }
if ($$('.mmtip')) { var TipzMark = new Tips($$('.mmtip'), { className: 'mmx', fixed: false }); }
if ($$('.custinp')) { var TipzMark = new Tips($$('.custinp'), {
	className: 'cipop',
	initialize:function(){
		this.fx = new Fx.Style(this.toolTip, 'opacity', {duration: 5000, wait: false}).set(0);
	},
	fixed: true,
	onShow: function (toolTip) { this.fx.set(1); this.fx.start(0).delay(2000); }
});

}

        if ($('chatroom')) {
		var mz = (function() {
                initChat();
		});
		mz.periodical(4000);
        }

	if ($('infobox')) {
		$('infobox').style.display = "none";
	}
if ($('registerform')) {
$('registerform').addEvent('submit', function(e) {
	new Event(e).stop();
	this.send({ onComplete: registerParse });
});
}

});




function parseChat(data) {
	var cfields = data.split('<><><>');
	$('chatroom').setHTML(cfields[0]);
	$('chatpeople').setHTML(cfields[1]);
}

function initChat() {
		var mv = new Ajax("/pisi.i?ajax=1&ls=1&chan=0", { method: 'get', onComplete: parseChat });
		mv.request();
}



function registerParse(txt) {
	var data = txt.split('::');
	if (data[0] == 1) {
		modal('modal', 'NULL');
		var boxa = new Element("div");
		boxa.setProperty("id", "modalbox");
		boxa.addClass("modalbox");
		boxa.injectAfter($('modal'));
		boxa.setHTML(data[1]);
	} else {
                modal('modal', 'NULL');
                var boxa = new Element("div");
                boxa.setProperty("id", "modalbox");
                boxa.addClass("modalbox");
                boxa.injectAfter($('modal'));
                boxa.setHTML(data[1]);

	}
}

function enabler(where, what, offx, offy, speed) {
	var coords = $(where).getPosition();
	var x = coords['x'];
	var y = coords['y'];
	what = $(what);	
	what.style.left = (x + offx) + "px";
	what.style.top = (y + offy) + "px";
	what.style.position = "absolute";
	what.setOpacity(0.0);
	what.style.display = "";
	var fade =  new Fx.Style(what, 'opacity', {duration: speed, wait: false });
        fade.start(0, 0.95);

}

function disabler(whatto, speed) {
	var item = $(whatto);
        var fade =  new Fx.Style(item, 'opacity', {duration: speed, wait: false });
         fade.start(0.95, 0);

	var k = (function() { item.style.display = "none"; }).delay(speed + 10);
}

function expander(eitem) {
        var ex = $(eitem);
	if (states[eitem]) {
		states[eitem].slideIn();
	} else {
        	states[eitem]=new Fx.Slide(ex); states[eitem].hide(); ex.style.display = '';
        	states[eitem].slideIn();
	}
}

function hider(eit) {
	var ex = $(eit);
	if (states[eit]) {
		states[eit].slideOut();
	} else {
        	states[eit]=new Fx.Slide(ex); states[eit].slideOut();
	}
}


function modal(attach, text) {
	var item = $(attach);
	item.style.display="block";
	item.style.left="0px";
	item.setOpacity(0.0);
	item.style.height="100%";
        item.effect('width', {duration: 600, transition: Fx.Transitions.linear}).start(0,document.body.clientWidth);
        item.effect('opacity', {duration: 1000, transition: Fx.Transitions.linear}).start(0,0.7);
}

function modalClose(elem) {
	var item = $(elem);
        var fade =  new Fx.Style(item, 'opacity', {duration: 450, wait: false });
        fade.start(0.7, 0);
	item.style.right="0px";
        // item.effect('height', {duration: 400, transition: Fx.Transitions.linear}).start(document.body.clientHeight,0);
        item.effect('width', {duration: 400, transition: Fx.Transitions.linear}).start(document.body.clientWidth,0);

	if ($("modalbox")) { $("modalbox").remove(); } 
	var k = (function() { item.style.display = "none"; }).delay(400 + 10);
}


function selectCat(sId) {
	var item = $(sId);
	item.style.display="block";
        item.setOpacity(0.0);
        item.effect('opacity', {duration: 400, transition: Fx.Transitions.linear}).start(0,1.0);
}

function setCat(sId, text, where) {
	$(where).setHTML(text);
	$("sCat").value = sId;
	disabler('catlist',200);
}

function fwdOK() {
	alert("UspeÅno poslano");
	disabler('fwdmail',400);
}

function fwdContent(cID) {
	var k = new Ajax("/fwd.i?id=" + cID + "&to=" + $('fwdto').value, {method: 'get', onComplete: fwdOK});
	k.request();
}

function postComm(id, type) {
	var txt = $("commtxt").value;
	var z = new Ajax("/pcomm.i", {method: "post", data: $("cform"), update: $("vcomments")});
	z.request();

	hider("commbox");
}

function getContent(page, what) {
	var k = new Ajax(page, {method: 'get', update: $(what)}).request();
}


function checkFinish(final) {
	if (final == "0") {
		$("uploadReport").setHTML("Procesiram podatke...");		
	} else {
		$("uploadReport").setHTML("Preusmerjam...");
		location = ("/vsebina." + final);
	}
}

function reportUp(str) {
        var ob = eval(str);
	if ((ob.done + "") == (ob.size + "")) {
		$("uploadReport").setHTML("Procesiram podatke...");
		$clear(upperiodical);
		var zk = ( function() {
			var zka = new Ajax("/stanje/" + uploadid + "/", {method: 'get', onComplete: checkFinish });
			zka.request();
		});
		zk();
		zk.periodical(3200);
	} else {
        	var done = (ob.received/ob.size)*100;
        	$("uploadReport").setHTML(Math.round(done*100)/100 + " %");
	}
}


function trackUpload(uploadid) {
	var url = '/progress?X-Progress-ID=' + uploadid;
	var u = new Ajax(url, { method: 'get', onComplete: reportUp });
	u.request();
}

function startUp(myid) {
	var upId = $(myid).value;
	uploadid = upId;
	var zm = (function () {
		trackUpload(upId);
	});
	upperiodical = zm.periodical(3500);
}

function chatSend() {

}
