
VIEWS.GoogleCheckout = {
    menu: ['MenuMenu', 'AboutMenu', 'StreamMenu', 'DistributionMenu', 'NewsMenu', 'ContactMenu'],
    init: function(args) {
		//this.menuPos = "checkoutCheckout";
		this.setMenuPos("checkoutBack","checkoutCheckout");
		this.currPlayer = args.playerIndex;
		this.playListID = AMOSTV.players[this.currPlayer].positions.curr;
		$('googleCheckoutContainer').show();
		
		/*$('googleCheckoutText').innerHTML = "Do you wish to purchase one hour of " + AMOSTV.players[this.currPlayer].playlists[AMOSTV.players[this.currPlayer].positions.curr].name + " for $0.99?" +
											"<p>Press 'Login / Sign up an account' below and continue the purchase process by creating or accessing your Google Checkout Account.</p>";*/


		/*$('googleCheckoutText').innerHTML = "Do you wish to purchase a subscription for full access to Amos TV videos for $4.95?" +
											"<p>Press 'Login / Sign up an account' below and continue the purchase process by creating or accessing your Google Checkout Account.</p>";*/
		
		this.token = Math.floor(1 + (Math.random()*180000));
		var that = this;
		var amazoncheckoutbutton = false;
		var paypalcheckoutbutton = false;
		if(CLICKEVENTADDED == false)
		{
			
			jQuery(".amazoncheckoutbutton").click(function()
			{
				if(amazoncheckoutbutton == false)
				{
					var cookieName = "googleCheckoutAmosSubscribtion";
					var cookieValue = "?token--"+that.token;
					CookieHandler.setCookie(cookieName, cookieValue);
					amazoncheckoutbutton = true;
					jQuery(".amazoncheckoutbutton").css({display:"none"});
					document.forms["amazonbuttonform"].submit();
				}
			});
			jQuery(".paypalcheckoutbutton").click(function()
			{
				if(paypalcheckoutbutton == false)
				{
					var cookieName = "googleCheckoutAmosSubscribtion";
					var cookieValue = "?token--"+that.token;
					CookieHandler.setCookie(cookieName, cookieValue);
					paypalcheckoutbutton = true;
					jQuery(".paypalcheckoutbutton").css({display:"none"});
					document.forms["paypalbuttonform"].submit();
				}
			});
			jQuery(".googlecheckoutbutton").click(function()
			{
				that.googleSubmitForm();
			});
			CLICKEVENTADDED = true;
		}
		
		//247707412876761    test google account
		//245144056868497 amos
		

		//Google Checkout Subscribtion
		
		//remove this for checkout for video.
	
		var return_url = window.location.protocol + "//" + window.location.hostname + "" + "/index.html?token="+this.token+"?playerID=" + this.currPlayer +"?playListID=" + AMOSTV.players[this.currPlayer].positions.curr +"?subscribtion=Enabled";	
			
		var buyNowButton = "<form method='POST' action='https://checkout.google.com/api/checkout/v2/checkoutForm/Merchant/245144056868497' accept-charset='utf-8' name='BB_BuyButtonForm' target='_top' id='BB_BuyButtonForm'>"+
		"<input type='hidden' name='shopping-cart.items.item-1.item-name' value='Amos TV Subscription Monthly Renewal'/>"+
		"<input type='hidden' name='shopping-cart.items.item-1.item-description' value='Monthly renewal charge to keep your full access to AMOS TV.'/>"+
		"<input type='hidden' name='shopping-cart.items.item-1.unit-price.currency' value='USD'/>"+
		"<input type='hidden' name='shopping-cart.items.item-1.unit-price' value='0.00'/>"+
		"<input type='hidden' name='shopping-cart.items.item-1.quantity' value='1'/>"+
		"<input type='hidden' name='shopping-cart.items.item-1.subscription.type' value='google'/>"+
		"<input type='hidden' name='shopping-cart.items.item-1.subscription.period' value='MONTHLY'/>"+
		//"<input type='hidden' name='shopping-cart.items.item-1.subscription.payments.subscription-payment-1.times' value='12'>"+
		"<input type='hidden' name='shopping-cart.items.item-1.subscription.payments.subscription-payment-1.maximum-charge' value='4.95'>"+
		"<input type='hidden' name='shopping-cart.items.item-1.subscription.payments.subscription-payment-1.maximum-charge.currency' value='USD'>"+
		"<input type='hidden' name='shopping-cart.items.item-1.subscription.recurrent-item.item-name' value='Full Access to Videos on Amos TV'>"+
		"<input type='hidden' name='shopping-cart.items.item-1.subscription.recurrent-item.item-description' value='Your flat charge for full access to videos on Amos TV'>"+
		"<input type='hidden' name='shopping-cart.items.item-1.subscription.recurrent-item.quantity' value='1'>"+
		"<input type='hidden' name='shopping-cart.items.item-1.subscription.recurrent-item.unit-price' value='4.95'>"+
		"<input type='hidden' name='shopping-cart.items.item-1.subscription.recurrent-item.unit-price.currency' value='USD'>"+
		"<input type='hidden' name='shopping-cart.items.item-1.subscription.recurrent-item.digital-content.description' value='You have now full access to videos on Amos TV.'>"+
       	"<input type='hidden' name='shopping-cart.items.item-1.digital-content.description' value='<a href=\""+return_url+"\"> IMPORTANT: Click here to return to AMOS TV and retrieve your subscribtion</a>' />"+
      	"<input type='hidden' name='shopping-cart.items.item-2.item-name' value='Amos TV Subscription Immediate Access'>"+
       	"<input type='hidden' name='shopping-cart.items.item-2.item-description' value='Get access to AMOS TV during the first month of the subscription.'>"+
       	"<input type='hidden' name='shopping-cart.items.item-2.unit-price' value='4.95'>"+
       	"<input type='hidden' name='shopping-cart.items.item-2.unit-price.currency' value='USD'>"+
		"<input type='hidden' name='shopping-cart.items.item-2.digital-content.description' value='Your immediate access has been confirmed.' />"+
       	"<input type='hidden' name='shopping-cart.items.item-2.quantity' value='1'>"+
		//"<input type='image' name='Google Checkout' alt='Fast checkout through Google' src='http://checkout.google.com/buttons/checkout.gif?merchant_id=MERCHANT_ID&w=180&h=46&style=white&variant=text&loc=en_US' height='46' width='180'/>"+
		//"<input alt='' src='https://sandbox.google.com/buttons/buy.gif?merchant_id=247707412876761&amp;w=117&amp;h=48&amp;style=trans&amp;variant=text&amp;loc=en_US' type='image'/>"+
        "</form>";
		

		//create or update the div that contains buynow button
		var buynowDiv = null;
		if($('buynowDiv') == null) {
		 buynowDiv = new Element('div', { 'id': 'buynowDiv'});
		buynowDiv.innerHTML = buyNowButton;
			buynowDiv.setStyle({visability: "hidden"});
			 $('googleCheckoutContainer').appendChild(buynowDiv);
		 } else {
			buynowDiv = $('buynowDiv');
			 buynowDiv.innerHTML = buyNowButton;
			 buynowDiv.setStyle({visability: "hidden"});
		}

		_gaq.push(['_trackPageview', '/amosgtv/preCheckout/'+this.currPlayer+'_'+AMOSTV.players[this.currPlayer].playlists[AMOSTV.players[this.currPlayer].positions.curr].name]);
		VIEWS.GoogleMenu.show(0);
		VIEWS.GoogleMenu.setFocus();
		AMOSTV.stopLoading();
    },
	
    exit: function() {
		$('googleCheckoutContainer').hide();
    },

	//removes focus on the old highlighted button and highlight the new one
	setMenuPos: function(prevPos, previewPos) {
		$(prevPos).removeClassName("focused");
		this.menuPos = previewPos;
		var menuItem = $(previewPos);
		menuItem.addClassName("focused");
	},
	
	show: function(){
		$('googleCheckoutContainer').show();
	},
	
	googleSubmitForm: function() {
		//brightcove.removeExperience("googlePreview");
		
		//remove comment below for checkout one video.
		//var cookieName = "googleCheckoutAmos"+this.currPlayer+""+AMOSTV.players[this.currPlayer].positions.curr;
		//end remove.
		
		//remove line below for checkout one video.
		var cookieName = "googleCheckoutAmosSubscribtion"
		//end remove.
		
		var cookieValue = "?token--"+this.token;
		CookieHandler.setCookie(cookieName, cookieValue);
		$('BB_BuyButtonForm').submit();
	},
	
	googleCheckoutBack: function(){
		AMOSTV.loadView('List',{player:this.currPlayer,loadCheckout: false});
	},
	
	jwtCallback: function(jwtObj){
		goog.payments.inapp.buy({
			parameters: {},
			jwt: jwtObj,
			success: function(success) { DOUT(success); VIEWS.GoogleCheckout.checkoutComplete();},
			failure: function(fail) { DOUT(fail); AMOSTV.loadView('List');}
		})
	},
	
	
	checkoutComplete: function(){
		CookieHandler.setPlayerCookie(this.currPlayer, this.playListID); AMOSTV.loadView('GoogleCheckoutDone', {currPlayer: this.currPlayer, playList: this.playListID});
	},
	
	purchaseInapp: function() {
        new Ajax.Request("jwt/createJWT.php?a=encode&itemName="+this.currPlayer+" - "+ AMOSTV.players[this.currPlayer].playlists[AMOSTV.players[this.currPlayer].positions.curr].name +"&itemDesc=One hour of playtime", { method: 'get',
            onSuccess: function(response, jsonResponse){
				//remove comment below for checkout one video.
				//console.log(response);
				//end remove.
                var json = response.responseText;
                //var obj = eval('(' + json + ')');
                VIEWS.GoogleCheckout.jwtCallback(json);
            },
            onFailure: function() {
                AMOSTV.networkError('ajax');
            },
			
			onException: function(sender,exception){
				console.log(exception);
				//AMOSTV.networkError(exception);
			}
        });
		
		// AMOSTV.ajax("jwt/JWT.php?a=encode&q="+jwtObj , function(jwtResp){
			// var resp = jwtResp;
			// console.log("resp = " + resp);
			// console.log("hej");
		// });
	},
	
    keyHandler: function(key) {
        if ( key == KEYS.OK ) {
			switch(this.menuPos){
					case "checkoutCheckout": //user clicks the access/login google checkout account button so we sumbit the form
						//remove code line below for checkout one video.
						this.googleSubmitForm();
						//end remove.
						//remove comment below for checkout one video.
						//this.purchaseInapp();
						//end remove.
					break;
					
					case "checkoutBack": // user clicks back button so we go back to thumbsarea view
						this.googleCheckoutBack();
					break;
			}
        }
		
		if ( key == KEYS.UP ) {
			if(this.menuPos == "checkoutBack"){
				this.setMenuPos(this.menuPos, "checkoutCheckout");
			}			
		}
		
		if ( key == KEYS.DOWN ) {
			if(this.menuPos == "checkoutCheckout"){
				this.setMenuPos(this.menuPos, "checkoutBack");
			}	
		}
		
		if ( key == KEYS.ESC || key == KEYS.BACK) {
			this.googleCheckoutBack();
		}
    },
	
	mouseClicked: function(buttonClicked){
		switch(buttonClicked){			
			case "checkoutCheckout":
				//remove line below for checkout one video.
				this.googleSubmitForm();
				//end remove.
				//remove comment below for checkout one video.
				//this.purchaseInapp();
				//end remove.
			break;
			
			case "checkoutBack":
				this.googleCheckoutBack();
			break;
		}	
	}, 
	
	mouseOver: function(buttonHovered){
		switch(buttonHovered){
			case "checkoutCheckout":
				this.setMenuPos(this.menuPos, "checkoutCheckout");
			break;
			
			case "checkoutBack":
				this.setMenuPos(this.menuPos, "checkoutBack");
			break;
		}
	}
};


