
(function($)
{	
	/**************************************************************************/

	var Themis=function(Themis,page,options,requestCurrent)
	{
		/**********************************************************************/

		var $this=this;

		$this.defaults=
		{
			openStart					:	'',
			title						:	'',
			meta						:
			{
				keywords				:	'',
				description				:	''
			},
			displayMenuSliderVertical	: true,
			displayMenuSliderHorizontal	: true
		};

		$this.page=page;

		$this.options=$.extend($this.defaults,options);

		$this.Themis=$(Themis);

		$this.ThemisWindow=$('#Themis-window');
		$this.ThemisWindowScroll=$('#Themis-window-scroll');
		$this.ThemisWindowContent=$('#Themis-window-content');

		$this.ThemisCloseButton=$('#Themis-close-button');

		$this.enable=true;
		$this.scrollbar='';

		$this.requestPrevious='';
		$this.requestCurrent=requestCurrent;
		$this.requestType=$this.requestCurrent=='' ? 1 : 2;

		$this.ThemisVerticalMenuSlider='';
		$this.ThemisHorizontalMenuSlider='';

		$this.ThemisHeight=parseInt($('#Themis').css('height'));
		$this.ThemisVericalMenuElementWidth=parseInt($('#Themis-vertical-menu li:first').css('width'));

		$this.ThemisVericalMenuElementLeft=800;

		$this.ThemisWindowWidthExpand=parseInt($('div.main').css('width'));
		$this.ThemisWindowWidthCollapse=321;

		$this.ThemisHorizontalMenuHeight=451;

		$this.ThemisVerticalMenuBoxWrapper=parseInt($('.Themis-vertical-menu-box-wrapper').first().css('height'));

		/**********************************************************************/
		/**********************************************************************/

		this.load=function()
		{
			$this.handleRequest();
			$this.createVerticalMenuSlider();
			$this.bindVerticalMenuHoverEvent();
		};

		/**********************************************************************/
		/**********************************************************************/

		this.bindVerticalMenuHoverEvent=function()
		{
			var ThemisVerticalMenu=$('#Themis-vertical-menu');
			var ThemisVerticalMenuElement=ThemisVerticalMenu.children('li');

			ThemisVerticalMenuElement.hover(
				function() 
				{
					$(this).find('.Themis-vertical-menu-box-foreground').stop().animate({top:'80px'},250);
					$(this).find('.Themis-vertical-menu-box-foreground-hover').stop().animate({top:'80px',opacity:1},250);

					$(this).find('.Themis-vertical-menu-box-icon-hover').stop().animate({opacity:1}); 
					$(this).find('.Themis-vertical-menu-box-background').stop().animate({top:'0px'},500); 
				},
				function()
				{
					$(this).find('.Themis-vertical-menu-box-foreground').stop().animate({top:'0px'},250);
					$(this).find('.Themis-vertical-menu-box-foreground-hover').stop().animate({top:'0px',opacity:0},250);

					$(this).find('.Themis-vertical-menu-box-icon-hover').stop().animate({opacity:0}); 
					$(this).find('.Themis-vertical-menu-box-background').stop().animate({top:'-30px'},500); 
				}
			);				
		};

		/**********************************************************************/

		this.bindHorizontalMenuHoverEvent=function()
		{
			var ThemisHorizontalMenu=$('#Themis-horizontal-menu');
			var ThemisHorizontalMenuElement=ThemisHorizontalMenu.children('li');

			ThemisHorizontalMenuElement.hover(
				function() 
				{
					$(this).find('.Themis-horizontal-menu-icon').stop().animate({opacity:0},250);
					$(this).find('.Themis-horizontal-menu-icon-hover').stop().animate({opacity:1},250);
				},
				function()
				{
					if(!$(this).hasClass('selected'))
					{
						$(this).find('.Themis-horizontal-menu-icon').stop().animate({opacity:1},250);
						$(this).find('.Themis-horizontal-menu-icon-hover').stop().animate({opacity:0},250);
					}
				}
			)					
		};			

		/**********************************************************************/

		this.createVerticalMenuSlider=function()
		{
			if(!$this.options.displayMenuSliderVertical) return;
			
			$this.Themis.css({'overflow':'visible'});

			var ThemisVerticalMenu=$('#Themis-vertical-menu');
			$this.ThemisVerticalMenuSlider=ThemisVerticalMenu.bxSlider(
			{
				auto:false,
				pause:1000,
				nextText:null,
				prevText:null,
				mode:'horizontal',
				displaySlideQty:5,
				infiniteLoop:true,
				hideControlOnEnd:false,
				wrapperClass:'bx-wrapper bx-wrapper-vertical-menu'
			});					
		};

		/**********************************************************************/

		this.createHorizontalMenuSlider=function()
		{
			if(!$this.options.displayMenuSliderHorizontal) return;
			
			var ThemisHorizontalMenu=$('#Themis-horizontal-menu');
			$this.ThemisHorizontalMenuSlider=ThemisHorizontalMenu.bxSlider(
			{
				auto:false,
				pause:1000,
				nextText:null,
				prevText:null,
				mode:'vertical',
				displaySlideQty:5,
				infiniteLoop:true,
				hideControlOnEnd:false,
				wrapperClass:'bx-wrapper bx-wrapper-horizontal-menu'
			});					
		};

		/**********************************************************************/
		/**********************************************************************/

		this.handleRequest=function()
		{
			if($this.requestType==2)
			{
				$this.doRequest();
			}
			else
			{
				var start=false;
				if(window.location.hash=='') 
				{
					if($this.options.openStart.length) 
					{
						start=true;
						window.location.href=$this.getURL($this.options.openStart);
					}
				}

				if(!start)
				{
					var requestCurrent=$this.checkRequest();	
					if(requestCurrent!==false)
					{
						$this.requestCurrent=requestCurrent;
						if($this.requestCurrent!=$this.requestPrevious) $this.doRequest();
					}
				}

				$(window).bind('hashchange',function(event) 
				{
					event.preventDefault();

					if($this.isEnable()==false) return;

					var requestCurrent=$this.checkRequest();

					if(requestCurrent===false) return;

					$this.requestCurrent=requestCurrent;
					$this.doRequest();
				}); 
			}
		};

		/**********************************************************************/

		this.doRequest=function()
		{
			if(!$this.enable) return(false);
			$this.enable=false;

			$this.selectHorizontalMenu();

			var open=$this.isOpen();

			if($this.requestCurrent=='main') $this.close(open);
			else $this.open(open);    

			return(true);			
		};

		/**********************************************************************/

		this.checkRequest=function()
		{
			var request=window.location.hash.substring(2);

			if(request=='main') return('main');

			for(var id in $this.page)
			{
				if(id==request) return(request);
			};

			window.location=$this.getURL('main');

			return(false);
		};

		/**********************************************************************/

		this.open=function(isOpen)
		{	
			$this.showCloseButton(false);

			if(isOpen)
			{
				$this.closePage({complete:function() 
				{
					$this.openPage({complete:function() { }});
				}});   
			}
			else
			{
				$this.collapseVerticalMenu({complete:function() 
				{
					$this.expandHorizontalMenu({complete:function() 
					{
						$this.openPage({complete:function() { }});
					}});
				}}); 
			};               
		};

		/**********************************************************************/

		this.close=function(open)
		{
			if(!open) 
			{
				$this.enable=true;
				return;
			}

			$this.showCloseButton(false);
			$this.Themis.removeClass('open');

			$this.closePage({complete:function()   
			{
				$this.collapseHorizontalMenu({complete:function() 
				{
					$this.expandVerticalMenu({complete:function()
					{
						$this.enable=true;
					}});
				}});
			}});
		};

		/**********************************************************************/

		this.collapseVerticalMenu=function(event)
		{
			var i=0,j=0;

			var ThemisVerticalMenu=$('#Themis-vertical-menu');
			var ThemisVerticalMenuElement=ThemisVerticalMenu.find('li');
			var ThemisVerticalMenuElementCount=ThemisVerticalMenuElement.length;

			ThemisVerticalMenuElement.each(function() 
			{
				$(this).children('div.Themis-vertical-menu-box-wrapper').animate({height:'0px'},getRandom(500,1000),'easeInOutExpo',function() 
				{						
					if((++i)==ThemisVerticalMenuElementCount)
					{
						if($this.options.displayMenuSliderVertical)
						{
							$this.ThemisVerticalMenuSlider.destroyShow();
							$this.Themis.css({'overflow':'hidden'});
						}
						else ThemisVerticalMenu.css({'height':'0'});
						
						ThemisVerticalMenuElement.css({'position':'absolute'});

						ThemisVerticalMenuElement.each(function() 
						{
							var index=$(this).parent('ul').children('li').index($(this));
							$(this).css('left',$this.ThemisVericalMenuElementWidth*index);
						});

						ThemisVerticalMenuElement.animate({left:$this.ThemisVericalMenuElementLeft,opacity:0},500,function() 
						{
							if((++j)==ThemisVerticalMenuElementCount)
							{
								ThemisVerticalMenuElement.css('display','none');
								$this.doEvent(event);
								return;
							}
						});
					}
				});                  
			});                               
		};

		/**********************************************************************/

		this.expandVerticalMenu=function(event)
		{
			var i=0;

			var ThemisVericalMenu=$('#Themis-vertical-menu');
			var ThemisVericalMenuElement=ThemisVericalMenu.children('li');
			var ThemisVericalMenuElementCount=ThemisVericalMenuElement.length;

			ThemisVericalMenu.css('height',$this.ThemisHeight);

			ThemisVericalMenuElement.each(function() 
			{
				$(this).css(
				{
					'left'		: $this.ThemisVericalMenuElementLeft,
					'display'	: 'block',
					'opacity'	: 1,
					'position'	: 'absolute'					
				});

				$(this).children('div.Themis-vertical-menu-box-wrapper').animate({height:$this.ThemisVerticalMenuBoxWrapper},500,'easeInOutExpo',function() 
				{
					var element=$(this).parent('li');
					var index=ThemisVericalMenuElement.index(element);
					var left=$this.ThemisVericalMenuElementWidth*index;

					element.animate({left:left},500,function() 
					{
						$(this).css('position','static');

						if((++i)==ThemisVericalMenuElementCount)
						{
							$this.createVerticalMenuSlider();
							$this.bindVerticalMenuHoverEvent();	
							$this.doEvent(event);
						}
					});
				});                  
			});                               
		};           

		/**********************************************************************/

		this.expandHorizontalMenu=function(event)
		{
			var ThemisHorizontalMenu=$('#Themis-horizontal-menu-wrapper');

			$this.ThemisWindow.css('display','block');
			ThemisHorizontalMenu.animate({height:$this.ThemisHorizontalMenuHeight},500,function() 
			{
				$this.createHorizontalMenuSlider();
				$this.bindHorizontalMenuHoverEvent();
				$this.doEvent(event);
			});
		};

		/**********************************************************************/

		this.collapseHorizontalMenu=function(event)
		{
			var ThemisHorizontalMenu=$('#Themis-horizontal-menu-wrapper');

			ThemisHorizontalMenu.animate({height:'0px'},500,function() 
			{
				if($this.options.displayMenuSliderHorizontal)
					$this.ThemisHorizontalMenuSlider.destroyShow();
				$this.ThemisWindow.css('display','none');
				$this.doEvent(event);
			});
		};            

		/**********************************************************************/

		this.expandWindow=function(event)
		{
			$this.ThemisWindow.animate({width:$this.ThemisWindowWidthExpand},500,'easeOutExpo',function() 
			{
				$this.doEvent(event);
			});
		};

		/**********************************************************************/

		this.collapseWindow=function(event)
		{
			$this.ThemisWindow.animate({width:$this.ThemisWindowWidthCollapse},100,'easeInOutSine',function() 
			{
				$this.doEvent(event);
			});               
		};

		/**********************************************************************/

		this.closePage=function(event)
		{
			$(':input,a').qtip('destroy');
			$this.collapseWindow({complete:function() 
			{
				$this.ThemisWindowContent.html(''); 
				$this.ThemisWindowScroll.css('display','none');

				$this.doEvent(event);
			}});
		};

		/**********************************************************************/

		this.openPage=function()
		{
			if($this.requestType==2)
			{
				$this.openPageComplete(null);
			}
			else
			{
				$.get('page/'+$this.getPageData($this.requestCurrent,'html'),{},function(page) 
				{	
					$this.openPageComplete(page);
				},
				'html');   
			}
		};
		
		/**********************************************************************/
		
		this.openPageComplete=function(page) 
		{
			if($this.requestType==1)	
				$this.ThemisWindowContent.html(page); 
				
			$this.ThemisWindowScroll.css('display','block');

			jQuery.getScript('page/script/base.js',function() 
			{
				if($this.getPageData($this.requestCurrent,'js')!='')
					jQuery.getScript('page/script/'+$this.getPageData($this.requestCurrent,'js'));
			});

			$this.createScrollbar();

			$this.expandWindow({complete:function() 
			{
				if($this.requestType==1)
				{
					$this.setMeta();
					$this.setTitle();
				}
				
				$this.enable=true;
				$this.showCloseButton(true);
				$this.requestPrevious=$this.requestCurrent;
				$this.Themis.addClass('open');
			}});  			
		};

		/**********************************************************************/

		this.createScrollbar=function()
		{
			$this.scrollbar=$('#Themis-window-scroll').jScrollPane({maintainPosition:false,autoReinitialise:true}).data('jsp');
		};

		/**********************************************************************/
		/**********************************************************************/

		this.showCloseButton=function(show)
		{
			$this.ThemisCloseButton.css('display',show ? 'block' : 'none');
		};

		/**********************************************************************/

		this.isOpen=function()
		{
			return($this.Themis.hasClass('open'));
		};

		/**********************************************************************/

		this.isEnable=function()
		{
			if(!$this.enable)
			{
				window.location.href=$this.getURL($this.requestCurrent);
				return(false);
			}  

			return(true);
		};

		/**********************************************************************/

		this.getPageData=function(key,property)
		{
			return($this.page[key][property]);
		};

		/**********************************************************************/

		this.getURL=function(page)
		{
			return(($this.requestType==1 ? '#!' : '?_escaped_fragment_=')+page);
		};

		/**********************************************************************/
		/**********************************************************************/

		this.doEvent=function(event)
		{
			if(typeof(event)!='undefined')
			{
				if(typeof(event.complete)!='undefined') event.complete.apply();
			};                  
		};

		/**********************************************************************/

		this.selectHorizontalMenu=function()
		{	
			var ThemisHorizontalMenu=$('#Themis-horizontal-menu');
			var ThemisHorizontalMenuElement=ThemisHorizontalMenu.children('li');

			ThemisHorizontalMenuElement.removeClass('selected');
			ThemisHorizontalMenuElement.find('span.Themis-horizontal-menu-icon').css('opacity',1);
			ThemisHorizontalMenuElement.find('span.Themis-horizontal-menu-icon-hover').css('opacity',0);

			try
			{
				var object=ThemisHorizontalMenuElement.find('a[href="'+$this.getURL($this.requestCurrent)+'"]').parent('li');
				object.addClass('selected');
				object.find('span.Themis-horizontal-menu-icon').css('opacity',0);
				object.find('span.Themis-horizontal-menu-icon-hover').css('opacity',1);
			}
			catch(e) {}
		};

		/**********************************************************************/
		/**********************************************************************/

		this.setMeta=function()
		{
			for(var i in $this.defaults.meta)
			{
				var value='';

				try
				{
					value=$this.page[$this.requestCurrent].meta[i];
				}
				catch(e) { }

				if(!value.length) value=$this.options.meta[i];

				$('meta[name="'+i+'"]').attr(value);					
			}
		};

		/**********************************************************************/

		this.setTitle=function()
		{
			var value='';

			try
			{
				value=$this.page[$this.requestCurrent].title;
			}
			catch(e) { }

			if(!value.length) value=$this.options.title;

			document.title=value;
		};

		/**********************************************************************/
		/**********************************************************************/
	};

	/**************************************************************************/

	$.fn.Themis=function(page,options,requestCurrent)
	{
		/**********************************************************************/

		var Themis=new Themis(this,page,options,requestCurrent);
		Themis.load();

		/**********************************************************************/
	};

	/**************************************************************************/

})(jQuery);