/*
	Imprezja - JavaScripts
	Grzegorz Kaliciak, grzegorz@kaliciak.net / etrust.pl
	v1.0.0, 12.08.2007
*/

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}

function change_bg(x) {
  var div2 = document.getElementById('menu');
  div2.style.backgroundPosition='0 '+x;
}

function m_show(id) {
	document.getElementById('m_zima').style.display='none';
	document.getElementById('m_wiosna').style.display='none';
	document.getElementById('m_lato').style.display='none';
	document.getElementById('m_jesien').style.display='none';
	document.getElementById(id).style.display='';
}









function inputLabelFromLabel(input_id,label_id) {
	if (!document.getElementById) return false;
	if (!document.getElementById(input_id)) return false;
	if (!document.getElementById(label_id)) return false;
	var textInput = document.getElementById(input_id);
	var label = document.getElementById(label_id);
	var text = label.innerHTML;
	textInput.value = text;
	textInput.onfocus = function() {
		if (this.value == text) {
			this.value = '';
		}
	}
	textInput.onblur = function() {
		if (this.value == '') {
			this.value = text;
		}
	}
}


addLoadEvent(function(){
	inputLabelFromLabel("login","login_l");
	inputLabelFromLabel("pass","pass_l");
	inputLabelFromLabel("search","search_l");
	inputLabelFromLabel("jakie","jakie_l");
	inputLabelFromLabel("email","email_l");
	inputLabelFromLabel("email_od","email_od_l");
	inputLabelFromLabel("email_do","email_do_l");
});




var techbox = Class.create();

techbox.prototype = {

	yPos : 0,
	xPos : 0,

	initialize_orig: function(ctrl) {
		this.content = ctrl.href;
		Event.observe(ctrl, 'click', this.activate.bindAsEventListener(this), false);
		ctrl.onclick = function(){return false;};
	},
	
	initialize: function(obj, url) {
		this.content = url;
		this.obj = obj;
		if (url == false) {
			//alert(obj.innerHTML);
			if (obj.tagName == 'FORM') {
				Event.observe(obj, 'submit', this.activate.bindAsEventListener(this), false);
			} else {
				Event.observe(obj, 'click', this.activate.bindAsEventListener(this), false);
			}
			//obj.onclick = this.activate.bindAsEventListener(this);
			//lbox[i].onsubmit = function() {return anonumous_thru(lbox[i], 'accounts/login_ajax') };
			this.content = loginpath ? loginpath : 'accounts/login_ajax';		
		} else {
			this.activate(null);
		}
		return false;
	},
	
	// Turn everything on - mainly the IE fixes
	activate: function(e){
		if (e) Event.stop(e);
		this.displayLightbox();
		return false;
	},
	
	displayLightbox: function(display){
		if ($('wrap')) $('wrap').style.display = 'none';
		if ($('content-left')) $('content-left').style.display = 'none';
		if($('techbox')) Element.remove($('techbox'));
		addTechboxMarkup($('content'));
		$('techbox').techbox = this;
		$('tbLoadMessage').show();
		if(display != 'none') this.loadInfo();
	},
	
	// Begin Ajax request based off of the href of the clicked linked
	loadInfo: function() {
		var myAjax = new Ajax.Request(
        		this.content,
        		{method: 'post', parameters: "", onComplete: this.processInfo.bindAsEventListener(this)}
		);
		
	},
	
	// Display Ajax response
	processInfo: function(response){
		//alert("="+response.responseText+"=");
		if (response.responseText.match(/^OK/)) { 
			//window.location = this.next;
			//alert('OK');
			if (this.obj.href) {
				window.location = this.obj.href;
			} else if (this.obj.nodeName == 'FORM') {
				this.obj.submit();
			}
			//alert('OK2');
			this.deactivate();
		}
		if($('lbContent')) Element.remove($('lbContent'));
		info = "<div id='lbContent'>" + response.responseText + "</div>";
		new Insertion.Before($('tbLoadMessage'), info)
		$('tbLoadMessage').hide();
		$('techbox').className = "done";	
		this.actions();			
	},
	
	// Search through new links within the lightbox, and attach click event
	actions: function(){
		lbActions = document.getElementsByClassName('lbAction');

		for(i = 0; i < lbActions.length; i++) {
			Event.observe(lbActions[i], 'click', this[lbActions[i].rel].bindAsEventListener(this), false);
			lbActions[i].onclick = function(){return false;};
		}

	},
	
	// Example of creating your own functionality once lightbox is initiated
	submit: function(e){

	   $('ajax_login_form').request({onComplete: this.processInfo.bindAsEventListener(this)});
	 
	},
	
	insert: function(e){
	   link = Event.element(e);
	   if (!link.href) link = link.parentNode;
	   //Element.remove($('lbContent'));
	   //alert(link.href);
	   var myAjax = new Ajax.Request(
			  link.href,
			  {method: 'get', parameters: "", onComplete: this.processInfo.bindAsEventListener(this)}
	   );
	 
	},
	
	// Example of creating your own functionality once lightbox is initiated
	deactivate: function(){
		Element.remove($('lbContent'));
		if ($('wrap')) $('wrap').style.display = '';
		if ($('content-left')) $('content-left').style.display = '';
		Element.remove($('techbox'));

	}
}


function techbox_initialize(){
	//addLightboxMarkup();
	if (is_anonymous()) {
		lbox = document.getElementsByClassName('login_required_techbox');
		for(i = 0; i < lbox.length; i++) {
			valid = new techbox(lbox[i], false);
			
		}
	}
}

function addTechboxMarkup(obj) {
	lb					= document.createElement('div');
	lb.id				= 'techbox';
	lb.className 	= 'loading';
	lb.innerHTML	= '<div id="techpad" style="padding: 100px 0 0 0px;"><div id="tbLoadMessage" style="height: 300px; margin-left: 300px;">' +
						  '<p>Wczytywanie...</p>' +
						  '</div></div>';
	obj.appendChild(lb);
}

Event.observe(window, 'load', techbox_initialize, false);










