(function($){
	textNode = function(conf){
		this.props = {
			id:			null,
			className:	'textNode',
			width:		'auto',
			height:		'auto',
			text:		'textNode',
			pos:		'center',
			cssClass:	'texto',
			z:			'',
			adjust:		true
		}
		$.extend(this.props,conf);
		this.output = $('<div>'+this.props.text+'</div>').addClass(this.props.cssClass);

		this.text = function(text){
			(text != undefined) ? (this.props.text = text) : (this.props.text = '');
			return this.output.text(this.props.text);
		}
		this.appendTo = function(c){
			this.getOutput().css({'text-align':'center', 'z-Index':this.getConf().z});
			//this.getOutput().width(this.props.width).height(this.props.height);
			this.getOutput().appendTo(c);
			this.getOutput().data('textNode',this);
			return this;
		}
		this.adjust = function(){
			this.getOutput().copySize(this.getOutput().parent());
		}
		this.getOutput = function(){
			return this.output;
		}
		this.pos = function(){
			(this.props.pos =='center') ? (this.getOutput().center()) : void(0);		
		}
		this.width = function(w){
			this.props.width = w;
			this.getOutput().width(this.props.width);
		}
		this.height = function(h){
			this.props.height = h;
			this.getOutput().height(this.props.height);
		}
		this.getConf = function(){
			return this.props;
		}
		return this;
	}//textNode
	
	
	footerBtn = function(conf){
		this.props = {
		   className : 'footerBtn',
			    attr : {id:null},
			    text : 'boton',
			  height : 'auto',
			   width : 'auto',
			cssClass : 'footerBtn',
			  imgPos : 'left',
			 textCss : 'texto',
				   z : '',  
			drawIcon : true,
			   hover : true,
		  hoverClass : 'hover',
		   iconClass : 'icon',
			textAlign: null,
			      url: ''
		}
		$.extend(this.props,conf);

		this.output		= $('<div></div>').addClass(this.props.cssClass).attr(this.props.attr);
		this.textNode	= new textNode({text:this.props.text,adjust:false});
		this.hover		= $('<div></div>').addClass(this.props.hoverClass);
		this.img		= $('<div></div>').css({'position':'absolute'});

		
		this.EvtHover = function(){
			this.getOutput().bind('mouseenter',{target:this}, function(e){
				e.data.target.hover.show(0);
			});
			this.getOutput().bind('mouseleave',{target:this}, function(e){
				e.data.target.hover.hide(0);
			});			
		}
		this.click = function(handler){
			this.output.bind('click',{target:this},handler);	
		}
		this.text = function(t){
			this.textNode.text(t);
		}
		this.getOutput = function(){
			return this.output;
		}
		this.appendTo = function(c){
			this.getOutput().appendTo(c).css({'z-index':this.getConf().z});
			this.drawHover();
			this.textNode.appendTo(this.getOutput());
			(this.drawIcon)			  ? (this.drawIcon()) : void(0);
			(this.getConf().hover)    ? (this.EvtHover()) : void(0);
			(!this.getConf().url=='') ? (this.makeLink()) : void(0);
			return this;
		}
		this.drawHover = function(){
			this.hover.appendTo(this.getOutput()).hide(0);
		}
		this.drawIcon = function(){
			this.img.addClass(this.props.iconClass);
			this.img.appendTo(this.getOutput());
			//this.img.centerY();
		}
		this.toggleControl = function(){
			this.getOutput().toggle(0);
		}
		this.showControl = function(){
			this.getOutput().show(0);
		}
		this.hideControl = function(){
			this.getOutput().hide(0);
		}
		this.getConf = function(){
			return this.props;
		}
		this.textAlign = function(){
			pos = this.getConf().textAlign;
			left = this.hover.getOutput().css('left');
			(pos =='left') ? (this.textNode.getOutput().css({'left':left})) : void(0);
		}
		this.makeLink = function(){
			this.getOutput().bind('click', {obj:this}, function(e){
				var o = e.data.obj;
				o.gotoUrl(o.getConf().url);
			});
		}
		this.gotoUrl = function(url){
			window.open(url);
		}
	}
	showtBox = function(conf){
		
	}

	tweet = function(conf){
		this.props = {
			text:''
		}
		$.extend(this.props,conf);
		this.output = $('<div></div>').addClass('tweet');
		this.wrap   = $('<div></div>').addClass('wrap');
		this.text   = new textNode({text:this.props.text,pos:'center'});
		
		this.appendTo = function(c){
			this.getOutput().appendTo(c);
			this.getOutput().append(this.text.getOutput());
		}
		this.getOutput = function(){
			return this.output;
		}
		this.width = function(w){
			this.getOutput().width(w);		
			this.wrap.width(w);
			return this;
		}
		this.height = function(h){
			this.getOutput().height(h);
			this.wrap.height(h);
			return this;
		}
		this.centerText = function(){
			this.text.getOutput().center();
		}
	}
	
	twitterStream = function(conf){
		this.props = {
		}
		$.extend(this.props,conf);
		this.output			= $('<div></div>').addClass('twitterStream');
		this.twitterlogo	= $('<div></div>').addClass('twitterLogo');
		this.scrollable		= $('<div></div>').addClass('scrollable');
		this.scrollApi		= null; 
		this.items			= $('<div></div>').addClass('items');
		this.tweets			= [];

		this.getOutput = function(){
			return this.output;
		}
		this.appendTo = function(c){
			o = this.getOutput();
			o.appendTo(c);
			o.append(this.twitterlogo.height(o.height()));
			o.append(this.scrollable);
			this.scrollable.append(this.items);
			
			this.addTweet('');
			this.addTweet('');
			this.startScrollable();
		}

		this.addTweet = function(t){
			var tt = new tweet({text:t});
			tt.appendTo(this.items);
			this.tweets[this.tweets.length] = tt;
		}
		this.startScrollable = function(){
			this.scrollable.scrollable({vertical:true, mousewheel:true,circular:true}).autoscroll({autoplay:true});
			this.scrollApi = this.scrollable.data('scrollable');
		}

		this.makeTweet = function(text){
			this.item	= $('<div></div>').addClass('tweet');
			this.item.width(this.getOutput().width()).height(this.getOutput().height());
			var text = new textNode({text:tweet,pos:'center'});
			text.appendTo(item);
			text.getOutput().css({'text-align':'left'});
			return item;
		}

		this.toggleControl = function(){
			this.getOutput().toggle(0);
		}
		this.showControl = function(){
			this.getOutput().show(0);
		}
		this.hideControl = function(){
			this.getOutput().hide(0);
		}
		
		
	}
	
	stickyFooter = function(conf){
		this.props = {
			className:	'stickyFooter',
			text:		'boton',
			height:		'auto',
			width:		'auto',
			cssClass:	'footerBtn',
			expanded:	true
		}
		$.extend(this.props,conf);
				
		this.output				= $('<div></div>').addClass('sticky color-marca');
		this.expandBtn			= new footerBtn({text:'',cssClass:'footerBtn expandBtn'}); 
		this.participateBtn		= new footerBtn({text:'Participar',cssClass:'footerBtn participate'});
		this.shareBtn			= $('<div></div>').attr('id','shareBtn');
		this.twitter			= new twitterStream();
		this.googleBtn          = new footerBtn({text:'',cssClass:'footerBtn googleBtn'});
		this.likeBtn			= new footerBtn({text:'',cssClass:'footerBtn likeBtn'});
		this.menuShare			= new menu({triggerMode:'toggle', drawOnTop:true, overlapTrigger:false, drawHead: false, cssClass:'menu-share',triggerElement:'#shareBtn', triggerClass:'footerBtn compartir', triggerText:'Compartir'});
		this.wrap				= $('<div></div>').addClass('stickywrap');

		this.getOutput = function(){
			return this.output;
		}
		this.getState = function(){
			var state = '';
			(this.props.expanded) ? (state = 'expanded') : (state = 'collapsed'); 
			return state;
		}
		this.getHeight = function(){
			return this.getOutput().height();
		}
		this.getWidth = function(){
			return this.getOutput().width();
		}
		this.center	= function(){
			this.getOutput().centerX().css({'position':'fixed'});
		}
		this.stick = function(){
			//this.wrap.bottomStick();
			this.menuShare.positionate();
		}
		this.toRight = function(){
			this.getOutput().toRight().css({'position':'fixed'});
		}
		this.showExpand = function(){
			this.expandBtn.getOutput().removeClass('expanded').addClass('collapsed');
			this.expandBtn.text('Expandir');
			this.expandBtn.textNode.getOutput().centerX();
		}
		this.showCollapse = function(){
			this.expandBtn.getOutput().removeClass('collapsed').addClass('expanded');
			this.expandBtn.text('');
			this.expandBtn.textNode.getOutput().centerX();
		}
		this.toggleControls = function(show){
			if(show){
				this.menuShare.trigger.showControl();
				this.likeBtn.showControl();
				this.twitter.showControl();	
				//this.participateBtn.showControl();
			}
			else{
				this.menuShare.trigger.hideControl();
				this.likeBtn.hideControl();
				this.twitter.hideControl();
				//this.participateBtn.hideControl();
			}
		}
			
		this.slideCollapse = function(){
			var o = this;
			o.getOutput().animate({bottom: -(o.getOutput().height())}, 300,function(){
				o.collapse();
				$(this).animate({bottom:0});
			});
		}
		this.slideExpand   = function(){
			var o = this;
			o.getOutput().animate({bottom: -(o.getOutput().height())}, 300,function(){
				o.expand();
				$(this).animate({bottom:0});
			});
		}
		this.collapse = function(){
			this.getOutput().removeClass('expanded').addClass('collapsed');
			this.props.expanded = false;
			this.showExpand();
			this.toggleControls(false);
			this.getOutput().trigger('onCollapse');
		}
		this.expand = function(){
			this.getOutput().removeClass('collapsed').addClass('expanded');
			this.props.expanded = true;
			this.showCollapse();
			this.toggleControls(true);
			this.getOutput().trigger('onExpand');
		}
		this.registerEvents = function(){
			$(window).bind('scroll resize'  ,{obj:this}, function(e){
				e.data.obj.menuShare.positionate();
			});
			if(($.browser.msie) && ($.browser.version<7)){
				$(window).bind('scroll resize'  ,{obj:this}, function(e){
					e.data.obj.getOutput().bottomStick();
				});
				this.getOutput().bind('onCollapse onExpand', {obj:this},function(e){
					e.data.obj.getOutput().bottomStick();
				});
			}
			this.expandBtn.getOutput().bind('click',{obj:this},function(e){
				sticky = e.data.obj;
				(sticky.props.expanded) ? (sticky.slideCollapse()) : (sticky.slideExpand());
			});
		}
		this.addShareItems = function(){
			 var url= document.location.href;
			/*http://meneame.net/submit.php?url=URL Meneame
			  http://www.google.com/reader/link?url=URL Buzz
			  http://digg.com/submit?url=URL Digg*/
			this.menuShare.addItem({url:'http://www.facebook.com/share.php?u=' + url,				 cssClass:'item', text:'Facebook',    iconClass:'icon socialicon-menu facebook'});
			this.menuShare.addItem({url:'http://twitter.com/home?status=Leyendo%20' + url,	 cssClass:'item', text:'Twitter',     iconClass:'icon socialicon-menu twitter'});
			//this.menuShare.addItem({url:'http://www.facebook.com/share.php?u=' + url,		     cssClass:'item', text:'Youtube',	 iconClass:'icon socialicon-menu youtube'});
			this.menuShare.addItem({url:'http://www.stumbleupon.com/submit?url=' + url, cssClass:'item', text:'Stumbleupon', iconClass:'icon socialicon-menu stumbleupon'});
			this.menuShare.addItem({url:'http://www.delicious.com/save?jump=yes&url=' + url,										 cssClass:'item', text:'Delicious',   iconClass:'icon socialicon-menu delicious'});
		}
		this.draw = function(){
			var url= document.location.href;
			$('body').prepend(this.getOutput().addClass(this.getState()));
			this.expandBtn.appendTo(this.getOutput());
			this.expandBtn.getOutput().addClass(this.getState());
			this.shareBtn.appendTo(this.getOutput());
			//this.participateBtn.appendTo(this.getOutput());
			//////////////////////////////////////////////////////
			this.likeBtn.appendTo(this.getOutput());
			this.likeBtn.getOutput().empty().html("<div style='width: 400px; height: 45px; overflow: hidden; position: absolute;'><iframe src='http://www.facebook.com/plugins/like.php?href="+ url +"&amp;show_faces=false&amp;width=400&amp;action=like&amp;font=tahoma&amp;colorscheme=light&amp;height=25' scrolling='no' frameborder='0' style='border:none; overflow:hidden; width:400px; height:25px; margin-left: -10px; margin-top:0px;' allowTransparency='true'></iframe></div>");
			/////////////////////////////////////////////////////
			this.googleBtn.appendTo(this.getOutput());
            //this.googleBtn.getOutput().empty().html("<div class='g-plusone' data-size='standard' data-count='true'></div>");
			this.menuShare.draw();
			this.addShareItems();
			this.getOutput().bottomStick();
			this.registerEvents();
			this.collapse();
		}
	}//stickyFooter	

	menu = function(conf){
		if(menu.id_count == undefined) {
            menu.id_count = 1;
        } else {
            menu.id_count ++;
        }
        
        if(menu.instances == undefined) {
            menu.instances = new Object();
        }
        this.menuId = 'menuId_' + menu.id_count;
		menu.instances[this.menuId] = this;

		this.props = {
				className :      'menu',
				 cssClass :          '',
						z :        9999,
				container :   $('body'),
		   triggerElement :        null,
 			 triggerClass :   'trigger',
		   overlapTrigger :        true,
			  closerClass :    'closer',
			   closerText :     'close',
				drawHead  :		   true,
			    drawOnTop :		  false,
			  triggerText :   'trigger',
			 triggerMode  :      'show',
				datafile  :  'data.json'   			
		}
		$.extend(this.props,conf);
		this.expanded = true;
		this.data     = null;
		this.output   = $('<div></div').addClass(this.props.cssClass);
		this.head	  = $('<div></div>').addClass('head');
		this.closer	  = new footerBtn({cssClass: this.props.closerClass,  text: this.props.triggerText});
		this.trigger  = new footerBtn({cssClass: this.props.triggerClass, text: this.props.triggerText});
		this.items    = [];

		this.addItem = function(conf){
			var props = {
				cssClass : 'item',
				    text : '',
				     url : ''
			};
			$.extend(props,conf);
			var item = new footerBtn(props);
			this.items[this.items.length] = item;
			item.appendTo(this.getOutput());
		}	
		this.draw = function(){
			this.trigger.appendTo(this.getTriggerElement().parent());
			(this.props.drawOnTop) ? (this.props.container = this.trigger.getOutput().parent()) : void(0);
			this.output.prependTo(this.props.container).css({'z-index':this.getConf().z});
			this.getTriggerElement().remove();
			(this.props.drawHead) ? (this.drawHead()) : void(0);
			this.closer.appendTo(this.head);
			this.registerEvents();
			this.hide(0);
		}
		this.drawHead = function(){
			this.head.appendTo(this.getOutput());
		}
		this.loadData = function(cual){
			/*var menuApi = this.getApi();
			datafile = this.getConf().datafile;
			$.ajax({cache:false, type:'get', url:datafile,
				success: function(e){
					alert('hola');
					menuApi.setData(e);	
				},
			dataType: 'json'
			});*/
		}
		this.onDataLoad = function(){
			//alert('dataLoaded');
			/*d = this.data;
			$.each(d,function(key,value){
				
			});*/
		}
		this.setData = function(data){
			this.data = data;
			this.onDataLoad();
		}
		this.persist = function(){
			this.getOutput().data('menu', this);
		}
		this.getApi = function(){
			return this.getOutput().data('menu');
		}
		this.getTriggerElement = function(){
			return $(this.getConf().triggerElement);
		}
		
		this.positionate = function(){
			(this.props.overlapTrigger) ? (this.overlapTrigger()) : void(0);
			(this.props.drawOnTop)		? (this.drawOnTop())	  : void(0);
		}
		this.drawOnTop = function(){
			var bottom  = this.trigger.getOutput().height();
			var left    = this.trigger.getOutput().position().left;
			this.getOutput().css({'bottom':bottom, 'top':'', 'left':left});
		}
		this.overlapTrigger = function(){
			this.getOutput().overlap(this.trigger.getOutput());	
		}
		this.show = function(t){
			this.expanded = true;
			this.positionate();
			//this.getOutput().show(0).fixate();
			this.getOutput().fadeIn(t,function(){
				//$(this).fixate();
			});
		}
		this.hide = function(t){
			this.expanded = false;
			this.getOutput().fadeOut(t,function(){
				$(this).unStick();
			});
		}
		this.isExpanded = function() {
			var ret = (this.getOutput().css('display')=='none') ? false : true;
			return ret;
		}
		this.toggle = function(t){
			(this.isExpanded()) ? (this.hide(t)) : (this.show(t));
		}
		this.registerEvents = function(){
			this.trigger.getOutput().bind('click',{target:this},function(e){
				var t = e.data.target;
				$('body').click();
				e.stopPropagation();
				(t.props.triggerMode=='show') ? (t.show(300)) : (t.toggle(300));
				e.stopPropagation();				
			});
			this.closer.getOutput().bind('click',{target:this},function(e){
				e.data.target.hide(100);
			});
			$('body').bind('click',{target:this}, function(e){
				e.stopPropagation();
				e.data.target.hide(300);	
			});
			/*$(window).bind('scroll',{target:this}, function(e){
				e.data.target.positionate();
			});*/
		}
		this.getOutput = function(){
			return this.output;
		}
		this.setContainer = function(c){
			this.props.container = c;
		}
		this.getContainer = function(){
			return this.getConf().container;
		}
		this.getConf = function(){
			return this.props;
		}
		this.position = function(pos){
			if(pos!=undefined){
				this.getOutput().css({'top':'','left':'','right':'','bottom':''});
				this.getOutput().css(pos);
			}
		}
	}//menu
})(jQuery);
