// This is the website specific javascript file, anything that is site specific gets put in here

$(document).ready(function(){ 
 
	
	$('#s1').cycle({
		fx: 'fade',
		timeout:  5000,
		cssBefore: {  
        	left: 115,  
        	top:  115 
    	}
	});	
	
	$('#s2').cycle({
	//	fx: 'cover',
		timeout:  10000
		
	});	
	
	

	$('.exhibition').each(function() {
		$(this).find('p:not(:first)').hide();
	});
	
//	$('.thumbs').jcarousel();
	
/*	$('.gallery .title').click(function() {
	
			init_gallery();

		return;
		if ($(this).parent().hasClass('open'))
			hide_galleries();
		else {
			$(this).parent().addClass('open');
			show_gallery();
		}
		
	
	});
	
*/	

	var showing = $('<img/>');
	


	var alsoshowing = $('<img style="display:none;" />');
	
	showing.attr('id','mainimage');
	
	//alsoshowing.attr('id','subimage');
	
	var mainImage = $('#main');
	
	
	mainImage.hide();
	
	mainImage.append(showing);
	
	mainImage.append(alsoshowing);
	
	var active = null;
	
	var image_sets = new Array();
	
/*	$('.thumbs li img').load(function() {
		var $t = $(this);
		alert($t.offset().top);
	
	});
*/	
	$('.thumbs_holder').css('overflow','hidden');
	
	var offset = 0;
	
	var height = 330;
	
	var max_height = $('.thumbs').height();

	var i = 0;
	
	
	var images = [];
	
	var page_number = 0;
	
	var count = 0;
	
	var total_count = $('.thumbs img').length;
	
	var offsets = [];
	
	$('.thumbs img').each(function() {
		
		var $t = $(this);
		
		$t.attr('id','image'+i);
		
		$t.load(function() {
			var str = $t.offset().top;
			if (!images[str])
				images[str] = [];
			images[str].push($t);
			if (!in_array(str,offsets))
				offsets[offsets.length] = str;
			$t.addClass('image-'+str);
			count++;
			
			if (count == total_count)
				check_batch();
		})
		i++;
	});

	
	function shift_images() {
		return;
		max_height = $('.thumbs').height();
		
		var last_offset = 0;
		for(var propertyName in images) {
			last_offset = propertyName;
		}
		
		max_height = last_offset;
		
		for(var propertyName in images) {
			if (propertyName == last_offset)
				break;
		
			imgs = images[propertyName];
			var count = imgs.length;
			var total_width = 0;
			for (var i = 0;i<count;i++) {
				total_width += imgs[i].width();
			}
			var max_width = 780;
			var padding = (max_width - total_width) / count;
			var pad = parseInt(padding);
	//		$('.image-'+propertyName).css('padding-right',pad);
			
			var dif = padding - (pad * count);
	//		$('.image-'+propertyName+':last').css('padding-right',0).parent().css('padding-right',0);
		}
	
	}

	$('#prev_batch').click(function() {
		offset += height;
		page_number--;
		$('.thumbs li').animate({top:offset});
		check_batch();
		
		return false;
	});
	
	$('#next_batch').click(function() {
		offset -= height;
		page_number++;
		$('.thumbs li').animate({top:offset});
		check_batch();
		return false;
	});
	
	function check_batch() {
		$('#next_batch, #prev_batch').hide();
		if (offset < 0)
			$('#prev_batch').show();
		
		if (max_height > 0) {
			if (offset > 0 - max_height + 700)
				$('#next_batch').show();
		}
		else {
			if (offsets.length > (page_number + 1) * 2) // if there are more images on line x
				$('#next_batch').show();
			else
				$('#next_batch').hide();
		}
		if (page_number > 0)
				$('#prev_batch').show();
		
		
	}
	

	$('.thumbs a').each(function() {		
		var $this = $(this);
		var img = $this.find('img');
		var href = $this.attr('href');
		var title = $this.attr('title');
		var desc = $this.parent().find('.description').html();
		
		



		
		//alert(id);
		$('#main').click(function() {
			$(this).fadeOut();
			$('.thumbs').animate({opacity:1})
			$('#mainControls').fadeIn();
			
			$('#main').removeClass('theborder');
			$('.inner').removeClass('noborder');
			$('.inner').removeClass('nopadding');
			
		});
		
		$this.click(function() {
			$('.thumbs').animate({opacity:0.2})
			$('#mainControls').fadeOut();
			active = $(this);
			//alert("test");
			show_image(active);
			mainImage.fadeIn();
			
			
			$('#main').addClass('theborder');
			$('.inner').addClass('noborder');
			$('.inner').addClass('nopadding');
			
			return false;
		})
		
	});
	
	$('#next_image').click(function() {
			var li = active.parent().next();
			active = li.find('a');
		//	show_image(active);
			active.click();	
			return false;
		});
		
		$('#prev_image').click(function() {
			var li = active.parent();
			active = li.prev().find('a');
			active.click();
	//		show_image(active);
		
		
			return false;
		});
	
	function show_image(el) {
		$('#alt_imgs').html('');
		//alert("blah");
		var i=1;
			for (i=1;i<=9;i++)
			{
				$("#"+(i+1)).html('')
			}
		showing.attr('src',el.attr('href')).load(function() {
			
			
			
			$('#info').html(el.attr('title')+'<br/>'+el.parent().find('.description').html() + 
			
			'<a href="#" class="helv new_back_button"><img src="/assets/img/left_arrow.jpg" style="vertical-align:center;padding-top:1px"> Back</a>');
			
			//'<span class="back_button">Back</span>');
	//		alert("-"+(parseInt(showing.width() / 2))+'px');
			showing.css('margin-left',"-"+((parseInt(showing.width() /2) ))+'px');
			
			
			//showing.css('margin-left',"-"+(parseInt(alsoshowing.width() / 2))+'px');
		});
			var id = el.parent().find('.img_id').html();
			var text;
			
		$.getJSON("/json?image="+id,
			function(json){
			  i=0;
			  r=0;
			  
			  //alert(json);
				  $(json).each(function ()  {
					  //alert(json[i]);
					var img = $('<img/>');
					//alternate_images[i].attr('id','subimage');
					img.attr('src',json[i]);
					
					
					var a = $('<a/>');
					a.attr('href',json[i+1]).html(img);
					
					
					
					a.click(function() {
						showing.attr('src',$(this).attr("href"));
						return false;
					})
					
					
					var width = 300;
					
					
					//var width = $('#main').width() - showing.width() - showing.offset().left;
					
					var width = $('#main').width() - 510 - showing.offset().left;
					$('#alt_imgs').append(a).width(width);
					
					//$("#"+(r+1)+ " a").attr('href')
					i++;
					i++;
					r++;
				  });
			});
		check_next_prev();
	}
	
	
	function check_next_prev() {
		$('#prev_image').hide();
		$('#next_image').hide();
		
		
		var li = active.parent();
		
		if (li.next().length > 0)
			$('#next_image').show();
		if (li.prev().length > 0)
			$('#prev_image').show();
	}

});



function in_array (needle, haystack, argStrict) {
    // Checks if the given value exists in the array  
    // 
    // version: 1008.1718
    // discuss at: http://phpjs.org/functions/in_array
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: vlado houba
    // +   input by: Billy
    // +   bugfixed by: Brett Zamir (http://brett-zamir.me)
    // *     example 1: in_array('van', ['Kevin', 'van', 'Zonneveld']);
    // *     returns 1: true
    // *     example 2: in_array('vlado', {0: 'Kevin', vlado: 'van', 1: 'Zonneveld'});
    // *     returns 2: false
    // *     example 3: in_array(1, ['1', '2', '3']);
    // *     returns 3: true
    // *     example 3: in_array(1, ['1', '2', '3'], false);
    // *     returns 3: true
    // *     example 4: in_array(1, ['1', '2', '3'], true);
    // *     returns 4: false
    var key = '', strict = !!argStrict;
 
    if (strict) {
        for (key in haystack) {
            if (haystack[key] === needle) {
                return true;
            }
        }
    } else {
        for (key in haystack) {
            if (haystack[key] == needle) {
                return true;
            }
        }
    }
 
 
}



