
(function($)
{	
	/**************************************************************************/

	var fhajiyev=function(fhajiyev,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.fhajiyev=$(fhajiyev);

		$this.fhajiyevWindow=$('#fhajiyev-window');
		$this.fhajiyevWindowScroll=$('#fhajiyev-window-scroll');
		$this.fhajiyevWindowContent=$('#fhajiyev-window-content');

		$this.fhajiyevCloseButton=$('#fhajiyev-close-button');

		$this.enable=true;
		$this.scrollbar='';

		$this.requestPrevious='';
		$this.requestCurrent=requestCurrent;
		$this.requestType=$this.requestCurrent=='' ? 1 : 2;

		$this.fhajiyevVerticalMenuSlider='';
		$this.fhajiyevHorizontalMenuSlider='';

		$this.fhajiyevHeight=parseInt($('#fhajiyev').css('height'));
		$this.fhajiyevVericalMenuElementWidth=parseInt($('#fhajiyev-vertical-menu li:first').css('width'));

		$this.fhajiyevVericalMenuElementLeft=800;

		$this.fhajiyevWindowWidthExpand=parseInt($('div.main').css('width'));
		$this.fhajiyevWindowWidthCollapse=321;

		$this.fhajiyevHorizontalMenuHeight=451;

		$this.fhajiyevVerticalMenuBoxWrapper=parseInt($('.fhajiyev-vertical-menu-box-wrapper').first().css('height'));

		/**********************************************************************/
		/**********************************************************************/

		this.load=function()
		{
			$this.handleRequest();
			$this.createVerticalMenuSlider();
			$this.bindVerticalMenuHoverEvent();
		};

		/**********************************************************************/
		/**********************************************************************/

		this.bindVerticalMenuHoverEvent=function()
		{
			var fhajiyevVerticalMenu=$('#fhajiyev-vertical-menu');
			var fhajiyevVerticalMenuElement=fhajiyevVerticalMenu.children('li');

			fhajiyevVerticalMenuElement.hover(
				function() 
				{
					$(this).find('.fhajiyev-vertical-menu-box-foreground').stop().animate({top:'80px'},250);
					$(this).find('.fhajiyev-vertical-menu-box-foreground-hover').stop().animate({top:'80px',opacity:1},250);

					$(this).find('.fhajiyev-vertical-menu-box-icon-hover').stop().animate({opacity:1}); 
					$(this).find('.fhajiyev-vertical-menu-box-background').stop().animate({top:'0px'},500); 
				},
				function()
				{
					$(this).find('.fhajiyev-vertical-menu-box-foreground').stop().animate({top:'0px'},250);
					$(this).find('.fhajiyev-vertical-menu-box-foreground-hover').stop().animate({top:'0px',opacity:0},250);

					$(this).find('.fhajiyev-vertical-menu-box-icon-hover').stop().animate({opacity:0}); 
					$(this).find('.fhajiyev-vertical-menu-box-background').stop().animate({top:'-30px'},500); 
				}
			);				
		};

		/**********************************************************************/

		this.bindHorizontalMenuHoverEvent=function()
		{
			var fhajiyevHorizontalMenu=$('#fhajiyev-horizontal-menu');
			var fhajiyevHorizontalMenuElement=fhajiyevHorizontalMenu.children('li');

			fhajiyevHorizontalMenuElement.hover(
				function() 
				{
					$(this).find('.fhajiyev-horizontal-menu-icon').stop().animate({opacity:0},250);
					$(this).find('.fhajiyev-horizontal-menu-icon-hover').stop().animate({opacity:1},250);
				},
				function()
				{
					if(!$(this).hasClass('selected'))
					{
						$(this).find('.fhajiyev-horizontal-menu-icon').stop().animate({opacity:1},250);
						$(this).find('.fhajiyev-horizontal-menu-icon-hover').stop().animate({opacity:0},250);
					}
				}
			)					
		};			

		/**********************************************************************/

		this.createVerticalMenuSlider=function()
		{
			if(!$this.options.displayMenuSliderVertical) return;
			
			$this.fhajiyev.css({'overflow':'visible'});

			var fhajiyevVerticalMenu=$('#fhajiyev-vertical-menu');
			$this.fhajiyevVerticalMenuSlider=fhajiyevVerticalMenu.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 fhajiyevHorizontalMenu=$('#fhajiyev-horizontal-menu');
			$this.fhajiyevHorizontalMenuSlider=fhajiyevHorizontalMenu.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.fhajiyev.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 fhajiyevVerticalMenu=$('#fhajiyev-vertical-menu');
			var fhajiyevVerticalMenuElement=fhajiyevVerticalMenu.find('li');
			var fhajiyevVerticalMenuElementCount=fhajiyevVerticalMenuElement.length;

			fhajiyevVerticalMenuElement.each(function() 
			{
				$(this).children('div.fhajiyev-vertical-menu-box-wrapper').animate({height:'0px'},getRandom(500,1000),'easeInOutExpo',function() 
				{						
					if((++i)==fhajiyevVerticalMenuElementCount)
					{
						if($this.options.displayMenuSliderVertical)
						{
							$this.fhajiyevVerticalMenuSlider.destroyShow();
							$this.fhajiyev.css({'overflow':'hidden'});
						}
						else fhajiyevVerticalMenu.css({'height':'0'});
						
						fhajiyevVerticalMenuElement.css({'position':'absolute'});

						fhajiyevVerticalMenuElement.each(function() 
						{
							var index=$(this).parent('ul').children('li').index($(this));
							$(this).css('left',$this.fhajiyevVericalMenuElementWidth*index);
						});

						fhajiyevVerticalMenuElement.animate({left:$this.fhajiyevVericalMenuElementLeft,opacity:0},500,function() 
						{
							if((++j)==fhajiyevVerticalMenuElementCount)
							{
								fhajiyevVerticalMenuElement.css('display','none');
								$this.doEvent(event);
								return;
							}
						});
					}
				});                  
			});                               
		};

		/**********************************************************************/

		this.expandVerticalMenu=function(event)
		{
			var i=0;

			var fhajiyevVericalMenu=$('#fhajiyev-vertical-menu');
			var fhajiyevVericalMenuElement=fhajiyevVericalMenu.children('li');
			var fhajiyevVericalMenuElementCount=fhajiyevVericalMenuElement.length;

			fhajiyevVericalMenu.css('height',$this.fhajiyevHeight);

			fhajiyevVericalMenuElement.each(function() 
			{
				$(this).css(
				{
					'left'		: $this.fhajiyevVericalMenuElementLeft,
					'display'	: 'block',
					'opacity'	: 1,
					'position'	: 'absolute'					
				});

				$(this).children('div.fhajiyev-vertical-menu-box-wrapper').animate({height:$this.fhajiyevVerticalMenuBoxWrapper},500,'easeInOutExpo',function() 
				{
					var element=$(this).parent('li');
					var index=fhajiyevVericalMenuElement.index(element);
					var left=$this.fhajiyevVericalMenuElementWidth*index;

					element.animate({left:left},500,function() 
					{
						$(this).css('position','static');

						if((++i)==fhajiyevVericalMenuElementCount)
						{
							$this.createVerticalMenuSlider();
							$this.bindVerticalMenuHoverEvent();	
							$this.doEvent(event);
						}
					});
				});                  
			});                               
		};           

		/**********************************************************************/

		this.expandHorizontalMenu=function(event)
		{
			var fhajiyevHorizontalMenu=$('#fhajiyev-horizontal-menu-wrapper');

			$this.fhajiyevWindow.css('display','block');
			fhajiyevHorizontalMenu.animate({height:$this.fhajiyevHorizontalMenuHeight},500,function() 
			{
				$this.createHorizontalMenuSlider();
				$this.bindHorizontalMenuHoverEvent();
				$this.doEvent(event);
			});
		};

		/**********************************************************************/

		this.collapseHorizontalMenu=function(event)
		{
			var fhajiyevHorizontalMenu=$('#fhajiyev-horizontal-menu-wrapper');

			fhajiyevHorizontalMenu.animate({height:'0px'},500,function() 
			{
				if($this.options.displayMenuSliderHorizontal)
					$this.fhajiyevHorizontalMenuSlider.destroyShow();
				$this.fhajiyevWindow.css('display','none');
				$this.doEvent(event);
			});
		};            

		/**********************************************************************/

		this.expandWindow=function(event)
		{
			$this.fhajiyevWindow.animate({width:$this.fhajiyevWindowWidthExpand},500,'easeOutExpo',function() 
			{
				$this.doEvent(event);
			});
		};

		/**********************************************************************/

		this.collapseWindow=function(event)
		{
			$this.fhajiyevWindow.animate({width:$this.fhajiyevWindowWidthCollapse},100,'easeInOutSine',function() 
			{
				$this.doEvent(event);
			});               
		};

		/**********************************************************************/

		this.closePage=function(event)
		{
			$(':input,a').qtip('destroy');
			$this.collapseWindow({complete:function() 
			{
				$this.fhajiyevWindowContent.html(''); 
				$this.fhajiyevWindowScroll.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.fhajiyevWindowContent.html(page); 
				
			$this.fhajiyevWindowScroll.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.fhajiyev.addClass('open');
			}});  			
		};

		/**********************************************************************/

		this.createScrollbar=function()
		{
			$this.scrollbar=$('#fhajiyev-window-scroll').jScrollPane({maintainPosition:false,autoReinitialise:true}).data('jsp');
		};

		/**********************************************************************/
		/**********************************************************************/

		this.showCloseButton=function(show)
		{
			$this.fhajiyevCloseButton.css('display',show ? 'block' : 'none');
		};

		/**********************************************************************/

		this.isOpen=function()
		{
			return($this.fhajiyev.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 fhajiyevHorizontalMenu=$('#fhajiyev-horizontal-menu');
			var fhajiyevHorizontalMenuElement=fhajiyevHorizontalMenu.children('li');

			fhajiyevHorizontalMenuElement.removeClass('selected');
			fhajiyevHorizontalMenuElement.find('span.fhajiyev-horizontal-menu-icon').css('opacity',1);
			fhajiyevHorizontalMenuElement.find('span.fhajiyev-horizontal-menu-icon-hover').css('opacity',0);

			try
			{
				var object=fhajiyevHorizontalMenuElement.find('a[href="'+$this.getURL($this.requestCurrent)+'"]').parent('li');
				object.addClass('selected');
				object.find('span.fhajiyev-horizontal-menu-icon').css('opacity',0);
				object.find('span.fhajiyev-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.fhajiyev=function(page,options,requestCurrent)
	{
		/**********************************************************************/

		var fhajiyev=new fhajiyev(this,page,options,requestCurrent);
		fhajiyev.load();

		/**********************************************************************/
	};

	/**************************************************************************/

})(jQuery);