/* ---------------------------------------------------------------
	
	FUNCTION.JS 
		
--------------------------------------------------------------- */
/*
	EXTEND LIBRARY
*/
jQuery.extend();
jQuery.fn.extend();

var rootPath = function(){
	return jQuery('script#coreJS')[0].src.split('common/js')[0];
}

/*
	AUTOMATION
*/
//FUNCTION initRollOvers
function initRollOvers(){
	if(!document.getElementById) return;

	var preImgArr = new Array();

	$('img.ahover, input.ahover').each(function(){
		var src = $(this).attr('src');
		var isov = src.substring(0,src.lastIndexOf('.'));
		if( isov.substring(isov.length-3, isov.length) != "_ov"
		 && isov.substring(isov.length-3, isov.length) != "_on"
		 && isov.substring(isov.length-4, isov.length) != "_off"
		){
			var ftype = src.substring(src.lastIndexOf('.'), src.length);
			var hsrc = src.replace(ftype, '_ov'+ftype);
			var dsrc = src;
			$(this).attr('hsrc', hsrc);
			$(this).attr('dsrc', dsrc);

			preImgArr.push(new Image());
			preImgArr[preImgArr.length-1].src = hsrc;

			$(this).bind('mouseover', function(e){
				$(this).attr('src', $(this).attr('hsrc'));
				enablePNG (this);
			});
			$(this).bind('mouseout', function(e){
				$(this).attr('src', $(this).attr('dsrc'));
				enablePNG (this);
			});
			if ($.browser.safari) {
				$(this).bind('click', function(e){
					$(this).attr('src', $(this).attr('dsrc'));
					enablePNG (this);
				});
			}
		}
	});
}
//FUNCTION enableFlash (for IE6.5+ on winXP SP2 KB912945)
function enableFlash(){
	if(jQuery.browser.msie) {
		jQuery('object, applet, embed').each(function () {
			this.removeAttribute('data');
			this.outerHTML += "";
		});
	}
}
//FUNCTION enablePNG
function enablePNG (obj) {
	if(jQuery.browser.msie && jQuery.browser.version < 7){
		if(obj && /\.png/.test(obj.src)) {
			var imgSource = obj.src;
			obj.src = rootPath()+'common/imgs/space.gif';
			obj.style.filter = 
				'progid:' +
				'DXImageTransform.Microsoft.AlphaImageLoader' +
				'(src="' + imgSource + '",' +
				'sizingMethod="scale")';
		}else{
			jQuery('img[src*=png]').each(function () {
				var imgSource = this.src;
				this.src = rootPath()+'common/imgs/space.gif';
				this.style.filter = 
					'progid:' +
					'DXImageTransform.Microsoft.AlphaImageLoader' +
					'(src="' + imgSource + '",' +
					'sizingMethod="scale")';
			});
		}
	}
}
//FUNCTION enableCSSPNG
function enableCSSPNG (){
	if(jQuery.browser.msie && jQuery.browser.version < 7){
		var styleSheets = document.styleSheets['masterCSS'].imports;
		//for (var i = 0; i < styleSheets.length; i++) {
		for (var i in styleSheets) {
			var styleSheet = styleSheets[i];
			var rules = styleSheet.rules || styleSheet.cssRules;
			//for (var j = 0; j < rules.length; j++) {
			for (var j in rules) {
				var rule = rules[j];
				var styles = rule.style;
				for (var property in styles) {
					var val = styles[property];
					if( !/png/.test(val) || !/backgroundImage/.test(property) ) continue;
					var regEx = new RegExp("[\.\.\/\]+(([^\"\'\'\"\)]*)\)","");
					var reg = val.match(regEx);
					var imgSrc = RegExp.$1;
					var filter = '' +
							'filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src="' +
							rootPath() + imgSrc +
							'", sizingMethod="scale");';
					styleSheet.addRule(rule.selectorText, "background-image:none");
					styleSheet.addRule(rule.selectorText, filter);
				}
			}
		}
	}
}
//FUNCTION cssBrowserSelector
var cssBrowserSelector = function() {
	var
	ua = navigator.userAgent.toLowerCase(),
	is = function(t){ return ua.indexOf(t) != -1; },
	h = jQuery('body')[0],
	b =	(!(/opera|webtv/i.test(ua)) && /msie (\d)/.test(ua)) ? ('ie ie'+RegExp.$1)
			: is('gecko/') ? 'gecko'
			: is('opera/9') ? 'opera opera9'
			: /opera (\d)/.test(ua) ? 'opera opera'+RegExp.$1
			: is('konqueror') ? 'konqueror'
			: is('applewebkit/') ? 'webkit safari'
			: is('mozilla/') ? 'gecko'
			: '',
	o= (is('x11')||is('linux')) ?' linux'
			:is('mac') ? ' mac'
			:is('win') ? ' win'
			:'';
	var c = b+o+' js';
	h.className += h.className? ' '+c : c;
};
//FUNCTION smoothScroll
var actX;
var actY;
var tarY = 0;
var tarX = 0;
var scrollInt;
var smoothScroll = function () {
	$('a').click(function(){
		var anc = this.href.split('#')[1];
		if(!anc) return;
		if( /#/.test(this.href)  && this.href.match(location.href) && $('#'+anc)[0] ){
			tarX = ($(document).width() > $('#'+anc).position().left + $(window).width())
				? $('#'+anc).position().left
				: $(document).width() - $(window).width();
			tarY = ($(document).height() > $('#'+anc).position().top + $(window).height())
				? $('#'+anc).position().top
				: $(document).height() - $(window).height();
			actX = $(document).scrollLeft();
			actY = $(document).scrollTop();
			clearInterval(scrollInt);
			scrollInt = setInterval('setScroll()', 20);
			return false;
		}
	});
}
function setScroll(){
	actY += (tarY - actY) / 6;
	actX += (tarX - actX) / 6;
	if(Math.abs(tarX - actX) < 2 && Math.abs(tarY - actY) < 2){
		clearInterval(scrollInt);
	}
	scrollTo( Math.round(actX), Math.round(actY));
}

function blankToPop () {
	$('a.commonPop').each(function(){
		$(this).removeAttr("target");
		$(this).attr('popURL', this.href);
		( $(this).attr("rel") ? "" : $(this).attr("rel","") );
		this.href = "javascript:;";
		$(this).click(function () {
			eval("commonPop('"+$(this).attr("popURL")+"',"+$(this).attr("rel")+")");
		});
	});
}

/*
	USER FUNCTION
*/
//FUNCTION commonPop
//pFile = popup File
//pWidth = popup Width [optional]
//pHeight = popup Height [optional]
//pStatus = popup Features[optional]
function commonPop(pFile, pWidth, pHeight,pStatus){
	//var fName = pFile.substring(pFile.lastIndexOf('/')+1, pFile.length);
	var wName = "";
	if(pWidth == null){ pWidth = 700}
	if(pHeight == null){ pHeight = 600}
	if(pStatus == null){ pStatus = 'yes';}
	if(pWidth > screen.availWidth){
		pWidth = screen.availWidth;
		pStatus = 'yes';
	}
	if(pHeight > screen.availHeight-50){
		pHeight = screen.availHeight-50;
		pStatus = 'yes';
	}
	var wFeatures= 'menubar=no,scrollbars='+pStatus+',resizable='+pStatus+',width='+pWidth+',height='+pHeight;
	void(window.open(pFile, wName, wFeatures));
}
//FUNCTION moveOpener
//pFile = file
function moveOpener(pFile){
	if(opener){
		void(opener.location.href = pFile);
	}else{
		void(document.location.href = pFile);
	}
}
//FUNCTION AS externalInterface
function AS (str) {
	if(jQuery.browser.msie){
		return window[str];
	}else{
		return document[str];
	}
}
//FUNCTION infoDisp
$.fn.infoDisp = function(settings){
	settings = $.extend({
		url: "",
		target: "_self"
	}, settings)
	var holder = this;
	$.getJSON(settings.url +'?jsoncallback=?', 
		function(data){
			$.each(data, function(i, item){
				holder.append('<dt>'+ item.date +'</dt><dd><a href="'+ item.link +'" target="'+ settings.target +'">'+ item.title +'</a></dd>');
			});
		}
	);
}
//FUNCTION flashDisp
$.fn.flashDisp = function(settings){
	settings = $.extend({
		file: "",
		width: 0,
		height: 0
	}, settings)
	var holder = this;
	holder.html('<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0" width="'+ settings.width +'" height="'+ settings.height +'"><param name="allowScriptAccess" value="sameDomain" /><param name="movie" value="'+ settings.file +'" /><param name="quality" value="high" /><param name="menu" value="false" /><param name="scale" value="noscale" /><embed src="'+ settings.file +'" quality="high" menu="false" scale="noscale" allowscriptaccess="sameDomain" pluginspace="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="'+ settings.width +'" height="'+ settings.height +'" id="emb_public" name="emb_public"></embed></object>');
}