﻿DataSubscription = {
	Boundtime: 24,
	DeviceColorId: '',
	CurrentPageId: undefined,

	SetBoundtime: function (months) {
		DataSubscription.Boundtime = months;
		DataSubscription.Modem.UpdateBoundtime();
	},

	ReadMore: function (id, pageId) {
		$.ajax({
			type: "POST",
			url: "/WebServices/TreWS.asmx/AddDataSubscriptionToBasket",
			contentType: "application/json",
			data: "{ 'subscriptionIdStr': '" + id + "' }",
			dataType: "json",
			success: function () {
				$.ajax({
					url: "/Templates/UtilityPages/DataDeviceAjaxPage.aspx?id=" + pageId,
					cache: false,
					data: "",
					success: function (response) {
						// get data in json format
						$("#deviceViewer").html(response).css('height', 'auto');
						// scroll to top of subscription boxes
						$("html, body").animate({ scrollTop: $(".dataSubscriptionList").offset().top }, 1000);
						$(".readEtAdapt").attr("src", "/media/images/buttons/readmore-adapt-white-btn.gif");

						$('.subscriptionSelected .buttonSpace div').show();
						$('.subscriptionSelected').removeClass('subscriptionSelected');
						$('#dataSub' + id).addClass('subscriptionSelected');
						$('#dataSub' + id + ' .buttonSpace div').hide();

						// Initiate onclick functions for color picker
						$(".modem").each(function (i, mobile) {
							var colors = $(this).find(".colorPicker");
							var image = $(this).find(".modemImage > img");
							var button = $(this).find(".readMoreButton").children().first();

							colors.each(function (i, link) {
								$(this).click(function (event) {
									event.preventDefault();
									DataSubscription.Modem.SwitchColor($(this), image);
								});
							});
						});

						DataSubscription.Modems.Contract();
						Tre.Init("#deviceViewer");
					}
				});
			},
			error: function () {
				//alert('err');
			}
		});
	},
	/*
	deprecated
	Expand : function () { General.Expand('subscriptionReadMore','DataSubscription','Dölj'); },
	Contract : function () { General.Contract('subscriptionReadMore','DataSubscription','Mer om 3Bredband Plus'); },*/
	Modem: {
		Reset: function () {
			$('.modemPickerWrapper .modem').removeClass('modemSelected');
			$('.modemPickerWrapper .modem .buttonSpace .button a').html('<img src="/media/images/buttons/pick-btn.png" alt="Välj" />');
		},
		Select: function (id, colorId, currentPage) {
			$(function () {
				$('#modem' + id).addClass('modemSelected');

				$('.modem .buttonSpace .button').show().siblings('.phoneColors').hide();
				$('#modem' + id + ' .buttonSpace .button').hide().siblings('.phoneColors').show();

				if (id != '0') { //0 => noModem
					$('.deviceSummary').show();
				}
				else {
					$('.deviceSummary').hide();
				}

				$('#proceedBtnDisabled').hide();
				$('#proceedBtnEnabled').show();
			});
		},
		Deselect: function (id) {
			$('#modem' + id).removeClass('modemSelected');
			$('#modem' + id + ' .buttonSpace .button a').html('<img src="/media/images/buttons/pick-btn.png" alt="Välj" />');
			$('.deviceSummary').hide();
			$('#proceedBtnDisabled').show();
			$('#proceedBtnEnabled').hide();
		},

		Alter: function (id, colorId, currentPage, boundtime) {
			var select = !$('#modem' + id).hasClass('modemSelected');
			if (!select) {
				colorId = '';
			}
			DataSubscription.Boundtime = boundtime;

			//Use to set the Addon.Device on AddonPicker.aspx
			if (typeof (Addon) != "undefined") {
				Addon.DeviceId = colorId;
			}

			DataSubscription.DeviceColorId = colorId;
			DataSubscription.Modem.Post(id, colorId, currentPage, select, boundtime);
		},

		NoModem: function (currentPage) {
			var selected = $('#modem0').hasClass('modemSelected');

			//Use to set the Addon.Device on AddonPicker.aspx
			if (typeof (Addon) != "undefined") {
				Addon.DeviceId = '';
			}

			DataSubscription.Modem.Post(0, '', currentPage, !selected, DataSubscription.Boundtime);
		},

		Post: function (id, colorId, currentPage, select, boundtime) {
			$.ajax({
				type: "POST",
				url: "/WebServices/TreWS.asmx/GetDataSubscriptionObject",
				contentType: "application/json",
				data: "{ 'deviceIdStr': '" + colorId + "', 'currentPage': '" + currentPage + "', 'boundtimeStr': '" + boundtime + "' }",
				dataType: "json",
				success: function (data) {
					DataSubscription.Modem.UpdateBasket(data);
					if (!select) {

						DataSubscription.Modem.Deselect(id);
					} else {

						DataSubscription.Modem.Reset();

						DataSubscription.Modem.Select(id);

					}

					DataSubscription.Modem.UpdateBoundtimeControl(currentPage);
				},
				error: function () {
				}
			});
		},

		UpdateBoundtimeControl: function (currentPage) {
			$.ajax({
				type: "POST",
				url: "/Templates/UtilityPages/BoundtimeControlsPage.aspx?page=" + currentPage,
				success: function (data) {
					$("#boundtimeControlWrapper").html(data);
			
					Tre.Init("#boundtimeControlWrapper");
				

		
			
				},
				error: function () {
					//alert('err UpdateBoundtimeControl');
				}
			});
		},

		UpdateBoundtime: function () {
		
			$.ajax({
				type: "POST",
				url: "/WebServices/TreWS.asmx/GetDataSubscriptionObject",
				contentType: "application/json",
				data: "{ 'deviceIdStr': '" + DataSubscription.DeviceColorId + "', 'currentPage': '" + DataSubscription.CurrentPageId + "', 'boundtimeStr': '" + DataSubscription.Boundtime + "' }",
				dataType: "json",
				success: function (data) {
					DataSubscription.Modem.UpdateBasket(data);
				},
				error: function () {
					//alert('err');
				}
			});
		},

		UpdateBasket: function (data) {
			var response = (typeof data.d) == 'string' ? eval('(' + data.d + ')') : data.d;
			$("#basket_subscriptionSpecs").html(response.SubscriptionSpecification);
			$("#basket_deviceMonthly").html(response.DeviceMonthly);
			if (response.DeviceUpfront == "+ 0 kr")
				$("#deviceUpfront").html("");
			else
				$("#deviceUpfront").html(response.DeviceUpfront);
			$("#basket_subscriptionTotalMonthly").html(response.SubscriptionTotalMonthly);
			$("#basket_subscriptionMonthly").html(response.SubscriptionMonthly);
			$("#totalPrice").html(response.TotalMonthly);
			$("#basket_deviceName").html(response.Device);
			$("#basket_deviceType").html(response.DeviceTypeName);
			$("#basket_computerMonthly").html(response.ComputerMonthly);
			$("#basket_computerUpfront").html(response.ComputerUpfront);
			$("#totalDeviceUpfront").html(response.TotalUpfront);
			$("#discountText").html(response.DiscountText);


			//update height for the stickyfloat generated div
			$("#stickyFloatEmptyDiv").height($(".sideblockWrapper").height());
			// redraw rounded corner in ie7-8
			if (typeof (curvyCorners) !== 'undefined') { curvyCorners.redraw(); }

			//if stickyfloat is at bottom and height is increased it will overflow, this is the fix
			var maxTop = $("#stickyFloatEmptyDiv").parent().height() - $(".sideblockWrapper").height(); //parent height minus sideblock height (because ie updates stickyFloatEmptyDiv too slow)  = max top value
			var curTop = parseInt($(".sideblockWrapper").css('top')); // current top
			if (curTop > maxTop) { $(".sideblockWrapper").css('top', maxTop + 'px'); }


			if (typeof (Addon) != "undefined") {
				setTimeout(function () { HandleAddonPickerBasketUpdates(response.BoundTimeMonths) }, 100);
			}




		},

		SwitchColor: function (link, image) {
			_gaq.push(['_trackEvent', 'klick', 'bytt färg', location.href, 1]);

			var metadata = DataSubscription.FetchMetadata(link);
			link.parent().addClass("activeColor");
			link.parent().siblings().removeClass("activeColor");
			image.attr("src", metadata.url);


			if (!$('#modem' + metadata.id).hasClass("modemSelected")) {
				$('#modem' + metadata.id + ' .buttonSpace .button a').attr('href', 'javascript:DataSubscription.Modem.Alter(' + metadata.id + ',' + metadata.colorId + ',' + metadata.pageId + ',' + DataSubscription.Boundtime + ');').html('<img src="/media/images/buttons/pick-btn.png" alt="Välj" />');
			} else {

				DataSubscription.DeviceColorId = metadata.colorId;
				DataSubscription.Modem.Post(metadata.id, metadata.colorId, metadata.pageId, true, DataSubscription.Boundtime);
			}
		}
	},
	Modems: {
		Expand: function () {
			$(".modem").show();
			General.Expand('modemsExpandContainer', 'DataSubscription.Modems', 'Dölj');
		},
		Contract: function () {
			$(".modem").hide();
			$('.modem').eq(0).show();
			$('.modem').eq(1).show();
			$('.modem').eq(2).show();
			$('.modem').eq(3).show();
			General.Contract('modemsExpandContainer', 'DataSubscription.Modems', 'Välj bland fler modem');
		}
	},
	FetchMetadata: function (element) {
		return element.metadata({ type: 'attr', name: 'data' });
	}
};

function HandleAddonPickerBasketUpdates(boundTime) {
 Addon.Boundtime = boundTime;
 Addon.UpdateBasket();
 setTimeout(function () {
 	if ($("#boundtimeControlWrapper select").length == 0) {
 		$("#boundtimeControlWrapper input:checked").click();
 	}
 	else {
 		$("#boundtimeControlWrapper select option:selected").change();
 	}

 }, 200);

}
