function wait() {
    $('body').append('<div id="loader"><div>');
    $("#loader").slideDown('normal');
}

function done() { 
    $("#loader").slideUp('fast').remove();
}

function close_twitter() {
            $('#t_closeme').remove('#t_closeme');

            $('#twitter_open').html('Twitter &darr;');

            $('#twitter').animate({
                'top': '-330px'
            }, 750, function() {
                $("body").append('<div class="twitter_open fixed" id="clone"></div>');
            });

            twitteropened = 0;
}


function close_contact()
{
	$('#c_closeme').remove('#c_closeme');

	$('#contact_open').html('Contact &darr;');

	$('#contact_box').animate({
		'top': '-370px'
	}, 750);

	contactopened = 0;
}

function close_facebook()
{
	$('f_closeme').remove('#f_closeme');

	$('#facebook_open').html('Facebook &darr;');

	$('#facebook_box').animate({
		'top': '-388px'
	}, 750);

	facebookopened = 0;
}

// function Duplicate
$.fn.duplicate = function(count, cloneEvents) {
    var tmp = [];
    for ( var i = 0; i < count; i++ ) {
        $.merge( tmp, this.clone( cloneEvents ).get() );
    }
    return this.pushStack( tmp );
}


$(document).ready(function(){


   // marker to load twitter feeds on first block open
   twitterloaded = 0;
   twitteropened = 0;
   contactopened = 0;
   facebookopened = 0;

   $("body").append('<div class="twitter_open fixed" id="clone"></div>');

   $('#t_closeme').live('click', function(){

       close_twitter();

   });


   $('#c_closeme').live('click', function(){

       close_contact();

   });

   $('#f_closeme').live('click', function(){
	  close_facebook();
   });


   $('.twitter_open').live("click",

        function() {

            if (twitteropened == 0) {

            close_contact();

            $("#clone").remove();


            if (twitterloaded == 0) {

                $("#t_container").getTwitter({
                        userName: "themlondon",
                        numTweets: 4,
                        loaderText: "Loading tweets...",
                        slideIn: false,
                        showHeading: false,
                        headingText: "Latest Tweets",
                        showProfileLink: false
                });

                twitterloaded = 1;

            }

            $('#twitter_open').html('Twitter &uarr;');

            $('body').append('<div id="t_closeme"></div>');

            $('#twitter').animate({
                'top': $('header').height() + 'px'
            }, 1000);

            twitteropened = 1;

        }

        else {

            close_twitter();
        }
        }
    );




    $('#contact_open').click(

        function() {

			if (contactopened == 0) {

				close_twitter();

				$('#contact_open').html('Contact &uarr;');

				$('body').append('<div id="c_closeme"></div>');

				$('#contact_box').animate({
					'top': $('header').height() + 'px'
				}, 1000);

				contactopened = 1;

			}

			else {

				close_contact();

			}
		}
    );

	$('#facebook_open').click(function(){
		if(facebookopened == 0)
		{
			close_facebook();

			$('#facebook_open').html('Facebook &uarr;');

			$('body').append('<div id="f_closeme"></div>');

			$('#facebook_box').animate({
				'top': $('header').height() + 'px'
			}, 1000);

			facebookopened = 1;
		}
		else
		{
			close_facebook();
		}
	});
        
    var twitterlink = '<a href="http://twitter.com/themlondon" target="_blank" style="float: right; text-decoration: none"><img src="/images/icon_twitter.gif" alt="Twitter" /></a>';     
    $('#t_blue p').append(twitterlink);
    
	$('#contact_box form').submit(
		function() {
			c_form = document.forms['contact_form'];
			c_name = c_form.name.value;
			c_email = c_form.email.value;
			c_message = c_form.message.value;

			url = '/page/contact/';
			data = '&name=' + escape(c_name) + '&email=' + escape(c_email) + '&message=' + escape(c_message);

			$.ajax({
				url: url,
				data: data,
				type: 'POST',
				success: function(response)
				{
					$('#contact_notification').css('display', 'none');
					$('#contact_error').css('display', 'none');
					
					if(response != 'ok')
					{
						$('#contact_error').html(response);
						$('#contact_error').show(1000);
					}
					else
					{
						$('#contact_notification').html('Your message was sent.');
						$('#contact_notification').show(1000);
					}
				}
			});

			return false;
		}
	);
});
