/* *******************************************************
J  a  v  a  c  a  t  z
Filename:   [project].js
Purpose:    This file is created to hold common functions
            that are used in project only.
            Please do not put other codes in this file.
Created by: KY 2009.05.26
******************************************************* */

function project_init() {
	$$('.topbar .videoListLayer').each( function(node) {
		$(node).setStyle({'display':'block'}).hide();
		/*
		$(node).absolutize().setStyle({'marginTop':'-'+$(node).getHeight()+'px'});
		var background = new Element('div').absolutize().setOpacity(0.5).setStyle({ 'backgroundColor':'#999', 'width':$(node).getWidth()+'px', 'height':$(node).getHeight()+'px' });
		$(node).insert({'top':background});
		*/
	} );
	$$('.topbar .video a').each( function(node) {
		$(node).setAttribute('onclick','return false;');
		$(node).onclick = function() { return false; };
		/*
		$(node).observe('mouseenter', function(e) {
			new Effect.toggle($$('.topbar .videoListLayer').first(), 'appear', { duration: 0.5 } );
		} );
		*/
		$(node).observe('click', function(e) {
			new Effect.toggle($$('.topbar .videoListLayer').first(), 'appear', { duration: 0.5 } );
		} );
	} );
	if($$('body.text-only').length == 0) {
		var specialnavOpacity = 0.2;
		$$('#specialnav .specialnav').each( function(node) {
			var mask = new Element('span', { 'class':'mask' });
			$(node).insert({ 'top': mask });
			$(node).observe('mouseenter', function(e) {
				// $(node).addClassName('current');
				if(!$(node).hasClassName('active') && $(node) && $(node).down('.mask')) {
					/*
					$(node).down('.mask').setOpacity(0.0);
					*/
					new Effect.Appear($(node).down('.mask'), { duration: 0.3, to: 0.0 });
					if($$('body#homepage').length == 0) {
						// $(node).down('ul').show();
						new Effect.BlindDown($(node).down('ul'), { duration: 0.3, afterFinish: function() { $(node).down('ul').setStyle({'height':'auto'}); } });
					}
				}
			} );
			$(node).observe('mouseleave', function(e) {
				// $(node).removeClassName('current');
				if(!$(node).hasClassName('active') && $(node) && $(node).down('.mask')) {
					/*
					$(node).down('.mask').setOpacity(specialnavOpacity);
					*/
					new Effect.Appear($(node).down('.mask'), { duration: 0.3, to: 0.2 });
					if($$('body#homepage').length == 0) {
						// $(node).down('ul').hide();
						new Effect.BlindUp($(node).down('ul'), { duration: 0.3, afterFinish: function() { $(node).down('ul').setStyle({'height':'auto'}); } });
					}
				}
			} );
			if(!$(node).hasClassName('active')) {
				$(mask).setOpacity(specialnavOpacity);
			} else {
				$(mask).setOpacity(0.0);
			}
			if($$('body#homepage').length == 0) {
				// $(node).down('ul').hide();
				$(node).down('ul').hide();
			}
			var bottom = new Element('span', { 'class':'bottom' });
			$(node).insert({ bottom: bottom });
		} );
	}
}

var topbanner = {
	currentIndex: 0,
	list: new Array(),
	init: function() {
		if(topbanner.list.length > 0) {
			setInterval('topbanner.next();',10000);
		}
	},
	next: function() {
		topbanner.currentIndex++;
		if(topbanner.currentIndex >= topbanner.list.length) topbanner.currentIndex = 0;
		$('page_inner').setStyle({ 'backgroundImage':'url("'+topbanner.list[topbanner.currentIndex]+'")' });
	}
};

var video = {
	show: function(fullpath, title) {
		disableOverlay.show();
		var width = 640;
		var height = 480;
		var left = (document.viewport.getScrollOffsets()[0] + (document.viewport.getWidth() - width) / 2);
		if(left < 0) left = 0;
		var top = (document.viewport.getScrollOffsets()[1] + (document.viewport.getHeight() - height - 60) / 2);
		if(top < 0) top = 0;
		if($('layerVideoPlayer')) $('layerVideoPlayer').remove();
		var layerVideoPlayer = new Element('div', { 'id':'layerVideoPlayer' }).absolutize().hide().setStyle({ 'backgroundColor':'#fff', 'marginLeft':left + 'px', 'marginTop':top + 'px', 'width':width+'px', 'height':'auto', 'zIndex':'99999' }).update('<div class="align_left"><a href="javascript:video.hide();" class="float_right" style="margin:3px 3px 3px 10px;" title="Close"><img src="img/button/close.gif" alt="Close" title="Close" /></a><div style="font-size:109%;font-weight:bold;margin:3px;">'+title+'</div></div><div id="videoPlayerFlash" style="width:'+width+'px;height:'+height+'px;"></div>');
		$('page').insert({'before':layerVideoPlayer});
		new Effect.Appear($(layerVideoPlayer), { duration: 1.0, afterFinish: function() { swfobject.embedSWF('flash/media_list_player.swf', 'videoPlayerFlash', width, height, '9.0.0', 'flash/expressInstall.swf', { 'autoplay':'1', 'path':fullpath }, { 'quality':'high', 'bgcolor':'#000000', 'play':'true', 'loop':'false' } ); } });
		$('layer_disable_overlay').observe('click', function(e) { video.hide(); });
	},
	hide: function() {
		$('layer_disable_overlay').stopObserving('click');
		disableOverlay.hide();
		if($('layerVideoPlayer')) {
			$('videoPlayerFlash').remove();
			new Effect.Fade($('layerVideoPlayer'), { duration: 1.0, afterFinish: function() { $('layerVideoPlayer').remove(); } });
		}
	}
};

var login = {
	show: function() {
		if($('loginLayer')) {
			new Effect.ScrollTo($('topnav'));
			// $('loginLayer').show();
			new Effect.Appear($('loginLayer'), { duration: 0.3 });
		}
	},
	hide: function() {
		if($('loginLayer')) {
			// $('loginLayer').hide();
			new Effect.Fade($('loginLayer'), { duration: 0.3 });
		}
	}
};

/* Print bar + Adjust fontsize */
var smallerFontSizePc = 100;	// in %
var biggerFontSizePc = 120;	// in %
addthis_pub  = 'javacatz';
/* full printBar */
var printBar = "<a href='javascript:printPage2();' title='Print this page'><img src='img/ico_print.gif' alt='Print' /></a> &nbsp; <a href=\"http://www.addthis.com/bookmark.php\" onMouseOver=\"return addthis_open(this, '', '[URL]', '[TITLE]')\" onMouseOut=\"addthis_close()\" onClick=\"return addthis_sendto()\" class='shareIcon'><img src='img/ico_share.gif' alt='Share' /></a> &nbsp;<img src='img/printbar_vline.gif' alt='|' />&nbsp; Text: <a href='javascript:adjustFontsizePc(" + smallerFontSizePc + ");' title='Smaller Text'><img src='img/ico_smallersize.gif' alt='smaller' /></a> <a href='javascript:adjustFontsizePc(" + biggerFontSizePc + ");' title='Bigger Text'><img src='img/ico_biggersize.gif' alt='bigger' /></a>";
var printBar2 = "Text: <a href='javascript:adjustFontsizePc(" + smallerFontSizePc + ");' title='Smaller Text'><img src='img/ico_smallersize.gif' alt='smaller' /></a> <a href='javascript:adjustFontsizePc(" + biggerFontSizePc + ");' title='Bigger Text'><img src='img/ico_biggersize.gif' alt='bigger' /></a>";
var printBar3 = "<a href=\"http://www.addthis.com/bookmark.php\" onMouseOver=\"return addthis_open(this, '', '[URL]', '[TITLE]')\" onMouseOut=\"addthis_close()\" onClick=\"return addthis_sendto()\" class='shareIcon'><img src='img/ico_share.gif' alt='Share' /></a> &nbsp;<img src='img/printbar_vline.gif' alt='|' />&nbsp; Text: <a href='javascript:adjustFontsizePc(" + smallerFontSizePc + ");' title='Smaller Text'><img src='img/ico_smallersize.gif' alt='smaller' /></a> <a href='javascript:adjustFontsizePc(" + biggerFontSizePc + ");' title='Bigger Text'><img src='img/ico_biggersize.gif' alt='bigger' /></a>";

function printbar_init() {
	/* show print bar */
	var tags = $$('.printBar');
	if(tags!=null && tags!="" && tags.length>0) {
		tags.each( function(node) { $(node).innerHTML = printBar; } );
	}
	tags = $$('.printBar2');
	if(tags!=null && tags!="" && tags.length>0) {
		tags.each( function(node) { $(node).innerHTML = printBar2; } );
	}
	tags = $$('.printBar3');
	if(tags!=null && tags!="" && tags.length>0) {
		tags.each( function(node) { $(node).innerHTML = printBar3; } );
	}
}
function adjustFontsizePc(sizechangePc) {
	if( sizechangePc == 'small' ) fontsizePc = smallerFontSizePc;	// same as font-size in body
	else if( sizechangePc == 'large') fontsizePc = biggerFontSizePc;
	else if( !isNaN(sizechangePc) ) fontsizePc = sizechangePc;
	$$('.fontSize').each( function(node) { $(node).style.fontSize = fontsizePc + "%"; } );
}



var backTopHTML = "<a href='#top' onclick='window.scrollTo(0,0);return false;' title='Back to top'>Back to top <img src='img/icon/ico_backtotop.gif' alt='[^]' /></a>";
var const__ky_backTop;
function ky_backTop_init(interval) {
	if(interval==null) interval = 500;
	const__ky_backTop = $$('.backTop');
	if(const__ky_backTop.length>0) {
		setInterval('ky_backTop()',interval);
	}
}
function ky_backTop() {
	const__ky_backTop.each( function(node) {
	    if (node.cumulativeOffset()[1] > document.viewport.getHeight()) {
			if(node.innerHTML.indexOf("#top") == -1) {	// use meaningful substring in your backTopHTML for indexOf
				node.innerHTML = backTopHTML;
			}
		} else {
		    //if(node.innerHTML != "") 
			node.innerHTML = "";
		}
	} );
}



function tableAltRow(id) {
	if(id) {
		var boolDark = false;
		$$('#'+id+' tr').each( function(node) { if(boolDark) $(node).addClassName("tr_even"); else $(node).addClassName("tr_odd"); boolDark = !boolDark; } );
	} else {
		$$('table.tableAltRow').each( function(tableNode) {
			var boolDark = false;
			var temp_tr = $(tableNode).getElementsByTagName('tr');
			for(var i=0; i<temp_tr.length; i++) {
				if(boolDark) $(temp_tr[i]).addClassName("tr_even");
				else $(temp_tr[i]).addClassName("tr_odd");
				boolDark = !boolDark;
			}
		} );
	}
}

function listAltRow(id) {
	if(id) {
		var boolDark = false;
		$$('ul#'+id).childElements().each( function(node) {
			if(boolDark) $(node).addClassName("li_even");
			else $(node).addClassName("li_odd"); boolDark = !boolDark;
		} );
	} else {
		$$('ul.listAltRow').each( function(listNode) {
			var boolDark = false;
			var temp_li = $(listNode).childElements();
			for(var i=0; i<temp_li.length; i++) {
				if(boolDark) $(temp_li[i]).addClassName("li_even");
				else $(temp_li[i]).addClassName("li_odd");
				boolDark = !boolDark;
			}
		} );
	}
}

