﻿/**
* jquery.readmore - Substring long paragraphs and make expandable with "more" link
* @date 7 July 2010
* @author Jake Trent  http://www.jtsnake.com/
* @version 1.1
*/

var newsBodyHeight, newsSlidesHeight, newsSlidesContainerHeight, newsSlideHeight,openFlag;
var newsMoreRowHeight = 20;
var largestHeight = 0;

(function ($) {
	$.fn.readmore = function (settings) {

		this.each(function () {
			if ($(this).find('table').height() > largestHeight)
				largestHeight = $(this).find('table').height();

			if ($(this).height() < $(this).find('table').height()) {
				$(this).find('.newsText').height($(this).height() - $(this).find('.newsHeaderRow').height() - 22);
				$(this).find('.newsContentRow').height($(this).height() - $(this).find('.newsHeaderRow').height() - 22);
				$(this).find('.newsMoreRow').height(newsMoreRowHeight);
				$(this).find('.newsMore').html('... <a class="more">閱讀更多</a>');

				linkage($(this));
				openFlag = 0;
			}
		});

		newsBodyHeight = $('#newsBody').height();
		newsSlidesHeight = $('#newsSlides').height();
		newsSlidesContainerHeight = $('#newsSlides').find('.slides_container').height();
		newsSlideHeight = $('#newsSlides').find('.slides_container').find('.newsSlide').height();

		function linkage(elem) {
			elem.find(".more").click( function () {
				if (openFlag == 0) {
					$('#newsBody').animate({height:largestHeight+80}, 500);
					$('#newsSlides').animate({height:largestHeight+60}, 500);
					$('#newsSlides').find('.slides_container').animate({height:largestHeight+30}, 500);
					$('#newsSlides').find('.slides_container').find('.newsSlide').height(largestHeight+30);

					$.each($(".newsSlide"), function() {
						$(this).find('.newsContentRow').animate({height:'100%'}, 500);
						$(this).find('.newsText').animate({height:'100%'}, 500);

						if ($(this).find(".more").html() != '') {
							$(this).find(".more").html('<a class="more">隱藏</a>');
						}
					});
					openFlag = 1;
				} else {
					$('#newsBody').animate({height:newsBodyHeight}, 500);
					$('#newsSlides').animate({height:newsSlidesHeight}, 500);
					$('#newsSlides').find('.slides_container').find('.newsSlide').height(newsSlideHeight);
					$('#newsSlides').find('.slides_container').animate({height:newsSlidesContainerHeight}, 500);

					$.each($(".newsSlide"), function() {
						if ($(this).css('display') == 'none') {
							currentCell = false;
						} else {
							currentCell = true;
						}
						if (!currentCell) $(this).css('display','block');
						refHeight = $(this).height() - $(this).find('.newsHeaderRow').height() - 22;
						$(this).find('.newsContentRow').animate({height:refHeight}, 500);
						$(this).find('.newsText').animate({height:refHeight}, 500);
						if (!currentCell) $(this).css('display','none');

						if ($(this).find(".more").html() != '') {
							$(this).find(".more").html('<a class="more">閱讀更多</a>');
						}
					});
					openFlag = 0;
				}
			});
		}

		return this;
	};
})(jQuery);
