(function($, undefined){
	$.widget("ui.movement", {
	   // default options
		options: {
			speed: 800,
			delay: 5000
		},
		_create: function() {
			this.topPanelWidth = 0;
			this.topPanelWebsiteWidth = $(".top-panel-website", this.element).width();
			this.topPanelMenuWidth = $(".top-panel-menu", this.element).width();
			/*initialize session storahe*/
			if(/*!sessionStorage.topPanel*//*!USTORE.getValue('topPanel')*/!$.cookie("topPanel"))$.cookie("topPanel", "opened");/*USTORE.setValue('topPanel','opened');*//*sessionStorage.topPanel = "opened";*/
			
			/*widths*/
			this._bindeEvents();
			
			/*capitalize first letter*/
			this.capitalizeFirstLetter();
			
			this.setPanelsDimensions();
			
			if(/*sessionStorage.topPanel*//*USTORE.getValue('topPanel')*/$.cookie("topPanel") == "closed"){
				$(".top-panel-menu", this.element).css("right", -this.topPanelMenuWidth + 30);
			}
			
			/*initial window width*/
			this.windowWidth = $(window).width();
			this.backPanelOffset = parseInt($(".left-back-panel", this.element).offset().left);
			this.topMenuLeft = 0;
		},
		
		/*set panels dimensions*/
		setPanelsDimensions: function () {
			var self = this;
			if(/*sessionStorage.topPanel*//*USTORE.getValue('topPanel')*/$.cookie("topPanel") == "closed"){
				$(".right-back-panel-top", this.element).css("display", "none");
				$(".right-back-panel", this.element).css("display", "block");
				$(".left-back-panel", this.element).css({
					"display": "block",
					"visibility": "visible"
				});	
			}
			
			if($(".right-back-panel-top", this.element).css("display") != "none") var rightBackPanelOffset = $(".right-back-panel-top", this.element).offset();
			else var rightBackPanelOffset = $(".right-back-panel", this.element).offset();
			this.topPanelWidth = rightBackPanelOffset.left;
			
			if(/*sessionStorage.topPanel*//*USTORE.getValue('topPanel')*/$.cookie("topPanel") == "closed"){
				$(".top-panel", this.element).css("left", 46 - parseInt(self.topPanelWidth));
			}
			
			//self.setHeight();
									
			/*set width of top left panel*/
			$(".top-panel", this.element).width(self.topPanelWidth).css("min-width", self.topPanelWebsiteWidth + 46);
			//alert(self.topPanelWidth);
			$(".top-panel .top-panel-blank", this.element).width(self.topPanelWidth - 640 - 46);
			
			var topPanelMenuOffset = parseInt($(".top-panel-menu", self.element).offset().left);
			var backPanelOffset = parseInt($(".left-back-panel", this.element).offset().left);
			var topPanelMenuWidth = parseInt($(".top-panel-menu", self.element).width());
			var menuRight = parseInt($(".top-panel-menu", self.element).css("right"));
			if(topPanelMenuOffset + topPanelMenuWidth >= backPanelOffset && self.backPanelOffset > backPanelOffset && menuRight < -20 && /*sessionStorage.topPanel*//*USTORE.getValue('topPanel')*/$.cookie("topPanel") == "closed") {
				$(".top-panel-menu", self.element).css("right", menuRight + self.backPanelOffset - backPanelOffset + 10);
				self.topMenuLeft = $(".top-panel-menu", self.element).css("right");
			}
            
			$(".top-panel").height($(document).height());
		},
		
		/*set height of panels*/
		setHeight: function (ss) {
			/*set dimensions of page*/
			var topPanelHeight = $(".top-panel-content", self.element).height() + 205;
			var backPanelHeight = $(".back-panel", self.element).height();
						
			var maxHeight = 0;
			if(topPanelHeight > backPanelHeight) maxHeight = topPanelHeight;
			else maxHeight = backPanelHeight;
			if(topPanelHeight <= $(window).height() && backPanelHeight <= $(window).height()) $(this.element).css("height", "100%");
			else {$(this.element).css("height", maxHeight);}
		},
		
		_bindeEvents: function () {
			var self = this;
			
			$(".right-back-panel-top a", this.element).live({
				"click": function () {
					self.slideLeftPanel();
				}	
			});
            
            /*if ($(".right-back-panel-top a[href='" + location.hash + "']").size () == 0 && location.hash.search("/") != -1)
            {
                self.slideLeftPanel();          
            }*/
			
			$(".top-panel-menu a", this.element).live({
				"click": function () {
					self.slideRightPanel();
				}	
			});
			
			$(window).delay(100).resize(function () {
			    
				self.setPanelsDimensions();					
				
				var topPanelMenuOffset = parseInt($(".top-panel-menu", self.element).offset().left);
				var backPanelOffset = parseInt($(".left-back-panel", this.element).offset().left);
				var topPanelMenuWidth = parseInt($(".top-panel-menu", self.element).width());
				var menuRight = parseInt($(".top-panel-menu", self.element).css("right"));
								
				if(topPanelMenuOffset + topPanelMenuWidth >= backPanelOffset && self.backPanelOffset > backPanelOffset && menuRight < -20 && /*sessionStorage.topPanel*//*USTORE.getValue('topPanel')*/$.cookie("topPanel") == "closed") {
					$(".top-panel-menu", self.element).css("right", menuRight + self.backPanelOffset - backPanelOffset + 10);
					self.topMenuLeft = $(".top-panel-menu", self.element).css("right");
				}
				else if (backPanelOffset > 36 + topPanelMenuWidth && menuRight > -self.topPanelMenuWidth + 30 && /*sessionStorage.topPanel*//*USTORE.getValue('topPanel')*/$.cookie("topPanel") == "closed") {
					$(".top-panel-menu", self.element).css("right", -self.topPanelMenuWidth + 30);
					self.topMenuLeft = $(".top-panel-menu", self.element).css("right");
				} else if (self.backPanelOffset < backPanelOffset &&  menuRight > -self.topPanelMenuWidth + 30 && backPanelOffset >= 78 && /*sessionStorage.topPanel*//*USTORE.getValue('topPanel')*/$.cookie("topPanel") == "closed") {
					$(".top-panel-menu", self.element).css("right", menuRight + self.backPanelOffset - backPanelOffset);
					self.topMenuLeft = $(".top-panel-menu", self.element).css("right");
				} else if (backPanelOffset < 78 && /*sessionStorage.topPanel*//*USTORE.getValue('topPanel')*/$.cookie("topPanel") == "closed") {
					$(".top-panel-menu", self.element).css("right", "-20px");
					self.topMenuLeft = $(".top-panel-menu", self.element).css("right");
				}
				self.backPanelOffset = backPanelOffset;
			});
			
			$(".top-panel-menu", self.element).hover(function(){
				var backPanelOffset = parseInt($(".left-back-panel", this.element).offset().left);
				var menuRight = parseInt($(".top-panel-menu", self.element).css("right"));
				if(/*sessionStorage.topPanel*//*USTORE.getValue('topPanel')*/$.cookie("topPanel") == "closed" && 240 >= backPanelOffset)
					$(this).stop().animate({right: -self.topPanelMenuWidth + 30},{queue:false,duration:200});
			}, function() {
				var backPanelOffset = parseInt($(".left-back-panel", this.element).offset().left);
				var menuRight = parseInt($(".top-panel-menu", self.element).css("right"));
				if(/*sessionStorage.topPanel*//*USTORE.getValue('topPanel')*/$.cookie("topPanel") == "closed" && 240 >= backPanelOffset)
					$(this).stop().animate({right: self.topMenuLeft},{queue:false,duration:200});
			});
		},
		
		slideLeftPanel: function () {
			var self = this;
            $.cookie("topPanel", "closed");
            //USTORE.setValue('topPanel','closed');
			/*sessionStorage.topPanel = "closed";*/
			$(".right-back-panel-top", this.element).fadeOut(600, function () {
				$(".left-back-panel", self.element).css({
					"display": "none",
					"visibility": "visible"
				}).fadeIn(500);
				$(".right-back-panel", self.element).delay(200).fadeIn(500);
			});
			
			var movTopPanelDistance = 46 - self.topPanelWidth;
			$(".top-panel", self.element).animate({"left": movTopPanelDistance-20}, 500).animate({"left": "+=20"}, 150, function () {
				$(".top-panel-menu", this.element).animate({"right": -self.topPanelMenuWidth + 10}, 300).animate({"right": "+=20"}, 200, function () {
					var topPanelMenuOffset = parseInt($(".top-panel-menu", self.element).offset().left);
					var backPanelOffset = parseInt($(".left-back-panel", this.element).offset().left);
					var topPanelMenuWidth = parseInt($(".top-panel-menu", self.element).width());
					var menuRight = parseInt($(".top-panel-menu", self.element).css("right"));
					if(topPanelMenuOffset + topPanelMenuWidth >= backPanelOffset && menuRight < -20 && /*sessionStorage.topPanel*//*USTORE.getValue('topPanel')*/$.cookie("topPanel") == "closed") {
						$(".top-panel-menu", self.element).animate({"right": "-20px"}, 300);
						self.topMenuLeft = -20;
					}
				});
			});			
		},
		
		slideRightPanel: function () {
		    $.cookie("topPanel", "opened");
            //USTORE.setValue('topPanel','opened');
			/*sessionStorage.topPanel = "opened";*/
			$(".left-back-panel", self.element).delay(150).fadeOut(500, function () {
				$(this).css({
					"visibility": "hidden",
					"display": "block"
				});
				$(".right-back-panel-top", self.element).delay(100).fadeIn(500);
			});
			
			$(".right-back-panel", self.element).fadeOut(500);
			
			
			$(".top-panel-menu", this.element).animate({"right": 10}, 200, function () {
				$(".top-panel", self.element).animate({"left": 0}, 500);
			});
		},
		
		capitalizeFirstLetter: function () {
			$("div.first-letter", this.element).each( function (key, elem) {
				var firstLetter = $(elem).children("div.text").text().substr(0,1);
				$(elem).children("div.text").text($(elem).children("div.text").text().substr(1, $(elem).children("div.text").text().length-1));
				var spanFLetter = "<span class=\"firstLetter\">"+firstLetter+"</span>";
				$(elem).children("div.text").prepend(spanFLetter);
			});	
		},
		
		destroy: function() {
			$.Widget.prototype.destroy.apply(this, arguments); // default destroy
			// now do other stuff particular to this widget
		}
	});
	$.extend($.ui.movement, {
        version: "0.1.0"
    });
})(jQuery);
