$(document).ready(function() {
	
	// scroll thumbnails horizontally
	$('a.next').live('click',function(){
		var width = $('.scrollcont').width();
		$('#main').scrollTo({top:'+=0',left:'+='+width},500);
		return false
	});
	$('a.previous').live('click', function(){
		var width = $('.scrollcont').width();
		$('#main').scrollTo({top:'+=0',left:'-='+width},500);
		return false
	});
	
	// scroll vertically
	$('header nav ul li a').each(function(){
		$(this).click(function(){
			$(this).parent().parent().each(function(){
				$(this).children().children().removeClass("active");
			});
			$(this).addClass("active");
			var contindex = $(this).parent().index();
			$('#main').scrollTo($(".catpage:eq("+contindex+")"),200);
			if ($(this).hasClass("bgallery")) {
				$('.bgallery-contact').show();
			} else {
				$('.bgallery-contact').hide();
			}
			return false
		});
		
	});
	
	// scroll to about vertically
	$('a#about').click(function(){
		$('#main').scrollTo({top:'+=450', left:'0'},500);
		return false
	});
	


	
	
	
	
	
	// load supersized background image where available
	if ($('#supersize').length) {
		$.fn.supersized.options = {  
			startwidth: 1024,  
			startheight: 768,
			vertical_center: 1,
			slideshow: 0,
		};
  	$('#supersize').supersized();
	}
	
	
	$('.QLoader').live('click', function() {
		$('.bgallery-contact').attr('id','supersize');
			$.fn.supersized.options = {  
				startwidth: 1024,  
				startheight: 768,
				vertical_center: 1,
				slideshow: 0,
			};
	  	$('#supersize').supersized();
		
		
		$('#main').height($(window).height()-250);
		$('.scrollcont').height($(window).height()-250);

		$('#main').width($(window).width()-140);
		$('.scrollcont').width($(window).width()-140);
		
		var index = 1
		$('.catpage').each(function(){
			// set IDs
			$(this).attr('id','page'+index)
			$('#page'+index+' .firstcol').columnize({
				buildOnce: false,
				height: $(window).height()-250,
				width: $(window).width()-140,
				target: '#page'+index+' .firstcol',
				overflow: {
					id: '#page'+index+" .nextcol",
					width: $(window).width()-140,
					height: $(window).height()-250,
					doneFunc: function(){
						Shadowbox.setup('a.sbox', {
							animate: false,
							modal: true,
							overlayColor: '#35362a',
							overlayOpacity: 1,
							displayCounter: false
						});
						var firstels = $('#page'+index+' .firstcol .first').children().size()-1;
						var nextels = $('#page'+index+' .nextcol').children().size();
						var diffels = firstels-nextels;

						
						if ($('#page'+index+' .nextcol').is(':empty')) {
							//$('#page'+index+' .firstcol a.next').remove();
						} else {
							var el = $('#page'+index+' .firstcol a.next');
							$(el).detach();
							$('<a class="next" href="" title="next">next</a>').insertAfter('#page'+index+' .firstcol .first a:last');
						}

						if (nextels > 0) {
							$('<a class="previous" href="" title="previous">previous</a>').insertAfter('#page'+index+' .nextcol a:last');
							$('#page'+index+' .firstcol a:gt("+diffels+")').hide();
						} else {
							$('#page'+index+' .nextcol .previous').remove();
							$('#page'+index+' .firstcol a').each(function(){$(this).show});
						}
					}
				}
			});
			
			
			
			index++
		})
		
	});
	
	
});
