function open_simplecms_tab(id, height){
	jQuery('#'+id).animate({'height': height},750,function(){
		jQuery('#'+id).removeClass('loading');
		jQuery('#'+id).find('div.mp-content').animate({'opacity':1},450,function(){
			/* AND THEN ...? */
		});
	});
}
function start_liberty_loading(id){
    jQuery('#'+id).find('div.mp-content').animate({'opacity':0},450);
    jQuery('#'+id).animate({'height': 17},750,function(){
        jQuery('#'+id).addClass('loading');
    });
}
function end_liberty_loading(id,content){
    jQuery('#'+id).find('div.mp-content').css({'opacity':0,'margin-top':-800}).html(content);
    var content_height = jQuery('#'+id).find('div.mp-content').height();
    jQuery('#'+id).find('div.mp-content').animate({'margin-top':0},450,function(){
        /* AND THEN ...? */
    });
    number_of_images = jQuery('#'+id).find('div.mp-content img').length;
	image_count = 0;
	if(number_of_images>0){
		jQuery('#'+id).find('div.mp-content img').each(function(){
			this_src = $(this).attr('src');
			$(this).attr('src', this_src).load(function(){
				image_count++;
				content_height = jQuery('#'+id).find('div.mp-content').height();
				if(image_count==number_of_images){
					open_simplecms_tab(id, content_height);
				}
			});
		});
	}else{
		open_simplecms_tab(id, content_height);
	}
}
jQuery(document).ready(function(){
    jQuery('a#login-toggle').live('click',function(e){
        e.preventDefault();
        jQuery('form#mp-login-form').toggle('slow');
    });
    jQuery('nav#main-navigation a').live('click',function(e){
        e.preventDefault();
        var slug_id = jQuery(this).attr('data-slug-id');
        var mongoID = jQuery(this).attr('data-mongo-id');
        var nonce = jQuery(this).attr('data-nonce');
        var selected_page = jQuery(this).attr('data-id');
        var current_page = jQuery('nav#main-navigation').find('a.current').attr('data-id');
        if(selected_page!=current_page){
            start_liberty_loading('content-wrapper');
            jQuery('nav#main-navigation').find('a.current').removeClass('current');
            jQuery(this).addClass('current');
            jQuery.ajax({
                url:mp_root_url+'mp-includes/ajax/get-object.php',
                data:({ mongo_id: mongoID, nonce: nonce, use_mongo_meantime: 'true', shortcodes: true }),
                type: "POST",
                dataType: 'json',
                success: function(result){
                    end_liberty_loading('content-wrapper',result.content);
                }
            });
        }
    });
});

/* GOOGLE MAPS STUFF */
function initialize() {
  var myLatlng = new google.maps.LatLng(51.5045,-0.091002);
  var myOptions = {
    zoom: 14,
    center: myLatlng,
    mapTypeId: google.maps.MapTypeId.ROADMAP
  }
  var map = new google.maps.Map(document.getElementById("find-us-map"), myOptions);
  var marker = new google.maps.Marker({
      position: myLatlng,
      title:"Liberty Resourcing"
  });
  marker.setMap(map);  
}
function loadScript() {
  var script = document.createElement("script");
  script.type = "text/javascript";
  script.src = "http://maps.googleapis.com/maps/api/js?sensor=false&callback=initialize";
  document.body.appendChild(script);
}
window.onload = loadScript;
/* END OF GOOGLE MAPS API */
