﻿
function showSpinner(show, text, id) {	
	var spinner = $find((id) ? id : "spinner");
	if (spinner) {
		spinner.set_loading(show);
		spinner.set_text(text);
	}
}


/// <summary></summary>
Type.registerNamespace("Tripzoom");


Tripzoom.Spinner = function(element) {
    Tripzoom.Spinner.initializeBase(this, [element]);
    
    //this.initialize();
    //this.set_text('');

    this._src = '';
    this._noloadsrc = null;
    this._defaulttext = '';
    this._text = "";
    this._img = null;
    this._span =  null;
    
	this._showtext = true;
	
	this._clickDelegate = Function.createDelegate(this, this._clickHandler);
}


Tripzoom.Spinner.prototype =  {
	// variables

    
    initialize: function() {
		Tripzoom.Spinner.callBaseMethod(this, 'initialize');
    
		this._img = document.createElement("img");
		this._img.align = 'absmiddle';
		this._img.style.marginRight = '5px';
	    this._element.appendChild(this._img);
	    
		this._img.src = this._src;
	    
	    this._span = document.createElement("span");
	    this._element.appendChild(this._span);
		
		this.set_loading(true);
		
		if (this._clickDelegate) 
			$addHandler(this.get_element(), 'click', this._clickDelegate);
		
		
    },

	dispose: function() {
		// enter code to release object here
	
		//$removeHandler(this.get_element(), "click", this._clickDelegate);
		this._clickDelegate = null;
	
		Tripzoom.Spinner.callBaseMethod(this, 'dispose');
	},
	
	add_click : function(handler) {
		this.get_events().addHandler("click", handler);
	},
	
	remove_click : function(handler) {
		this.get_events().removeHandler("click", handler);
	},
	
	_clickHandler : function() {
        /// <summary>
        /// Raise the <code>click</code> event
        /// </summary>
        /// <returns />
        var handlers = this.get_events().getHandler('click');
        if (handlers) {
            handlers(this, Sys.EventArgs.Empty);
        }
    },	
	
	
	
	
  
	get_showtext : function() { 
		return this._showtext;
	}, 
	set_showtext : function(value) { 
		this._showtext = value;
	}, 
	
	get_src: function() { 
		return this._src;
	},
	
	set_src: function(value) {
		if (this._src != value) { 
			this._src = value;
			
			this.raisePropertyChanged('src');
		}
	},
	
	get_text: function() { 
		return this._text;
	},
	
	get_noloadsrc: function() {
		return this._noloadsrc;
	},
	
	set_noloadsrc: function(value) { 
		if (this._noloadsrc != value) { 
			this._noloadsrc = value;
			
			this.raisePropertyChanged('noloadsrc');
		}
	},
	
	set_loading : function(value) { 
		if (value)	{
			this.set_visible(true);
			this._img.src = this._src;
		} else if (this._noloadsrc) {
			this._img.src = this._noloadsrc;
		} else {
			this.set_visible(false);
		}
	},   
	
	set_text: function(value) {
		if (this._text != value) { 
			this._text = (value) ? value : "";
			
			if (this._showtext)
				this._span.innerHTML = this._defaulttext + " " + ((this._text) ? this._text : "");
			
			this.raisePropertyChanged('text');
		}
	},	
	
	get_defaulttext: function() { 
		return this._defaulttext;
	},
	
	set_defaulttext: function(value) {
		if (this._defaulttext != value)  {
			this._defaulttext = (value) ? value : "";				
			
			if (this._showtext) 
				this._span.innerHTML = this._defaulttext;
			this.raisePropertyChanged('defaulttext');
		}
	}		
	
}

/*
Tripzoom.Spinner.descriptor = { 
	properties : [	{ name: 'src', type: String },
					{ name: 'noloadsrc', type: String },
					{ name: 'defaulttext', type: String },
					{ name: 'text', type: String },
					{ name: 'showtext', type: Boolean }
				 ],
	events : [
		{
			name: 'click'
		}
	]
	
}
*/

Tripzoom.Spinner.registerClass('Tripzoom.Spinner', Sys.UI.Control);

