/**
 * Проект:       TellMeDay.com
 * Файл:         $HeadURL: https://svn.gvp.gov.ru/svn/tellmeday/trunk/templates/js/prototype_extension_v1.js $
 * 
 * Назначение:   расширения prototype для проекта
 *
 * @автор        Мамонтов Андрей <andrvm@andrvm.ru>
 * @copyright    2008 Andrey Mamontov (andrvm)
 * 
 * @date		 $Date: 2010-05-25 22:23:46 +0400 (Вт, 25 май 2010) $
 * @revision	 $Revision: 115 $
 * @changeby	 $Author: andrvm $
 * 
 */

 /* protoload 0.1 beta by Andreas Kalsch
 * last change: 09.07.2007
 *
 * This simple piece of code automates the creating of Ajax loading symbols.
 * The loading symbol covers an HTML element with correct position and size - example:
 * $('myElement').startWaiting() and $('myElement').stopWaiting()
 */
 Protoload = {
	// the script to wait this amount of msecs until it shows the loading element
	timeUntilShow: 250,
	// opacity of loading element
	opacity: 0.8,

	// Start waiting status - show loading element
	startWaiting: function(element, className, timeUntilShow) {
		
		if (typeof element == 'string')
			element = $(element);
		if (className == undefined)
			className = 'waiting';
		if (timeUntilShow == undefined)
			timeUntilShow = Protoload.timeUntilShow;
		
		element._waiting = true;
		
		if (!element._loading) {
			var e = document.createElement('div');
			(element.offsetParent || document.body).appendChild(element._loading = e);
			e.style.position = 'absolute';
			try {e.style.opacity = Protoload.opacity;} catch(e) {}
			try {e.style.MozOpacity = Protoload.opacity;} catch(e) {}
			try {e.style.filter = 'alpha(opacity='+Math.round(Protoload.opacity * 100)+')';} catch(e) {}
			try {e.style.KhtmlOpacity = Protoload.opacity;} catch(e) {}

		}
		
		element._loading.className = className;
		
		window.setTimeout((function() {
			if (this._waiting) {
			
				var left = this.offsetLeft, 
					top = this.offsetTop,
					width = this.offsetWidth,
					height = this.offsetHeight,
					l = this._loading;
					
				l.style.left = left+'px';
				l.style.top = top+'px';
				l.style.width = width+'px';
				l.style.height = height+'px';
				l.style.display = 'inline';
			}
		}).bind(element), timeUntilShow);
	},
	
	// Stop waiting status - hide loading element
	stopWaiting: function(element) {
		if (element._waiting) {
			element._waiting = false;
			element._loading.parentNode.removeChild(element._loading);
			element._loading = null;
		}
	}
 };

 // выравнивание высоты блоков
 Object.extend(Array.prototype, {
 	// автоподбор высоты
  	autoHeight: function(){
  			var tmpH=0;
  			this.each(function(item){
      			$(item).setStyle({height:tmpH+'px'});}.bind(
      			    this.each(function(item){
  						var h_= parseInt($(item).getStyle('height'),  10);	
    					tmpH  = h_ > tmpH ?  h_ : tmpH;
      			    })))
  	},
  	// закругление углов с помощью 4-х элементов
  	curve: function(){
  		   var e1 = e2 = e3 = e4 = null;
   		   this.each(function(item){
   		   	  // создаем элементы span 
   		   	  e1 = document.createElement('span');
   		   	  $(e1).addClassName('tl');
   		   	  e2 = document.createElement('span');
   		   	  $(e2).addClassName('tr');
   		   	  e3 = document.createElement('span');
   		   	  $(e3).addClassName('bl');
   		   	  e4 = document.createElement('span');
   		   	  $(e4).addClassName('br');
   		   	  item.appendChild(e1);
   		   	  item.appendChild(e2);
   		   	  item.appendChild(e3);
   		   	  item.appendChild(e4);
   		   })
 
  	},
  	    
    /**
     * Tooltip
     */
    tooltip: function(param){
    	
    	// html
    	var html 	= $$('html')[0];
    	// body
    	var body 	= document.body;
    	// box
    	var tbox    = null;
    	// cash
    	var cash    = {tbox:[]};
    	// screen width
    	var sw      = html.clientWidth;
    	// screen height
    	var sh      = html.clientHeight;
    	// offsetX (only body position with margin:0 auto)
    	var offsetX = Math.round((sw - body.clientWidth) / 2);
    	// offsetY (only body position with margin:0 auto)
    	var offsetY = Math.round((sh - body.clientHeight) / 2);
    	// element where the event occurred
    	var e = null;
       
    	// create tooltip box
    	function createbox(evt){
    	    		
    		// element where the event occurred
    		e  = evt.target ? evt.target : evt.srcElement;
    		// id for tooltip box in the cache
     	    var id = e.id.indexOf('_') >=0 ? e.id.substr(e.id.indexOf('_') + 1, e.id.length) : cash.tbox.length + 1;
     	 
     	    if(typeof(cash.tbox[id]) != 'undefined') {     	    	
     	    	tbox = cash.tbox[id];     	    	   	    	
     	    }
     	    else{    
     	    	// create tooltip box
     	    	tbox = document.createElement('div');
     	    	// set class
     	    	$(tbox).addClassName('tipbox');
     	    	//
     	    	$(tbox).addClassName(!param ? 'standart' : param); 
     	    	// set attributes
     	    	tbox.setAttribute('id', 'tipbox_' + id);
     	    	// set id (if non exist)
     	    	if(!e.id) e.setAttribute('id', 'ptipbox_'+ id);
     	    	// fill tooltip
     	    	$(tbox).update(e.title);
     	    	// hide title
     	    	e.setAttribute('title', '');
     	    	// add tooltop to chache
     	    	cash.tbox[id] = tbox;
     	           	   
     	    	// add mousemove event handler
     	    	$(tbox).observe('mousemove', posbox.bind(this));    	
    			
     	    };
     	    
     	     //add tooltip to body
     	    body.appendChild(tbox);
   
     	    posbox(evt);	    	
    
    	};
    	
    	// position tooltip
    	function posbox(evt){
    	    		
    		if(tbox == null) return;
    		
    		// element where the event occurred
    		e  = evt.target ? evt.target : evt.srcElement;
    		
    		// tooltip position
     	   	var x  = y = '';
       		// mouse coordinates	
     	   	var gY = (evt.pageY) ? evt.pageY : evt.clientY + (document.documentElement.scrollTop  || body.scrollTop)  - document.documentElement.clientTop;
     	    var gX = (evt.pageX) ? evt.pageX : evt.clientX + (document.documentElement.scrollLeft || body.scrollLeft) - document.documentElement.clientLeft;
     	   
     	     
     	    var realX = gX - offsetX + 1; // kill flicker in firefox (+1)
     	    var realY = gY - offsetY + 1;
     	    
     	    // left || right
     	    if( realX + parseInt($(tbox).getWidth()) > body.clientWidth )
     	    	$(tbox).setStyle({right:(body.clientWidth - realX) + 'px'});
     	    else
     	    	$(tbox).setStyle({left:realX + 'px'});
     	         	         	    
    		$(tbox).setStyle({top:  gY + 'px'});
		     	    		
    	}	
    	
    	// deleting toolbox
    	function delbox(evt){
    	    		    		    		
    		// element where the event occurred
    		e = evt.target ? evt.target : evt.srcElement;
    		// get id from element where event occurred
    		var id = e.id.indexOf('_') >=0  ? e.id.substr(e.id.indexOf('_') + 1, e.id.length) : '';
    		
    		if (!id || $('tipbox_' + id) == null) return;
    	      		
    		// remove tooltim from DOM
    		body.removeChild($('tipbox_' + id));   
        		
    	}
    	
    	this.each(function(item){
    		
    	    item.observe('mouseover', createbox.bind(this));
    		item.observe('mousemove', posbox.bind(this));
    		item.observe('mouseout',  delbox.bind(this));
    		
    	})    	
    	
    }	
 });
 
 // проверка формы на заполнение
 Form.Methods.check = function(form, a){
       var f_ = true;
       ($(form).getElements().each(
            function(item){
 				if (!item.value & item.type!='hidden' & item.type!='button' & item.type!='submit')
                f_ = false;
            }))
       return f_ ? f_ : (alert(a), false);
 }
 
 // определение браузеров (C) jQuery
 var userAgent = navigator.userAgent.toLowerCase();
 
 Prototype.browser = {
 	
	version: (userAgent.match( /.+(?:rv|it|ra|ie)[\/: ]([\d.]+)/ ) || [])[1],
	safari: /webkit/.test( userAgent ),
	opera: /opera/.test( userAgent ),
	msie: /msie/.test( userAgent ) && !/opera/.test( userAgent ),
	mozilla: /mozilla/.test( userAgent ) && !/(compatible|webkit)/.test( userAgent )
	
 }
 
 Element.addMethods();
 Element.addMethods(Protoload);
 Object.extend(Element, Protoload);
