// JavaScript Document
if (jQuery.browser.msie) {
	if (parseInt(jQuery.browser.version) == 6) {
		try {
			document.execCommand("BackgroundImageCache", false, true);
		}catch(e){
		}
	}
}
function trim() {
    if (typeof (arguments[0]) != "undefined") {
        var stringToTrim = arguments[0];
        return stringToTrim.replace(/^\s+|\s+$/g, "");
    } else {
        return "";
    }
}
function rehash() {
	var hashstr = arguments[0];
	var pagetitle = document.title;
	if (typeof (hashstr) != "undefined" && typeof (hashstr) != "object") {
		var port = "";
		if (jQuery.url.attr("query") == null) {
			document.location.href = jQuery.url.attr("protocol") + "://" + jQuery.url.attr("host") + jQuery.url.attr("path") + "#" + hashstr;
		} else {
			document.location.href = jQuery.url.attr("protocol") + "://" + jQuery.url.attr("host") + jQuery.url.attr("path") + "?" + (jQuery.url.attr("query") || "") + "#" + hashstr;
		}
	}
	document.title = pagetitle;
} 
function checkhash() {
	var tmp_urlanchor = jQuery.url.attr("anchor");
	var tmp_obj = new Object;
	if (tmp_urlanchor != null) {
		var tmp_hash = tmp_urlanchor.split("&");
		var tmp_b;
		for (var loopi = 0; loopi < tmp_hash.length; loopi++) {
			tmp_b = tmp_hash[loopi].split("=");
			eval("tmp_obj." + tmp_b[0] + "= '" + tmp_b[1] + "';");
		}
	}
	return tmp_obj;
} 
function blockblack() {
	var msg = "";
    if (typeof (arguments[0]) != "undefined") {
        msg = arguments[0];
    }
	var timeout;
	timeout = parseInt(arguments[1]);
	if (isNaN(timeout)) { timeout = 0; }
	if (timeout > 0 && timeout < 3000) {timeout = 3000; }
	$.blockUI({ message: '<h1>' + msg + '</h1>', css: {
		border: 'none',
		padding: '15px',
		backgroundColor: '#000',
		'-webkit-border-radius': '10px',
		'-moz-border-radius': '10px',
		opacity: '.5',
		color: '#fff'
		}
	});
	if (timeout > 0) {
		setTimeout($.unblockUI, timeout);
	}
}
function blocklight() {
	var msg = "";
    if (typeof (arguments[0]) != "undefined") {
        msg = arguments[0];
    }
	var timeout;
	timeout = parseInt(arguments[1]);
	if (isNaN(timeout)) { timeout = 0; }
	if (timeout > 0 && timeout < 3000) { timeout = 3000; }
	$.blockUI({ message: '<h1><span style="float: left; margin-right: 0.3em;" class="ui-icon ui-icon-info"/><b style="color:blue;">Message:</b><br>' + msg + '</h1>', css: {
		border: '1px solid #FAD42E',
		color: '#363636',
		padding: '7px',
		background: '#FBEC88',
		'-webkit-border-radius': '5px',
		'-moz-border-radius': '5px',
		'text-align': 'left'
		}
	});
	if (timeout > 0) {
		setTimeout($.unblockUI, timeout);
	}
}
function blockerr() {
	var msg = "";
    if (typeof (arguments[0]) != "undefined") {
        msg = arguments[0];
    }
	var timeout;
	timeout = parseInt(arguments[1]);
	if (isNaN(timeout)) { timeout = 0; }
	if (timeout > 0 && timeout < 3000) { timeout = 3000; }
	$.blockUI({ message: '<h1><span style="float: left; margin-right: 0.3em;" class="ui-icon ui-icon-alert"/><b style="color:red;">Error:</b><br>' + msg + '</h1>', css: {
		border: '1px solid #CD0A0A',
		color: '#363636',
		padding: '7px',
		background: '#FEF1EC',
		'-webkit-border-radius': '5px',
		'-moz-border-radius': '5px',
		'text-align': 'left'
		}
	});
	if (timeout > 0) {
		setTimeout($.unblockUI, timeout);
	}
}
function blockcheck() {
	var msg = "";
    if (typeof (arguments[0]) != "undefined") {
        msg = arguments[0];
    }
	var js = function(){};
    if (typeof (arguments[1]) == "function") {
        js = arguments[1];
    }
	var tmphtml = '<div style="width: 600px;" id="blockcheck" class="ui-dialog ui-widget ui-widget-content ui-corner-all">';
	tmphtml += '<div class="ui-dialog-titlebar ui-widget-header ui-corner-all ui-helper-clearfix">';
	tmphtml += '<span class="ui-dialog-title">Message</span>';
	tmphtml += '<a href="#" class="ui-dialog-titlebar-close ui-corner-all blockcheck_close"><span class="ui-icon ui-icon-closethick">close</span></a></div>';
	tmphtml += '<div id="dialog" class="ui-dialog-content ui-widget-content" style="height: auto; min-height: 63px; width: auto;">';
	tmphtml += '<p>' + msg + '</p>';
	tmphtml += '</div>';
	tmphtml += '<div class="ui-dialog-buttonpane ui-widget-content ui-helper-clearfix"><button type="button" class="ui-state-default ui-corner-all blockcheck_do">Ok</button><button type="button" class="ui-state-default ui-corner-all blockcheck_close">Cancel</button></div>';
	tmphtml += '</div>';
	$.blockUI({ message: tmphtml, css: {
		border: 'none',
		color: '#363636',
		padding: '0px',
		background: '#FEF1EC',
		'-webkit-border-radius': '5px',
		'-moz-border-radius': '5px',
		'text-align': 'left'
		}
	});
	$(".blockcheck_close").bind("click", function() { $.unblockUI(); });
	$(".blockcheck_do").bind("click", js);
	$(".blockcheck_close, .blockcheck_do").bind("mouseenter", function() { $(this).addClass("ui-state-hover"); });
	$(".blockcheck_close, .blockcheck_do").bind("mouseleave", function() { $(this).removeClass("ui-state-hover"); });
}
function settip(jqueryobj) {
	if (typeof(jqueryobj.attr("note")) != "undefined") {
		var position = jqueryobj.offset();
		var objLeft = position.left;
		var objTop = position.top;
		var objWidth = jqueryobj.width();
		var tiphtml = '<div class="STARTOOLTIP" style="margin-left:' + objLeft + 'px;width:' + objWidth + 'px;"><div class="TIPTEXT">' + jqueryobj.attr("note") + '</div></div>';
		$("body").append(tiphtml);
		var tipHeight = $(".STARTOOLTIP").height() + 4;
		$(".STARTOOLTIP").css("top", objTop - tipHeight);
	}
}
function checkform() {
	var checkint = 0;
	if ($(".upload_check").length > 0) {
		$(".upload_check").each(function(){
			checkint += parseInt($(this).val());						 
		});
	}
	if (checkint > 0) {
		try {
			parent.blockblack("檔案上傳中，請稍後...");
		} catch (e) {
			blockblack("檔案上傳中，請稍後...");
		}
		if (typeof(recheckform) != "undefined") {
			clearTimeout(recheckform);
		}
		recheckform = setTimeout('checkform()', 2000);
	} else {
		if (typeof(formsend.url) == "undefined") {
			formsend.url = "ajax/action.php";
		}
		if (typeof(formsend.mod) == "undefined") {
			formsend.mod = "POST";
		}
		formsend.mod = formsend.mod.toUpperCase();
		if (formsend.mod != "GET") {
			formsend.mod = "POST";
		}
		if (typeof (formsend.msg) == "undefined") {
			formsend.msg = "請稍後...";
		}
		if (formsend.debug != true) {
			formsend.debug = false;
		}
		var serial = "";
		var json_serial = new Object;
		if (typeof (formsend.basecol) == "object") {
			$.each(formsend.basecol, function(k, v) {
				serial += "&" + v + "=" + encodeURIComponent(trim($("#" + v).val()));
				eval("json_serial." + v + "=encodeURIComponent(trim($('#" + v + "').val()));");
			});
		}
		if (typeof (formsend.multicol) == "object") {
			$.each(formsend.multicol, function(k, v) {
				var tmpv = "";
				if ($("#" + v + " input").length > 0) {
					$("#" + v + " input:checked").each(function() {
						if (typeof ($(this).val()) != "undefined") {		
							if (tmpv.length > 0) { tmpv += ","; }
							tmpv += $(this).val();
						}
					});
				} else if ($("#" + v + " option:selected").length > 0) {
					$("#" + v + " option:selected").each(function() {
						if (typeof ($(this).val()) != "undefined") {		
							if (tmpv.length > 0) { tmpv += ","; }
							tmpv += $(this).val();
						}
					});
				}
				serial += "&" + v + "=" + encodeURIComponent(tmpv);
				eval("json_serial." + v + "=encodeURIComponent(tmpv);");
			});
		}
		if (typeof (formsend.ckeditorcol) == "object") {
			$.each(formsend.ckeditorcol, function(k, v) {
				var tmp_v = eval("CKEDITOR.instances." + v + ".getData()");
				serial += "&" + v + "=" + encodeURIComponent(tmp_v);
				eval("json_serial." + v + "=encodeURIComponent(tmp_v);");
			});
		}
		if ($("#addmorerow").length > 0) {
			serial += "&addmore=" + $("#addmorerow input:checked").val();
			json_serial.addmore = $("#addmorerow input:checked").val();
		}
		serial += "&" + new Date().getTime();
		json_serial.randomval = new Date().getTime();
		if (formsend.debug) {
			alert(serial);
		}
		try {
			parent.blockblack(formsend.msg);
		} catch (e) {
			blockblack(formsend.msg);
		}
		if (formsend.mod == "POST") {
			$.post(formsend.url, json_serial, function() { }, "script");
		} else {
			$.getScript(formsend.url);
		}
	}
}
var SubMenu = new Object;
function showSubMenu(sid) {
	if ($("#RightMain2Col .LeftCol").length > 0) {
		if (sid == "MTV") {
			if ( typeof(SubMenu.MTV) == "undefined") {
				$("#RightMain2Col .LeftCol").load("/block/SubMenu.php?InMTV=1", function () { SubMenu.MTV = $("#RightMain2Col .LeftCol").html(); });
			} else {
				$("#RightMain2Col .LeftCol").html(SubMenu.MTV);
			}
		} else {
			if ( typeof(SubMenu[sid]) == "undefined") {
				$("#RightMain2Col .LeftCol").load("/block/SubMenu.php?CT=" + sid, function () { SubMenu[sid] = $("#RightMain2Col .LeftCol").html(); });
			} else {
				$("#RightMain2Col .LeftCol").html(SubMenu[sid]);
			}
			//$("#RightMain2Col .LeftCol").load("/block/SubMenu.php?CT=" + sid);
		}
	}
}
function AddFavorite() {
	var SetHome = "http://onethe.tw/";
	var webname = "玩樂台灣網路電視台";
	if ($.browser.msie) {
		try {
			if (parseInt(jQuery.browser.version) < 8) {
				window.external.addFavorite(SetHome, webname);
			} else {
				window.external.addToFavoritesBar(SetHome, webname, 'slice');
			}
		}catch (e) {
			blockerr("加入「我的最愛」失敗，請選取工具列中的「我的最愛」進行操作", 3000);
		}
	} else if ($.browser.mozilla) {
		try {
			window.sidebar.addPanel(webname, SetHome, "");	
		}catch (e) {
			blockerr("加入「書籤」失敗，請選取工具列中的「書籤」進行操作", 3000);
		}
	} else {
		blockerr("目前本功能暫不支援您的瀏覽器，請使用IE或是Firefox，或是自行進行操作", 3000);
	}
}
function SetHome() {
	var SetHome = "http://onethe.tw/";
	if ($.browser.msie) {
		try {
			document.body.style.behavior='url(#default#homepage)';  
			document.body.setHomePage(SetHome);  
		}catch (e) {
			blockerr("設為首頁失敗，請選取工具列中的「工具」->[網際網路選項]進行操作", 3000);
		}
	} else if ($.browser.mozilla) {
		try {
			try {
				netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
			}
			catch (e) {
				blockerr("此操作無法被瀏覽器接受！<br>請在瀏覽器網址列輸入about:config並送出<br>然後找到[signed.applets.codebase_principal_support]選項，並將之設定為true");
			}
			var prefs = Components.classes['@mozilla.org/preferences-service;1'].getService(Components.interfaces.nsIPrefBranch);
			prefs.setCharPref('browser.startup.homepage',SetHome); 
		} 
		catch (e) {
			blockerr("設為首頁失敗，請選取工具列中的「工具」->[選項]進行操作", 3000);
		}
	} else {
		blockerr("目前本功能暫不支援您的瀏覽器，請使用IE或是Firefox，或是自行進行操作", 3000);
	}
}
function ShopSearch() {
	var w = encodeURIComponent($("#ShopSearch").val());
	if (w.length > 0) {
		if (jQuery.url.attr("path") == "/SearchChannel.php") {
			ReSearch();	
		} else {
			document.location.href='/SearchChannel.php#ShopSearch=' + w;	
		}
	}
}