$(function() {
	quoteHolders();
});

$(window).bind('load', function() {
	var startPageBoxes = $('.startpage-box');
	if (startPageBoxes.length == 0)
		return;
	
	startPageBoxes.each(function() {
		var box = $(this),
			quote = $('.startpage-quote', box.parent()),
			both = $(this).add(quote);
		
		//
		// cache some used variables
		box._outerHeight = box.outerHeight() + 10;
		
		//
		// build some css
		$(box.parent()).css('position', 'relative');
		
		//
		// create overlay link.
		var link = $('<a href="' + box.find('a').attr('href') + '">&nbsp;</a>');
		link.css({
			position: 'absolute',
			top: 0,
			left: 0,
			width: box.outerWidth(),
			height: box.outerHeight(),
			textDecoration: 'none'
		});
		
		$(box.parent()).append(link);
		
		box.css({
			position: 'absolute',
			top: 0,
			left: 0,
			backgroundColor: '#fff'
		});
		
		quote.css({
			position: 'absolute',
			top: -box._outerHeight,
			left: 0,
			backgroundColor: '#fff'
		});
		
		//
		// activate hover
		var overAnimateSpeed = 600,
			outAnimateSpeed = 300;
			
		$(box.parent()).hover(function() {
			box.stop().animate({top: box._outerHeight}, overAnimateSpeed);
			quote.stop().animate({top: 0}, overAnimateSpeed);
		}, function() {
			box.stop().animate({top: 0}, outAnimateSpeed);
			quote.stop().animate({top: -box._outerHeight}, outAnimateSpeed);
		});
	});
});


var quoteHolders = function() {
	//
	// behöver inte köras om det inte finns några citat.
	var quoteHolderText = $('.quoteHolderText'),
		quoteHolder = $('.quoteHolder');
	
	if (quoteHolderText.length == 0)
		return;
	
	quoteHolder.css({left: 0});
	quoteHolderText.css({
		'width': 210,
		// height: quoteHolderText.height(),
		overflow: 'hidden',
		'text-overflow': 'ellipsis-word'
	});
	
	var readMore = $('<a href="#">Läs mer</a>'),
		readMoreContainer = null;
	
	quoteHolderText.find('.citatxxxbrodtext:not(:first)').hide();
	(readMoreContainer = $('<p class="citatxxxbrodtext quote-readmore"></p>').append(readMore)).insertAfter(quoteHolderText.find('.citatxxxbrodtext:last'));
	
	var quoteHolderTextHolders = quoteHolderText.find('>div');
	var toggler = true;
	
	readMore.click(function() {
		if (toggler) {
			// readMoreContainer.hide();
			
			readMore.text('Stäng');
			quoteHolder.stop().animate({left: -243}, 400);
			quoteHolderTextHolders.hide();
			quoteHolderText.stop().animate({width: 453}, 400, function() {
				quoteHolderText.find('.citatxxxbrodtext:not(:first)').show();
				quoteHolderTextHolders.fadeIn('fast');
			});
		} else {
			quoteHolderText.find('.citatxxxbrodtext:not(:first)').hide();
			readMoreContainer.show();
			readMore.text('Läs mer');
			quoteHolderTextHolders.hide();
			quoteHolder.stop().animate({left: 0}, 400);
			quoteHolderText.stop().animate({width: 210}, 400, function() {
				quoteHolderTextHolders.fadeIn('fast');
			});
		}
		
		toggler = !toggler;
	});
	
	/*quoteHolder.hover(function() {
		quoteHolder.stop().animate({left: -243}, 400);
		quoteHolderText.stop().animate({width: 453}, 400, function() {
			quoteHolderText.find('.citatxxxbrodtext:not(:first)').show();
		});
	}, function() {
		quoteHolderText.find('.citatxxxbrodtext:not(:first)').hide();
		quoteHolder.stop().animate({left: 0}, 400);
		quoteHolderText.stop().animate({width: 210}, 400);
	});*/
}
