var speedFade = 100;
var speedScroll = 1;
var speedSlide = 100;
var speedAnimate = 100;
var homeImages = 3;

$JQ(document).ready(function() {
	JCC.HOME.randomBG();
	JCC.VIEWER.initialise('viewer', 'viewerItems', 'viewerItem', 'viewerItemActive', 'viewerPrevious', 'viewerNext');
});

$JQ(window).resize(function() {
});

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

JCC = {
};

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

JCC.GENERIC = {
};

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

JCC.HOME = {
	randomBG: function() {
		if ($JQ('.templateHome').size() > 0) {
			$JQ('body').removeClass('BG1');
			var randomNumber = Math.floor(Math.random() * homeImages)
			randomNumber++;
			randomNumber = 'BG' + randomNumber;
			$JQ('body').addClass(randomNumber);
		}
	}
};

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

JCC.VIEWER = {
	initialise: function(classContainer, classItems, classItem, classItemActive, classPrevious, classNext) {
		var countContainers = 0;
		$JQ('.' + classPrevious + ', .' + classNext).remove();
		$JQ('.' + classContainer).each(function() {
			var eachContainer = $JQ(this);
			var horizontal = 0;
			if (eachContainer.attr('class').indexOf('Horizontal') >= 0) {
				horizontal = 1;
			}
			countContainers++;
			eachContainer.attr('id', 'viewer_' + countContainers);
			var countItems = 0;
			var totalItems = $JQ('.' + classItem, eachContainer).size();
			var itemsWidth = 0;
			var itemsHeight = 0;
			var currentItem = $JQ('.' + classItem + ':nth(0)', eachContainer);
			var currentNumber = 1;
			$JQ('.' + classItem, eachContainer).each(function() {
				var eachItem = $JQ(this);
				countItems++;
				eachItem.attr('id', 'viewer_' + countContainers + '_' + countItems);
				eachItem.css('display', 'block');
				itemsHeight += LIBRARY.getTotalHeight(eachItem);
				itemsWidth += LIBRARY.getTotalWidth(eachItem);
				if (eachItem.attr('class').indexOf(classItemActive) >= 0) {
					currentItem = eachItem;
					currentNumber = countItems;
				}
				if (countItems == totalItems) {
					currentItemHeight = LIBRARY.getTotalHeight(currentItem);
					currentItemWidth = LIBRARY.getTotalWidth(currentItem);
					eachContainer.css('height', currentItemHeight + 'px');
					eachContainer.css('width', currentItemWidth + 'px');
					if (horizontal == 1) {
						$JQ('.' + classItems, eachContainer).css('height', currentItemHeight + 'px');
						$JQ('.' + classItems, eachContainer).css('width', itemsWidth + 'px');
						eachContainer.scrollTo(currentItem, 0, {axis:'x'});
					} else {
						$JQ('.' + classItems, eachContainer).css('width', currentItemWidth + 'px');
						$JQ('.' + classItems, eachContainer).css('height', itemsHeight + 'px');
						eachContainer.scrollTo(currentItem, 0, {axis:'y'});
					}
					var linkString = JCC.VIEWER.buildLinks(countContainers, currentNumber, totalItems);
					eachContainer.siblings('.pagination').append(linkString);
					JCC.VIEWER.bindViewerLinks(classContainer, classItems, classPrevious, classNext);
				}
			});
		});
	},

	bindViewerLinks: function(classContainer, classItems, classPrevious, classNext) {
		$JQ('.' + classPrevious + ', .' + classNext).unbind('click');
		$JQ('.' + classPrevious + ', .' + classNext).bind('click',function() {
			var theLink = $JQ($JQ(this));
			var linkParts = theLink.attr('href').split('#');
			var theTarget = $JQ('#' + linkParts[1]);
			var newHeight = LIBRARY.getTotalHeight(theTarget);
			var newWidth = LIBRARY.getTotalWidth(theTarget);
			var theItems = theTarget.parents('.' + classItems);
			var theContainer = theItems.parents('.' + classContainer);
			var horizontal = 0;
			var idParts = linkParts[1].split('_');
			var countContainers = idParts[1];
			var currentNumber =  idParts[2];
			var totalItems = parseInt(theTarget.siblings().size()) + 1;
			if (theContainer.attr('class').indexOf('Horizontal') >= 0) {
				horizontal = 1;
			}
			theItems.css('height', newHeight + 'px');
			if (horizontal == 1) {
				theContainer.scrollTo(theTarget, {duration: speedScroll, axis:'x', onAfter:function() {
					theContainer.animate({'height': newHeight + 'px'}, speedAnimate, function() {
						var linkString = JCC.VIEWER.buildLinks(countContainers, currentNumber, totalItems);
						$JQ('span.links', theContainer.siblings('.pagination')).remove();
						theContainer.siblings('.pagination').append(linkString);
						JCC.VIEWER.bindViewerLinks(classContainer, classItems, classPrevious, classNext);
						//$JQ(window).scrollTo(theContainer, {duration: speedScroll, axis:'y'});
					});
				}});
			} else {
				theContainer.scrollTo(theTarget, {duration: speedScroll, axis:'y', onAfter:function() {
					theContainer.animate({'height': newHeight + 'px'}, speedAnimate, function() {
						var linkString = JCC.VIEWER.buildLinks(countContainers, currentNumber, totalItems);
						$JQ('span.links', theContainer.siblings('.pagination')).remove();
						theContainer.siblings('.pagination').append(linkString);
						JCC.VIEWER.bindViewerLinks(classContainer, classItems, classPrevious, classNext);
						//$JQ(window).scrollTo(theContainer, {duration: speedScroll, axis:'y'});
					});
				}});
			}
			return false;
		});
	},
	
	buildLinks: function(countContainers, currentNumber, totalItems) {
		var showTotal = totalItems;
		if (showTotal < 10) {
			showTotal = '0' + showTotal;
		}
		var showCurrent = currentNumber;
		if (showCurrent < 10) {
			showCurrent = '0' + showCurrent;
		}
		var nextItem = parseInt(currentNumber) + 1;
		if (nextItem > totalItems) {
			nextItem = 1;
		}
		var showNext = nextItem;
		if (showNext < 10) {
			showNext = '0' + showNext;
		}
		var previousItem = currentNumber - 1;
		if (previousItem == 0) {
			previousItem = totalItems;
		}
		var showPrevious = previousItem;
		if (showPrevious < 10) {
			showPrevious = '0' + showPrevious;
		}
		
		var linkString = '<span class="links"><a class="viewerPrevious" href="#viewer_' + countContainers + '_' + previousItem + '" title="' + showPrevious + '">&lt;</a> ' + showCurrent + '/' + showTotal + ' <a class="viewerNext" href="#viewer_' + countContainers + '_' + nextItem + '" title="' + showNext + '">&gt;</a></span>'
		return linkString;
	}
};

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