/* A TARGET */
(function($) {
  $.fn.target = function() {
    return this.each(function(index, elm) {
      if (substr($(this).attr("class"), 0, 7) == 'target-') {
        $(this).attr("target", substr($(this).attr("class"), 7));
      }
    });
  }
})(jQuery);

$(document).ready(function() {
  $("a").target();

  $("ul#menuProductCategory").children("li").children("a").each(function() {
  	$(this).bind("mouseover mouseout", function(e) {
  		if (e.type == "mouseover") {
  			$(this).prev("span").css("visibility", "visible");
  			$(this).next("span").css("visibility", "visible");
  		} else {
  			$(this).prev("span").css("visibility", "hidden");
  			$(this).next("span").css("visibility", "hidden");
  		}
  	});
  });

	$("div.column1").children("ul").children("li.parent").each(function(i) {
		if ((i + 1) < 8) {
			if ($(":hidden[name='webshopPart']").val() == 8) {
				var part = 2;
			} else {
				var part = (($(":hidden[name='webshopPart']").val() > 3)?3:$(":hidden[name='webshopPart']").val());
			}
			
			$(this).children("a").css({
				backgroundImage: 'url(/etc/sites/liv-inn.nl/images/' + part + '/sub_menu_'+ (i + 1) +'.jpg)'
			});
		}
	});
	
	$("div.column1").children("ul").children("li.child:last-child").css({
		borderBottom: "none",
		paddingBottom: "3px"
	});
	
	$("div.column1").children("ul").children("li.parent").each(function(i) {
		$(this).click(function() {
			$(this).parent().next("ul#childContainer").toggle();
			
			if ($("div.column1").children("ul").children("li.parent").children("a").eq(0).attr("href") == "#filter") {
				return false;
			}
		});
	});
	
	$("div.shoppingCartInfo").children("div").eq(0).bind("click", function() {
		showShoppingCart();
	});
	
	function showShoppingCart() {
		tb_show('Winkelwagen', '/cart/?KeepThis=true&TB_iframe=true&height=400&width=800');
	}
	
	$("input[name='search_value']").bind("keyup", function(e) {
		if (e.keyCode == 13 && $(this).val().length > 0) {
			window.location.href = '/search/?searchString=' + $(this).val();
		}
	});
	
	$("input[name='search_value']").siblings("a").eq(0).bind("click", function() {
		if ($("input[name='search_value']").val().length > 0) {
			window.location.href = '/search/?searchString=' + $("input[name='search_value']").val();
		}
	});
	
});


jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        // CAUTION: Needed to parenthesize options.path and options.domain
        // in the following expressions, otherwise they evaluate to undefined
        // in the packed version for some reason...
        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};
