﻿/*
 * Cookie plugin
 *
 * Copyright (c) 2006 Klaus Hartl (stilbuero.de)
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 *
 */
 jQuery.cookie = function(name, value, options) {  
     if (typeof value != 'undefined') { // name and value given, set cookie  
         options = options || {};  
         if (value === null) {  
             value = '';  
             options.expires = -1;  
         }  
         var expires = '';  
         if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {  
             var date;  
             if (typeof options.expires == 'number') {  
                 date = new Date();  
                 date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));  
             } else {  
                 date = options.expires;  
             }  
             expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE  
         }  
         // CAUTION: Needed to parenthesize options.path and options.domain  
         // in the following expressions, otherwise they evaluate to undefined  
         // in the packed version for some reason...  
         var path = options.path ? '; path=' + (options.path) : '';  
         var domain = options.domain ? '; domain=' + (options.domain) : '';  
         var secure = options.secure ? '; secure' : '';  
         document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');  
     } else { // only name given, get cookie  
         var cookieValue = null;  
         if (document.cookie && document.cookie != '') {  
             var cookies = document.cookie.split(';');  
             for (var i = 0; i < cookies.length; i++) {  
                 var cookie = jQuery.trim(cookies[i]);  
                 // Does this cookie string begin with the name we want?  
                 if (cookie.substring(0, name.length + 1) == (name + '=')) {  
                     cookieValue = decodeURIComponent(cookie.substring(name.length + 1));  
                     break;  
                 }  
             }  
         }  
         return cookieValue;  
     }  
 };  

//clear text fields
$(document).ready(clearTextFields);
function clearTextFields() {
	if ($(".initTextFields").length > 0 ) {
		var $fields = $(".clear");
		$fields.bind("focus",fieldsFocus);
		$fields.bind("blur",fieldsBlur);
	}
}
function fieldsFocus() {
	if (this.value == this.defaultValue) {
		this.value = "";
	}
	else {
		this.select();
	}
}
function fieldsBlur() {
	var re = /^\s+(\s)*$/;
	if (re.test(this.value) || this.value == "") {
		this.value = this.defaultValue;
	}
}

//init item subscribtion
$(document).ready(initItemSub);

var $cookie_name = "userEmail";

function initItemSub() {
	if ($(".item_mailSub").length == 0) return;
	var $subThis = $(".item_mailSub");
	$subThis.bind("click",function(e) {
		e.preventDefault();						   
		if($(this).parent().children(".item_subForm")) {
			var $thisSubForm = $(this).parent().children(".item_subForm");
			$(".item_subForm").fadeOut(100);
			if($.cookie($cookie_name)) {
				$thisSubForm.children(".item_mailInput").attr("value",$.cookie($cookie_name));
			}
			$thisSubForm.fadeIn(100);
		}
	});
	var $closeLinks = $(".closeSub");
	$closeLinks.bind("click",function(e) {
		e.preventDefault();
		$(this).parent().children(".item_mailInput").attr("value","请输入你的邮箱");
		$(this).parent().fadeOut(100);
	});
	var $subBtns = $(".item_subBtn ");
	$subBtns.bind("click",function(e) {
		if (chkItemSubForm($(this).parent())) {
			proceedItemSub($(this).parent());
		}
	});
	$subBtns.bind("keypress",function(e) {
		if (e.keyCode == 13 && chkItemSubForm($(this).parent())) {
			proceedItemSub($(this).parent());
		}
	});
}
function chkItemSubForm(whichForm) {
	var $thisInput = whichForm.children(".item_mailInput");
	var $thisInputText = $thisInput.attr("value");
	var reBlank = /^\s+(\s)*$/;
	var reMail = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
	if ($thisInputText == $thisInput[0].defaultValue) {
		$thisInput.select();
		return false;
	}
	if (reBlank.test($thisInputText) || $thisInputText == "") {
		$thisInputText = $thisInput[0].defaultValue;
		$thisInput.select();
		return false;
	}
	if (!reMail.test($thisInputText)) {
		$thisInput.select();
		return false;
	}
	else {
		$.cookie($cookie_name, $thisInputText);
		return true;
	}
}
function proceedItemSub(whichForm) {
	var thisId = whichForm[0].id;
	var thisEmail = whichForm[0].elements[0].value;
	submitItemSub(thisId,thisEmail,whichForm);
}

//init multiItems subscribtion
$(document).ready(initMultiSub);

//var $cookie_name = "userEmail";

function initMultiSub() {
	if ($(".multiSubLink").length == 0 || $(".multiSubChk").length == 0) return;
	var $subItems = $(".multiSubLink");
	$subItems.bind("click",function(e) {
		e.preventDefault();
		var $chkedItems = chkChkBox();
		if ($chkedItems == 0) {
			$(".multiSubscribe em").show().fadeOut(3000);
		}
		else confirmMultiSub();
	});
}
function chkChkBox() {
	var $items = $(".item_multiSub :checkbox:checked");
	return $items.length;
}
function confirmMultiSub() {
	$('<div id="transBg" class="hide"></div>').prependTo("body");
	$("#transBg").fadeTo(1,0.8,function() { $("#transBg").css({background:"#000",height:$(document).height()});});
	$('<div id="multiSubHolder" class="hide"><div class="multiSub"><h3>合并订阅的项目列表:</h3><ul class="multiSubList"></ul><form id="multiSubForm" class="initTextFields" action=""><input type="text" name="email" id="multiSubMail" class="item_mailInput inputBox textField clear mr5 w180" value="请输入你的邮箱" /><input type="button" name="confirmMultiSub" class="multiSubBtn stdBtn" value="确认订阅" /></form><a class="multiSubClose" href="#" title="关闭">关闭</a><span class="loading hide">请稍候...</span></div></div>').prependTo("body").fadeIn(300);
	
	$(".multiSub").css("margin-top",$(document).scrollTop() + 100);
	//$(document).scroll(function() {$(".multiSub").css("margin-top",$(document).scrollTop() + 100);});
	if($.cookie($cookie_name)) {
		$("#multiSubMail").attr("value",$.cookie($cookie_name));
	}
	$(".clear").bind("focus",fieldsFocus);
	$(".clear").bind("blur",fieldsBlur);
	

	if ($("#multiSubHolder").length != 0) {
		$(".multiSubClose").bind("click",function(e) {
			e.preventDefault();
			$("#multiSubHolder").empty().add("#transBg").fadeOut(200);									  
		});
	}
	var $chkedItems = $(".item_multiSub :checkbox:checked");
	$chkedItems.each(function() {
		var $itemName = $(this).attr("alt");
		var $itemId = $(this).attr("value");
		$('<li class=' + $itemId + '>' + $itemName + '</li>').appendTo(".multiSubList");
	});
	$(".multiSubBtn").bind("click",function(e) {
		e.preventDefault();
		if (chkItemSubForm($(this).parent())) {
			proceedMultiSub($(this).parent());
		}
	});
}
//function proceedMultiSub() {
	//$("#multiSubHolder").add("#transBg").fadeOut(200);									  
//}
function proceedMultiSub(whichForm) {
	var itemsId = new Array();
	$(".multiSubList li").each(function(i) {
		itemsId.push($(this).attr("class"));								
	});
	var thisEmail = whichForm[0].elements[0].value;
	submitItemsSub(itemsId,thisEmail);
}


//init detailItem subscribtion
$(document).ready(initDetailSub);

function initDetailSub() {
	if ($(".detail .item").length == 0) return;
	var $detailItem = $(".detail .item .item_mailSub a");
	$detailItem.bind("click",function(e) {
		e.preventDefault();
		confirmDetailSub($(this).attr("id"))
	});
}
function confirmDetailSub(itemId) {
	$('<div id="transBg" class="hide"></div>').prependTo("body");
	$("#transBg").fadeTo(1,0.8,function() { $("#transBg").css({background:"#000",height:$(document).height()});});
	$('<div id="detailSubHolder" class="hide"><div class="detailSub"><h3>用于接收更新通知的Email:</h3><form id="detailSubForm" class="initTextFields" action=""><input type="text" name="email" id="detailSubMail" class="item_mailInput inputBox textField clear mr5 w180" value="请输入你的邮箱" /><input type="button" name="confirmDetailSub" class="detailSubBtn stdBtn" value="确认订阅" /></form><a class="detailSubClose" href="#" title="关闭">关闭</a></div></div>').prependTo("body").fadeIn(300);
	
	$(".detailSub").css("margin-top",$(document).scrollTop() + 220);
	$(document).scroll(function() {$(".detailSub").css("margin-top",$(document).scrollTop() + 220);});
	if($.cookie($cookie_name)) {
		$("#detailSubMail").attr("value",$.cookie($cookie_name));
	}
	$(".clear").bind("focus",fieldsFocus);
	$(".clear").bind("blur",fieldsBlur);

	if ($("#detailSubHolder").length != 0) {
		$(".detailSubClose").bind("click",function(e) {
			e.preventDefault();
			$("#detailSubHolder").add("#transBg").fadeOut(200);									  
		});
	}
	$(".detailSubBtn").bind("click",function(e) {
		e.preventDefault();
		if (chkItemSubForm($(this).parent())) {
			proceedDetailSub(itemId,$(this).parent());
		}
	});
}
function proceedDetailSub(itemId,whichForm) {
	var thisEmail = whichForm[0].elements[0].value;
	submitDetailSub(itemId,thisEmail,whichForm);
}

//submit item(s) subscribtion
function submitItemSub(whichId,whichEmail,whichForm) {
	//$(".item_subForm .loading").ajaxStart(function() { $(this).prevAll().hide(); $(this).show();}).ajaxStop(function() { $(this).hide(); $(this).prevAll().show();});
	whichForm.empty();
	$('<span class="loading hide">请稍候...</span>').appendTo(whichForm);
	$(".item_subForm .loading").ajaxStart(function() { $(this).show();}).ajaxStop(function() { $(this).hide();});
	
	$.post("subscribe.php",{"sName" : whichId,"sEmail" : whichEmail},function(data) {
		whichForm.html(data);
		$('<a class="itemSubClose" href="#" title="关闭">关闭</a>').prependTo(whichForm).bind("click",function(e) {
			e.preventDefault();
			$(whichForm).fadeOut(200);									  
		});;
	});
}
function submitItemsSub(itemsId,whichEmail) {
	var $multiSubHolder = $(".multiSub");	
	$multiSubHolder.empty();
	$('<span class="loading hide">请稍候...</span>').appendTo($multiSubHolder);
	$(".multiSub .loading").ajaxStart(function() { $(this).show();}).ajaxStop(function() { $(this).hide();});
	
	$.post("subscribe.php",{"sNames[]" : itemsId,"sEmail" : whichEmail},function(data) {
		$multiSubHolder.html(data);
		$('<a class="multiSubClose" href="#" title="关闭">关闭</a>').appendTo($multiSubHolder).bind("click",function(e) {
			e.preventDefault();
			$("#multiSubHolder").empty().add("#transBg").fadeOut(200);									  
		});;
	});
}
function submitDetailSub(itemId,thisEmail,whichForm) {
	var $detailSub = $(".detailSub");																									   
	$detailSub.empty();
	$('<span class="loading hide">请稍候...</span>').appendTo($detailSub);
	$(".detailSub .loading").ajaxStart(function() { $(this).show();}).ajaxStop(function() { $(this).hide();});
	
	$.post("subscribe.php",{"sName" : itemId,"sEmail" : thisEmail},function(data) {
		$detailSub.html(data);
		$('<a class="detailSubClose" href="#" title="关闭">关闭</a>').prependTo($detailSub).bind("click",function(e) {
			e.preventDefault();
			$("#detailSubHolder").empty().add("#transBg").fadeOut(200);									  
		});;
	});
}

//init textInput validation
$(document).ready(initTextValid);

function initTextValid() {
	if ($(".req").length > 0) {
		$(".req").each(function() {
			if ($(this).attr("type") == "text") {
				var re = /^\s+(\s)*$/;
				$(this).bind("blur",function() {
					if (re.test(this.value) || this.value == "") {
						$(this).addClass("valid");
					}
					else {
						if (this.className.indexOf("valid") != -1) {
							$(this).removeClass("valid");
						}
					}
				});
			}
		});
	}
}
