/// <reference path="http://ajax.microsoft.com/ajax/jQuery/jquery-1.6-vsdoc.js" />

var $j = jQuery.noConflict();

$j(document).ready(function () {

	$j("a[rel*=external], a.external").live("click", function () {
		window.open(this.href);
		return false;
	});

	// carrousel initialization
	if ($j(".pushbox").length) {
		$j(".pushbox").carrousel("start", {
			imageWidth: $j(".pushbox").width()
		});

		$j(".innerPushbox").click(function () {
			var href = $j(".button", this).first().attr("href");
			if (href != null) {
				window.location = href;
			}
		});
	}

	/*******
	** vp -print - (c) - 0.0.1 **	
	*******/
	$j('.print').css('display', 'block').find('a').click(function (event) {
		window.open($j(this).attr('href'));
		event.preventDefault();
	});

	/*******
	** vp - var img rotator - (c) - 0.0.1 **	
	*******/
	if ((typeof bg_img != "undefined") && ($j('.homepage').length)) {
		if (bg_img.length) {
			var ln = Math.floor(Math.random() * bg_img.length);

			$j('#wrapper').addClass('loading');
			$j('#main').addClass('loading');

			$j('#wrapper').css('background-image', 'url(' + bg_img[ln].right + ')').ready(function () {
				$j('#wrapper').removeClass('loading');
			});
			$j('#main').css('background-image', 'url(' + bg_img[ln].left + ')').ready(function () {
				$j('#main').removeClass('loading');
			});
		}
	}

	/*******
	** vp - mainNav - (c) - 0.0.1 **	
	*******/
	if ($j('.mainNav li.parent').length > 0) {
		$j('.mainNav li.parent').each(function () {
			var root = $j(this);
			root.mouseover(function (event) {
				$j(this).find('.foldout').show();
			});
			root.mouseout(function (event) {
				$j(this).find('.foldout').hide();
			});
		});
	};

	/*******
	** vp - Show extra - (c) - 0.0.1 **	
	*******/
	if ($j('.horizontalList h2 a').length > 0) {
		$j('.horizontalList h2 a').each(function () {
			var root = $j(this);
			root.click(function (event) {
				if ($j(this).text().toLowerCase().indexOf('toon alles') != -1) {
					$j(this).empty().append('<span>Verberg de extra rijen</span>');
				}
				else {
					$j(this).empty().append('<span>Toon alles</span>');
				}
				$j(this).parent().parent().find('ul').not(':first').slideToggle('slow', function () { });
				event.preventDefault();
			});
		});
	};

	initCookieFunctions();
});

function PrintPage() {
	$j(document).ready(function () {
		window.print();
	});
}

String.prototype.trim = function () {
	return this.replace(/^\s+|\s+$/g, "");
}

function initCookieFunctions() {

	var cookieProperties = { expires: 365, path: '/' };

	/********
	* Mijn Stoffen:
	********/
	if ($j('div.productPage, div.myFabrics').length) {
		var myfabrics = getMyFabrics();

		if ($j.inArray($j('#pageRef').html(), myfabrics) == -1) {
			$j('#hplSave').parent().show();
		} else {
			$j('#liSaved').show();
		}

		$j("#hplSave").click(function (e) {
			var myfabrics = getMyFabrics();
			var product = $j("#pageRef").text();

			if ($j.inArray(product, myfabrics) == -1) {
				if (myfabrics.length) {
					myfabrics.push(product);
					$j.cookie("myfabrics", myfabrics, cookieProperties);
				} else {
					$j.cookie("myfabrics", product, cookieProperties);
				}
				setMyFabricCount();
			}

			$j(this).parent().hide();
			$j('#liSaved').show();
			e.preventDefault();
		});

		$j("div.myFabrics a.remove").click(function (e) {
			var href = $j(this).attr("href");
			var product = href.substr(1, href.length);

			var myfabrics = getMyFabrics();

			if ($j.inArray(product, myfabrics) > -1) {
				$j.cookie("myfabrics", $j.grep(myfabrics, function (value) {
					return value != product;
				}).join(","), cookieProperties);
			}

			$j(this).parent("div").remove();

			setMyFabricCount();
			e.preventDefault();
		});
	}

	setMyFabricCount();

	function setMyFabricCount() {
		var myfabrics = getMyFabrics();

		$j('#hplMyFabrics span').html(myfabrics.length);
	}

	function getMyFabrics() {
		var fabrics = $j.cookie("myfabrics");
		return ($j.isEmptyObject(fabrics) || fabrics === "") ? [] : fabrics.trim().split(",");
	}

	/********
	* Stalenhangers:
	********/
	if ($j("div#content.exampleOrderPage").length) {
		$j("#btnOrder").click(function () {
			var orders = getOrders();

			return orders.length > 0;
		});

		$j("div.innerPushbox a.add").click(function (event) {
			var orders = getOrders();
			var product = $j(this).prev("h2").text();

			if ($j.inArray(product, orders) == -1) {
				if (orders.length) {
					orders.push(product);
					$j.cookie("orders", orders, cookieProperties);
				} else {
					$j.cookie("orders", product, cookieProperties);
				}

				// add html:
				var orderul = $j("ul#exampleOrder");
				var html = '<li><a href="javascript:void(0);"><span>' + product + '</span></a><a href="#" class="delete">' + orderul.attr("title") + '</a></li>';
				orderul.append(html);
			}

			return false;
		});

		$j("ul#exampleOrder a.delete").live("click", function () {
			var orders = getOrders();
			var product = $j(this).parent("li").find("span").text();

			if ($j.inArray(product, orders) > -1) {
				$j.cookie("orders", $j.grep(orders, function (value) {
					return value != product;
				}).join(","), cookieProperties);
			}

			// remove html:
			$j(this).parent("li").remove();

			return false;
		});

		function getOrders() {
			var orders = $j.cookie("orders");
			return ($j.isEmptyObject(orders) || orders === "") ? [] : orders.trim().split(",");
		}
	}
}

