﻿/// <reference path="../AppLibs.js" />
/// <reference path="../AppPlugins.js" />
/// <reference path="../AppManager.js" />

(function (window)
{
	//================================================================================================== Content Begin
	var App = window.App = {};

MrR.Extensions(App,
{
	initFlash: function ()
	{
		var DivNavi = document.getElementById('AreaLogo'), DivCountTotal = document.getElementById('ArgCT');
		if(DivNavi)
		{
			var ArgCT = DivCountTotal.innerHTML;
			MrR.RunActionFor('swfobject', 100, 100, '找不到swfobject', function ()
			{
				swfobject.embedSWF("/Flashs/Navigate.swf", "AreaLogo", "100%", "100%", "9", "/Flashs/expressInstall.swf", { CT: ArgCT }, { menu: 'false', wmode: 'transparent', scale: 'NoScale', salign: 'tl' }, {});
			});
		}
	},
	initSitePlugin: function ()
	{
		jQuery("#AreaPlurk iframe:first").attr("src", "http://www.plurk.com/getWidget?uid=4094968&amp;h=280&amp;w=200&amp;u_info=2&amp;bg=333333&tl=222222");
	},
	initFloatTooltip: function ()
	{
		//=================================================================== ToolTip前置作業
		var xOffset = 20, yOffset = -60;
		jQuery("*[alt], *[title]").each(function ()
		{
			var element = jQuery(this), tooltip = null;
			if (this.alt == undefined && this.title == undefined) { return; } //兩種都為空就返回

			//設定值, 並且取消原本設定, 改由下面jquery設定
			if (this.alt != undefined && this.alt.length >= 1) { tooltip = this.alt; jQuery(this).removeAttr("alt"); }
			else if (this.title != undefined && this.title.length >= 1) { tooltip = this.title; jQuery(this).removeAttr("title"); }

			if (tooltip == undefined || tooltip.length <= 0) { return; } //如果沒有值就返回

			var txt = tooltip.replace(/\"/g, "'"); //取代單引號			
			element.attr("ToolTip", txt).wrap('<span class="ToolTipBox"></span>');
		});

		//=================================================================== ToolTip顯示處理
		jQuery("*[ToolTip]").hover
		(
			function (e)
			{
				var txt = jQuery(this).attr("ToolTip"), span = jQuery(this).closest(".ToolTipBox");

				var toolTip = span.append('<p class="ToolTip">' + txt + '</p>').find("p[class='ToolTip']");
				var toolTipX = (e.pageX + xOffset) - (toolTip.width() / 2 + 25);
				var toolTipY = (e.pageY + yOffset) - (toolTip.height());

				toolTip.css("left", toolTipX).css("top", toolTipY).show();
			},
			function () { jQuery(this).closest(".ToolTipBox").find("p[class='ToolTip']").remove(); }
		)
		.mousemove(function (e)
		{
			var toolTip = jQuery(this).closest(".ToolTipBox").find("p[class='ToolTip']");
			var toolTipX = (e.pageX + xOffset) - (toolTip.width() / 2 + 25);
			var toolTipY = (e.pageY + yOffset) + 20;
			toolTip.css("left", toolTipX).css("top", toolTipY);
			//window.status = "點擊圖片可以看完整圖片 - " + this.TipTitle;
		});
	},
	initControls: function ()
	{

		var defaultText = '請輸入您要搜尋的關鍵字..';
		jQuery("#SearchBox input:text").val(defaultText)
		.focus(function ()
		{
			$(this).addClass('hover');
			if ($(this).val() == defaultText) { $(this).val(''); }
		})
		.blur(function()
		{
			var curValue = $(this).val();
			if ( curValue.length <= 0 ) { $(this).val(defaultText); $(this).removeClass('hover'); }
		});

		jQuery("#SearchBox input:button").click(function()
		{
			var key = jQuery("#SearchBox input:text").val();

			if( !key || key == defaultText || key.length < 1 )
			{
				alert(defaultText); return;
			}

//			jQuery("#SearchBox input:hidden").fancybox(
//			{
//				'type':'iframe', 'width': 680, 'height': 380,
//				'href': '/Search/' + key
//			}).click();
		});
		//======================================================== Menu
		jQuery("#TreeView").dynatree(
		{
			debugLevel: 1,
			clickFolderMode: 2,
			selectMode: 1, // 1:single, 2:multi, 3:multi-hier
			persist: true, 	//記錄在Cookie中
			activeVisible: true, // Make sure, active nodes are visible (expanded).
			fx: { height: "toggle", duration: 200 },
			onActivate: function (dtnode)
			{
				var isInitializing = dtnode.tree.isInitializing(); // Tree loading phase
				var isReloading = dtnode.tree.isReloading(); // Loading phase, and reading status from cookies
				var isUserEvent = dtnode.tree.isUserEvent(); // Event was triggered by mouse or keyboard

				if( isUserEvent && dtnode.data.URL )
				{
					location.href = dtnode.data.URL;
				}
				//MrR.Cookie.Set( 'CurrentSelected' , cookieNIDs, 24);
				//if (dtnode.data.URL) location.href = dtnode.data.URL;
			},
			onPostInit: function(isReloading, isError) {
				// 'this' is the current tree
				// isReloading is true, if status was read from existing cookies
				// isError is only used in Ajay mode
				// Fire an onActivate() event for the currently active node
				this.reactivate();
			},
			generateIds: true, // Generate id attributes like <span id='dynatree-id-KEY'>
			idPrefix: "dynatree-id-", // Used to generate node id's like <span id="dynatree-id-<key>">.
			keyPathSeparator: "/", // Used by node.getKeyPath() and tree.loadKeyPath().
			cookieId: "AreaCategory", // Choose a more unique name, to allow multiple trees.
			cookie: {
				expires: null // Days or Date; null: session cookie
				,path:"/"
			}
		});

		//jQuery("#Logo-MVP").css("cursor", "pointer").click(function () { window.open('https://mvp.support.microsoft.com/default.aspx/profile=bd9fb79b-54eb-4d3a-9f44-fd34cf16c43c'); });

	},
	initGoogleAnalyze: function()
	{
		var _gaq = window._gaq = ( window._gaq || [] );

		_gaq.push(['_setAccount', 'UA-17591198-1']);
		_gaq.push(['_setDomainName', '.Blog']);
		_gaq.push(['_trackPageview']);

		var ga = MrR.UI.CreateTag('script');
		ga.type = 'text/javascript';
		ga.async = true;
		ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';

		MrR.UI.AttachTagToHead( ga );
	}

});



	//MrR.UI.AttachStyle('/Styles/dynatree/054.css');
	//MrR.UI.AttachStyle('/Styles/FancyBox/131.css');
	MrR.UI.AttachStyle('/Styles/syntaxhighlighter/shCore2.css');
	
	//MrR.UI.AttachStyle('/Styles/syntaxhighlighter/shCore.css');
	//MrR.UI.AttachStyle('/Styles/syntaxhighlighter/shThemeRazgrizLeeNet.css');
	//MrR.UI.AttachStyle('/Styles/shCore3.css');
	//MrR.UI.AttachScript('/Scripts/Fancybox/fancybox.js');
	MrR.RunActionFor( 'SyntaxHighlighter', 300, 100, true, function ()
	{
		SyntaxHighlighter.all();
	});

	MrR.Event.AttachToWindowLoad(function ()
	{
		App.initFlash();
		//App.initGoogleAnalyze();
		jQuery(document).ready(function ()
		{
			App.initSitePlugin();
			App.initControls();
			App.initFloatTooltip();
		});
	});

	//================================================================================================== Content End
})(window);


