/**
 * common.js
 */

$(function(){
	
	
	//-------------
	// Initialize
	//-------------
	$('html, body').animate({scrollTop: $('div#wrapper').offset().top}, 0, 'linear');
	
	
	//-------------
	// Window
	//-------------
	window.CommonInitialier = function(){
		
	}
	
	CommonInitialier.animateFooter = function() {
		if(SECTION == 'index') {
			var targetY = $(window).height() + $(window).scrollTop() - $('div#footer-wrapper').height();
			$('div#footer-wrapper').stop(true, true).delay(500).animate({top: targetY}, 1000, 'easeInOutQuint');
		}
	}
	
	
	//-------------
	// Scroll
	//-------------
	// Window Scroll
	$(window).scroll(function () {
		var offsetY = $(window).scrollTop();
		if(offsetY == 0) $('div#to-top-wrapper').hide();
		else $('div#to-top-wrapper').show();
		
		
		if($('div#footer-wrapper').filter(":animated").length === 0) {
			CommonInitialier.animateFooter();
			
			// Previous-Next navigation
			if($('div#previous-navigation').length > 0) {
				if($('div#previous-navigation').css('display') == 'none') return;
				var targetY = Math.abs(offsetY) + 110;
				$('div#previous-navigation').stop(true, true).delay(1000).animate({top: targetY}, 1000, 'easeInOutQuint');
			}
			
			if($('div#next-navigation').length > 0) {
				if($('div#next-navigation').css('display') == 'none') return;
				var targetY = Math.abs(offsetY) + 110; 
				$('div#next-navigation').stop(true, true).delay(1000).animate({top: targetY}, 1000, 'easeInOutQuint');
			}
			
			// Image trigger
			if($('div#background-image-trigger').length > 0) {
				var triggerHeight = Math.ceil(($(window).height() - 150 - $('div#footer-wrapper').height()) / 2);
				var imageTriggerObj  = $('div#background-image-trigger');
				imageTriggerObj.css('height', triggerHeight + 'px');				
				imageTriggerObj.css('top', (offsetY + 150) + 'px');
			}
			if($('div#background-image-trigger2').length> 0) {
				var triggerHeight = Math.ceil(($(window).height() - 150 - $('div#footer-wrapper').height()) / 2);
				var imageTriggerObj2 = $('div#background-image-trigger2');
				imageTriggerObj2.css('height', triggerHeight + 'px');
				imageTriggerObj2.css('top', (offsetY + 150 + triggerHeight) + 'px');
			}
		} else {
			if($('div#newsletter-container').css('display') == 'none') {
				CommonInitialier.animateFooter();
			}
		}
	});
	$('div#to-top-wrapper a').click(function(){
		$('html, body').animate({scrollTop: Math.floor($('div#wrapper').offset().top)}, 500, 'easeInQuad');
	});
	//$(window).trigger('scroll');
	
	
	//----------------
	// Header Setting
	//----------------
	$('div#header-background').css('opacity', '0.9');
	$('a.navigation-link').hover(
		function(){
			var idName            = $(this).attr('id');
			var matchResult       = idName.match(/nav-(\d){1}/i);
			var myId              = matchResult[1];
			var targetDescription = $('ul#navigation-description-list > li:nth-child('+ myId +')');
			var targetLeft        = $(this).parent().position().left;
			targetDescription.css('left', Math.floor(725 - targetDescription.width()) + 'px');
			targetDescription.css('opacity', 0.0);
			targetDescription.show();
			targetDescription.animate({opacity: '1.0', left: targetLeft}, 800 - (100 * myId), 'linear', function(){
				$(this).css('filter', 'none');
			});
		}, 
		function(){
			var idName            = $(this).attr('id');
			var matchResult       = idName.match(/nav-(\d){1}/i);
			var targetDescription = $('ul#navigation-description-list > li:nth-child('+matchResult[1]+')');
			targetDescription.hide();
		}
	);
	$('a.navigation-link').click(function(){
		var idName            = $(this).attr('id');
		var matchResult       = idName.match(/nav-(\d){1}/i);
		var targetDescription = $('ul#navigation-description-list > li:nth-child('+matchResult[1]+')');
		var targetDescription = $('ul#navigation-description-list > li:nth-child('+matchResult[1]+')');
		targetDescription.hide();
	});
	var keywordDefaultText = '';
	$('input#search-keyword').inputDefaultText({text: keywordDefaultText});
	$('input#search-keyword').bind('FOCUSED', function(evt){
		$('p#search-error-text').hide();
	});
	$('button#search-submit').buttonTextHover({normalColor: '#5a5a5a', hoverColor: '#000000'});
	$('button#search-submit').click(function(){
		$('p#search-error-text').hide();
		var keyword = jQuery.trim($('input#search-keyword').val());
		if(keyword == keywordDefaultText || keyword.length < 3) {
			$('p#search-error-text').show();
			return false;
		}
		var formObj = window.getDefinitionByName('search-form');
		formObj.keyword = keyword;
		formObj.method  = 'get';
		formObj.submit();
	});
	
	
	//----------------
	// Footer Setting
	//----------------
	//$('div#footer-background').css('opacity', '0.9');
	// NewsLetter
	$('a#newsletter-header-link').click(function(){
		
		var newsletterContainer = $('div#newsletter-container');
		if(!newsletterContainer.is(':hidden')) {// Close Newsletter
			$(this).css('background-image', "url('./images/common/footer/newsletterArrowToBottom.png')");
			newsletterContainer.slideToggle('normal', function(){
				//$('html, body').animate({scrollTop: $('div#wrapper').offset().top}, 1000, 'easeInQuad', function(){});
				CommonInitialier.animateFooter();
			});
		} else {// Open Newsletter
			$(this).css('background-image', "url('./images/common/footer/newsletterArrowToTop.png')");
			newsletterContainer.slideToggle('normal', function(){
				$('html, body').stop(true, true).animate({scrollTop: $('div#footer-wrapper').offset().top}, 500, 'easeInQuad', function(){
					CommonInitialier.animateFooter();
				});
			});
		}
		
		// Reset
		var errors = $('ul#newsletter-error');
		errors.css('display', 'none');
		errors.css('opacity', '1.0');
	});
	
	// Submit
	$('a#newsletter-submit').click(function(){
		// Reset
		var errors = $('ul#newsletter-error');
		errors.css('display', 'none');
		errors.css('opacity', '1.0');
		
		var name     = $('input#newsletter-name-field').val();
		var email    = $('input#newsletter-email-field').val();
		var postcode = $('input#newsletter-postcode-field').val();
		var agree    = $('input#newsletter-agree-checkbox').attr('checked');
		var error = '';
		if(name == '') {
			error += "<li>Please fill your name.</li>";
		}
		if(!email.isEmail()) {
			error += "<li>Please enter a valid email address.</li>";
		}
		if(postcode == '') {
			error += "<li>Please fill your postcode.</li>";
		}
		if(agree == false) {
			error += "<li>Please agree about the privacy policy.</li>";
		}
		if(error != '') {
			errors.css('display', 'block');
			errors.html(error);
			return;
		}
		
		// Send a request
		var params = {name: name, email: email, postcode: postcode, agree: agree};
		
		 $.post('api/add/', params, 
			function(data){
				if(data.status == 'success') {
					onResultHandler(data);
				} else {
					onErrorHandler(data);
				}
			}, 
			'json'
		);
		
		function onResultHandler(data) {
			var error = "Your message has been successfully sent.";
			errors.css('display', 'block');
			errors.html(error);
			errors.delay(3000).animate({opacity: '0.0'}, 500, 'linear', function(){
				$(this).css('filter', 'none');
				$(this).hide();
				$('a#newsletter-reset').click();
			});
		}
		
		function onErrorHandler(data) {
			var error = '';
			for(var i = 0; i < data.message.length; i ++) {
				error += "<li>" + data.message + "</li>";
			}
			if(error != '') {
				errors.css('display', 'block');
				errors.html(error);
				return;
			}
		}
		
	});
	
	// Reset
	$('a#newsletter-reset').click(function(){
		
		// Reset
		var errors = $('ul#newsletter-error');
		errors.css('display', 'none');
		errors.html('');
		
		$('input#newsletter-name-field').val('');
		$('input#newsletter-email-field').val('');
		$('input#newsletter-postcode-field').val('');
		$('input#newsletter-agree-checkbox').attr('checked', '');
	});
});
