﻿/*
* Buy Now Popup Confirmation
* Copyright (c) 2009 Andrew Greenstreet | andrewgstreet@gmail.com
* Revision: 1.0
*/
//At a baseline, these are our buy now links
var yesURL = "http://my.t-mobile.com";
var noURL = "http://www.t-mobile.com";

function buyNow(_yesURL, _noURL) {
    //alert("Are you a T-mobile Customer\nYes: " + yesURL + "\nNo: " + noURL);
    if (_yesURL != undefined && _yesURL != "undefined" && _yesURL != "") {
        yesURL = _yesURL;
    }
    if (_noURL != undefined && _noURL != "undefined" && _noURL != "") {
        noURL = _noURL;
    }
    showConfirmCustomerYesNo("Are you currently a T-Mobile customer?", customerYesCallback, customerNoCallback);
    //Fire any callbacks assigned for buynow, floodlight for instance
    for (var i = 0; i < buyNowCallbacks.length; i++) {
        buyNowCallbacks[i].func(buyNowCallbacks[i].args);
    }
}

function customerYesCallback() {
    if (trackEventAndGoto({ id: "buynow.yes", href: yesURL })) {
        //if the id exists, the tracking system will automatically change the page
        //if it doens't we'll use the tracking systems page redirect function
        ts_gotoURL();
    }
}

function customerNoCallback() {
    if (trackEventAndGoto({ id: "buynow.no", href: noURL })) {
        //if the id exists, the tracking system will automatically change the page
        //if it doens't we'll use the tracking systems page redirect function
        ts_gotoURL();
    }
}

function showConfirmCustomerYesNo(message, yesCallback, noCallBack) {
	$('#confirmCustomerYesNo').modal(
	{
		close:false,
		position: ["20%",],
		overlayId:'confirmModalOverlay',
		containerId:'confirmModalContainer', 
		onShow: function (dialog) {
			dialog.data.find('.message').append(message);

			// if the user clicks "yes"
			dialog.data.find('.yes').click(
			                                    function () {
				                                    // call the yes callback
				                                    if ($.isFunction(yesCallback)) {
					                                    yesCallback.apply();
				                                    }
				                                    // close the dialog
				                                    $.modal.close();
			                                    }
			                               );
			
			dialog.data.find('.no').click(
			                                    function () {
				                                    // call the yes callback
				                                    if ($.isFunction(noCallBack)) {
					                                    noCallBack.apply();
				                                    }
				                                    // close the dialog
				                                    $.modal.close();
			                                     }
			                              );
			
	    }
	  }
	);
}

var buyNowCallbacks = new Array();
function registerBuynowCallback(_func, _args) {
    buyNowCallbacks.push({ func: _func, args: _args });
}
