/*
 * common.js
 * depends jquery.js (v1.3.2) + jquery.easing.js;
 * 
 */


// selector extends
$.extend($.expr[':'], {

	// out link
	external: function(a,i,m) {
		if (!a.href) {
			return false;
		}
		return a.hostname && a.hostname !== window.location.hostname;
	}
	
});


// extends method
$.fn.extend({

	// open new window
	openWindow: function() {
		$(this).click(function() {
			window.open(this.href);
			return false;
		});
	}
	
});


// local navigation active proc[plugin]
$.fn.path = function() {
	$(this).each(function() {
		if (this.href == window.location.href) {
			$(this).addClass('active');
		}
	});
	return this;
};

$.fn.globalPath = function(options) {
	var o = $.extend({
		root: '/en/',
		ignoreFiles: '/index\.html|index\.htm|index\.shtml|index\.php|index\.cgi/i'
	}, options);
	$(this).each(function() {
		if (this.hostname != location.hostname) {return this;}
		this.path = this.pathname.replace(o.ignoreFiles, '');
		if (!this.path.match(/^\//)) {
			this.path = '/' + this.path;
		}
		if (this.path == o.root) {
			if (this.path == location.pathname.replace(o.ignoreFiles, '')) {
				$(this).find('img').addClass('active').swapImg();
			} else if (this.path + 'index.html' == location.pathname){
				$(this).find('img').addClass('active').swapImg();
			}
		}
		else if (location.pathname.search(this.path) == 0) {
			$(this).find('img').addClass('active').swapImg();
		}
	});
	return this;
};

$.fn.swapImg = function(options) {
	var o = $.extend({
		swapImgPostfix: '_on'
	}, options);
	$(this).each(function() {
		this.swapImgSrc = $(this).attr('src').replace(/(\.gif|\.jpeg|\.jpg|\.png)/i, o.swapImgPostfix + '$1');
		$(this).attr('src', this.swapImgSrc);
	});
	return this;
};

$.fn.rollover = function(options) {
	var o = $.extend({
		swapImgPostfix: '_on'
	}, options);
	$(this).not('.active').each(function() {
		this.defaultSrc = $(this).attr('src');
		this.hoverSrc = this.defaultSrc.replace(/(\.gif|\.jpeg|\.jpg|\.png)/i, o.swapImgPostfix + '$1');
		this.hoverImg = new Image;
		this.hoverImg.src = this.hoverSrc;
	}).hover(function() {
		$(this).attr('src', this.hoverSrc);
	}, function() {
		$(this).attr('src', this.defaultSrc);
	});
	return this;
};


// tab change [plugin]
$.fn.tabs = function(initial) {
	$(this).each(function() {
		$(this).find('.tab-naviOn').removeClass('tab-naviOn');
		var tabsBody = $(this).children('div');
		var tabsNav = $(this).children('ul');
		var tabsNavInitial = initial? 'nth-child(' + initial + ')': 'first';
		var tabsNavSelected = $(tabsNav).children('li:' + tabsNavInitial);
		var tabsNavLink = $(tabsNav).find('[href^="#"]');
		$(tabsBody).addClass('tabs-body').hide();
		$(tabsNav).addClass('tabs-nav');
		$(tabsNavSelected).addClass('tab-naviOn').each(function() {
			$($(this).find('[href^="#"]').attr('href')).show();
		});
		$(tabsNavLink).click(function() {
			$(tabsBody).hide();
			$(tabsNav).find('.tab-naviOn').removeClass('tab-naviOn');
			$(this).parent('li').addClass('tab-naviOn');
			$($(this).attr('href')).show();
			return false;
		});
	});
};




$(function() {

	// assessment JavaScript
	$('body').addClass('js-enabled');
	
	// assessment browser
	var browserName;
	var browserVer;
	if ($.browser.msie) {
		browserName = 'msie';
		switch ($.browser.version) {
			case '6.0': browserVer = 'v6'; break;
			case '7.0': browserVer = 'v7'; break;
			case '8.0': browserVer = 'v8'; break;
			default: browserVer = 'v5';
		}
		
		$('body').addClass(browserVer);
	} else if ($.browser.mozilla) {
		browserName = 'mozilla';
	} else if ($.browser.safari) {
		browserName = 'safari';
	} else if ($.browser.opera) {
		browserName = 'opera';
	} else {
		browserName = 'unknown';
	}
	$('body').addClass(browserName);

	// links
	//$('a:external').addClass('external').openWindow();
	$('a.openWin').openWindow();
	$('a[href$=".pdf"]').addClass('pdf').openWindow();
	$('a[href$=".zip"]').addClass('zip');
	$('a[href$=".exe"]').addClass('exe');
	$('a[href^="https:"]').addClass('https');
	$('a[href^="ftp:"]').addClass('ftp');
	$('a[href^="mailto:"]').addClass('mailto');
	
	//POPUP
	$('a.popWin').click(function(){
		window.open(this.href, 'popWindow','toolbar=yes,location=no,directories=no,status=no,menubar=yes,scrollbars=yes,resizable=no,width=550,height=820');
		return false;
	});
	$('.btnClose > a').click(function(){
		window.close();
		return false;
	});

	// scroll
	if (!$.browser.opera) {
		$('.pageTop > a').click(function() {
			$('html, body').animate({scrollTop: 0}, 600, 'easeOutExpo');
			return false;
		});
		$('[href^="#"]:not(.pageTop > a):not(a.switcher)').click(function() {
			if ($.browser.msie && $.browser.version == 7.0) return; // IE7 not run
			var targetOffset = $(this.hash).offset().top;
			var documentHeight = $(document).height();
			var windowHeight = $(window).height();
			if ((documentHeight - targetOffset) < windowHeight) {
				targetOffset = (documentHeight - windowHeight);
			}
			$('html, body').animate({scrollTop: targetOffset}, 600, 'easeOutExpo');
		});
	}
	
	// proc active local navigation
	$('#LocalNaviArea a:not([href*="#"])').path();

	// proc active global navigation
	$('#GlobalNaviArea ul a').globalPath();

	// rollover
	$('.over').rollover();

	//  change font size
	var font_cnt = $.cookie('c_font_cnt');
	if(font_cnt == null) font_cnt = 1;
		
	$('.switcher a').click(function() {
		if(font_cnt<5 && $(this).attr('title') == 'Font Size: Enlarge'){
			font_cnt++;
		}else if($(this).attr('title') == 'Font Size: Standard'){
			font_cnt = 1;
		}
		switchStyle();
		return false;
	});
	
	if(font_cnt) {
		switchStyle();
	}
	
	function switchStyle() {
		styleName = 'Font Size: '+font_cnt;
		$('link[rel*="stylesheet"][title]').each(function() {
			this.disabled = true;
			if ($(this).attr('title') == styleName) {
				this.disabled = false;
			}
		});
		$('img[class*="hd-fontLevel"]').each(function() {
			this.swapImgSrc = $(this).attr('src').replace(/(_on)/i, '');
			if ($(this).attr('alt') == styleName) { 
				this.swapImgSrc = this.swapImgSrc.replace(/(\.gif|\.jpeg|\.jpg|\.png)/i, '_on' + '$1')
			}
			$(this).attr('src', this.swapImgSrc);
		});

		$.cookie('c_font_cnt', font_cnt, {
			expires: 30,  // day
			path: '/'     // dir
		});
	}

	// change tabs
	//$('#tabChange').tabs();

});


