// public/javascripts/application.js
jQuery.ajaxSetup({ 
  'beforeSend': function(xhr) {xhr.setRequestHeader("Accept", "text/javascript")}
})

jQuery.fn.submitWithAjax = function() {
  this.submit(function() {
    $.post(this.action, $(this).serialize(), null, "script");
    return false;
  })
  return this;
};

$(document).ready(function() {
	$(".no-javascript").hide()
	if($("#send_new_password")){
		$("#send_new_password").submitWithAjax();
	}  
	collectFacyImages();
	place_footer();
});

function collectFacyImages(){
	$('.images_holder').each(function(){
		$('a.ramme',this).fancybox({
			'overlayShow': true 
		});
	});
	$('.fancy').each(function(){
		$(this).fancybox({
			'overlayShow': true 
		});
	});

}

function startFancybox(obj){
	$(".images_holder > a:first", $(obj).parent()).click()
}

function place_footer(){
	$("html").css("background-color","#eee");
	$("body").css("background-color","#FFF");
}

function share_on_facebook(title,url) {
		if (url == false) { url = encodeURIComponent(location.href) };
		if (title == false) { title = encodeURIComponent(document.title) };
		window.open('http://www.facebook.com/sharer.php?u='+url+'&t='+title ,'sharer','toolbar=0,status=0,width=626,height=436');
		return false;
}
function share_on_email(title,url) {
	
		if (url == false) { url = encodeURIComponent(location.href) };
		if (title == false) { title = encodeURIComponent(document.title) };		
		$("#dialog").append('<form id="notify_form" name="notify_form"></form>')
		$("#notify_form").append('<label for="name">Din epost</label>')
		$("#notify_form").append('<input type="text" name="sender" id="sender" class="text ui-widget-content ui-corner-all" style="width:95%"/><br /><br />')
		$("#notify_form").append('<label for="email">Mottakers epost</label>')
		$("#notify_form").append('<input type="text" name="reciver" id="reciver" value="" class="text ui-widget-content ui-corner-all" style="width:95%"/><br /><br />')
		$("#notify_form").append('<label>Medling til mottaker </label>')
		$("#notify_form").append('<textarea name="comment" style="width:95%;" rows=4 id="comment" class="text ui-widget-content ui-corner-all" ></textarea>')
		$("#notify_form").append('<input type="hidden" name="title" value="'+title+'" />')
		$("#notify_form").append('<input type="hidden" name="url" value="'+url+'" />')
		$("#dialog").dialog({
			bgiframe: false,
			autoOpen: false,
			height: 350,
			width:500,
			modal: true,
			buttons: {
				send : function() {
					if ($('#sender').attr("value") == "" || $('#reciver').attr("value") == ""){ 
						$('#dialog').prepend("<div class='feil'>Både sender og mottaker må fylles inn</div>")
					} else {
						$.get("/javascripts/notify.js", $("#notify_form").serialize() , null, "script");
					}
				}
			},
			close: function() {
				$("#dialog").remove()
			}
		});
		$('#dialog').dialog('open');
		return false;

}