/**
 * @author Jarosław Kraśniewski (K-Bros)
 */
function ajaxHistory(){
    this.historyListener = function(historyHash){
    
    };
    unFocus.History.addEventListener('historyChange', this.historyListener);
    this.historyListener(unFocus.History.getCurrent());
};

var ah;
var subPageInter;

tabela = new Array();
req = new Array();
aktual = 0;

function loadPage(url){
    //temp = url.split("/");
	//temp.reverse();
    //tempA = temp[1].split(".");
    //location.hash = temp[1];
    //unFocus.History.addHistory(temp[1]);
    //doSomeAjax(temp[0]);
	location.hash = url;
    unFocus.History.addHistory(url);
    doSomeAjax(url);
}

function processAjax(url, divid){
    if (document.getElementById(divid)) {
        document.getElementById(divid).innerHTML = '';
        document.getElementById(divid).style.background = 'url(img/loading.gif) no-repeat center center';
        document.getElementById(divid).style.height = '400px';
    }
    licznik = tabela.length;
    tabela.push(divid);
    if (window.XMLHttpRequest) { // Non-IE browsers
        req[licznik] = new XMLHttpRequest();
        req[licznik].onreadystatechange = function(){
            if (targetDiv(req[aktual], tabela[aktual])) {
                aktual++;
            }
        }
        try {
            req[licznik].open("GET", url, true);
        } 
        catch (e) {
            //alert(e);
        }
        req[licznik].send(null);
    }
    else 
        if (window.ActiveXObject) { // IE
            req[licznik] = new ActiveXObject("Microsoft.XMLHTTP");
            if (req[licznik]) {
                req[licznik].onreadystatechange = function(){
                    if (targetDiv(req[aktual], tabela[aktual])) {
                        aktual++;
                    }
                }
                req[licznik].open("GET", url, true);
                req[licznik].send();
            }
        }  
}

function targetDiv(req, id){
    if (req.readyState == 4) { // Complete
        if (req.status == 200) { // OK response
            document.getElementById(id).style.background = 'none';
            document.getElementById(id).innerHTML = req.responseText;
            document.getElementById(id).style.height = 'auto';
            onLoadInit('flashPlayer', 1);
            onLoadInit('gallery', 1);
            onLoadInit('news', 1);
            return true;
        }
        else {
            //alert("Błąd: " + req.statusText);
            exit;
        }
    }
    return false;
}

function doSomeAjax(url){
	var ver = lang;
    if (document.getElementById('content') && url) {
        processAjax('content/'+ ver + '/' + url, 'content');
    } else  {
		if ((document.getElementById('content')) && (location.hash != '')) {
            var hash = location.hash;
            processAjax('content/'+ ver + '/' + hash.substring(1), 'content');
        }
        else {
            processAjax('content/'+ ver + '/main.html', 'content');
        }
	}
}

function loadurl(dest){
    try {
        xmlhttp = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");
    } 
    catch (e) {
    
    }
    xmlhttp.onreadystatechange = triggered;
    xmlhttp.open("GET", dest);
    xmlhttp.send("null");
}

function triggered(){
    if ((xmlhttp.readyState == 4)(xmlhttp.status == 200)) {
        document.getElementById("ajaxresponse").innerHTML = xmlhttp.responseText;
    }
}

function zwin(again){
	$('.newsWrap').each(function(){
	    wys_min = 0;
	    $(this).find('a').each(function(){
	        $(this).attr('target', '_blank');
	    })
		small = 0;
		all = 0;
	    $(this).find('p').each(function(){
	   		all = all + 1;
	        if ($(this).height() > 20) {
	            $(this).children('.more').remove();
	            $(this).append('<span class="more">...&nbsp;<a href="javascript: void(0)">więcej&nbsp;&#187;</a></span>');
	            wys_min = wys_min + $(this).height() + 15;
	            $(".more").click(function(e){
	                e.preventDefault();
	            })
	            return false;
	        }
	        else {
				wys_min = wys_min + $(this).height() + 15;
				small = small + 1;
	        }
	    })
		
		
		if (small == all) { 
			wys_min = ((wys_min/all + 15)*3); 
			count = 0;
			$(this).find('p').each(function(){
				if (count == 3) {
					$(this).children('.more').remove();
	                $(this).append('<span class="more">...&nbsp;<a href="javascript: void(0)">więcej&nbsp;&#187;</a></span>');
	                $(".more").click(function(e){
	                    e.preventDefault();
	                })
					return false;
				}
				if ($(this).html() != '&nbsp;') {
					count = count + 1;
				}
			})
		}
		if ($(this).height() > wys_min) {
			if (again == 1) {
					$(this).animate({
	            	height: wys_min
	            }, {
	                duration: 200,
	                easing: 'easeIn',
	                queue: false
	            });
			} else {
				$(this).height(wys_min);
			}	
		}
	});
}

function onLoadInit(id, count){
    count = count + 1;
    if (document.getElementById(id)) {
        if (id == "gallery") {
            $('#gallery a').lightBox();
        }
        else 
            if (id == "news") {
            
                zwin();
                $(".newsWrap").click(function(e){
                    this.blur();
                    wys = 0;
                    wys = $(this).find(".newsContent").height() + 20;
                    if ($(this).height() < $(this).find('.newsContent').height()) {
                        zwin(1);
                        $(this).animate({
                            height: wys
                        }, {
                            duration: 200,
                            easing: 'easeOut',
                            queue: false
                        });
                        $(this).find('p').find('.more').remove();
                    }
                    else {
                        zwin(1);
                    }
                });
            }
            else {
                Shadowbox.setup($('a.movieThumb'), {
                    overlayOpacity: 0.8
                });
            }
    }
    else {
        if (count < 5) {
            setTimeout("onLoadInit('" + id + "', " + count + ")", 1000)
        }
    }
}

