var Project = {

	Global : {

		RemoveFocus : function(){
			if(!document.getElementsByTagName) return;
			for(var i = 0; document.getElementsByTagName('a')[i]; i++){
				document.getElementsByTagName('a')[i].onfocus = document.getElementsByTagName('a')[i].blur;
			}
			for(var i = 0; document.getElementsByTagName('input')[i]; i++){
				var tmp = document.getElementsByTagName('input')[i];
				if(tmp.type == "submit" || tmp.type == "checkbox" || tmp.type == "radio") tmp.onfocus = tmp.blur;
			}
		},

		ButtonRollOver : function(obj){
			obj.style.backgroundPosition = "0px -19px";
		},

		ButtonRollOut : function(obj){
			obj.style.backgroundPosition = "0px 0px";
		},

		printAt : function(start,end){
			var prefix = "&#109;a" + "i&#108;" + "&#116;o";
			var path = "hr" + "ef" + "=";
			var addAt = start + "@" + end;
			document.write('<a ' + path + '\'' + prefix + ':' + addAt + '\' onfocus="blur();">' + addAt + '</a>');
		},

		stripslashes : function(str){
			//return str.replace('/\0/g', '0').replace('/\(.)/g', '$1');
			return str.replace(/\\'/g,'\'').replace(/\\"/g,'"').replace(/\\\\/g,'\\').replace(/\\0/g,'\0');
		},

		log : function(message){
			var log = document.getElementById("log");
			if(log) log.value += message + "\n";
		}

	},

	AutoCompliter : {

		backspaceKeycode : 8, ctrlKeycode : 17, downArrowKeycode : 40, shiftKeycode : 16, ctrlKeyDown : false,

		initCompleter : function(input, select){
			if(!document.getElementById) return;

			var input = document.getElementById(input);
			if(!input){ alert("Brak obiektu input"); return; }
			var select = document.getElementById(select);
			if(!select){ alert("Brak obiektu select"); return; }

			input.refference = select;
			select.refference = input;

			Project.AutoCompliter.initSelect(select);
			Project.AutoCompliter.initInput(input);
		},

		initInput : function(input){
			input.setAttribute("autocomplete", "off");

			input.onkeydown = this.inputKeyDown;
			input.onkeyup = this.inputKeyUp;
			input.onfocus = this.inputFocussed;
			input.onblur = this.inputUnfocussed;
			
			return true;
			
		},

		inputKeyUp : function(){
			var upCode = 38;
			var downCode = 40;
			if(!this.skip) {
				xajax_completeArtist(this.value, this.refference.id);
			}
			this.skip = false;
		},
		
		inputKeyDown : function(e){

			var ctrlCode = 17;
			var upCode = 38;
			var downCode = 40;
			var tabCode = 9;
			var enterCode = 13;
			var escCode = 27;
			

			theEvent = e ? e : event;

			if(theEvent.keyCode == ctrlCode) Project.AutoCompliter.ctrlKeyDown = true;
			if(theEvent.keyCode == downCode){
				Project.AutoCompliter.moveDown(this.refference);
				this.skip = true;
			}else if(theEvent.keyCode == upCode) {
				Project.AutoCompliter.moveUp(this.refference);
				this.skip = true;
			}else if(theEvent.keyCode == tabCode || theEvent.keyCode == enterCode){
				if(this.refference.selectedIndex >= 0){
					this.value = this.refference.value;
					//return false; // dowywalenia
				}
			}else if(theEvent.keyCode == escCode){

			}

		},

		moveDown : function(select){

			if(select.list){
				if(select.selectedIndex < 0){
					select.selectedIndex = 0;
				}else{
					if(select.selectedIndex + 1 < select.list.childNodes.length){
						select.list.childNodes[select.selectedIndex].className = '';
						select.selectedIndex += 1;
					}
				}
				select.value = select.list.childNodes[select.selectedIndex].childNodes[0].nodeValue;
				select.list.childNodes[select.selectedIndex].className = 'active';
			}


		},

		moveUp : function(select){
			if(select.list && select.selectedIndex >= 0){

				var item;

				if(select.selectedIndex == 0){
					select.list.childNodes[select.selectedIndex].className = '';
					select.selectedIndex = -1;
				}else if(select.selectedIndex - 1 >= 0){
					select.list.childNodes[select.selectedIndex].className = '';
					select.selectedIndex -= 1;
					
				}
				
				if(select.selectedIndex >= 0) item = select.list.childNodes[select.selectedIndex];
				if(item){
					select.value = item.childNodes[0].nodeValue;
					item.className = 'active';
				}
			}

		},

		inputFocussed : function(){
			xajax_completeArtist(this.value, this.refference.id);
		},

		inputUnfocussed : function(){
			
			if(this.skip){
				this.skip = false;
			}else{
				if(this._timeout) clearTimeout(this._timeout);
				this._timeout = setTimeout("Project.AutoCompliter.displayBox(false, '" + this.refference.id + "');", 200);
			}

			return false;
		},


		initSelect : function(select){
			var select = select;
			select.selectedIndex = -1;
			
			
			return true;
		},

		selectClicked : function(){
			
			var input = this.parentNode.parentNode.refference;
			input.value = this.childNodes[0].nodeValue;
			Project.Global.log("clicked" + input.value);
			input.form.submit();
			//this.value = this.refference.value;
			//Project.AutoCompliter.selectChanged(this);
			return true;
		},

		response : function(r, select){
			Project.Global.log("resp");
			var select = document.getElementById(select);
			if(!select) return false;

			var r = r.split("|::|");

			if(select.list) select.removeChild(select.list);
			var ul = document.createElement('ul');
			ul.className = 'selectReplacement';
			for(var i = 0; i < r.length; i++){
				var li = document.createElement('li');
				var txt = document.createTextNode(Project.Global.stripslashes(r[i]));
				li.appendChild(txt);
				li.index = i;
				li.onclick = this.selectClicked;
				li.onmouseover = this.liOver;
				li.onmouseout = this.liOut;
				ul.appendChild(li);
				
			}
			select.appendChild(ul);
			select.list = ul;
			select.selectedIndex = -1;
		},

		liOver : function(){
			Project.Global.log("liover");
			this.className = 'active';
		},

		liOut : function(){
			if(this.index != this.parentNode.parentNode.selectedIndex) this.className = '';
			Project.Global.log("liout " + this.index + " " + this.parentNode.parentNode.selectedIndex);
		},

		displayBox : function(status, select){
			var display = status ? 'block' : 'none';
			document.getElementById(select).style.display = display;
		}
	},

	ScrollPage : {
		
		scrollInterval : null,

		iecompattest : function(){
			return (document.compatMode != "BackCompat") ? document.documentElement : document.body;
		},

		scrollwindow : function(){

			var currentpos = document.all ? Project.ScrollPage.iecompattest().scrollTop : window.pageYOffset;
			currentpos = currentpos - (currentpos / 10);

			if(currentpos <= 0) clearInterval(Project.ScrollPage.scrollInterval);

			window.scroll(0,currentpos)
		},

		startScroll : function(){
			var currentpos = document.all ? Project.ScrollPage.iecompattest().scrollTop : window.pageYOffset;
			Project.ScrollPage.scrollInterval = setInterval("Project.ScrollPage.scrollwindow()", 10);
			if(window.addEventListener){
				window.addEventListener('DOMMouseScroll', Project.ScrollPage.onWheel, false);
			}else{
				/** IE/Opera. */
				window.onmousewheel = document.onmousewheel = Project.ScrollPage.onWheel;
			}

			return false;
		},

		onWheel : function(event){
			clearInterval(Project.ScrollPage.scrollInterval);
		}
	},

	SendMail : function(form){
		if(Project.Validate(form)){
			Project.SetLoader();
			xajax_sendContactMail(xajax.getFormValues(form));
		}
		return false;
	},

	Add2Newsletter : function(form){
		if(Project.Validate(form)){
			xajax_add2Newsletter(xajax.getFormValues(form));
		}
		return false;
	},

	SetLoader : function(){
		xajax.$('ajaxloader').style.display = 'block';
		xajax.$('ajaxloader').style.position = 'absolute';
		xajax.$('ajaxloader').style.height = xajax.$('ajaxloader').parentNode.offsetHeight + "px";
		xajax.$('ajaxloader').style.width = xajax.$('ajaxloader').parentNode.offsetWidth + "px";
		//Project.Effect.Alpha.SetAlpha(xajax.$('ajaxloader'), 60);
	},

	/*####################################
	## -> EFFECTS
	#####################################*/

	Effect : {

		Slide : {
			Speed : 40, Divs : document.getElementsByTagName("div"),
			Start : function(id, timeout, status){
				var t = timeout ? timeout : Project.Effect.Slide.Speed;
				var tmpdiv = Project.Effect.Slide.Divs[id];
				if(tmpdiv.style.display == 'block'){
					if(!status || (status && status == "up")){
						if(tmpdiv._timeout) clearTimeout(tmpdiv._timeout);
						tmpdiv._timeout = setTimeout("Project.Effect.Slide.SlideUp('" + id + "')", t);
					}
				}else{
					if(!status || (status && status == "dn")){ 
						tmpdiv.style.display = 'block';
						if(!tmpdiv.H){
							tmpdiv.H = tmpdiv.offsetHeight;
							if(!Project.GetIE6()){
								var paddingtop = Project.GetStyle(tmpdiv, "paddingTop", "padding-top");
								var paddingbtm = Project.GetStyle(tmpdiv, "paddingBottom", "padding-bottom");
								var offset = parseInt(paddingbtm) + parseInt(paddingtop);
								tmpdiv.H = tmpdiv.offsetHeight - offset;
								
							}
						}
						tmpdiv.style.height = "1px";
						tmpdiv._timeout = setTimeout("Project.Effect.Slide.SlideDown('" + id + "')", t);
					}
				}
				
				return false;
			},

			SlideDown : function(id){
		
				var obj = Project.Effect.Slide.Divs[id];
				var currentH = obj.offsetHeight;
				currentH += Project.Effect.Slide.Speed;
				
				if(currentH >= obj.H){
					obj.style.height = obj.H + "px";
					obj._timeout = false;
				}else{
					obj.style.height = currentH + "px";
					obj._timeout = setTimeout(function() { Project.Effect.Slide.SlideDown(id); }, Project.Effect.Slide.Speed);
				}
			},

			SlideUp : function(id){
				var obj = Project.Effect.Slide.Divs[id];
				var currentH = obj.offsetHeight;
				currentH -= Project.Effect.Slide.Speed;
				if(currentH <= 0){
					obj.style.height = "1px";
					obj.style.display = 'none';
					obj._timeout = false;
					
				}else{
					obj.style.height = currentH + "px";
					obj._timeout = setTimeout(function() { Project.Effect.Slide.SlideUp(id); }, Project.Effect.Slide.Speed);
				}
			},

			setOpen : function(id){
				var tmpdiv = Project.Effect.Slide.Divs[id];
				if(tmpdiv){
					tmpdiv.style.display = 'block';
					if(!tmpdiv.H){
						tmpdiv.H = tmpdiv.offsetHeight;
						if(!Project.GetIE6()){
							var paddingtop = Project.GetStyle(tmpdiv, "paddingTop", "padding-top");
							var paddingbtm = Project.GetStyle(tmpdiv, "paddingBottom", "padding-bottom");
							var offset = parseInt(paddingbtm) + parseInt(paddingtop);
							tmpdiv.H = tmpdiv.offsetHeight - offset;
							
						}
					}
					tmpdiv.style.height = tmpdiv.H + "px";
				}
			}
		},

		Alpha : {

			Speed : 25, Divs : document.getElementsByTagName("div"),
			Start : function(id, timeout, status){
				var t = timeout ? timeout : Project.Effect.Slide.Speed;
				var tmpdiv = Project.GetDiv(id);
				if(tmpdiv._alpha >= 0){
					if(status == "up"){
						tmpdiv.style.display = "block";
						if(tmpdiv._timeout) clearTimeout(tmpdiv._timeout);
						tmpdiv._timeout = setTimeout("Project.Effect.Alpha.FadeUp('" + id + "')", t);
					}
					if(status == "dn"){
						if(tmpdiv._timeout) clearTimeout(tmpdiv._timeout);
						tmpdiv._timeout = setTimeout("Project.Effect.Alpha.FadeDn('" + id + "')", t);
					}
				}else{
					tmpdiv._alpha = 100;
					tmpdiv._timeout = setTimeout("Project.Effect.Alpha.FadeDn('" + id + "')", t);
				}

				
				
				return false;
			},

			FadeUp : function(id){
				var obj = Project.GetDiv(id);
				var current = obj._alpha;
				current += Project.Effect.Alpha.Speed;
				obj._alpha = current;
				
				if(current >= 100){
					obj._alpha = 100;
					Project.Effect.Alpha.SetAlpha(obj, 100);
					obj._timeout = false;
				}else{
					Project.Effect.Alpha.SetAlpha(obj, current);
					obj._timeout = setTimeout(function() { Project.Effect.Alpha.FadeUp(id); }, Project.Effect.Alpha.Speed);
				}
				
			},

			FadeDn : function(id){
				var obj = Project.GetDiv(id);
				var current = obj._alpha;
				current -= Project.Effect.Alpha.Speed;
				obj._alpha = current;

				

				if(current <= 25){
					//obj.style.display = "none";
					current = 25;
					obj._alpha = 25;
					//alert(obj._alpha);
					Project.Effect.Alpha.SetAlpha(obj, obj._alpha);
					obj._timeout = false;
				}else{
					Project.Effect.Alpha.SetAlpha(obj, current);
					obj._timeout = setTimeout(function() { Project.Effect.Alpha.FadeDn(id); }, Project.Effect.Alpha.Speed);
				}

				//Project.Effect.Alpha.trace(obj._alpha + "<br>");
				
			},

			SetAlpha : function(obj, opacity) {

				var clientPC = navigator.userAgent.toLowerCase();
				var is_ie = ((clientPC.indexOf("msie") != -1) && (clientPC.indexOf("opera") == -1));
				
				if(!is_ie) opacity = opacity / 100;
			
				obj.style.MozOpacity = opacity;
				obj.style.opacity = opacity;
				obj.style.filter = "alpha(opacity=" + opacity + ")";

			},

			trace : function(e){
				Project.GetDiv('raport').innerHTML += e;
			}
			
		}
	},

	/*####################################
	## -> ACCOUNT
	#####################################*/

	

	Unsubscribe : function(form){
		if(Project.Validate(form)){
			Project.SetLoader();
			xajax_Unsubscribe(xajax.getFormValues(form));
		}
		return false;
	},

	GetDiv : function(id){
		var divs = document.getElementsByTagName("div");
		var tmp = divs[id] ? divs[id] : false;
		return tmp;
	},

	GetError : function(id){
		var e = new Project.ValidateErrors();
		var tmp = e[id] ? e[id] : false;
		return tmp;
	},

	GetStyle : function(obj, IES, CSS){
		if(obj.currentStyle){
			return obj.currentStyle[IES];
		}else if(window.getComputedStyle){
			var compStyle = window.getComputedStyle(obj, "");
			return compStyle.getPropertyValue(CSS);
		}
		return 0;
	},
	
	GetIE6 : function(){
		var version = navigator.appVersion.toLowerCase().split(";");
		if(version[1].indexOf("msie") != -1){
			var tmp = version[1].split("msie");
			if(parseInt(tmp[1]) < 7){
				return true;
			}
		}
		return false;
	},

	Click : function(link){
		window.location = link;
	},

	Validate : function(idform){

		var form = document.forms[idform];
		var checked = true;
		var payment = new Array();
		var error = Project.GetError('Start');

		for(var i = 0; i < form.elements.length; i++){
			var element = form.elements[i];
			var e = Project.GetError(element.name);

			if(element.value == "" && e){
				error += "\n- " + e;
				checked = false;
			}else{
				if(element.name == "email" || element.name == "n_email"){
					var x = element.value;
					var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
					if(!filter.test(x)){
						error += "\n- " + Project.GetError('correct_email');
						checked = false;
					}
				}
			}
		}

		if(!checked) alert(error);
		return checked;
	},

	ValidateErrors : function(){
		this.Start = 'Błąd:';
		this.sendername = 'Nie wpisano imienia';
		this.mailcontent = 'Nie wpisano treści';
		this.email = this.n_email = 'Nie wpisano adresu e-mail';
		this.correct_email = 'Niepoprawny format adresu e-mail';
		this.search_artist = 'Nie wpisano imienia/nazwy artysty';

	}
}
