﻿(function($) { 
	jQuery.fn.smoothDivScroll = function(options){

		var defaults = {
		scrollingHotSpotLeft: "div.scrollingHotSpotLeft", 
		scrollingHotSpotRight: "div.scrollingHotSpotRight", 
		scrollWrapper: "div.scrollWrapper", 
		scrollableArea: "div.scrollableArea", 
		maintableArea: "div.main_table", 
		hiddenOnStart: false, 
		ajaxContentURL: "", 
		countOnlyClass: "", 
		mouseDownSpeedBooster: 1, 
		autoScrollOnStart: false, 
		autoScrollSpeed: 0.1, 
		visibleHotSpots: "", 
		hotSpotsVisibleTime: 10 
		};
		options = $.extend(defaults, options);
		return this.each(function() {
			var $mom = $(this);
			if(options.ajaxContentURL.length !== 0){
				$mom.scrollableAreaWidth = 0;
				$mom.find(options.scrollableArea).load((options.ajaxContentURL), function(){	
					$mom.find(options.scrollableArea).children((options.countOnlyClass)).each(function() {
						$mom.scrollableAreaWidth = $mom.scrollableAreaWidth + $(this).outerWidth(true);
					});
					$mom.find(options.scrollableArea).css("width", ($mom.scrollableAreaWidth + "px"));
					if(options.hiddenOnStart) {
						$mom.hide();
					}
					windowIsResized();
					setHotSpotHeightForIE();
				});		
			}
			var scrollXpos;
			var booster;
			var motherElementOffset = $mom.offset().left;
			var hotSpotWidth = 0;
			booster = 1;
			$(window).one("load",function(){
				if(options.ajaxContentURL.length === 0) {
					$mom.scrollableAreaWidth = 0;
					$mom.find(options.scrollableArea).children((options.countOnlyClass)).each(function() {
						$mom.scrollableAreaWidth = $mom.scrollableAreaWidth + $(this).outerWidth(true);
					});
					$mom.find(options.scrollableArea).css("width", $mom.scrollableAreaWidth + "px");
					
					if(options.hiddenOnStart) { 
						$mom.hide();
					}
				}
				if(options.autoScrollOnStart)
				{
					autoScrollInterval = setInterval(autoScrollRight, 15);
				}
				switch(options.visibleHotSpots)
				{
					case "always":
						makeHotSpotBackgroundsVisible();
						break;
					case "onstart":
						makeHotSpotBackgroundsVisible();
						hideHotSpotBackgroundsInterval = setInterval(hideHotSpotBackgrounds, (options.hotSpotsVisibleTime * 800));
						break;
					default:
						break;	
				}
				
			});
			$mom.find(options.scrollingHotSpotRight).one('mouseover',function(){
				if(options.autoScrollOnStart)
				{
					clearInterval(autoScrollInterval);
				}
			});	
			$mom.find(options.scrollingHotSpotLeft).one('mouseover',function(){
				if(options.autoScrollOnStart)
				{
					clearInterval(autoScrollInterval);
				}
			});	
			function makeHotSpotBackgroundsVisible()
			{
				$mom.find(options.scrollingHotSpotLeft).addClass("scrollingHotSpotLeftVisible");
				$mom.find(options.scrollingHotSpotRight).addClass("scrollingHotSpotRightVisible");
			}
			function hideHotSpotBackgrounds()
			{
				clearInterval(hideHotSpotBackgroundsInterval);
				$mom.find(options.scrollingHotSpotLeft).fadeTo("slow", 0.0, function(){
				$mom.find(options.scrollingHotSpotLeft).removeClass("scrollingHotSpotLeftVisible");
				});
				$mom.find(options.scrollingHotSpotRight).fadeTo("slow", 0.0, function(){
				$mom.find(options.scrollingHotSpotRight).removeClass("scrollingHotSpotRightVisible");
				});
			}
			$(window).bind("resize",function(){
				windowIsResized();
			});
			function windowIsResized()
			{
				if(!(options.hiddenOnStart))
				{
					$mom.scrollableAreaWidth = 0;
					$mom.find(options.scrollableArea).children((options.countOnlyClass)).each(function() {
						$mom.scrollableAreaWidth = $mom.scrollableAreaWidth + $(this).outerWidth(true);
					});
					
					$mom.find(options.scrollableArea).css("width", $mom.scrollableAreaWidth + 'px');
				}
				var bodyWidth = $("body").innerWidth();
				if($mom.scrollableAreaWidth < bodyWidth)
				{	
					hideLeftHotSpot();
					hideRightHotSpot();
				}
				else
				{
					showHideHotSpots();
				}
			}
			function hideLeftHotSpot(){
				$mom.find(options.scrollingHotSpotLeft).hide();
			}
			function hideRightHotSpot(){
				$mom.find(options.scrollingHotSpotRight).hide();
			}
			function showLeftHotSpot(){
				$mom.find(options.scrollingHotSpotLeft).show();
				if(hotSpotWidth <= 0)
				{
					hotSpotWidth = $mom.find(options.scrollingHotSpotLeft).width();
				}
			}
			function showRightHotSpot(){
				$mom.find(options.scrollingHotSpotRight).show();
				if(hotSpotWidth <= 0)
				{
					hotSpotWidth = $mom.find(options.scrollingHotSpotRight).width();
				}
			}
			function setHotSpotHeightForIE()
			{
				jQuery.each(jQuery.browser, function(i, val) {
					if(i=="msie" && jQuery.browser.version.substr(0,1)=="6")
					{
						$mom.find(options.scrollingHotSpotLeft).css("height", ($mom.find(options.scrollableArea).innerHeight()));
						$mom.find(options.scrollingHotSpotRight).css("height", ($mom.find(options.scrollableArea).innerHeight()));				
					}
				});
			}
			var rightScrollInterval;
			$mom.find(options.scrollingHotSpotRight).bind('mousemove',function(e){
				var x = e.pageX - (this.offsetLeft + motherElementOffset);
				x = Math.round(x/(hotSpotWidth/30));
				scrollXpos = x;
			});
			$mom.find(options.scrollingHotSpotRight).bind('mouseover',function(e){
				rightScrollInterval = setInterval(doScrollRight, 20);
			});	
			$mom.find(options.scrollingHotSpotRight).bind('mouseout',function(e){
				clearInterval(rightScrollInterval);
				scrollXpos = 0;
			});
			$mom.find(options.scrollingHotSpotRight).bind('mousedown',function(e){
				booster = options.mouseDownSpeedBooster;
			});
			$("*").bind('mouseup',function(e){
				booster = 1;
			});
			var doScrollRight = function()
			{	document.getElementById('hidn_tmp').value = 1;
				var w_table = $('#main_table').width();
				$mom.find(options.scrollWrapper).scrollLeft($mom.find(options.scrollWrapper).scrollLeft() + (scrollXpos*booster));
				showHideHotSpots();
			};
			var autoScrollRight = function()
			{	
				$mom.find(options.scrollWrapper).scrollLeft($mom.find(options.scrollWrapper).scrollLeft() + options.autoScrollSpeed);
				showHideHotSpots();
			};
			var leftScrollInterval;
			$mom.find(options.scrollingHotSpotLeft).bind('mousemove',function(e){
				var x = $mom.find(options.scrollingHotSpotLeft).innerWidth() - (e.pageX - motherElementOffset);
				x = Math.round(x/(hotSpotWidth/30));
				scrollXpos = x;
			});
			$mom.find(options.scrollingHotSpotLeft).bind('mouseover',function(e){
				if(options.autoScrollOnStart)
				{
					clearInterval(autoScrollInterval);
				}
				leftScrollInterval = setInterval(doScrollLeft, 15);
			});	
			$mom.find(options.scrollingHotSpotLeft).bind('mouseout',function(e){
				clearInterval(leftScrollInterval);
				scrollXpos = 0;
			});
			$mom.find(options.scrollingHotSpotLeft).bind('mousedown',function(e){
				booster = options.mouseDownSpeedBooster;
			});
			var doScrollLeft = function()
			{	
				document.getElementById('hidn_tmp').value = 1;
				$mom.find(options.scrollWrapper).scrollLeft($mom.find(options.scrollWrapper).scrollLeft() - (scrollXpos*booster));
				showHideHotSpots();
				
			};
			function showHideHotSpots()
			{
				var w_table = $('#main_table').width();
				if($mom.find(options.scrollWrapper).scrollLeft() === 50)
				{
					hideLeftHotSpot();
					showRightHotSpot();
					var tw = $('#main_table').width();
				}
				else if((w_table)+50 <= ($mom.find(options.scrollWrapper).innerWidth() + $mom.find(options.scrollWrapper).scrollLeft()))
				{		
					var pos = $(".main_table").show().position();
					hideRightHotSpot();
					showLeftHotSpot();
				}
				else
				{
					var pos = $(".main_table").show().position();
					var changeWidth = $mom.find(options.scrollWrapper).scrollLeft();
					var totalWidth = $('#main_table').width();
					isScrollBottom(changeWidth,totalWidth);
					showRightHotSpot();
					showLeftHotSpot();		
				}
			}
	});			
};
})(jQuery);
function isScrollBottom(wd,tw) 
{
	if(wd<0)
		wd = 0;
	var obj = document.getElementById('scroll_footer');
	var pgwidth = document.body.clientWidth;
	var pgW = pgwidth;
	obj.style.width = (pgwidth/10)+'px';
	pgwidth /= 2;
	pgwidth = pgwidth - ((pgW/10) + (pgW/150));
	wd1 = (wd/(tw-pgW))*pgwidth;
	pgwidth -=(pgW/300);
	if(wd1 < pgwidth)
	{	obj.style.left = wd1+'px';	}
	else
		obj.style.left = pgwidth+'px';
}