/*
jquery.nm.verticalscrolling.js

Custom jquery plugin for vertical scrolling by Nevermore
Based on: jquery 1.4.2
*/

// Prepare selected elements for scrolling. start scrolling initially, When mouse enters,
// stop scrolling and when mouse enters, start scrolling again.
$.fn.prepareScrollingItems = function() {
	return this.each(function() {
		var $scrollable = $(this);
		InitializeScrollingItems($scrollable);
		$scrollable.hover(
			function () { StopScrollingItems($scrollable); },
			function () { StartScrollingItems($scrollable); }
		);
	});
};

// Deactivate scrolling items for all scrollable areas.
$.fn.deactivateAllScrollingItems = function() {
	return this.each(function() {
		var $scrollable = $(this);
		StopScrollingItems($scrollable);
	});
};

// Activate scrolling items for 1 scrollable area.
$.fn.activateScrollingItems = function() {
	return this.each(function() {
		var $scrollable = $(this);
		StartScrollingItems($scrollable);
	});
};

// Start scrolling
function StartScrollingItems ($scrollable) {
    var scrollInterval = $scrollable.data('ptScrollingInterval');
    if (scrollInterval != null) {
        clearInterval(scrollInterval);
    }
    var intervalTime = 5;
    if ($.browser.mozilla || $.browser.safari || $.browser.opera) {
        intervalTime = 30;
    }
    else if ($.browser.chrome) {
        intervalTime = 500;
    }
    else if ($.browser.msie && document.documentMode >= 9) {
        intervalTime = 30;
    }
    scrollInterval = setInterval(function () { ScrollItems($scrollable); }, intervalTime);
    $scrollable.data('ptScrollingInterval', scrollInterval);
}

// Stop scrolling
function StopScrollingItems ($scrollable) {
    var scrollInterval = $scrollable.data('ptScrollingInterval');
    if (scrollInterval != null) {
        clearInterval(scrollInterval);
        scrollInterval = null;
        $scrollable.data('ptScrollingInterval', scrollInterval);
    }
}

// Initialize scrolling items inside containing element
// Method is:
// 1) Make sure that visible area is fully covered. Keep repeating div's until area is filled.
// 2) Set width of scrollable area.
// 3) Store scrolling interval in jquery's object datastore.
// 4) Set maxheight of scrollable area twice the height of containing element
// 5) Clone items and place on top of the original items
function InitializeScrollingItems ($scrollable) {
	var scrollableHeight = parseInt($scrollable.css('max-height'),10);
	var $items = $scrollable.find('.items');
	$items.each( function () {
		var $images = $items.find('div');
		if ($items.height() <= scrollableHeight) {
			var numberOfImages =  $images.length;
			if (numberOfImages > 0) {
				$images.each( function() { 
					var cls = $(this).attr('class');
					$items.append('<div class="' + cls + '">' + $(this).html() + '</div>');
				});
				InitializeScrollingItems($scrollable, $items);
			}
		}
		else {
			$scrollable.css('width', $items.outerWidth(true));
			$scrollable.data('ptScrollingInterval', null);
			$items.css('max-height', ($scrollable.height() * 2));
			var $newItems = $items.clone();
			$newItems.css('top', $items.position().top - $items.height());
			$items.before($newItems);
			$items.addClass('first');
			$newItems.addClass('second');
		}
	});
}

// Actually scroll the scrollable area within containing element
function ScrollItems($scrollable) {
	var scrollableHeight = parseInt($scrollable.css('max-height'),10);
	var $first = $scrollable.find('.first');
	var $second = $scrollable.find('.second');
	if ($first.position().top >= scrollableHeight) {
		$first.css('top', $second.position().top - $first.height());
		$first.removeClass('first');
		$first.addClass('second');
		$second.removeClass('second');
		$second.addClass('first');
	}
	$first.css('top', parseInt($first.css('top'),10) + 1);
	$second.css('top', parseInt($second.css('top'),10) + 1);
}
