/* Setup namespaces */
if (typeof(BCNTRY) == 'undefined') {BCNTRY = {};}
if (typeof(BCNTRY.login) == 'undefined') {BCNTRY.login = {};}

BCNTRY.login.login_contribution_panel_init = function () {
	// Instantiate a Panel from markup
	BCNTRY.login_contribution = new YAHOO.widget.Panel(
		"login_contribution_popup", {
			width:"550px", 
			visible:false, 
			modal: true, 
			constraintoviewport:true, 
			fixedcenter:true
		});

    BCNTRY.login_contribution.render();
    // IE6 does not handle drop shadows well.  Need to add other class
    if (YAHOO.env.ua.ie == 6) {
        yd.addClass('login_contribution_popup','ie6');
    }

    // Unsubscribe from default handlers - IE bug
    BCNTRY.login_contribution.showMaskEvent.unsubscribe();
    BCNTRY.login_contribution.hideMaskEvent.unsubscribe();

    BCNTRY.login_contribution_popup_show = function () {
        if ($('chat-bcs-login-popup'))
            $('chat-bcs-login-popup').style.display = 'inline';
        yd.setStyle('login_contribution_popup','display','block');
        BCNTRY.login_contribution.show();
        if ($('errors') && $('errors').value) {
            if($('current_previous_state').value == 'password') {
            BCNTRY.email_password_show();
        }
        else if ($('current_previous_state').value == 'new_account') {
            BCNTRY.show_new_member();
        }
    }
};

    BCNTRY.login_contribution_popup_hide = function () {
        BCNTRY.login_contribution.hide();
        if ($('chat-bcs-login-popup'))
            $('chat-bcs-login-popup').style.display = 'none';
    };

    ye.addListener("close_large_image", "click", function () { BCNTRY.login_contribution_popup_hide(); return false;} , BCNTRY.large_image, true);
    ye.addListener("login_contribution_popup_mask", "mousedown", function () { BCNTRY.login_contribution_popup_hide(); return false;}, BCNTRY.large_image, true);
    ye.addListener("submit_content", "click", function () {BCNTRY.login_contribution_popup_show(); BCNTRY.show_existing_member(); template_links_tl('tr_template', 'Login:submit_button'); return false;});
    ye.addListener("box_close", "click", function () {BCNTRY.login_contribution_popup_hide(); template_links_tl('tr_template', 'Login:close_x'); return false;});
    ye.addListener("forgot_password", "click", function () {BCNTRY.email_password_show(); template_links_tl('tr_template', 'Login:forgot_password'); return false;});
    ye.addListener("exisiting_radio_new", "click", function () {BCNTRY.show_new_member(); template_links_tl('tr_template', 'Login:sign_up_now'); return false;});
    ye.addListener("new_radio_old", "click", function () {BCNTRY.show_existing_member(); template_links_tl('tr_template', 'Login:login'); return false;});

};

BCNTRY.show_new_member = function () {
    $('existing_member').style.display = "none";
    $('email_password').style.display = "none";
    $('not_email_password').style.display = "block";
    $('new_member').style.display = "block";
    $('new_radio_new').checked = true;
    $('new_mv_username').value = $('existing_mv_username').value;
    $('new_previous_state').value = 'new_account';
}

BCNTRY.show_existing_member = function () {
    $('new_member').style.display = "none";
    $('email_password').style.display = "none";
    $('not_email_password').style.display = "block";
    $('existing_member').style.display = "block";
    $('existing_radio_old').checked = true;
    $('existing_mv_username').value = $('new_mv_username').value;
    $('existing_previous_state').value = 'existing_memeber';
}

BCNTRY.email_password_show = function () {
    $('not_email_password').style.display = "none";
    $('email_password').style.display = "block";
    $('previous_state').value = 'password';
};

ye.onDOMReady(function() {
 BCNTRY.login.login_contribution_panel_init();
 BCNTRY.login_contribution_popup_show();
});


//history setup stuff
var initialState = 'start';
YAHOO.util.History.register('app', initialState, historyMachine);
try {
	YAHOO.util.History.initialize('yui-history-field', 'yui-history-iframe');
	// Makes sure the app always starts in a "start" state
	YAHOO.util.History.onLoadEvent = { fire: function() {
		YAHOO.util.History.navigate('app', 'start');
	}};
} catch ( e ) { 
	// The only exception that gets thrown here is when the browser is not A-grade.
	// Since scripting is enabled, we still try to provide the user with a better
	// experience using AJAX. The only caveat is that the browser history will not work.
	alert("It appears that you are using a browser that is not fully supported. We recommend IE, Firefox, or Safari. "+e.message);
}

function form_reload(form_in) {
	outbound = false;
	var form = form_in;
	// Disabling mv_click on JS form submissions
	if ( form.mv_click && form.mv_click.value ) {
		form.mv_click.value = '';
	}
	form.submit();
	return;
}

var f = 'user';
// run page_init function on page load finish
YAHOO.namespace("yui_activity.container");

function reload_page() {
	var page;
	if($('shipping_panel')) {
		page = 'shipping_book';
	} else if($('billing_panel')) {
		page = 'billing_book';
	} else if($('payment_panel')) {
		page = 'payment_book';
	}
	// Safari requires the url to be different, so just generate a random querystring
	var random = Math.floor(Math.random()*1001)
	window.location.href = '/' + catalog + '/user/' + page + '?submit=' + random; 
}

function select_address(type, load_options) {
	return;
}

function delete_address(type, nickname) {
	document.body.style.cursor = 'wait';
	yc.delayedAsyncRequest(
		'POST',
		'/' + catalog + '/user/ajax/delete_address.html',
		{
			success: function(r) {
				var result = r.responseText;
				document.body.style.cursor = 'default';
				if(result == 1) {
					reload_page();
				} else {
					$('response_text').innerHTML = 'Unable to delete address.';
					if(type == 'billing') {
						hide(YAHOO.yui_activity.container.billing_panel);
					} else {
						hide(YAHOO.yui_activity.container.shipping_panel);
					}	
				}
			},
			failure: function(r) {
				document.body.style.cursor = 'default';
				ajax_error(r);
			},
			timeout: ajax_timeout
		},
		'type='+type+'&nickname='+nickname
	);
	return;
}

function set_default(type, nickname) {
	document.body.style.cursor = 'wait';
	yc.delayedAsyncRequest(
		'POST',
		'/' + catalog + '/user/ajax/set_default.html',
		{
			success: function(r) {
				var result = r.responseText;
				document.body.style.cursor = 'default';
				if(result == 1) {
					reload_page();
				} else {
					$('response_text').innerHTML = 'Unable to change default address.';
				}
			},
			failure: function(r) {
				document.body.style.cursor = 'default';
				ajax_error(r);
			},
			timeout: ajax_timeout
		},
		'type='+type+'&nickname='+nickname
	);
	return;
}

function save_address(option, type) {
	document.body.style.cursor = 'wait';
	$(type + '_save_progress').style.visibility = "visible";
	var args = 'option='+option+'&type='+type +get_cgi(option, type);
	var result = 0;
	yc.delayedAsyncRequest(
		'POST',
		'/' + catalog + '/user/ajax/save_address.html',
		{
			success: function(r) {
				result = r.responseText;
				document.body.style.cursor = 'default';
				$(type + '_save_progress').style.visibility = "hidden";
				if(result == 0 && result.substring(1,2) != '0') {
					populate_panel(option, type, 'user', 1);
				} else {
					reload_page();
				} 
			},
			failure: function(r) {
				document.body.style.cursor = 'default';
				$(type + '_save_progress').style.visibility = "hidden";
				ajax_error(r);
			},
			timeout: ajax_timeout
		},
		args
	);
	
	return;
}

function historyMachine(state)
{
	var manager = YAHOO.yui_activity.overlay_manager;
	if (manager && state == 'start') {
		if (manager.find('billing_panel'))  hide('billing_panel');
		if (manager.find('shipping_panel')) hide('shipping_panel');
		if (manager.find('payment_panel')) hide('payment_panel');
	}

	return true;
}

function populate_payment_panel(option, on_error) {
	if(!on_error) {
		try {
			YAHOO.util.History.navigate('app', 'payment_panel');
		} catch(e) {}
	}

	document.body.style.cursor = 'wait';

	if($('payment_panel')) {
		while($('payment_panel').hasChildNodes())
			$('payment_panel').removeChild($('payment_panel').firstChild);
	}
	div_panel = document.createElement("div");
	div_panel.id = 'payment';
	$('payment_panel').appendChild(div_panel);

	var ajax_call = '/' + catalog + '/user/ajax/populate_payment_panel.html';
	yc.delayedAsyncRequest(
		'POST',
		ajax_call,
		{   success: function(r) {
				$('payment').innerHTML = r.responseText;
				if(option == 'edit' || on_error) {
					$('cc_exp_mth').value = $('set_cc_exp_mth').value;
					$('cc_exp_yr').value = $('set_cc_exp_yr').value;
				}				
				var panel = initiate_panel('payment');
				setPanelPosition('payment', 'user');
				document.body.style.cursor = 'default';
				panel.show();
				panel.showMask();
			},
			failure: function(r) {
				document.body.style.cursor = 'default';
				ajax_error(r);
			},
			timeout: ajax_timeout
		},
		'option=' + option + '&ccid=' + $('ccid_editing').value + '&on_error=' + on_error
	);
	return;
}

function save_payment(option) {
	document.body.style.cursor = 'wait';
	var args = 'option=' + option + '&cc_nickname=' + $('cc_nickname').value + '&cc_number=' + $('cc_number').value + '&cc_exp_mth=' 
		+ $('cc_exp_mth').value + '&cc_exp_yr=' + $('cc_exp_yr').value + '&ccid_editing=' + $('ccid_editing').value;
	if(option == 'edit') {
		args += '&ccid=' + $('ccid_editing').value;
	}
	var result = 0;
	yc.delayedAsyncRequest(
		'POST',
		'/' + catalog + '/user/ajax/save_payment.html',
		{
			success: function(r) {
				result = r.responseText;
				document.body.style.cursor = 'default';
				if(result != 0) {
					reload_page();
				} else {
					populate_payment_panel(option, 1);
				}
			},
			failure: function(r) {
				document.body.style.cursor = 'default';
				ajax_error(r);
			},
			timeout: ajax_timeout
		},
		args
	);
	return;
}

function delete_payment(ccid) {
	document.body.style.cursor = 'wait';
	yc.delayedAsyncRequest(
		'POST',
		'/' + catalog + '/user/ajax/delete_payment.html',
		{
			success: function(r) {
				var result = r.responseText;
				document.body.style.cursor = 'default';
				if(result == 1) {
					reload_page();
				} else {
					$('response_text').innerHTML = 'Unable to delete address.';
					hide(YAHOO.yui_activity.container.payment_panel);
				}
			},
			failure: function(r) {
				document.body.style.cursor = 'default';
				ajax_error(r);
		},
			timeout: ajax_timeout
		},
		'&ccid=' + ccid
	);
	return;
}
