/*
	DOMAssistant is developed by Robert Nyman, http://www.robertnyman.com, and it is released according to the
	Creative Commons Deed license (http://creativecommons.org/licenses/GPL/2.0/)
	For more information, please see http://www.robertnyman.com/domassistant
*/
var DOMAssistant = {
	
	methodsToAdd : [],
	
	init : function (){
		this.applyMethod.call(window, "$", this.$);
		window.DOMAssistant = this;
		this.addBaseMethods();
	},
	
	addBaseMethods : function (){
		document.getElementsByClassName = this.getElementsByClassName;
		document.getElementsByAttribute = this.getElementsByAttribute;
		if(typeof HTMLElement == "function"){
			HTMLElement.prototype.getElementsByClassName = this.getElementsByClassName;
			HTMLElement.prototype.getElementsByAttribute = this.getElementsByAttribute;		
		}
		this.methodsToAdd.push(["getElementsByClassName", this.getElementsByClassName]);
		this.methodsToAdd.push(["getElementsByAttribute", this.getElementsByAttribute]);
	},
	
	applyMethod : function (method, func){
		if(typeof this[method] != "function"){
			this[method] = func;
		}
	},
	
	addMethods : function (elm){
		if(elm){
			var elms = (elm.constructor == Array)? elm : [elm];
			for(var i=0; i<elms.length; i++){	
				for(var j=0; j<this.methodsToAdd.length; j++){
	            	this.applyMethod.call(elms[i], this.methodsToAdd[j][0], this.methodsToAdd[j][1]);
	            }
			}
		}
	},
	
	$ : function (){
		var elm = null;
		if(document.getElementById){
			elm = (arguments.length > 1)? [] : null;
			var current;
			for(var i=0; i<arguments.length; i++){
				current = arguments[i];
				if(typeof current != "object"){
					current = document.getElementById(current);
				}
				if(arguments.length > 1){
					elm.push(current);
				}
				else{
					elm = current;
				}
			}
			DOMAssistant.addMethods(elm);
		}
		return elm;
    },
	
	getElementsByClassName : function (className, tag){
		var elms = ((!tag || tag == "*") && this.all)? this.all : this.getElementsByTagName(tag || "*");
		var returnElms = [];
		var className = className.replace(/\-/g, "\\-");
		var regExp = new RegExp("(^|\\s)" + className + "(\\s|$)");
		var elm;
		for(var i=0; i<elms.length; i++){
			elm = elms[i];		
			if(regExp.test(elm.className)){
				returnElms.push(elm);
			}
		}
		return (returnElms);
	},
	
	getElementsByAttribute : function (attr, attrVal, tag){
	    var elms = ((!tag || tag == "*") && this.all)? this.all : this.getElementsByTagName(tag || "*");
	    var returnElms = [];
	    if(typeof attrVal != "undefined"){
			var attrVal = new RegExp("(^|\\s)" + attrVal + "(\\s|$)");
		}
	    var current;
	    var currentAttr;
	    for(var i=0; i<elms.length; i++){
	        current = elms[i];
	        currentAttr = current.getAttribute(attr);
	        if(typeof currentAttr == "string" && currentAttr.length > 0){	
	            if(typeof attrVal == "undefined" || (attrVal && attrVal.test(currentAttr))){
					returnElms.push(current);
	            }
	        }
	    }
	    return returnElms;
	}	
}
DOMAssistant.init();

/*
	DOMAssistant is developed by Robert Nyman, http://www.robertnyman.com, and it is released according to the
	Creative Commons Deed license (http://creativecommons.org/licenses/GPL/2.0/)
	For more information, please see http://www.robertnyman.com/domassistant
	
	This module by Robert Nyman, http://www.robertnyman.com
*/
DOMAssistant.initContent = function (){
	this.addContentMethods();
};

DOMAssistant.addContentMethods = function (){
	if(typeof HTMLElement == "function"){
		HTMLElement.prototype.prev = DOMAssistant.prev;
		HTMLElement.prototype.next = DOMAssistant.next;
		HTMLElement.prototype.create = DOMAssistant.create;
		HTMLElement.prototype.setAttributes = DOMAssistant.setAttributes;
		HTMLElement.prototype.addContent = DOMAssistant.addContent;
		HTMLElement.prototype.replaceContent = DOMAssistant.replaceContent;
		HTMLElement.prototype.remove = DOMAssistant.remove;
	}
	this.methodsToAdd.push(["prev", this.prev]);
	this.methodsToAdd.push(["next", this.next]);
	this.methodsToAdd.push(["create", this.create]);
	this.methodsToAdd.push(["setAttributes", this.setAttributes]);
	this.methodsToAdd.push(["addContent", this.addContent]);
	this.methodsToAdd.push(["replaceContent", this.replaceContent]);
	this.methodsToAdd.push(["remove", this.remove]);
};

DOMAssistant.prev = function (){
	var prevSib = this.previousSibling;
	while(prevSib && prevSib.nodeType != 1){
		prevSib = prevSib.previousSibling;
	}
	return prevSib;
};

DOMAssistant.next = function (){
	var nextSib = this.nextSibling;
	while(nextSib && nextSib.nodeType != 1){
		nextSib = nextSib.nextSibling;
	}
	return nextSib;
};

DOMAssistant.create = function (name, attr, append, content){
	var elm = document.createElement(name);
	elm = DOMAssistant.$(elm);
	if(attr){
		elm.setAttributes(attr);
	}
	if(typeof content != "undefined"){
		elm.addContent(content);
	}
	if(append){
		this.addContent(elm);	
	}
	return elm;
};

DOMAssistant.setAttributes = function (attr){	
	for(var i in attr){
		if(/class/i.test(i)){
			this.className = attr[i];
		}
		else{
			this.setAttribute(i, attr[i]);
		}	
	}
};

DOMAssistant.addContent = function (content){
	var retVal = null;
	if(typeof content == "string"){
		retVal = this.innerHTML += content;
	}
	else{		
		retVal = this.appendChild(content);
	}
	return retVal;
};

DOMAssistant.replaceContent = function (newContent){
	for(var i=(this.childNodes.length - 1); i>=0; i--){
    	this.childNodes[i].parentNode.removeChild(this.childNodes[i]);
    }
	this.addContent(newContent);
};

DOMAssistant.remove = function (){
	this.parentNode.removeChild(this);
};

DOMAssistant.initContent();
/*
	DOMAssistant is developed by Robert Nyman, http://www.robertnyman.com, and it is released according to the
	Creative Commons Deed license (http://creativecommons.org/licenses/GPL/2.0/)
	For more information, please see http://www.robertnyman.com/domassistant
	
	This module by Robert Nyman, http://www.robertnyman.com
*/
DOMAssistant.initEvents = function (){
	this.addEventMethods();
};

DOMAssistant.addEventMethods = function (){
	if(typeof HTMLElement == "function"){
		HTMLElement.prototype.addEvent = DOMAssistant.addEvent;
		HTMLElement.prototype.handleEvent = DOMAssistant.handleEvent;
		HTMLElement.prototype.removeEvent = DOMAssistant.removeEvent;
	}
	this.methodsToAdd.push(["addEvent", this.addEvent]);
	this.methodsToAdd.push(["handleEvent", this.handleEvent]);	
	this.methodsToAdd.push(["removeEvent", this.removeEvent]);
};

DOMAssistant.addEvent = function (evt, func){
	if(this.addEventListener){
		this.addEventListener(evt, func, false);
	}
	else{
		if(!this.events){
			this.events = {};
		}
		if(!this.events[evt]){
			this.events[evt] = [];
		}							
		this.events[evt].push(func);
		this["on" + evt] = DOMAssistant.handleEvent;
	}
	return true;
};

DOMAssistant.handleEvent = function (evt){
	var evt = evt || event;
	var eventType = evt.type;
	var eventColl = this.events[eventType];
	for (var i=0; i<eventColl.length; i++) {
		eventColl[i].call(this, evt);
	}
};

DOMAssistant.removeEvent = function (evt, func){
	if(this.removeEventListener){
		this.removeEventListener(evt, func, false);
	}
	else if(this.events){
		var eventColl = this.events[evt];
		for (var i=0; i<eventColl.length; i++) {
			if(eventColl[i] == func){
				delete eventColl[i]
				eventColl.splice(i, 1);
			}
		}
	}
};

DOMAssistant.preventDefault = function (evt){
	if(evt && evt.preventDefault){
		evt.preventDefault();
	}
	else{
		event.returnValue = false;
	}
};

DOMAssistant.cancelBubble = function (evt){
	if(evt && evt.stopPropagation){
		evt.stopPropagation();
	}
	else{
		event.cancelBubble = true;
	}
};

DOMAssistant.initEvents();

/*
	DOMAssistant is developed by Robert Nyman, http://www.robertnyman.com, and it is released according to the
	Creative Commons Deed license (http://creativecommons.org/licenses/GPL/2.0/)
	For more information, please see http://www.robertnyman.com/domassistant
	
	This module by Robert Nyman, http://www.robertnyman.com
	Inspired and influenced by Dean Edwards, Matthias Miller, and John Resig: http://dean.edwards.name/weblog/2006/06/again/
*/
DOMAssistant.functionsToCall = [
printPreviewControl
	/*
		functionName // name of function
		"functionName()" // name of function with parentheses and optional arguments
	*/
];

DOMAssistant.initLoad = function (){
	this.DOMLoaded = false;
	this.DOMLoadTimer = null;
};

DOMAssistant.DOMHasLoaded = function (){
	if(DOMAssistant.DOMLoaded) return;
	DOMAssistant.DOMLoaded = true;
	DOMAssistant.execFunctions();
};

DOMAssistant.execFunctions = function (){
	if(this.DOMLoaded){
		clearInterval(this.DOMLoadTimer);
	}
	var functionToCall;
	for(var i=0; i<this.functionsToCall.length; i++){
		try{
			functionToCall = this.functionsToCall[i];
			if(typeof functionToCall == "function"){
				functionToCall();
			}
			else if (typeof functionToCall == "string"){
				eval(this.functionsToCall[i]);
			}
		}
		catch(e){
			// Optional: handle error here
		}
	}
};
// ---
/* Internet Explorer */
/*@cc_on @*/
/*@if (@_win32)
	if(document.getElementById){
		document.write("<script id=\"ieScriptLoad\" defer src=\"//:\"><\/script>");
	    document.getElementById("ieScriptLoad").onreadystatechange = function() {
	        if (this.readyState == "complete") {
	            DOMAssistant.DOMHasLoaded();
	        }
	    };
	}
/*@end @*/
// ---
/* Mozilla/Opera 9 */
if (document.addEventListener) {
	document.addEventListener("DOMContentLoaded", DOMAssistant.DOMHasLoaded, false);
}
// ---
/* Safari */
if(navigator.userAgent.search(/WebKit/i) != -1){
    DOMAssistant.DOMLoadTimer = setInterval(function (){
		if(document.readyState.search(/loaded|complete/i) != -1) {
			DOMAssistant.DOMHasLoaded();
		}
	}, 10);
}
// ---
/* Other web browsers */
window.onload = DOMAssistant.DOMHasLoaded;
// ---
DOMAssistant.initLoad();

function printPreviewControl() {
  var main = $("main");
  if ($("submit").value=="Hledej!")
    var language = "cz";
  else
    var language = "en";
  
  var en = { link : "Print version", warning : "This is a print preview of this page", notice : "This message won't be printed ", back : "Return to the existing page."}
  var cz = { link : "Tisková verze", warning : "Toto je tisková verze stránky", notice : "Tato zpráva se nevytiskne ", back : "Zpět na původní stránku"}
  
  main.insertBefore(main.create("p", {id : "tisk-obal"}, true), main.firstChild);
  $("tisk-obal").create("a", {id : "tisk", href : "#"}, true, (language=="cz" ? cz.link : en.link));
           
  function setActiveStyleSheet(title)
  {
     var i, a, main;
     for(i=0; (a = document.getElementsByTagName("link")[i]); i++)
     {
       if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title"))
       {
         a.disabled = true;
         if(a.getAttribute("title") == title) a.disabled = false;
       }
     }
  }
          
  function enablePrintPreview()
  {
     setActiveStyleSheet("Tisk");
     /*window.print();*/
    
    $("tisk-obal").remove();
    
    main.insertBefore(main.create("p", {id : "tisk-zprava"}, true), main.firstChild);
    $("tisk-zprava").create("h2", {}, true, (language=="cz" ? cz.warning : en.warning))
    $("tisk-zprava").create("p", {id : "tisk-obal"}, true, (language=="cz" ? cz.notice : en.notice))
    $("tisk-obal").create("a", {id : "tisk", href : "#"}, true, (language=="cz" ? cz.back : en.back));
    
     $("tisk").addEvent("click", function(e)
     {
       disablePrintPreview();
       DOMAssistant.preventDefault(e);
     })
                                    
  }
      
  function disablePrintPreview()
  {
  
    $("tisk-zprava").remove();      
    
    main.insertBefore(main.create("p", {id : "tisk-obal"}, true), main.firstChild);
    $("tisk-obal").create("a", {id : "tisk", href : "#"}, true, (language=="cz" ? cz.link : en.link));     
  
    setActiveStyleSheet("Normal");   
    $("tisk").addEvent("click", function(e)
    {
      enablePrintPreview();
      DOMAssistant.preventDefault(e);
    })                          
  }
          
  $("tisk").addEvent("click", function(e)
  {
    enablePrintPreview();
    DOMAssistant.preventDefault(e);           
  })
}          

