;(function(h){var m=h.scrollTo=function(b,c,g){h(window).scrollTo(b,c,g)};m.defaults={axis:'y',duration:1};m.window=function(b){return h(window).scrollable()};h.fn.scrollable=function(){return this.map(function(){var b=this.parentWindow||this.defaultView,c=this.nodeName=='#document'?b.frameElement||b:this,g=c.contentDocument||(c.contentWindow||c).document,i=c.setInterval;return c.nodeName=='IFRAME'||i&&h.browser.safari?g.body:i?g.documentElement:this})};h.fn.scrollTo=function(r,j,a){if(typeof j=='object'){a=j;j=0}if(typeof a=='function')a={onAfter:a};a=h.extend({},m.defaults,a);j=j||a.speed||a.duration;a.queue=a.queue&&a.axis.length>1;if(a.queue)j/=2;a.offset=n(a.offset);a.over=n(a.over);return this.scrollable().each(function(){var k=this,o=h(k),d=r,l,e={},p=o.is('html,body');switch(typeof d){case'number':case'string':if(/^([+-]=)?\d+(px)?$/.test(d)){d=n(d);break}d=h(d,this);case'object':if(d.is||d.style)l=(d=h(d)).offset()}h.each(a.axis.split(''),function(b,c){var g=c=='x'?'Left':'Top',i=g.toLowerCase(),f='scroll'+g,s=k[f],t=c=='x'?'Width':'Height',v=t.toLowerCase();if(l){e[f]=l[i]+(p?0:s-o.offset()[i]);if(a.margin){e[f]-=parseInt(d.css('margin'+g))||0;e[f]-=parseInt(d.css('border'+g+'Width'))||0}e[f]+=a.offset[i]||0;if(a.over[i])e[f]+=d[v]()*a.over[i]}else e[f]=d[i];if(/^\d+$/.test(e[f]))e[f]=e[f]<=0?0:Math.min(e[f],u(t));if(!b&&a.queue){if(s!=e[f])q(a.onAfterFirst);delete e[f]}});q(a.onAfter);function q(b){o.animate(e,j,a.easing,b&&function(){b.call(this,r,a)})};function u(b){var c='scroll'+b,g=k.ownerDocument;return p?Math.max(g.documentElement[c],g.body[c]):k[c]}}).end()};function n(b){return typeof b=='object'?b:{top:b,left:b}}})(jQuery);

var moodsExt;
$(document).ready(init);

function init() {
$(document.body).removeClass('jsOff').addClass("jsOn");

// $('#contactlink').click(function(e) { 
// popUpShow('contact');
// this.blur();
// cancelClick(e);
// });

if($('#content').hasClass('home') && !$('.navigation')[1]) {
	$(document.body).addClass("first");
} else if ($('#onThisPage')[0]) {
	var cont = $('#onThisPage ul')[0];
	$('#content div.post').each(function() {
		var $this = $(this);
		var aLink = $this.find('a[href^="http://moodsandappetites.com/?p="]')[0];
		var l = aLink.href.substring(aLink.href.indexOf('?p=')+3);
		var t = $(aLink).text();
		var onePost = document.createElement('a');
		var onePostLi = document.createElement('li');
		$(onePost).attr('href','#post-'+l).html(t)
		.click(function(e) {
			e.preventDefault();
				$.scrollTo( '#post-'+l, 1200, {  offset:-16 } );
			})
		.appendTo(onePostLi);
		$(onePostLi).appendTo(cont);
	});
}

	$('#recent a').each(function() {
		var l = this.href.substring(this.href.indexOf('?p=')+3);
		if($('#post-'+l)[0]) {
			//this.setAttribute('href','#post-'+l);
			$(this).click(function(e) {
			e.preventDefault();
				$.scrollTo( '#post-'+l, 1200, {  offset:-16 } );
			});
		}
		
	});
	
	
	$('#content p.backToTop a').click(function(e) {
		e.preventDefault();
		$.scrollTo( (0,0), 1200, { offset:-16 } );
	});

// switch(a) {
// 	case '?page_id=2': document.body.setAttribute('id','about'); break;                     
// 	case '?page_id=3': document.body.setAttribute('id','book'); break;
// 	case '': document.body.setAttribute('id','index'); break;  
// }


if ((jQuery.browser.safari) || (navigator.userAgent.indexOf("Camino") != -1)) { 
	
}

if (jQuery.browser.msie) {
	
};

var links = externalLinks('http://moodsandappetites');

links.each(function() {
	$(this).attr('target','moodslink').click(function(e) { 
	e.preventDefault();
	var l = this.href;
	if(moodsExt == null || moodsExt.closed)
	{
		moodsExt = window.open(l,'moodslinks'); moodsExt.focus();
	}
	else if(!moodsExt.closed)
	{
		moodsExt.focus();
		moodsExt.location = l;
	}
	});
})

	
};//end init


function getId() {
return document.body.getAttribute('id');
};

function externalLinks(base_url) {
	return $('a[href^=http]:not("[href^=' + base_url + ']")');
};

function prepareContactForm() {
	if (jQuery.browser.msie) {
		$('#amcf_submitBtn').hover(function() { 
		$(this).toggleClass("over"); },function() {
		$(this).toggleClass("over"); });
		$('#am_contactform input').focus(function() {$(this).addClass('ieFocus')});
		$('#am_contactform input').blur(function() {$(this).removeClass('ieFocus')});
		$('#am_contactform textarea').focus(function() {$(this).addClass('ieFocus')});
		$('#am_contactform textarea').blur(function() {$(this).removeClass('ieFocus')})
	};
	if($.browser.safari) {
		$("#am_contactform input").addClass('safari');
		$("#am_contactform textarea").addClass('safari');
	}
		$("#am_contactform input").focus( function() { 
			if($(this).next().is(':visible')) { $(this).next().hide('slow')}
		});
		var options = { beforeSubmit: validate, success: showResponse };
		$('#am_contactform').ajaxForm(options);
};

function validate(formData, jqForm, options) { 
	var form = jqForm[0];
	if (!form.amcf_name.value) { $("#amcf_nameErr").show(); }
	if (emailCheck(form.amcf_email.value)) { $("#amcf_emailErr").show(); }
	if (!form.amcf_name.value || emailCheck(form.amcf_email.value)) return false;
};

function showResponse()  {
	$('#am_contactform').unbind('submit'); $('#amcf_submitBtn').unbind('click');
	$('#am_contactform').hide('slow',function() {
	$('#amcf_thankYou').fadeIn('slow')});
};

function emailCheck(value) {
	return !value.match(/^[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i);
};

function cancelClick(e){
		if (window.event){
			window.event.cancelBubble = true;
			window.event.returnValue = false;
		}
		if (e && e.stopPropagation && e.preventDefault){
			e.stopPropagation();
			e.preventDefault();
		}
};

function setOpacity(obj, opacity) {
  opacity = (opacity == 100)?99.999:opacity;
  obj.style.filter="progid:DXImageTransform.Microsoft.Alpha(opacity="
+ opacity + ");";
  obj.style.KHTMLOpacity = opacity/100;
  obj.style.MozOpacity = opacity/100;
  obj.style.opacity = opacity/100;
};

function popUpShow(classN) {
	try {
		if (document.getElementById("popUpHideSelect") == null) {
		
		popUpHEIGHT = 340;
		if (classN=='contact') { 
				winStyle = "style='height:340px;background-color:#EABC8E;'"; 
		}
		
		$("body").append("<iframe id='popUpHideSelect' style='opacity:0'></iframe><div id='popUpOverlay'></div><div id='popUpWindow' "+winStyle+"></div>"); 
		$("#popUpOverlay").click(popUpRemove);
		setOpacity(document.getElementById("popUpHideSelect"), 0);
		setOpacity(document.getElementById("popUpOverlay"), 60);
	}
		
	$("#popUpWindow").append(contactForm());
	prepareContactForm();
	bindCloseButton();
	
	popUpPosition();

	$("#popUpWindow").css({display:"block"}); 
        
    document.onkeyup = function(e){   
      if (e == null) {
        keycode = event.keyCode;
      } else {
        keycode = e.which;
      }
      if(keycode == 27){
        popUpRemove();
      }  
    };
    
  } catch(e) {
    alert( e );
  }
};
function bindCloseButton() {
	$('#popUpClose').hover(function() {
			$(this).addClass("hover");
	}, function() {
			$(this).removeClass("hover");
	});
	$("#popUpClose").click(popUpRemove);
}
function popUpRemove() {
	$("#popUpOverlay").unbind();
	$("#popUpCloseWindow").unbind('hover');
	$("#popUpCloseWindow").unbind('click');
	$('#popUpWindow form').unbind('submit'); $('#popUpWindow button').unbind('click'); 
	$("#popUpWindow").fadeOut("fast",function(){$('#popUpWindow, #popUpOverlay, #popUpHideSelect').remove()});
	$("#popUpLoad").remove();
	return false;
};
function popUpPosition() {

	if ( !(jQuery.browser.msie && typeof XMLHttpRequest == "function") ) { 
        $("#popUpWindow").css({marginTop:'-'+popUpHEIGHT/2+"px"});
     }
};
function contactForm() {
var f = '<div id="popUpClose"></div>'+
'<form method="post" id="am_contactform" action="http://happilyunmarriedeverafter.com/wp-content/themes/nina/contact_x.php">'+
'<fieldset>'+
  '<legend>Contact form</legend>'+

      '<label for="amcf_name">Name</label>'+
      '<input id="amcf_name" name="amcf_name" />'+
      '<label for="amcf_name" id="amcf_nameErr">Please enter your name!</label>'+

      '<label for="amcf_email">Email</label>'+
      '<input id="amcf_email" name="amcf_email" />'+
      '<label for="amcf_email" id="amcf_emailErr">Please enter a valid email address!</label>'+
 
     '<label for="amcf_note">Note</label>'+
     '<textarea rows="5" cols="17" id="amcf_note" name="amcf_note"></textarea>'+
  
     '<button type="submit" id="amcf_submitBtn"></button>'+
  
'</fieldset>'+
'</form>'+
'<div id="amcf_thankYou"><p>Thank you for contacting us!</p></div>'
return f;
};

// $(window).load(function(){
// 
//    var preload = [
//    'http://moodsandappetites.com/wp-content/themes/default/images/linksPagesWhite.png'
//    ];		
// 
//  $(document.createElement('img')).bind('load', function(){
//     if(preload[0]) this.src = preload.shift();
//    }).trigger('load');
//    
// });