/**
 * Gallery with sliding thumbs
 *
 * http://www.whiteoctober.co.uk
 * Dave Fletcher
 *
 * Licensed under the MIT (MIT-LICENSE.txt)
 *
 *
 */
 
 /*
 
 Not working on Safari 3
 
	- Talk to Pete about how important it is to support it
 	- Try giving the image a source on line 73
 	- Try accessing elements using $() selectors rather than the .gallery element etc
 	- ... anything else? ...
 	- Try taking the whole thing out of the jquery plugin
 
 */

jQuery.iFadingGallery = {
	
	// properties
	imageContainer: null,
	thumbArray: null,
	thumb: null,
	imagePreloader: null,

	/**
	 * selects a thumbnail
	 */	
	selectThumb: function() {
		
		$(this.mainpiccontainer).empty();
		if (this.rel.search('flash') >= 0)
		{
			var vals = this.rel.split('|');
			
			this.gallery.mainpiccontainer.append('<div id="galleryflashcont" style="width: '+vals[2]+'px; height: '+vals[1]+'px"></div>');
			var so = new SWFObject(this.href, "galleryflashcont", vals[2], vals[1], "7");
			so.write("galleryflashcont");
			jQuery.iFadingGallery.centreFlash();
		}
		else if (this.rel.search('movie') >= 0)
		{
			var vals = this.rel.split('|');
			
			this.gallery.mainpiccontainer.append('<div id="galleryflashcont" style="width: '+vals[2]+'px; height: '+vals[1]+'px"></div>');
			var so = new SWFObject(rootpath+'swf/player_flv_maxi.swf?r='+new Date().getTime(), "galleryflashcont", vals[2], vals[1], "7");
			so.addVariable("flv", this.href);
			so.addVariable("showiconplay", "1");
			so.addVariable("showvolume", "1");
			so.addVariable("autoload", "1");
			so.addVariable("showplayer", "always");
			so.addVariable("bgcolor1", "ffffff"); //b8d30b
			so.addVariable("bgcolor2", "ffffff");
			so.addVariable("margin", "0");
			so.addVariable("videobgcolor", "ffffff");
			so.addVariable("iconplaybgcolor", "e90c06");
			so.addVariable("playercolor", "e90c06");
			so.addVariable("buttoncolor", "e0e1e1");
			so.addVariable("buttonovercolor", "ffffff");
			so.addVariable("loadingcolor", "e0e1e1");
			so.addVariable("sliderovercolor", "ffffff");
			so.addVariable("slidercolor2", "e0e1e1");
			so.addVariable("slidercolor1", "e0e1e1");
			so.addVariable("playeralpha", "100");
			so.write("galleryflashcont");
			jQuery.iFadingGallery.centreFlash();
		}
		else
		{
			//alert('its an image');
			galleryimg = $('<img style="opacity: 0;" src=""/>').appendTo(this.gallery.mainpiccontainer);
			galleryimg.css('opacity', 0);
			//alert('its been appended');
			galleryimg.bind('load', jQuery.iFadingGallery.fadeMeIn);
			//alert('the load event has been bound');
			galleryimg.get(0).gallery = this.gallery;
			//alert('the gallery has been set');
			//alert(this.href);
			//alert('about to set the src on the image');
			$(galleryimg).attr('src', this.href);

		}
		this.gallery.currentThumb = this;
		
		this.gallery.titleel.empty().append($(this).find('img').attr('alt'));

		return false;
	},

	/**
  * Performs all the actions once we've preloaded the image
  */
	fadeMeIn: function(event) {

		jQuery.iFadingGallery.centreImage(this.gallery, this.height, this.width);
		$(this).fadeTo("normal", 1);
		
	},	

	/**
  * Resizes the image container
  */
	centreImage: function(gallery, heightNew, widthNew) {

		gallery.mainpiccontainer.find('img')
			.css('marginLeft', Math.floor((630-widthNew)/2))
			.css('marginTop', Math.floor((380-heightNew)/2));
		
	},

	/**
  * Centers the flash if smaller than the whole thing
  */
	centreFlash: function() {

		$('#galleryflashcont').each(function()
		{
			objectHeight = $(this).height();
			objectWidth = $(this).width();
			if (objectHeight < 380) $(this).css('top', Math.round((380-objectHeight)/2));
			if (objectWidth < 630) $(this).css('left', Math.round((630-objectWidth)/2));

		});
		
	},	
	/**
	 * Builds the gallery
	 */
	innit: function(options) {
		
		return this.each(
			function() {
				
				el = this;

				// bind the click event to the selectThumb function on each thumbnail
				$(this).find('.workthumbs a').bind('click', jQuery.iFadingGallery.selectThumb).each( function()
				{
					this.gallery = el;
					this.mainpiccontainer = $(el).find('.work-container');
				
				});
				this.titleel = $(this).find('h1');
				this.thumbs = $(this).find('.workthumbs a');
				this.mainpiccontainer = $(this).find('.work-container');

				var strcurrentsrc = this.mainpiccontainer.find('img').attr('src');

				if (this.thumbs.get(0).rel == 'img')
				{
					$(this.mainpiccontainer).empty();
					galleryimg = $('<img style="opacity: 0;" src=""/>')
						.css('opacity', 0)
						.appendTo(this.mainpiccontainer)
						.bind('load', jQuery.iFadingGallery.fadeMeIn).get(0);
					galleryimg.gallery = this;
					$(galleryimg).attr('src', strcurrentsrc);
					//this.imagePreloader.src = strcurrentsrc;
				}
					
				//if (this.mainImage.height() > 0) this.mainpiccontainer.height(this.mainImage.height());
				
			}
		);
	}

};

jQuery.fn.extend ({
	
	FadingGallery: jQuery.iFadingGallery.innit
	
});



 function dump(arr,level) {
 	var dumped_text = "";
 	if(!level) level = 0;

 	//The padding given at the beginning of the line.
 	var level_padding = "";
 	for(var j=0;j<level+1;j++) level_padding += "    ";
 
 	if(typeof(arr) == 'object') { //Array/Hashes/Objects 
 		for(var item in arr) {
 			var value = arr[item];
 
 			if(typeof(value) == 'object') { //If it is an array,
 				dumped_text += level_padding + "'" + item + "' ...\n";
 				dumped_text += dump(value,level+1);
 			} else {
 				dumped_text += level_padding + "'" + item + "' => \"" + value + "\"\n";
 			}
 		}
 	} else { //Stings/Chars/Numbers etc.
 		dumped_text = "===>"+arr+"<===("+typeof(arr)+")";
 	}
 	return dumped_text;
 }