$(document).ready(function() {
	// IE6/7 z-index fix
    var zIndexNumber = 1000;
	$('div').each(function() {
		$(this).css('zIndex', zIndexNumber);
		zIndexNumber -= 10;
	});

    $("#top-menu-sign-in").click(function(e) {          
		e.preventDefault();
        $("#sign-in-menu-iframe").show();
		$("#top-menu-sign-in-container").toggleClass("menu-open");
		$("#sign-in-menu-email-error").html("");
        $("#sign-in-menu-password-error").html("");
        $("#sign-in-menu-error").html("");            
    });
	
	$("#sign-in-menu-iframe").mouseup(function() {
		return false
	});
	
	$(document).mouseup(function(e) {
		if($(e.target).parent("a.sign-in-form").length==0) {
			$("#top-menu-sign-in-container").removeClass("menu-open");
			$("#sign-in-menu-iframe").hide();
		}
	});			
	
	var options = { 
        beforeSubmit:  signInBeforeSubmit,  // pre-submit callback 
        success:       signInSuccess, // post-submit callback 
        error:         signInError
    }; 

    $('#sign-in-form').submit(function() { 
        $(this).ajaxSubmit(options); 
        return false; 
    });
});
        
// pre-submit callback 
function signInBeforeSubmit(formData, jqForm, options) { 
    $("#sign-in-menu-email-error").html("");
    $("#sign-in-menu-password-error").html("");
    $("#sign-in-menu-error").html("");            
    return true; 
} 
 
// post-submit callback 
function signInSuccess(responseText, statusText, xhr, $form)  { 
    jsonResponse = jQuery.parseJSON( responseText );    
    if( jsonResponse == null )
        return;
    $.each(jsonResponse[1], function(key,value) {
            $('<input />').attr('type', 'hidden')
            .attr('name', key)
            .attr('value', value)
           .appendTo('#sign-in-form-gotodashboard');
    });
    
    $("#sign-in-form-gotodashboard").attr("action", jsonResponse[0]);
    $("#sign-in-form-gotodashboard").submit();
} 

function signInError(xhr, status, ex) { 
    jsonResponse = jQuery.parseJSON( xhr.responseText );
    if( jsonResponse.code == 415 )
    {
        if( jsonResponse.map.email != undefined )
            $("#sign-in-menu-email-error").html( jsonResponse.map.email );
        if( jsonResponse.map.password != undefined )
            $("#sign-in-menu-password-error").html( jsonResponse.map.password );
        if( jsonResponse.map.__all__ != undefined )
            $("#sign-in-menu-error").html( jsonResponse.map.__all__ );
    }
}