﻿$jq.namespace('MatchCore.CP');

$jq(document).ready(function() {
	MatchCore.CP.Promo.init();
});

MatchCore.CP.Promo = function() {
    var _currentKeyword;
    var _currentPromoID;
    var _subscribeUrl;
	var _failureDelegate = new MatchCore.Delegate();
	var _successDelegate = new MatchCore.Delegate();
	var _busy = false;
	
	var failure = function (response) {
	    if (_failureDelegate.count() > 0)
			_failureDelegate.fire(response);
		else 
			MatchCore.CP.Messaging.show(response.Messages);
	};
	
	var success = function (response) {
	    if (_successDelegate.count() > 0)
			_successDelegate.fire(response);
	};
	
	var callAgree = function(promoId) {
		var postData = {
	        "sid" : MatchCore.CP.SID(),
			"theme" : MatchCore.CP.Theme(),
			"promoID" : promoId
		};
			
		MatchCore.CP.Services.invoke({
			method : 'AgreeToTerms',
			data : postData,
			verb : 'POST',
			success : function(response) {
				if (response.Success) {
					success(response);
					
					window.location.href = _subscribeUrl
				}
				else
					failure(response);
			}
		});
	};
	
	return {
		init : function(opts) {
			$jq('.btnPromoTermsAgree').bind('click', this.agree);
			
			$jq('.cp_promo').each(function() {
				var loginView = $jq('.loginView', this);
				var registerView = $jq('.registerView', this);
				var promoTermsView = $jq('.promoTermsView', this);
				
				if (promoTermsView.length > 0) {
					MatchCore.CP.Login.onSuccess(function() {
						MatchCore.CP.Messaging.reset();
						loginView.fadeOut('fast', function() {
							promoTermsView.fadeIn('slow');
						});
					});
					
					MatchCore.CP.Registration.onSuccess(function() {
						MatchCore.CP.Messaging.reset();
						registerView.fadeOut('fast', function() {
							promoTermsView.fadeIn('slow');
						});
					});
				}
				else {
					MatchCore.CP.Login.onSuccess(function() {
						MatchCore.CP.Messaging.reset();
						MatchCore.CP.Promo.agree();
					});
					
					MatchCore.CP.Registration.onSuccess(function() {
						MatchCore.CP.Messaging.reset();
						MatchCore.CP.Promo.agree();
					});
				}
			});
		},
		
		agree : function() {
		
			//check for promoID
			if (_currentPromoID) {
				callAgree(_currentPromoID);
			}
			else {
				//get the promoID from the validate method
				var postData = {
					"sid" : MatchCore.CP.SID(),
					"theme" : MatchCore.CP.Theme(),
					"keyword" : _currentKeyword
				};
				MatchCore.CP.Services.invoke({
					method : 'ValidatePromotion',
					data : postData,
					verb : 'POST',
					success : function(response) {
						if (response.Success) {
						    _subscribeUrl = response.RedirectURL;
							callAgree(response.PromoId);
						}
						else {
							failure(response);
						}
					}
				});
			}
		},
		
		CurrentKeyword : function(keyword) {
            if (arguments.length > 0) {
                _currentKeyword = keyword;
            }
            else {
                return _currentKeyword;
            }
        },
        
        CurrentPromoID : function(promoID) {
            if (arguments.length > 0) {
				if (promoID > 0)
					_currentPromoID = promoID;
            }
            else {
                return _currentPromoID;
            }
        },
        
         SubscribeUrl : function(subscribeUrl) {
            if (arguments.length > 0) {
                _subscribeUrl = subscribeUrl;
            }
            else {
                return _subscribeUrl;
            }
        }
	};
}();