/**
 * jQuery addon
 */
jQuery.prototype.showverb = function(flag)
{
	if (flag)
	{
		$(this).show();
	}
	else
	{
		$(this).hide();
	}
};

/**
 * jQuery addon:
 * show an element and slowly fade it away
 * used for success messages, etc.
 */
jQuery.prototype.showAndFade = function()
{
	$(this).show();
	$(this).fadeOut(10000);
};

/**
 * baseUrl()
 *
 * Returns the baseUrl, compareable to the baseUrl() zend view helper
 * in the FE-BE.
 *
 * @return string baseUrl
 */
baseUrl = function (path) {
	var url = $("head base").attr("href");
	if ("string" != typeof url) {
    	url = "//!!/js/baseUrl()/failed/!!//";
  	}
  	if (undefined != typeof path) {

		if (path.substring(0,4) == 'http')
			return path;

  		url += path;
  	}

	return url;
};

/**
 * translate()
 *
 * FE-FE translate "view helper".
 */
translate = function (text) {
	return text;
};

/**
 * loginLightbox
 */
loginLightbox = {
	link : null,
	elem : 'a.loginlink',
	elen : 'a[href$=/user/auth/loginbox]',
	init : function()
	{
		$(loginLightbox.elem).attr('accesskey', '1');
		$(loginLightbox.elem).click(loginLightbox.open);
	},
	keydown: function (e) {
	},
	open: function(link)
	{
		loginLightbox.link = link;

		// $(this) works, $(link) not!
		$(this).keydown(function(e){
			loginLightbox.keydown(e);
		});

		// login box
		var lb_width = '380';
		// var lb_height = '180';
		var lb_left = ($('body').width() / 2) - (lb_width / 2) + 200;
		var url = baseUrl('user/auth/loginbox');
		var lb_left = ($('body').width() / 2) - (lb_width / 2);
		var lb_middle = ($('body').width() / 2);

		if (url.substring(0,5) != 'https') {
			window.location.href = baseUrl($('a.loginlink').attr('href').substring(0, 8 + $('a.loginlink').attr('href').substr(8).indexOf('/')) +  window.location.href.substr(8+window.location.href.substr(8).indexOf('/'))) +'#openlogin';
			return false;
		}

		$('.lb-login-content a.b-close').click(loginLightbox.close());

		var style = ' style="left:'+lb_left+'px; top: -500px; width:'+lb_width+'px;"';

		$('body').append('<div id="lb-login-background"></div>');

		$('#lb-login-background').click(loginLightbox.close);

		$('#lb-login-background').fadeTo(400, 0.7, function(){
			$('div.lb-login-content').animate({width: lb_width, left: lb_left},400,function(){
				$(this).animate({top: '100'},400);
			});
		});

		$('body').append
		(
			'<div id="lb-login-content" class="login"' + style + '>' +
				'<h1>' + translate('Anmelden') + '</h1>' +
				'<a id="lb-login-close" class="iconbutton b-close" href="javascript:loginLightbox.close();" title="' +
				translate('Anmeldung schliessen') + '">x</a><div class="loader"></div>' +
			'</div>'
		);

		$('#lb-login-content').keydown(loginLightbox.keydown);

		$('#lb-login-content .loader').load(url, function()
		{
			$(this).addClass('done');
		}
		, "html");

		$('#lb-login-content').animate({top: 40}, 400);
		return false;
	},
	close: function()
	{
		$('#lb-login-content').fadeOut(400, function(){
			$(this).remove();
		});
		$('#lb-login-background').fadeTo(400,0,function(){
			$(this).remove();
		});
	}
};

$(document).ready(function(){
	loginLightbox.init();
	checkForSupportedBrowser();

	if (window.location.href.match(/#openlogin$/) == '#openlogin') {
		loginLightbox.open();
	}

	portraitbox();
});

function portraitbox() {
	portraitboxlast = false;
	$('.teaser-area .box').each(function(index) {
		if ($(this).hasClass('portrait-box') == false) {
			portraitboxlast = false;
		} else {
			if ($(this).hasClass('portrait-box') == true) {
				if (portraitboxlast == true) {
					height1 = $('.teaser-area .box:eq('+(index-1)+') .boxwrap p').height();
					height2 = $('.teaser-area .box:eq('+index+') .boxwrap p').height();

					if (height1 > height2) {
						$('.teaser-area .box:eq('+(index)+') .boxwrap p').height(height1);
					}
					if (height1 < height2) {
						$('.teaser-area .box:eq('+(index-1)+') .boxwrap p').height(height2);
					}
					portraitboxlast = false;
				} else {
					portraitboxlast = true;
				}
			}
		}
	});
	$('.landscape-box a img').each(function(index){
		boxheight = $(this).parent().parent().height();
		imgheight = $(this).height();
		$(this).css('top',Math.round((boxheight-imgheight)/2));
	});
}

function recall(element, url, messageId) {
	$(element)
		.text('')
		.unbind('click')
		.attr('href', '#')
		.removeClass('morelink')
		.addClass('indicator-link')
		.addClass('indicator');

	$('.singlemessage').load(
		url + '/message_id/' + messageId + ' .singlemessage',
		null,
		function(){
			$('#message-fade').fadeOut(10000);
		}
	);

	return false;
}
