/*

	Różne skrypty dla 4images
	(c) 2011 by pewusoft i 4images

*/

/*  --------------
	KOD KALENDARZA
    -------------- */
// ===================================================================
// Author: Matt Kruse <matt@mattkruse.com>
// WWW: http://www.mattkruse.com/
// ===================================================================
/* SOURCE FILE: AnchorPosition.js */
function getAnchorPosition(anchorname){var useWindow=false;var coordinates=new Object();var x=0,y=0;var use_gebi=false, use_css=false, use_layers=false;if(document.getElementById){use_gebi=true;}else if(document.all){use_css=true;}else if(document.layers){use_layers=true;}if(use_gebi && document.all){x=AnchorPosition_getPageOffsetLeft(document.all[anchorname]);y=AnchorPosition_getPageOffsetTop(document.all[anchorname]);}else if(use_gebi){var o=document.getElementById(anchorname);x=AnchorPosition_getPageOffsetLeft(o);y=AnchorPosition_getPageOffsetTop(o);}else if(use_css){x=AnchorPosition_getPageOffsetLeft(document.all[anchorname]);y=AnchorPosition_getPageOffsetTop(document.all[anchorname]);}else if(use_layers){var found=0;for(var i=0;i<document.anchors.length;i++){if(document.anchors[i].name==anchorname){found=1;break;}}if(found==0){coordinates.x=0;coordinates.y=0;return coordinates;}x=document.anchors[i].x;y=document.anchors[i].y;}else{coordinates.x=0;coordinates.y=0;return coordinates;}coordinates.x=x;coordinates.y=y;return coordinates;}
function getAnchorWindowPosition(anchorname){var coordinates=getAnchorPosition(anchorname);var x=0;var y=0;if(document.getElementById){if(isNaN(window.screenX)){x=coordinates.x-document.body.scrollLeft+window.screenLeft;y=coordinates.y-document.body.scrollTop+window.screenTop;}else{x=coordinates.x+window.screenX+(window.outerWidth-window.innerWidth)-window.pageXOffset;y=coordinates.y+window.screenY+(window.outerHeight-24-window.innerHeight)-window.pageYOffset;}}else if(document.all){x=coordinates.x-document.body.scrollLeft+window.screenLeft;y=coordinates.y-document.body.scrollTop+window.screenTop;}else if(document.layers){x=coordinates.x+window.screenX+(window.outerWidth-window.innerWidth)-window.pageXOffset;y=coordinates.y+window.screenY+(window.outerHeight-24-window.innerHeight)-window.pageYOffset;}coordinates.x=x;coordinates.y=y;return coordinates;}
function AnchorPosition_getPageOffsetLeft(el){var ol=el.offsetLeft;while((el=el.offsetParent) != null){ol += el.offsetLeft;}return ol;}
function AnchorPosition_getWindowOffsetLeft(el){return AnchorPosition_getPageOffsetLeft(el)-document.body.scrollLeft;}
function AnchorPosition_getPageOffsetTop(el){var ot=el.offsetTop;while((el=el.offsetParent) != null){ot += el.offsetTop;}return ot;}
function AnchorPosition_getWindowOffsetTop(el){return AnchorPosition_getPageOffsetTop(el)-document.body.scrollTop;}
/*  --------------
	KOD KALENDARZA KONIEC
    -------------- */
	
function opt_search (needle, haystack) {
    var key = '';
	var string = '';
 
    for (key in haystack) {
        if (typeof haystack[key].text != "undefined" && haystack[key].text.toString().toLowerCase().indexOf(needle.toLowerCase()) != -1 && haystack[key].value.toString() != "0") {
			return key;
        }
    }
 
    return false;
} 
	
function trim(str) {
	var	str = str.replace(/^\s\s*/, ''),
		ws = /\s/,
		i = str.length;
	while (ws.test(str.charAt(--i)));
	return str.slice(0, i + 1);
}
	
function add_woz(id) {
	el = document.getElementById(id);
	window.wozyselectcount = window.wozyselectcount + 1;
	
	var calutkidiv;
	calutkidiv = document.createElement('div');
	calutkidiv.id = 'wozydiv'+window.wozyselectcount;
	
	var nowyselect;
	nowyselect = document.createElement('select');
	nowyselect.id = 'wozyselect'+window.wozyselectcount;
	nowyselect.setAttribute('onchange', 'if (this.options[this.selectedIndex].value==0) {this.selectedIndex = this.selectedIndex+1};');
	nowyselect.setAttribute('class', 'categoryselect wozyselect input');
	nowyselect.name = 'image_wozy[]';
	
	minusbutton = document.createElement('div');
	minusbutton.id = 'wozyminus'+window.wozyselectcount;
	minusbutton.setAttribute('class', 'minus');
	minusbutton.setAttribute('onclick', 'del_woz(\''+id+'\', '+window.wozyselectcount+');');	
	
	var xmlhttp;
	if (window.XMLHttpRequest) {
		xmlhttp=new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	xmlhttp.onreadystatechange=function() {
		if (xmlhttp.readyState==4 && xmlhttp.status==200) {
			
			nowyselect.innerHTML = xmlhttp.responseText;
			var is_ie;
			if (nowyselect.innerHTML.indexOf('<option') == -1) {
				nowyselect = document.createElement('span');	
				nowyselect.innerHTML = '<select name="image_wozy[]" id="wozyselect'+window.wozyselectcount+'" class="categoryselect wozyselect input" onchange="if (this.options[this.selectedIndex].value==0) {this.selectedIndex = this.selectedIndex+1};">'+xmlhttp.responseText+'</select>';
				is_ie = true;
			} else {
				if (nowyselect.options.length >= 2) nowyselect.selectedIndex = 1;
				is_ie = false;
			}
			calutkidiv.appendChild(nowyselect);
			calutkidiv.appendChild(minusbutton);
			
			brboth = document.createElement('br');
			brboth.style.clear = 'both';
			calutkidiv.appendChild(brboth);
			
			sinput = document.createElement('input');
			sinput.name = '';
			sinput.id = 'wozysearch'+window.wozyselectcount;
			sinput.setAttribute('type', 'text');
			sinput.value = 'Szukaj...';
			sinput.setAttribute('onkeyup', 'search_woz('+window.wozyselectcount+', this.value);');
			sinput.setAttribute('onfocus', 'if (this.value==\''+sinput.value+'\') this.value=\'\';');
			sinput.setAttribute('onblur', 'if (this.value==\'\') this.value=\''+sinput.value+'\';');
			sinput.setAttribute('class', 'categoryselect input');
			calutkidiv.appendChild(sinput);
			
			calutkidiv.appendChild(document.createElement('br'));
			calutkidiv.appendChild(document.createElement('br'));
			
			el.appendChild(calutkidiv);
			if (is_ie) {
				if (document.getElementById('wozyselect'+window.wozyselectcount).options.length >= 2) document.getElementById('wozyselect'+window.wozyselectcount).selectedIndex = 1;	
			}
			/*document.getElementById(sinput.id).style.border = 'none';
			document.getElementById(sinput.id).style.width = (document.getElementById(nowyselect.id).offsetWidth).toString()+'px';*/
		}
	}
	xmlhttp.open("GET","data/wozy.html?r="+Math.floor(Math.random()*10001),true);
	xmlhttp.send(null);
}

function search_woz(wozid, text) {
	document.getElementById('wozysearch'+wozid).style.color="";
	if (text.length >= 3) {
		idx = opt_search(text, document.getElementById('wozyselect'+wozid).options);
		if (idx !== false) {
			document.getElementById('wozyselect'+wozid).selectedIndex=idx;
		} else {
			document.getElementById('wozysearch'+wozid).style.color="#ff0000";
		}
	}
}

function del_woz(id, wozid) {
	el = document.getElementById(id);
	el.removeChild(document.getElementById('wozydiv'+wozid));
}

function show_help() {
	var xy = getAnchorPosition(window.txa_id);
	var okno;
	if (document.getElementById('keyword_help')) {
		okno = document.getElementById('keyword_help');
		okno.style.display='none';
	} else {
		var txa = document.getElementById(window.txa_id);
		okno = document.createElement('div');
		okno.id = 'keyword_help';
		okno.style.display = 'none';
		okno.style.left = (xy.x+10+txa.offsetWidth).toString()+'px';
		okno.style.top = (xy.y-10).toString()+'px';
		okno.style.height = (txa.offsetHeight).toString()+'px';
		okno.style.width = '200px';
		okno.setAttribute('onclick', 'document.getElementById(\'keyword_help\').style.display=\'none\'; return false;');
		okno.title = 'Kliknij, aby zamknąć';
		document.body.appendChild(okno);
	}
	okno.style.display='block';
	okno.innerHTML='Ładowanie...';
	var xmlhttp;
	if (window.XMLHttpRequest) {
		xmlhttp=new XMLHttpRequest();
	} else {
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	xmlhttp.onreadystatechange=function() {
		if (xmlhttp.readyState==4 && xmlhttp.status==200) {
			document.getElementById('keyword_help').innerHTML=xmlhttp.responseText;
		} else if (xmlhttp.readyState==4 && xmlhttp.status!=200) {
			document.getElementById('keyword_help').style.display = 'none'; 
		}
	}
	xmlhttp.open("GET","data/keywords_help.html?r="+Math.floor(Math.random()*10001),true);
	xmlhttp.send(null);
}

function show_utility(txaid) {
	if (txaid) window.txa_id = txaid;
	show_help();
}

	var captcha_reload_count = 0;
	var captcha_image_url = "{url_captcha_image}";

  function new_captcha_image() {
    if (captcha_image_url.indexOf('?') == -1) {
		  document.getElementById('captcha_image').src= captcha_image_url+'?c='+captcha_reload_count;
		} else {
		  document.getElementById('captcha_image').src= captcha_image_url+'&c='+captcha_reload_count;
		}

    document.getElementById('captcha_input').value="";
    document.getElementById('captcha_input').focus();
    captcha_reload_count++;
  }

	function opendetailwindow(url) {
		if (!url) url = '';
		window.open(url,'detailwindow','toolbar=no,scrollbars=yes,resizable=no,width=680,height=480');
  }
	
	var tout = []; 

	nextObject = function( MyObject ) 
   {
      NextObj = MyObject;
	  do NextObj = NextObj.nextSibling;
      while (NextObj && NextObj.nodeType != 1);
        return NextObj;
   }
	
  function highlightComment(aid) {
	el_a = document.getElementById("comment"+aid);
	if (!el_a) return false;
	trow = el_a.parentNode;
	
	nextrow = trow.parentNode;
	
	var s = "4px solid yellow";
	nextrow.style.outline = s;
	
	if (tout[aid]) clearTimeout(tout[aid]);
	tout[aid] = setTimeout("dehighlight('"+aid+"')", 3000);

  }
  
  function getElementsByClassName(classname, node) {
    if(!node) node = document.getElementsByTagName("body")[0];
    var a = [];
    var re = new RegExp('\\b' + classname + '\\b');
    var els = node.getElementsByTagName("*");
    for(var i=0,j=els.length; i<j; i++)
    if(re.test(els[i].className))a.push(els[i]);
    return a;
  }
  
  function dehighlight(aid) {
	el_a = document.getElementById("comment"+aid);
	if (!el_a) return false;
	trow = el_a.parentNode;
	
	nextrow = trow.parentNode;
	
	var s = "none";
	nextrow.style.outline = s;
	clearTimeout(tout[aid]);
  }
  
  function load_hl() {
	var aid = window.location.hash;
	idx = aid.indexOf("#comment");
	if (idx == -1) return false;
	aid = aid.substr(8,aid.length);
	highlightComment(aid);
  }
 
 function referto(where, what, who) {
	if (!where) return false;
	theform = document.getElementById(where);
	if (!theform) return false;
	if (!theform.comment_text) return false;
	if (!what) return false;
	if (!who) return false;
 
	pagehref = window.location.href.replace(window.location.hash,'');
	linkurl = pagehref.replace(/[\?&]?sid=[0-9a-f]{5,40}/gi,'')+"#comment"+what;
    theform.comment_text.value += "[URL="+linkurl+"]@"+who+"[/URL]: ";
	theform.comment_text.focus();
 }
 
 function init() {
	if ("onhashchange" in window) window.onhashchange = load_hl;
	load_hl();
 }
 
 function insertOption(t,data){
  var elSel = t;
  if (elSel.options.length > 0) {
    var elOptNew = document.createElement('option');
    elOptNew.text = data;
    elOptNew.value = data;
    var elOptOld = elSel.options[0];  
    try {
      elSel.add(elOptNew, elOptOld); // standards compliant; doesn't work in IE
    }
    catch(ex) {
      elSel.add(elOptNew, 0); // IE only
    }
  }
}
