/**
 * Teambox
 */
var protouch_teambox_removehelper = false;
var protouch_teambox_userformsubmit = false;
protouch.teamBox = function () {
	if($("#teambox").length > 0 && !this.initialized.teambox) {

		$("#teambox, #addteambox").hover (
			function () {
				$("#teambox #teambox_infotext").stop(true, true).fadeIn();
			},
			function () {
				$("#teambox #teambox_infotext").stop(true, true).fadeOut();
			}
		);

		$("#addteambox").click (
			function () {
				$("#teambox #teambox_infotext").fadeOut(50,function () {
					$("#teambox #teambox_addproduct").fadeIn(1000,function() {
						$("#teambox #teambox_addproduct").fadeOut();
					});
				});

				$.ajax({
					url : protouch.settings.ajax.teambox + "&action=addproduct",
					data : $("#teambox_fields select, #teambox_fields input"),
					type : 'GET',
					dataType : 'json',
					success : function(data) {
						try {
							if ($(data.products).length > 0) {
								$("#teambox").addClass("active");
								$(".teambox_shadow").addClass("active");
							}
						}
						catch (e) {}

					}/*,
					error : function (jqXHR, textStatus, errorThrown) {

						console.log (jqXHR);
						console.log (textStatus);
						console.log (errorThrown);

					}
					*/
				});
			}
		);

		$("#products .remove").click (
			function (element) {

				protouch_teambox_removehelper = true;
				try {
					var productid = parseInt(this.id.replace("rem_",""));
					var deletedata = {
						id : productid,
						time : element.timeStamp
					};
					$.ajax({
						url : protouch.settings.ajax.teambox + "&action=removeproduct",
						data : deletedata,
						type : 'GET',
						dataType : 'json',
						success : function(data) {
							if (data.reload) {
								location.reload();
							}
							else {
								if ($('#product_' + data.id)) {
									$('#product_' + data.id).animate({height:0,margin:0},300, function() {
										$('#product_' + data.id).remove();
										protouch_teambox_removehelper = false;
									});

								}
							}
						}
					});
				}
				catch (e) {
					throw (e);
				}
			}
		);
		$("#products .product .name").each(
			function (i) {
				this.myslideindex = i;
				this.myparent = $(this.parentNode);
				this.myheightmin = 22;
				this.myheightmax = 32 + parseInt(this.myparent.find(".detail").height());
				$(this).click ( function() {
					if (protouch_teambox_removehelper) {
						return;
					}
					if ((this.myparent.hasClass("open")) && (this.myparent.height() > this.myheightmin)) {
						this.myparent.animate({height:this.myheightmin}, 400, function() {
							$(this).removeClass("open");
						});
					}
					else if ((!this.myparent.hasClass("open")) && (this.myparent.height() == this.myheightmin)) {
						this.myparent.animate({height:this.myheightmax}, 400, function() {
							$(this).addClass("open");
						});
					}
				});
			}
		);

		$("#userform").submit(function () {
			try {
				var formdata = $(this).serialize();


				if (protouch_teambox_userformsubmit) {
					return true;
				}
				protouch_teambox_userformsubmit = false;

				$.ajax({
					url : protouch.settings.ajax.teambox + "&action=checkform&" + formdata,
					type : 'GET',
					success : function(responsedata) {
						if ((typeof(responsedata) == null) || (responsedata == null)) {
							return false;
						}
						if (responsedata.formsubmit) {
							gotoUrl(protouch.settings.teambox + "retailer_2");
							/*
							protouch_teambox_userformsubmit = true;
							$(this).submit();
							$("#userform").submit();
							*/
						}
						else {
							$("#userform  p").removeClass("error");
							for (var i = 0; i < responsedata.error.length; i ++) {
								$("#userform  p." + responsedata.error[i]).addClass("error");
							}
						}
					}
				});

				return protouch_teambox_userformsubmit;
			}
			catch (e) {
				throw e;
			}
		});

		$("#stores .remove").click (
			function (element) {
				try {
					var storeid = parseInt(this.id.replace("srem_",""));
					var deletedata = {
						id : storeid,
						time : element.timeStamp
					};
					$.ajax({
						url : protouch.settings.ajax.teambox + "&action=removestore",
						data : deletedata,
						type : 'GET',
						dataType : 'json',
						success : function(data) {
							if (data.reload) {
								location.reload();
							}
							else {
								if ($('#store_' + data.id)) {
									$('#store_' + data.id).animate({height:0,margin:0},300, function() {
										$('#store_' + data.id).remove();
									});
								}
								if (data.count == 0) {
									$("#stores").remove();
									$("#errorlayer").show();
									$("#submitteambox").hide();
								}
							}
						}
					});
				}
				catch (e) {
					throw (e);
				}
			}
		);
		/**
		 * Manipulate Product Color Switch
		 */
		if ($("#teambox_fields select.color").length > 0) {

			$("#teambox_fields select.color option").each(function() {
				protouch.vars.colorurls.push($(this).val());
				$(this).val($(this).text())
			});

			$("#teambox_fields select.color").change(function() {
				var locationurl = protouch.vars.colorurls[this.selectedIndex];
				gotoUrl(locationurl);
			});
		}
		this.initialized.teambox = true;
	}
};

