// JavaScript Document
$(document).ready(function() {
	
	var resizeTimer;
	
	$(window).resize(function() {
		
		if (resizeTimer) clearTimeout(resizeTimer);
		
		resizeTimer = setTimeout("windowResize()",1000);
		
	});
	
	windowResize = function(update) {
		
		if (update == null) update = 0;
		
		if (update == 0 && $.browser.msie && $.browser.version == "6.0") {
			
			if ($("#content").length > 0) {
				var newWidth = $(window).width();
				newWidth = newWidth - parseInt($("#content").css('left')) - parseInt($("#content").css('right'));
				
				var newHeight = $(window).height();
				newHeight = newHeight - parseInt($("#content").css('top')) - parseInt($("#content").css('bottom'));
				
				if ($("#paper").length > 0) {
					
					if ($("#paper div.tabs").length > 0) {
						var tabsHeight = 34;
					} else {
						var tabsHeight = 0;
					}
					
					$("#paper").height(newHeight);
					
					$("#paper div.in").outerHeight(newHeight - 70 - tabsHeight);
					
				}
				
				$("#content").width(newWidth);
				$("#content").height(newHeight);
			}
			
			if ($("#portfolio").length > 0) {
				var newWidth = $(window).width();
				newWidth = newWidth - parseInt($("#content").css('left')) - parseInt($("#content").css('right'));
				
				var newHeight = $(window).height();
				newHeight = newHeight - parseInt($("#content").css('top')) - parseInt($("#content").css('bottom'));	
			}
			
		}
		
		if ($("#paper").length > 0) {
			
			if ($("#paper div.tabs").length > 0) {
				var tabsHeight = 34;
			} else {
				var tabsHeight = 0;
			}
			
			var contentW = $("#content").width();
			var contentH = $("#content").height();
			
			scroller.Paper.Object = $("#paper");
			
			scroller.Paper.Width = $("#paper").width();
			scroller.Paper.Height = $("#paper div.in div.scroll").height() + 70 + 40 + tabsHeight; //bottom 70px + padding 40px
			
			if (scroller.Paper.Height > contentH || $("#portfolio").length > 0) scroller.Paper.Height = contentH;
			
			$("#paper").height(scroller.Paper.Height);
			$("#paper div.in").innerHeight(scroller.Paper.Height - 70 - tabsHeight); //bottom 70px
			
			var top = (contentH - scroller.Paper.Height) + "px";
			if ($("#paper").attr("left")) {
				var left = $("#paper").attr("left") + "px";
			} else {
				var left = ((contentW - scroller.Paper.Width)/2) + "px";
			}
			
			$("#paper").css({'top' : top, 'left' : left});
			
			scroller.Viewport.Object = $('.in', $("#paper"));
			scroller.Scroll.Object = $('.scroll', $("#paper"));
			scroller.Thumb.Object = $('#scroll_thumb', $("#paper"));
			
			//vidljivi dio
			scroller.Viewport.Height = scroller.Viewport.Object.height();
			scroller.Viewport.Outer = scroller.Viewport.Object.outerHeight();
			
			//velicina sadrzaja
			scroller.Scroll.Height = scroller.Scroll.Object.height();
			
			scroller.Thumb.Top = tabsHeight;
			if (scroller.Thumb.Object.attr("bottom")) {
				scroller.Thumb.Bottom = tabsHeight + scroller.Viewport.Outer + 20 - scroller.Thumb.Object.height() - parseInt(scroller.Thumb.Object.attr("bottom"));
			} else {
				scroller.Thumb.Bottom = tabsHeight + scroller.Viewport.Outer + 20 - scroller.Thumb.Object.height();
			}
			
			scroller.iScroll = 0;
			scroller.ScrollRatio = scroller.Viewport.Height / scroller.Scroll.Height;
			scroller.ThumbRatio = (scroller.Scroll.Height - scroller.Viewport.Height) / (scroller.Thumb.Bottom - scroller.Thumb.Top);
			
			scroller.Thumb.Object.toggleClass('disable', scroller.ScrollRatio >= 1);
			
			scroller.Thumb.Object.css('top', scroller.Thumb.Top);
			scroller.Scroll.Object.css('top', 0);
			scroller.iMouse = scroller.Thumb.Object.offset().top;
			
			scroller.setEvents();
			
		} else if ($("div.content_wide div.in_wide div.scroll").length > 0) {
			
			pagination.content.Width = $("#content").width();
			pagination.content.Height = $("#content").height();
			
			$("div.content_wide div.in_wide").height(pagination.content.Height);
			
			pagination.contentScroll.Object = $("div.content_wide div.in_wide div.scroll");
			
			pagination.contentScroll.Width = pagination.contentScroll.Object.width();
			pagination.contentScroll.Height = pagination.contentScroll.Object.height();
			
			var divHeight = $("div.content_wide div.in_wide div.scroll div:first").outerHeight(true);
			
			pagination.contentScroll.Scroll = Math.floor(pagination.content.Height / divHeight) * divHeight;
			
			pagination.totalPage = Math.ceil(pagination.contentScroll.Height / pagination.contentScroll.Scroll);
			
			$(".pagination").each(function(index, value) { 
				$(this).toggleClass("disable", pagination.totalPage <= 1 );
			});
			
			if (pagination.totalPage > 1) {
				var strPages = pagination.getPages(1);
				
				$(".pagination").each(function(index, value) { 
					$(this).html(strPages);
				});
			}
			
			pagination.goToPage(1);
			
		}
		
		//portfolio
		if (update == 0) {
			if ($("#portfolio").is(':hidden') == false) {
				
				var viewW = $("#portfolio div.portfolio").width();
				
				var scrollW = 0;
				$("#portfolio div.portfolio div.portfolio_scroll div.framebox").each(function(index, value) { 
					scrollW += $(this).outerWidth(true);
				});
				
				$("#portfolio div.portfolio div.portfolio_scroll").width(scrollW);
				
				if (scrollW > viewW) {
					$("#portfolio div.left").show();
					$("#portfolio div.right").show();
					
					$("#portfolio div.portfolio").css({'left':'60px','right':'60px'});
					
					$("#portfolio div.left a").hide();
					$("#portfolio div.right a").show();
				} else {
					$("#portfolio div.left").hide();
					$("#portfolio div.right").hide();
					
					$("#portfolio div.portfolio").css({'left':'0px','right':'0px'});
				}
				
				$("#portfolio div.portfolio div.portfolio_scroll").css('left','0px');
				
			}
		}
		if (update == 0 || update == 1) {
			if ($("#portfolio_small").is(':hidden') == false) {
				
				var viewH = $("#portfolio_small div.portfolio").height();
				var scrollH = $("#portfolio_small div.portfolio div.portfolio_scroll").height();
				
				if (scrollH > viewH) {
					$("#portfolio_small div.up").show();
					$("#portfolio_small div.down").show();
					
					$("#portfolio_small div.portfolio").css({'top':'20px','bottom':'20px'});
	
					$("#portfolio_small div.up a").hide();
					$("#portfolio_small div.down a").show();
				} else {
					$("#portfolio_small div.up").hide();
					$("#portfolio_small div.down").hide();
					
					$("#portfolio_small div.portfolio").css({'top':'0px','bottom':'0px'});
				}
				
				$("#portfolio_small div.portfolio div.portfolio_scroll").css('top','0px');
				
			}
			
		}
		//end portfolio
		
	};
	
	scroller = {
		
		Paper: {Object: null, Width:0, Height:0},
		Viewport: {Object: null, Height:0, Outer:0},
		Scroll: {Object: null, Height:0},
		Thumb: {Object: null, Top: 0, Bottom: 0},
		
		ScrollRatio: 0,
		ThumbRatio: 0,
		
		wheelAmount: 40,  //how many pixels must the mouswheel scroll at a time.
		
		iPosition: {start: 0, now: 0},
		iScroll: 0,
		iMouse: 0,
		
		eventsSeted: false,
		
		setEvents: function() {
			
			if (scroller.eventsSeted == false) {
				scroller.eventsSeted = true;
				
				scroller.Thumb.Object.bind('mousedown', scroller.start);
				
				scroller.Thumb.Object[0].ontouchstart = function(oEvent){
					oEvent.preventDefault();
					scroller.Thumb.Object.unbind('mousedown');
					scroller.start(oEvent.touches[0]);
					
					return false;
				};
				
				if (scroller.Paper.Object[0].addEventListener) {
					scroller.Paper.Object[0].addEventListener('DOMMouseScroll', scroller.wheel, false);
					scroller.Paper.Object[0].addEventListener('mousewheel', scroller.wheel, false );
				} else {
					scroller.Paper.Object[0].onmousewheel = scroller.wheel;
				}
			}
		},
		
		start: function(oEvent){
			
			scroller.iMouse = oEvent.pageY;
			
			scroller.iPosition.start = parseInt(scroller.Thumb.Object.css('top'));
			
			$(document).bind('mousemove', scroller.drag);
			
			document.ontouchmove = function(oEvent){
				$(document).unbind('mousemove');
				scroller.drag(oEvent.touches[0]);
			};
			
			$(document).bind('mouseup', scroller.end);
			
			scroller.Thumb.Object.bind('mouseup', scroller.end);
			
			scroller.Thumb.Object[0].ontouchend = document.ontouchend = function(oEvent){
				$(document).unbind('mouseup');
				scroller.Thumb.Object.unbind('mouseup');
				scroller.end(oEvent.touches[0]);
			};
			
			return false;
		},
		
		wheel: function(oEvent){
			if (scroller.ScrollRatio < 1) {
				oEvent = $.event.fix(oEvent || window.event);
				
				var iDelta = oEvent.wheelDelta ? oEvent.wheelDelta/120 : -oEvent.detail/3;
				
				scroller.iScroll -= iDelta * scroller.wheelAmount;
				
				scroller.iScroll = Math.min((scroller.Scroll.Height - scroller.Viewport.Height), Math.max(0, scroller.iScroll));
				
				scroller.Thumb.Object.css('top', scroller.Thumb.Top + scroller.iScroll / scroller.ThumbRatio);
				scroller.Scroll.Object.css('top', -scroller.iScroll);
				
				oEvent.preventDefault();
			};
		},
		end: function (oEvent){
			$(document).unbind('mousemove', scroller.drag);
			$(document).unbind('mouseup', scroller.end);
			scroller.Thumb.Object.unbind('mouseup', scroller.end);
			document.ontouchmove = scroller.Thumb.Object[0].ontouchend = document.ontouchend = null;
			
			return false;
		},
		
		drag: function(oEvent){
			if (scroller.ScrollRatio < 1) {
				
				scroller.iPosition.now = scroller.iPosition.start + (oEvent.pageY - scroller.iMouse);
				
				scroller.iPosition.now = Math.min(scroller.Thumb.Bottom, Math.max(scroller.Thumb.Top, scroller.iPosition.now));
				
				scroller.iScroll = (scroller.iPosition.now - scroller.Thumb.Top) * scroller.ThumbRatio;
				
				scroller.Thumb.Object.css('top', scroller.iPosition.now);
				scroller.Scroll.Object.css('top', -scroller.iScroll);
			}
			
			return false;
		}
		
	};
	
	pagination = {
		
		content: {Width:0, Height:0},
		contentScroll: {Object: null, Width:0, Height:0, Scroll:0},
		
		totalPage: 0,
		
		goToPage: function(page) {
			
			var top = this.contentScroll.Scroll * (page - 1);
			
			if (this.totalPage > 1) {
				top = Math.min((this.contentScroll.Height - this.content.Height), Math.max(0, top));
			
				top = (top * -1) + "px";
			
				this.contentScroll.Object.animate({'top' : top}, "slow");
			} else {
				this.contentScroll.Object.css({'top' : '0px'});
			}
			
			if (this.totalPage > 1) {
				var strPages = this.getPages(page);
						
				$(".pagination").each(function(index, value) { 
					$(this).html(strPages);
				});
			}
		},
		
		getPages: function(curPage) {
			
			var page_str = '';
			if (this.totalPage > 1) {
				
				page_str += '<span>';
		
				if (curPage > 1) {
					page_str += '<a href="javascript:pagination.goToPage(' + (curPage-1) + ')"><b>&laquo;</b></a>';
				} else {
					page_str += '<span class="page-sep">, </span><strong>&laquo;</strong><span class="page-sep">, </span>';
				}
				
				max = 20;
				start = (curPage - (max/2));
				if (start < 1) start = 1;
				end = start + max-1;
				if (end > this.totalPage) {
					end = this.totalPage;
					start = end - max+1;
					if (start < 1) start = 1;
				}
				
				for (i = start; i <= end; i++) {
					if (i == start && start > 1) page_str += '<span class="page-sep">, </span>...';
					
					if (i == curPage) {
						page_str += '<span class="page-sep">, </span><strong>' + i + '</strong><span class="page-sep">, </span>';
					} else {
						page_str += '<a href="javascript:pagination.goToPage(' + i + ')">' + i + '</a>';
					}
					if (i < end) page_str += '<span class="page-sep">, </span>';
					
					if (i == end && end < this.totalPage) page_str += '...<span class="page-sep">, </span>';
				}
				
				if (curPage < this.totalPage) {
					page_str += '<a href="javascript:pagination.goToPage(' + (curPage+1) + ')"><b>&raquo;</b></a>';
				} else {
					page_str += '<span class="page-sep">, </span><strong>&raquo;</strong>';
				}
		
				page_str += '</span>';
			}
			
			return page_str;
		}
	};
	
	windowResize(0);
	
});
