﻿onContent(
	function() {
	    // take the data from the cookie and prepare the thumbs
	    Notebook.AddFromServer();
	    if ($("NotebookOpenLink")) $("NotebookOpenLink").observe("click", Notebook.Open);
	    if ($("NotebookCloseLink")) $("NotebookCloseLink").observe("click", Notebook.Close);
	    if ($("CurrentNotebookFilter")) {
	        $("CurrentNotebookFilter").selectedIndex = 0;
	        $("CurrentNotebookFilter").observe("change", Notebook.Filter);
	    }
	}
);

	var Notebook = {
	    /* The following are arrays of UserProduct and UserGalleryImage */
	    addedProducts: [],
	    addedGalleryImages: [],
	    state: 0, /*0=close,1=animating,2=opened*/
	    lastPanel: '',
	    delimiter: "|",
	    afterLBClose: false,
	    ViewPanel: function(panelName) {
	        if (Notebook.lastPanel != "") {
	            $("Notebook" + Notebook.lastPanel + "Panel").hide();
	        }
	        if (panelName == 'Lists' && !Membership.IsLogged()) panelName = 'Login';
	        if (panelName == 'Login' && Membership.IsLogged()) panelName = 'Lists';
	        Notebook.lastPanel = panelName;
	        $("Notebook" + Notebook.lastPanel + "Panel").show();
	    },
	    Empty: function() {
	        $("NotebookProducts").innerHTML = "";
	        $("NotebookGalleryImages").innerHTML = "";
	        Notebook.addedGalleryImages = [];
	        Notebook.addedProducts = [];
	    },
	    Clear: function() {
	        if (confirm("Are you sure you wish to clear the notebook?")) {
	            Notebook.Empty();
	            Mantis.CaesarStone.Pages.NotebookControl.ClearNotebook();
	        }
	    },
	    Open: function() {
	        // already opened...
	        if (Notebook.state == 2) return;

	        var h = 0;
	        if (window.innerHeight) h = window.innerHeight;
	        if (document.body.scrollHeight) h = document.body.scrollHeight;
	        if (document.body.offsetHeight) h = document.body.offsetHeight;

	        if (Notebook.lastPanel == '') {
	            if (Membership.IsLogged()) Notebook.ViewPanel('Lists');
	            else Notebook.ViewPanel('Login');
	        }
	        $("NotebookShadow").show();
	        $("NotebookWrap").show();
	        $("NotebookPanel").setStyle({ top: '-500px' });

	        // animating mode
	        Notebook.state = 1;
	        new Effect.Morph('NotebookPanel', {
	            style: 'top: 0px;',
	            afterFinish: function() { Notebook.state = 2; }
	        });
	    },
	    Close: function() {
	        // already closed...
	        if (Notebook.state == 0) return;

	        //animating mode
	        Notebook.state = 1;
	        new Effect.Morph('NotebookPanel', {
	            style: 'top: -500px;',
	            afterFinish: function() {
	                // mark closed mode
	                Notebook.state = 0;
	                $("NotebookShadow").hide();
	                $("NotebookWrap").hide();
	            }
	        });
	    },

	    Filter: function() {
	        var isProducts = $("CurrentNotebookFilter").selectedIndex == 0;
	        isProducts ? $("NotebookProducts").show() : $("NotebookProducts").hide();
	        isProducts ? $("NotebookGalleryImages").hide() : $("NotebookGalleryImages").show();
	    },

	    AddFromServer: function() {
	        if (Membership.IsLogged()) {
	            Mantis.CaesarStone.Pages.NotebookControl.GetProducts(
                    AjaxProOutput(
                        function(serverResponse) {
                            var products = serverResponse;
                            for (var c = 0; c < products.length; c++) {
                                Notebook.AddProduct(products[c]);
                            }
                        }
                    )
                );
	            Mantis.CaesarStone.Pages.NotebookControl.GetGalleryImages(
                AjaxProOutput(
                    function(serverResponse) {
                        var images = serverResponse;
                        for (var c = 0; c < images.length; c++) {
                            if (images[c].EntityType == 2) // Application Gallery
                                Notebook.AddAGalleryImage(images[c]);
                            else
                                Notebook.AddGalleryImage(images[c]);
                        }
                    }
                )
            );
	        } else Notebook.ViewPanel('Login');
	    },

	    AddProduct: function(productId, disableAutoOpen, comment) {
	        // avoid empty data from cookie by mistake
	        if (typeof (productId) == "undefined" || productId == "") return;
	        $("CurrentNotebookFilter").selectedIndex = 0;
	        Notebook.Filter();
	        if (typeof (productId) == "string" || typeof (productId) == "number") {
	            if (!disableAutoOpen) Notebook.Open();

	            if (Notebook.addedProducts.indexOf(productId) == -1)
	                var product = Products.GetById(productId.toString(), Notebook.GetProductComplete, comment);
	            else
	                alert("This product is already in your notebook.");
	        } else {
	            //This is a NotebookItem
	            if (Notebook.addedProducts.indexOf(productId.EntityId) == -1) {
	                Products.GetById(productId.EntityId, Notebook.CreateProductThumb, productId.Comment);
	                Notebook.addedProducts.push(productId.EntityId);
	            }
	            else
	                alert("This product is already in your notebook.");
	        }
	    },

	    GetProductComplete: function(product, comment) {
	        if (product == null) return;
	        // if user is not logged, store locally and wait for logon
	        // if the user is logged, add the product thumb and update the membership user details

	        var productId = product.ProductId;
	        product.Comment = comment;

	        if (Notebook.addedProducts.indexOf(productId) == -1) {
	            if (Membership.IsLogged()) {
	                Mantis.CaesarStone.Pages.NotebookControl.AddProduct(
				        productId,
				        comment,
				        AjaxProOutput(
					        function(serverResponse) {
					        }
				        )
			        );
	            }
	            else Notebook.ViewPanel('Login');
	            Notebook.addedProducts.push(product.ProductId);
	            Notebook.CreateProductThumb(product, comment);
	        }
	    },

	    SaveProduct: function(productId, comment) {
	        if (Membership.IsLogged()) {
	            Mantis.CaesarStone.Pages.NotebookControl.AddProduct(productId, comment, AjaxProOutput(function(serverResponse) { }));
	        }
	    },

	    AddGalleryImage: function(galleryImageId, disableAutoOpen, isProduct, comment) {
	        // avoid empty data from cookie by mistake
	        if (typeof (galleryImageId) == "undefined" || galleryImageId == "") return;
	        $("CurrentNotebookFilter").selectedIndex = 1;
	        Notebook.Filter();
	        if (typeof (galleryImageId) == "string" || typeof (galleryImageId) == "number") {
	            // used for easier usage in the gallery flash 
	            if (isProduct) {
	                Notebook.AddProduct(galleryImageId, false);
	                return;
	            }
	            if (!disableAutoOpen) Notebook.Open();
	            if (Notebook.addedGalleryImages.indexOf(galleryImageId) == -1)
	                var galleryImage = Galleries.GetGalleryImageById(galleryImageId, Notebook.GetGalleryImageComplete, comment);
	            else
	                alert("This image is already in your notebook.");
	        } else {
	            //This is a NotebookItem
	            if (Notebook.addedGalleryImages.indexOf(galleryImageId.EntityId.toString()) == -1) {
	                Galleries.GetGalleryImageById(galleryImageId.EntityId, Notebook.CreateGalleryImageThumb, galleryImageId.Comment);
	                Notebook.addedGalleryImages.push(galleryImageId.EntityId.toString());
	            } else alert("This image is already in your notebook.");
	        }
	    },

	    AddAGalleryImage: function(galleryImageId, disableAutoOpen, isProduct, comment) {
	        // avoid empty data from cookie by mistake
	        if (typeof (galleryImageId) == "undefined" || galleryImageId == "") return;
	        $("CurrentNotebookFilter").selectedIndex = 1;
	        Notebook.Filter();
	        if (typeof (galleryImageId) == "string" || typeof (galleryImageId) == "number") {
	            // used for easier usage in the gallery flash 
	            if (isProduct) {
	                Notebook.AddProduct(galleryImageId, false);
	                return;
	            }
	            if (!disableAutoOpen) Notebook.Open();
	            if (Notebook.addedGalleryImages.indexOf(galleryImageId + "a") == -1)
	                var galleryImage = AGalleries.GetGalleryImageById(galleryImageId, Notebook.GetAGalleryImageComplete, comment);
	            else
	                alert("This image is already in your notebook.");
	        } else {
	            //This is a NotebookItem
	            if (Notebook.addedGalleryImages.indexOf(galleryImageId.EntityId + "a") == -1) {
	                AGalleries.GetGalleryImageById(galleryImageId.EntityId, Notebook.CreateAGalleryImageThumb, galleryImageId.Comment);
	                Notebook.addedGalleryImages.push(galleryImageId.EntityId + "a");
	            } else alert("this image is already in your notebook.");
	        }
	    },

	    GetGalleryImageComplete: function(galleryImage, comment) {
	        if (galleryImage == null) return;

	        var galleryImageId = galleryImage.GalleryImageId;
	        // if user is not logged, store it temporarily and wait for login
	        // if the user is logged, add the product thumb and update the membership user details

	        if (Notebook.addedGalleryImages.indexOf(galleryImageId) == -1) {
	            if (Membership.IsLogged()) {
	                Mantis.CaesarStone.Pages.NotebookControl.AddGalleryImage(galleryImageId.toString(), comment, "", AjaxProOutput(function(serverResponse) { }));
	            } else Notebook.ViewPanel('Login');
	            Notebook.addedGalleryImages.push(galleryImageId.toString());
	            Notebook.CreateGalleryImageThumb(galleryImage, comment);
	        }
	    },

	    GetAGalleryImageComplete: function(galleryImage, comment) {
	        if (galleryImage == null) return;

	        var galleryImageId = galleryImage.AGalleryImageId + "a";
	        // if user is not logged, store it temporarily and wait for login
	        // if the user is logged, add the product thumb and update the membership user details

	        if (Notebook.addedGalleryImages.indexOf(galleryImageId) == -1) {
	            if (Membership.IsLogged()) {
	                Mantis.CaesarStone.Pages.NotebookControl.AddGalleryImage(galleryImageId.toString(), comment, "", AjaxProOutput(function(serverResponse) { }));
	            } else Notebook.ViewPanel('Login');
	            Notebook.addedGalleryImages.push(galleryImageId);
	            Notebook.CreateAGalleryImageThumb(galleryImage, comment);
	        }
	    },

	    SaveGalleryImage: function(galleryImageId, comment) {
	        if (Membership.IsLogged()) {
	            Mantis.CaesarStone.Pages.NotebookControl.AddGalleryImage(galleryImageId, comment, "", AjaxProOutput(function(serverResponse) { }));
	        }
	    },

	    ToggleCommentProduct: function(productId) {
	        var e = $("NotebookProductComment" + productId);
	        if (e.style.display == "block") {
	            e.style.display = "none";
	            Notebook.UpdateCommentProduct(productId);
	            if (e.value != "") {
	                $("NotebookProduct_" + productId).addClassName("hascomment");
	                $("NotebookProduct_" + productId).title = "there is a comment on this item";
	            } else {
	                $("NotebookProduct_" + productId).removeClassName("hascomment");
	                $("NotebookProduct_" + productId).title = "";
	            }
	        }
	        else e.style.display = "block";
	    },

	    UpdateCommentProduct: function(productId) {
	        var comment = $("NotebookProductComment" + productId).value;
	        Notebook.SaveProduct(productId, comment);
	    },

	    ToggleCommentGalleryImage: function(galleryImageId) {
	        var e = $("NotebookGalleryImageComment" + galleryImageId);
	        if (e.style.display == "block") {
	            e.style.display = "none";
	            Notebook.UpdateCommentGalleryImage(galleryImageId);
	            if (e.value != "") {
	                $("NotebookGalleryImage_" + galleryImageId).addClassName("hascomment");
	                $("NotebookGalleryImage_" + galleryImageId).title = "there is a comment on this item";
	            } else {
	                $("NotebookGalleryImage_" + galleryImageId).removeClassName("hascomment");
	                $("NotebookGalleryImage_" + galleryImageId).title = "";
	            }
	        }
	        else e.style.display = "block";
	    },

	    UpdateCommentGalleryImage: function(galleryImageId) {
	        var comment = $("NotebookGalleryImageComment" + galleryImageId).value;
	        Notebook.SaveGalleryImage(galleryImageId, comment);
	    },

	    CreateProductThumb: function(oProduct, comment) {
	        if (!oProduct) return;
	        var ProductId = 0;
	        if (typeof (oProduct.ProductId) == "undefined") ProductId = oProduct.EntityId;
	        else ProductId = oProduct.ProductId;
	        var Comment = "";
	        if (typeof (oProduct.Comment) != "undefined") Comment = oProduct.Comment;
	        else if (typeof (comment) != "undefined") Comment = comment;

	        var o = $("NotebookProducts");
	        var template = '<li class="item{4}" id="NotebookProduct_{0}" style="display:none" > \
					<div class="thumb"><img src="{2}" /> \
					    <div class="header">{1}</div> \
					    <div class="clearfix"> \
						    <div class="view" onclick="Notebook.ViewProduct({0})" title="'+$("NotebookAltTextView").innerHTML+'"></div> \
						    <div class="send" onclick="Notebook.SendProduct({0})" title="' + $("NotebookAltTextSend").innerHTML + '"></div> \
						    <div class="comment" onclick="Notebook.ToggleCommentProduct({0})" title="' + $("NotebookAltTextComment").innerHTML + '"></div> \
						    <div class="delete" onclick="Notebook.DeleteProduct({0})" title="' + $("NotebookAltTextDelete").innerHTML + '"></div> \
						</div> \
					</div> \
					<textarea onchange="Notebook.UpdateCommentProduct({0})" class="commentbox" id="NotebookProductComment{0}" style="display:none">{3}</textarea> \
					<input type="checkbox" name="NotebookProduct" value="{0}" /> \
				</li>';

	        template = template.replace(/\{0}/g, ProductId);
	        template = template.replace(/\{1}/g, oProduct.Header);
	        template = template.replace(/\{2}/g, oProduct.MediumImage);
	        template = template.replace(/\{3}/g, Comment);
	        template = template.replace(/\{4}/g, (Comment != "" ? " hascomment" : ""));

	        new Insertion.Bottom(o, template);

	        Sortable.create('NotebookProducts', { ghosting: false, constraint: true, hoverclass: 'over',
	            onChange: function(element) {
	                Notebook.Reorder();
	            }
	        });

	        Effect.Appear($("NotebookProduct_" + ProductId));
	        $("NotebookProduct_" + ProductId).observe("mouseover", function() { $("NotebookProduct_" + ProductId).addClassName("itemhover"); });
	        $("NotebookProduct_" + ProductId).observe("mouseout", function() { $("NotebookProduct_" + ProductId).removeClassName("itemhover"); });
	    },

	    CreateGalleryImageThumb: function(oGalleryImage, comment) {
	        if (!oGalleryImage) return;
	        var GalleryImageId = 0;
	        if (typeof (oGalleryImage.GalleryImageId) == "undefined") GalleryImageId = oGalleryImage.EntityId;
	        else GalleryImageId = oGalleryImage.GalleryImageId;
	        var Comment = "";
	        if (typeof (oGalleryImage.Comment) != "undefined") Comment = oGalleryImage.Comment;
	        else if (typeof (comment) != "undefined") Comment = comment;

	        var o = $("NotebookGalleryImages");
	        var template = '<li class="item{4}" id="NotebookGalleryImage_{0}" style="display:none" > \
					<div class="thumb"><img src="{2}"/> \
					    <div class="header">{1}</div> \
					    <div class="clearfix"> \
						    <div class="view" onclick="Notebook.ViewGalleryImage(\'{0}\')" title="' + $("NotebookAltTextView").innerHTML + '"></div> \
						    <div class="send" onclick="Notebook.SendGalleryImage(\'{0}\')" title="' + $("NotebookAltTextSend").innerHTML + '"></div> \
						    <div class="comment" onclick="Notebook.ToggleCommentGalleryImage(\'{0}\')" title="' + $("NotebookAltTextComment").innerHTML + '"></div> \
						    <div class="delete" onclick="Notebook.DeleteGalleryImage(\'{0}\')" title="' + $("NotebookAltTextDelete").innerHTML + '"></div> \
						</div> \
					</div> \
					<textarea onchange="Notebook.UpdateCommentGalleryImage(\'{0}\')" class="commentbox" id="NotebookGalleryImageComment{0}" style="display:none">{3}</textarea> \
					<input type="checkbox" name="NotebookGalleryImage" value="{0}" /> \
				</li>';

	        template = template.replace(/\{0}/g, GalleryImageId);
	        template = template.replace(/\{1}/g, oGalleryImage.Header);
	        template = template.replace(/\{2}/g, SiteParams.UploadFolder + oGalleryImage.SmallImage);
	        template = template.replace(/\{3}/g, Comment);
	        template = template.replace(/\{4}/g, (Comment != "" ? " hascomment" : ""));

	        new Insertion.Bottom(o, template);

	        Sortable.create('NotebookGalleryImages', { ghosting: false, constraint: true, hoverclass: 'over',
	            onChange: function(element) {
	                Notebook.Reorder();
	            }
	        });

	        Effect.Appear($("NotebookGalleryImage_" + GalleryImageId));
	        $("NotebookGalleryImage_" + GalleryImageId).observe("mouseover", function() { $("NotebookGalleryImage_" + GalleryImageId).addClassName("itemhover"); });
	        $("NotebookGalleryImage_" + GalleryImageId).observe("mouseout", function() { $("NotebookGalleryImage_" + GalleryImageId).removeClassName("itemhover"); });
	    },

	    CreateAGalleryImageThumb: function(oGalleryImage, comment) {
	        if (!oGalleryImage) return;
	        var GalleryImageId = 0;
	        if (typeof (oGalleryImage.AGalleryImageId) == "undefined") GalleryImageId = oGalleryImage.EntityId + "a";
	        else GalleryImageId = oGalleryImage.AGalleryImageId + "a";
	        var Comment = "";
	        if (typeof (oGalleryImage.Comment) != "undefined") Comment = oGalleryImage.Comment;
	        else if (typeof (comment) != "undefined") Comment = comment;

	        var o = $("NotebookGalleryImages");
	        var template = '<li class="item{4}" id="NotebookGalleryImage_{0}" style="display:none" > \
					<div class="thumb"><img src="{2}"/> \
					    <div class="header">{1}</div> \
					    <div class="clearfix"> \
						    <div class="view" onclick="Notebook.ViewAGalleryImage(\'{0}\')" title="' + $("NotebookAltTextView").innerHTML + '"></div> \
						    <div class="send" onclick="Notebook.SendGalleryImage(\'{0}\')" title="' + $("NotebookAltTextSend").innerHTML + '"></div> \
						    <div class="comment" onclick="Notebook.ToggleCommentGalleryImage(\'{0}\')" title="' + $("NotebookAltTextComment").innerHTML + '"></div> \
						    <div class="delete" onclick="Notebook.DeleteGalleryImage(\'{0}\')" title="' + $("NotebookAltTextDelete").innerHTML + '"></div> \
						</div> \
					</div> \
					<textarea onchange="Notebook.UpdateCommentGalleryImage(\'{0}\')" class="commentbox" id="NotebookGalleryImageComment{0}" style="display:none">{3}</textarea> \
					<input type="checkbox" name="NotebookGalleryImage" value="{0}" /> \
				</li>';

	        template = template.replace(/\{0}/g, GalleryImageId);
	        template = template.replace(/\{1}/g, oGalleryImage.Header);
	        template = template.replace(/\{2}/g, SiteParams.UploadFolder + oGalleryImage.ImageSmall);
	        template = template.replace(/\{3}/g, Comment);
	        template = template.replace(/\{4}/g, (Comment != "" ? " hascomment" : ""));

	        new Insertion.Bottom(o, template);

	        Sortable.create('NotebookGalleryImages', { ghosting: false, constraint: true, hoverclass: 'over',
	            onChange: function(element) {
	                Notebook.Reorder();
	            }
	        });

	        Effect.Appear($("NotebookGalleryImage_" + GalleryImageId));
	        $("NotebookGalleryImage_" + GalleryImageId).observe("mouseover", function() { $("NotebookGalleryImage_" + GalleryImageId).addClassName("itemhover"); });
	        $("NotebookGalleryImage_" + GalleryImageId).observe("mouseout", function() { $("NotebookGalleryImage_" + GalleryImageId).removeClassName("itemhover"); });
	    },

	    RemoveProductThumb: function(productId) {
	        $("NotebookProduct_" + productId).hide();
	    },

	    RemoveGalleryImageThumb: function(galleryImageId) {
	        $("NotebookGalleryImage_" + galleryImageId).hide();
	    },

	    ViewProduct: function(productId) {
	        if ($("ProductsWrap")) {
	            /* we are in the collection page */
	            Products.View(productId, true);
	            Notebook.Close();
	        } else {
	            top.location = "Product.aspx?ProductId=" + productId;
	        }

	    },

	    ViewGalleryImage: function(galleryImageId) {
	        var galleryImage = Galleries.GetGalleryImageById(galleryImageId);
	        Notebook.ViewImage(SiteParams.UploadFolder + galleryImage.LargeImage, galleryImage.Description, galleryImage.LargeWidth);
	    },

	    ViewAGalleryImage: function(galleryImageId) {
	        var galleryImage = AGalleries.GetGalleryImageById(galleryImageId.toString().replace("a", ""));
	        Notebook.ViewImage(SiteParams.UploadFolder + galleryImage.ImageLarge, galleryImage.ImageLargeAlt);
	    },

	    ViewImage: function(imgSrc, imgAlt, imgWidth) {
	        var o = $("NotebookImageLightboxImg");
	        o.src = imgSrc;
	        o.alt = imgAlt;
	        $('lightbox-wrapper').appendChild($('NotebookImageLightbox'));
	        Notebook.OpenLightbox();
	        if (imgWidth > 0) {
	            var left = (620 - imgWidth) / 2;
	            $('NotebookImageLightbox').style.left = left + "px";
	            $('NotebookImageLightbox').style.width = imgWidth + "px";
	        } else {
	            $('NotebookImageLightbox').style.left = "-10px";
	            $('NotebookImageLightbox').style.width = "633px";
	        }
	    },

	    CloseImage: function() {
	        Notebook.CloseLightbox();
	        $("NotebookWrap").appendChild($("NotebookImageLightbox"));
	    },

	    DeleteProduct: function(productId) {
	        if (Membership.IsLogged() && confirm("Are you sure you wish to delete this product?")) {
	            Mantis.CaesarStone.Pages.NotebookControl.RemoveProduct(
		            productId,
		            AjaxProOutput(
			            function(serverResponse) {
			            }
		            )
	            );
	            Notebook.addedProducts.splice(Notebook.addedProducts.indexOf(productId), 1);
	            Notebook.RemoveProductThumb(productId);
	        }
	    },

	    DeleteGalleryImage: function(galleryImageId) {
	        if (Membership.IsLogged() && confirm("Are you sure you wish to delete this image?")) {
	            Mantis.CaesarStone.Pages.NotebookControl.RemoveGalleryImage(
		            galleryImageId,
		            AjaxProOutput(
			            function(serverResponse) {
			            }
		            )
	            );
	            Notebook.addedGalleryImages.splice(Notebook.addedGalleryImages.indexOf(galleryImageId), 1);
	            Notebook.RemoveGalleryImageThumb(galleryImageId);
	        }
	    },

	    CloseProduct: function(productId) {
	        Effect.Fade("ProductImage_" + productId, { afterFinish: function() { $("ProductImage_" + productId).hide() } });
	    },

	    Reorder: function() {
	        Notebook.addedProducts = [];
	        Notebook.addedGalleryImages = [];
	        for (var c = 0; c < $("NotebookProducts").childElements().length; c++) {
	            Notebook.addedProducts.push($("NotebookProducts").childElements()[c].id.split("_")[1]);
	        }
	        for (var c = 0; c < $("NotebookGalleryImages").childElements().length; c++) {
	            Notebook.addedGalleryImages.push($("NotebookGalleryImages").childElements()[c].id.split("_")[1]);
	        }
	        Notebook.UpdateServer();
	    },
	    UpdateServer: function() {
	        var comments = new Array();
	        for (var c = 0; c < Notebook.addedProducts.length; c++) {
	            if ($("NotebookProduct_" + c)) comments.push($("NotebookProductComment" + c).value);
	        }
	        Mantis.CaesarStone.Pages.NotebookControl.SynchProducts(
                Notebook.addedProducts,
                comments,
                AjaxProOutput(
                    function(serverResponse) {
                        var comments = new Array();
                        for (var c = 0; c < Notebook.addedGalleryImages.length; c++) {
                            if ($("NotebookGalleryImage_" + c)) comments.push($("NotebookGalleryImageComment" + c).value);
                        }
                        Mantis.CaesarStone.Pages.NotebookControl.SynchGalleryImages(
                            Notebook.addedGalleryImages,
                            comments,
                            AjaxProOutput(
                                function(serverResponse) {
                                }
                            )
                        );
                    }
                )
            );
	    },

	    SyncToServer: function() {
	        if (Membership.IsLogged()) {
	            Mantis.CaesarStone.Pages.NotebookControl.GetProducts(
                    AjaxProOutput(
                        function(serverResponse) {
                            var products = serverResponse;
                            for (var c = 0; c < products.length; c++) {
                                Notebook.AddProduct(products[c]);
                            }
                            Mantis.CaesarStone.Pages.NotebookControl.GetGalleryImages(
                                AjaxProOutput(
                                    function(serverResponse) {
                                        var images = serverResponse;
                                        for (var c = 0; c < images.length; c++) {
                                            if (images[c].EntityType == 1) {
                                                Notebook.AddGalleryImage(images[c]);
                                            } else {
                                                Notebook.AddAGalleryImage(images[c]);
                                            }
                                        }
                                        Notebook.UpdateServer();
                                    }
                                )
                            );
                        }
                    )
                );
	        }
	    },
	    PrintProduct: function(productId) {
	        if (!Membership.IsLogged()) return;

	        var products = new Array();
	        var gallery = new Array();
	        products.push(productId);
	        Mantis.CaesarStone.Pages.NotebookControl.PrintSelected(
                products,
                gallery,
                AjaxProOutput(
                    function(serverResponse) {
                        window.open("http://" + location.host + serverResponse);
                    }
                )
            );
	    },
	    PrintGalleryImage: function(galleryImageId) {
	        if (!Membership.IsLogged()) return;

	        var products = new Array();
	        var gallery = new Array();
	        gallery.push(galleryImageId);
	        Mantis.CaesarStone.Pages.NotebookControl.PrintSelected(
                products,
                gallery,
                AjaxProOutput(
                    function(serverResponse) {
                        window.open("http://" + location.host + serverResponse);
                    }
                )
            );
	    },
	    PrintSelected: function() {
	        if (!Membership.IsLogged()) return;

	        var products = new Array();
	        var gallery = new Array();
	        if ($("aspnetForm").NotebookProduct != null) {
	            if (typeof ($("aspnetForm").NotebookProduct.checked) != "undefined") {
	                if ($("aspnetForm").NotebookProduct.checked) products.push($("aspnetForm").NotebookProduct.value);
	            }
	            else {
	                for (var c = 0; c < $("aspnetForm").NotebookProduct.length; c++) {
	                    if ($("aspnetForm").NotebookProduct[c].checked) products.push($("aspnetForm").NotebookProduct[c].value);
	                }
	            }
	        }
	        if ($("aspnetForm").NotebookGalleryImage != null) {
	            if (typeof ($("aspnetForm").NotebookGalleryImage.checked) != "undefined") {
	                if ($("aspnetForm").NotebookGalleryImage.checked) gallery.push($("aspnetForm").NotebookGalleryImage.value);
	            }
	            else {
	                for (var c = 0; c < $("aspnetForm").NotebookGalleryImage.length; c++) {
	                    if ($("aspnetForm").NotebookGalleryImage[c].checked) gallery.push($("aspnetForm").NotebookGalleryImage[c].value);
	                }
	            }
	        }
	        if (products.length + gallery.length > 0) {
	            Mantis.CaesarStone.Pages.NotebookControl.PrintSelected(
                    products,
                    gallery,
                    AjaxProOutput(
                        function(serverResponse) {
                            window.open("http://" + location.host + serverResponse);
                        }
                    )
                );
	        }
	    },
	    SendProduct: function(productId) {
	        if ($("aspnetForm").NotebookProduct != null) {
	            if (typeof ($("aspnetForm").NotebookProduct.checked) != "undefined") {
	                if ($("aspnetForm").NotebookProduct.value == productId) $("aspnetForm").NotebookProduct.checked = true;
	                else $("aspnetForm").NotebookProduct.checked = false;
	            }
	            else {
	                for (var c = 0; c < $("aspnetForm").NotebookProduct.length; c++) {
	                    if ($("aspnetForm").NotebookProduct[c].value == productId) $("aspnetForm").NotebookProduct[c].checked = true;
	                    else $("aspnetForm").NotebookProduct[c].checked = false;
	                }
	            }
	        }
	        if ($("aspnetForm").NotebookGalleryImage != null) {
	            if (typeof ($("aspnetForm").NotebookGalleryImage.checked) != "undefined") {
	                $("aspnetForm").NotebookGalleryImage.checked = false;
	            }
	            else {
	                for (var c = 0; c < $("aspnetForm").NotebookGalleryImage.length; c++) {
	                    $("aspnetForm").NotebookGalleryImage[c].checked = false;
	                }
	            }
	        }
	        Notebook.SendSelected();
	    },
	    SendGalleryImage: function(galleryImageId) {
	        if ($("aspnetForm").NotebookProduct != null) {
	            if (typeof ($("aspnetForm").NotebookProduct.checked) != "undefined") {
	                $("aspnetForm").NotebookProduct.checked = false;
	            }
	            else {
	                for (var c = 0; c < $("aspnetForm").NotebookProduct.length; c++) {
	                    $("aspnetForm").NotebookProduct[c].checked = false;
	                }
	            }
	        }
	        if ($("aspnetForm").NotebookGalleryImage != null) {
	            if (typeof ($("aspnetForm").NotebookGalleryImage.checked) != "undefined") {
	                if ($("aspnetForm").NotebookGalleryImage.value == galleryImageId) $("aspnetForm").NotebookGalleryImage.checked = true;
	                else $("aspnetForm").NotebookGalleryImage.checked = false;
	            }
	            else {
	                for (var c = 0; c < $("aspnetForm").NotebookGalleryImage.length; c++) {
	                    if ($("aspnetForm").NotebookGalleryImage[c].value == galleryImageId) $("aspnetForm").NotebookGalleryImage[c].checked = true;
	                    else $("aspnetForm").NotebookGalleryImage[c].checked = false;
	                }
	            }
	        }
	        Notebook.SendSelected();
	    },
	    SendSelected: function() {
	        if (!Membership.IsLogged()) return;
	        $('lightbox-wrapper').appendChild($('NotebookSendLightbox'));
	        $('SendLightboxCommentBox').value = "";
	        $('NotebookSendLightboxInputs').innerHTML = '<input type="text" name="SendLightboxEmail_0" id="SendLightboxEmail_0" />';
	        Notebook.OpenLightbox();
	    },
	    SendSelectedSubmit: function() {
	        var emails = new Array();
	        var products = new Array();
	        var gallery = new Array();
	        var d = 0;
	        var validRegExp = /^[^@]+@[^@]+.[a-z]{2,}$/i;
	        while ($("SendLightboxEmail_" + d) != null) {
	            if ($("SendLightboxEmail_" + d).value.search(validRegExp) != -1) {
	                emails.push($("SendLightboxEmail_" + d).value);
	            }
	            d++;
	        }
	        if (emails.length == 0) return;
	        if ($("aspnetForm").NotebookProduct != null) {
	            if (typeof ($("aspnetForm").NotebookProduct.checked) != "undefined") {
	                if ($("aspnetForm").NotebookProduct.checked) products.push($("aspnetForm").NotebookProduct.value);
	            }
	            else {
	                for (var c = 0; c < $("aspnetForm").NotebookProduct.length; c++) {
	                    if ($("aspnetForm").NotebookProduct[c].checked) products.push($("aspnetForm").NotebookProduct[c].value);
	                }
	            }
	        }
	        if ($("aspnetForm").NotebookGalleryImage != null) {
	            if (typeof ($("aspnetForm").NotebookGalleryImage.checked) != "undefined") {
	                if ($("aspnetForm").NotebookGalleryImage.checked) gallery.push($("aspnetForm").NotebookGalleryImage.value);
	            }
	            else {
	                for (var c = 0; c < $("aspnetForm").NotebookGalleryImage.length; c++) {
	                    if ($("aspnetForm").NotebookGalleryImage[c].checked) gallery.push($("aspnetForm").NotebookGalleryImage[c].value);
	                }
	            }
	        }
	        Mantis.CaesarStone.Pages.NotebookControl.SendSelected(
                emails,
                $("SendLightboxCommentBox").value,
                products,
                gallery,
                AjaxProOutput(
                    function(serverResponse) {
                    }
                )
            );
	        Notebook.SendSelectedClose();
	    },
	    SendSelectedClose: function() {
	        $("NotebookWrap").appendChild($("NotebookSendLightbox"));
	        Notebook.CloseLightbox();
	    },
	    CommentSelected: function() {
	        if (!Membership.IsLogged()) return;
	        Mantis.CaesarStone.Pages.NotebookControl.GetComment(
                AjaxProOutput(function(serverResponse) {
                    $("CommentLightboxCommentBox").value = serverResponse;
                    $('lightbox-wrapper').appendChild($('NotebookCommentLightbox'));
                    Notebook.OpenLightbox();
                })
            );
	    },
	    CommentSubmit: function() {
	        Mantis.CaesarStone.Pages.NotebookControl.SaveComment($("CommentLightboxCommentBox").value,
                AjaxProOutput(function(serverResponse) {
                    Notebook.CommentClose();
                })
            );
	    },
	    CommentClose: function() {
	        $("NotebookWrap").appendChild($("NotebookCommentLightbox"));
	        Notebook.CloseLightbox();
	    },
	    AddEmail: function() {
	        var c = 0;
	        while ($("SendLightboxEmail_" + c) != null) { c++; }
	        var input = "<input type=\"text\" name=\"SendLightboxEmail_" + c + "\" id=\"SendLightboxEmail_" + c + "\" />";
	        new Insertion.Bottom($("NotebookSendLightboxInputs"), input);
	    },
	    OpenLightbox: function() {
	        if (Notebook.state == 2) Notebook.afterLBClose = true;
	        Notebook.Close();
	        initialize($("lightboxAll"));
	    },
	    CloseLightbox: function() {
	        if (Notebook.afterLBClose) Notebook.Open();
	        closeLightBox();
	        Notebook.afterLBClose = false;
	    }
	}
