( function() {
	
	/* menu hover effect */
	function menu_hover() {
		$('.menu-main li').hover(
			function(e) {
				$( e.currentTarget ).addClass('hover');
			},
			function(e) {
				$( e.currentTarget ).removeClass('hover');
			}
		);
	}
	
	/* form submission */
	function form_submission() {
		$('#search-submit')
			.click( /* handle click event */
				function(e) {
					$('#frm-qsearch').submit();
				}
			)
			.hover( /* hover effect */
				function(e) {
					$( e.currentTarget ).addClass('hover');
				},
				function(e) {
					$( e.currentTarget ).removeClass('hover');
				}
			);
	}
	
	/**
	* enable case study & video rotation on the front page
	*/
	function init_rotator() {
		if ( $('.rotator').length == 0 )
			return;
			
		// case study rotation
		window.setInterval(
			function() {
				var current = $('.casestudy-rotator .rotator-selected');
				if ( current.length == 0 )
					current = $('.casestudy-rotator .rotator:first');
				
				var next = current.next();
				if ( next.length == 0 )
					next = $('.casestudy-rotator .rotator:first');
					
				current.hide();
				current.removeClass('rotator-selected');
				next.fadeIn();
				next.addClass('rotator-selected');
			}, 
			10000
		);
		
		// video rotation
		window.setInterval(
			function() {
				var current = $('.video-rotator .rotator-selected');
				if ( current.length == 0 )
					current = $('.video-rotator .rotator:first');
				
				var next = current.next();
				if ( next.length == 0 )
					next = $('.video-rotator .rotator:first');
					
				current.hide();
				current.removeClass('rotator-selected');
				next.fadeIn();
				next.addClass('rotator-selected');
			}, 
			10000
		);
	}
	
	/** 
	* enable hover affect and map click to redirect to a-href at title
	* used in front page
	*/
	function hover_region() {
		
		var target = $('.hover-region');
		if ( target.length == 0 )
			return;
			
		target
			.hover( 
				function(e) {
					$( e.currentTarget ).addClass('hover');
				},
				function(e) {
					$( e.currentTarget ).removeClass('hover');
				}
			)
			.click(
				function(e) {
					var href = $( e.currentTarget ).find('.title a').attr('href');
					if ( /^http:.*/.test( href ) )
						window.open( href );
					else
						window.location.href = href;
					e.preventDefault();
					return false;
				}
			);
	}
	
	/** 
	* for about page
	*/
	function init_about() {
		if ( $('.about-tab').length == 0 )
			return;
		
		if ( $('.about-tab-jquery').length > 0 )
			$('.about-tab-static').hide();
			
		$('.about-tab-jquery').show();
		$('.tab-content:gt(0)').hide();
		
		// enable tab action
		$('.tab-menu')
			.hover(
				function(e) {
					$( e.currentTarget ).addClass('hover');
				},
				function(e) {
					$( e.currentTarget ).removeClass('hover');
				}
			)
			.click( function(e) {
				var target = $( e.currentTarget ).attr('title');
				$('.tab-content').hide();
				$( '.' + target ).fadeIn();
				
				$('.tab-menu').removeClass('active');
				$( e.currentTarget ).addClass('active');
			});
	}
	
	/* for faqs page */
	function init_faqs() {
		if ( $('.faq-answer').length == 0 )
			return;
			
		$('.content-faqs-static').hide();
		$('.faq-answer').hide();
		
		// this enable the sliding of FAQs
		$('.faq-q').addClass('faq-question');
		$('.faq-question').click( function(e)	{
			$( e.currentTarget ).parent().find('.faq-answer').slideToggle();
		});
	}
	
	/* for feedback pages */
	function init_feedback() {
		if ( $('#sel-feedback-year').length == 0 )
			return;
			 
		$('#sel-feedback-year').bind('change',
			function(e) {
				window.location.href = $('#sel-feedback-year option:selected').val();
			}
		);	
	}
	
	function init_searchform() {
		$('.search-submit-static').hide();
		$('.search-submit').show();
	}
	
	$( window ).bind('load', function() {
		menu_hover();
		form_submission();
		hover_region();
		init_rotator();
		init_about();
		init_faqs();
		init_feedback();
		init_searchform();
	});
	
})();
