var Loren = {
	calendar: new Array(),
	months: new Array('Januar', 'Februar', 'Mars', 'April', 'Mai', 'Juni', 'Juli', 'August' ,'September', 'Oktober', 'November', 'Desember'),
	init: function(page) {
		var self = this;
		Cufon.replace('h1')('h2')('h3')('A.heading');

		$('#presentation IMG#header').click($.proxy(this.togglePresentation, this));

		if (page == 'front') {
			$('#calendar_popup .left IMG').click(function() { self.showCalendar(--self.curcal); });
			$('#calendar_popup .right IMG').click(function() { self.showCalendar(++self.curcal); });
		} else {
			$('.gallery a').fancybox({
				'titlePosition': 'inside'
			});


			$('#fb_post_comment_text').attr('value', '');
		
			$('#footer').ajaxError(function(e, xhr, settings, exception) {
				response = xhr.responseText;
				if (settings.data) {
					if (response.status == 'ok') {
					}
				}
			});
		}
	},

	/*********************
	Media links
	*********************/
	toggleMedia: function() {
    if (this.MediaExpanded) return this.collapseMedia()
    else return this.expandMedia();
	},

	expandMedia: function() {
		this.MediaExpanded = true;
		$('#btnMediaMore').attr({
			'src': '/wp-content/themes/loren/images/btnMindre.png',
			'width': 65
		}).parent().attr('title','Se mindre');

		$('.media UL LI').show('normal');
		return false;
	},

	collapseMedia: function() {
		this.MediaExpanded = false;
		$('#btnMediaMore').attr({
			'src': '/wp-content/themes/loren/images/btnFlere.png',
			'width': 52
		}).parent().attr('title','Se flere');

		$('.media UL LI').each(function(i) {
			if (i > 2) $(this).hide('normal');
		});
		return false;
	},

	togglePresentation: function() {
		if ($('#presentation').height() <= 110) {
			this.showPresentation();
		} else {
			this.closePresentation();
		}
	},

	showPresentation: function() {
		$('#flashy').flash({
			src: '/flash/loren_presentasjon.swf',
			width: 1003,
			height: 590,
			wmode: 'opaque'
		});
		$('#presentation IMG#header').hide();
		$('#presentation').animate({height: 590}, 1000).css('overflow','visible');
		$('#flashy').animate({height: 590}, 1000);
	},

	closePresentation: function() {
    $('#overlay').fadeTo('normal', 0, function() { $('#overlay').hide(); });
		$('#presentation').animate({height: 80}, 900, function() {
			$('#presentation IMG#header').show();
		}).css('overflow','visible');

		$('#flashy').animate({height: 0}, 1000);
	},

	showThumb: function(nr, params) {
		var p = params.split(',');
		var img = new Image();

		$(img).load(function() {
			$('#photo_popup .inner').html('').append($(this));
			$('#photo_popup').center().fadeTo('fast', 1);
		}).attr('src', p[nr]);
		$('#overlay').height($(document).height()).fadeTo('normal', .8);
	},

	closePhoto: function() {
		$('#overlay, #photo_popup').fadeTo('normal', 0, function() {
      $('#overlay, #photo_popup').hide();
    });
	},

	showCalendar: function(id) {
		if (id < 1) id = 1;
		if (id > this.calendar.length) id = this.calendar.length;

    this.curcal = id;
    var cal = $('#calendar_popup .inner');
		

    $('#calendar_popup .inner .head H1').text(this.calendar[id-1].Title);
		$('#calendar_popup .inner .head SPAN').text(this.calendar[id-1].Sted);
    $('#calendar_popup .inner .dato .day').text(this.calendar[id-1].Day);
    $('#calendar_popup .inner .dato .month').text(this.calendar[id-1].Month);

		Cufon.refresh('h1');



    if (this.calendar[id-1].Picture) {
			var c = this.calendar[id-1].Content;
			if (c.indexOf(this.calendar[id-1].Picture) < 0) {
				$('#calendar_popup .inner .text').html('<img src="' + this.calendar[id-1].Picture + '" />')
			} else $('#calendar_popup .inner .text').html('');
		} else $('#calendar_popup .inner .text').html('');


    $('#calendar_popup .inner .text').append(this.unescapeHTML(this.calendar[id-1].Content));
		$('#calendar_popup .left, #calendar_popup .right').css('padding-top', ($('#calendar_popup').height() / 2)-15);

    $('#overlay').height($(document).height()).fadeTo('normal', .8);
    $('#calendar_popup').center().fadeTo('fast', 1, function() {
    	$('#calendar_popup .left, #calendar_popup .right').css('padding-top', ($('#calendar_popup').height() / 2)-15);
		});
	},

	unescapeHTML: function(html) {
		return $("<div />").html(html).text();
	},

	showCalendarID: function(id) {
		for (var i=0;i<this.calendar.length;i++) {
			if (this.calendar[i].PostID == id) {
				this.showCalendar(i);
			}
		}
		return false;
	},

	closeCalendar: function() {
		this.hideCalendar();
		return false;
	},

	hideCalendar: function() {
		$('#overlay, #calendar_popup').fadeTo('normal', 0, function() {
			$('#overlay, #calendar_popup').hide();
		});
	},

	toggleCalendar: function() {
		if (this.CalExpanded) return this.collapseCalendar()
		else return this.expandCalendar();
	},

	expandCalendar: function() {
		this.CalExpanded = true;
		$('#btnCalMore').attr({
			'src': '/wp-content/themes/loren/images/btnMindre.png',
			'width': 65
		}).parent().attr('title','Se mindre');

		$('#calendar .calendar').show('normal');
		return false;
	},

	collapseCalendar: function() {
		this.CalExpanded = false;
		$('#btnCalMore').attr({
			'src': '/wp-content/themes/loren/images/btnFlere.png',
			'width': 52
		}).parent().attr('title','Se flere');

		$('#calendar .calendar').each(function(i) {
			if (i > 2) $(this).hide('normal');
		});
		return false;
	},


	Share: function(page, link, title) {
		if (page == 'Facebook') {
			window.open('http://www.facebook.com/sharer.php?u=' + encodeURIComponent(link), 'sharer', 'toolbar=0,status=0,width=626,height=436');
		} else if (page == 'Twitter') {
			window.open('http://twitter.com/home?status=' + encodeURIComponent(link + ' - ' + title), 'sharer', 'toolbar=0,status=0,width=790,height=460');
		}

		return false;
	},

	showLinks: function(id,self) {
		$('#overlay').height($(document).height()).fadeTo('normal', .8);
		title = $(self).text();
		$('#links'+id+' H1').text(title.replace("+ ", ""));
		$('#links'+id).center().fadeTo('fast', 1, function() {
		});
		return false;
	},

	closeLinks: function() {
		$('#overlay, #links7, #links8, #links9, #links10, #links11, #links12').fadeTo('normal', 0, function() {
			$('#overlay, #links7, #links8, #links9, #links10, #links11, #links12').hide();
		});
		return false;
	},

	/**********************
	Facebook functions
	**********************/
	FBinit: function() {
		var self = this;

		FB.init({appId: '108120762571318', status: true, cookie: true, xfbml: true});
		FB.Event.subscribe('auth.login', function(response) {
			$('#guest').hide();
			self.FBShowUserInfo();
			$('#logged_in').show();
		});

		self.FBShowUserInfo();
	},

	FBShowUserInfo: function() {
		var self = this;
		FB.getLoginStatus(function(response) {
			if (response.authResponse) {
				self.session = response.authResponse;
				FB.api({ method: 'fql.query', query: 'SELECT name, pic_square FROM profile WHERE id=' + response.authResponse.userID }, function(result) {
					var user = result[0];

					$('#user_info').html('<img src="' + user.pic_square + '"><div>Du er logget inn som ' + user.name + '</div>');
					Cufon.replace('#user_info DIV');
					self.name = user.name;
					$('#guest').hide();
					$('#logged_in').show();
				});
			}
		});
	},

	FBPostComment: function() {
		if ($('#fb_post_comment_text').attr('value') == '') {
			alert('Kommentar boksen er tom!');
			return;
		}

/*		$.getJSON('/', {
			json: 'submit_comment',
			post_id: this.postID,
			name: this.name,
			email: this.session.userID + '@loren.no',
			content: $('#fb_post_comment_text').attr('value')
		});
*/
		if ($('#fb_comments').length == 0) {
			$('#sidebar UL:last-child').append('<li id="fb_comments">');
			$('#fb_comments').append('<h3>1 kommentar til denne artikkelen:</h3>');
		} else {
			var cnt = $('#fb_comments H3').text().substring(0,2);
			cnt++;
			$('#fb_comments H3').text(cnt + ' kommentarer til denne artikkelen');
		}

		Cufon.refresh('h3');
		$('#fb_comments').append('<div class="comment"><img src="http://graph.facebook.com/' + this.session.userID + '/picture?type=square" /><span class="name">' + this.name + '</span><p>' + $('#fb_post_comment_text').attr('value') + '</p><div class="clear"></div></div>');

		if ($('#fb_post_comment_share').attr('checked')) {
			FB.api({
				method: 'fql.query',
				query: 'SELECT publish_stream FROM permissions WHERE uid=' +response.authResponse.userID
			}, function(response) {
				if (response[0].publish_stream == 0) {
//				if (response == 0) {
					FB.login(function(response) {
						if (response.perms == 'publish_stream') {
              FB.api({
                method: 'stream.publish',
                message: $('#fb_post_comment_text').attr('value'),
                attachment: { 'name': document.title, href: document.location.href }
              });
							$('#fb_post_comment_text').attr('value','');
						}
					}, {scope: 'publish_stream'});
				} else if (response[0].publish_stream == 1) {
console.log('publish');
          FB.api({
            method: 'stream.publish',
            message: $('#fb_post_comment_text').attr('value'),
            attachment: { 'name': document.title, href: document.location.href }
          });
					$('#fb_post_comment_text').attr('value','');
				}
			});
		} else {
			$('#fb_post_comment_text').attr('value','');
		}

		return false;
	},

	FBLogout: function() {
		FB.logout(function() {
      $('#logged_in').hide();
			$('#guest').show();
			document.location.reload();
		});
		return false;
	}
}

window.fbAsyncInit = function() {
	Loren.FBinit();
};

jQuery.fn.center = function () {
  this.css("position","absolute");
  this.css("top", ( $(window).height() - this.height() ) / 2+$(window).scrollTop() + "px");
  this.css("left", ( $(window).width() - this.width() ) / 2+$(window).scrollLeft() + "px");
  return this;
}
