if (noDrag == undefined) var noDrag = 0;
var pdrags = new Hash();
var DDS = new Array();

function IdeaCart() {
	this.productCount = 0;
	this.products = new Hash();
	if ($('hiddenCart')) {
		this.cartTemplate = new Template("");
	} else {
		this.cartTemplate = new Template("<table width=100% border=0 cellspacing=0 cellpadding=0><tr><td height=16><img src=\"images/arrowblack.gif\" width=7 height=8><b> #{product}</b></td><td rowspan=2 align=right valign=top><a href=\"JavaScript:ideacart1.RemoveFromCart('#{pid}')\"><img src=\"images/xx.gif\" border=0></a></td></tr><tr><td height=17><b>#{amount}</b> Adet <b style=\"color:#FF0000; padding-left:10px;\">#{price} #{curr}</b></td></tr></table>");
	}
	this.lastUpdate = "";
	this.lastId = new Hash();
}
IdeaCart.prototype.AddToCart = function (pid, label, price, curr, tax, rebate, rebateType, bt, quickorder) {
	if(!$('quantity_'+pid) && DDS.length==0) {
		window.location = 'index.php?do=catalog/order2&pid='+pid;
		return false;
	} else {
		quickorder = true;
	}
	label = unescape(label);
	if (DDS.length > 0 && this.lastId.toArray().length == 0 && this.productCount > 0) {
		this.products.each(function (pair) {
			if (ideacart1.lastId[pair.value.pid] == undefined) ideacart1.lastId[pair.value.pid] = 0;
			if (pair.value.ext > parseInt(ideacart1.lastId[pair.value.pid])) ideacart1.lastId[pair.value.pid] = pair.value.ext;
		});
	}
	temp2 = "";
	if (DDS.length > 0) {
		var temp = new Array()
		for (i = 0; i < DDS.length; i++) {
			var optionSelect = DDS[i].id
			optionLabel = $(optionSelect).value
			if (optionLabel != "") temp.push(optionLabel);
		}
		temp2 = temp.join("/");
	}
	if (temp2 != "") {
		label = label + " " + temp2;
	}
	pid2 = pid;
	if (this.lastId[pid] && this.products["pr_" + pid + "_" + this.lastId[pid]]) {
		exist = false;
		index = 0;
		for (i = 1; i <= this.lastId[pid]; i++) {
			if (!this.products["pr_" + pid + "_" + i]) continue;
			currentel = this.products["pr_" + pid + "_" + i]["product"].toString();
			if (label.toLowerCase() == currentel.toLowerCase()) {
				exist = true;
				index = i;
			}
		}
		if (exist) {
			ext = index;
		} else {
			this.lastId[pid]++;
			ext = this.lastId[pid];
		}
	} else {
		ext = 1;
		this.lastId[pid] = 1;
	}
	pid = pid + "_" + ext;
	if ($("cartItem_" + pid) != null) {
		if (this.products["pr_" + pid]["amount"] >= parseInt($('realamount_' + pid2).value)) {
			this.quantityWarn();
			return;
		}
		if ($('quantity_' + pid2) != null) {
			this.products["pr_" + pid]["amount"] = parseInt(this.products["pr_" + pid]["amount"]) + parseInt($('quantity_' + pid2).value);
			if (this.products["pr_" + pid]["amount"] >= parseInt($('realamount_' + pid2).value)) {
				this.products["pr_" + pid]["amount"] = parseInt($('realamount_' + pid2).value);
				this.quantityWarn();
			} else {
				if ($('hiddenCart')) {
					var hiddenCart = parseInt($('hiddenCart').value);
					hiddenCart = hiddenCart + parseInt($('quantity_' + pid2).value);
					$('hiddenCart').value = hiddenCart;
					$('hiddenCartContainer').innerHTML = '(' + hiddenCart + ')';
				}
			}
		} else {
			this.products["pr_" + pid]["amount"] = parseInt(this.products["pr_" + pid]["amount"]) + 1;
			if ($('hiddenCart')) {
				var hiddenCart = parseInt($('hiddenCart').value);
				hiddenCart = hiddenCart + 1;
				$('hiddenCart').value = hiddenCart;
				$('hiddenCartContainer').innerHTML = '(' + hiddenCart + ')';
			}
		}
		pr = parseFloat(this.products["pr_" + pid]["price"]);
		if ((rebate != 0 && rebateType == 1) || rebateType == 0) {
			if (rebateType == 0) {
				pr = rebate;
			} else {
				pr = pr * (100 - rebate) / 100;
			}
		}
		pr = pr * (100 + tax) / 100;
		total = parseFloat(pr) * parseFloat(this.products["pr_" + pid]["amount"]);
		total = total.toFixed(2);
		$("cartItem_inner_" + pid).innerHTML = this.cartTemplate.evaluate({
			'product': this.products["pr_" + pid]["product"].truncate(22, "..."),
			'amount': this.products["pr_" + pid]["amount"],
			'pid': pid,
			'price': total,
			'curr': curr,
			'tax': tax,
			'rebate': rebate,
			'rebateType': rebateType,
			'bt': bt
		});
	} else {
		if (parseInt($('realamount_' + pid2).value) == 0) return;
		this.productCount++;
		this.products["pr_" + pid] = new Hash();
		if ($('quantity_' + pid2) != null) {
			this.products["pr_" + pid]["amount"] = parseInt($('quantity_' + pid2).value);
			if (this.products["pr_" + pid]["amount"] >= parseInt($('realamount_' + pid2).value)) {
				this.products["pr_" + pid]["amount"] = parseInt($('realamount_' + pid2).value);
				this.quantityWarn();
			} else {
				if ($('hiddenCart')) {
					var hiddenCart = parseInt($('hiddenCart').value);
					hiddenCart = hiddenCart + parseInt($('quantity_' + pid2).value);
					$('hiddenCart').value = hiddenCart;
					$('hiddenCartContainer').innerHTML = '(' + hiddenCart + ')';
				}
			}
		} else {
			this.products["pr_" + pid]["amount"] = 1;
			if ($('hiddenCart')) {
				var hiddenCart = parseInt($('hiddenCart').value);
				hiddenCart = hiddenCart + 1;
				$('hiddenCart').value = hiddenCart;
				$('hiddenCartContainer').innerHTML = '(' + hiddenCart + ')';
			}
		}
		this.products["pr_" + pid]["price"] = price;
		this.products["pr_" + pid]["product"] = label;
		this.products["pr_" + pid]["pid"] = pid2;
		this.products["pr_" + pid]["curr"] = curr;
		this.products["pr_" + pid]["tax"] = tax;
		this.products["pr_" + pid]["rebate"] = rebate;
		this.products["pr_" + pid]["rebateType"] = rebateType;
		this.products["pr_" + pid]["bt"] = bt;
		this.products["pr_" + pid]["ext"] = ext;
		var parent_div = document.createElement('div');
		Element.extend(parent_div);
		parent_div.hide();
		parent_div.id = 'cartItem_' + pid;
		var child_div = document.createElement('div');
		Element.extend(child_div);
		child_div.addClassName("productOnCart");
		pr = price;
		if ((rebate != 0 && rebateType == 1) || rebateType == 0) {
			if (rebateType == 0) {
				pr = rebate;
			} else {
				pr = pr * (100 - rebate) / 100;
			}
		}
		pr = pr * (100 + tax) / 100;
		pr = pr.toFixed(2);
		total = parseFloat(pr) * parseFloat(this.products["pr_" + pid]["amount"]);
		total = total.toFixed(2);
		child_div.innerHTML = this.cartTemplate.evaluate({
			product: this.products["pr_" + pid]["product"].truncate(22, "..."),
			amount: this.products["pr_" + pid]["amount"],
			pid: pid,
			price: total,
			curr: curr
		});
		child_div.id = "cartItem_inner_" + pid;
		parent_div.appendChild(child_div);
		if (this.productCount == 1) $('CartContainer').innerHTML = "";
		$('CartContainer').appendChild(parent_div);
		Effect.Appear('cartItem_' + pid, {
			duration: 1.0
		});
	}
	if (DDS.length == 0) {
		if (this.products["pr_" + pid]["amount"] >= parseInt($('realamount_' + pid2).value)) {
			$('cartPic_' + pid2).src = "themes/" + buttonsDir + "/nostock.gif";
			if (pdrags[pid2]) pdrags[pid2].destroy();
		}
		var temp_div = document.createElement("div");
		Element.extend(temp_div);
		if ($('Item_' + pid2)) {
			itemDiv = $('Item_' + pid2);
		} else if ($('Row_' + pid2)) {
			itemDiv = $('Row_' + pid2);
		}
		temp_div.innerHTML = itemDiv.innerHTML;
		document.body.appendChild(temp_div);
		temp_div.setStyle({
			position: "absolute",
			width: itemDiv.getWidth(),
			overflow: "hidden",
			backgroundColor: "#FFFFFF"
		});
		pos = Position.cumulativeOffset(itemDiv);
		temp_div.style.left = pos[0] + "px";
		temp_div.style.top = pos[1] + "px";
		cartpos = Position.cumulativeOffset($('CartContainer'));
		new Effect.Parallel([new Effect.MoveBy(temp_div, cartpos[1] - pos[1], cartpos[0] - pos[0], {
			sync: true
		}), new Effect.Opacity(temp_div, {
			sync: true,
			to: 0.0,
			from: 1.0,
			afterFinish: function (obj) {
				obj.element.remove();
			}
		})], {
			duration: 1.0
		});
	}
	if (quickorder) {
		if (SaveCart(escape(this.products.inspect()), 'type=r'));
		window.location = "index.php?do=catalog/order2";
	} else SaveCart(escape(this.products.inspect()), 'type=e');
}
IdeaCart.prototype.RemoveFromPage = function (el) {
	Element.extend(el);
	el.remove();
}
IdeaCart.prototype.TruncateCart = function (obj) {
	this.productCount = 0;
	this.products = new Hash();
	this.FreeCart();
	SaveCart(escape(this.products.inspect()), 'type=e,method=post');
}
IdeaCart.prototype.FreeCart = function (obj) {
	if ($('hiddenCart')) {
		$('CartContainer').innerHTML = "";
	} else {
		$('CartContainer').innerHTML = "<center><br/>Alışveriş sepetinizde <br/> Ürün bulunmamaktadır.<br/><br/></center>";
	}
}
IdeaCart.prototype.RemoveFromCart = function (pid) {
	if (this.products["pr_" + pid] == undefined) return;
	pid2 = this.products["pr_" + pid]["pid"];
	if (this.products["pr_" + pid]) {
		if (this.products["pr_" + pid]["amount"] > 1) {
			this.products["pr_" + pid]["amount"] = this.products["pr_" + pid]["amount"] - 1;
			rebate = ideacart1.products["pr_" + pid]["rebate"];
			rebateType = ideacart1.products["pr_" + pid]["rebateType"];
			tax = ideacart1.products["pr_" + pid]["tax"];
			pr = parseFloat(this.products["pr_" + pid]["price"]);
			if ((rebate != 0 && rebateType == 1) || rebateType == 0) {
				if (rebateType == 0) {
					pr = rebate;
				} else {
					pr = pr * (100 - rebate) / 100;
				}
			}
			pr = pr * (100 + tax) / 100;
			total = parseFloat(pr) * parseFloat(this.products["pr_" + pid]["amount"]);
			total = total.toFixed(2);
			$("cartItem_inner_" + pid).innerHTML = this.cartTemplate.evaluate({
				product: this.products["pr_" + pid]["product"].truncate(22, "..."),
				amount: this.products["pr_" + pid]["amount"],
				pid: pid,
				price: total,
				curr: this.products["pr_" + pid]["curr"]
			});
		} else {
			this.products.remove('pr_' + pid);
			this.productCount--;
			new Effect.Fade('cartItem_' + pid, {
				duration: 1.0,
				afterFinish: function (obj) {
					ideacart1.RemoveFromPage(obj.element);
					if (ideacart1.productCount < 1) {
						ideacart1.productCount = 0;
						ideacart1.FreeCart();
					}
				}
			});
		}
		if ($('cartPic_' + pid2) != undefined) {
			$('cartPic_' + pid2).src = "themes/" + buttonsDir + "/addtocart.gif";
			if (!noDrag) pdrags[pid] = new Draggable('Item_' + pid2, {
				revert: true,
				ghosting: false,
				zindex: 1003
			});
		}
		SaveCart(escape(this.products.inspect()), 'type=e,method=post');
	}
}
IdeaCart.prototype.CompleteRemoveFromCart = function (pid) {
	this.products.remove('pr_' + pid);
	this.productCount--;
	ideacart1.RemoveFromPage($('cartItem_' + pid));
	ideacart1.lastId = new Hash();
	this.products.each(function (pair) {
		ideacart1.lastId[pair.value.pid] = pair.value.ext;
	});
	if (ideacart1.productCount < 1) {
		ideacart1.productCount = 0;
		ideacart1.FreeCart();
	}
}
IdeaCart.prototype.ReloadCart = function () {
	this.productCount = 0;
	$('CartContainer').innerHTML = "";
	var hiddenCart = 0;
	this.products.each(function (pair) {
		pid = pair.key.substring(3);
		var parent_div = document.createElement('div');
		Element.extend(parent_div);
		parent_div.id = 'cartItem_' + pid;
		var child_div = document.createElement('div');
		Element.extend(child_div);
		child_div.addClassName("productOnCart");
		pr = parseFloat(ideacart1.products["pr_" + pid]["price"]);
		rebate = ideacart1.products["pr_" + pid]["rebate"];
		rebateType = ideacart1.products["pr_" + pid]["rebateType"];
		tax = ideacart1.products["pr_" + pid]["tax"];
		if (rebate != 0) {
			if (rebateType == 0) {
				pr = rebate;
			} else {
				pr = pr * (100 - rebate) / 100;
			}
		}
		pr = pr * (100 + tax) / 100;
		total = parseFloat(pr) * parseFloat(ideacart1.products["pr_" + pid]["amount"]);
		if ($('hiddenCart')) {
			hiddenCart = hiddenCart + parseFloat(ideacart1.products["pr_" + pid]["amount"]);
			$('hiddenCart').value = hiddenCart;
			$('hiddenCartContainer').innerHTML = '(' + hiddenCart + ')';
		}
		total = total.toFixed(2);
		child_div.innerHTML = ideacart1.cartTemplate.evaluate({
			product: ideacart1.products["pr_" + pid]["product"].truncate(22, "..."),
			amount: ideacart1.products["pr_" + pid]["amount"],
			pid: pid,
			price: total,
			curr: ideacart1.products["pr_" + pid]["curr"]
		});
		child_div.id = "cartItem_inner_" + pid;
		parent_div.appendChild(child_div);
		ideacart1.productCount++;
		$('CartContainer').appendChild(parent_div);
	});
	if (ideacart1.productCount == 0) {
		if ('hiddenCart') {
			$("CartContainer").update('');
		} else {
			$("CartContainer").update('<center><br>Alışveriş sepetinizde <br> Ürün bulunmamaktadır.<br><br></center>');
		}
	}
}
IdeaCart.prototype.quantityWarn = function () {
	if ($('quantityWarn') != null) {
		quantityWarning = quantityWarning.replace("(arg0)", parseInt($('realamount_' + pid2).value));
		$('quantityWarn').update(quantityWarning);
		Effect.Appear("quantityWarn", {
			queue: {
				position: 'front',
				scope: 'myscope',
				limit: 2
			},
			duration: 1.0
		});
		Effect.Fade("quantityWarn", {
			queue: {
				position: 'end',
				scope: 'myscope',
				limit: 2
			},
			duration: 1.0
		});
	}
}
