// Preload Images
img1 = new Image(16, 16);
img1.src="images/spinner.gif";

img2 = new Image(220, 19);
img2.src="images/ajax-loader.gif";

var dealerLoginModal;
var heightt;

jQuery(function ($) {
	var dealerlogin = {
		container: null,
		init: function () {
			$("input.dealerlogin, a.dealerlogin").click(function (e) {
				e.preventDefault();	

				$("#dealerlogin-modal-content").modal({
					overlayId: 'dealerlogin-overlay',
					containerId: 'dealerlogin-container',
					closeHTML: null,
					minHeight: 80,
					opacity: 65, 
					position: ['0',],
					overlayClose: true,
					onOpen: dealerlogin.open,
					onClose: dealerlogin.close
				});
			});
		},
		open: function (d) {
			dealerLoginModal = d;
			var self = this;
			self.container = d.container[0];
			d.overlay.fadeIn('slow', function () {
				$("#dealerlogin-modal-content", self.container).show();
				var title = $("#dealerlogin-modal-title", self.container);
				title.show();
				d.container.slideDown('slow', function () {
					setTimeout(function () {
						var h = $("#dealerlogin-modal-data", self.container).height()
							+ title.height()
							+ 20; // padding
						heightt = h + 30;
						d.container.animate(
							{height: h}, 
							200,
							function () {
								$("div.close", self.container).show();
								$("#dealerlogin-modal-data", self.container).show();
							}
						);
					}, 300);
				});
			});
		},
		close: function (d) {
			var self = this; // this = SimpleModal object
			d.container.animate(
				{top:"-" + (d.container.height() + 20)},
				500,
				function () {
					self.close(); // or $.modal.close();
				}
			);
		}
	};

	dealerlogin.init();

});

var responseOpened = false;

// When DOM is ready
$(document).ready(function(){

    // When the form is submitted
    $("#login").submit(function(){

        // disable 'Submit' Button
//        $('#submit').attr('disabled', 'disabled');

        // Show Gif Spinning Rotator
//        $('#ajax_loading').show();

        // 'this' refers to the current submitted form
        var str = $(this).serialize();

        // -- Start AJAX Call --
        $.ajax({
            type: "POST",
            url: "do_login.php",  // Send the login info to this page
            data: str,
            success: function(msg){

                $("#login").ajaxComplete(function(event, request, settings){
                    // enable 'Submit' Button
//                    $('#submit').removeAttr('disabled');

                    // Hide Gif Spinning Rotator
//                    $('#ajax_loading').hide();

                    if (msg.indexOf('OK') != -1) // login okay
                    {
                        var login_response = '<div id="logged_in">' +
                        '<div style="width: 350px; float: left; margin-left: 70px;">' +
                        '<div style="width: 40px; float: left;">' +
                        '<img style="margin: 5px 0px 10px 0px;" width="50" height="50" align="absmiddle" src="images/success.png">' +
                        '</div>' +
                        '<div style="margin: 5px 0px 0px 0px; float: right; width: 300px;">'+
                        "You have successfully logged in! <br /> Please wait while you're redirected...</div></div>";

                        $('.simplemodal-close').hide();
                        $('#login').hide();

						$('#login_response').css('margin-top', '-10px');
						$('#login_response').css('margin-left', '-70px');
                        dealerLoginModal.container.animate({width:450, height:150, left: dealerLoginModal.container.position().left + 75}, 400, function() {
                        	$('#login_response').html(login_response); // Refers to 'status'
                        });

                        // After 3 seconds redirect the user to his/her splash page
                        setTimeout('go_to_private_page("' + msg.substring(2, msg.length) + '")', 3000);
                    }
                    else // ERROR?
                    {
                        var login_response = '<img src="images/error.png" width="16" height="16" /> ' + msg;
//                        if (!responseOpened) $('#login_response').slideDown(400, function(){$('#login_response').html(login_response)});
//                        else
                        $('#login_response').html(login_response);
                        if ($('#login_response').height() != 25) $('#login_response').height(0);
						$('#login_response').animate({height:25}, 400);
						var title = $("#dealerlogin-modal-title", self.container);
						var h = $("#dealerlogin-modal-data", self.container).height()
							+ title.height()
							+ 50; // padding
//						if (responseOpened) h -= 25;
                        dealerLoginModal.container.animate({height:heightt}, 400);
                        responseOpened = true;
                    }

                });

            }

        });

        // -- End AJAX Call --

        return false;

    }); // end submit event

});




function go_to_private_page(page)
{
    window.location = page; // Members Area
}

