// Event.observe(window, 'load', myLightbox.start(document.getElementById('anchor_{shownPicture.id}')), false);

// BEGIN rok.krulec@
// rok.krulec@
// todo: treba nardit funkcijo, da pobere vse id-je v formu in jih napravi za URL
// todo: OK security ?@!@# to gre vse skozi GET == unsafe
// todo: neka metoda, ki to z nekim MD5 challengeom vse prenasa
function caCollect(items) {
        var i = 0;
        var result = '';
        while (i < items.length) {
                if(document.getElementById(items[i]) != null) {
                        result = result + '&' + urlEncode(items[i])+ '=' + urlEncode(document.getElementById(items[i]).value);
                }
                i++;
        }
        return result;
}
// rok.krulec@

/* ---------------------- AJAX ------------------------- */

// Kako to uporabit:
// vsem elementom, ki jih hocemo preko Ajaxa vlect dol damo <a href="link" rel="ca_neki">
// neki je ime div-a kjer hocemo, da se tisto pokaze
// <body onLoad="caTags()"> sparsa vse rel tage in jim pripravi onClick
// v HTTP_Get_Callback sem vtaknu, da vsakic ko kej z Ajaxom potegne se enkrat sparsa caTags, ker je nova vsebina
//
// vsem formom, ki imajo id = 'ca_neki', jih pripravi za avtomatski ajax submit
var caTags = function () {

        if (!document.getElementsByTagName){ return; }
        var anchors = document.getElementsByTagName('a');

        for (var i=0; i<anchors.length; i++){
                var anchor = anchors[i];

                var relAttribute = String(anchor.getAttribute('rel'));

                // use the string.match() method to catch 'ca_' references in the rel attribute
                if (anchor.getAttribute('href') && (relAttribute.toLowerCase().match('ca_'))){
//			Event.observe(anchor.getAttribute('id'),'click',
			   anchor.onclick = function(e) {
                                var targ;
                                if (!e) var e = window.event;
                                if (e.target) targ = e.target;
                                else if (e.srcElement) targ = e.srcElement;
                                if (targ.nodeType == 3) // defeat Safari bug
                                        targ = targ.parentNode;

// Pizda kako imam poln kurac tega Javascripta in spletnega dreka
				
				// Da ne pofuram onClick pofuram onKeyDown in ga sprozim iz kle. KeyDown na anchorju itak...
				if(targ.onkeydown) {
					var kd_result = targ.onkeydown();
				} else kd_result = true;
                                if(kd_result == true) {
                                        ajFetch(targ.getAttribute('href'),targ.getAttribute('rel').replace('ca_',''));
					return false; // tole more bit obvezno kle, drugace sprozi full-screen redirect
                                } else return false;
                           }
                }
        }

        var forms = document.getElementsByTagName('form');
        for (var i=0; i<forms.length; i++){
                var form = forms[i];
                var idAttribute = String(form.getAttribute('id'));
//alert(idAttribute);
//		form.unbind("submit");
                if (form.getAttribute('action') && (idAttribute.toLowerCase().match('ca_'))){

                // pofukan ENTER v IE6 // kdo je car ?
                var inputs = Form.getInputs(form,'text');
                for (var r=0; r<inputs.length; r++) {
                  var r_input = inputs[r];
                //  alert(r_input.name);
                  r_input.onkeydown = function(e) { 
                    if (!e) var e = window.event;
                    if(e.keyCode == Event.KEY_RETURN) {
                        var IE6 = false /*@cc_on || @_jscript_version <= 5.7 @*/;
                        if(IE6) return false; // Don't submit form if IE6
                    }
                  }
                }

                        var submits = Form.getInputs(form,'submit');
//alert(submits.first().getAttribute('name'));
                        submits.first().onclick = function(e) {
                                var targ;
                                if (!e) var e = window.event;
                                if (e.target) targ = e.target;
                                else if (e.srcElement) targ = e.srcElement;
                                if (targ.nodeType == 3) // defeat Safari bug
                                        targ = targ.parentNode;

//								alert(targ.parentNode.action);
//                                alert($(targ.parentNode.id).serialize(false));
                                
                                ajFetchPost(targ.parentNode.action,$(targ.parentNode.id).serialize(),targ.parentNode.id.replace('ca_',''));

                                return false;
                         }
                }

        }
};

Event.observe(window, 'load', caTags);

/* Ajax function class */
/* Declarations */
var arAjx = new Array(1);
var arStops = new Array;

/* ajFetch function */
function ajFetch(url, destination, cycle, delay, noloading) {
        /* Execution timer */
        var timerOne = new Date();
        floatInt = timerOne.getTime();

        var curInstance;
        if (cycle == null) {
                cycle = false;
        }
        if (noloading == null) {
                noloading = false;
        }
        if (delay == null) {
                delay = 0;
        }
        if (arAjx.length == 1) {
                curInstance = 1;
                arAjx[1] = new caAjax;
        } else {
                var found = false;
                for (i=1;i<arAjx.length;i++) {
                        if (arAjx[i].Is_Free()) {
                                curInstance = i;
                                found = true;
                        }
                }

                if (!found) {
                        arAjx.push(new caAjax);
                        curInstance = arAjx.length;
                        arAjx[curInstance] = new caAjax;
                }
        }

        if (noloading) { arAjx[curInstance].Set_Loading(false); }

//      if (paused) {
//              arAjx[curInstance].HTTP_Get(url+"&pause=1", destination);
//      } else {
                arAjx[curInstance].HTTP_Get(url, destination);
//      }

        if (arStops[destination] != true) {
                if (cycle) {
                        setTimeout("ajFetch('"+url+"','"+destination+"',true,"+delay+");",delay);
                }
        } else {
                arStops[destination] = null;
        }
}

/* ajFetch POST function */
function ajFetchPost(url, params, destination, cycle, delay, noloading) {

        /* Execution timer */
        var timerOne = new Date();
        floatInt = timerOne.getTime();

        var curInstance;
        if (cycle == null) {
                cycle = false;
        }
        if (noloading == null) {
                noloading = false;
        }
        if (delay == null) {
                delay = 0;
        }
        if (arAjx.length == 1) {
                curInstance = 1;
                arAjx[1] = new caAjax;
        } else {
                var found = false;
                for (i=1;i<arAjx.length;i++) {
                        if (arAjx[i].Is_Free()) {
                                curInstance = i;
                                found = true;
                        }
                }

                if (!found) {
                        arAjx.push(new caAjax);
                        curInstance = arAjx.length;
                        arAjx[curInstance] = new caAjax;
                }
        }

        if (noloading) { arAjx[curInstance].Set_Loading(false); }

//      if (paused) {
//              arAjx[curInstance].HTTP_Get(url+"&pause=1", destination);
//      } else {
                arAjx[curInstance].HTTP_Post(url, params, destination);
//      }

        if (arStops[destination] != true) {
                if (cycle) {
                        setTimeout("ajFetch('"+url+"','"+destination+"',true,"+delay+");",delay);
                }
        } else {
                arStops[destination] = null;
        }
}

/* Ajax functions, objects, classes and callback */
function caAjax() {
        /* Declarations */
        /* Public methods */
        this.HTTP_Get = HTTP_Get;
        this.HTTP_Post = HTTP_Post;
        this.Is_Free = Is_Free;
        this.Set_Loading = Set_Loading;

        /* Private variables */
        var ajobj = XMLHTTP_object();
        var http_destobj = null;

        var free = true;
        var show_loading = true;

        /* Methods */

        // rok.krulec@
        // tole je neka priredba po domace, pomoje bo tole treba s prototype/framework klici zamenjat
        function HTTP_Post(url, params, destination) {

                if (url != null) {
                  url = url + '&chromeless=yes';
                }
//                alert('get='+url);

                if (free && (url != null) && (destination != null) && ((ajobj.readyState == 0) || (ajobj.readyState == 4))) {
                        http_destobj = destination;

                        ajobj.open("POST", url, true);
                        ajobj.onreadystatechange = HTTP_Get_CallBack;
                        ajobj.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT");
                        ajobj.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
                        ajobj.setRequestHeader("Content-length", params.length);
                        ajobj.setRequestHeader("Connection", "close");

                        ajobj.send(params);
                        free = false;
                } else {
                        return false;
                }
        }
        // rok.krulec@

        function HTTP_Get(url, destination) {
//alert('HTTP_Get='+url);

                // rok.krulec@
                if (url != null) {
                  url = url + '&chromeless=yes';
                }
//                alert('get='+url);
                // rok.krulec@

                if (free && (url != null) && (destination != null) && ((ajobj.readyState == 0) || (ajobj.readyState == 4))) {
                        http_destobj = destination;

                        ajobj.open("GET", url, true);
                        ajobj.onreadystatechange = HTTP_Get_CallBack;
                        ajobj.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT");
                        ajobj.send(null);
                        free = false;
                } else {
                        return false;
                }
        }

        /* Get CallBack (Handle received data) */
        function HTTP_Get_CallBack() {

//                alert('HTTP_Get_Callback() readyState'+ajobj.readyState);
                if (ajobj.readyState == 4) {
                        var results = ajobj.responseText;

                        if (typeof(results)=='string' && results != null && results != '') {
                                /* Function callback evaluator */
                                if (eval("typeof(" + http_destobj + ") == 'function'")) {
                                        /* Function detected, pass data to function */
                                        eval(http_destobj + "(results);");
                                } else {
                                        /* Not a function, pass data to the html object */
					if(http_destobj)
                                        if (typeof document.getElementById(http_destobj).innerHTML != "undefined") {
						if(http_destobj != null)
                                                document.getElementById(http_destobj).innerHTML = results;
                                        }
                                }
                        }
                        free = true;

                        // rok.krulec@
                        // todo: tole se preveckrat pozene, ker gre v rekurzijo (na oko se ne opazi, samo sux)
                        // highlight: Felis Navidad in Slider vse najboljse za rojstni dan :)))
                        // higlight: Kdo je CAR ? Tantadruj je CAR !!!
                        // dober link: http://en.wikipedia.org/wiki/XMLHttpRequest
                        //
                        // getResponseHeader dela, samo ce je readyState == 4 (jebote Bill)
                        var refresh_header = ajobj.getResponseHeader('refresh');
                        if (refresh_header) {
                               refresh_header = refresh_header.substring(refresh_header.indexOf('=')+1);
//                               alert('refresh='+refresh_header);
                               HTTP_Get(refresh_header, http_destobj);
                        }
                        // rok.krulec@

                // rok.krulec@
                // reload rel tags when we get something using ajFetch
                caTags();
                // rok.krulec@

                }
        }

        /* Helpers functions */
        function Is_Free() {
                return free;
        }

        function Set_Loading(value) {
                show_loading = value;
        }

        /* AJAX OBJECT */
        function XMLHTTP_object() {
          var xmlhttp;
          /*@cc_on
          @if (@_jscript_version >= 5)
                try {
                  xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
                } catch (e) {
                  try {
                        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
                  } catch (E) {
                        xmlhttp = false;
                  }
                }
          @else
          xmlhttp = false;
          @end @*/
          if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
                try {
                  xmlhttp = new XMLHttpRequest();
                } catch (e) {
                  xmlhttp = false;
                }
          }
          return xmlhttp;
        }
}

function ajStop(destination) {
        arStops[destination] = true;
}

