
// product_listing animations for variants
$(function() {
  $("a[class^='link_for_']").click(function() {
  	$("div[class^='" + $(this).attr('class').split(' ').slice(0,1) + "']").animate({paddingTop: '8px', opacity: 'hide'}, 300);
  	$("a[class^='" + $(this).attr('class') + "']").slideUp('fast');
    $("div[class^='"  + $(this).next("div[class^='variants_for_']").attr('class') +  "']").slideDown();
  });
});






// addtocart from product_listing (add only 1 article)
$(function() {
  $("#content a[class='addtocart']").click(function() {
		id = $(this).attr('href').split('products_id=').slice(1);
		$.ajax({ // call shopping_cart class
			type: "POST",
			contentType: "application/x-www-form-urlencoded; charset=iso-8859-1",
			url: 	"ajax_cart.php",
			data: "products_id=" + id +
						"&action=buy_now" +
						"&ajax=1",
			dataType: "json",
			success: function(json) {
				$.ajax({ // reload cart box and animate modifications
					type: "POST",
					contentType: "application/x-www-form-urlencoded; charset=iso-8859-1",
					url: 	"ajax_cart.php",
					data: "&action=update_cartbox",
          success: function(html) {
						$("#shopping_cartBox").html(html);
						$("span.article_count, span.zwischensumme").stop(true).animate({color: '#40bf00'}).animate({color: '#333'}, 1000);
					}
				});
			}
		});
		$(this).stop(true).animate({opacity: '0.3'},10).animate({opacity: '1'},500);
		$('#cart_message').stop(true).fadeIn().animate({opacity: '1'},2000).fadeOut();
		return false;
  });
});


// addtowishlist from product_listing (add only 1 article)
// $(function() {
//   $("#content a[class='addtowishlist']").click(function() {
// 		id = $(this).attr('href').split('products_id=').slice(1);
// 		$.ajax({ // call shopping_cart class
// 			type: "POST",
// 			contentType: "application/x-www-form-urlencoded; charset=iso-8859-1",
// 			url: 	"ajax_wishlist.php",
// 			data: "products_id=" + id +
// 						"&action=add_wishlist" +
// 						"&ajax=1",
// 			dataType: "json",
// 			success: function(json) {
// 			}
// 		});
// 		$(this).stop(true).animate({opacity: '0.3'},10).animate({opacity: '1'},500);
// 		$('#wishlist_message').stop(true).fadeIn().animate({opacity: '1'},2000).fadeOut();
// 		return false;
//   });
// });






// addtocart from product_info (with quantity and attributes)
$(function() {
  $("#content input[class='addtocart']").click(function() {

  	$(this).closest("tr").addClass('current');
  	$(this).closest("tr").prev().addClass('current_attr');

		id       = $("tr.current input[name='products_id']").attr('value');
		menge    = $("tr.current input[name='menge']").attr('value');
		attr_id  = $("tr.current_attr select[name^='id']").attr('name');
		attr_val = $("tr.current_attr select[name^='id']").attr('value');

		$.ajax({ // call shopping_cart class
			type: "POST",
			contentType: "application/x-www-form-urlencoded; charset=iso-8859-1",
			url: 	"ajax_cart.php",
			data: "products_id=" + id +
						"&menge=" + menge +
						"&" + attr_id + "=" + attr_val +
						"&action=add_product" +
						"&ajax=1",
			dataType: "json",
			success: function(html) {
				$.ajax({ // reload cart box and animate modifications
					type: "POST",
					contentType: "application/x-www-form-urlencoded; charset=iso-8859-1",
					url: 	"ajax_cart.php",
					data: "&action=update_cartbox",
          success: function(html) {
						$("#shopping_cartBox").html(html);
						$("span.article_count, span.zwischensumme").stop(true).animate({color: '#40bf00'}).animate({color: '#333'}, 1000);
					}
				});
			}
		});
		$(this).stop(true).animate({opacity: '0.3'},10).animate({opacity: '1'},500);
		$(this).closest("tr").removeClass('current');
		$(this).closest("tr").prev().removeClass('current_attr');
		$('#cart_message').stop(true).fadeIn().animate({opacity: '1'},2000).fadeOut();
		return false;
  });
});



// addtowishlist from product_info
$(function() {
  $("#content a[class='addtowishlist']").click(function() {

  	$(this).closest("tr").addClass('current');
  	$(this).closest("tr").prev().addClass('current_attr');

		id       = $("tr.current input[name='products_id']").attr('value');
		menge    = $("tr.current input[name='menge']").attr('value');
		attr_id  = $("tr.current_attr select[name^='id']").attr('name');
		attr_val = $("tr.current_attr select[name^='id']").attr('value');

		$.ajax({ // call shopping_cart class
			type: "POST",
			contentType: "application/x-www-form-urlencoded; charset=iso-8859-1",
			url: 	"ajax_wishlist.php",
			data: "products_id=" + id +
						"&menge=" + menge +
						//"&" + attr_id + "=" + attr_val +
						"&action=add_wishlist" +
						"&ajax=1",
			dataType: "json",
			success: function(html) {
// 				$.ajax({ // reload cart box and animate modifications
// 					type: "POST",
// 					contentType: "application/x-www-form-urlencoded; charset=iso-8859-1",
// 					url: 	"ajax_cart.php",
// 					data: "&action=update_cartbox",
//           success: function(html) {
// 						$("#shopping_cartBox").html(html);
// 						$("span.article_count, span.zwischensumme").stop(true).animate({color: '#40bf00'}).animate({color: '#333'}, 1000);
// 					}
// 				});
			}
		});
		$(this).stop(true).animate({opacity: '0.3'},10).animate({opacity: '1'},500);
		$(this).closest("tr").removeClass('current');
		$(this).closest("tr").prev().removeClass('current_attr');
		$('#wishlist_message').stop(true).fadeIn().animate({opacity: '1'},2000).fadeOut();
		return false;
  });
});







// quicksearch
$(function() {
  //$("#keywords2").focus();

	jQuery.fn.ajax_search = function() {
    var keywords              = $("#keywords2").attr('value');
		var onlyarticlenr         = $('#onlyarticlenr_box:checked').val();
		var search_in_description = $('#search_in_description_box:checked').val();
		var page_valid = arguments[0];
		var animate    = arguments[1];
		var page       = 1;
		if ( page_valid != '' ) page = page_valid;

		if ( keywords.length > 1 ) {
			$.ajax({
				type: "POST",
				contentType: "application/x-www-form-urlencoded; charset=iso-8859-1",
				url: 	"ajax_search.php",
				data: "keywords=" + keywords +
							"&search_in_description=" + search_in_description +
							"&onlyarticlenr=" + onlyarticlenr +
							"&page=" + page +
							"&ajax=1",
				success: function(html) {
					$('#quicksearch_box').html(html).fadeIn();
					$('#keywords2').css('background-image', 'none');
				}
			});
		}
	};

  $("#keywords2").keyup(function(e){
    var keywords = $(this).attr('value');
    if ( keywords.length > 1 ) {
      if (e.keyCode != 13 && e.keyCode != 35 && e.keyCode != 36 && e.keyCode != 37 && e.keyCode != 39 && e.keyCode != 116 ) {
    		$(this).css('background', '#fff url("images/loading-mini.gif") no-repeat 98% 50%');
		 	}
    }
    else {
      $('#keywords2').css('background-image', 'none');
    }
  });

	$("#keywords2").delay({
		delay: 200,
		event: 'keyup',
		fn: function(e){
			if (e.keyCode != 13 && e.keyCode != 35 && e.keyCode != 36 && e.keyCode != 37 && e.keyCode != 39 && e.keyCode != 116 ) {
				$(this).ajax_search();
			}
		}
	});

	$("#keywords2").click(function(){
		if (!$(this).hasClass('selected')) $(this).select().addClass('selected');
		if ($(this).attr('value').length > 1) $(this).ajax_search();
	});

	$("#search_in_description_box").click(function(){
		$(this).ajax_search();
	});

	$("#onlyarticlenr_box").click(function(){
		$(this).ajax_search();
	});

	$("#search_pages a").live("click",function(){
		var page = $(this).attr('href').split('page=').slice(1);
		$(this).addClass('current').ajax_search(page);
		return false;
	});

});





// color plugin
(function(jQuery){jQuery.each(["backgroundColor","borderBottomColor","borderLeftColor","borderRightColor","borderTopColor","color","outlineColor"],function(i,attr){jQuery.fx.step[attr]=function(fx){if(fx.state==0){fx.start=getColor(fx.elem,attr);fx.end=getRGB(fx.end)}fx.elem.style[attr]="rgb("+[Math.max(Math.min(parseInt((fx.pos*(fx.end[0]-fx.start[0]))+fx.start[0]),255),0),Math.max(Math.min(parseInt((fx.pos*(fx.end[1]-fx.start[1]))+fx.start[1]),255),0),Math.max(Math.min(parseInt((fx.pos*(fx.end[2]-fx.start[2]))+fx.start[2]),255),0)].join(",")+")"}});function getRGB(color){var result;if(color&&color.constructor==Array&&color.length==3){return color}if(result=/rgb\(\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*\)/.exec(color)){return[parseInt(result[1]),parseInt(result[2]),parseInt(result[3])]}if(result=/rgb\(\s*([0-9]+(?:\.[0-9]+)?)\%\s*,\s*([0-9]+(?:\.[0-9]+)?)\%\s*,\s*([0-9]+(?:\.[0-9]+)?)\%\s*\)/.exec(color)){return[parseFloat(result[1])*2.55,parseFloat(result[2])*2.55,parseFloat(result[3])*2.55]}if(result=/#([a-fA-F0-9]{2})([a-fA-F0-9]{2})([a-fA-F0-9]{2})/.exec(color)){return[parseInt(result[1],16),parseInt(result[2],16),parseInt(result[3],16)]}if(result=/#([a-fA-F0-9])([a-fA-F0-9])([a-fA-F0-9])/.exec(color)){return[parseInt(result[1]+result[1],16),parseInt(result[2]+result[2],16),parseInt(result[3]+result[3],16)]}return colors[jQuery.trim(color).toLowerCase()]}function getColor(elem,attr){var color;do{color=jQuery.curCSS(elem,attr);if(color!=""&&color!="transparent"||jQuery.nodeName(elem,"body")){break}attr="backgroundColor"}while(elem=elem.parentNode);return getRGB(color)}var colors={aqua:[0,255,255],azure:[240,255,255],beige:[245,245,220],black:[0,0,0],blue:[0,0,255],brown:[165,42,42],cyan:[0,255,255],darkblue:[0,0,139],darkcyan:[0,139,139],darkgrey:[169,169,169],darkgreen:[0,100,0],darkkhaki:[189,183,107],darkmagenta:[139,0,139],darkolivegreen:[85,107,47],darkorange:[255,140,0],darkorchid:[153,50,204],darkred:[139,0,0],darksalmon:[233,150,122],darkviolet:[148,0,211],fuchsia:[255,0,255],gold:[255,215,0],green:[0,128,0],indigo:[75,0,130],khaki:[240,230,140],lightblue:[173,216,230],lightcyan:[224,255,255],lightgreen:[144,238,144],lightgrey:[211,211,211],lightpink:[255,182,193],lightyellow:[255,255,224],lime:[0,255,0],magenta:[255,0,255],maroon:[128,0,0],navy:[0,0,128],olive:[128,128,0],orange:[255,165,0],pink:[255,192,203],purple:[128,0,128],violet:[128,0,128],red:[255,0,0],silver:[192,192,192],white:[255,255,255],yellow:[255,255,0]}})(jQuery);






// tabs - jQuery plugin for accessible, unobtrusive tabs by Klaus Hartl - http://stilbuero.de/tabs/
$.fn.tabs = function(options) {
    var ON_CLASS = 'on';
    var OFF_CLASS = 'tabs-hide';
    var on = options && options.on && (typeof options.on == 'number' && options.on > 0) ? options.on - 1 : 0;
    return this.each(function() {
        //$(this).find('>div').not(':eq(' + on + ')').addClass(OFF_CLASS);
        $(this).find('>ul>li:eq(' + on + ')').addClass(ON_CLASS);
        var container = this;
        $(this).find('>ul>li>a').click(function() {
            if (!$(this.parentNode).is('.' + ON_CLASS)) {
                var re = /([_\-\w]+$)/i;
                var target = $('#' + re.exec(this.href)[1]);
                if (target.size() > 0) {
                    $(container).find('>div:visible').addClass(OFF_CLASS);
                    target.removeClass(OFF_CLASS);
                    $(container).find('>ul>li').removeClass(ON_CLASS);
                    $(this.parentNode).addClass(ON_CLASS);
                }
            }
            return false;
        });
    });
};


/**
 * jQuery delayed event execution.
 */
$.fn.delay = function(options) {
    var timer;
    var delayImpl = function(eventObj) {
        if (timer != null) {
            clearTimeout(timer);
        }
        var newFn = function() {
            options.fn(eventObj);
        };
        timer = setTimeout(newFn, options.delay);
    }

    return this.each(function() {
        var obj = $(this);
        obj.bind(options.event, function(eventObj) {
             delayImpl(eventObj);
        });
    });
};




/*
highlight v3
Highlights arbitrary terms.
<http://johannburkard.de/blog/programming/javascript/highlight-javascript-text-higlighting-jquery-plugin.html>
MIT license.
Johann Burkard
*/
jQuery.fn.highlight=function(b){function a(e,j){var l=0;if(e.nodeType==3){var k=e.data.toUpperCase().indexOf(j);if(k>=0){var h=document.createElement("span");h.className="highlight";var f=e.splitText(k);var c=f.splitText(j.length);var d=f.cloneNode(true);h.appendChild(d);f.parentNode.replaceChild(h,f);l=1}}else{if(e.nodeType==1&&e.childNodes&&!/(script|style)/i.test(e.tagName)){for(var g=0;g<e.childNodes.length;++g){g+=a(e.childNodes[g],j)}}}return l}return this.each(function(){a(this,b.toUpperCase())})};jQuery.fn.removeHighlight=function(){return this.find("span.highlight").each(function(){this.parentNode.firstChild.nodeName;with(this.parentNode){replaceChild(this.firstChild,this);normalize()}}).end()};




/*
 * jQuery EasIng v1.1.2 - http://gsgd.co.uk/sandbox/jquery.easIng.php
 *
 * Uses the built In easIng capabilities added In jQuery 1.1
 * to offer multiple easIng options
 *
 * Copyright (c) 2007 George Smith
 * Licensed under the MIT License:
 *   http://www.opensource.org/licenses/mit-license.php
 */

// t: current time, b: begInnIng value, c: change In value, d: duration

jQuery.extend( jQuery.easing,
{
	easeInQuad: function (x, t, b, c, d) {
		return c*(t/=d)*t + b;
	},
	easeOutQuad: function (x, t, b, c, d) {
		return -c *(t/=d)*(t-2) + b;
	},
	easeInOutQuad: function (x, t, b, c, d) {
		if ((t/=d/2) < 1) return c/2*t*t + b;
		return -c/2 * ((--t)*(t-2) - 1) + b;
	},
	easeInCubic: function (x, t, b, c, d) {
		return c*(t/=d)*t*t + b;
	},
	easeOutCubic: function (x, t, b, c, d) {
		return c*((t=t/d-1)*t*t + 1) + b;
	},
	easeInOutCubic: function (x, t, b, c, d) {
		if ((t/=d/2) < 1) return c/2*t*t*t + b;
		return c/2*((t-=2)*t*t + 2) + b;
	},
	easeInQuart: function (x, t, b, c, d) {
		return c*(t/=d)*t*t*t + b;
	},
	easeOutQuart: function (x, t, b, c, d) {
		return -c * ((t=t/d-1)*t*t*t - 1) + b;
	},
	easeInOutQuart: function (x, t, b, c, d) {
		if ((t/=d/2) < 1) return c/2*t*t*t*t + b;
		return -c/2 * ((t-=2)*t*t*t - 2) + b;
	},
	easeInQuint: function (x, t, b, c, d) {
		return c*(t/=d)*t*t*t*t + b;
	},
	easeOutQuint: function (x, t, b, c, d) {
		return c*((t=t/d-1)*t*t*t*t + 1) + b;
	},
	easeInOutQuint: function (x, t, b, c, d) {
		if ((t/=d/2) < 1) return c/2*t*t*t*t*t + b;
		return c/2*((t-=2)*t*t*t*t + 2) + b;
	},
	easeInSine: function (x, t, b, c, d) {
		return -c * Math.cos(t/d * (Math.PI/2)) + c + b;
	},
	easeOutSine: function (x, t, b, c, d) {
		return c * Math.sin(t/d * (Math.PI/2)) + b;
	},
	easeInOutSine: function (x, t, b, c, d) {
		return -c/2 * (Math.cos(Math.PI*t/d) - 1) + b;
	},
	easeInExpo: function (x, t, b, c, d) {
		return (t==0) ? b : c * Math.pow(2, 10 * (t/d - 1)) + b;
	},
	easeOutExpo: function (x, t, b, c, d) {
		return (t==d) ? b+c : c * (-Math.pow(2, -10 * t/d) + 1) + b;
	},
	easeInOutExpo: function (x, t, b, c, d) {
		if (t==0) return b;
		if (t==d) return b+c;
		if ((t/=d/2) < 1) return c/2 * Math.pow(2, 10 * (t - 1)) + b;
		return c/2 * (-Math.pow(2, -10 * --t) + 2) + b;
	},
	easeInCirc: function (x, t, b, c, d) {
		return -c * (Math.sqrt(1 - (t/=d)*t) - 1) + b;
	},
	easeOutCirc: function (x, t, b, c, d) {
		return c * Math.sqrt(1 - (t=t/d-1)*t) + b;
	},
	easeInOutCirc: function (x, t, b, c, d) {
		if ((t/=d/2) < 1) return -c/2 * (Math.sqrt(1 - t*t) - 1) + b;
		return c/2 * (Math.sqrt(1 - (t-=2)*t) + 1) + b;
	},
	easeInElastic: function (x, t, b, c, d) {
		var s=1.70158;var p=0;var a=c;
		if (t==0) return b;  if ((t/=d)==1) return b+c;  if (!p) p=d*.3;
		if (a < Math.abs(c)) { a=c; var s=p/4; }
		else var s = p/(2*Math.PI) * Math.asin (c/a);
		return -(a*Math.pow(2,10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )) + b;
	},
	easeOutElastic: function (x, t, b, c, d) {
		var s=1.70158;var p=0;var a=c;
		if (t==0) return b;  if ((t/=d)==1) return b+c;  if (!p) p=d*.3;
		if (a < Math.abs(c)) { a=c; var s=p/4; }
		else var s = p/(2*Math.PI) * Math.asin (c/a);
		return a*Math.pow(2,-10*t) * Math.sin( (t*d-s)*(2*Math.PI)/p ) + c + b;
	},
	easeInOutElastic: function (x, t, b, c, d) {
		var s=1.70158;var p=0;var a=c;
		if (t==0) return b;  if ((t/=d/2)==2) return b+c;  if (!p) p=d*(.3*1.5);
		if (a < Math.abs(c)) { a=c; var s=p/4; }
		else var s = p/(2*Math.PI) * Math.asin (c/a);
		if (t < 1) return -.5*(a*Math.pow(2,10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )) + b;
		return a*Math.pow(2,-10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )*.5 + c + b;
	},
	easeInBack: function (x, t, b, c, d, s) {
		if (s == undefined) s = 1.70158;
		return c*(t/=d)*t*((s+1)*t - s) + b;
	},
	easeOutBack: function (x, t, b, c, d, s) {
		if (s == undefined) s = 1.70158;
		return c*((t=t/d-1)*t*((s+1)*t + s) + 1) + b;
	},
	easeInOutBack: function (x, t, b, c, d, s) {
		if (s == undefined) s = 1.70158;
		if ((t/=d/2) < 1) return c/2*(t*t*(((s*=(1.525))+1)*t - s)) + b;
		return c/2*((t-=2)*t*(((s*=(1.525))+1)*t + s) + 2) + b;
	},
	easeInBounce: function (x, t, b, c, d) {
		return c - jQuery.easing.easeOutBounce (x, d-t, 0, c, d) + b;
	},
	easeOutBounce: function (x, t, b, c, d) {
		if ((t/=d) < (1/2.75)) {
			return c*(7.5625*t*t) + b;
		} else if (t < (2/2.75)) {
			return c*(7.5625*(t-=(1.5/2.75))*t + .75) + b;
		} else if (t < (2.5/2.75)) {
			return c*(7.5625*(t-=(2.25/2.75))*t + .9375) + b;
		} else {
			return c*(7.5625*(t-=(2.625/2.75))*t + .984375) + b;
		}
	},
	easeInOutBounce: function (x, t, b, c, d) {
		if (t < d/2) return jQuery.easing.easeInBounce (x, t*2, 0, c, d) * .5 + b;
		return jQuery.easing.easeOutBounce (x, t*2-d, 0, c, d) * .5 + c*.5 + b;
	}
});