YAHOO.util.Event.onDOMReady(function () {
	
	var winwidth = document.all ? document.body.clientWidth : window.innerWidth; 
	document.getElementById('bg-wrapper').style.padding = '0 ' + ((winwidth - 1000) / 2) + 'px';
}); 


/*
     Initialize and render the MenuBar when its elements are ready 
     to be scripted.
*/

YAHOO.util.Event.onContentReady("nav", function () {

	if (!document.getElementById("kula-forum"))	{
	    /*
	         Instantiate a MenuBar:  The first argument passed to the 
	         constructor is the id of the element in the page 
	         representing the MenuBar; the second is an object literal 
	         of configuration properties.
	    */
		
	    var oMenuBar = new YAHOO.widget.MenuBar("nav", { 
                autosubmenudisplay: true, 
                hidedelay: 750, 
                lazyload: false });
	
	    /*
	         Call the "render" method with no arguments since the 
	         markup for this MenuBar instance is already exists in 
	         the page.
	    */
	
	    oMenuBar.render();
	}
});

YAHOO.namespace("sg_signin.container");

YAHOO.util.Event.onContentReady("sg_signin", function () {
	
	var checkToken = function(o)	{
		
		if (o.responseText == 'token error')	{
			alert('Error, please try again');
			window.location.reload();
			return false;
		}
		
		return true;
		
	}
	
	// Hide the Member ID field
	if (document.getElementById('wpsc_checkout_form_20'))	{
		document.getElementById('wpsc_checkout_form_20').parentNode.style.display = 'none';
	}
	
	var replaceHtml = function (el, html) {
		var oldEl = typeof el === "string" ? document.getElementById(el) : el;
		var newEl = oldEl.cloneNode(false);
		newEl.innerHTML = html;
		oldEl.parentNode.replaceChild(newEl, oldEl);
		/* Since we just removed the old element from the DOM, return a reference
		to the new element, which can be used to restore variable references. */
		return newEl;
	};
	
	var handleClearSignInSuccess = function(o)	{
		
		if (o.responseText !== undefined)	{
			
			if (!checkToken(o))	{
				return;
			}
			
			var response = o.responseText; 
			response = response.split("<!")[0];
			eval(response);
			
			// return the sg_signin_greeting to it's original state
			document.getElementById('sg_signin_greeting').innerHTML = sg_signin_greeting_string;
			
			// return the member info form to it's original state
			document.getElementById('customer_info').innerHTML = customer_info;
			
			// return the shopping cart summary to it's state
			replaceHtml('shopping_cart_summary', shopping_cart_summary);
			
			YAHOO.util.Event.addListener(["sg_signin_open","sg_signin_open_img"], "click", YAHOO.sg_signin.container.choose_pose.show, YAHOO.sg_signin.container.choose_pose, true);
			
			// Hide the Member ID field
			if (document.getElementById('wpsc_checkout_form_20'))	{
				document.getElementById('wpsc_checkout_form_20').parentNode.style.display = 'none';
			}
		}
	};

	var handleClearSignInFailure = function(o){
		alert("handleClearSignInFailure failed: " + o.status);
	};

	var clearSignInCallback =
		{
		  success:handleClearSignInSuccess,
		  failure:handleClearSignInFailure
		};
	
	var clearSignIn = function()	{
		var sUrl = location.href;
		var postData = "cart_action=sg_signin&step=clear_signin";
		var request = YAHOO.util.Connect.asyncRequest('POST', sUrl, clearSignInCallback, postData);
	}
	
	// Define various event handlers for Dialog
	var handleSubmit = function() {
		this.submit();
	};
	var handleCancel = function() {
		this.cancel();
	};
	var handleSuccess = function(o) {
		
		if (!checkToken(o))	{
			return;
		}
		
		var response = o.responseText; 
		response = response.split("<!")[0];
		
        eval(response);
		
		YAHOO.sg_signin.BasicLocal = function() {
			// Use a LocalDataSource
			var oDS = new YAHOO.util.LocalDataSource(members);
			// Optional to define fields for single-dimensional array
			oDS.responseSchema = {fields : ["state"]};
	
			// Instantiate the AutoComplete
			var oAC = new YAHOO.widget.AutoComplete("signin_name", "signin_name_container", oDS);
	
			oAC.animVert = false;
			oAC.prehighlightClassName = "yui-ac-prehighlight";
			oAC.useShadow = true;
			
			return {
			    oDS: oDS,
			    oAC: oAC
			};
		}();
		
		YAHOO.sg_signin.container.choose_pose.hide();
		YAHOO.util.Dom.addClass('loading_pose', 'delay_show');
		YAHOO.sg_signin.container.enter_name.show();
	
	};
	var handleFailure = function(o) {
		alert("Submission failed: " + o.status);
	};
	
	var showChoosePose = function()	{
		YAHOO.util.Dom.removeClass('choose_pose', 'delay_show');
		YAHOO.sg_signin.container.choose_pose.show();
	};
	
	// Dialog 1: Choose Pose
	YAHOO.sg_signin.container.choose_pose = new YAHOO.widget.Dialog("choose_pose", 
		{ width : "600px",
		height: "530px",
		fixedcenter : true,
		visible : false, 
		constraintoviewport : true,
		buttons : [
		{ text:"Cancel", handler:handleCancel } ],
		hideaftersubmit : false,
		modal: true
		});

	// Validate the entries in the form to require that both first and last name are entered
	YAHOO.sg_signin.container.choose_pose.validate = function() {
		return true;
	};

	// Wire up the success and failure handlers
	YAHOO.sg_signin.container.choose_pose.callback = { 
		success: handleSuccess,
		failure: handleFailure };
	
	// Render the Dialog
	YAHOO.sg_signin.container.choose_pose.render();
	// end Dialog 1 
	
	YAHOO.util.Event.addListener(["sg_signin_open","sg_signin_open_img"], "click", showChoosePose, YAHOO.sg_signin.container.choose_pose, true);
	YAHOO.util.Event.addListener("clear_sign_in", "click", clearSignIn);
	
	// Dialog 2: Enter name
	
	// Define various event handlers for Dialog
	var handleSubmitEnterName = function() {
		YAHOO.util.Dom.removeClass('loading_name', 'delay_show');
		this.submit();
	};
	var handleCancelEnterName = function() {
		document.getElementByID('signin_name').value = '';
		this.cancel();
	};
	
	var handleSuccessEnterName = function(o) {
		
		if (!checkToken(o))	{
			return;
		}
		
		YAHOO.util.Dom.addClass('loading_name', 'delay_show');
		
		var response = o.responseText; 
		response = response.split("<!")[0];

		eval(response);

		if (error != '')	{
			YAHOO.util.Dom.addClass('enter_name_error', 'error');
			document.getElementById('enter_name_error').innerHTML = error;
		}
		
		else if (typeof(member) == 'object')	{
			
			// clear out the error if it existed before
			document.getElementById('enter_name_error').innerHTML = '';
			YAHOO.util.Dom.removeClass('enter_name_error', 'error');
			
			
			// Add the sign in greeting
			document.getElementById('sg_signin_greeting').innerHTML = sg_signin_greeting_string;
			
			// replace the customer info div
			document.getElementById('customer_info').innerHTML = customer_info;
			
			// refresh the shopping cart summary
			if (sg_coupon)	{
				replaceHtml('shopping_cart_summary', shopping_cart_summary);
			}
			
			YAHOO.util.Event.addListener("clear_sign_in", "click", clearSignIn);
			
			// hide the enter name dialog
			YAHOO.sg_signin.container.enter_name.hide();
			
		}
	};
	
	var handleFailureEnterName = function(o) {
		alert("Submission failed: " + o.status);
	};

	var returnToPoses = function()	{
		YAHOO.sg_signin.container.enter_name.hide();
		YAHOO.util.Dom.addClass('loading_pose', 'delay_show');
		YAHOO.sg_signin.container.choose_pose.show();
	};

	YAHOO.sg_signin.container.enter_name = new YAHOO.widget.Dialog("enter_name", 
		{ width : "35em",
		fixedcenter : true,
		visible : false, 
		constraintoviewport : true,
		buttons : [ { text:"Submit", handler:handleSubmitEnterName, isDefault:true },
		  { text:"Cancel", handler:handleCancel } ],
		hideaftersubmit : false,
		modal: true
		});

	// Validate the entries in the form to require that both first and last name are entered
	YAHOO.sg_signin.container.enter_name.validate = function() {
		return true;
	};

	// Wire up the success and failure handlers 
	YAHOO.sg_signin.container.enter_name.callback = { success: handleSuccessEnterName,
		failure: handleFailureEnterName };
	
	// Render the Dialog
	YAHOO.sg_signin.container.enter_name.render();

	YAHOO.util.Event.addListener("return_to_poses", "click", returnToPoses);
	
	var poseClick = function()	{
		document.getElementById('sg_pose').value = this.id;
		YAHOO.util.Dom.removeClass('loading_pose', 'delay_show');
		document.getElementById('sg_pose_submit').click();
	};
	
	var poses = ["Boat", "Bow", "Bridge", "Camel", "Cobra", "Dancer", "Half Moon", "Lotus", "Plow", "Tree", "Triangle", "Warrior II"];
				
	YAHOO.util.Event.addListener(poses, "click", poseClick);
	
	// end Dialog 2
	
});


