﻿//date formate
function do_jackbookDOTCOM_changeDate(){
	jQuery("div.sh_dateHeader").each(function(i){
		if(jQuery.trim(jQuery(this).text()) == ""){
			jQuery(this).hide();
		}else{
			var shtml = jackbookDOTCOM_changeDate(jQuery(this).text());
			shtml += getYearFromDate_sh(jQuery(this).text());
			jQuery(this).html(shtml);
		}
	});
	return false;
}

//funp button
function do_funp_genButton(buttonStyle){
	jQuery("div.funp").each(function(i){
		var shtml = funp_genButton(jQuery(this).text(),buttonStyle);
		jQuery(this).html(shtml);
	});
	return false;
}

var button_dimension = {
   1 : {'w' : 60, 'h': 55 },
   2 : {'w' : 60, 'h': 69 },
   3 : {'w' : 80, 'h': 15 },
   4 : {'w' : 91, 'h': 17 },
   5 : {'w' : 95, 'h': 22 },
   6 : {'w' :115, 'h': 22 },
   7 : {'w' : 60, 'h': 66 },
   8 : {'w' :100, 'h': 34 },
   9 : {'w' :106, 'h': 55 },
  10 : {'w' : 60, 'h': 55 },
  11 : {'w' :100, 'h': 20 },
  12 : {'w' : 80, 'h': 15 },
  13 : {'w' : 80, 'h': 15 }
};

function funp_genButton(url,style) {
   if (!style) style = 1;
   if (!url) url='';
   else url="?url="+encodeURIComponent(url);
   /*document.write('<iframe src="http://funp.com/tools/buttoniframe.php'+url+'&s='+style+
                  '" height="'+button_dimension[style]['h']
                  +'" width="'+button_dimension[style]['w']
                  +'" scrolling="no" frameborder="0" marginheight="0" marginwidth="0"></iframe>');*/
	return ('<iframe src="http://funp.com/tools/buttoniframe.php'+url+'&s='+style+
            '" height="'+button_dimension[style]['h']
            +'" width="'+button_dimension[style]['w']
            +'" scrolling="no" frameborder="0" marginheight="0" marginwidth="0"></iframe>');
}

function getYearFromDate_sh(sdate){
	var syear = '<td>';
	syear += '<div class="jackbookDOTCOM_year">' + sdate.substr(sdate.lastIndexOf(', ')+2,4) + '</div>';
	syear += '</td></tr></table>';
	return syear;
}

function jackbookDOTCOM_changeDate(thedate) {
	var newdate = jackbookDOTCOM_dateformat(thedate);
	return newdate;
}

function jackbookDOTCOM_dateformat(dateformat) {
	// Tuesday, July 17, 2008
	dateformat = trim(dateformat);
	if(dateformat.length>0) {
		var vpostdate= dateformat.split(",");
		vpostdate = trim(vpostdate[1]);
		var vpostdate= vpostdate.split(" ");
		var vresult = '<table><tr><td>';
		vresult += '<div class="jackbookDOTCOM_date">' + vpostdate[0].substring(0, 3) + '<div  class="day">' + vpostdate[1] + '</div></div>';
		vresult += '</td>';
		return vresult;
	}
}

//thanks to webtoolkit.info for this trim snippet,
function trim(str, chars) {
	return ltrim(rtrim(str, chars), chars);
}

function ltrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}

function rtrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}

//auto scroll menu plugin for jQuery
(function(){
	//sh_scrollMenu 是這個自製 Plugin 的名稱
	jQuery.fn.sh_scrollMenu = function(options){
		/*
		參數
		height：區塊欲顯示的高度
		*/
		settings = jQuery.extend({
			height: ""
		}, options);
	
		//每個符合搜尋條件的元素皆套用
		return jQuery(this).each(function(){
			//區塊高度
			if(settings.height != ""){
				jQuery(this).css("height",settings.height);
			}
		
			//設定 overflow:hidden
			jQuery(this).css("overflow","hidden");
			
			//滑動區塊
			jQuery(this).mousemove(function(e){
				sh_autoScroll(e,this);
			});
			
			//鎖定區塊
			jQuery(this).click(function(){
				sh_lockScroll(this);
			});
		});
	};
})();

//設定是否鎖定操作頁.
function sh_lockScroll(ob){
	switch(ob.getAttribute("sh_lock")){
		case null: ob.setAttribute("sh_lock","on")  
		break
		case "on": ob.setAttribute("sh_lock","off")
		break
		case "off": ob.setAttribute("sh_lock","on")
		break
	}
}

//設於 onmouseover(event,this)中, 以做為操作頁上下滑動用.
function sh_autoScroll(e,ob){
	var sh_lastClientYValue = 0;
	
	if(ob.getAttribute("sh_lock") != "on"){
		if(ob.getAttribute("sh_lastClientYValue") == null){
			ob.setAttribute("sh_lastClientYValue",sh_lastClientYValue);
		}else{
			sh_lastClientYValue = ob.getAttribute("sh_lastClientYValue");
		}
		
		var tempScrollTop = eval(ob.scrollTop);
		var jumpValue = eval(ob.scrollHeight/ob.offsetHeight);
		var jumpElements = Math.abs(eval(sh_lastClientYValue - e.clientY));

		if(jumpElements >= 5){
			if(e.clientY < sh_lastClientYValue){
				if(tempScrollTop - jumpValue * jumpElements >= 0){
					ob.scrollTop = tempScrollTop - (jumpValue * jumpElements);
				}else{
					ob.scrollTop = 0;
				}
			}else{
				if(tempScrollTop + jumpValue * jumpElements <= ob.scrollHeight){
					ob.scrollTop = tempScrollTop + (jumpValue * jumpElements);
				}else{
					ob.scrollTop = ob.scrollHeight;
				}
			}
			//sh_lastClientYValue = e.clientY;
			ob.setAttribute("sh_lastClientYValue",e.clientY);
		}
	}
}