$(document).ready(function(){
		

	//var leftScroll = $("#scrollable").width() - $(window).width() + 50;
	
	var leftScroll = 0;
	
	//var leftScroll = $("#scrollable").width();
	var cols = 5; // number of columns
	var colWidth = 380; // width of each column
	var currentCol = 0;
	var totalGalleryWidth = 0;
	var subId;
	var navId = "home";
	var x1 = (-leftScroll + $(window).width() - 50); // returns 1870
	
	var numCol = 4; // number of colomns on the home page
	
	var first = true;
	var firstSub = true;
	var firstLoad = true;
	
	var dragging = false;
	
	var subnavOnStage = false;
	var atHome = true;
	var receivedFromPage = false;
	
	var path = $.address.path().substr(1);
	
	

    /*$('a').address.internalChange(function() {  
        return $(this).attr('href').replace(/^#/, '');  
		alert('test');
    });
*/


/*
$('#scrollable').touch({
		animate: false,
		sticky: false,
		dragx: true,
		dragy: false,
		rotate: false,
		resort: false,
		scale: false
	});*/

$('body').click(function(event) {
	if ($(event.target).is('#back')) {
		
	//alert('clicked');
	
	$("#scrollable").animate({
				left: 50
			}, {	
				duration:750
			});
	}
});




$.address.init(function(event) {  
   // alert('init');  
}); 

$.address.change(function(event) {  
    //alert('change'); 
	 
}); 

$.address.internalChange(function(event) {  
    //alert('internalChange');  
	
}); 

$.address.externalChange(function(event) {  
    //alert('externalChange path = ' + $.address.path().substr(1)); 
	
	//alert('external change');
	
	path = $.address.path().substr(1);
	
	//alert(path);
	
	if($.address.path() == "/")
	{
		$.address.path("home");	
	}
	else if ($.address.path() == "/home")
	{
		navId = 'home';
		scrollableOffStage();
	}
	else
	{	
		//navId = ($.address.path()).substr(1);
		
		receivedFromPage = false;
		//implies it is a sub-page
		//grab the section after the slash...
		//var path = $.address.path().substr(1);
		//alert(($.address.path().substr(1))).split('/')[1];
		//alert(path.split('/')[0]);
		navId = path.split('/')[0];
		subId = path.split('/')[1];
		
		//alert(navId + subId);
		
		if (subId == null)
		{
			//alert('subId == null');
			
			if (navId == 'projects')
			{	
				subId = 'brisbane';	
			}
			else if (navId == 'aboutus')
			{
				subId = 'vision';	
			}
			else if (navId == 'services')
			{
				subId = 'services-provided';
			}
			else {
				subId = '';	
			}
			
			$.address.path(navId + "/" + subId);
		}
		
		//alert('navId = ' + navId + ' and subId = ' + subId);
		//highlightNav(path.split('/')[0]);
		
		
		$("#scrollable").animate({
				left:+($(window).width() + 100)
			}, {	
				duration:750,
				complete:function()
				{
					// on complete, load the nav OFF the stage
					firstSub = true;
					loadNav();
				}
			});
	}
	
	/*else
	{
		//if the user didn't arrive at the page through link
		
			//implies it is a main-page
			navId = ($.address.path()).substr(1);
			changeContentMainNav();
		}*/

	
	//
	//alert(($.address.path()).substr(1));
	//if(subnavOnStage)
	//{
	//	moveContentOffStage();
	//}
	//else
	//{
	//	moveCopyGalleryOffStage();
	//}
	
	
}); 


/*    $('a').address(function() {  
       // navId = $(this).attr('href'); 
		return $(this).attr('href').replace(/^#/, ''); 
		 
    }); */ 

	
	//if logo is clicked, take user home
	$("#logo").click(function(event) {
		
		if(navId == 'home')
		{
			//alert('true');
			// do nothing
		}
		else
		{
			//alert('false');
			//navId = 'home';
			scrollableOffStage();
		}
		
		navId = 'home';
		$.address.path("home");	
		
		setWidth();
		
		
	
	});
	
	$("#scrollable").draggable({ 
		start: function(){
			dragging = true;
			//setWidth();
			//alert(($("#container-nav").width()) + ($("#container-subnav").width()) + ($("#container-copy").width()) + totalGalleryWidth - $(window).width() +50);
			//alert('leftScroll in draggable '+leftScroll);	
			//alert((-leftScroll + $(window).width() -50));
			//x1 = (-leftScroll + $(window).width() -50);
		},
		axis: 'x',
		containment: [-x1,0,50,0],
		scroll:false,
		cursor:'move',
		//stop: scrollerPosition
		touchSelector: 'a, .touchable', 
		slideSelector: 'a.slide',
		stop: function() {dragging = false;}
	
	});
	
	function scrollerPosition()
	{
		//alert(-totalGalleryWidth);
		//var scrollPos = $("#scrollable").offset();
		//var leftPos = scrollPos.left-50;
		//var itemNum = Math.floor(Math.abs(leftPos/colWidth));
		//currentCol = -itemNum;
	}
		

	//HOME PAGE NAVIGATION (IMAGES)------------------------------------------------------------------------------------------------------------------------------------------------
		$('body').click(function(event) {
   if ($(event.target).is('#scrollable .col .cell a img') || $(event.target).is('#scrollable .col .cell a span')) {
	
	//$("#scrollable .col .cell a").click(function(event) {

		// set the navId as the target's id
		navId = $(event.target).parent().attr('id');
		
		atHome = false;
		
			// move the scrollable area OFF stage
			$("#scrollable").animate({
				left:+($(window).width() + 100)
			}, {	
				duration:750,
				complete:function()
				{
					// on complete, load the nav OFF the stage
					loadNav();
				}
			});
	}});
	
	function loadNav()
	{
		$.ajax({
		  url: "navigation.html",
		  cache: false,
		  dataType: 'html',
		  success: function(html){
			// replace contents with loaded html
			$("#scrollable").html(html);
			
			//highlight current navigation item
			highlightNav(navId);
			
			//load subnav
			if (navId == 'projects' || navId == 'aboutus' || navId == 'services')
				{
					//alert('calling loadSubNav()');
					loadSubNav();
				}
				else
				{
					//alert('calling loadOther()');
					loadOther();	
				}
			
			//move scrollable on stage
			scrollableOnStage();
			
		  }// end success
		});// end ajax
	} // end loadNav
	
	
	
	//bring scrollable area back on stage
	function scrollableOnStage()
	{
		$("#scrollable").animate({
			left:50}, {
			duration:750, 
			complete: function() { 
				}
			});//end animate	
	}
	
	//bring scrollable area back on stage
	function scrollableOffStage()
	{
		$("#scrollable").animate({
			left:+($(window).width() + 100)}, {
			duration:750, 
			complete: function() {
				//check if home is active
				if(navId == "home")
				{
					
					loadHome();
					subnavOnStage = false;
					firstSub = true;
					first = true;
				}
 
				}
			});//end animate	
	}
	
	function loadHome()
	{setWidth();
		$.ajax({
		  url: "home-content.html",
		  cache: false,
		  dataType: 'html',
		  success: function(html){
			// replace contents with loaded html
			$("#scrollable").html(html);
			
			//move scrollable on stage
			scrollableOnStage();
			
			atHome = true;
			
		  }// end success
		});// end ajax	
	}
	
	function subNavOffStage()
	{
		$("#container-subnav").animate({
			left:+($(window).width() + 100)}, {
			duration:750, 
			complete: function() {
				//subNavOffStage(); 
				}
			});//end animate		
	}
	
	function purge()
	{
		//alert('purge()');
		$("#container-copy").remove();  
		$("#container-gallery").remove();
		loadSubNav();  
	}
	
	function purgeEverything()
	{
		//alert('purgeEverything()');
		$("#container-copy").remove();  
		$("#container-gallery").remove();
		$("#container-subnav").remove();
		subnavOnStage = false;
		loadSubNav();  
	}
	
	//MAIN NAVIGATION------------------------------------------------------------------------------------------------------------------------------------------------
	$('body').click(function(event) {
   //alert('clicked');
   if ($(event.target).is('#container-nav li a')) {
     //set navId as target's id
	 navId = $(event.target).attr('id');
	 //alert(navId);
	 // if the menu item selected was home, take the user back to the unique home page
	 if (navId == "home")
	 {
		atHome = true;
		setWidth();
		scrollableOffStage(); 
	 }
	 else
	 {
		 highlightNav(navId);
		 moveContentOffStage();

		 atHome = false;
	 }
	 
	 // set Projects to first time viewing
		 first = true;
	 	firstSub = true; 
	 }
	 
	 if (navId == 'projects')
	 {
		subId = 'brisbane'; 
	 }
	 else if (navId == 'services')
	 {
		subId = 'services-provided';
	 }
	 else if (navId == 'aboutus')
	 {
		subId = 'vision'; 
	 }
	 else
	 {
		 subId = null;
	 }
	 
 });
 
 
 function changeContentMainNav()
 {
	// if the menu item selected was home, take the user back to the unique home page
	 if (navId == "home")
	 {
		atHome = true;
		setWidth();
		scrollableOffStage(); 
	 }
	 else
	 {
		 //highlightNav(navId);
		 //moveContentOffStage();
		 
		 $("#scrollable").animate({
				left:+($(window).width() + 100)
			}, {	
				duration:750,
				complete:function()
				{
					// on complete, load the nav OFF the stage
					loadNav();
				}
			});

		 atHome = false;
	 }
	 
	 // set Projects to first time viewing
		 first = true;
	 	firstSub = true; 
 }
 
 function highlightNav(id)
	{
		$("#container-nav li div").animate({opacity:0});
		$("#container-nav li div#overlay-"+id).animate({opacity:0.5},{
			//complete:subNavOffStage
			//complete:hideProject
			});
	}
	
	//moves gallery, copy and subnav off the stage
	function moveContentOffStage()
	{
		$("#container-gallery").animate({
			left:+($(window).width() + 100)}, {
			duration:750, 
			complete: function() {
				}
			});//end animate	
			
		$("#container-copy").delay(200).animate({
			left:+($(window).width() + 100)}, {
			duration:750, 
			complete: function() {
				
				}
			});//end animate	
			
		if(subnavOnStage)
		{
			//alert('subnavOnStage');
			//alert('navId != "projects" || navId != "aboutus" || navId != "services"');
		$("#container-subnav").delay(400).animate({
			left:+($(window).width() + 100)}, {
			duration:1000, 
			complete: function() {
				//alert('onComplete');
				//loadOther();
				//loadSubNav();
				purgeEverything(); 
				}
			});//end animate	
		}
		else
		{
			//alert('!subnavOnStage');
			//alert("else");
			//hack
			window.setTimeout(purge, 1500);
		}
	}
	
	function loadSubNav()
	{
		//alert('loadSubNav and navId='+navId);
		
		//NOTE THAT EACH CONDITION MUST BE FULLY EXPRESSED
		if (navId == 'projects' || navId == 'aboutus' || navId == 'services')
		{
			//alert('navId == "projects" || navId == "aboutus" || navId == "services"');
		$.ajax({
		  url: "navigation-sub-"+navId+".html",
		  cache: false,
		  dataType: 'html',
		  success: function(html){
			  
			  // add subnav or append
			  if(firstSub)
			  {
				//alert('firstSub true');
				$("#scrollable").append(html);  
				$("#container-subnav").animate({left:$(window).width()},0).animate({left:139},750);
				subnavOnStage = true;
				firstSub = false;
			  }
			  else
			  {
				 //alert('firstSub false');
				//$("#container-subnav").remove();  
				//$("#scrollable").append(html); 
			  }
			  
			  if (!receivedFromPage)
			  {
				  //if the request was from address, perform highlight
				  $("#container-subnav li a#"+subId).addClass('selected');
			  }
			
			//$("#container-subnav").animate({left:$(window).width()},0).animate({left:139},750);
			hideProject();
		  }
		}); // end ajax
		}
		else
		{
			//alert('loadSubNav else');
			hideProject();
		}
	}
		
	//SUB NAV CLICK------------------------------------------------------------------------------------------------------------------------------------------------
	$('body').click(function(event) {
   if ($(event.target).is('#container-subnav li a') || $(event.target).is('#container-copy a')) {
	 
	 if($(event.target).attr('id') == 'www')
	 {
		 //alert('www');
		 window.open( $(event.target).attr('href') );
        return false;
	 }
	 else
	 {
		//alert('not www');
		subId = $(event.target).attr('id');
		receivedFromPage = true;
		highlightSubNav(subId); 
	 }
	 
	 }
 });
 
 
 	function highlightSubNav(navItem)
	{
		//alert("highlightSubNav");
		//$("#container-nav li a").removeClass('selected');
		//$("#container-nav li a").addClass('selected');
		$("#container-subnav li a").removeClass('selected');
		
			/*if (navItem != '')
			{
				//
				
			}*/
			
			//alert(navItem);
			
		$(navItem).addClass('selected');	
		$("#container-subnav li a#"+navItem).addClass('selected');
		
		if (receivedFromPage)
		{
			//function launched from within page
			moveCopyGalleryOffStage();
		}
		else
		{
			//function launched from address
			//alert('moveContentOffStage called');
			moveContentOffStage();	
		}
	}
 
 	function moveCopyGalleryOffStage()
	{
		//alert('moveCopyGalleryOffStage');
		$("#container-gallery").animate({
			left:+($(window).width() + 100)}, {
			duration:750, 
			complete: function() {
				}
			});//end animate	
			
		$("#container-copy").delay(200).animate({
			left:+($(window).width() + 100)}, {
			duration:750, 
			complete: function() {
				//alert('about to purge');
				purge();
				}
			});//end animate		
	}
 
 	function hideProject()
	{
		//alert('hideProject called');
		//alert('subId in hideProject() is ' + subId);
		
		//alert(navId);
		//alert(subId);
		
		//alert('first ' + first);
		
		if (subId != '')
		{
			first = false;	
		}
		
		if (first == true && navId == 'projects')
		{
			//alert('first && navId == "projects"');
			$.ajax({
		  url: "projects-brisbane.html",
		  cache: false,
		  dataType: 'html',
		  success: function(html){
			//  alert(html);
			$("#scrollable").append(html);
			setWidth();
			
			//alert('subId is ' + subId);
			if (subId == null)
			{
				subId = 'brisbane';
			}
			
			if (subId == 'brisbane' || subId == null)
			{
				$("#container-subnav li a").removeClass('selected');
				$("#container-subnav li a#brisbane").addClass('selected');
			}
			
			$("#container-copy").animate({left:$(window).width()}, 0).animate({left:278},750);
			$("#container-gallery").animate({left:($(window).width()+$("#container-copy").width())}, 0).delay(250).animate({left:728},750);
			first = false;
			
		  }
		});

		} 
		else if (first == true && navId == 'aboutus')
		{
			//alert('first && navId == "aboutus"');
			$.ajax({
		  url: "aboutus-vision.html",
		  cache: false,
		  dataType: 'html',
		  success: function(html){
			//setWidth();
			$("#scrollable").append(html);
			
			if (subId == null)
			{
				subId = 'vision';
			}
			
			if (subId == 'vision' || subId == null)
			{
				$("#container-subnav li a").removeClass('selected');
				$("#container-subnav li a#vision").addClass('selected');
			}
			
			$("#container-copy").animate({left:$(window).width()}, 0).animate({left:278},750, setWidth);
			//$("#container-gallery").animate({left:($(window).width()+$("#container-copy").width())}, 0).delay(250).animate({left:728},750);
			first = false;	
			}
		});
		}
		else if (first == true && navId == 'services')
		{
			//alert('first && navId == "projects"');
			$.ajax({
		  url: "services-services-provided.html",
		  cache: false,
		  dataType: 'html',
		  success: function(html){
			//  alert(html);
			$("#scrollable").append(html);
			//setWidth();
			
			if (subId == null)
			{
				subId = 'services-provided';
			}
			
			if (subId == 'services-provided' || subId == null)
			{
				$("#container-subnav li a").removeClass('selected');
				$("#container-subnav li a#services-provided").addClass('selected');
			}
			$("#container-copy").animate({left:$(window).width()}, 0).animate({left:278},750);
			$("#container-gallery").animate({left:($(window).width()+$("#container-copy").width())}, 0).delay(250).animate({left:728},750, setWidth);
			first = false;
			
		  }
		});

		} 
		else
		{
			//alert('neither true');
			/*$("#container-copy").delay(250).animate({
			left:+$(window).width() // MOVE OFF STAGE
			},750,function()
			{
				alert('onComplete of container-copy moving off stage');
				$("#container-copy").remove();
				
				//needed when content is not being loaded for the first time*/
				if(navId == 'projects' || navId == 'aboutus' || navId == 'services')
				{
					//alert('navId == "projects" || navId == "aboutus"');
					loadProject();
				}
				else
				{
					//alert('else loadOther');
					loadOther();
				}

			/*});
			
		$("#container-gallery").animate({
			left:+($(window).width()+$("#container-copy").width())  // MOVE OFF STAGE
			}, {
			duration:750,
			complete:function()
			{
				$("#container-gallery").remove();
				//loadProject();
			}
			
			});*/
			
		}
			
		//loadProject();
		//	
	}
 
	function loadProject()
	{
		//alert('loadProject');
		
		//alert('navId = ' + navId+ ' ' + 'subId ' + subId);
		
		$.ajax({
		  url: navId+"-"+subId+".html",
		  cache: false,
		  dataType: 'html',
		  success: function(html){
			//alert('onComplete');
			$("#scrollable").append(html);
			//setWidth();
			
			//POSITION COPY AND GALLERY DIVS OFF STAGE
			$("#container-copy").animate({left:$(window).width()},0);
			$("#container-gallery").animate({left:$(window).width()+$("#container-copy").width()},0);
			
			//MOVE THEM ON STAGE
			$("#container-copy").animate({left:278},750);
			//$("#container-gallery").delay(250).animate({left:728},750, setWidth);
			$("#container-gallery").delay(250).animate({left:$("#container-copy").width() + $("#container-nav").width() + $("#container-subnav").width() + 50},750, setWidth);
			
			/*$("#container-gallery img").hide();
			$("#container-gallery img").ready(function () {
				$("#container-gallery img").fadeIn(600);
			});*/
			
			$("#container-gallery img").hide().not(function() {
			   return this.complete && $(this).fadeIn(600);
		   }).bind("load", function () { $(this).fadeIn(600); });
			
		  },
		  error:showError
		});
		
		//setWidth();
	}	
	
	function showError(jqXHR, textStatus, errorThrown)
	{
		//alert(textStatus);
	}
	
	function loadOther()
	{
		
		//alert('navId = ' + navId)
		
		if(navId != 'latest-project')
		{
		//alert('loadOther() + '+ navId);
		$.ajax({
		  url: navId+".html",
		  cache: false,
		  dataType: 'html',
		  success: function(html){
			//  alert(html);
			$("#scrollable").append(html);
			//setWidth();
			
			//POSITION COPY AND GALLERY DIVS OFF STAGE
			$("#container-copy").animate({left:$(window).width()},0);
			$("#container-gallery").animate({left:$(window).width()+$("#container-copy").width()},0);
			
			//MOVE THEM ON STAGE
			$("#container-copy").animate({left:139},750);
			$("#container-gallery").delay(250).animate({left:($("#container-copy").width() + $("#container-nav").width() + $("#container-subnav").width()) },750, setWidth);
			
			$("#container-gallery img").hide().not(function() {
			   return this.complete && $(this).fadeIn(600);
		   }).bind("load", function () { $(this).fadeIn(600); });		
			
		  }
		});	
		}
		else
		{
			$.ajax({
		  url: navId+".html",
		  cache: false,
		  dataType: 'html',
		  success: function(html){
			//  alert(html);
			$("#scrollable").append(html);
			//setWidth();
			
			//POSITION COPY AND GALLERY DIVS OFF STAGE
			$("#container-copy").animate({left:$(window).width()},0);
			$("#container-gallery").animate({left:$(window).width()+$("#container-copy").width()},0);
			
			//MOVE THEM ON STAGE
			$("#container-copy").animate({left:139},750);
			$("#container-gallery").delay(250).animate({left:($("#container-copy").width() + $("#container-nav").width() + $("#container-subnav").width() + 50)},750, setWidth);
			
			$("#container-gallery img").hide().not(function() {
			   return this.complete && $(this).fadeIn(600);
		   }).bind("load", function () { $(this).fadeIn(600); });
			}
		});	
		}
	}
			
			
	function setWidth()
	{
		totalGalleryWidth = 0;
		
		$("#container-gallery").children().each(function()
		{
			totalGalleryWidth += $(this).width();
		});
		
		//alert('$("#container-nav").width()) = ' + $("#container-nav").width() + ' ($("#container-subnav").width()) = ' + $("#container-subnav").width() + ' ($("#container-copy").width()) = ' + $("#container-copy").width() + 'totalGalleryWidth = ' + totalGalleryWidth - $(window).width() + 50);
		
		// CALCULATES WIDTH
		//leftScroll = (278 + ($("#container-copy").width()) + totalGalleryWidth + 50 - $(window).width());
		if(navId != 'latest-project' && navId != 'home')
		{
			leftScroll = (($("#container-nav").width()) + ($("#container-subnav").width()) + ($("#container-copy").width()) + totalGalleryWidth + 50);
			//alert(leftScroll);
		}
		else if (navId != 'home')
		{
			//alert('latest project');
			leftScroll = (($("#container-nav").width()) + ($("#container-copy").width()) + totalGalleryWidth);
		}
		else
		{
			//alert('navId must = home');
			leftScroll = 1140;	
		}
		//alert('leftScroll in setWidth() ' + leftScroll);
		
		if (navId == 'home')
		{
			$("#scrollable").css({
				
				width: 380 * numCol + 50
				
				});
		}
		else if (navId == 'aboutus' || navId == 'services'){
			//alert('setting');
			$("#scrollable").css({
				
				width: $("#container-nav").width() + $("#container-subnav").width() + ($("#container-copy").width()) + 50
				
				});
		}
		else if (navId == 'contactus') {
			$("#scrollable").css({
				width: $("#container-nav").width() +($("#container-copy").width()) + 50
				});
		}
		else
		{
			$("#scrollable").css({
				width: ($("#container-nav").width()) + ($("#container-subnav").width()) + ($("#container-copy").width()) + totalGalleryWidth + 50
				});
		}
		
		if ($("#scrollable").width() < $(window).width())
		{
			//alert($("#scrollable").width());
			//alert('scrollable width is less than window width');
			x1 = -50;
		}
		else
		{
			//alert('leftScroll = ' + leftScroll);
			//alert('window width = ' + $(window).width());
			x1 = Math.abs((-leftScroll + $(window).width() - 50));
			//alert('x1 = ' + x1);
		}
		
		//alert('x1 ' + x1);
		
		$("#scrollable").draggable('option',"containment",[-x1,0,50,0]);
		var containment = $( "#scrollable" ).draggable( "option", "containment" );
		//alert('containment ' + containment);
	}
	
	
	function setX1()
	{
		if ($("#scrollable").width() < $(window).width())
		{
			//alert($("#scrollable").width());
			//alert('scrollable width is less than window width');
			x1 = -50;
		}
		else
		{
			//alert('leftScroll = ' + leftScroll);
			//alert('window width = ' + $(window).width());
			x1 = Math.abs((-leftScroll + $(window).width() - 50));
			//alert('x1 = ' + x1);
		}
		
		$("#scrollable").draggable('option',"containment",[-x1,0,50,0]);
		var containment = $( "#scrollable" ).draggable( "option", "containment" );	
	}
			
	$(".arrows").css({
		opacity:0.8
	});
	
	$(".arrows").hover(function(){
		$(this).css({
			opacity:1
		});
	}, function() {
		$(this).css({
			opacity:0.8
		});
	});
	
	$("#leftArrow").click(function(){arrowClick('left')});
	$("#rightArrow").click(function(){arrowClick('right')});
	
	function arrowClick(direction)
	{
		//alert('called');
		
		setWidth();
		
		// absolute position relative to doc
		var scrollPos = $("#scrollable").offset();
		var leftPos = scrollPos.left;
		//alert(leftPos);
		//leftScroll = leftScroll + $(window).width();
		
		if(direction=="left")
		{
			//alert('leftPos: '+leftPos);
			leftPos = leftPos + colWidth;
			if(leftPos > 50)
			{
				leftPos=50;
			}
		}
		else
		{
			//alert('leftPos: '+leftPos + 'leftScroll :' + (-leftScroll));
			//alert('right');
			
			//sets value to offset by everytime this condition is true BEFORE checking...
			leftPos = leftPos - colWidth;
			//alert('leftPos' + leftPos + ' leftSroll:' + leftScroll);
			//alert('$("#scrollable").width() ' + $("#scrollable").width());
			
			if(leftPos < -leftScroll + $(window).width() - 50 )
			{
				
				//alert('leftScroll RIGHT is ' + leftScroll);
				
				// and if the content is smaller than the window width, always position it at left:50
				if ($("#scrollable").width() < $(window).width())
				{
					//alert('true');
					//alert("$('#scrollable').width() < $(window).width()");
					// if the scrollable area is smaller than the screen width, set it to 50 (default)
					leftPos = 50;
				}
				else
				{
					//alert('false');
					//leftPos = -leftScroll - 50;
					//alert(-leftScroll);
					
					//leftPos = -(leftScroll) - 150;
					
					if (firstLoad == true || navId == 'home') {
						//alert('true');
						leftPos = -($('#scrollable').width() - $(window).width());	
						//alert('navid = home and leftPos = ' + leftPos);
					}
					else
					{
						//alert('false');
						//leftScroll = $('#scrollable').width();
					leftPos = -(leftScroll + $(window).width() -150);
					//alert('navid != home and leftPos = ' + leftPos);
					}
					
					
					
					
					//leftPos = -328;
					//alert(leftPos);
				}
				
			}
		}
		
		//MOVING ACTION			
		$("#scrollable").stop().animate({
			left: leftPos
		}, 500, 'easeOutExpo');
	}
	
	
	$(window).resize(function() 
	{
		resize();
	});
	
	 $('body').scroll(function () { 
	 //alert('scrolled');
      $('#rare').css({ left: $(window).width() - 70, bottom: 5 });
    });

	
	function resize()
	{
		var scrollPos = $("#scrollable").offset();
		var leftPos = scrollPos.left;
		var scrollPercent = leftPos/leftScroll;
		
		setX1();
		
		//leftScroll = $("#scrollable").width() - $(window).width() + 50;
		
		//$("#scrollable").draggable('option',"containment",[-leftScroll,0,50,0]);	
		
		$("#logo").css({
		left: ($(window).width()/2) - ($("#logo").width()/2)
		});
		
		
		$('#rare').css({ left: $(window).width() - 70, bottom: 5 });
	
	}
	
	// hide images then fade them in on load
	$("img").hide();
	$("img").load(function () {
		$("img").fadeIn(600);
	});	
	
});
