/*
 * Author: Digital Zoom Studio
 * Website: http://digitalzoomstudio.net/
 * Portfolio: http://codecanyon.net/user/ZoomIt/portfolio
 */

(function($) {
	$.fn.scroller = function(o) {

		var defaults = {
			totalWidth : undefined,
			totalwidth : undefined,
			settings_multiplier : 3,
			settings_smoothing : 'onf',
			settings_scrollbary_class : 'simple-scrollbary',
			settings_scrollbarybg_class : 'simple-scrollbary-bg',
			settings_scrollbarx_class : 'simple-scrollbarx',
			settings_scrollbarxbg_class : 'simple-scrollbarx-bg',
			settings_scrollbar:'on',
			settings_scrollbyhover:'off',
			settings_fadeoutonleave:'off',
			settings_replacewheelxwithy:'off',
			settings_refresh:0
		};
		var o = $.extend(defaults, o);

		this.each( function() {

			//console.log(this);
			var totalWidth=0;
			var totalHeight=0;
			var auxdeltax=0;
			var auxdeltay=0;
			var viewIndexWidth=0;
			var scrollIndexY=0;
			var scrollIndexX=0;
			var scrollbar_height=0;
			var inner;
			var outer;
			var comWidth=0;
			var comHeight=0;
			var scrollbary;
			var scrollbary_bg;
			var scrollbarx;
			var scrollbarx_bg;
			var cthis = $(this);
			var mousex=0;
			var mousey=0;
			var scrollbary_pressed=false;
			var scrollbarx_pressed=false;
			var scrollbary_height=0;
			var scrollbarx_height=0;
			var scrollbary_width=0;
			var scrollbarx_width=0;
			
			var viewIndexX=0;
			var viewIndexY=0;
			
			
			var dir_hor=true;
			var dir_ver=true;
			
			var duration_smoothing = 60;
			
			inner = cthis.find('.inner');
			if(is_ios()){
				cthis.css('overflow', 'auto')
				return
			}
			inner.wrap('<div class="scroller"></div>')
			outer=cthis.find('.scroller');
			
			
			init();
			
			function init(){
			if(o.totalWidth!=undefined)
			totalWidth = o.totalWidth;
			else
			totalWidth = cthis.width();
			
			if(o.totalHeight!=undefined)
			totalHeight = o.totalHeight;
			else
			totalHeight = cthis.height();
			
			outer.css({
				'width' : totalWidth,
				'height' : totalHeight
			})
			cthis.css({
				'width' : totalWidth,
				'height' : totalHeight,
				'overflow' : 'inherit'
			})
			if(jQuery.browser.msie!=undefined && jQuery.browser.version==7)
			cthis.css('overflow', 'visible');
			inner.css({
				//'width' : totalWidth
			})
			comWidth = inner.width();
			comHeight = inner.height();
			if(inner.find('.real-inner').length==1){
			comWidth = inner.find('.real-inner').width();
			comHeight = inner.find('.real-inner').height();
			}
			
			//determining the direction ------------
			if(comHeight<=totalHeight) dir_ver=false;
			if(comWidth<=totalWidth) dir_hor=false;
			
			if(dir_ver==false && dir_hor==false)
			return;
			
			
			if(o.settings_scrollbar=='on'){
			if(dir_ver){
			cthis.append('<div class="' + o.settings_scrollbarybg_class + '"></div>')
			cthis.append('<div class="' + o.settings_scrollbary_class + '"></div>')
			}
			if(dir_hor){
			cthis.append('<div class="' + o.settings_scrollbarxbg_class + '"></div>')
			cthis.append('<div class="' + o.settings_scrollbarx_class + '"></div>')
			}
			}
			
			
			if(dir_ver){
			scrollbary = cthis.children('.' + o.settings_scrollbary_class);
			scrollbary_bg = cthis.children('.' + o.settings_scrollbarybg_class);
			scrollbary_height = scrollbary.height();
			scrollbary_bg.css('height', totalHeight);
			
			
			if(o.settings_fadeoutonleave=='on'){
				scrollbary.css('opacity', 0);
				scrollbary_bg.css('opacity', 0);
			}
			}
			
			
			if(dir_hor){
			scrollbarx = cthis.children('.' + o.settings_scrollbarx_class);
			scrollbarx_bg = cthis.children('.' + o.settings_scrollbarxbg_class);
			scrollbarx_height = scrollbarx.width();
			scrollbarx_bg.css('width', totalWidth);
			
			
			if(o.settings_fadeoutonleave=='on'){
				scrollbarx.css('opacity', 0);
				scrollbarx_bg.css('opacity', 0);
			}
			if(o.settings_refresh>0)
			setInterval(reinit, o.settings_refresh);
			}
			
			
			}
			function reinit(){
				comWidth = inner.width();
				comHeight = inner.height();
				if(inner.find('.real-inner').length==1){
				comWidth = inner.find('.real-inner').width();
				comHeight = inner.find('.real-inner').height();
				}
			}
			
			$.fn.scroller.reinit = function() {
				reinit();
			}
			if(o.settings_scrollbyhover!='on')
			cthis.mousewheel(function(event, delta,deltaX,deltaY) {
				auxdeltax=-Math.floor(deltaX);
				auxdeltay=Math.floor(deltaY);
				if(o.settings_replacewheelxwithy=='on')
				auxdeltax=Math.floor(deltaY);
				
				
				if(dir_ver){
				if(deltaY>0 && auxdeltay<deltaY)
				auxdeltay++;
				
				if(deltaY<0 && auxdeltay>deltaY)
				auxdeltay--;
				
				viewIndexY+= ( auxdeltay * o.settings_multiplier );
				scrollIndexY = viewIndexY / (comHeight - totalHeight) * -(totalHeight - scrollbary_height);
				}
				
				
				if(dir_hor){
					
				if(deltaX<0 && auxdeltax>deltaX)
				auxdeltax++;
				
				if(deltaX>0 && auxdeltax<deltaX)
				auxdeltax--;
				
				viewIndexX+= ( auxdeltax * o.settings_multiplier );
				//console.log(deltaX, deltaY, delta, auxdeltax, viewIndexX)
				scrollIndexX = viewIndexX / (comWidth - totalWidth) * -(totalWidth - scrollbarx_height);
				}
				
				
				animateScrollbar();
				
				event.stopPropagation();
				event.preventDefault();
			});
			
			if(scrollbary_bg)
			scrollbary_bg.mousedown(function(event){
				scrollbary_pressed=true;
				return false;
			})
			if(scrollbary)
			scrollbary.mousedown(function(event){
				scrollbary_pressed=true;
				return false;
			})
			
			if(scrollbarx_bg)
			scrollbarx_bg.mousedown(function(event){
				scrollbarx_pressed=true;
				return false;
			})
			if(scrollbarx)
			scrollbarx.mousedown(function(event){
				scrollbarx_pressed=true;
				return false;
			})
			
			jQuery(document).mousemove(function(e){
				mousex = (e.pageX - cthis.offset().left);
				mousey = (e.pageY - cthis.offset().top);
				if(o.settings_scrollbyhover=='on' && (mousex<0 || mousey<0 || mousex>totalWidth + 20 || mousey>totalHeight + 20)) return;
				if(dir_ver==true && (scrollbary_pressed==true || o.settings_scrollbyhover=='on')){
				scrollIndexY= mousey / totalHeight * (totalHeight - scrollbary_height);
				viewIndexY= mousey / totalHeight * (totalHeight - comHeight);
				animateScrollbar();
				}
				
				if(dir_hor==true && (scrollbarx_pressed==true || o.settings_scrollbyhover=='on')){
				//scrollIndexY=
				//console.log('ceva');
				scrollIndexX= mousex / totalWidth * (totalWidth - scrollbarx_height);
				viewIndexX= mousex / totalWidth * (totalWidth - comWidth);
				animateScrollbar();
				}
				
				if(o.settings_fadeoutonleave=='on'){
					scrollbary.animate({
						'opacity' : 1
					},{queue:false, duration:500});
					scrollbary_bg.animate({
						'opacity' : 1
					},{queue:false, duration:500});
				}
				
			})
			jQuery(document).mouseup(function(event){
				//console.log('mouseup')
				scrollbary_pressed=false;
				scrollbarx_pressed=false;
			})
			function animateScrollbar(){
				//console.log(viewIndexX, viewIndexY);
				if(dir_ver){
				if(viewIndexY>0) viewIndexY = 0;
				if(viewIndexY<-(comHeight - totalHeight)) viewIndexY = -(comHeight - totalHeight);
				
				
				if(scrollIndexY<0) scrollIndexY = 0;
				if(scrollIndexY>(totalHeight - scrollbary_height)) scrollIndexY = (totalHeight - scrollbary_height);
				
				if(o.settings_smoothing!='on'){
				inner.css({
				'top' : viewIndexY
				})
				scrollbary.css({
				'top' : scrollIndexY
				})
				
				}else{
				inner.animate({
				'top' : viewIndexY
				},{queue:false, duration:duration_smoothing})
				scrollbary.animate({
				'top' : scrollIndexY
				},{queue:false, duration:duration_smoothing})
				}
				}
				
				if(dir_hor){
					
				if(viewIndexX>0) viewIndexX = 0;
				if(viewIndexX<-(comWidth - totalWidth)) viewIndexX = -(comWidth - totalWidth);
				
				//console.log(viewIndexX);
				if(scrollIndexX<0) scrollIndexX = 0;
				if(scrollIndexX>(totalWidth - scrollbarx_height)) scrollIndexX = (totalWidth - scrollbarx_height);
				
				if(o.settings_smoothing!='on'){
				inner.css({
				'left' : viewIndexX
				})
				scrollbarx.css({
				'left' : scrollIndexX
				})
				
				}else{
				inner.animate({
				'left' : viewIndexX
				},{queue:false, duration:duration_smoothing})
				scrollbarx.animate({
				'left' : scrollIndexX
				},{queue:false, duration:duration_smoothing})
				}
				
				}
			}
			if(o.settings_fadeoutonleave=='on')
			cthis.mouseout(function(e){
					scrollbary.animate({
						'opacity' : 0
					},{queue:false, duration:500});
					scrollbary_bg.animate({
						'opacity' : 0
					},{queue:false, duration:500});
			})
			
			return this;
		});
	};
})(jQuery);
function is_ios(){
    return (
        (navigator.platform.indexOf("iPhone") != -1) ||
        (navigator.platform.indexOf("iPod") != -1) ||
        (navigator.platform.indexOf("iPad") != -1)
    )
}

