

$(document).ready(function () {
	jQuery.fn.center = function () {
    this.css("position","absolute");
    this.css("top", (($(window).height() - this.outerHeight()) / 2) + $(window).scrollTop() + "px");
    this.css("left", (($(window).width() - this.outerWidth()) / 2) + $(window).scrollLeft() + "px");
    return this;
}
    var elNewbook = $("#newbook").find(".book-info .slider").data("index", 0);
    $("#newbook").find(".previous a").bind("click", function () {
        if (elNewbook.data("index") > 0)
            elNewbook.data("index", elNewbook.data("index") - 1).animate({ left: "+=185px" });
        else elNewbook.data("index", 2).animate({ left: "-=370px" });

    });
    $("#newbook").find(".next a").bind("click", function () {
        if (elNewbook.data("index") < 2)
            elNewbook.data("index", elNewbook.data("index") + 1).animate({ left: "-=185px" });
        else elNewbook.data("index", 0).animate({ left: "+=370px" });
    });

	var elIntroduction = $("#introduction").find(".book-info .slider").data("index", 0);
    $("#introduction").find(".previous a").bind("click", function () {
        if (elIntroduction.data("index") > 0)
            elIntroduction.data("index", elIntroduction.data("index") - 1).animate({ left: "+=185px" });
		else elIntroduction.data("index", 2).animate({ left: "-=370px" });

    });
    $("#introduction").find(".next a").bind("click", function () {
        if (elIntroduction.data("index") < 2)
            elIntroduction.data("index", elIntroduction.data("index") + 1).animate({ left: "-=185px" });
		else elIntroduction.data("index", 0).animate({ left: "+=370px" });
    });

    var elTop50EBook = $("#top50ebook").find(".books .slider").data("index", 0);
    $("#top50ebook").find("a.previous").bind("click", function () {
        if (elTop50EBook.data("index") > 0)
        { elTop50EBook.data("index", elTop50EBook.data("index") - 1).animate({ left: "+=480px" }) }
        else elTop50EBook.data("index", 6).animate({ left: "-=2880px" });

    });
    $("#top50ebook").find("a.next").bind("click", function () {
        if (elTop50EBook.data("index") < 6)
            elTop50EBook.data("index", elTop50EBook.data("index") + 1).animate({ left: "-=480px" });
        else elTop50EBook.data("index", 0).animate({ left: "+=2880px" });
    });
	var str = '1';
	var elcategorygroup = $("#categorygroup"+str).find(".books .slider").data("index", 0);
    $("#categorygroup").find("a.previous").bind("click", function () {
        if (elcategorygroup.data("index") > 0)
        { elcategorygroup.data("index", elcategorygroup.data("index") - 1).animate({ left: "+=480px" }) }
        else elcategorygroup.data("index", 6).animate({ left: "-=2880px" });

    });
    $("#categorygroup"+str).find("a.next").bind("click", function () {
        if (elcategorygroup.data("index") < 6)
            elcategorygroup.data("index", elcategorygroup.data("index") + 1).animate({ left: "-=480px" });
        else elcategorygroup.data("index", 0).animate({ left: "+=2880px" });
    });


    $('#lolol').css({top:'50%',left:'50%',margin:'-'+($('#lolol').height() / 2)+'px 0 0 -'+($('#lolol').width() / 2)+'px'});

    /*
	var elTop50EBook = $("#top50ebook").find(".books .slider").data("index", 0).data("loadedIndex", 0);
    $("#top50ebook").find("a.previous").bind("click", function () {
        if (elTop50EBook.data("index") > 0)
            elTop50EBook.data("index", elTop50EBook.data("index") - 1).animate({ left: "+=480px" });
    });
    $("#top50ebook").find("a.next").bind("click", function () {
        if (elTop50EBook.data("index") < 12)
            if (elTop50EBook.data("loadedIndex") > elTop50EBook.data("index"))
                elTop50EBook.data("index", elTop50EBook.data("index") + 1).animate({ left: "-=480px" });
            else
                $.get("/Slideshow/Top50EBook", { index: elTop50EBook.data("index") + 1 }, function (data) {
                    elTop50EBook.append(data);
                    elTop50EBook.data("loadedIndex", elTop50EBook.data("index") + 1);
                    elTop50EBook.data("index", elTop50EBook.data("index") + 1).animate({ left: "-=480px" });
                });
    });

    var elTop50Book = $("#top50book").find(".books .slider").data("index", 0).data("loadedIndex", 0);
    $("#top50book").find("a.previous").bind("click", function () {
        if (elTop50Book.data("index") > 0)
            elTop50Book.data("index", elTop50Book.data("index") - 1).animate({ left: "+=480px" });
    });
    $("#top50book").find("a.next").bind("click", function () {
        if (elTop50Book.data("index") < 12)
            if (elTop50Book.data("loadedIndex") > elTop50Book.data("index"))
                elTop50Book.data("index", elTop50Book.data("index") + 1).animate({ left: "-=480px" });
            else
                $.get("/Slideshow/Top50Book", { index: elTop50Book.data("index") + 1 }, function (data) {
                    elTop50Book.append(data);
                    elTop50Book.data("loadedIndex", elTop50Book.data("index") + 1);
                    elTop50Book.data("index", elTop50Book.data("index") + 1).animate({ left: "-=480px" });
                });
    });
	*/
} ());


// Page Loading Script START
$(document).ready(function () {
    $("a.purchaseProceed").click(function (event) {
        event.preventDefault();
        linkLocation = this.href;
        redirectPage();
        pageLoadingShow();
    });

    function pageLoadingShow() {
        centerPopup();
        $("#pageLoadingPopup").css({
            "opacity": "0.7"
        });
        $("#pageLoadingPopup").fadeIn(400);
        $("#pageLoading").fadeIn(400);
        $("#pageLoadingContent").html("正在載入頁面...");
        $("#pageLoadingHeading").text("首尚文化");
    }

    function centerPopup() {
        var windowWidth = $(document.body)[0].clientWidth;  // document.documentElement.clientWidth;
        var windowHeight = document.documentElement.clientHeight;
        var popupHeight = $("#pageLoading").height();
        var popupWidth = $("#pageLoading").width();

        $("#pageLoading").css({
            "position": "absolute",
            "top": windowHeight / 2 - popupHeight / 2,
            "left": windowWidth / 2 - popupWidth / 2
        });
        //only need force for IE6

        $("#pageLoadingPopup").css({
            "height": windowHeight
        });
    }

    function redirectPage() {
        window.location = linkLocation;
    }




});
// Page Loading Script END

 function manageCookie(id, book_id, mode) {
		$.ajax({
			 url: location.protocol + '//' + location.host + "/save_cookie.php",
			 type: "POST",
			 async: false,
			 data: { ID: id, book_ID: book_id, action: mode},
			dataType:  'json',
			 cache: false,
			 success: function (json) {
				if (mode == 'ADD') {
					//alert(response.filename);

					popUpDiv('已新增至購物籃','電子書 &lt;'+ json.bookname+'&gt; 已經新增至購物籃。', true);
					$("#cart").empty().append(json.message).fadeIn('slow');
					$("#popUpQty").html(parseInt($("#cart_qty").html()));
					$('#no_book_indicator').empty().append('&nbsp;&nbsp;&nbsp;你暫時沒有任何書藉。');

				} else if (mode == 'REMOVE'){

					$("#cart").empty().append(json.message);
					//update small_cal
					$('#'+book_id).remove();
					$('.small_cal').empty().append('<strong>小結 : US$'+json.price+'</strong>');

					if (json.cart_size == 0){
						$('#no_book_indicator').empty().append('&nbsp;&nbsp;&nbsp;你暫時沒有任何書藉。');
						$('#paypal').hide();
					} else {
						$('#no_book_indicator').empty();
						$('#paypal').show();
					}

					popUpDiv('書本已移除','電子書 &lt;'+ json.bookname+'&gt; 已經從購物籃移除。');
				} else if( mode == "DESTROY"){
					$("#cart").empty().append(json.message);
				}else {
					alert('failed' + json.message).fadeIn('slow');;
					//removal is only applicable to
					//$('.hello').remove();
				}
			 },
			error:function(xhr,err){
			    alert("readyState: "+xhr.readyState+"\nstatus: "+xhr.status);
			    alert("responseText: "+xhr.responseText);
			}

		});

    }

function updateCartHeader(){

//only one of the following exist
		$.ajax({
			 url: location.protocol + '//' + location.host + "/save_cookie.php",
			 type: "POST",
			 cache: false,
			 success: function (response) {
				alert(response);
			 },
			error:function(xhr,err){
			    alert("readyState: "+xhr.readyState+"\nstatus: "+xhr.status);
			    alert("responseText: "+xhr.responseText);
			}

		});


	//$("#tbooks-list").empty().append(dataFromServer);

}

function addPreview(userId, bookId, bookName){
	if(userId == 0){
		window.location="/Login/?to=/Detail/Book/"+bookId+"/";
	}else{
		$.ajax({
			type: "POST",
			url: "/AddPreview.php",
			data: "userId="+userId+"&bookId="+bookId,
			dataType: "json",
			success: function(msg){
				if(msg.result == "Success"){
					popUpDiv('已新增至書架','電子書試閱版 &lt;'+ bookName+'&gt; 已經新增至你的書架。請即到你的流動裝置(iPhone, iPad, Android, Windows Phone 7)刷新你的電子書架，以閱讀此試閱版。<br /><br /><br /><div style="text-align: center"><a href="http://itunes.apple.com/us/app/id436322079?mt=8&ls=1" target="_blank"><img src="/images/App_Store.jpg" width="150" height="48" /></a><br /><br /><a href="https://market.android.com/details?id=handheldculture.ngp" target="_blank"><img src="/images/Android_Market.png" width="150" height="48" /></a><br /><br /><a href="#" class="readerLinks" onclick="disablePopup();popUpDiv(\'你必須已安裝 Zune\' , \'要瀏覧首尚文化Windows Phone 7 App 的下載頁面，你必須已安裝 Zune<br /><br />欲知詳情可以參看<a href=http://www.microsoft.com/windowsphone/en-ww/howto/wp7/apps/find-and-buy-apps-in-marketplace.aspx target=_blank>這裡</a><br /><br /><br /><a href=http://redirect.zune.net/External/LaunchZuneProtocol.aspx?pathuri=navigate%3FphoneAppID=c65d3440-53ac-e011-a53c-78e7d1fa76f8>我已經安裝 Zune</a><br/><br/><a href=# onclick=disablePopup()>取消</a>&nbsp;\')"><img src="/images/wm7.png" width="150" height="48" /></a></div>', false);
				}else if(msg.result == "Added"){
					popUpDiv('你已經新增過此項目', '你已經擁有此電子書試閱版，無須重覆下載。請即到你的流動裝置(iPhone, iPad, Android, Windows Phone 7)刷新你的電子書架，以閱讀此試閱版。<br /><br /><br /><div style="text-align: center"><a href="http://itunes.apple.com/us/app/id436322079?mt=8&ls=1" target="_blank"><img src="/images/App_Store.jpg" width="150" height="48" /></a><br /><br /><a href="https://market.android.com/details?id=handheldculture.ngp" target="_blank"><img src="/images/Android_Market.png" width="150" height="48" /></a><br /><br /><a href="#" class="readerLinks" onclick="disablePopup();popUpDiv(\'你必須已安裝 Zune\' , \'要瀏覧首尚文化Windows Phone 7 App 的下載頁面，你必須已安裝 Zune<br /><br />欲知詳情可以參看<a href=http://www.microsoft.com/windowsphone/en-ww/howto/wp7/apps/find-and-buy-apps-in-marketplace.aspx target=_blank>這裡</a><br /><br /><br /><a href=http://redirect.zune.net/External/LaunchZuneProtocol.aspx?pathuri=navigate%3FphoneAppID=c65d3440-53ac-e011-a53c-78e7d1fa76f8>我已經安裝 Zune</a><br/><br/><a href=# onclick=disablePopup()>取消</a>&nbsp;\')"><img src="/images/wm7.png" width="150" height="48" /></a></div>',false);
				}
			}
		});
	}
}


// Google Analytics START
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-26730073-1']);
_gaq.push(['_trackPageview']);

(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
// Google Analytics END

