var litium = {};
litium.shoppingCartElementId = null;
litium.shoppingCartUpdatePanelPostBackScript = null;
litium.productCatalogLanguageId = null;

litium.templates = {
    common: (function() {
        var buybutton = {};
        
        buybutton.init = function() {
        
            $(function() {
				if(navigator.userAgent.indexOf("MSIE 7") == -1)
				{
					// Add event listener to when update panels are updated
					var prm = Sys.WebForms.PageRequestManager.getInstance();
					
					function EndRequest(sender,args) {
						var source = $(sender._postBackSettings.sourceElement);
						if(source.attr("id").indexOf("m_linkButtonReload") != -1)
						{
							// Cart reloaded when product was added to the cart
							// Toggle classes on links and stuff before slide open
							$("#shoppingcart").toggleClass("open").css("cursor", "pointer");
							$("#cart").toggleClass("active").css("cursor", "pointer");
							
							// Slide open
							$('#quickcart').slideToggle('normal', function() { 
								setTimeout(function() { 
									// Start slide close
									$('#quickcart').slideToggle('normal', function() {
										// Toggle classes when closed
										$("#shoppingcart").toggleClass("open").css("cursor", "pointer");
										$("#cart").toggleClass("active").css("cursor", "pointer");
									}); 
									
								}, 2000);
							});
						}
						else if(source.attr("id").indexOf("m_removeButton") != -1)
						{
							// Cart was reloaded when product was deleted
							$("#shoppingcart").toggleClass("open").css("cursor", "pointer");
							$("#cart").toggleClass("active").css("cursor", "pointer");
							$('#quickcart').show();
						}
					}
					prm.add_endRequest(EndRequest);

					$(".productbuybutton").each(
						function()
						{
							var parent = $(this).parent();
							var clone = $(this).clone();
							clone.removeAttr("onclick");
							clone.removeAttr("id");
							clone.removeAttr("name");
							parent.append(clone);
							$(this).hide();
							
							clone.click(function(e) {
								
								var parentTr = $(this).parents(".productItem");
								var productId = parentTr.attr("id");
								var quantityField = parentTr.find(".productquantity");
								var quantity = quantityField.length == 0 ? 1 : quantityField.val();
								
								App_Code.Vedum.ECommerce.WebServices.ECommerceService.AddProductToCart(productId, quantity, litium.productCatalogLanguageId,
									function(result)
									{
										if(result.Successful && litium.shoppingCartUpdatePanelPostBackScript != null)
										{
											eval(litium.shoppingCartUpdatePanelPostBackScript);
										}
									}
								);
			                    
								return false;
							})
						}
					);
				}
                //$(".productbuybutton")
            });
        }
        return buybutton;
    })(),
    smallShoppingCart: (function() {
        var cart = {};
        cart.init = function() {
            $(function() {

                $("#shoppingcart").click(function(e) {
                    if (e.target.nodeName !== "A") {
                        var cart = $("#shoppingcart .cart");
                        cart.slideToggle("fast");
                        $(this).toggleClass("open");
                    }
                }).css("cursor", "pointer");
            });
        };
        
        cart.toggleSequence = function()
        {
			$(function() {
				$('#quickcart').slideToggle('fast', function() { 
					$('#quickcart').slideToggle('fast', function() {});
				});
			});
        };
        
        return cart;
    })(),

    loginPanel: (function() {
        var panel = {};
        panel.init = function() {
            $(function() {
                var isVisible = $('div#forgotten-password-panel > input:hidden').val() == 'True'
                if (isVisible)
                    $('div#forgotten-password-panel').show();
                else
                    $('div#forgotten-password-panel').hide();

                $('a[id$=c_forgottenPasswordLink]').click(function() {
                    var fpv = $('div#forgotten-password-panel > input:hidden');
                    var fpvValue = fpv.val();

                    $('div#forgotten-password-panel').slideToggle("fast");

                    if (fpvValue == 'False')
                    { fpv.val('True'); $('input[id$=c_textBoxEmail]').focus(); }
                    else
                    { fpv.val('False'); $('input[id$=c_textBoxLoginName]').focus(); }

                    return false;
                });
            });
        }
        return panel;
    })(),

    include: (function() {
        var self = {};
        var defaults = { iframeHeight: '600px' };

        self.resize = function(iframe, iframeHeight) {
            try {
                iframe.style.height = iframe.contentWindow.document.body.scrollHeight + 'px';
            } catch (e) {
                iframe.style.height = iframeHeight;
            }
        };

        self.init = function(options) {
            ///<summary>
            /// Initialises the resizing of iframes in the document.
            /// Parameters: options: { iframeHeight: height declaration, e.g. '600px' }
            ///</summary>
            var i;
            var iframe;
            var iframes = document.getElementsByTagName('iframe');
            var settings = jQuery.extend({}, defaults, options);
            var onloadValue = 'litium.templates.include.resize(this, "' + settings.iframeHeight + '");'

            for (i = 0; iframe = iframes[i]; i++) {
                iframe.setAttribute('onload', onloadValue);
            }
        };

        return self;
    })(),

    orderhistory: (function() {
        var self = {};

        var showOrder = function() {
            var orderId = document.location.hash;
            if (orderId.length > 1) {
                var $order = $(document.location.hash);
                if ($order.length) {
                    $(document.location.hash).show();
                    window.self.location = orderId;
                }
            }
        };

        self.init = function(options) {
            ///<summary>
            /// Initialises the links to order details.
            ///</summary>
            $(document).ready(function() {
                $('ul.orders > li').hide();
                showOrder();

                $('table.ordermenu > tbody > tr').each(function() {
                    var orderHref = $(this).find("> td:first a").attr('href');
                    $(this).children("td").click(function() {
                        $('ul.orders > li:visible').hide();

                        //Set selected class to clicked row
                        $('table.ordermenu > tbody > tr').each(function() {
                            $(this).removeClass("selected");
                        });
                        $(this).parent().addClass("selected");
                        
                        $(orderHref).show();
                        document.location = orderHref;
                    });
                });
            });
        };

        return self;
    })()
};

