jQuery(function() {
	function mycarousel_callback(carousel)
	{
		jQuery(".control_3").bind("click", function() {
			carousel.scroll(jQuery.jcarousel.intval(ImgNum));
			return false;
		});
	
		jQuery(".control_1").bind("click", function() {
			carousel.scroll(jQuery.jcarousel.intval(ImgNum));
			return false;
		});
	}
	
	if (jQuery('.fgcount_1').length == 0)
	{
		jQuery('#mycarousel').jcarousel({
			scroll: 8,
			visible: 8,
			initCallback: mycarousel_callback
		});
	}
	
	fadegallery_set_current('0');
	
	
	jQuery('.fallery_all').toggle(function(){
		jQuery('.control_0').addClass('control_0_active');
		jQuery('a.gf').each(function(id){
			jQuery('.fallery_show_all').append('<a href="'+jQuery(this).attr('href')+'" target="_blank"><img src="'+jQuery(this).attr('href')+'" alt="" /></a>');
		});
		jQuery('#thumbs').hide();
		jQuery('.co').fadeOut();
		jQuery('#mainimage').hide();
		
		jQuery('#pcaption .a').hide();
		jQuery('#pcaption .b').show().html('<p><strong>Foto overzicht: '+jQuery('#pcaption small').text().substr(1)+' foto\'s</strong></p>');
	},
	function(){
		jQuery('.control_0').removeClass('control_0_active');
		jQuery('.fallery_show_all').empty();
		jQuery('#thumbs').show();
		jQuery('.co').fadeIn();
		jQuery('#mainimage').show();
		jQuery('#pcaption .a').show();
		jQuery('#pcaption .b').empty();
	});
});

var eerste_klik = 1;
var fade_stop = false;

function cache_images ()
{
	if (eerste_klik == 1)
	{
		jQuery('#mainimage').css('background', '#000000');
		jQuery('.fallery_loading').show();
		
		eerste_klik = 0;
		
		//cache the images
		pix.imgsLen = pix.imgs.length;
		pix.imgsLen_ = pix.imgs.length -1;
		pix.cache = [];
		for(var i=0; i<pix.imgsLen; i++)
		{
			pix.cache[i] = new Image;
			pix.cache[i].src = pix.imgs[i];
		}
		
		setTimeout(function() {jQuery('.fallery_loading').fadeOut();}, 2000);
	}
}


var ImgNum = 0;
var ImgMax = pix.imgs.length - 1;

//Time delay between Slides in milliseconds
var delay = 5000;

var lock = false;
var run;

function NextPrevious(direction)
{
	if (document.images) 
	{
		ImgNum = ImgNum + direction;
		if (ImgNum > ImgMax) 
		{
			ImgNum = 0;
		}
		if (ImgNum < 0) 
		{
			ImgNum = ImgMax;
		}
		DispImg(ImgNum);
	}
}

function SelectImage(slideNum)
{
	if (document.images) 
	{
		ImgNum = slideNum;
		
		var targetOffset = jQuery('#slide'+ImgNum).offset().top;
		jQuery('html, body').animate({scrollTop: targetOffset}, 400);
		
		DispImg(ImgNum);
	}
}

function DispImg(ImgNum) 
{
	cache_images();
	var target = getElement("pcaption");
	
	//empty out existing children
	while (target.hasChildNodes())
	{
		target.removeChild(target.childNodes.item(0));
	}
	
	crossfade(document.getElementById('slideshow'), pix.imgs[ImgNum], '1', '', ImgNum);
	jQuery('#pcaption').html(NewCap[ImgNum]);
	jQuery('.fotograaf_naam').html(NewFotograaf[ImgNum]);
}

function getElement(id)
{
	if (document.getElementById) //DOM; IE5, NS6, Mozilla, Opera
	{
		return document.getElementById(id);
	}
	else if (document.all) //Proprietary DOM; IE4
	{
		return document.all[id];
	}
	else if (document.layers) // Filthy Netscape
	{
		return document.layers[id];
	}
	else if (document[id]) //Netscape alternative
	{
		return document[id];
	}
	else
	{
		return false;
	}
}

function AutoPlay() 
{
	if (lock == true) 
	{
		lock = false;
		jQuery('.control_2').removeClass('control_2_active');
		window.clearInterval(run);
	}
	else if (lock == false) 
	{
		lock = true;
		
		var targetOffset = jQuery('#controls').offset().top;
		jQuery('html, body').animate({scrollTop: targetOffset}, 400);
		jQuery('.control_2').addClass('control_2_active');
		NextPrevious(1);
 		run = setInterval("NextPrevious(1)", delay);
  	}
}


function crossfade()
{
	//if the timer is not already going
	if(pix.clock == null)
	{
		//copy the image object 
		pix.obj = arguments[0];
		
		//copy the image src argument 
		pix.src = arguments[1];
		
		//store the supported form of opacity
		if(typeof pix.obj.style.opacity != 'undefined')
		{
			pix.type = 'w3c';
		}
		else if(typeof pix.obj.style.MozOpacity != 'undefined')
		{
			pix.type = 'moz';
		}
		else if(typeof pix.obj.style.KhtmlOpacity != 'undefined')
		{
			pix.type = 'khtml';
		}
		else if(typeof pix.obj.filters == 'object')
		{
			//weed out win/ie5.0 by testing the length of the filters collection (where filters is an object with no data)
			//then weed out mac/ie5 by testing first the existence of the alpha object (to prevent errors in win/ie5.0)
			//then the returned value type, which should be a number, but in mac/ie5 is an empty string
			pix.type = (pix.obj.filters.length > 0 && typeof pix.obj.filters.alpha == 'object' && typeof pix.obj.filters.alpha.opacity == 'number') ? 'ie' : 'none';
		}
		else
		{
			pix.type = 'none';
		}
		
		//change the image alt text if defined
		if(typeof arguments[3] != 'undefined' && arguments[3] != '')
		{
			pix.obj.alt = arguments[3];
		}
		
		//if any kind of opacity is supported
		if(pix.type != 'none')
		{
			//create a new image object and append it to body
			//detecting support for namespaced element creation, in case we're in the XML DOM
			// pix.newimg = document.getElementsByTagName('address')[0].appendChild((typeof document.createElementNS != 'undefined') ? document.createElementNS('http://www.w3.org/1999/xhtml', 'img') : document.createElement('img'));
			pix.newimg = document.getElementById('fadegallery_fading').appendChild((typeof document.createElementNS != 'undefined') ? document.createElementNS('http://www.w3.org/1999/xhtml', 'img') : document.createElement('img'));

			//set positioning classname
			pix.newimg.className = 'idupe';
			
			//set src to new image src
			pix.newimg.src = pix.src;

			//move it to superimpose original image
			
			// pix.newimg.style.left = pix.getRealPosition(pix.obj, 'x') + 'px';
			// pix.newimg.style.top = pix.getRealPosition(pix.obj, 'y') + 'px';
			
			// pix.newimg.style.left = '1px';
			// pix.newimg.style.top = '1px';
			
			//copy and convert fade duration argument 
			// pix.length = parseInt(arguments[2], 10) * 1000;
			pix.length = parseInt(arguments[2], 10) * 1000;
			
			//create fade resolution argument as 20 steps per transition
			pix.resolution = parseInt(arguments[2], 10) * 20;
			
			//start the timer
			pix.clock = setInterval('pix.crossfade()', pix.length/pix.resolution);
		}
		
		//otherwise if opacity is not supported
		else
		{
			//just do the image swap (?)
			pix.obj.src = pix.src;
		}
	}
	else
	{
		// als al bezig is, verander naar nieuwe image url
		pix.obj = arguments[0];
		pix.src = arguments[1];
		pix.newimg.src = pix.src;
	}
	
	fadegallery_set_current (arguments[4]);
};


function fadegallery_set_current (idname)
{
	jQuery('.gallery').css('opacity', '');
	jQuery('#slide'+idname).css('opacity', '.2');
}

//crossfade timer function
pix.crossfade = function()
{
	//decrease the counter on a linear scale
	pix.count -= (1 / pix.resolution);
				
	//if the counter has reached the bottom
	if(pix.count < (1 / pix.resolution))
	{
		//set the original image to the src of the new image
		pix.obj.src = pix.src;
		
		//clear the timer
		clearInterval(pix.clock);
		pix.clock = null;
		
		//reset the counter
		pix.count = 1;
		
		document.getElementById('mainimage_link').href = pix.src;
	}
	
	//set new opacity value on both elements
	//using whatever method is supported
	switch(pix.type)
	{
		case 'ie' :
			pix.obj.filters.alpha.opacity = pix.count * 100;
			pix.newimg.filters.alpha.opacity = (1 - pix.count) * 100;
			break;
			
		case 'khtml' :
			pix.obj.style.KhtmlOpacity = pix.count;
			pix.newimg.style.KhtmlOpacity = (1 - pix.count);
			break;
			
		case 'moz' : 
			//restrict max opacity to prevent a visual popping effect in firefox
			pix.obj.style.MozOpacity = (pix.count == 1 ? 0.9999999 : pix.count);
			pix.newimg.style.MozOpacity = (1 - pix.count);
			alert ('moz');
			break;
			
		default : 
			//restrict max opacity to prevent a visual popping effect in firefox
			// pix.obj.style.opacity = (pix.count == 1 ? 0.9999999 : pix.count);
			pix.obj.style.opacity = 0.99999;
			// alert (pix.count);
			pix.newimg.style.opacity = (1 - pix.count);
	}
	
	//now that we've gone through one fade iteration 
	//we can show the image that's fading in
	pix.newimg.style.visibility = 'visible';

	//if the counter is at the top, which is just after the timer has finished
	if(pix.count == 1)
	{
		//remove the duplicate image
		pix.newimg.parentNode.removeChild(pix.newimg);
	}
};

//get real position method
pix.getRealPosition = function()
{
	this.pos = (arguments[1] == 'x') ? arguments[0].offsetLeft : arguments[0].offsetTop;
	this.tmp = arguments[0].offsetParent;
	while(this.tmp != null)
	{
		this.pos += (arguments[1] == 'x') ? this.tmp.offsetLeft : this.tmp.offsetTop;
		this.tmp = this.tmp.offsetParent;
	}
	
	/* return this.pos +2; */
	return this.pos +1;
};

