﻿var liteweb = {
	vroot: "",
	_pageInit: [],
	_SearchMsg: "Search Site...",
	_Newsletter: "Email address here...",
	AppendInit: function(obj) {
		liteweb._pageInit.push(obj);
	},
	init: function(vroot) {
		liteweb.vroot = vroot;
		$(".brands").bind("change", function() {
			var c = $("#categories")[0];
			while (c.options.length > 1)
				c.options[1] = null;
			c.options[0].text = "Please wait...";
			$.post(liteweb.vroot + "/_Ajax/GetCategoriesByBrand.ashx",
				{ brandId: this.value },
				function(data, status) {
					var c = $("#categories")[0];
					var cats = eval(data);
					if (typeof cats == "object" && cats.length > 0) {
						for (var i = 0; i < cats.length; i++) {
							c.options.length++;
							var len = c.options.length - 1;
							c.options[len].value = cats[i][0];
							c.options[len].text = cats[i][1];
						}
					}
					c.options[0].text = cats.length > 0 ? "Select..." : "No Categories Found";
				},
				"script"
			);
		});
		liteweb._init();
	},
	_init: function() {
		for (var i = 0; i < liteweb._pageInit.length; i++)
			if ("function" == typeof liteweb._pageInit[i])
			liteweb._pageInit[i]();
		//liteweb._initSiteSearch();
	},
	CheckSiteSearch: function(f, ac) {
		if ("focus" == ac && f.value.Trim() == liteweb._SearchMsg) {
			f.value = '';
			f.style.color = f.oldColor;
		}
		if ("blur" == ac && f.value.Trim() == '') {
			f.oldColor = f.style.color;
			f.value = liteweb._SearchMsg;
			f.style.color = "#a0a0a0";
		}
	},
	CheckNewsletter: function(f, ac) {
		if ("focus" == ac && f.value.Trim() == liteweb._Newsletter) {
			f.value = '';
			f.style.color = f.oldColor;
		}
		if ("blur" == ac && f.value.Trim() == '') {
			f.oldColor = f.style.color;
			f.value = liteweb._Newsletter;
			f.style.color = "#a0a0a0";
		}
	},
	_initSiteSearch: function() {
		var sq = $("input[name=q]");
		sq.bind("focus", function() {
			liteweb.CheckSiteSearch(this, 'focus');
		});
		sq.bind("blur", function() {
			liteweb.CheckSiteSearch(this, 'blur');
		});
		sq[0].value = "";
		liteweb.CheckSiteSearch(sq[0], 'blur');
		var sf = document.forms["SearchForm"];
		sf.onsubmit = function() {
			var err = sq[0].value.Trim() == liteweb._SearchMsg ||
				sq[0].value.Trim() == "";
			if (err) {
				alert("Please enter your search querry!");
				sq[0].focus();
				return false;
			}

		}
	},
	AutoHide: function(id, effect) {
		effect = (effect || "fade").toLowerCase();
		var timeoutFunction;
		if (effect == "fade")
			$("#" + id).fadeOut();
		else
			$("#" + id).slideUp();
	},
	_initNewsletter: function() {
		var sq = $("input[name=newsletter-email]");
		sq.bind("focus", function() {
			liteweb.CheckNewsletter(this, 'focus');
		});
		sq.bind("blur", function() {
			liteweb.CheckNewsletter(this, 'blur');
		});
		sq[0].value = "";
		liteweb.CheckNewsletter(sq[0], 'blur');
		var sf = document.forms["Newsletter"];
		sf.onsubmit = function() {
			var err = sq[0].value.Trim() == liteweb._Newsletter ||
				sq[0].value.Trim() == "";
			if (err) {
				alert("Please enter your email address!");
				sq[0].focus();
				return false;
			}
			err = liteweb_Val.ValEmail(sq[0].value);
			if (!err) {
				alert("Please enter a correct email address!");
				sq[0].focus();
				return false;
			}
			return true;
		}
	}
};

liteweb.AppendInit(function() {
	liteweb.dur = 650;
	var lastClicked = null;
	var mainArea = $(".main-area");
	var lef = $(".left");
	$(".shop-content ul li a").click(function() {

		var $this = $(this.parentNode);
		var ul = $this.children("ul");
		var children = ul.children("li");
		if (children.length == 0)
			return true;
		if (ul.css("display") == "none") {
			if (lastClicked && lastClicked != this) {
				$(lastClicked).click();
			}
			lastClicked = this;
			

			if (!this.__height)
				this.__height = ul.height();

			ul.css({
				"display": "block",
				"height": 0
			});
			ul.animate({ height: this.__height }, { duration: liteweb.dur / 2, complete: function() {
				this.__height = ul.outerHeight();
			} 
			});
		} else {
			if (lastClicked == this)
				lastClicked = null;
			ul.animate({ height: 0 }, { duration: liteweb.dur / 2,
				complete: function() {
					ul.css("display", "none");
				}
			});
		}
		var span = $(this).children("a span.p");
		span.html(span.html().trim() == "+" ? "- " : "+ ");
		return false;
	});
});

$.fn.infiniteCarousel = function() {
	function repeat(str, num) {
		return new Array(num + 1).join(str);
	}
	return this.each(function() {
		var $wrapper = $('> div', this).css('overflow', 'hidden'),
            $slider = $wrapper.find('> ul'),
            $items = $slider.find('> li'),
            $single = $items.filter(':first'),

            singleWidth = $single.outerWidth(),
            visible = Math.ceil($wrapper.innerWidth() / singleWidth), // note: doesn't include padding or border
            currentPage = 1,
            pages = Math.ceil($items.length / visible);


		// 1. Pad so that 'visible' number will always be seen, otherwise create empty items
		if (($items.length % visible) != 0) {
			$slider.append(repeat('<li class="empty" />', visible - ($items.length % visible)));
			$items = $slider.find('> li');
		}

		// 2. Top and tail the list with 'visible' number of items, top has the last section, and tail has the first
		$items.filter(':first').before($items.slice(-visible).clone().addClass('cloned'));
		$items.filter(':last').after($items.slice(0, visible).clone().addClass('cloned'));
		$items = $slider.find('> li'); // reselect

		// 3. Set the left position to the first 'real' item
		$wrapper.scrollLeft(singleWidth * visible);

		// 4. paging function
		function gotoPage(page) {
			var dir = page < currentPage ? -1 : 1,
                n = Math.abs(currentPage - page),
                left = singleWidth * dir * visible * n;

			$wrapper.filter(':not(:animated)').animate({
				scrollLeft: '+=' + left
			}, {
				duration: 500,
				easing: "easeInQuart",
				complete:
				function() {
					if (page == 0) {
						$wrapper.scrollLeft(singleWidth * visible * pages);
						page = pages;
					} else if (page > pages) {
						$wrapper.scrollLeft(singleWidth * visible);
						// reset back to start position
						page = 1;
					}

					currentPage = page;
				}
			});

			return false;
		}

		$wrapper.after('<a class="arrow back">&lt;</a><a class="arrow forward">&gt;</a>');

		// 5. Bind to the forward and back buttons
		$('a.back', this).click(function() {
			return gotoPage(currentPage - 1);
		});

		$('a.forward', this).click(function() {
			return gotoPage(currentPage + 1);
		});

		// create a public interface to move to a specific page
		$(this).bind('goto', function(event, page) {
			gotoPage(page);
		});
	});
};
liteweb.AppendInit(function() {
	$(".product-list img").bind("mouseover", function() {
		var $this = $(this);
		this.showImage = true;
		var largeImage = $this.attr("rel");
		if (largeImage) {
			if (!this.$largeImage) {
				this.title = "";
				this.largeImage = document.createElement("img");
				this.largeImage.src = largeImage;
				this.$largeImage = $(this.largeImage);
				var offset = $this.offset();
				this.$largeImage.addClass("large-image");
				this.$largeImage.css({
					top: offset.top - 100,
					left: offset.left + 75,
					position: "absolute",
					display: "none"
				});
				$(this.parentNode).append(this.$largeImage);
				var pImage = this;
				this.$largeImage.bind("load", function() {
					$(this).css({
						top: offset.top - $(this).height() / 2,
						left: offset.left + $this.width() + 10
					});
					if (pImage.showImage)
						$(this).fadeIn(600);
				});
			}
			else {
				this.$largeImage.fadeIn(600);
			}
		}
	});
	$(".product-list img").bind("mouseout", function() {
		this.showImage = false;
		if (this.$largeImage)
			this.$largeImage.fadeOut();
	});
	var drymaxImages = $(".drymax-specs img");
	var len = drymaxImages.length;
	var currentDrymax = 0;
	function switchDrymaxImg() {
		var oldImg = currentDrymax;
		currentDrymax++;
		currentDrymax = currentDrymax == len ? 0 : currentDrymax;
		$(drymaxImages[oldImg]).fadeOut(1000);
		$(drymaxImages[currentDrymax]).fadeIn(1000);
	}
	if (len > 0)
		setInterval(switchDrymaxImg, 4000);
	$(".left-button").click(function() {
		var pNode = this.parentNode;
		while (pNode != null && pNode.tagName.toLowerCase() != "a")
			pNode = pNode.parentNode;
		if (pNode != null) {
			location.href = pNode.href;
		}
	});
});

