var PhoneTransaction = function() {
	this.devicePrId = PDAttributes.prId;
	this.servicePrId = $("#buymode_servicePrId").val();
	this.lineNum = $("#buymode_lineNum").val();
	this.buymode = this.buyModeFromDropdown();
	this.promoLogoEnabled = ($("#bonusLink").length ? 1 : 0);
};
PhoneTransaction.prototype = {
    buyModeFromDropdown: function() {
		return $('select.buyModeDropDown:visible:last').val();
	},
	setServicePrId: function(servicePrId) {
		this.servicePrId = servicePrId;
		$("#buymode_servicePrId").val(servicePrId);
	},
	numberOfExistingLinesIsValid: function(numOfLines) {
		return (numOfLines == parseInt(numOfLines) && numOfLines >= 2);
	},
	submitAdditionalInfo: function() {
		var planSelectorStr = 'input[name=addFamilyLine]:radio';
		if( $(planSelectorStr).length ) {
			if( $(planSelectorStr+':checked').length ) {
				this.setServicePrId($(planSelectorStr+':checked').val());
			}
		}
		var lineSelectorStr = 'input[name=numExistingLines]';
		if( $(lineSelectorStr).length ) {
			var numOfExistingLines = $(lineSelectorStr).val();
			if( this.numberOfExistingLinesIsValid( numOfExistingLines ) ) {
				this.setLineNum(parseInt(numOfExistingLines) + 1);
			}
		}
		this.updateBreakdownCharges('plan');
	},
	setLineNum: function(lineNum) {
		this.lineNum = lineNum;
		$("#buymode_lineNum").val(lineNum);
	},
	updateBuyMode: function() {
		this.setServicePrId(0);
		this.setLineNum(0);
		this.buymode = this.buyModeFromDropdown();
		$("#buymode_type").val(this.buymode);
		this.updateBreakdownCharges('');
	},
	cancelChangingBuyMode: function() {
		ProductDetailPageCloseOverlay();
		$('select.buyModeDropDown:visible:last').val('');
		this.updateBuyMode();
	},
	updateBreakdownCharges: function(fromOverlayState) {
		if( this.buymode != '' ) {
			var query = 'buyMode='+this.buymode+'&devicePrId='+this.devicePrId+'&servicePrId='+this.servicePrId+'&lineNum='+this.lineNum;
			var that = this;
			var fromOverlayStateStr = (fromOverlayState!='' ? '&fromOverlayState='+fromOverlayState : '');
			$('#breakdownCharges').html('Loading...');
			$.get('/proddetail/ajax/phonePricingRequiredInfo.php?'+ query + fromOverlayStateStr,
				  function(requiredInfoHTML) {
					  if( requiredInfoHTML == 'product_unavailable' ) {
						  location.reload();
					  }
					  else if( requiredInfoHTML == '' ) {
						  ProductDetailPageCloseOverlay();
						  $.get('/proddetail/ajax/phonepricing.php?'+ query,
								function(data) {
									$('#breakdownCharges').html(data);
									that.toggleDisplay((data!=''));
									if( data != '' ) {
										if( $('#breakdownCharges tbody.prices').hasClass('visibilityYes') ) {
											$('#breakdownCharges').removeClass('closed').addClass('open');
										} else {
											$('#breakdownCharges').removeClass('open').addClass('closed');
										}
										SetBreakdownChargesToggleLink();
										SetCarrierPricingRequireScript();
									}
								});
						  if( that.promoLogoEnabled ) {
							  $.get('/proddetail/ajax/phoneBonusBadge.php?devicePrId='+that.devicePrId+'&buyMode='+that.buymode,
									function(data) {
										$('#bonusBadgeContent').html(data);
										$('#bonusLink').toggle((data!=''));
									});
						  }
					  }
					  else {
						  that.toggleDisplay(false);
						  ProductDetailPageCloseOverlay();
						  ProductDetailPageOpenOverlay(requiredInfoHTML);
					  }
				  });
		} else {
			if( this.promoLogoEnabled ) {
				$('#bonusBadgeContent').html('');
				$('#bonusBadge').hide();
				$('#bonusLink').hide();
			}
			this.toggleDisplay(false);
		}
	},
	setZip: function(location) {
		var that = this;
		$.get('/proddetail/ajax/setZipCode.php?location='+location,
			  function(data) {
				  that.updateBreakdownCharges('zip');
			  });
	},
	toggleDisplay: function(bDisplay) {
		$('#breakdownCharges').toggle(bDisplay);
		$('#addToCartBtn').toggle(bDisplay);
	}
}

var phoneTransaction; // evil "singleton"

// phone customer and buymode dropdowns
$(document).ready(function() {
	if( $('select.buyModeDropDown').length ) {
		phoneTransaction = new PhoneTransaction();
		$('select:buyModeDropDown').change(function() {
		   $(this).siblings('div:buyModeDropDown').hide();
		   if($(this).val()) {
			   $('#'+$(this).val()).show();
		   }
		   phoneTransaction.updateBuyMode();
	    });
	}
});

var hover = false;
	  
// phone bonus badge
$(document).ready(function() {
  if( $('#bonusLink').length ) {
	  $('#bonusLink').hover(function() {
	      $('#bonusBadge').show();
	  }, function() {
	      setTimeout(function() {
	          if(hover == false) {
	              $('#bonusBadge').hide('slow');
	          }
	      }, 1000);
	  });
	  
	  $('#bonusBadge').hover(function() {
          $(this).show(); 
          hover = true;
      }, function() {
          $(this).hide(); 
          hover = false;
      });
  }
});

$(document).ready(function() {
    $('#addToCartBtn').click(function(){
        UserTrackPageView('PD3BC', PDAttributes.prId);
        })
    })
