_last_clicked_faq = '';

function trim(str, chars) { return ltrim(rtrim(str, chars), chars); }
function ltrim(str, chars){ chars = chars || "\\s"; return str.replace(new RegExp("^[" + chars + "]+", "g"), ""); }
function rtrim(str, chars){ chars = chars || "\\s"; return str.replace(new RegExp("[" + chars + "]+$", "g"), ""); }
function gns(n) { var x = n.nextSibling; while (x && x.nodeType != 1) { x = x.nextSibling; } return x; }
function ischildof(elem,parent) { var x = elem; while(x) { x=x.parentNode; if(x == parent) {return true;}} return false; }

function setCurrentFaq(faqHash) {
	var all = $('tresc1').getElementsByTagName('a');
	
	for(a in all) {
		if(String(all[a].href).indexOf(faqHash) > -1) {
			var element = all[a];
			element.style.color = "white";
			element.style.background = "#DE0000";
			element.style.padding = "3px";
			_last_clicked_faq = element;
			
			window.heightSet = -element.parentNode.parentNode.offsetTop+26;
			window.setTimeout("$('tresc1').style.top = heightSet+'px'; try { scroll_lewy.reset(); } catch (e) {}", 2000);
		}
	}
	return true;
}


function ajaxGoTo(link, content_div){
    advAJAX.get({
        url: "/" + link + "/ajax/1",
        unique: false,
        onSuccess: function(obj){
        
            eval('try { var notka = ' + obj.responseText + ' } catch(e) { }');
            $(content_div).innerHTML = notka.content;
            
            var item = notka.content;
            data = item.split('\n');
            arraylen = data.length;
            var i = 0;
            try {
                while (i < arraylen) {
                    if (data[i].indexOf("script") > -1) {
                        i++;
                        while (data[i].indexOf("/script") < 0) {
                            if (data[i] != '' && data[i] != "\r") {
                                _data = trim(data[i]);
                                if (_data.substring(0, 1) != '<') {
                                    eval('try { ' + _data + ' } catch(e) { }');
                                }
                            }
                            i++;
                        }
                    }
                    i++;
                }
            } 
            catch (e) {}
            
            try { scrol.reset(); } catch (e) { void (e.message); }
            
            target('metka_engine');
            target('a_blank');
            
            if(window.repositoryMode != 'on') {
            	setTimeout(function(){ urchin('/' + link); __gemius(notka.gemius); }, 500);
            }
            
            if($('tresc1') != null && $('tresc1')!= undefined) {
            	setCurrentFaq(link);
            }
        },
        onError: function(obj){
            alert(obj);
        }
    });
}


function showFaqTree(_last_clicked_faq, context) {
	var uls = document.getElementById('tresc1').getElementsByTagName('ul');
	for(i = 0; i < uls.length; i++ ) {
		if(uls[i].className == 'sub') {
			uls[i].style.display = 'none';
		}
	}
	
	var x = _last_clicked_faq;
	var next = gns(x);
	if(next && next.tagName == 'UL') {
		next.style.display = 'block';
	}
	
	while(x && x != context) {
		x = x.parentNode; 
		if(x && x.tagName == 'UL') {
			x.style.display = 'block'; 
		}
	}
	return true;
}


function ajaxFaqGoTo(a, link, content_div){
	
    if (_last_clicked_faq != '' && _last_clicked_faq != a) { // && link.match("/dir/")) {
        _last_clicked_faq.style.color = '';
		_last_clicked_faq.style.background = '';
		_last_clicked_faq.style.padding = '';
		_last_clicked_faq.className = '';
	}

	if(true) {//link.match("/dir/")) {
		a.style.color = "white";
		a.style.background = "#DE0000";
		a.style.padding = "3px";
		a.parentNode.parentNode.style.display = "block";
		a.className = 'selected';
		_last_clicked_faq = a;
	}

	if(window.repositoryMode == 'on') {
		showFaqTree(_last_clicked_faq);
		scroll_lewy.reset();
	}

    /* zaznaczenie nowego zestawu faq */
    window.newFAQset = true;
    
    advAJAX.get({
        url: "/" + link + "/ajax/1",
        unique: false,
		onLoading : function(obj)
        { $(content_div).innerHTML = '<img src="/images/lapa_load.gif" style="margin-left:210px; margin-top:100px;" alt="" />';},
    
        onSuccess: function(obj){
        
            eval('var notka = ' + obj.responseText);
            $(content_div).innerHTML = notka.content;
            try { scroll_prawy.reset(); } catch(except) {}
            
            var item = notka.content;
            data = item.split('\n');
            arraylen = data.length;
            var i = 0;
            while (i < arraylen) {
                if (data[i].indexOf("script") > -1) {
                    i++;
                    while (data[i].indexOf("/script") < 0) {
                        eval(data[i]);
                        i++;
                    }
                }
                i++;
            }
            
            target('metka_engine');
            target('a_blank');
            
            if(window.repositoryMode != 'on') {
            	setTimeout(function(){ urchin('/' + link); __gemius(notka.gemius); }, 500);
            }
        },
        onError: function(obj){
            alert(obj);
        }
    });
}

function ajaxGoTo3(_url, div){
    if (!div) 
        div = document.getElementById('tresc');
    urchin(_url);
    _print_id = _url;
    div.innerHTML = '';
    $('lapa_loading').style.display = 'block';
    advAJAX.get({
        unique: false,
        url: window.location.protocol + '//' + window.location.host + '/' + _url + '/ajax/1',
        onSuccess: function(obj){
            eval('var notka = ' + obj.responseText);
            div.innerHTML = notka.content + '<br /><br />';
            var data = notka.content;
            var ScriptFragment = '(?:<script.*?>)((\n|.)*?)(?:<\/script>)';
            var match = new RegExp(ScriptFragment, 'img');
            var response = data.replace(match, ''); //w response masz calosc bez skryptów JS
            var scripts = data.match(match); //w scripts masz same skrypty JS
            match = new RegExp(ScriptFragment, 'im');
            if (scripts) {
                for (var i = 0; i < scripts.length; i++) {
                    try {
                        eval(scripts[i].match(match)[1]);
                    } 
                    catch (e) {
                    }
                }
            }
            
            $('lapa_loading').style.display = "none";
        },
        onError: function(obj){
        }
    });
    return false;
}


function blog_ajax(_url){
    advAJAX.get({
        unique: false,
        url: _url + '/ajax/1',
        onSuccess: function(obj){
            eval(obj.responseText);
            
            $('lewa_data').innerHTML = notka.raw[0].date;
            $('lewa_name').innerHTML = notka.raw[0].title;
            $('lewa_tresc').innerHTML = notka.raw[0].content;
        },
        onError: function(obj){
            alert("Błąd: " + obj.status);
        }
    });
    return false;
}

function changeBlogList(_url){
    urchinTracker(_url);
}

function target(myClass){
    var list = document.getElementsByTagName('a');
    for (i = 0; i < list.length; i++) {
        if (list[i].className == myClass) 
            list[i].target = '_blank';
    }
}

function showHideFaq(id){
    if (document.getElementById(id)) {
        $(id).style.display = ($(id).style.display == 'block') ? 'none' : 'block';
        try {
        	if($(id).style.display == 'block') {
	        		
    			var actualBottom = $(id).offsetTop + $(id).offsetHeight;
    			var contentOffset = parseInt(String($('tresc2').style.top).replace('px',''));
    			var containerHeight = parseInt($('tresc_prawa').offsetHeight);

	        	if(containerHeight + contentOffset < actualBottom ) {
	        		$('tresc2').style.top = -1*(actualBottom-containerHeight+22)+'px';
	        		// console.log($('tresc2').style.top);
	        	}
        	}
        }
        catch(e) {}

        try {
            scrol.reset();
        } 
        catch (e) {
            void (0);
            try {
                scroll_prawy.reset();
            } 
            catch (e) {
                void (0);
            }
        }
    }
}

window.onload = function(){
    target('metka_engine');
    target('a_blank');
};

