var jso={
  gI:function(a,b){
    // get Element by ID
    if(!b)b=document;
    return b.getElementById(a)
  },
  gT:function(a,b,i){
    // get Elements by Tag Name
    if(!b)b=document;
    b=b.getElementsByTagName(a)
    a=[]
    for(i=0;i<b.length;i++)a[a.length]=b[i];
    return a
  },
  gAV:function(a,b,c,d){
    // get Elements by Atribute [+ Value]
    if(!a)a="*";
    if(!b)b=document;
    if(!c)return false;
    b=b.getElementsByTagName(a)
    a=[];
    var i;
    for(i=0;i<b.length;i++)if(b[i][c]&&(!d||b[i][c].indexOf(d)>-1))a[a.length]=b[i];;
    return a
  },
  cE:function(a,b,c,d,e,i){
    // create Element
    a=document.createElement(a)
    if(b)for(i in b)a[i]=b[i];;
    if(c)for(i in c)if(typeof c[i]!="function")a.appendChild(c[i].nodeType?c[i]:document.createTextNode(c[i]));;;
    if(e)for(i in e)jso.aE(a,i,e[i]);;
    if(d)d.appendChild(a);
    return a
  },
  gE:function(x){
    // get Element from Event
    x=x||window.event;
    x=x.target||x.srcElement;
    if(x.nodeType==3)x=x.parentNode;
    return x
  },
  aE:function(O,E,F){
    // add Event to Element
    return(O.x=O.addEventListener)?O.x(E,F,0):(O.x=O.attachEvent)?O.x('on'+E,F):!1;
  },
  rE:function(O,E,F){
    // remove  Event from Element
    return(O.x=O.removeEventListener)?O.x(E,F,0):(O.x=O.detachEvent)?O.x('on'+E,F):!1;
  }
}

var mega={
  start:function(){
    var search_input=jso.gI("search_field")
    if(search_input){
      jso.aE(search_input,"focus",mega.form_focus)
      jso.aE(search_input,"blur",mega.form_blur)
    }
    var my_email_input=jso.gI("my_email")
    if(my_email_input){ 
      jso.aE(my_email_input,"focus",mega.form_focus)
      jso.aE(my_email_input,"blur",mega.form_blur)
    }
    var friends_email_input=jso.gI("friends_email")
    if(friends_email_input){
      jso.aE(friends_email_input,"focus",mega.form_focus)
      jso.aE(friends_email_input,"blur",mega.form_blur)
    }
    var party_search_input=jso.gI("party_search")
    if(party_search_input){
      jso.aE(party_search_input,"focus",mega.form_focus)
      jso.aE(party_search_input,"blur",mega.form_blur)
    }
    
    var height_elements = document.getElementsByTagName("div");
    var proper_height_elements =new Array();
    var p = new Array();
    var p_klub =  new Array();
    for (i=0; i<height_elements.length; i++){
    	if (height_elements[i].className == "ie_helper") 
    		proper_height_elements.push(height_elements[i]);
    		
    	var x = document.getElementsByTagName("p");
    	 	for (z=0; z<x.length; z++){
    			if (x[z].className == "title") 
    			p.push(x[z]);
    			
    			if (x[z].className == "klub") 
    			p_klub.push(x[z]);
    		}
    }
    var height_val=0;
   	var p_height_val=0;
   	var klub_height_val=0;
   		
     for (i=0; i<p.length; i++){
     	if (p_height_val < p[i].offsetHeight)
     			p_height_val = p[i].offsetHeight;
     		}
			for (i=0; i<p.length; i++){
				p[i].style.minHeight=p_height_val+"px";
				if (navigator.appName=="Microsoft Internet Explorer")
					p[i].style.height=p_height_val+"px";
			}
			
     for (i=0; i<p_klub.length; i++){
     	if (klub_height_val < p_klub[i].offsetHeight)
     			klub_height_val = p_klub[i].offsetHeight;
     		}
			for (i=0; i<p_klub.length; i++){
				p_klub[i].style.minHeight=klub_height_val+"px";
				if (navigator.appName=="Microsoft Internet Explorer")
					p_klub[i].style.height=klub_height_val+"px";
			}
			
     for (i=0; i<proper_height_elements.length; i++){
     	if (height_val < proper_height_elements[i].offsetHeight)
     		height_val = proper_height_elements[i].offsetHeight;
     		}
			for (i=0; i<proper_height_elements.length; i++){
				proper_height_elements[i].style.minHeight=height_val+"px";
				if (navigator.appName=="Microsoft Internet Explorer")
					proper_height_elements[i].style.height=height_val+"px";
			}
			
 //linki na opisie klubu
 		var linki = document.getElementsByTagName('h4');
 		  links = new Array();
 		  var x = 0;
 		    for (i=0; i<linki.length; i++){
    			if (linki[i].className == "marker"){ 
    			var link = linki[i]
    			links.push(link);
    			link.className+=" proper";
    			link.style.left=x++*73+"px";
    			
    			if (link.parentNode.className!="active"){
    					if (navigator.appName=="Microsoft Internet Explorer")
    						link.nextSibling.style.display="none";
    					else link.nextSibling.nextSibling.style.display="none";
    					link.style.top="0";
    					link.style.borderBottom="1px solid #ccc";
    				} 
    				link.onclick=mega.display_change;	
    			}
    		}
  }, 
 
 display_change:function(e){
 	if (this.parentNode.className!="active"){
 		this.parentNode.className="active";
 		
 		if (navigator.appName=="Microsoft Internet Explorer"){
 			this.nextSibling.style.display="block";
 			this.nextSibling.style.top="20px";
 		}
 		else {
 			this.nextSibling.nextSibling.style.display="block";
 			this.style.top="-20px";
 		}
 		this.style.borderBottom="0 none";
 		for (i=0; i<links.length; i++){
 			if (links[i]!=this){
 				
 				if (navigator.appName=="Microsoft Internet Explorer"){
 					links[i].nextSibling.style.display="none";
 					this.parentNode.style.position="static";
 				}
 				else links[i].nextSibling.nextSibling.style.display="none"
 				links[i].parentNode.className="";
 				links[i].style.top="0";
 				links[i].style.borderBottom="1px solid #ccc";
 			}
 		}
 	}
 	return false;
 },
 
 form_focus:function(e){
    e = jso.gE(e)
    if(!e.oldValue || e.oldValue == e.value){
      e.oldValue = e.value
      e.value = ""
    }
  },
 form_blur:function(e){
    e = jso.gE(e)
    if(!e.value){
      e.value = e.oldValue
    }
  }
}

jso.aE(window,"load",mega.start)

//-----------------------------------------------------

window.addLoadEvent = function(func) {
   var oldonload = window.onload;
   if (typeof window.onload != 'function') {
      window.onload = func;
   } else {
      window.onload = function() {
         if (oldonload) {
            oldonload();
         }
         func();
      }
   }
}

var linki = {
  main : function() {
    linki = document.getElementsByTagName("a");
    for(i=0; i < linki.length; i++) {
    	if (linki[i].className=='back')
    		linki[i].onclick = back;
    }
  }
}
function back(){
 if (history.length>1) {
  if(document.referrer.indexOf(document.location.hostname)>-1) {
       history.back();
       return false;
    } else {
      return true
    }
  } else {
    return true;
  }
}
window.addLoadEvent(function() { linki.main(); });

//-----------------------------------------------------------------

function loadpage(page_request, containerid){
	if (page_request.readyState == 4 && (page_request.status==200 || window.location.href.indexOf("http")==-1)) {
		document.getElementById(containerid).innerHTML=page_request.responseText;
	}
}

function ajaxpage(url, containerid) {
	var page_request = false;
	if (window.XMLHttpRequest) {// Mozilla, Safari
		page_request = new XMLHttpRequest();
	} else if (window.ActiveXObject){ // IE
		try {
			page_request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				page_request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	} else {
		return false;
	}
	page_request.onreadystatechange = function() {
		loadpage(page_request, containerid);
	}
	page_request.open('GET', url, true);
	page_request.send(null)
}

function adhit(adid) {
	var page_request = false;
	if (window.XMLHttpRequest) {// if Mozilla, Safari etc
		page_request = new XMLHttpRequest();
	} else if (window.ActiveXObject){ // if IE
		try {
			page_request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				page_request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	} else {
		return false;
	} 
	page_request.open('GET', '/ad/hit.do?id=' + adid, true);
	page_request.send(null)
}

function disableForm(theform) {
	if (document.all || document.getElementById) {
		for (i = 0; i < theform.length; i++) {
			var tempobj = theform.elements[i];
			if (tempobj.type.toLowerCase() == "submit" || tempobj.type.toLowerCase() == "reset") 
				tempobj.disabled = true;
		}
		return true;
	} else {
		return false;
	}
}

function openmypage(picturePath, width, height) {
	var myWindow = window.open("", "", "width="+width+",height="+height+",location=0,menubar=0,resizable=0,scrollbars=0,status=0,titlebar=1,toolbar=0,left=100,top=60");
	myWindow.document.write("<HTML><HEAD><TITLE>MegaParty.pl</TITLE></HEAD><BODY marginheight=0 marginwidth=0 topmargin=0 leftmargin=0 rightmargin=0 bottommargin=0 background='/gfx/backGroundMain.gif'><CENTER><IMG SRC='"+picturePath+"' BORDER=0 onClick='window.close();' style='cursor:pointer' ALT='Kliknij aby zamknąć' TITLE='Kliknij aby zamknąć'></CENTER></BODY></HTML>");
}

function textCount(fieldId) {
	var field = document.getElementById(fieldId);
	var maxlimit;
	if (document.getElementById('unicode').checked) {
		maxlimit = 70;
	} else {
		maxlimit = 160;
	}
	if (field.value.length > maxlimit) {
		field.value = field.value.substring(0, maxlimit);
	}
	document.getElementById('counter').value = maxlimit - field.value.length;
}

function changeValue(elementId, delta) {
	var element = document.getElementById(elementId);
	var value = element.value;
	if (delta < 0) {
		for (i = 0; i < -delta; i++) {
			value--;
		}
	} else {
		for (i = 0; i < delta; i++) {
			value++;
		}
	}
	if (value == "NaN" || value < 0) {
		value = 0;
	}
	element.value = value;
}

var Ticker = new Class({
	setOptions: function(options) {
		this.options = Object.extend({
			speed: 5000,
			delay: 5000,
			direction: 'vertical',
			onComplete: Class.empty,
			onStart: Class.empty
		}, options || {});
	},
	initialize: function(el,options){
		this.setOptions(options);
		this.el = $(el);
		this.items = this.el.getElements('li');
		var w = 0;
		var h = 0;
		if(this.options.direction.toLowerCase()=='horizontal') {
			h = this.el.getSize().size.y;
			this.items.each(function(li,index) {
				w += li.getSize().size.x;
			});
		} else {
			w = this.el.getSize().size.x;
			this.items.each(function(li,index) {
				h += li.getSize().size.y;
			});
		}
		this.el.setStyles({
			position: 'absolute',
			top: 0,
			left: 0,
			width: w,
			height: h
		});
		this.fx = new Fx.Styles(this.el,{duration:this.options.speed,onComplete:function() {
			var i = (this.current==0)?this.items.length:this.current;
			this.items[i-1].injectInside(this.el);
			this.el.setStyles({
				left:0,
				top:0
			});
		}.bind(this)});
		this.current = 0;
		this.next();
	},
	next: function() {
		this.current++;
		if (this.current >= this.items.length) this.current = 0;
		var pos = this.items[this.current];
		this.fx.start({
			top: -pos.offsetTop,
			left: -pos.offsetLeft
		});
		this.next.bind(this).delay(this.options.delay+this.options.speed);
	}
});

