(function() {
	
	$(function() {
		$("#mainmenu li").filter(function() {
			if( $(this).text() == "Networking" ) {
				return true;
			}
		}).remove();
	});

	var SocialBar = function(options) {
		var tpl = "\
		<div class='social-bar'>\
			<div class='social-bar-area'>\
				<div class='social-bar-container'>\
					<div class='social-bar-bg'></div>\
					<div class='social-bar-content'>\
						<div class='social-bar-toolbar'>\
							<div class='toolbar-left'></div>\
							<div class='toolbar-right'></div>\
						</div>\
						<div class='social-bar-closed-info' style='cursor: pointer'><div style='padding: 6px'>Networking</div></div>\
						<div class='social-bar-openclose-button close-button'></div>\
					</div>\
				</div>\
			</div>\
		</div>\
		";
		
		options.startState = "closed";
		
		this.element = $(tpl);
		
		this.openCloseBtn = this.element.find(".social-bar-openclose-button");
		this.toolbar = this.element.find(".social-bar-toolbar");
		this.closedInfo = this.element.find(".social-bar-closed-info");
		this.container = this.element.find(".social-bar-container");
		this.closedInfo = this.element.find(".social-bar-closed-info").hide();
		this.element.find(".social-bar-bg").css("opacity",0.5);
		this.element.find(".social-bar-closed-info").click(jQuery.proxy( this.toggleVisibility, this ));
		this.openCloseBtn.click(jQuery.proxy( this.toggleVisibility, this ));
		this.isVisible = true;
		this.isToggling = false;
		this.toolbarLeft = this.toolbar.find(".toolbar-left");
		this.toolbarRight = this.toolbar.find(".toolbar-right");
		
		this.plugins = [];
	
		if( options.rss ) {
			this.plugins.push(new SocialBarElement(this, new RssFeed() ));
		}
		
		if( options.bookmark ) {
			this.plugins.push(new SocialBarElement(this, new SocialBookmark()));
		}

		if( options.messengers.length > 0 ) {
			this.plugins.push(new SocialBarElement(this, new SocialMessengers(options.messengers)));
		}
		
		if( options.networks.length > 0 ) {
			this.plugins.push(new SocialBarElement(this, new SocialNetworks(options.networks)));
		}
	
		if( options.twitterFeed ) {
			this.plugins.push(new SocialBarElement(this, new TwitterBox(options.twitterFeed)));
		}
		
		for( var i=0, length=this.plugins.length ; i<length ; i++ ) {
			var pos = this.plugins[i].plugin.getPosition();
			var element = this.plugins[i].getElement();
			if( this.plugins[i].plugin.isAvailable() ) {
				if( pos == "right" ) {
					this.toolbarRight.append(element);
				} else {
					this.toolbarLeft.append(element);
				}
			}
		}
		
		if( options.startState === "closed" ) {
			this.toggleVisibility();
		}
		
	}
	
	SocialBar.prototype = {
		getElement : function() {
			return this.element;
		},
		
		toggleVisibility : function() {
			if( !this.isToggling ) {
				this.isToggling = true;
				if( this.isVisible ) {
					this.hide();
				} else {
					this.show();
				}
				
				this.isVisible = !this.isVisible;
			}
		},
		
		
		show : function() {
			
			this.toggleSocialBar("switchToToolBar");
		},
		
		hide : function() {
			this.toggleSocialBar("switchToClosedInfo");
			
			if( this.activeElement ) {
				this.activeElement.togglePlugin();
				this.activeElement = undefined;
			}
		},
		
		toggleSocialBar : function( action ) {
			var self = this;
			jQuery(this.container).animate({
				bottom: -30-10
			}, 500, 'linear' , function() {
				self[action]();
				self._redisplayContainer();
			});
		},
		
		_redisplayContainer : function() {
			var self = this;
			jQuery(this.container).animate({
				bottom: 0
			}, 500, 'linear', function() {
				self.isToggling = false;
			});
		},
		
		switchToClosedInfo : function() {
			this.closedInfo.show();
			this.toolbar.hide();
			this.openCloseBtn.removeClass("close-button").addClass("open-button");
			this.container.css("width",100);
			
			
			this.fixOpenCloseButton();
			
		},
		
		switchToToolBar : function() {
			this.closedInfo.hide();
			this.toolbar.show();
			this.openCloseBtn.addClass("close-button").removeClass("open-button");
			this.container.css("width","100%");
			
			this.fixOpenCloseButton();
			
		},
		
		fixOpenCloseButton : function() {
			if( jQuery.browser.msie ) {
				if(jQuery.browser.version <= 6 ) {
					this.openCloseBtn.get(0).style.filter = "";//("style","");
					this.openCloseBtn.get(0).style.backgroundImage = "";
					var bgUrl = String(this.openCloseBtn.get(0).currentStyle.backgroundImage);
					var match = bgUrl.match(/^url\("http:\/\/(?:.*?)(\/.*\.png)"\)$/i);

					if( match !== null ) {
						var filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+match[1]+"',sizingMethod='crop');";
						this.openCloseBtn.get(0).style.backgroundImage = "none";
						this.openCloseBtn.get(0).style.filter = filter;
					}
				}
			}
		}
	};
	
	var SocialBarElementContent = function(content) {
		this.element = $("<div class='social-bar-element-content'><div class='social-bar-element-content-bg'></div><div class='social-bar-element-content-inner'></div></div>");
		this.content = content;
		this.inner = this.element.find(".social-bar-element-content-inner");
		this.bg = this.element.find(".social-bar-element-content-bg");
		this.element.find(".social-bar-element-content-inner").append(this.content);
	}
	
	SocialBarElementContent.prototype = {
		updateSize : function() {
			this.element.show();
			var width = this.content.outerWidth()+30;
			var height = this.content.outerHeight()+10;
			
			if( height > 300 ) {
				height = 300;
			}
			
			this.bg.width(width);
			this.bg.height(height);
			
			
			//this.bg.width(width+20);
			//this.bg.height(newHeight-15);
			//
			this.inner.css("width",width-10);
			this.inner.css("height",height-10);
			//
			this.element.css("width",this.bg.outerWidth());
			this.element.css("height",this.bg.outerHeight());
			
			this.element.hide();
		},
		
		getElement : function() {
			return this.element;
		}
	}
	
	var SocialBarElement  = function(socialBar, plugin) {
		this.element = jQuery("<div class='toolbar-element'></div>");
		this.icon = plugin.getToolbarIcon();
		this.plugin = plugin;
		var self = this;
		this.element.click(function(event) {
			self.togglePlugin(event);
		});
		this.socialBar = socialBar;
		this.contentElement = new SocialBarElementContent(this.plugin.getElement());
		this.plugin.setContent(this.contentElement);
		this.icon.css("font-size",0);
		
		$("body").append(this.icon);
		var iconWidth = this.icon.width();
		var iconHeight = this.icon.height();
		if( jQuery.browser.msie ) {
			if(jQuery.browser.version <= 6 ) {
				var bgUrl = String(this.icon.get(0).currentStyle.backgroundImage);
				var match = bgUrl.match(/^url\("http:\/\/(?:.*?)(\/.*\.png)"\)$/i);
				
				if( match !== null ) {
					var filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+match[1]+"',sizingMethod='crop');";
					this.icon.get(0).style.backgroundImage = "none";
					this.icon.get(0).style.filter = filter;
				}
			}
		}
		var top = Math.floor((30-iconHeight)/2);
		this.icon.css({"position":"relative","top":top});
		
		this.element.append(this.icon);
	}
	
	SocialBarElement.prototype = {
		getElement : function() {
			return this.element;
		},
		
		updatePosition : function() {
		},
		
		togglePlugin : function( event ) {
			if( event ) {
				event.preventDefault();
			}
			if( this.socialBar.activeElement == this ) {
				this.socialBar.activeElement.hide();
				this.socialBar.activeElement = undefined;
			} else {
				if( this.socialBar.activeElement ) {
					this.socialBar.activeElement.hide();
				}
				this.socialBar.activeElement = this;
				this.socialBar.activeElement.show();
			}
		},
		
		show : function() {
			this.contentElement.getElement().hide();
			if( this.plugin.getPosition() == "left" ) {
				this.socialBar.toolbarLeft.append(this.contentElement.getElement());
			} else {
				this.socialBar.toolbarRight.append(this.contentElement.getElement());
			}
			this.contentElement.updateSize();
			if( this.plugin.getPosition() === "left" ) {
				this.contentElement.getElement().css("left",this.element.offset().left-20);
			}
			this.contentElement.getElement().show(400);
		},
		
		hide : function() {
			this.contentElement.getElement().remove()
		}
	}

	
	/*
	need class for button and class for menu/container
	where e.g. rss, twitter or bookmark is living in
	*/
	
	window.SocialBar = SocialBar;
})();

jQuery(function() {
	/*
	TODO at first we need to check if networking is visible and get the info about twitter, and other pages
	
	then we get the element and add it to the page
	*/
	$.getJSON(baseUrl+'ajax.php',
					{'nav[path]':"art_networking.1",
					 'type':'getInfo'},function(data) {
						
			if( data.active ) {			
				var sb = new SocialBar(data.options);
				$("body").append(sb.getElement());
			}
		});
});
