var loading_img = "<img src='/admin/img/loading.gif' align='absbottom'/> ";

$(function()
{
	
	if(SystemMessage != "")
	{
		var options = {title: "系統訊息",draggable:true,modal:true};
		var dialog = new Boxy(boxy_content(SystemMessage), options);
	}
	
	$('div[@id=data_row]').each(function()
	{
		$(this).find('a[@rel*=lightbox]').lightBox();
	});
	
	$('div[@id=photos] a[@rel*=lightbox]').lightBox();
	
	var wait_input_email = " 訂閱/取消E-Mail";
	$("#epaper_email").toggleClass("wait_input");
	$("#epaper_email").attr("value",wait_input_email);
	$("#epaper_email").blur(function()
	{
		if($(this).attr("value") == "")
		{
			$(this).toggleClass("wait_input");
			$(this).attr("value",wait_input_email)
		}
	});
	
	$("#epaper_email").focus(function()
	{
		
		if($(this).attr("value") == wait_input_email)
		{
			$(this).toggleClass("wait_input");
			$(this).attr("value","");
		}
	});
	
	$("#form_epaper").submit(function()
	{
		var mail = $("#epaper_email").attr("value");
		var regexp=/^[a-zA-Z0-9_]{2,25}(.*)[@][a-zA-z0-9-]{1,100}[.]([a-zA-z0-9-]{2,10}|[a-zA-z0-9-]{2,10}[.][a-zA-z0-9-]{1,10})$/;
		var Result = regexp.test(mail);
		if(mail == "" || mail == wait_input_email || !Result)
		{
			var options = {title: "輸入錯誤",modal:true};
			var dialog = new Boxy(boxy_content("尚未輸入E-Mail或E-Mail格式錯誤"), options);
			return false;
		}
	});
	
	actionCount = $(actionSelector).length;
	action3Count = $(action3Selector).length;

	if(actionCount !=0)
	{
		cntActionIdx = 0;
		$(actionSelector + ":eq("+ cntActionIdx + ")").show();
	}
	
	if(action3Count !=0)
	{
		cntAction3Idx = 0;
		var src = $(action3Selector + ":eq("+ cntAction3Idx + ")").attr("src");
		$("#actiontd").css("background-image","url("+src+")");
	}
	
	setTimeout("ShowAction();",actionInterval);
	setTimeout("ShowAction3();",action3Interval);
	
	$("[@id=content] img").each(function()
	{
		$(this).load(function()
		{
			var width = $(this).width();
			var height = $(this).height()
			limitWidth = 570;
			limitHeight = 100000;
			
			if(width <= limitWidth && height <= limitHeight)return;
			
			var rsWidth = width;
			var rsHeight = height;
			var Scale = 1.0;
			
			if(width > limitWidth)
			{
				Scale = limitWidth / (width + 0.0);
				rsWidth = width *  Scale;
				rsHeight = height *  Scale;
			}
			
			if (rsHeight > limitHeight)
			{
				Scale = limitHeight / (height + 0.0);
				rsWidth = width *  Scale;
				rsHeight = height *  Scale;
			}
			
			$(this).width(rsWidth);
			$(this).height(rsHeight);
		});
			
	});
	
	
	$("[@id=left_action_list] img").each(function()
	{
		var width = $(this).width();
		var height = $(this).height()
		limitWidth = 156;
		limitHeight = 100000;
		
		if(width <= limitWidth && height <= limitHeight)return;
		
		var rsWidth = width;
		var rsHeight = height;
		var Scale = 1.0;
		
		if(width > limitWidth)
		{
			Scale = limitWidth / (width + 0.0);
			rsWidth = width *  Scale;
			rsHeight = height *  Scale;
		}
		
		if (rsHeight > limitHeight)
		{
			Scale = limitHeight / (height + 0.0);
			rsWidth = width *  Scale;
			rsHeight = height *  Scale;
		}
		
		$(this).width(rsWidth);
		$(this).height(rsHeight);
	});
	
});

var cntActionIdx = -1;
var actionCount = 0;
var actionInterval = 6000;
var actionSelector = "[@id=action_block] a";

var cntAction3Idx = -1;
var action3Count = 0;
var action3Interval = 7000;
var action3Selector = "#actiontd img[@id=action_img]";

function img_resize(imgObj,MaxWidth,MaxHeight)
{
	var width = $(imgObj).width();
	var height = $(imgObj).height()
	limitWidth = MaxWidth;
	limitHeight = MaxHeight;
	
	if(width <= limitWidth && height <= limitHeight)return;
	
	var rsWidth = width;
	var rsHeight = height;
	var Scale = 1.0;
	
	if(width > limitWidth)
	{
		Scale = limitWidth / (width + 0.0);
		rsWidth = width *  Scale;
		rsHeight = height *  Scale;
	}
	
	if (rsHeight > limitHeight)
	{
		Scale = limitHeight / (height + 0.0);
		rsWidth = width *  Scale;
		rsHeight = height *  Scale;
	}
	
	$(imgObj).width(rsWidth);
	$(imgObj).height(rsHeight);
}

function ShowAction()
{
	//if(actionCount < 2) return;
	if(cntActionIdx != -1)
	{
		$(actionSelector + ":eq("+ cntActionIdx + ")").fadeOut("slow",function()
		{
			if(cntActionIdx +1 < actionCount )
			{
				cntActionIdx++;
			}
			else
			{
				cntActionIdx = 0;
			}
			
			$(actionSelector + ":eq("+ cntActionIdx + ")").show("slow",function()
			{
				setTimeout("ShowAction();",actionInterval);
			});
			
		});
	}
}

function ShowAction3()
{
	//if(actionCount < 2) return;
	if(cntAction3Idx != -1)
	{
		$("#actiontd").fadeOut("slow",function()
		{
			if(cntAction3Idx +1 < action3Count )
			{
				cntAction3Idx++;
			}
			else
			{
				cntAction3Idx = 0;
			}
			var src = $(action3Selector + ":eq("+ cntAction3Idx + ")").attr("src");
			$("#actiontd").css("background-image","url("+src+")");
			$("#actiontd").show("slow",function()
			{
				
				setTimeout("ShowAction3();",action3Interval);
			});
			
		});
	}
}

function ShowAction6()
{
	//if(actionCount < 2) return;
	if(cntAction3Idx != -1)
	{
		if(cntAction3Idx +1 < action3Count )
		{
			cntAction3Idx++;
		}
		else
		{
			cntAction3Idx = 0;
		}
		
		var src = $(action3Selector + ":eq("+ cntAction3Idx + ")").attr("src");
		$("#actiontd").attr("background",src);
		
		setTimeout("ShowAction3();",action3Interval);
	}
}
	
function ShowSubmitMsg()
{
	var options = {title: "系統處理中，請等候",modal:true};
	var dialog = new Boxy(boxy_content(loading_img+"資料送出中，請稍候..."), options);
}

function boxy_content(strContent)
{
	return "<div class='boxy_content'>" +  strContent + "</div>";
}

function boxy_closebtn()
{
	return "<div><a href='#' onclick='Boxy.get(this).hide(); return false'>關閉</a></div>";
}