/*
 * Several classes are defined in this file
 */

/*********************************************************************/

/**
 * @namespace
 * Class LayersOSButton
 * 
 * This class acts as an abstract class to define common functions and properties of all Buttons
 * <br /><br />
 * Created on 16/07/2009
 * 
 * @class LayersOSLikeButton
 *
 * @property {Object} referal Object this button may perform operations over.
 * @property {Object} displayable Display object (usually a JQuery representation of an HTML piece of code) this button is represented by.
 * 
 * @author "Antonio Perez (tony@layers.com)"
 * @version <1.0>
 *  
 */
function LayersOSButton(){			
	
}

LayersOSButton.prototype._referal;
LayersOSButton.prototype._displayable;


/**
 * LayersOSButtonClick
 * <br />
 * Click event handler for this button.
 * <br />
 * Created on 15/07/2009
 * 
 * @memberOf LayersOSButton
 * @function
 * 
 * @author "Antonio Perez (tony@layers.com)"
 * @version <1.0>
 * 
 */
LayersOSButton.prototype.LayersOSButtonClick = function(){
  	// Always to be redefined by the implementing class
	console.error("Click event not implemented for this class!");	
}

/**
 * enable
 * <br />
 * Function to enable this button. When enabled, this button click event is set. 
 * Also, the displayable object representing this button will have its 'enabled' css class applied. 
 * <br />
 * Created on 15/07/2009
 * 
 * @memberOf LayersOSButton
 * @function
 * 
 * @author "Antonio Perez (tony@layers.com)"
 * @version <1.0>
 * 
 */
LayersOSButton.prototype.enable = function(){
	var me = this;		
	this._displayable.addClass('layersOS_dock_item_clickable');
	this._displayable.removeClass('layersOS_dock_item_disabled');
	this._displayable.removeClass('layersOS_dock_item_selected');
	this._displayable.unbind('click');
	this._displayable.click(
		function(){
			me.LayersOSButtonClick(this);
		}
	);
}

/**
 * disable
 * <br />
 * Function to disable this button. When disable, this button click event is removed. 
 * Also, the displayable object representing this button will have its 'disabled' css class applied. 
 * <br />
 * Created on 15/07/2009
 * 
 * @memberOf LayersOSButton
 * @function
 * 
 * @author "Antonio Perez (tony@layers.com)"
 * @version <1.0>
 * 
 */
LayersOSButton.prototype.disable= function(){
	var me = this;
	this._displayable.removeClass('layersOS_dock_item_clickable');
	this._displayable.addClass('layersOS_dock_item_disabled');
	this._displayable.unbind('click');
}





/***********************************************************************************/


/**
 * @namespace
 * Class LayersOSInfoButton
 * 
 * This button represents the one the user clicks when he wants to view
 * the information of the layer he is viewing.
 * <br /><br />
 * Created on 15/07/2009
 * 
 * @class LayersOSInfoButton
 *
 * @property {Object} referal Object this button may perform operations over.
 * @property {Object} displayable Display object (usually a JQuery representation of an HTML piece of code) this button is represented by.
 * 
 * @author "Antonio Perez (tony@layers.com)"
 * @version <1.0>
 *  
 */
function LayersOSInfoButton(referal, displayable){
	this._referal = referal;
	this._displayable = displayable;
}

// Inherit from LayersOSButton class
LayersOSInfoButton.prototype = new LayersOSButton();


/**
 * LayersOSButtonClick
 * <br />
 * Implementation of LayersOSButtonClick. Click event handle for this button.
 * <br />
 * Created on 15/07/2009
 * 
 * @memberOf LayersOSInfoButton
 * @function
 * 
 * @author "Antonio Perez (tony@layers.com)"
 * @version <1.0>
 * 
 */
LayersOSInfoButton.prototype.LayersOSButtonClick = function(){
  	var me = this;  	
	if( this._displayable.hasClass('layersOS_dock_item_selected') ) {
		// hide window
		$('#layersOS_current_layer_info').attr("style", "display:none");
		this._displayable.removeClass('layersOS_dock_item_selected');
	} else {		
		// hide other windows				
		os.toolbar.hideActiveButtons();
		this._displayable.addClass('layersOS_dock_item_selected');				
		this._referal.drawLayerInfoBox();
	}
}


/*********************************************************************/

/**
 * @namespace
 * Class LayersOSEmailButton
 * 
 * This button represents the one the user clicks when he wants to email
 * the layer he is currently viewing by email.
 * <br /><br />
 * Created on 15/07/2009
 * 
 * @class LayersOSEmailButton
 *
 * @property {Object} referal Object this button may perform operations over.
 * @property {Object} displayable Display object (usually a JQuery representation of an HTML piece of code) this button is represented by.
 * 
 * @author "Antonio Perez (tony@layers.com)"
 * @version <1.0>
 *  
 */
function LayersOSEmailButton(referal, displayable){
	this._referal = referal;
	this._displayable = displayable;	
}

// Inherit from LayersOSButton class
LayersOSEmailButton.prototype = new LayersOSButton();


/**
 * LayersOSButtonClick
 * <br />
 * Implementation of LayersOSButtonClick. Click event handle for this button.
 * <br />
 * Created on 15/07/2009
 * 
 * @memberOf LayersOSEmailButton
 * @function
 * 
 * @author "Antonio Perez (tony@layers.com)"
 * @version <1.0>
 * 
 */
LayersOSEmailButton.prototype.LayersOSButtonClick = function(){
  	var me = this;  
	if( this._displayable.hasClass('layersOS_dock_item_selected') ) {		
		// hide email form
		$('#layersOS_send_email').attr("style", "display:none");
		this._displayable.removeClass('layersOS_dock_item_selected');
	} else {		
		// hide other windows				
		os.toolbar.hideActiveButtons();
		this._displayable.addClass('layersOS_dock_item_selected');				
		this._referal.drawSendEmailForm();
	}
}


/*********************************************************************/

/**
 * @namespace
 * Class LayersOSCommentButton
 * 
 * This button represents the one the user clicks when he wants to comment
 * the layer he is currently viewing.
 * <br /><br />
 * Created on 15/07/2009
 * 
 * @class LayersOSCommentButton
 *
 * @property {Object} referal Object this button may perform operations over.
 * @property {Object} displayable Display object (usually a JQuery representation of an HTML piece of code) this button is represented by.
 * 
 * @author "Antonio Perez (tony@layers.com)"
 * @version <1.0>
 *  
 */
function LayersOSCommentButton(referal, displayable){
	this._referal = referal;
	this._displayable = displayable;
}

// Inherit from LayersOSButton class
LayersOSCommentButton.prototype = new LayersOSButton();


/**
 * LayersOSButtonClick
 * <br />
 * Implementation of LayersOSButtonClick. Click event handle for this button.
 * <br />
 * Created on 15/07/2009
 * 
 * @memberOf LayersOSCommentButton
 * @function
 * 
 * @author "Antonio Perez (tony@layers.com)"
 * @version <1.0>
 * 
 */
LayersOSCommentButton.prototype.LayersOSButtonClick = function(){
  	var me = this; 
	if( this._displayable.hasClass('layersOS_dock_item_selected') ) {
    	// hide window		
        $('#layersOS_comments_box').attr("style", "display:none");
		this._displayable.removeClass('layersOS_dock_item_selected');

    } else {
    	// hide other windows
        os.toolbar.hideActiveButtons();
    	
		// show Comment dock list
        $('#layersOS_comments_box').attr("style", "display:block");
		this._displayable.addClass('layersOS_dock_item_selected');		

    	// Create Layer List Accordion if there is no content
        this._referal.drawCommentsBox();   		
    }	
}


/*********************************************************************/

/**
 * @namespace
 * Class LayersOSLikeButton
 * 
 * This button represents the one the user clicks when he wants to let the system 
 * know that he likes the layer he is viewing.
 * <br /><br />
 * Created on 15/07/2009
 * 
 * @class LayersOSLikeButton
 *
 * @property {Object} referal Object this button may perform operations over.
 * @property {Object} displayable Display object (usually a JQuery representation of an HTML piece of code) this button is represented by.
 * 
 * @author "Antonio Perez (tony@layers.com)"
 * @version <1.0>
 *  
 */
function LayersOSLikeButton(referal, displayable){
	this._referal = referal;
	this._displayable = displayable;	
}

// Inherit from LayersOSButton class
LayersOSLikeButton.prototype = new LayersOSButton();
LayersOSLikeButton.prototype._liked = false;

/**
 * LayersOSButtonClick
 * <br />
 * Implementation of LayersOSButtonClick. Click event handle for this button.
 * <br />
 * Created on 15/07/2009
 * 
 * @memberOf LayersOSLikeButton
 * @function
 * 
 * @author "Antonio Perez (tony@layers.com)"
 * @version <1.0>
 * 
 */
LayersOSLikeButton.prototype.LayersOSButtonClick = function(){
  	var me = this; 	
	try{
		this.disable();
		os.likeLayer();		
	}catch(e){
		console.error(e);
	}
	
}

/**
 * switchIcon
 * <br />
 * Switch this icon image depending on its state
 * <br />
 * Created on 15/07/2009
 * 
 * @memberOf LayersOSLikeButton
 * @function
 * 
 * @author "Antonio Perez (tony@layers.com)"
 * @version <1.0>
 * 
 */
LayersOSLikeButton.prototype.switchIcon = function(){	
  	
	this.enable();
	
	if(this._liked){
		this._displayable.find('img')[1].src = 'http://edita.layers.com/deploy/img/ico_ilikeit_off.gif';
		this._liked = false;
	}else{
		this._displayable.find('img')[1].src = 'http://edita.layers.com/deploy/img/ico_ilikeit_on.gif';
		this._liked = true;
	}	
	
}

/**
 * setState
 * <br />
 * Set this button state depending on if the user liked this layer or not
 * <br />
 * Created on 15/07/2009
 * 
 * @memberOf LayersOSLikeButton
 * @function
 * 
 * @author "Antonio Perez (tony@layers.com)"
 * @version <1.0>
 * 
 */
LayersOSLikeButton.prototype.setState = function(){

	if (os.currentLayer._liked) {
		this._displayable.find('img')[1].src = 'http://edita.layers.com/deploy/img/ico_ilikeit_on.gif';
	}
	
	this._liked = os.currentLayer._liked;
}

/*********************************************************************/



/**
 * @namespace
 * Class LayersOSReportButton
 * 
 * This button represents the one the user clicks when he wants to report a
 * possibly unappropiate layer.
 * <br /><br />
 * Created on 15/07/2009
 * 
 * @class LayersOSLikeButton
 *
 * @property {Object} referal Object this button may perform operations over.
 * @property {Object} displayable Display object (usually a JQuery representation of an HTML piece of code) this button is represented by.
 * 
 * @author "Antonio Perez (tony@layers.com)"
 * @version <1.0>
 *  
 */
function LayersOSReportButton(referal, displayable){	
	this._referal = referal;
	this._displayable = displayable;
}

// Inherit from LayersOSButton class
LayersOSReportButton.prototype = new LayersOSButton();
LayersOSReportButton.prototype._reported = false;

/**
 * LayersOSButtonClick
 * <br />
 * Implementation of LayersOSButtonClick. Click event handle for this button.
 * <br />
 * Created on 15/07/2009
 * 
 * @memberOf LayersOSReportButton
 * @function
 * 
 * @author "Antonio Perez (tony@layers.com)"
 * @version <1.0>
 * 
 */
LayersOSReportButton.prototype.LayersOSButtonClick = function(){
  	var me = this; 	
	try{
		if(me._reported == false){
			me.disable();

			var res = confirm(STRING_accept_report_abuse);

			if (res) {
				os.reportLayer();
			}else{
				me.enable();
				me._reported = true;
				return false;
			}
		}
	
	}catch(e){
		//console.error(e);
	}
}

/**
 * switchIcon
 * <br />
 * Switch this icon image depending on its state
 * <br />
 * Created on 15/07/2009
 * 
 * @memberOf LayersOSLikeButton
 * @function
 * 
 * @author "Antonio Perez (tony@layers.com)"
 * @version <1.0>
 * 
 */
LayersOSReportButton.prototype.switchIcon = function(){	
  	
	this.enable();
	
	if(this._reported){
		this._displayable.find('img')[1].src = 'http://edita.layers.com/deploy/img/ico_report_off.gif';
		this._reported = false;
	}else{
		this._displayable.find('img')[1].src = 'http://edita.layers.com/deploy/img/ico_report_on.gif';
		this._reported = true;
	}	
	
}

/**
 * setState
 * <br />
 * Set this button state depending on if the user reported this layer or not
 * <br />
 * Created on 15/07/2009
 * 
 * @memberOf LayersOSLikeButton
 * @function
 * 
 * @author "Antonio Perez (tony@layers.com)"
 * @version <1.0>
 * 
 */
LayersOSReportButton.prototype.setState = function(){

	if (os.currentLayer._reported){			
		this._displayable.find('img')[1].src = 'http://edita.layers.com/deploy/img/ico_report_on.gif';
	}
	
	this._reported = os.currentLayer._reported;
}


/*********************************************************************/

/**
 * @namespace
 * Class LayersOSNotificationsButton
 * 
 * This button represents the one the user clicks when he wants to read notifications
 * generated by the system in relation to other users activity.
 * <br /><br />
 * Created on 15/07/2009
 * 
 * @class LayersOSNotificationsButton
 *
 * @property {Object} referal Object this button may perform operations over.
 * @property {Object} displayable Display object (usually a JQuery representation of an HTML piece of code) this button is represented by.
 * 
 * @author "Antonio Perez (tony@layers.com)"
 * @version <1.0>
 *  
 */
function LayersOSNotificationsButton(referal, displayable){
	this._referal = referal;
	this._displayable = displayable;
}

// Inherit from LayersOSButton class
LayersOSNotificationsButton.prototype = new LayersOSButton();

/**
 * LayersOSButtonClick
 * <br />
 * Implementation of LayersOSButtonClick. Click event handle for this button.
 * <br />
 * Created on 15/07/2009
 * 
 * @memberOf LayersOSNotificationsButton
 * @function
 * 
 * @author "Antonio Perez (tony@layers.com)"
 * @version <1.0>
 * 
 */
LayersOSNotificationsButton.prototype.LayersOSButtonClick = function(){
  	var me = this; 
	alert('NotificationsButtonClick');
}


/*********************************************************************/

/**
 * @namespace
 * Class LayersOSHelpButton
 * 
 * This button represents the one the user clicks when he wants to get help on ways to use the layerOS toolbar.
 * <br /><br />
 * Created on 15/07/2009
 * 
 * @class LayersOSHelpButton
 *
 * @property {Object} referal Object this button may perform operations over.
 * @property {Object} displayable Display object (usually a JQuery representation of an HTML piece of code) this button is represented by.
 * 
 * @author "Antonio Perez (tony@layers.com)"
 * @version <1.0>
 *  
 */
function LayersOSHelpButton(referal, displayable){
	this._referal = referal;
	this._displayable = displayable;
}

// Inherit from LayersOSButton class
LayersOSHelpButton.prototype = new LayersOSButton();

/**
 * LayersOSButtonClick
 * <br />
 * Implementation of LayersOSButtonClick. Click event handle for this button.
 * <br />
 * Created on 15/07/2009
 * 
 * @memberOf LayersOSHelpButton
 * @function
 * 
 * @author "Antonio Perez (tony@layers.com)"
 * @version <1.0>
 * 
 */
LayersOSHelpButton.prototype.LayersOSButtonClick = function(){
  	var me = this; 
	alert('HelpButtonClick');
}

/*********************************************************************/

/*
 * Join button and its methods
 */
function LayersOSJoinButton(referal, displayable){
	this._referal = referal;
	this._displayable = displayable;

	var me = this;
	
	this._displayable.click(
		function(){
			me.layersOSJoinButtonClick();
		}
	);
}

LayersOSJoinButton.prototype._referal;
LayersOSJoinButton.prototype._displayable;



LayersOSJoinButton.prototype.endLayersOSJoinButtonClick = function(){

}


LayersOSJoinButton.prototype.layersOSJoinButtonClick = function(){

	if(this._displayable.hasClass('layersOS_dock_item_selected')){
		$('#layersOS_dock_left_window').hide();
		this._referal._joinButton._displayable.removeClass('layersOS_dock_item_selected');
	}else{
		// hide other windows
        os.toolbar.hideActiveButtons();
		
		this._displayable.addClass('layersOS_dock_item_selected');		
		
		this._referal.drawJoinForm();
	}
}




/*********************************************************************/


/*
 * Login button
 */
function LayersOSLoginButton(referal, displayable){
	this._referal = referal;
	this._displayable = displayable;
	
	this.enable();
}

LayersOSLoginButton.prototype = new LayersOSButton();

LayersOSLoginButton.prototype.LayersOSButtonClick = function(){
	if(this._displayable.hasClass('layersOS_dock_item_selected')){
		$('#layersOS_dock_left_window').hide();
		this._referal._loginButton._displayable.removeClass('layersOS_dock_item_selected');
	}else{
		// hide other windows
        os.toolbar.hideActiveButtons();
		
		this._displayable.addClass('layersOS_dock_item_selected');		
		
		this._referal.drawLoginForm();
	}
}

/*********************************************************************/



/*
 * Logout button
 */
function LayersOSLogoutButton(referal, displayable){
	this._referal = referal;
	this._displayable = displayable;

	var me = this;

	this._displayable.append( this._referal._templates.logoutButtonHTML() );
	
	this._displayable.find('#layersOS_logout_link').click(
		function(){
			me.layersOSLogoutButtonClick();
			return false;
		}		
	);
}

LayersOSLogoutButton.prototype._referal;
LayersOSLogoutButton.prototype._displayable;



LayersOSLogoutButton.prototype.endLayersOSLogoutButtonClick = function(){
	
}


LayersOSLogoutButton.prototype.layersOSLogoutButtonClick = function(){
	try{
    	os.logout();
	}catch(e){
		throw(e);
	}    
}



/*********************************************************************/



/*
 * Layer title button
 */
function LayersOSTitleButton(referal, displayable){
	this._referal = referal;
	this._displayable = displayable;
}

LayersOSTitleButton.prototype = new LayersOSButton();

LayersOSTitleButton.prototype.LayersOSButtonClick = function(){	
	
	if( this._displayable.hasClass('layersOS_dock_item_selected') ) {
		// hide window
		$('#layersOS_mylayer_title').hide();
		this._displayable.removeClass('layersOS_dock_item_selected');
	} else {		
		// hide other windows
        os.toolbar.hideActiveButtons();
		this._displayable.addClass('layersOS_dock_item_selected');
		os.toolbar._shareButton.enable();			
		this._referal.drawEditTitleForm();
	}
}

/*********************************************************************/



/*
 * New Layer button
 */
function LayersOSNewLayerButton(referal, displayable){
	this._referal = referal;
	this._displayable = displayable;
	this._isOpen = false;

	var me = this;

	this._displayable.click(
		function(){
			me.layersOSNewLayerButtonClick();
		}
	);
}

LayersOSNewLayerButton.prototype._referal;
LayersOSNewLayerButton.prototype._displayable;
LayersOSNewLayerButton.prototype._isOpen;

LayersOSNewLayerButton.prototype.layersOSNewLayerButtonClick = function(){	
 // alert("LayersOSNewLayerButton.prototype.layersOSNewLayerButtonClick");
  
	try{
		
		if (this._displayable.hasClass('layersOS_add_layer_selected')) {
			// hide window
			$('#layersOS_new_layer_options').hide();
			this._displayable.removeClass('layersOS_add_layer_selected');
			return false;
		}else {
			// hide other windows
    		os.toolbar.hideActiveButtons();								
			this._displayable.addClass('layersOS_add_layer_selected');							
		}
				
		var me = this;
	
		var ownLayer = (os.currentLayer._owner == os.getCurrentIdentity()._displayName || os.currentLayer._owner == '');
	
		var emptyLayer = true;
	
	  console.info("os.currentLayer");
	  console.info(os.currentLayer);
	  console.info("typeof(os.currentLayer)");
	  console.info(typeof(os.currentLayer));
	  console.info("current layer objects");
	  
	  var tmpXX = os.currentLayer.getObjects()
	  console.info(tmpXX);
	  console.info("typeof(getObjects)");
	  console.info(typeof(tmpXX));
	
		for (key in os.currentLayer.getObjects() ){
		  console.info("key is->");
		  console.info(key);
		  if(typeof(key)!="function"){  
		    emptyLayer = false;
//  			break;  
			}
		}
		
		
		// Check whether there is an unsaved layer whose owner is the current user		
		if (ownLayer && !emptyLayer) {
			var res = confirm(STRING_delete_layer_confirm);
			if (res) {
				this.layersOSNewNewLayerButtonClick();		
			}else{
				return true;
			}
		}else if(emptyLayer && this._isOpen){
			this._referal._applicationBar._displayable.hide();
			this._isOpen = false;
		}else if(emptyLayer && !this._isOpen){
			this._referal._applicationBar._displayable.attr("style", "display:block");;
			this._referal._shareButton.enable();
			this._isOpen = true;
		}else if (!ownLayer) {
			// Check whether the current layer owner is not the current user.
			this.showCreateOptions();
		}else {
			// We are creating a brand new layer otherwise
			this.layersOSNewNewLayerButtonClick();
		}		
	
		this._referal._applicationBar.activeAllButtons();
		this._referal._infoButton.disable();
		this._referal._emailButton.disable();	
		this._referal._commentButton.disable();
		this._referal._likeButton.disable();
		this._referal._reportButton.disable();				
	}catch(e){
		throw e;
	}
}

LayersOSNewLayerButton.prototype.layersOSNewNewLayerButtonClick = function(){
	
	console.info("LayersOSNewLayerButton.prototype.layersOSNewNewLayerButtonClick");
	
	if( this._displayable.hasClass('layersOS_dock_item_selected') ) {
		// hide window
		$('#layersOS_new_layer_options').hide();
		this._displayable.removeClass('layersOS_dock_item_selected');
	} else {
		// hide other windows				
		$('#layersOS_new_layer_options').hide();						
		
		try{
		  os.removeCurrentLayer();
  		os.currentLayer._siteURL = os.getBackgroundURL();
  		os.currentLayer._owner = os.currentIdentity._displayName;

  		this._referal._applicationBar.attr("style", "display:block");;
  		this._referal.setTitleText(STRING_layer_default_title);
  		this._referal._layerTitleButton.enable();	
  		this._referal._shareButton.enable();
  		this._referal._shareButton.setLabel(STRING_share_layer);  
		}catch(e){
		  console.info(e);
		}
		
	}		
	
}

LayersOSNewLayerButton.prototype.layersOSReNewLayerButtonClick = function(){
	
	$('#layersOS_new_layer_options').hide();
	var reTitle = os.currentLayer._title;	
	var formerLayerId = os.currentLayer._id;
	
	os.removeCurrentLayer();
		
	os.currentLayer._siteURL = os.getBackgroundURL();
	os.currentLayer._owner = os.currentIdentity._displayName;
	os.currentLayer._dirtyTitle = true;
	os.currentLayer._llink = formerLayerId;	
	os.currentLayer._title = reTitle;
	this._referal.setTitleText(reTitle);
	
	this._referal._layerTitleButton.disable();
	this._referal._shareButton.enable();
	this._referal._applicationBar.attr("style", "display:block");;
}

LayersOSNewLayerButton.prototype.showCreateOptions = function(){
	
	var me = this;
	
	$('#layersOS_new_layer_options').attr("style", "display:block");;
	
	//New Layer
	$('#layersOS_new_layer')
		.click(
			function(){
				me.layersOSNewNewLayerButtonClick();
				//Hide the buttons
				$('.layersOS_list').hide();
			}
		);
		
	//Reply a layer
	$('#layersOS_new_re_layer')
		.click(
			function(){
				me.layersOSReNewLayerButtonClick();
				//Hide The buttons
				$('.layersOS_list').hide();
			}
		);
}

/*********************************************************************/


/*
 * Identities button
 */
function LayersOSIdentitiesButton(referal, displayable){
	this._referal = referal;
	this._displayable = displayable;

	var me = this;

	$('#layersOS_dock_identities_avatar').attr('src', os.getCurrentIdentity()._imageURL)
										 .attr('title', os.getCurrentIdentity()._displayName);
											 
	this._displayable.click(
		function(){
			me.layersOSIdentitiesButtonClick();
		}
	);
}

LayersOSIdentitiesButton.prototype._referal;
LayersOSIdentitiesButton.prototype._displayable;


LayersOSIdentitiesButton.prototype.layersOSIdentitiesButtonClick = function(){			
	
	if( this._displayable.hasClass('layersOS_dock_item_selected') ) {
		// hide window
		$('#layersOS_identities').hide();
		this._displayable.removeClass('layersOS_dock_item_selected');
	} else {		
		// hide other windows				
		$('.layersOS_list,.layersOS_dock_window').hide();
		this._displayable.addClass('layersOS_dock_item_selected');				
		this._referal.drawIdentitiesList();
	}
}

/*********************************************************************/


/*
 * Identity button
 */
function LayersOSIdentityButton(referal, displayable, displayName, imageURL){
	this._referal = referal;
	this._displayable = displayable;
	this._displayName = displayName;
	this._imageURL = imageURL;

	var me = this;

	this._displayable.append(this._referal._templates.identityButtonHTML(this._displayName, this._imageURL));
											 
	this._displayable.find('#' + this._displayName).click(	
		function(){
			me.layersOSIdentityButtonClick();
		}
	);
}

LayersOSIdentityButton.prototype._referal;
LayersOSIdentityButton.prototype._displayable;


/*
 * Change Identity
 */
LayersOSIdentityButton.prototype.layersOSIdentityButtonClick = function(){			
	
	try{
		os.setCurrentIdentity( this._displayName );
					
	}catch(e) {
		//console.info(e);	
	}
                       
}


/*********************************************************************/



/*
 * Application button
 */
function LayersOSApplicationButton(referal, displayable, app){
	this._referal = referal;
	this._displayable = displayable;
	this._app = app;

	//Put the button HTML
	this._displayable.append( this._referal._templates.getApplicationButtonHTML(app) );

	var me = this;

	this._displayable.find('#' + app._id).mousedown(
		function(){
			me.LayersOSApplicationButtonClick(this);
		}
	);
}

LayersOSApplicationButton.prototype._referal;
LayersOSApplicationButton.prototype._displayable;

LayersOSApplicationButton.prototype.LayersOSApplicationButtonClick = function(button){
	var me = this;

	if ($(button).hasClass('layersOS_tool_enabled')) {
	  	// unselect all toolbar icons
		$('#layersOS_toolbar_content .layersOS_tool').removeClass('layersOS_tool_selected').addClass('layersOS_tool_unselected');
		
		// select tool
		$(button).removeClass('layersOS_tool_unselected').addClass('layersOS_tool_selected');
		
		
				
		// enable toolbar icon if it's not a clickone tool		
		if ($(button).hasClass('layersOS_tool_clickonce') == false) {
			$(button).removeClass('layersOS_tool_disabled').addClass('layersOS_tool_enabled');
		}
		
		//me._referal._applicationBar.objectCreate($(button)[0].id);
		try{
			//console.info(me._app._id);
			//console.info( os.openedApps[me._app._id] );
			
			for(key in os.openedApps){
				if (key != me._app._id){
					os.openedApps[key].end();
				}
			}
			// Tell the applications to init without rendering and telling them the backgroundPage is already loaded
			os.openedApps[me._app._id].init(false, true);
			//return this.openedApps[me];
		}catch(e){
			os.loadApp(me._app,me.LayersOSApplicationButtonClickEnd);
		}		
	}
}

LayersOSApplicationButton.prototype.LayersOSApplicationButtonClickEnd = function(appObj){
	console.info(appObj);
	// Tell the applications to init without rendering and telling them the backgroundPage is already loaded
	appObj.init(false, true);
}

/*********************************************************************/

/*
 * Layers over this page button
 */
function LayersOSLayersOverThisPageButton(referal, displayable){
	this._referal = referal;
	this._displayable = displayable;
	
	var me = this;	
	
	$('#layersOS_layers_over_page').find('.layersOS_list_close').click(
		function(){
			$('#layersOS_layers_over_page').hide();
			me._displayable.removeClass('layersOS_dock_item_selected');
			return false;
		}
	)
											 
	this._displayable.click(
		function(){
			me.layersOSLayersOverThisPageButtonClick();
		}
	);
}

LayersOSLayersOverThisPageButton.prototype._referal;
LayersOSLayersOverThisPageButton.prototype._displayable;


LayersOSLayersOverThisPageButton.prototype.layersOSLayersOverThisPageButtonClick = function(){			
	
	if( this._displayable.hasClass('layersOS_dock_item_selected') ) {
    	// hide window
        $('#layersOS_layers_over_page').hide();
		this._displayable.removeClass('layersOS_dock_item_selected');

    } else {
    	// hide other windows
        $('.layersOS_list,.layersOS_dock_window').hide();
    	
		// show layers dock list
        $('#layersOS_layers_over_page').attr("style", "display:block");;
		this._displayable.addClass('layersOS_dock_item_selected');		

    	// Create Layer List Accordion if there is no content
        if( $('#layersOS_layers_over_page_content').find('li').length == 0 ){
			$('#layersOS_layers_over_page').find('.layersOS_list_loading').attr("style", "display:block");;			
            this._referal._layersOverThisPageAccordion = new LayersOSLayerListAccordion(this._referal, $('#layersOS_layers_over_page'));
        }
    		
    }	

}

/*********************************************************************/

/*
 * Layers over this page logged button
 */
function LayersOSLayersOverThisPageLoggedButton(referal, displayable){
	this._referal = referal;
	this._displayable = displayable;
	
	var me = this;	
	
	$('#layersOS_layers_over_page').find('.layersOS_list_close').click(
		function(){
			$('#layersOS_layers_over_page').hide();
			me._displayable.removeClass('layersOS_dock_item_selected');
			return false;
		}
	)
											 
	this._displayable.click(
		function(){
			me.layersOSLayersOverThisPageLoggedButtonClick();
		}
	);
}

LayersOSLayersOverThisPageLoggedButton.prototype._referal;
LayersOSLayersOverThisPageLoggedButton.prototype._displayable;


LayersOSLayersOverThisPageLoggedButton.prototype.layersOSLayersOverThisPageLoggedButtonClick = function(){			
	
	if( this._displayable.hasClass('layersOS_dock_item_selected') ) {
    	// hide window
        $('#layersOS_layers_over_page').hide();
		this._displayable.removeClass('layersOS_dock_item_selected');

    } else {
    	// hide other windows
        $('.layersOS_list,.layersOS_dock_window').hide();
    	
		// show layers dock list		
        $('#layersOS_layers_over_page').attr("style", "display:block");;
		this._displayable.addClass('layersOS_dock_item_selected');		

    	// Create Layer List Accordion if there is no content
        if( this._referal._myLayersOverThisPageAccordion == undefined ){
			$('#layersOS_layers_over_page').find('.layersOS_list_loading').attr("style", "display:block");;			
            this._referal._layersOverThisPageLoggedAccordion = new LayersOSLayerListLoggedAccordion(this._referal, $('#layersOS_layers_over_page'));
        }
    		
    }	

}


/*********************************************************************/

/*
 * My friends layers button
 */
function LayersOSMyFriendsLayersButton(referal, displayable){
	this._referal = referal;
	this._displayable = displayable;
	
	var me = this;	
	
	$('#layersOS_layers_myfriends').find('.layersOS_list_close').click(
		function(){
			$('#layersOS_layers_myfriends').hide();
			me._displayable.removeClass('layersOS_dock_item_selected');
			return false;
		}
	)		
									 
	this._displayable.click(
		function(){			
			me.layersOSMyFriendsLayersButtonClick();
		}
	);
}

LayersOSMyFriendsLayersButton.prototype._referal;
LayersOSMyFriendsLayersButton.prototype._displayable;


LayersOSMyFriendsLayersButton.prototype.layersOSMyFriendsLayersButtonClick = function(){			
	
	if( this._displayable.hasClass('layersOS_dock_item_selected') ) {
    	// hide window
        $('#layersOS_layers_myfriends').hide();
		this._displayable.removeClass('layersOS_dock_item_selected');

    } else {
    	// hide other windows
        $('.layersOS_list,.layersOS_dock_window').hide();
    	
		// show layers dock list
        $('#layersOS_layers_myfriends').attr("style", "display:block");;
		this._displayable.addClass('layersOS_dock_item_selected');		

    	// Create Layer List Accordion if there is no content
        if( this._referal._myFriendsLayersAccordion == undefined ){
			$('#layersOS_layers_myfriends').find('.layersOS_list_loading').attr("style", "display:block");;			
            this._referal._myFriendsLayersAccordion = new LayersOSMyFriendsLayersAccordion(this._referal, $('#layersOS_layers_myfriends'));
        }
    		
    }	

}

/*********************************************************************/
function LayersOSLayerPaginationButtons(referal, displayable, accordion){
	this._referal = referal;
	this._displayable = displayable;
	this._accordion = accordion;

	var me = this;
	
	var pagingHTML = $(this._referal._templates.getLayerPaginationButtonsHTML());
	this._displayable.append(pagingHTML);	
	
	this.enableOrDisableButtons(pagingHTML);
								
	pagingHTML.find('.layersOS_paging_prev').click(			
		function(){
			me._displayable.find('.layersOS_list_layer_icons').fadeOut();
			
			me._displayable.children(":first").slideUp('normal', function(){			
				me._accordion.loadList('previous', displayable);
				me.enableOrDisableButtons(pagingHTML);
			});	
		}		
	);
		
	pagingHTML.find('.layersOS_paging_next').click(
		function(){
			me._displayable.find('.layersOS_list_layer_icons').fadeOut();
			
			me._displayable.children(":first").slideUp('normal', function(){					
				me._accordion.loadList('next', displayable);
				me.enableOrDisableButtons(pagingHTML);
			});								
		}
	);
	
}

LayersOSLayerPaginationButtons.prototype._referal;
LayersOSLayerPaginationButtons.prototype._displayable;
LayersOSLayerPaginationButtons.prototype._accordion;

LayersOSLayerPaginationButtons.prototype.enableOrDisableButtons = function(pagingHTML){
	
	pagingHTML.find('.layersOS_paging_link')
		.attr('disabled','disabled')
		.addClass('layersOS_paging_disabled');
				
	// Check to see if previous paging button should be enabled or not
	if ( (this._accordion.getCurrentIndex() - this._accordion._itemsPerPage) > 0 ){		
		pagingHTML.find('.layersOS_paging_prev')
			.removeAttr('disabled')
			.removeClass('layersOS_paging_disabled');			
	}
	
	// Check to see if next paging button should be enabled or not
	if ( this._accordion.getCurrentIndex()  < this._accordion.getTotalItems() ){
		pagingHTML.find('.layersOS_paging_next')
			.removeAttr('disabled')
			.removeClass('layersOS_paging_disabled');
	}
}


/*********************************************************************/

function LayersOSLayerMultiplePaginationButtons(referal, displayable, accordion, workingIterator, type){
	this._referal = referal;
	this._displayable = displayable;
	this._accordion = accordion;
	this._workingIterator = workingIterator;

	var me = this;
	
	var pagingHTML = $(this._referal._templates.getLayerPaginationButtonsHTML());
	this._displayable.append(pagingHTML);	
	
	this.enableOrDisableButtons(pagingHTML);
								
	pagingHTML.find('.layersOS_paging_prev').click(			
		function(){
			me._displayable.find('.layersOS_list_layer_icons').fadeOut();
			
			me._displayable.children(":first").slideUp('normal', function(){			
				me._accordion.loadList('previous', displayable, type);
				me.enableOrDisableButtons(pagingHTML);
			});	
		}		
	);
		
	pagingHTML.find('.layersOS_paging_next').click(
		function(){
			me._displayable.find('.layersOS_list_layer_icons').fadeOut();
			
			me._displayable.children(":first").slideUp('normal', function(){					
				me._accordion.loadList('next', displayable, type);
				me.enableOrDisableButtons(pagingHTML);
			});								
		}
	);
	
}

LayersOSLayerMultiplePaginationButtons.prototype._referal;
LayersOSLayerMultiplePaginationButtons.prototype._displayable;
LayersOSLayerMultiplePaginationButtons.prototype._accordion;
LayersOSLayerMultiplePaginationButtons.prototype._workingIterator;

LayersOSLayerMultiplePaginationButtons.prototype.enableOrDisableButtons = function(pagingHTML){
	
	pagingHTML.find('.layersOS_paging_link')
		.attr('disabled','disabled')
		.addClass('layersOS_paging_disabled');
				
	// Check to see if previous paging button should be enabled or not
	if ( (this._workingIterator._index - this._accordion._itemsPerPage) > 0 ){		
		pagingHTML.find('.layersOS_paging_prev')
			.removeAttr('disabled')
			.removeClass('layersOS_paging_disabled');			
	}
	
	// Check to see if next paging button should be enabled or not
	if ( this._workingIterator._index  < this._workingIterator._count ){
		pagingHTML.find('.layersOS_paging_next')
			.removeAttr('disabled')
			.removeClass('layersOS_paging_disabled');
	}
}


/*********************************************************************/



/*
 * Publish button
 */
function LayersOSPublishButton(referal, displayable){
	this._referal = referal;
	this._displayable = displayable;
}

LayersOSPublishButton.prototype = new LayersOSButton();

LayersOSPublishButton.prototype.LayersOSButtonClick = function(){
  	var me = this;
	
	this.disable();
	
	try{		
		os.saveLayer(os.getCurrentLayer());
	}catch(e){
		console.error(e);
		message = e.msg;
		os.showError(message);
	} 	  
}

/**
 * toggleText
 * <br />
 * Implementation of LayersOSButtonClick. Click event handle for this button.
 * <br />
 * Created on 15/07/2009
 * 
 * @memberOf LayersOSPublishButton
 * @function
 * 
 * @author "Pablo Casado (pablo.casado@layers.com)"
 * @version <1.0>
 * 
 */
LayersOSPublishButton.prototype.setLabel = function(strLabel){	
	this._displayable.text(strLabel);
}

/*********************************************************************/
