var speedScroll = 350;
var speedSlide = 250;
var speedAnimate = 150;

var limits = new Array();
limits = {
	tag: {
		maxWidth: 156
	},
	title: {
		maxWidth: 142
	}
};

$JQ(document).ready(function() {
	PEPSIMAXCAST.CONTENT.bindLinkPost();
	PEPSIMAXCAST.CONTENT.bindLinkComments();
	PEPSIMAXCAST.CONTENT.bindLinkPanel();
	PEPSIMAXCAST.CONTENT.checkDimensionsTags();
	PEPSIMAXCAST.CONTENT.checkDimensionsTitles();
	PEPSIMAXCAST.CONTENT.bindLinkTags();
	PEPSIMAXCAST.CONTENT.checkLinksPositions();
	PEPSIMAXCAST.CALENDAR.initialiseCalendars();
	LIBRARY.prepopulateFieldsText('fieldPrepopulated');
	LIBRARY.positionFooter($JQ('.pageInside'), $JQ('.footerInside'));
});

$JQ(window).resize(function() {
	LIBRARY.positionFooter($JQ('.pageInside'), $JQ('.footerInside'));
});

// *******************************************************************************************

PEPSIMAXCAST = {
};

// *******************************************************************************************

PEPSIMAXCAST.GENERIC = {
	getCurrentMonthName: function() {
		var months = new Array('January','February','March','April','May','June','July','August','September','October','November','December');
		var date = new Date();
		var day = date.getDate();
		var month = date.getMonth();
		var yy = date.getYear();
		var year = (yy < 1000) ? yy + 1900 : yy;
		year = year.toString().substring(2);
		return months[month] + " - " + year;
	},
	
	getMaxWidth: function(elementType) {
		var theMaxWidth = limits[elementType]['maxWidth'];
		return theMaxWidth;
	}
};

// *******************************************************************************************

PEPSIMAXCAST.CONTENT = {
	bindLinkPost: function() {
		var count = 0;
		$JQ('.post').each(function() {
			var eachElement = $JQ(this);
			count++;
			eachElement.attr('id', 'post' + count);
		});

		$JQ('a.linkPost').unbind('click');
		$JQ('a.linkPost').bind('click',function() {
			var theTarget = $JQ(this).parents('.contentItem');
			PEPSIMAXCAST.CONTENT.toggleItem(theTarget, function() {
				$JQ(window).scrollTo(theTarget, {duration: speedScroll, axis:'y'});
			});
			return false;
		});
	},

	bindLinkComments: function() {
		$JQ('a.linkComments').unbind('click');
		$JQ('a.linkComments').bind('click',function() {
			var theTarget = $JQ(this).parents('.contentItem');
			if ($JQ('.postFull', theTarget).css('display') != 'none') {
				if ($JQ('.comments', theTarget).css('display') != 'none') {
					PEPSIMAXCAST.CONTENT.closeComments(theTarget, function() {
						$JQ(window).scrollTo(theTarget, {duration: speedScroll, axis:'y'});
					});
				} else {
					PEPSIMAXCAST.CONTENT.openComments(theTarget, function() {
						$JQ(window).scrollTo($JQ('.comments', theTarget), {duration: speedScroll, axis:'y'});
					});
				}
			} else {
				PEPSIMAXCAST.CONTENT.toggleItem(theTarget, function() {
					if ($JQ('.comments', theTarget).css('display') != 'none') {
						$JQ(window).scrollTo($JQ('.comments', theTarget), {duration: speedScroll, axis:'y'});
					} else {
						$JQ(window).scrollTo(theTarget, {duration: speedScroll, axis:'y'});
					}
				});
			}
			return false;
		});
	},

	toggleItem: function(theElement, callback) {
		var count = 0;
		var closed = 0;
		$JQ('.contentItem').each(function() {
			var eachElement = $JQ(this);
			count++;
			if ($JQ('.post', eachElement).attr('id') != $JQ('.post', theElement).attr('id')) {
				if ($JQ('.postFull', eachElement).css('display') != 'none') {
					closed = 1;
					PEPSIMAXCAST.CONTENT.closeComments(eachElement, function() {
						PEPSIMAXCAST.CONTENT.closePost(eachElement, function() {
							PEPSIMAXCAST.CONTENT.openPost(theElement, function() {
								PEPSIMAXCAST.CONTENT.openComments(theElement, function() {
									if (callback) {
										callback.call();
									}
								});
							});
						});
					});
				}
			}

			if ((count == $JQ('.contentItem').size()) && (closed == 0)) {
				if ($JQ('.postFull', theElement).css('display') != 'none') {
					PEPSIMAXCAST.CONTENT.closeComments(theElement, function() {
						PEPSIMAXCAST.CONTENT.closePost(theElement, function() {
							if (callback) {
								callback.call();
							}
						});
					});
				} else {
					PEPSIMAXCAST.CONTENT.openPost(theElement, function() {
						PEPSIMAXCAST.CONTENT.openComments(theElement, function() {
							if (callback) {
								callback.call();
							}
						});
					});
				}
			}
		});
	},

	openPost: function(theElement, callback) {
		$JQ('.postExcerpt', theElement).slideUp(speedSlide, function(){
			$JQ('.postExcerpt', theElement).css('display', 'none');
			$JQ('.postText', theElement).css('height', 'auto');
			$JQ('.postText p a.linkPost', theElement).addClass('active');
			$JQ('.postFull', theElement).slideDown(speedSlide, function() {
				$JQ('.postFull', theElement).css('display', 'inline');
				if(typeof document.body.style.maxHeight === "undefined") {
					//$JQ('.post', theElement).css('padding-bottom', '0px');
				}
				PEPSIMAXCAST.CONTENT.checkLinksPosition(theElement);
				if (callback) {
					callback.call();
				}
			});
		});
	},

	closePost: function(theElement, callback) {
		$JQ('.postFull', theElement).slideUp(speedSlide, function(){
			$JQ('.postFull', theElement).css('display', 'none');
			$JQ('.postText', theElement).css('height', 'auto');
			$JQ('.postText p a.linkPost', theElement).removeClass('active');
			$JQ('.postExcerpt', theElement).slideDown(speedSlide, function() {
				$JQ('.postExcerpt', theElement).css('display', 'inline');
				if(typeof document.body.style.maxHeight === "undefined") {
					//$JQ('.post', theElement).css('padding-bottom', '0px');
				}
				PEPSIMAXCAST.CONTENT.checkLinksPosition(theElement);
				if (callback) {
					callback.call();
				}
			});
		});
	},

	openComments: function(theElement, callback) {
		$JQ('.postLinks p a.linkComments', theElement).addClass('active');
		$JQ('.comments', theElement).slideDown(speedSlide, function() {
			$JQ('.comments', theElement).css('display', 'inline');
			if(typeof document.body.style.maxHeight === "undefined") {
				$JQ('.comments', theElement).css('padding-bottom', '0px');
				$JQ('.commentsBorder', theElement).css('margin-bottom', '-9px');
			}
			if (callback) {
				callback.call();
			}
		});
	},

	closeComments: function(theElement, callback) {
		$JQ('.postLinks p a.linkComments', theElement).removeClass('active');
		$JQ('.comments', theElement).slideUp(speedSlide, function() {
			$JQ('.comments', theElement).css('display', 'none');
			if(typeof document.body.style.maxHeight === "undefined") {
				$JQ('.comments', theElement).css('padding-bottom', '0px');
				$JQ('.commentsBorder', theElement).css('margin-bottom', '-9px');
			}
			if (callback) {
				callback.call();
			}
		});
	},

	bindLinkPanel: function() {
		$JQ('a.linkPanel').unbind('click');
		$JQ('a.linkPanel').bind('click',function() {
			var linkElement = $JQ(this);
			var theTarget = linkElement.parents('.panel');
			if ($JQ('.panelContent', theTarget).css('display') != 'none') {
				linkElement.removeClass('active');
				$JQ('.panelContent', theTarget).slideUp(speedSlide);
			} else {
				linkElement.addClass('active');
				$JQ('.panelContent', theTarget).slideDown(speedSlide);
			}

			return false;
		});
	},

	checkDimensionsTags: function() {
		$JQ('.panelTags .tagLeft').each(function() {
			var eachElement = $JQ(this);
			PEPSIMAXCAST.CONTENT.checkDimensionsTag(eachElement);
		});
	},

	checkDimensionsTag: function(theElement) {
		var theMaxWidth = PEPSIMAXCAST.GENERIC.getMaxWidth('tag');
		var oldLinkText = $JQ('a', theElement).html();
		if ($JQ('a', theElement).attr('class').indexOf('active') >= 0) {
			$JQ('a', theElement).parents('.tagLeft').addClass('tagLeftActive');
			$JQ('a', theElement).parents('.tagRight').addClass('tagRightActive');
		}
		$JQ('a', theElement).attr('title', oldLinkText);
		var count = 0;
		while (LIBRARY.getTotalWidth(theElement) > theMaxWidth) {
			count++;
			var newLinkText = oldLinkText.substring(0, (oldLinkText.length - count)) + '...';
			$JQ('a', theElement).html(newLinkText);
		}
		theElement.css('visibility', 'visible');
	},

	bindLinkTags:function() {
		$JQ('.panelTags .tagLeft').unbind('mouseenter');
		$JQ('.panelTags .tagLeft').bind('mouseenter',function() {
			var theElement = $JQ(this);
			if ($JQ('a', theElement).attr('class').indexOf('active') < 0) {
				$JQ(theElement).addClass('tagLeftActive');
				$JQ('.tagRight', theElement).addClass('tagRightActive');
			}
		});

		$JQ('.panelTags .tagLeft').unbind('mouseleave');
		$JQ('.panelTags .tagLeft').bind('mouseleave',function() {
			var theElement = $JQ(this);
			if ($JQ('a', theElement).attr('class').indexOf('active') < 0) {
				$JQ(theElement).removeClass('tagLeftActive');
				$JQ('.tagRight', theElement).removeClass('tagRightActive');
			}
		});
	},

	checkDimensionsTitles: function() {
		$JQ('.calendar h3 span').each(function() {
			var eachElement = $JQ(this);
			PEPSIMAXCAST.CONTENT.checkDimensionsTitle(eachElement);
		});
	},

	checkDimensionsTitle: function(theElement) {
		var theMaxWidth = PEPSIMAXCAST.GENERIC.getMaxWidth('title');
		var oldTitleText = theElement.html();
		theElement.attr('title', oldTitleText);
		var count = 0;
		while (LIBRARY.getTotalWidth(theElement) > theMaxWidth) {
			count++;
			var newTitleText = oldTitleText.substring(0, (oldTitleText.length - count)) + '...';
			theElement.html(newTitleText);
		}
		theElement.css('visibility', 'visible');
	},

	checkLinksPositions: function() {
		$JQ('.contentItem').each(function() {
			var eachElement = $JQ(this);
			PEPSIMAXCAST.CONTENT.checkLinksPosition(eachElement);
		});
	},

	checkLinksPosition: function(theElement) {
		if ($JQ('.postLinks', theElement).size() > 0) {
			var textHeight = LIBRARY.getTotalHeight($JQ('.postText', theElement));
			var linksHeight = LIBRARY.getTotalHeight($JQ('.postLinks', theElement));
			var rightHeight = LIBRARY.getTotalHeight($JQ('.postRight', theElement));
			if ((textHeight + linksHeight) < rightHeight) {
				$JQ('.postText', theElement).css('height', (rightHeight - linksHeight) + 'px');
			}
		}
	}
}

// *******************************************************************************************

PEPSIMAXCAST.CALENDAR = {
	initialiseCalendars: function() {
		var currentMonthName = PEPSIMAXCAST.GENERIC.getCurrentMonthName();
		var count = 0;
		$JQ('.contentCalendar').each(function() {
			var eachElement = $JQ(this);
			var currentMonth;
			count++;
			eachElement.attr('id', 'calendar-' + count);
			var contentCalendarInsideWidth = 0;
			$JQ('.calendar', eachElement).each(function() {
				var eachElement = $JQ(this);
				if(eachElement.attr('class').indexOf('active') >= 0) {
					currentMonthName = $JQ('table', eachElement).attr('summary');
				}
				eachElement.css('display', 'block');
				contentCalendarInsideWidth += LIBRARY.getTotalWidth(eachElement);
				var eachSummary = $JQ('table', eachElement).attr('summary');
				eachElement.attr('id', 'calendar-' + count + '-' + eachSummary.replace(/ /g, ''));
				if (eachSummary == currentMonthName) {
					currentMonth = eachElement;
					currentMonthHeight = LIBRARY.getTotalHeight(currentMonth);
				}
			});
			eachElement.css('height', currentMonthHeight + 'px');
			PEPSIMAXCAST.CONTENT.checkDimensionsTitles();
			$JQ('.contentCalendarInside', eachElement).css('height', currentMonthHeight + 'px');
			$JQ('.contentCalendarInside', eachElement).css('width', contentCalendarInsideWidth + 'px');
			eachElement.scrollTo(currentMonth, 0, {axis:'x'});
			PEPSIMAXCAST.CALENDAR.bindCalendarMonthLinks(eachElement);
		});
	},

	bindCalendarMonthLinks: function(parentElement) {
		$JQ('.tdMonth a', parentElement).unbind('click');
		$JQ('.tdMonth a', parentElement).bind('click',function() {
			var theElement = $JQ(this);
			var scrollTarget = parentElement.attr('id') + '-' + theElement.attr('title').replace(/ /g, '');
			var newMonth = $JQ('#' + scrollTarget);
			var newMonthHeight = LIBRARY.getTotalHeight(newMonth);
			$JQ('.contentCalendarInside', parentElement).css('height', newMonthHeight + 'px');
			parentElement.scrollTo(newMonth, {duration: speedScroll, axis:'x', onAfter:function() {
				parentElement.animate({'height': newMonthHeight + 'px'}, speedAnimate);
			}});
			return false;
		});
	}
};

// *******************************************************************************************

