/* Indicator  base on Prototype Library  */
/* var it = new SMC.Indicator({
			container: "container"
		});
	it.init(it);
	----------------------------------------------------------------- */
if (typeof SMC == 'undefined') {
 SMC = {};
}
SMC.Indicator = Class.create();
SMC.Indicator.prototype = {
  initialize:  function (options){
      this.setOptions(options);
      //checking container
	if (this.options.container != window) {
		this.options.x = null;
		this.options.y = null; 
		this.options.width = null;
		this.options.height = null;
		this.options.fixed = false;	
	}
  },
   setOptions: function(options) {
      this.options = {
    		zIndex : 1000,
			x :     null,
			y :     null,
			width : null,
			height : null,
			fixed : false,
			container : window,
			opacity : 0.7,
			iconPath : 'url(/images/bg/activity.gif)'
      }
      Object.extend(this.options, options || {});
   },
   
   show: function(e) {
   	//if modal element wasn't created let's do it
	if (!this.container) {
		this.create();
	}
	
	//setting new z-index
	var zIndex = this.options.zIndex;
	this.indicator.style.zIndex = zIndex;
	this.container.style.zIndex = zIndex - 1;
	if (this.WCH) {
		this.WCH.style.visibility = "";
		this.WCH.style.zIndex = zIndex - 2;
	}
	//making it visible
	Element.show(this.container);
	Element.show(this.indicator);
	//pointing that modal cover is visible
	this.visible = true;
	//if container is not window then we need start sticking
	if (this.options.container != window) {
		var self = this;
		//we try to update the size and position of
		//modal cover to cover container
		var update = function() {
			self.update();
		}
		if (!this.interval) {
			this.interval = setInterval(update, 100);
		}
		this.update();
	} else {
		//getting sizes
		var dim = document.body.getDimensions();
		var width = this.options.width || dim.width;
		var height = this.options.height || dim.height;

		//getting position
		var x = this.options.x || 0;
		var y = this.options.y || 0;
		//setting sizes
		this.setWidth(width);
		this.setHeight(height);
		//setting position
		this.setPosition(x, y);
	}
   },
   create: function() {
   	        
   		this.options.container = $(this.options.container) || window;
   	    //creating WCH
	    this.WCH = SMC.Utils.createWCH();
   	    //creating container
   	    var html = '<div id="smc-indicator-container" class="smc-indicator-container"></div>';
	    new Insertion.Bottom (document.body, html);
	    //making it absolutely positioned
	    this.container = $('smc-indicator-container');
	    this.container.update(' ');
		var st = this.container.style;
		st.display = "none";
		st.position = "absolute";
		st.zIndex = this.options.zIndex - 1;
		st.backgroundColor = '#ffffff';
		//creating indicator
	    new Insertion.Bottom (document.body, 
		 '<div id="smc-indicator-label-box" class="smc-indicator-box">loading </div>'
		 );
		 this.indicator = $("smc-indicator-label-box");
		//styling it
		var st = this.indicator.style;
		st.position = "absolute";
		st.display = "none";
		st.zIndex = this.options.zIndex;
		//st.backgroundColor = '#ffffff';
		st.width = '220px';
		st.height = '30px';
		st.fontWeight = 'bold';
		st.color = '#666666';
		st.textAlign ='center';
		st.paddingTop = '20px';
		st.backgroundImage = this.options.iconPath;
		st.backgroundRepeat = 'no-repeat';
		
		// Set opacity 
		if (this.container.filters) {
			if (!this.container.style.filter.match(/alpha/i)) {
				this.container.style.filter += ' alpha(opacity=' + (this.options.opacity * 100) + ')';
			} else if (this.container.filters.length && this.container.filters.alpha) {
				this.container.style.filter = this.container.style.filter.replace(/alpha\(opacity=\d+\)/ig, 'alpha(opacity=' + (Math.floor(this.options.opacity * 100)) + ')')
			}
		} else {  
				this.container.style.opacity = this.container.style.MozOpacity = this.options.opacity;
	
		}

   },
   update: function() {
   		if (this.options.container != window && this.visible) {
		var offs = Element.cumulativeOffset(this.options.container);
		var dim  = Element.getDimensions(this.options.container);
		this.setWidth(dim.width);
		this.setHeight(dim.height);
		this.setPosition(offs.left, offs.top);
	}
   },
   hide: function(destroy) { 
   		//clearing interval
   	this.setHeight(1);	
	if (this.options.container != window) {
		clearInterval(this.interval);
		this.interval = null;
	}
	//hiding elements
	if (this.container) Element.hide(this.container);
	if (this.indicator) Element.hide(this.indicator);
	SMC.Utils.hideWCH(this.WCH);
	//destroying if needed
	if (destroy) {
		if (this.WCH){
			if (this.WCH.outerHTML) {
				this.WCH.outerHTML = "";
			} else {
				SMC.Utils.destroy(this.WCH);
			}
		}
		/*if (this.container.outerHTML) {
			this.container.outerHTML = "";
		} else { */
			SMC.Utils.destroy(this.container);
			SMC.Utils.destroy(this.indicator);
		//}
		this.WCH = null;
		this.container = null;
		this.indicator = null;
	}
	//pointing that modal cover is visible
	this.visible = false;
   },
   setWidth: function(width){
   		if (!this.container) {
		return;
	}
		this.container.style.width = width + "px";
		if (this.WCH) {
			this.WCH.style.width = width + "px";
		}
	
   },
   setHeight: function (height){
   		if (!this.container) {
		return;
	}
		this.container.style.height = height + "px";		
		if (this.WCH) {
			this.WCH.style.height = height + "px";
		}
   },
   setPosition: function(x,y){
   		if (!this.container) {
		return;
	}
		this.container.style.left = x + "px";
		this.container.style.top = y + "px";
		if (this.WCH) {
			this.WCH.style.left = x + "px";
			this.WCH.style.top = y + "px";
		}
		var offs = Element.cumulativeOffset(this.options.container);
		//updating indicator position.
		var left = offs.left + Math.round((this.container.offsetWidth - this.indicator.offsetWidth) / 2);
		this.indicator.style.left = left + "px";
		//updating indicator position.
		var top = offs.top + Math.round((this.container.offsetHeight - this.indicator.offsetHeight) / 2);
		this.indicator.style.top = top + "px"; 

	
   },
   start: function (message){
   		//pointing that it is active
		this.active = true;
		if (!this.indicator) {
			this.create();
		}
		if (!message)
		 {
		 	message = 0; // default value
		 }
		var lang = document.getElementsByTagName("body")[0].getAttribute("lang");
		if (!lang) lang = 'en';
		var langText = {};
langText = { en : ['Loading', 'Error!!!, Connection Problem. Please tried again', 'Sending'],
		     es : ['Cargando', 'Ha ocurrido un error de conexión, por favor inténtelo nuevamente', 'Enviando'],
		     de : ['Es wird heruntergeladen', 'Fehler bei dem Anschluss. Versuchen Sie es bitte wieder', 'Es wird abgesendet'],
		     fr : ['Chargeant', 'Une erreur de connexion s\'est produite, merci de réessayer', 'Envoi en cours'] 
		   };
		//fillig with message
		this.indicator.update(langText[lang][message]);
		//showing it
		this.show();
   },
   stop: function (){
   		//pointing that it is inactive
		this.active = false;
		//destroying it
		this.hide(true);
   },
   isActive: function() {
   	return this.active;
   }
   	
};

