/**
 * @class: Util
 * @description: Defines Util functions
 * @author: tram.nguyen
 * @version: 1.0
 **/

(function($){

	
	$.fn.customSelect = function(options){
		var defaults = {
			itemsShow: 5,
			onSelect: null
		};
		options = $.extend(defaults, options);  
		return this.each(function(){  
			var that = $(this);
			var selectReal = that.find('select');
			var heightWindow;
			var fakeSelectHeight;
			var isShow;
			that.vars = {
				width: selectReal.width(),
				height: selectReal.height(),
				fakeSelect: null
			};
			that.vars.fakeSelect = $('<div class="optionLayer"><div class="csLayer"><ul></ul></div></div>').insertAfter(selectReal).addClass('hidden');
			selectReal.children().each(function(){
				var optionEl = $(this);
				$('<li><a href="javascript:void(0);" title="' + optionEl.text() + '" class="' + optionEl.val() + '">' + optionEl.text() + '</a></li>').appendTo(that.vars.fakeSelect.find('ul'));				
			});
			that.find('.icon').unbind('click').bind('click',function(e){
				if(e) e.preventDefault();
				if(that.vars.fakeSelect.is(':visible')){
					if(fakeSelectHeight > heightWindow){
						that.vars.fakeSelect.hide("slide",{direction:"down"},300);
						isShow = false;
					}
					else{
						that.vars.fakeSelect.slideUp(300).addClass('hidden');
						isShow = false;
					}
				}
				else{
					heightWindow = $(window).height() - $(this).offset().top;
					fakeSelectHeight = that.vars.fakeSelect.outerHeight();
					if(fakeSelectHeight > heightWindow){
						that.vars.fakeSelect.css({
							'top': -fakeSelectHeight - 5
						});
						that.vars.fakeSelect.show("slide",{direction:"down"},300);
						isShow = true;
					}
					else{
						that.vars.fakeSelect.css({
							'top': 23
						});
						that.vars.fakeSelect.slideDown(300).removeClass('hidden');
						isShow = true;
					}
				}
			});
			that.find('a.csText').unbind('click').bind('click',function(e){
				if(e) e.preventDefault();
				that.find('.icon').trigger('click');
			});
			$(document).click(function(e){
				if(isShow){
					if(!$(e.target).hasClass('icon')&&!$(e.target).hasClass('csText')){
						heightWindow = $(window).height() - $('.icon').offset().top;
						fakeSelectHeight = that.vars.fakeSelect.outerHeight();
						if(!that.vars.fakeSelect.is(':visible')){
							return;
						}
						if(fakeSelectHeight > heightWindow && that.vars.fakeSelect.is(':visible')){
							if(parseInt(that.vars.fakeSelect.css('top')) == 23){
								that.vars.fakeSelect.slideUp(300).addClass('hidden');
							}
							else{
								that.vars.fakeSelect.hide("slide",{direction:"down"},300);
							}
						}
						else{
							if(parseInt(that.vars.fakeSelect.css('top')) == -fakeSelectHeight - 5){
								that.vars.fakeSelect.hide("slide",{direction:"down"},300);
							}
							else{
								that.vars.fakeSelect.slideUp(300).addClass('hidden');
							}
						}
					}
				}
			});
			if(that.vars.fakeSelect.find('ul').children().length < options.itemsShow){
				that.vars.fakeSelect.find('ul').height(24 * that.vars.fakeSelect.find('ul').children().length);
			}
			that.vars.fakeSelect.find('ul').children().bind('click',function(){
				var liEl = $(this);
				that.find('.csText span').text(liEl.text());			
				if(options.onSelect){
					options.onSelect.call(selectReal, that.vars.fakeSelect, liEl.text());
				}				
			});
		});	
	};

	$.fn.changeContext = function(options){
		var defaults = {
			speed: 400
		};
		
		var options = $.extend(defaults, options); 
		return this.each(function(){
			var that = $(this);
			that.vars = {
				currentIndex: 0
			};
			var paging = that.find('.paging');
			var listProj = that.find('.listProj');
			var innerListProj = listProj.find('.innerListProj');
			if(!paging.length || !listProj.length || paging.find('li').length != listProj.find('.innerListProj').length){
				return;
			}
			paging.find('li').not(':first').removeClass('current');
			paging.find('li').each(function(index){
				var liEl = $(this);
				var distance = 0;
				liEl.find('a').unbind('click').bind('click',function(e){
					e.preventDefault();
					$('.current').removeClass('current');
					if(index>that.vars.currentIndex){
						distance = index - that.vars.currentIndex;
						listProj.animate({
							'marginLeft': parseInt(listProj.css('marginLeft')) - innerListProj.outerWidth()*distance
						},options.speed);
					}
					else if(index<that.vars.currentIndex){
						distance = that.vars.currentIndex - index;
						listProj.animate({
							'marginLeft': parseInt(listProj.css('marginLeft')) + innerListProj.outerWidth()*distance
						},options.speed);
					}
					that.vars.currentIndex = index;
					$(this).parent('li').addClass('current');
				});
			});
		});
	};
	
	$.fn.slideWrapContent = function(options){
		var defaults = {
			speed: 400,
			button_Opacity: 0.5
		};
		var options = $.extend(defaults, options); 
		return this.each(function(){
			var that = $(this);
			that.vars = {
				wrapContent: null,
				nextButton: null,
				prevButton: null,
				menuLinks: null,
				selectLinks: null
			};
			that.vars.wrapContent = $('.innerMain');
			that.vars.nextButton = that.find('.next');
			that.vars.prevButton = that.find('.prev');
			that.vars.menuLinks = $('.innerListProj > ul > li > a');
			that.vars.selectLinks = $('.csLayer > ul > li > a');
			var logoLink = $('.ftLeft').find('a:first img');
			var aboutUsLink = $('.txtAbout > a');
			var pages = that.vars.wrapContent.children();
			var aniComplete = true;
			var current = that.vars.wrapContent.data('currentPage') || 0;
			var curSelectElem = null, curMenuElem = null;
			var countPage = 0;
			var innerMainWidth = 0;
			if( current == 0){
				that.vars.prevButton.children().css({'opacity': options.buttonOpacity});			
				that.vars.prevButton.addClass('first');
			}
			while(countPage < pages.length){
				innerMainWidth += pages.eq(countPage).outerWidth();
				countPage++;
			}
			that.vars.wrapContent.css('width',innerMainWidth);
			logoLink.unbind('click').bind('click',function(e){
				if(e) e.preventDefault();
				var projectName = $(this).attr('class');
				that.vars.selectLinks.filter('[class=' + projectName + ']').trigger('click');
				
			});
			that.vars.selectLinks.unbind('click').bind('click',function(e){
				if(e) e.preventDefault();
				that.vars.nextButton.children().css({'opacity': 1});
				that.vars.nextButton.removeClass('last');
				var selectLink = $(this);
				var projectName = selectLink.attr('class');
				pages.each(function(index){
					var pageEl = $(this);
					var pageID = pageEl.attr('id');
					if(projectName == pageID){
						var mgLeft = 0,
							count = 0;
						while(count < index){
							mgLeft += pages.eq(count).outerWidth();
							count++;
						}
						that.vars.prevButton.children().css({'opacity': 1});
						that.vars.prevButton.removeClass('first');
						that.vars.wrapContent.animate({
							'marginLeft': -mgLeft
						}, options.speed);
						current = index;
						if(curSelectElem){
							$(curSelectElem).parent().removeClass('current');
						}
						curSelectElem = selectLink;
						$(curSelectElem).parent().addClass('current');	
						$(curMenuElem).parent().removeClass('current');
						curMenuElem = that.vars.menuLinks.filter('[class=' + projectName + ']');
						$(curMenuElem).parent().addClass('current');	
					}
				});
			});
			aboutUsLink.bind('click',function(e){
				if(e) e.preventDefault();
				
				var menuLink = $(this); 
				var projectName = $(this).attr('class');
				pages.each(function(index){
					var pageEl = $(this);
					var pageID = pageEl.attr('id');
					if(projectName == pageID){
						var mgLeft = 0,
							count = 0;
						while(count < index){
							mgLeft += pages.eq(count).outerWidth();
							count++;
						}
						that.vars.prevButton.children().css({'opacity': 1});
						that.vars.prevButton.removeClass('first');
						that.vars.wrapContent.animate({
							'marginLeft': -mgLeft
						}, options.speed);
						current = index;	
					}
				});
			});
			that.vars.menuLinks.bind('click',function(e){
				if(e) e.preventDefault();
				var menuLink = $(this); 
				var projectName = $(this).attr('class');
				if(curMenuElem){
					$(curMenuElem).parent().removeClass('current');
				}
				curMenuElem = menuLink;
				$(curMenuElem).parent().addClass('current');
				that.vars.selectLinks.filter('[class=' + projectName + ']').trigger('click');
			});
			function findProject(cur){
				return  pages.eq(cur).prev().attr('id');
			};
			that.vars.nextButton.unbind('click').bind('click',function(e){
				if(e) e.preventDefault();
				$(this).parent('li').parent('.listBtn').find('.prev').find('img').css({'opacity': 1});
				$(this).parent('li').parent('.listBtn').find('.prev').find('span').css({'opacity': 1});
				$(this).parent('li').parent('.listBtn').find('a.first').removeClass('first');
				if(!aniComplete){
					return;
				}
				if(current < pages.length -1){
					current++;
					
					var mgLeft = 0,
						count = 0;
					while(count < current){
						mgLeft += pages.eq(count).outerWidth();
						count++;
					};
					if(count == pages.length - 1){
						$(this).parent('li').find('img').css({'opacity': options.button_Opacity});
						$(this).parent('li').find('span').css({'opacity': options.button_Opacity});
						$(this).parent('li').find('a').addClass('last');
					}
					aniComplete = false;
					that.vars.wrapContent.animate({
						'marginLeft': -mgLeft
					},options.speed, function(){
										aniComplete = true;
										var idPro = pages.eq(current).attr('id');
										if(idPro){
											that.vars.selectLinks.filter('[class=' + idPro + ']').trigger('click');
										}
									});
				}
			});					
			
			$(document).keydown(function(e){
    			if (e.keyCode == 39) { 
				if(e) e.preventDefault();
				$(this).parent('li').parent('.listBtn').find('.prev').find('img').css({'opacity': 1});
				$(this).parent('li').parent('.listBtn').find('.prev').find('span').css({'opacity': 1});
				$(this).parent('li').parent('.listBtn').find('a.first').removeClass('first');
				if(!aniComplete){
					return;
				}
				if(current < pages.length -1){
					current++;
					
					var mgLeft = 0,
						count = 0;
					while(count < current){
						mgLeft += pages.eq(count).outerWidth();
						count++;
					};
					if(count == pages.length - 1){
						$(this).parent('li').find('img').css({'opacity': options.button_Opacity});
						$(this).parent('li').find('span').css({'opacity': options.button_Opacity});
						$(this).parent('li').find('a').addClass('last');
					}
					aniComplete = false;
					that.vars.wrapContent.animate({
						'marginLeft': -mgLeft
					},options.speed, function(){ aniComplete = true;});
				}
				}
			});
			

			that.vars.prevButton.unbind('click').bind('click',function(e){
				if(e) e.preventDefault();
				that.vars.nextButton.children().css({'opacity': 1});
				that.vars.nextButton.removeClass('last');
				if(!aniComplete){
					return;
				}
				if(current > 0){
					current--;
					var mgLeft = 0,
						count = 0;
					while(count < current){
						mgLeft += pages.eq(count).outerWidth();
						count++;
					};
					if(current == 0){
						that.vars.prevButton.children().css({'opacity': options.buttonOpacity});
						that.vars.prevButton.addClass('first');
					}
					aniComplete = false;
					that.vars.wrapContent.animate({
						'marginLeft': -mgLeft
					}, options.speed, function(){ 
						aniComplete = true;
						var idPro = pages.eq(current).attr('id');
						pages.data('current',current);
						if(idPro == '') {
							var cur = pages.data('current');
							while(findProject(cur) == ""){
								cur -= 1;
								idPro = findProject(cur);
							}
						}
						if(idPro){
							$('.csText span').text(that.vars.selectLinks.filter('[class=' + idPro + ']').text());
						}
						
					});
				}
			});		
			
			$(document).keydown(function(e){
    			if (e.keyCode == 37) { 
				if(e) e.preventDefault();
				$(this).parent('li').parent('.listBtn').find('.next').find('img').css({'opacity': 1});
				$(this).parent('li').parent('.listBtn').find('.next').find('span').css({'opacity': 1});
				$(this).parent('li').parent('.listBtn').find('a.last').removeClass('last');
				if(!aniComplete){
					return;
				}
				if(current > 0){
					current--;
					var mgLeft = 0,
						count = 0;
					while(count < current){
						mgLeft += pages.eq(count).outerWidth();
						count++;
					};
					if(current == 0){
						$(this).parent('li').find('img').css({'opacity': options.button_Opacity});
						$(this).parent('li').find('span').css({'opacity': options.button_Opacity});
						$(this).parent('li').find('a').addClass('first');
					}
					aniComplete = false;
					that.vars.wrapContent.animate({
						'marginLeft': -mgLeft
					},options.speed, function(){ aniComplete = true;});
				}
				}
			});
			

		});
	};
	
})(jQuery);
