// init page
jQuery(function() {
	initWindowSize();
})
jQuery(window).load(function() {
	setTimeout(function() {
		initCufon();
		initShowPage();
		initScroll();
		hideFormText();
		initFadeBtn();
	}, 500);
})

function initShowPage() {
	jQuery('body').addClass('ready');
	jQuery('#content').fadeIn();
	jQuery('.menu').fadeIn();
}

// initWindowSize
function initWindowSize() {
	var fixedClass = 'min-size';
	var body = jQuery('body');
	var sections = jQuery('#content .section');
	var textBox = jQuery('> .text-box', sections);
	var wH = jQuery(window).height();
	var minH = 400;
	var H;
	var deltaH = - 400;
	var delta = jQuery('#header').outerHeight() + jQuery('#footer').outerHeight();
	
	function calcH() {
		wH = jQuery(window).height();
		if(wH < minH) {
			body.addClass(fixedClass);
			H = minH - delta;
		}
		else {
			body.removeClass(fixedClass);
			H = wH - delta;
		}
		
		sections.css({
			height: H
		})
		
		textBox.each(function() {
			var box = jQuery(this),
				boxH = box.height(),
				mt = (H + deltaH) / 2;
			
			if(mt < 0) mt = 0;
			
			box.css({
				marginTop: mt
			})
		})
	};
	jQuery(window).resize(calcH);
	setTimeout(calcH, 100);
}

// initGreenBtn
function initFadeBtn() {
	if(jQuery.browser.msie && jQuery.browser.version < 9) return;
	jQuery('a.green-btn').each(function() {
		var btn = jQuery(this);
		btn.css({
			'backgroundColor': '#fff'
		})
		btn.mouseenter(function() {
			jQuery(this).animate({
				'backgroundColor': '#88bc67'
			}, {
				queue: false
			})
		}).mouseleave(function() {
			jQuery(this).animate({
				'backgroundColor': '#fff'
			}, {
				queue: false
			})
		})
	});
}


// hideFormText
function hideFormText() {
	var _inputs = document.getElementsByTagName('input');
	var _txt = document.getElementsByTagName('textarea');
	var _value = [];
	
	if (_inputs) {
		for(var i=0; i<_inputs.length; i++) {
			if (_inputs[i].type == 'text' || _inputs[i].type == 'password') {
				
				_inputs[i].index = i;
				_value[i] = _inputs[i].value;
				
				_inputs[i].onfocus = function(){
					if (this.value == _value[this.index])
						this.value = '';
				}
				_inputs[i].onblur = function(){
					if (this.value == '')
						this.value = _value[this.index];
				}
			}
		}
	}
	if (_txt) {
		for(var i=0; i<_txt.length; i++) {
			_txt[i].index = i;
			_value['txt'+i] = _txt[i].value;
			
			_txt[i].onfocus = function(){
				if (this.value == _value['txt'+this.index])
					this.value = '';
			}
			_txt[i].onblur = function(){
				if (this.value == '')
					this.value = _value['txt'+this.index];
			}
		}
	}
}

// initScroll
function initScroll() {
	var navLis = jQuery('.add-menu > li'),
		h = jQuery('#header').outerHeight(),
		links = jQuery('a', navLis),
		ico = jQuery('.ico', links),
		win = jQuery(window),
		duration = (jQuery.browser.msie && jQuery.browser.version < 9) ? 0 : 300,
		textBox = jQuery('.section > .text-box'),
		tShow = null,
		lin,
		textSpeed = 800,
		delayShowText = 100,
		activePos = 1;
	
	textBox.css({
		top: 2000
	});
	
	links.each(function() {
		var link = jQuery(this),
			parent = link.parent(),
			targetBox = jQuery(this.hash);
		
		if(targetBox.length) {
			this.textbox = jQuery('> .text-box', targetBox);
			this.topTarget = targetBox.offset().top;
			this.bottomTarget = this.topTarget + targetBox.outerHeight();
			link.click(function() {
				navLis.removeClass('active');
				parent.addClass('active');
				jQuery.scrollTo(this.topTarget, 300);
				activePos = this.topTarget;
				return false;
			})
		}
	})
	
	function setTop() {
		links.each(function() {
			var link = jQuery(this),
				targetBox = jQuery(this.hash);
			
			if(targetBox.length) {
				this.topTarget = targetBox.offset().top;
				this.bottomTarget = this.topTarget + targetBox.outerHeight();
			}
		});
		
		jQuery(window).scrollTop(activePos);
	}
	
	win.resize(setTop);
	
	win.scroll(function() {
		links.each(function(k) {
			if(this.topTarget != 'undefined' && win.scrollTop() >= this.topTarget && win.scrollTop() < this.bottomTarget) {
				navLis.removeClass('active');
				jQuery(this).parent().addClass('active');
				activePos = this.topTarget;
				
				lin = this;
				if(tShow) clearTimeout(tShow);
				textBox.not(lin.textbox).animate({
					top: 5000
				}, {
					queue: false,
					duration: textSpeed
				});
				tShow = setTimeout(function() {
					lin.textbox.animate({
						top: 133
					}, {
						queue: false,
						duration: textSpeed
					})
				}, delayShowText)
				
				ico.animate({
					opacity: 0
				}, {
					queue: false,
					duration: duration
				})
				jQuery('.ico', this).animate({
					opacity: 1
				}, {
					queue: false,
					duration: duration
				})
			}
		})
	})
	jQuery(window).scrollTop(activePos);
	
	jQuery('.menu a').each(function() {
		var link = jQuery(this);
		var ico = jQuery('.ico', this);
		
		ico.css({
			opacity: 0,
			visibility: 'visible'
		})
		
		link.mouseenter(function() {
			ico.animate({
				opacity: 1
			}, {
				queue: false,
				duration: duration
			})
		}).mouseleave(function() {
			if(!link.parent().hasClass('active')) {
				ico.animate({
					opacity: 0
				}, {
					queue: false,
					duration: duration
				})
			}
		})
	})
}

/**
 * jQuery.ScrollTo - Easy element scrolling using jQuery.
 * Copyright (c) 2007-2009 Ariel Flesler - aflesler(at)gmail(dot)com | http://flesler.blogspot.com
 * Dual licensed under MIT and GPL.
 * Date: 5/25/2009
 * @author Ariel Flesler
 * @version 1.4.2
 *
 * http://flesler.blogspot.com/2007/10/jqueryscrollto.html
 */
;(function(d){var k=d.scrollTo=function(a,i,e){d(window).scrollTo(a,i,e)};k.defaults={axis:'xy',duration:parseFloat(d.fn.jquery)>=1.3?0:1};k.window=function(a){return d(window)._scrollable()};d.fn._scrollable=function(){return this.map(function(){var a=this,i=!a.nodeName||d.inArray(a.nodeName.toLowerCase(),['iframe','#document','html','body'])!=-1;if(!i)return a;var e=(a.contentWindow||a).document||a.ownerDocument||a;return d.browser.safari||e.compatMode=='BackCompat'?e.body:e.documentElement})};d.fn.scrollTo=function(n,j,b){if(typeof j=='object'){b=j;j=0}if(typeof b=='function')b={onAfter:b};if(n=='max')n=9e9;b=d.extend({},k.defaults,b);j=j||b.speed||b.duration;b.queue=b.queue&&b.axis.length>1;if(b.queue)j/=2;b.offset=p(b.offset);b.over=p(b.over);return this._scrollable().each(function(){var q=this,r=d(q),f=n,s,g={},u=r.is('html,body');switch(typeof f){case'number':case'string':if(/^([+-]=)?\d+(\.\d+)?(px|%)?$/.test(f)){f=p(f);break}f=d(f,this);case'object':if(f.is||f.style)s=(f=d(f)).offset()}d.each(b.axis.split(''),function(a,i){var e=i=='x'?'Left':'Top',h=e.toLowerCase(),c='scroll'+e,l=q[c],m=k.max(q,i);if(s){g[c]=s[h]+(u?0:l-r.offset()[h]);if(b.margin){g[c]-=parseInt(f.css('margin'+e))||0;g[c]-=parseInt(f.css('border'+e+'Width'))||0}g[c]+=b.offset[h]||0;if(b.over[h])g[c]+=f[i=='x'?'width':'height']()*b.over[h]}else{var o=f[h];g[c]=o.slice&&o.slice(-1)=='%'?parseFloat(o)/100*m:o}if(/^\d+$/.test(g[c]))g[c]=g[c]<=0?0:Math.min(g[c],m);if(!a&&b.queue){if(l!=g[c])t(b.onAfterFirst);delete g[c]}});t(b.onAfter);function t(a){r.animate(g,j,b.easing,a&&function(){a.call(this,n,b)})}}).end()};k.max=function(a,i){var e=i=='x'?'Width':'Height',h='scroll'+e;if(!d(a).is('html,body'))return a[h]-d(a)[e.toLowerCase()]();var c='client'+e,l=a.ownerDocument.documentElement,m=a.ownerDocument.body;return Math.max(l[h],m[h])-Math.min(l[c],m[c])};function p(a){return typeof a=='object'?a:{top:a,left:a}}})(jQuery);

/*
 * jQuery Color Animations
 * Copyright 2007 John Resig
 * Released under the MIT and GPL licenses.
 */

;(function(jQuery){

	// We override the animation for all of these color styles
	jQuery.each(['backgroundColor', 'borderBottomColor', 'borderLeftColor', 'borderRightColor', 'borderTopColor', 'color', 'outlineColor'], function(i,attr){
		jQuery.fx.step[attr] = function(fx){
			if ( fx.state == 0 ) {
				fx.start = getColor( fx.elem, attr );
				fx.end = getRGB( fx.end );
			}

			fx.elem.style[attr] = "rgb(" + [
				Math.max(Math.min( parseInt((fx.pos * (fx.end[0] - fx.start[0])) + fx.start[0]), 255), 0),
				Math.max(Math.min( parseInt((fx.pos * (fx.end[1] - fx.start[1])) + fx.start[1]), 255), 0),
				Math.max(Math.min( parseInt((fx.pos * (fx.end[2] - fx.start[2])) + fx.start[2]), 255), 0)
			].join(",") + ")";
		}
	});

	// Color Conversion functions from highlightFade
	// By Blair Mitchelmore
	// http://jquery.offput.ca/highlightFade/

	// Parse strings looking for color tuples [255,255,255]
	function getRGB(color) {
		var result;

		// Check if we're already dealing with an array of colors
		if ( color && color.constructor == Array && color.length == 3 )
			return color;

		// Look for rgb(num,num,num)
		if (result = /rgb\(\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*\)/.exec(color))
			return [parseInt(result[1]), parseInt(result[2]), parseInt(result[3])];

		// Look for rgb(num%,num%,num%)
		if (result = /rgb\(\s*([0-9]+(?:\.[0-9]+)?)\%\s*,\s*([0-9]+(?:\.[0-9]+)?)\%\s*,\s*([0-9]+(?:\.[0-9]+)?)\%\s*\)/.exec(color))
			return [parseFloat(result[1])*2.55, parseFloat(result[2])*2.55, parseFloat(result[3])*2.55];

		// Look for #a0b1c2
		if (result = /#([a-fA-F0-9]{2})([a-fA-F0-9]{2})([a-fA-F0-9]{2})/.exec(color))
			return [parseInt(result[1],16), parseInt(result[2],16), parseInt(result[3],16)];

		// Look for #fff
		if (result = /#([a-fA-F0-9])([a-fA-F0-9])([a-fA-F0-9])/.exec(color))
			return [parseInt(result[1]+result[1],16), parseInt(result[2]+result[2],16), parseInt(result[3]+result[3],16)];

		// Otherwise, we're most likely dealing with a named color
		return colors[jQuery.trim(color).toLowerCase()];
	}
	
	function getColor(elem, attr) {
		var color;

		do {
			color = jQuery.curCSS(elem, attr);

			// Keep going until we find an element that has color, or we hit the body
			if ( color != '' && color != 'transparent' || jQuery.nodeName(elem, "body") )
				break; 

			attr = "backgroundColor";
		} while ( elem = elem.parentNode );

		return getRGB(color);
	};
	
	// Some named colors to work with
	// From Interface by Stefan Petre
	// http://interface.eyecon.ro/

	var colors = {
		aqua:[0,255,255],
		azure:[240,255,255],
		beige:[245,245,220],
		black:[0,0,0],
		blue:[0,0,255],
		brown:[165,42,42],
		cyan:[0,255,255],
		darkblue:[0,0,139],
		darkcyan:[0,139,139],
		darkgrey:[169,169,169],
		darkgreen:[0,100,0],
		darkkhaki:[189,183,107],
		darkmagenta:[139,0,139],
		darkolivegreen:[85,107,47],
		darkorange:[255,140,0],
		darkorchid:[153,50,204],
		darkred:[139,0,0],
		darksalmon:[233,150,122],
		darkviolet:[148,0,211],
		fuchsia:[255,0,255],
		gold:[255,215,0],
		green:[0,128,0],
		indigo:[75,0,130],
		khaki:[240,230,140],
		lightblue:[173,216,230],
		lightcyan:[224,255,255],
		lightgreen:[144,238,144],
		lightgrey:[211,211,211],
		lightpink:[255,182,193],
		lightyellow:[255,255,224],
		lime:[0,255,0],
		magenta:[255,0,255],
		maroon:[128,0,0],
		navy:[0,0,128],
		olive:[128,128,0],
		orange:[255,165,0],
		pink:[255,192,203],
		purple:[128,0,128],
		violet:[128,0,128],
		red:[255,0,0],
		silver:[192,192,192],
		white:[255,255,255],
		yellow:[255,255,0]
	};
	
})(jQuery);
