if(typeof TVB == "undefined" || !TVB) var TVB = {};

if(typeof TVB.String === "undefined" || !TVB.String) TVB.String = {};

if(typeof TVB.String.trim === "undefined" || !TVB.String.trim)
{
	TVB.String.trim = function (str)
	{
		return str.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
	}
}

if(!TVB.GlobalNavi)
{
	TVB.GlobalNavi = {
		language: "big5",
		empty_search_phase : {
				big5 : "請輸入關鍵字", gb : "请输入关键字", eng : "Please insert a search phase"
		},
		search_option : {
				"1": "tvb.com",  "2": "藝人Blog", "3": "討論區", "0": "全球網頁"
		},
		search_submit : function ()
		{
			var v = TVB.String.trim($("#gschtext").val());			
			if(v == "")
			{
				$("#gschtext").focus();
				alert(TVB.GlobalNavi.empty_search_phase[TVB.GlobalNavi.language]);
				return false;
			}
			else
				return true;
		},
		change_search_domain : function (id, key)
		{
			$("#gschdomain").html(key);
			$("#gsch-ss").val(id);
		},
		search_menu_position : function ()
		{
			var pos = $("#ghd-body > .gsch").position();
			var css = {
				"left" : (pos.left)+"px",
				"top" : "19px"
			}
			$("#gsch-menu").css(css);
		},
		main_menu_position : function ()
		{
			var menu_obj = $("#ghd-menu");
			var memu_items = $("#ghd-menu > ul > li");
			var menu_bar_width = $("#ghd-body > .gnav").width();
			
			menu_obj.css("width", (menu_bar_width-8)+"px");
			memu_items.css("width", (menu_bar_width-8)+"px");
		}
	};
}

(function($){
	$.fn.tvb_gtopnavi = function (opts) {
		var container = $(this);
		var default_opts = {
			menu: null,
			language: TVB.GlobalNavi.language
		};
		
		var option = $.extend(default_opts, opts);
		
		TVB.GlobalNavi.language = option.language;
		
		function init ()
		{
			$("#ghd-menu").tvb_gddmenu({btn:$("#ghd-body > .gnav > .gddbtn"), show_listener:TVB.GlobalNavi.main_menu_position});
			$("#gsch-menu").tvb_gddmenu({btn:$("#gschddbtn"), show_listener:TVB.GlobalNavi.search_menu_position});
			$(window).bind('resize.tvb_globalnavi', window_resize_listener);
			window_resize_listener();
			
			$("#gschtext").blur(search_textbox_blur);
			$("#gschtext").focus(search_textbox_focus);
		}
		
		function search_textbox_focus ()
		{
			$(this).css("background-position", "-250px");
		}
		
		function search_textbox_blur ()
		{
			var v = TVB.String.trim($(this).val());
			$(this).val(v);
			if(v == "")
				$(this).css("background-position", "80px -1px");
		}
		
		function window_resize_listener ()
		{
			var w = $(window).width();
			if(w < 975)
				w = 975;
			w = w-(container.children(".gnav").width())-(container.children(".gsch").width())-(container.children(".gmem").width())-20;
			container.children(".gbc").width(w);
		}
		
		init();
				
		return (this);
	}
	$.fn.tvb_gddmenu = function (opts) {
		var container = $(this);
		var default_opts = {
			left: 0,
			top: 20,
			btn: null,
			status: false
		};
		
		var option = $.extend(default_opts, opts);
		
		function check ()
		{
			if(container == null)
				return false;
			
			if(option.btn == null)
				return false;
			
			return true;
		}
		
		function init ()
		{
			$(option.btn).hover(
				btn_mouseover, btn_mouseout
			);
			container.hover (
				menu_mouseover, menu_mouseout
			)
			$(option.btn).click(btn_click);
			
			$(document.body).click(body_click);
			
			container.children("ul").children("li").hover(
				menu_item_mouseover, menu_item_mouseout
			)
			
			$(window).bind('resize.tvb_header_menu', window_resize);
		}
		
		function window_resize ()
		{
			hide();
		}
		
		function show ()
		{
			container.slideDown(200);
			$(option.btn).css("background-position", "0 -24px");
			if(typeof option.show_listener == "object" || typeof option.show_listener == "function")
				option.show_listener();
			option.status = true;
		}
		
		function hide ()
		{
			container.slideUp(200);
			$(option.btn).css("background-position", "0 0");
			option.status = false;
		}
		
		function body_click (e)
		{
			if(e.target.className != "gddbtn")
				hide();
		}
		
		function btn_click ()
		{
			if(!option.status)
				show();
			else
				hide();
		}
		
		function btn_mouseover ()
		{
			if(!option.status)
				$(option.btn).css("background-position", "0 -12px");
		}
		
		function btn_mouseout ()
		{
			if(!option.status)
				$(option.btn).css("background-position", "0 0");
		}
		
		function menu_mouseover ()
		{}
		
		function menu_mouseout ()
		{ 
			hide();
		}
		
		function menu_item_mouseover ()
		{
			$(this).css("background-color", "#F5F5F5");
			//$(this).children("p").css("text-decoration", "underline");
		}
		
		function menu_item_mouseout ()
		{
			$(this).css("background-color", "#FFF");
			//$(this).children("p").css("text-decoration", "none");
		}
		
		if(check())
			init();
			
		return (this);
	}
})(jQuery);

$("#ghd-body").ready(function () {
	$("#ghd-body").tvb_gtopnavi();
});

function clearBlank(what)
{
	if (what.value == '請輸入關鍵字')
	{
		what.value = '請輸入關鍵字';
	}
}

function fillBlank(what)
{
	if (what.value == '請輸入關鍵字')
	{
		what.value = '請輸入關鍵字';
	}
}

function validate(what)
{
	if (what.q.value == '請輸入關鍵字')
	{
		alert('請輸入關鍵字');
		return false;
	}
}

function prefillQ(what)
{
	if (what && what != "") {
		if($("#gschtext"))
		{
			$("#gschtext").val(what);
			$("#gschtext").css("background-position", "-250px");
		}
		else if($("#global_topnav_search_text"))
		{
			$("#global_topnav_search_text").val(what);
		}
	}
}

function preselectSS(what)
{
	try{
		/*if (what == 1) document.getElementById("selSS").selectedIndex = 0;
		else if (what == 2) document.getElementById("selSS").selectedIndex = 1;
		else if (what == 3) document.getElementById("selSS").selectedIndex = 2;
		else document.getElementById("selSS").selectedIndex = 3;*/
		TVB.GlobalNavi.change_search_domain(what, TVB.GlobalNavi.search_option[what]);
	}catch(e){}
}
    
function selectChange (obj)
{
	var val = 0;
	$("#selSS").attr("value", obj[obj.selectedIndex].value);
}
function fbConnectHandler() {
	FB.ensureInit(function() {
		FB.Connect.requireSession(
				function(){					
					parent.location.href = 'http://member.tvb.com/fb_login.php?destURL=' + "$url";
				},
				function(){}
		);
	});
}
/*$(document).ready(function () {
	$.tvblogin();	
});*/

TVB.GlobalNavi.getMembershipStatus = function (p_obj, p_lang) {
        var url = "http://member.tvb.com/api/header.php";
        var obj = p_obj;
        var login_link = "https://member.tvb.com/login.php?";
        var logout_link = "https://member.tvb.com/check.php?lang=zh&action=logout";
        var register_link = "https://member.tvb.com/register.php?";

        var lang = "zh";
        if(p_lang != null && p_lang != "")
                lang = p_lang;

        var translate = [];
        translate["zh"] = {"hello_title" : "您好", "logout_title" : "登出", "login_title" : "登入", "register_title" : "註冊"};
        translate["gb"] = {"hello_title" : "您好", "logout_title" : "登出", "login_title" : "登入", "register_title" : "注册"};
        translate["en"] = {"hello_title" : "Hello", "logout_title" : "Logout", "login_title" : "Login", "register_title" : "Register"};

        $.getJSON(url+"?callback=?", function (data) {
                var status = parseInt(data.status);
                
                if (typeof data.is_fb_need_logout != "undefined" && parseInt(data.is_fb_need_logout)==1 && status == 1) {
                                       FB.ensureInit( function () {
                                                FB.Connect.ifUserConnected( function () {
                                                        FB.Connect.logout(function () { window.location.reload(); });
                                                },function () {});
                                        });
                }
                
                var html = '<p class="ghd-login-status">';

                if(typeof data.fb_pic != "undefined" && status == 1) {
                        html += '<span class="fb-head"><img src="http://img.tvb.com/membership/img/fb_m_icon_18.gif" /><img src="'+data.fb_pic+'" width="18" height="18" /></span>';
                }
                else {
                        html += '<a href="javascript:void(0);" onclick="$.tvblogin.facebookLogin();" class="fb-g-login" id="fb-g-login"></a>';
                }
                if(status == 1) {
                        if(data.display_name.length > 6)
                                data.display_name = data.display_name.substr(0, 6)+"...";
                        html += '<a href="http://member.tvb.com/month_letter.php?lang=zh" style="padding-right:10px;">'+translate[lang]["hello_title"]+'&nbsp;!&nbsp;'+data.display_name+'</a><span style="font-size: 0px;">&nbsp;</span><a href="'+logout_link+'">'+translate[lang]["logout_title"]+'</a>';
                }
                else if (status == 0) {
                        html += '<a class="not-login" href="'+login_link+'&destURL='+encodeURI(document.URL)+'" style="padding-right:10px;">'+translate[lang]["login_title"]+'</a><span style="font-size: 0px;">&nbsp;</span><a href="'+register_link+'">'+translate[lang]["register_title"]+'</a>';
                }
                html += '</p>';
                obj.html(html);
                //$.tvblogin();
		if(typeof $.tvblogin != "undefined" && typeof $.tvblogin.print_login != "undefined")
		{
			$(".not-login").bind("click", function () { $.tvblogin.print_login(); return false;});
		}
		//	$.tvblogin.print_login();
        });
}
if (typeof TVB === "undefined" || !TVB)  var TVB = {};
    
TVB.adsys = {
	ad_inventory : [],
	player: null,
	page_key : "",
	product : null,
	config : {bgChangeMode: "css", cleanArtisteBackground: false},
	small_container : "#small-body-bg",
	big_container : "#big-body-bg",
	_init : function () {
		//this.page_key = Math.random()*1000;
		var d = new Date();
		this.page_key = d.getTime();
	},
	appendDiv : function (obj, container)
	{
		if(obj == null || container == null)
			return false;
		container.css("position", "relative");
		var html = '<div id="'+obj.id+'"></div>';
		if(typeof obj.link != "undefined" && obj.link != null && obj.link != "")
		{
			html = '<a href="'+obj.link+'" target="_blank">'+html+'</a>';
		}
		container.append(html);
		
		var adObj = $("#"+obj.id);
		adObj.css("position", "absolute");
		if(typeof obj.style == 'object')
			adObj.css(obj.style);	
		
		
		if(typeof obj.style.top != "undefined" && !(typeof this.config.paddingTop == "undefined" || this.config.paddingTop == null || this.config.paddingTop < 0))
		{
			var top = obj.style.top;
			switch(this.product)
			{
				case "event":
				case "artiste":
					var top = parseInt(obj.style.top)-parseInt(this.config.paddingTop);
					break;
				case "pp":
					var top = parseInt(obj.style.top)-parseInt(this.config.paddingTop)-parseInt($("#key_art").height());
					break;
			}
			adObj.css("top", top+"px");			
		}
	},
	addSmallBodyClickableArea : function () {
		if(typeof this.config.smallBodyClickableArea == "undefined" || this.config.smallBodyClickableArea == null || typeof this.config.smallBodyClickableArea != "object")
			return false;	
		
		var container = $(this.small_container);

		var html = "";
		for(var i=0; i < this.config.smallBodyClickableArea.length; i++)
		{
			if(typeof this.config.smallBodyClickableArea[i].id == "undefined" || this.config.smallBodyClickableArea[i].id == "")
				continue;
			
			this.appendDiv(this.config.smallBodyClickableArea[i], container);
		}
	},
	cleanBackgroundImage : function ()
	{
		switch(this.product)
		{
			case "artiste":
			case "artist":
				$(this.small_container).css("background-image", "none");
				break;
		}
	},
	setArtisteBackgroundImage : function ()
	{
		if(typeof this.config.artisteBackground == "undefined" || this.config.artisteBackground == null)
			return false;
		
		var o = $("#artiste_header");
		
		switch(this.config.bgChangeMode)
		{
			case "css":
				$(document.body).append("<style>"+o.selector+" {background-image: url("+this.config.artisteBackground+")!important; }</style>");
				break;
			case "js":
				o.css("background-image", this.config.artisteBackground+"!important");
				break;
		}

		return true;	
	},
	setBackground : function (obj)
	{
		if(typeof this.config.background == "undefined" || this.config.background == null)
			return false;
		
		var o = $(this.big_container);
		
		if(typeof obj != "undefined" && obj != null)
			o = obj;
	
		switch(this.config.bgChangeMode)
		{
			case "css":
				$(document.body).append("<style>"+o.selector+" {background: "+this.config.background+"!important; }</style>");
				break;
			case "js":
				o.css("background", this.config.background+"!important");
				break;
		}

		return true;		
	},
	setBackgroundImage : function (obj)
	{
		if(typeof this.config.backgroundImage == "undefined" || this.config.backgroundImage == null)
			return false;
		
		var o = $(this.big_container);
		
		if(typeof obj != "undefined" && obj != null)
			o = obj;
	
		switch(this.config.bgChangeMode)
		{
			case "css":
				$(document.body).append("<style>"+o.selector+" {background-image: url("+this.config.backgroundImage+")!important; }</style>");
				break;
			case "js":
				o.css("background-image", this.config.backgroundImage+"!important");
				break;
		}

		return true;			
	},
	setPadding : function (obj)
	{
		if(typeof this.config.paddingTop == "undefined" || this.config.paddingTop == null || this.config.paddingTop < 0)
			return false;
			
		var o = $(this.small_container);
		
		if(typeof obj != "undefined" && obj != null)
			o = obj;
		
		switch(this.product)
		{
			case "event":
			case "artiste":
			case "pp":
				o = $(this.big_container);
				break;
		}
		
		o.css("padding-top", this.config.paddingTop+"px");
		return true;		
	},
	setConfig : function (config)
	{
		if(typeof config == "undefined" || !config) 
			return false;	
			
		$.extend(this.config, config);

		return true;
	},
	getProduct : function ()
	{
		var host = $.url.attr("host");
		this.product = host.replace(".tvb.com", "");
	},
	setMinHeight : function (obj)
	{
		if(typeof this.config.minHeight == "undefined" || this.config.minHeight == null || this.config.minHeight < 0)
			return false;	
		
		var mh = parseInt(this.config.minHeight);
		
		var o = $("#big-body-bg");
		var s = $("#small-body-bg");
		
		if(typeof obj != "undefined" && obj != null)
			o = obj;
			
		var pTop = parseInt(o.css("padding-top").replace("px",""))+parseInt(s.css("padding-top").replace("px",""));
		
		if(isNaN(pTop))
			pTop = 0;

		if(o.height() < (mh-pTop))
			o.height(mh-pTop);

		return true;		
	},
	drawAd : function (config, product)
	{
		if(!this.setConfig(config))
			return false;
			
		if(typeof product !== "undefined" && product != null && product != "")
		{
			this.product = product;
		}
		else
			this.getProduct();
			
		this.setPadding();
		this.setMinHeight();
		
		if(this.config.artisteBackground)
		{
			this.setArtisteBackgroundImage();
		}
		this.setBackgroundImage();
		this.setBackground();
		this.addSmallBodyClickableArea();
	},
	/*
	Ad API
	*/
	/*
	embedCSS : function (css) {
		document.write('<style>'+css+'</style>');
	},
	embedJS : function (js) {
		document.write('<script>'+js+'</script>');
	},
	getPlayer : function () {
		var ele = $("#tv_player");
		if(ele !== null)
		{
			this.player = ele;
			return this.player;
		}
		else
			return false;
	},
	changeBigBackgroundImage : function (css) {
		var ele = $("#big-body-bg");
		if(ele && css != "")
		{
			ele.css("background-image", css);
		}		
	},
	changeBigBackground : function (css) {
		var ele = $("#big-body-bg");
		if(ele && css != "")
		{
			ele.css("background", css);
		}
	},
	changeSmallBackgroundImage : function (css) {
		var ele = $("#small-body-bg");
		if(ele && css != "")
		{
			ele.css("background-image", css);
		}
	},	
	changeSmallBackground : function (css) {
		var ele = $("#small-body-bg");
		if(ele && css != "")
		{
			ele.css("background", css);
		}
	},
	registerAd : function (ad) {
		if(ad !== null)
			this.ad_inventory[] = ad;
	},
	searchPageAd : function ()
	{
		var ele = $("script[src*='doubleclick']");
		
		ele.each(function (e) {
			this.registerAd(e);
		});
	},
	getAdByTags : function (tags) {
		var ele = $("script[src*='"+tags+"']");
		return ele;
	},
	reloadAdByTags : function (tags) {
		var ele = this.getAdByTags(tags);
		
		ele.each(function (e) {
			e.attr("src", e.attr("src"));
		});
	},*/
	/*
	Flash API
	*/
	getPageKey : function ()
	{
		return this.page_key;
	},
	syncPageAd : function (ad_url)
	{
		var ele = this.searchPageAd();
		
		ele.each(function (e) {
			ele.attr("src", ad_url);
		});
	}
};
TVB.adsys._init();
/*
try {
$(".glogo").ready(function () {
	$(".glogo").attr("src", "http://img.tvb.com/homepage/tvbcom_logo_bw.png");

});
$(".gft-crop-info-logo").ready(function () {
	$(".gft-crop-info-logo").css("background-image", "url(http://img.tvb.com/homepage/footer_tvb_logo_white_bw.gif)");
});
} catch (e) { }
*/
