/*
 *	http://closure-compiler.appspot.com/home
 */

var url = location.href;
var title = document.title;

$('div[id=infomation_l][class=TopAD]').ready(function() {

	$("#infomation_bigad_text").hide();

	// 每一張小圖划過去
	$('div[class^=infomation_smallad]', this).each(function(i, d) {
		// alert(i);
		$(this).click(function() {
			chAd(this);
		}).hover(function() {
			chAd(this);
		});

		var t = $(this).attr("bimg");
		$(this).prepend("<img src=\"" + t + "\" class=\"beLoad\" />");
		$("img.beLoad").hide();

	});

	// 大圖
	$("div.infomation_bigad").mouseover(function() {

		var t = jQuery.trim($("#infomation_bigad_text").html());

		if (t == '')
			return false;

		$("#infomation_bigad_text").show();

	}).mouseout(function() {
		$("#infomation_bigad_text").hide();

	}).click(function() {
		var l = jQuery.trim($(this).attr("opLinks"));
		if (typeof (l) == "undefined" || l == "") {
			return false;
		}
		window.open(l);
		return true;

	});

});

$(document)
		.ready(
				function() {

					$('#cateType').change(function() {
						if ($('#cateType').val() == "movie") {
							$('#movieType').show();
						} else {
							$('#movieType').hide();
						}
					});
					if ($('#cateType').val() == "movie") {
						$('movieType').show();
					}
					;

					if ($("body.login").html()) {
						function doLogin(o) {
							var v = $('#userid', o);
							if (v.val() == '') {
								alert('請輸入帳號！');
								v.focus();
								return false;
							}
							var v = $('#password', o);
							if (v.val() == '') {
								alert('請輸入密碼');
								v.focus();
								return false;
							}
							var v = $('#authCodeImg', o);
							if (v.val() == '') {
								alert('請輸入驗證碼');
								v.focus();
								return false;
							}
							document.getElementById('loginForm').submit();
						}
						$('#authCode').authCode();
						$('#LoginSubmit').click(function() {
							return doLogin($('#loginForm'));
						}).css({
							"cursor" : "pointer"
						});
						$('#LoginReset').click(function() {
							$('body.login form input').each(function(i, d) {
								$(this).val("");
							});
						}).css({
							"cursor" : "pointer"
						});

						$('input[type=text],input[type=password]',
								$('#loginForm')).each(function() {
							$(this).keydown(function(e) {
								if (e.keyCode == 13) {
									doLogin($('#loginForm'));
								}
							});
						});
					}
					;

					if ($('body.EditPassword').html()) {
						$('#authCode').authCode();

						function MemberEditPassword() {
							// alert('t');
							var v = $("#old_password");
							var v1 = $("#password");
							var v2 = $("#password2");
							if (v.val() == '') {
								alert('請輸入舊密碼');
								v.focus();
								return false;
							}
							if (v1.val() == '') {
								alert('請輸入密碼');
								v1.focus();
								return false;
							}
							if (v2.val() == '') {
								alert('請輸入密碼');
								v2.focus();
								return false;
							}
							if (v1.val() != v2.val()) {
								alert('兩次輸入的密碼不一致!');
								v1.focus();
								v1.val('');
								v2.val('');
								return false;
							}

							var v = $('#authCodeImg');
							if (v.val() == '') {
								alert('請輸入驗證碼');
								v.focus();
								return false;
							}
							document.getElementById('EditPAsswordFormList')
									.submit();
						}

						$('#EditPasswordSubmit').click(function() {
							MemberEditPassword();
						}).css({
							"cursor" : "pointer"
						});
						$('#EditPasswordReset').click(
								function() {
									$('body.EditPassword form input').each(
											function(i, d) {
												$(this).val("");
											});
								}).css({
							"cursor" : "pointer"
						});
						$('#EditPAsswordFormList input[id!=favorites\[\]]')
								.each(function(i, o) {
									var t = $(this).attr('type');
									if (t == "text" || t == "password") {
										$(this).keydown(function(e) {
											if (e.keyCode == 13)
												MemberEditPassword();
										});
									}
								});
					}
					;

					if ($('body.RegisterForm').html()) {
						$('#RegisterFormSubmit').click(function() {
							var v = $('input[type=checkbox][id=rule]');
							if (v.attr('checked') == true) {
								location.href = 'Register.html';
								return false;
							} else {
								alert('請勾選 我同意以上會員條款');
								v.focs();
								return false;
							}
						}).css({
							"cursor" : "pointer"
						});
					}

					if ($('body.Register').html()) {
						$('#authCode').authCode();
						var v = ($('#dpost').val() == ""
								|| $('#dpost').val() == 0 || typeof ($('#dpost')
								.val()) == "undefined") ? 106 : $('#dpost')
								.val();
						var frm = document.getElementById('RegisterFormList');
						callPostCode(frm.City, frm.Canton, frm.postcode, v);

						// alert($('#dpost').val());

						$('#upload').hide();
						$('input[type=text][id^=favorites]').each(
								function(i, o) {
									$(this).suggest("get.start.list.php", {
										onSelect : function() {
										}
									});
								});

						$('#uploadmig').change(
								function() {
									var v = $(this).val();
									$('#preImg').hide();
									if (v == "upload") {
										$('#preImg').hide();
										$('#upload').show();
									} else {
										$('#preImg').html(
												"<img src=\"" + v
														+ "\" width=\"60\">")
												.show();
										$('#upload').hide();
									}
								});

						var v = $('#dby').val();
						$('#by option[value=' + v + ']').attr('selected', true);
						var v = $('#dbm').val();
						$('#bm option[value=' + v + ']').attr('selected', true);
						var v = $('#dbd').val();
						$('#bd option[value=' + v + ']').attr('selected', true);

						$('div[id=favo_type\[\]]').each(
								function(i, d) {
									var v = jQuery.trim($(this).html());
									$('input[type=checkbox][value=' + v + ']')
											.attr('checked', true);
								});
						$('div[id=favo_start\[\]]').each(
								function(i, d) {
									var v = jQuery.trim($(this).html());
									$(
											'select[name=favorites\[\]]:eq('
													+ i + ') option[value=' + v
													+ ']').attr('selected',
											true);
								});

						// alert(frm.postcode.value)

						function RegisterForm() {
							var v = $("#userid");
							if (v.val() == '') {
								alert('請輸入會員帳號');
								v.focus();
								return false;
							}

							if ($('#rgt').val() == "new") {
								// alert('t');
								var v1 = $("#password");
								var v2 = $("#password2");
								if (v1.val() == '') {
									alert('請輸入密碼');
									v1.focus();
									return false;
								}
								if (v2.val() == '') {
									alert('請輸入密碼');
									v2.focus();
									return false;
								}
								if (v1.val() != v2.val()) {
									alert('兩次輸入的密碼不一致!');
									v1.focus();
									v1.val('');
									v2.val('');
									return false;
								}
							}

							var v = $("#nickname");
							if (v.val() == '') {
								alert('請輸入會員暱稱');
								v.focus();
								return false;
							}
							var v = $("#email");
							if (v.val() == '') {
								alert('請輸入E-mail');
								v.focus();
								return false;
							}

							if ($('#rgt').val() == "edit") {

								var v1 = $("#phone1");
								var v2 = $("#phone2");
								if (v1.val() == '') {
									alert('請輸入聯絡電話');
									v1.focus();
									return false;
								}
							}

							var v = $('#authCodeImg');
							if (v.val() == '') {
								alert('請輸入驗證碼');
								v.focus();
								return false;
							}
							document.getElementById('RegisterFormList')
									.submit();
						}

						$('#RegisterSubmit').click(function() {
							RegisterForm();
						}).css({
							"cursor" : "pointer"
						});
						$('#RegisterReset').click(function() {
							$('body.Register form input').each(function(i, d) {
								$(this).val("");
							});
						}).css({
							"cursor" : "pointer"
						});

						$('#RegisterFormList input[id!=favorites\[\]]').each(
								function(i, o) {
									var t = $(this).attr('type');
									if (t == "text" || t == "password") {
										$(this).keydown(function(e) {
											if (e.keyCode == 13)
												RegisterForm();
										});
									}
								});
						$("#userid").focus();
					}

					if ($("body.movie_list").html()) {

						function cTheater(area) {
							$("#movie_theater").html("");
							$('span[id=movie_theater_' + area + '_name]')
									.each(
											function(i, d) {
												var o = document
														.createElement("option");
												// 设置OPTION对象的值
												o.value = $(
														'span[id=movie_theater_'
																+ area
																+ '_url]:eq('
																+ i + ')')
														.html();
												// 设置OPTION对象的内容
												o.innerHTML = t = jQuery
														.trim($(this).html());
												$('#movie_theater').append(o);

												// $("#movie_theater").html($("#movie_theater").html()
												// +
												// "<option value=\"" + u + "\">
												// " + t + "
												// </option>\\r\\n");
											});
						}
						$('span[id=movie_theater_list]').each(
								function(i, d) {
									var t = jQuery.trim($(this).html());
									var v = $("#movie_area").html();
									$("#movie_area").html(
											v + "<option value=\"" + t + "\">"
													+ t + "</option>");
								});
						cTheater($("#movie_area").val());
						$('#movie_area').change(function() {
							var v = $(this).val();
							cTheater(v);
						});

						$('#movie_go').click(
								function() {
									window.open(jQuery.trim($(
											'#movie_theater option:selected')
											.val()));
								});

					}
					;

					if ($('body.magazine_view').html()) {
						$('#jumpMa').change(function() {
							// alert($(this).val());
							location.href = $(this).val();
						});
					}

					if ($('#header .BookmarkLink').html()) {
						$('#header .BookmarkLink').click(function() {
							if ($.browser.webkit || $.browser.safari) {
								alert('您使用的瀏覽器並不支援此語法');
								return false;
							} else {
								CreateBookmarkLink(url, title);
							}
						});
					}

					// setHomepage(url){
					if ($('#header .setHomepage').html()) {
						$('#header .setHomepage').click(function() {
							if ($.browser.webkit || $.browser.safari) {
								alert('您使用的瀏覽器並不支援此語法');
								return false;
							} else {
								setHomepage(url);
							}
						});
					}

					if ($('body.startList').html()) {
						$('#doSearch').click(function() {
							var v = $('#keyword', $("#startSearch")).val();
							document.getElementById('startSearch').submit();
							return false;
							// alert(v);
						});
					}
					if ($('body.goodsList').html()) {
						$('#doSearch').click(function() {
							var v = $('#keyword', $("#goodsSearch")).val();
							document.getElementById('goodsSearch').submit();
							return false;
							// alert(v);
						});
					}
					if ($('body.MyBox').html()) {
						$('#doSearch').click(function() {
							var v = $('#keyword', $("#MyBoxSearch")).val();
							document.getElementById('MyBoxSearch').submit();
							return false;
							// alert(v);
						});
					}
					$('#infomation_ad').ready(
							function() {
								var i = $('#infomation_ad img');
								$(
										'#infomation_ad img:eq('
												+ getRandomInt(0, i.length - 1)
												+ ')').show();
							});

					$('#doSearchForm').click(function() {
						var k = $('#search_keyword');
						if (k.val() == '請輸入關鍵字...' || k.val() == '') {
							alert('請輸入關鍵字');
							return false;
						}
						document.getElementById('SearchForm').submit();
						return false;
					}).css({
						"cursor" : "pointer"
					});

					function MemberNewPassword(o) {
						var v = $('#email', o);
						if (jQuery.trim(v.val()) == '') {
							alert('請輸入Email');
							v.focus();
							return false;
						}
						document.getElementById('MemeberPasswordForm').submit();
					}

					$('#FormSubmit', $('#MemeberPasswordForm')).click(
							function() {
								// alert('FormSubmit');
								MemberNewPassword($('#MemeberPasswordForm'));
							}).css({
						"cursor" : "pointer"
					});
					$('#FormReset', $('#MemeberPasswordForm')).click(
							function() {
								$('#email', $('#MemeberPasswordForm')).val('');
							}).css({
						"cursor" : "pointer"
					});
					$('input[type=text]', $('#MemeberPasswordForm'))
							.each(
									function() {
										$(this)
												.keydown(
														function(e) {
															// alert(e.keyCode);
															if (e.keyCode == 13) {
																MemberNewPassword($('#MemeberPasswordForm'));
															}
														});
									});

					$('.NewMagazineList').each(function() {
						$(this).css("display", "none");
					});
					$('.NewMagazineList:eq(0)').show();
					setInterval(function() {
						var t = $('#NewMagazineListNum');

						var i = $(t).html();
						var i = parseInt((i == "NaN" || i == "") ? 0
								: parseInt(i));
						// alert(i);

						switch (i % 2) {
						case 0:
							$('.NewMagazineList:eq(0)').hide().fadeOut("show");
							$('.NewMagazineList:eq(1)').fadeIn("show");
							break;
						case 1:
							$('.NewMagazineList:eq(1)').hide().fadeOut("show");
							$('.NewMagazineList:eq(0)').fadeIn("show");
							break;
						}
						i++;
						$(t).html(i);
					}, 5000);

					// 小圖 & 大圖 輪撥
					var i = document.createElement('div');
					$('body').append(
							$(i).html(1).attr('id', 'banner_nums').hide());
					setInterval(function() {
						var n = $('div[class^=infomation_smallad]').length;
						var i = $('#banner_nums');
						var o = $('div[class^=infomation_smallad]:eq('
								+ parseInt(i.html()) % n + ')');
						chAd(o);
						// alert(parseInt(i.html()) % n);
						// alert(i.html());
						$(i).html(parseInt(i.html()) + 1);
					}, 5000);

					// 新聞,好康,新片上架 tab
					$('#list_1,#list_2,#list_3')
							.each(
									function() {
										var id = $(this).attr('id').split('_');
										var id = id[1];
										$(this)
												.hover(
														function() {
															// alert('list_'+id);
															// alert('BlockListContImg_'+id);
															$(this)
																	.parent()
																	.parent()
																	.removeClass(
																			'BlockListContImg_1')
																	.removeClass(
																			'BlockListContImg_2')
																	.removeClass(
																			'BlockListContImg_3')
																	.addClass(
																			'BlockListContImg_'
																					+ id);
															$('.list_1').hide();
															$('.list_2').hide();
															$('.list_3').hide();
															$('.list_' + id)
																	.show();
														});
									});

					// 圖片 delay load 圖

					$(
							'img[id=img_show_delay],img[id=movie_view_img],img[id=start_view_img],.BlockoldmoivenewtextContnet img')
							.each(
									function() {
										$(this)
												.lazyload(
														{
															placeholder : "images/img-placeholder-grey.gif",
															effect : "fadeIn"
														});
									});

					// $("#show_facebook").show();

				});

// var MagazineImg;
// MagazineImg = 0;
function SetNewMagazineImg() {

	var t = $('#NewMagazineListNum');

	var i = $(t).html();
	var i = parseInt((i == "NaN" || i == "") ? 0 : parseInt(i));
	// alert(i);

	switch (i % 2) {
	case 0:
		$('.NewMagazineList:eq(0)').hide().fadeOut("show");
		$('.NewMagazineList:eq(1)').fadeIn("show");
		break;
	case 1:
		$('.NewMagazineList:eq(1)').hide().fadeOut("show");
		$('.NewMagazineList:eq(0)').fadeIn("show");
		break;
	}

	i++;
	$(t).html(i);

	// MagazineImg++;

	// alert($(i).css("display"));
	// alert($(i).css("display"));
}

function doCommand(id, type) {
	// alert(id);
	// alert(type);
	var o = $('#doCommandForm');
	$('#type', o).val(type);
	$('#pid', o).val(id);
	$('#action', o).val("do");
	// alert($(o).html());
	document.getElementById('doCommandForm').submit();
}

function doMyFavorites(id, type) {
	// alert(id);
	// alert(type);
	var o = $('#doMyFavoritesForm');
	$('#type', o).val(type);
	$('#pid', o).val(id);
	$('#action', o).val("do");
	// alert($(o).html());
	document.getElementById('doMyFavoritesForm').submit();
}

function doDeleteMyBox(id, type) {
	if (confirm("確定要刪除?")) {
		var o = $('#doMyBoxForm');
		$('#type', o).val(type);
		$('#pid', o).val(id);
		$('#action', o).val("do");
		// alert($(o).html());
		document.getElementById('doMyBoxForm').submit();
	}
}

function doLoginForm(f) {

	if (f.userid.value == '') {
		alert('請輸入帳號！');
		f.userid.focus();
		return false;
	}

	if (f.password.value == '') {
		alert('請輸入密碼');
		f.password.focus();
		return false;
	}
	if (f.authCodeImg.value == '') {
		alert('請輸入驗證碼');
		f.authCodeImg.focus();
		return false;
	}

	// authCodeImg

}

function alertMsgIng() {
	alert('施工中，請期待。');
	// alert($(this).attr("src"));
	return false;

}

function alertMsgNoData() {
	alert('暫無資料。');
	return false;

}

function goBack() {
	// alert($(this).parent().html());
	javascript: history.go(-1);
	// alert('暫無資料。')
	return false;
}

function MM_jumpMenu(targ, selObj, restore) { // v3.0
	eval(targ + ".location='" + selObj.options[selObj.selectedIndex].value
			+ "'");
	if (restore)
		selObj.selectedIndex = 0;
}

function MM_swapImgRestore() { // v3.0
	var i, x, a = document.MM_sr;
	for (i = 0; a && i < a.length && (x = a[i]) && x.oSrc; i++)
		x.src = x.oSrc;
}

function MM_preloadImages() { // v3.0
	var d = document;
	if (d.images) {
		if (!d.MM_p)
			d.MM_p = new Array();
		var i, j = d.MM_p.length, a = MM_preloadImages.arguments;
		for (i = 0; i < a.length; i++)
			if (a[i].indexOf("#") != 0) {
				d.MM_p[j] = new Image;
				d.MM_p[j++].src = a[i];
			}
	}
}

function MM_findObj(n, d) { // v4.01
	var p, i, x;
	if (!d)
		d = document;
	if ((p = n.indexOf("?")) > 0 && parent.frames.length) {
		d = parent.frames[n.substring(p + 1)].document;
		n = n.substring(0, p);
	}
	if (!(x = d[n]) && d.all)
		x = d.all[n];
	for (i = 0; !x && i < d.forms.length; i++)
		x = d.forms[i][n];
	for (i = 0; !x && d.layers && i < d.layers.length; i++)
		x = MM_findObj(n, d.layers[i].document);
	if (!x && d.getElementById)
		x = d.getElementById(n);
	return x;
}

function MM_swapImage() { // v3.0
	var i, j = 0, x, a = MM_swapImage.arguments;
	document.MM_sr = new Array;
	for (i = 0; i < (a.length - 2); i += 3)
		if ((x = MM_findObj(a[i])) != null) {
			document.MM_sr[j++] = x;
			if (!x.oSrc)
				x.oSrc = x.src;
			x.src = a[i + 2];
		}
}

function CreateBookmarkLink(url, title) {
	if (window.sidebar) {
		// Mozilla Firefox Bookmark
		window.sidebar.addPanel(title, url, "");
	} else if (window.external) {
		// IE Favorite
		window.external.AddFavorite(url, title);
	} else if (window.opera && window.print) {
		// Opera Hotlist
		return true;
	}
}

function setHomepage(url) {
	if (document.all) {
		document.body.style.behavior = 'url(#default#homepage)';
		document.body.setHomePage(url);
	} else if (window.sidebar) {
		if (window.netscape) {
			try {
				netscape.security.PrivilegeManager
						.enablePrivilege("UniversalXPConnect");
			} catch (e) {
				alert("該操作被流覽器拒絕，如果想啟用該功能，請在位址欄內輸入 about:config,然後將項 signed.applets.codebase_principal_support 值該為true");
			}
		}
		var prefs = Components.classes['@mozilla.org/preferences-service;1']
				.getService(Components.interfaces.nsIPrefBranch);
		prefs.setCharPref('browser.startup.homepage', url);
	}
}

function openMoviePop(isfile, file) {

	if (parseInt(isfile) == 1) {
		window.open(file);
	} else {
		alert('暫無資料。');
		return false;
	}

}

function chAd(o) {
	var i = $(o).attr("bimg");
	var t = $(o).attr("text");
	var l = jQuery.trim($(o).attr("links"));
	var l = (typeof (l) == "undefined" || l == "") ? "" : l;
	// alert($(o).html());
	if (typeof (l) == "undefined" || l == "") {
		$("div.infomation_bigad").css({
			"cursor" : "default"
		});
	} else {
		$("div.infomation_bigad").css({
			"cursor" : "pointer"
		});
	}

	$('div.infomation_bigad').css({
		"background-image" : "url(" + i + ")"
	}).attr("opLinks", l);

	$('div[id=infomation_bigad_text]').html(t);
};

function getRandomInt(min, max) {
	return Math.floor(Math.random() * (max - min + 1)) + min;
}

function img_resize(img, w, h) {
	var _w = img.width, _h = img.height;
	if (_w / w > _h / h)
		img.width = w;
	else
		img.height = h;
}
function command_start(start, li, pos) {
	$(li).parent("ul").css("background-position", "0px " + pos + "px");
	$("#command_start", $("#doCommandStart")).val(start);
}
function fn_doCommandStart(movie_id, start, type) {
	$("#command_start", $("#doCommandStart")).val(start);
	$("#command_type", $("#doCommandStart")).val(type);
	$("#movie_id", $("#doCommandStart")).val(movie_id);
	$("#post", $("#doCommandStart")).val("1");
	$("#action", $("#doCommandStart")).val("do");
	$("#doCommandStart").submit();
}
