var max_w;
var max_h;
var pageCount;
$(document).ready(function(a)
		{
	
			$(window).resize(function() {
				
				wresize=function(){
						pageCount=$("#mainDiv #items .page").size();
						max_w=$(window).width()-20;
						max_h=$(window).height()-20;
						
						$("#mainDiv,#scrollVDIV,#mainDiv #items .page").height(max_h);
						$("#mainDiv").width(max_w);
						$("#scrollHDIV").width(max_w);
						
						$("#mainDiv #items").width(max_w*pageCount);
			
						$("#mainDiv #items .page").width(max_w);
						
						
						if(max_h>529)
						{
							$("#mainDiv #items .page .firstRow div,#mainDiv #items .page .lastRow div").height((max_h-405)/2);
						}
						else
							$("#mainDiv #items .page .firstRow div,#mainDiv #items .page .lastRow div").height(60);
						Uscrolls();
						$("#mainDiv").scrollTop($("#mainDiv").scrollTop());
						$("#mainDiv").scrollLeft($("#mainDiv").scrollLeft());
				};
				setTimeout(function (){wresize();},1);
				setTimeout(function (){wresize();},3);
				
			});
			

			$("#scrollVDIV .s_up").mousedown(function()
					{
						$(this).css("backgroundPosition","left -18px");
						var w=setInterval(function(){
							if($("#mainDiv").scrollTop()>1)
								$("#mainDiv").scrollTop($("#mainDiv").scrollTop()-2);
							Uscrolls();
						},1);
						$(document).mouseup(function(){clearInterval(w);$("#scrollVDIV .s_up").css("backgroundPosition","left top");});
					});

			$("#scrollVDIV .s_down").mousedown(function()
			{
				$(this).css("backgroundPosition","left -18px");
				var w=setInterval(function(){
					if($("#mainDiv").scrollTop()< max_h*pageCount-1)
						$("#mainDiv").scrollTop( $("#mainDiv").scrollTop()+2);
					Uscrolls();
				},1);
				$(document).mouseup(function(){clearInterval(w);$("#scrollVDIV .s_down").css("backgroundPosition","left top");});
			});

			$("#scrollHDIV .s_left").mousedown(function()
					{
						$(this).css("backgroundPosition","left -18px");
						var w=setInterval(function(){
							if($("#mainDiv").scrollLeft()>1)
								$("#mainDiv").scrollLeft($("#mainDiv").scrollLeft()-2);
							Uscrolls();
						},1);
						$(document).mouseup(function(){clearInterval(w);$("#scrollHDIV .s_left").css("backgroundPosition","left top");});
					});

			$("#scrollHDIV .s_right").mousedown(function()
				{
					$(this).css("backgroundPosition","left -18px");
					var w=setInterval(function(){
						if($("#mainDiv").scrollLeft()< max_w*pageCount-1)
							$("#mainDiv").scrollLeft( $("#mainDiv").scrollLeft()+2);
						Uscrolls();
					},1);
					$(document).mouseup(function(){clearInterval(w);$("#scrollHDIV .s_right").css("backgroundPosition","left top");});
				});
			
			
			vsc= function(e)
    		{
            	 e.preventDefault();
                 var movex = (currX - e.pageX)*pageCount ;
       
                 $("#mainDiv").scrollLeft($("#mainDiv").scrollLeft()-movex);
                 currY = e.pageY;
                 currX = e.pageX;
                 Uscrolls();
    		};
			
			$("#scrollHDIV .sh_nav").mousedown(function(e)
					{
						e.preventDefault();
						var tempbg=$(this).css("backgroundColor");
						$(this).css("backgroundColor","gray");
		                currX = e.pageX;
		                $(document).bind( "mousemove",vsc);
		                $(document).mouseup( function(e){
		                    e.preventDefault();
		                    $("#scrollHDIV .sh_nav").css("backgroundColor",tempbg);
		                    $(document).unbind( "mousemove", vsc );
		                });


					});
			
			$("#scrollVDIV .sv_nav").mousedown(function(e)
					{
						e.preventDefault();
						var tempbg=$(this).css("backgroundColor");
						$(this).css("backgroundColor","gray");
		                currY = e.pageY;
		                $(document).bind( "mousemove",hsc= function(e)
		                		{
				                	 e.preventDefault();
				                     var movey = (currY - e.pageY)*$("#mainDiv").attr("scrollHeight")/max_h ;
                           
				                     $("#mainDiv").scrollTop($("#mainDiv").scrollTop()-movey);
				                     currY = e.pageY;
				                     Uscrolls();
		                		});
		                $(document).mouseup( function(e){
		                    e.preventDefault();
		                    $("#scrollVDIV .sv_nav").css("backgroundColor",tempbg);
		                    $(document).unbind( "mousemove", hsc );
		            });


					});
			
			$("#rDiv").css("display","block");
			
			$("#loading").css("display","none");
			
			$('#mainDiv').mousewheel(function(event, delta) {
			    if(delta==-1)
			    {
			    	if($("#mainDiv").scrollLeft()< max_w*pageCount-40)
						$("#mainDiv").scrollLeft( $("#mainDiv").scrollLeft()+40);
			    		
			    }
			    else
			    	if($("#mainDiv").scrollLeft()>40)
						$("#mainDiv").scrollLeft($("#mainDiv").scrollLeft()-40);
			    	else
			    		$("#mainDiv").scrollLeft(0);
			    Uscrolls();
			});
			mainContentScroll();
	});	

function mainContentScroll()
{
	$("#mainDiv .page .middleRow .content  .mc_sc").each(function(){

		var mc=$(this).children(".mainContent:eq(0)");
		if(mc.attr("scrollHeight")>mc.height())
		{
			if($(this).children(".mainContentScroll").size()==0)
			{
				mc.css("float","left");
				mc.width($(this).width()-11);
				
				var el=$("<div></div>").appendTo(this); 
				var el2=$("<div></div>").appendTo(el);
				
				el.attr("align","center");
				el.addClass('mainContentScroll');
				el.height(mc.height());
				
				mc.mousewheel(function(event,delta){
					if(delta==-1)
				    {
				    	if($(this).scrollTop()< $(this).attr("scrollHeight")-20)
							$(this).scrollTop( $(this).scrollTop()+20);
				    		
				    }
				    else if(delta==1)
				    	if($(this).scrollTop()>20)
							$(this).scrollTop($(this).scrollTop()-20);
				    	else
				    		$(this).scrollTop(0);
					
					$(this).parent().children(".mainContentScroll").children().height($(this).height()*$(this).height()/$(this).attr("scrollHeight"));
					$(this).parent().children(".mainContentScroll").children().css("top",$(this).scrollTop()*$(this).height()/$(this).attr("scrollHeight"));
					return false;
				});
				
				el2.mousedown(function(e){
					el2.css("position","relative");
					e.preventDefault();
					var tempbg=$(this).css("backgroundColor");
					
					$this=$(this);
					$this.css("backgroundColor","gray");
	                currY = e.pageY;
	                
	                var mc2=$this.parent().parent().children(".mainContent");
	                $(document).bind( "mousemove",hsc2= function(e)
	                		{
			                	 e.preventDefault();
			                     var movey = (currY - e.pageY)*mc2.attr("scrollHeight")/mc2.height() ;
	                   
			                     mc2.scrollTop(mc2.scrollTop()-movey);
			                     currY = e.pageY;
			                     
			                     $this.height(mc2.height()*mc2.height()/mc2.attr("scrollHeight"));
			 					$this.css("top",mc2.scrollTop()*mc2.height()/mc2.attr("scrollHeight"));
			                     
	                		});
	                $(document).mouseup( function(e){
	                    e.preventDefault();
	                    $this.css("backgroundColor",tempbg);
	                    $(document).unbind( "mousemove", hsc2 );
	                });
				
				});
			}
			else
			{
				var el2=$(this).children(".mainContentScroll").children().eq(0);
			}
			$(this).children(".mainContentScroll").show();
			el2.height(mc.height()*mc.height()/mc.attr("scrollHeight"));
			el2.css("top",mc.scrollTop()*mc.height()/mc.attr("scrollHeight"));
			
		}
		else
		{
			if($(this).children(".mainContentScroll").size()>0);
				$(this).children(".mainContentScroll").hide();
		}
	});
}
function Uscrolls()
{
	$("#scrollHDIV .sh_nav").width(($("#scrollHDIV").width()-36)  / pageCount );
	$("#scrollHDIV .sh_nav").css("left",((max_w-36) * $("#mainDiv").attr("scrollLeft") /( max_w * pageCount ))+19);


	if($("#mainDiv").height()<$("#mainDiv").attr("scrollHeight"))
	{
		$("#scrollVDIV .sv_nav").css("display","block");
		$("#scrollVDIV .sv_nav").height((max_h-36) * max_h / $("#mainDiv").attr("scrollHeight"));
		$("#scrollVDIV .sv_nav").css("top",((max_h-36) * $("#mainDiv").attr("scrollTop") / $("#mainDiv").attr("scrollHeight")));
	}
	else
		$("#scrollVDIV .sv_nav").css("display","none");
}
function goToPage(pageNo)
{

	$("#mainDiv").animate({scrollLeft: $("#mainDiv #items .page").eq(pageNo-1).offset().left+$("#mainDiv").scrollLeft()}, 1000,function(){Uscrolls();});
}

