// BUILT ON TOP OF JQUERY /////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
/*************************************************************************************************/

$(document).ready(function() {
						   
	// move the images from slideshowContent div into slideshow
	// this is so that any extra markup/content stays within slideshowContent & doesn't affect the slideshow (all slideshow children are 'slides')
	$(".slideshowContent img").appendTo(".slideshow");
	
	// same approach for the gallery, with the extra <li> wrap around each <img> too
	$(".galleryContent img").appendTo("ul.gallery");
	$("ul.gallery img").wrap("<li></li>");
	
	///////////////////////////////////////////////////////////////////////////////////////////////
	
	// make the contact sheet image filenames visible (basically display the title attribute as text)
	
	$(".client .gallery-item a").each(function() {
		$(this).append($(this).attr("title"));
	})
	
	///////////////////////////////////////////////////////////////////////////////////////////////
			   
	//$(".footer ul li:first-child").addClass("first-child");
						   
	///////////////////////////////////////////////////////////////////////////////////////////////
	
	// setup the slideshow
	// fade up the first image too
	// and make it a random one
	// from http://malsup.com/jquery/cycle/fade-in-first2.html
	// see more at http://malsup.com/jquery/cycle/more.html?v2.23
	
	//$(".slideshow p").addClass("slideshow");
	
	$('.slideshow img').css({
        opacity: 0
    });
	
	setTimeout(function() {
        $('.slideshow').cycle({
            fx: 'fade', // choose your transition type, ex: fade, scrollUp, shuffle, etc...
			speed:  2000,
			timeout: 2000,
			random: 1, // set randomness to true
            delay:  -3000
        });

	    $('.slideshow img').css({
	        opacity: 0
	    });

    }, 0);


	
	///////////////////////////////////////////////////////////////////////////////////////////////
	
	// simple galleria gallery
	//jQuery(function($) { $('.gallery .maincol ul').galleria(); }); 
	
	// more advanced galleria gallery
	jQuery(function($) {
		
		//$('.gallery .subcol ul.gallery').addClass('gallery'); // adds new class name to maintain degradability
		$(".gallery .subcol ul.gallery li:first-child").addClass("active") // makes first image active
		
		
		$('ul.gallery').galleria({
			history   : true, // activates the history object for bookmarking, back-button etc.
			clickNext : true, // helper for making the image clickable
			//insert    : '#main_image', // the containing selector for our main image
			insert	  : 'undefined',
			onImage   : function(image,caption,thumb) { // let's add some image effects for demonstration purposes
				
				/***********************************************************************************************************/
				// put the gallery thumbnails on top of the main image
				/*$(".galleria_container").insertAfter("ul.galleria");
				
				// and then this little routine wraps a parent div round the galleria ul
				// and then works out the necessary margin-left in order to centre the ul within its parent
				$("ul.galleria").wrap("<div class='galleria_thumbs'></div>");
				$("ul.galleria").css('margin-left', ( (601-$("ul.galleria").width()) /2 ));*/
				/***********************************************************************************************************/
				
				// fade in the image & caption
				if(! ($.browser.mozilla && navigator.appVersion.indexOf("Win")!=-1) ) { // FF/Win fades large images terribly slow
					image.css('display','none').fadeIn(1000);
				}
				caption.css('display','none').fadeIn(1000);
				
				// fetch the thumbnail container
				var _li = thumb.parents('li');
				
				// fade out inactive thumbnail
				_li.siblings().children('img.selected').fadeTo(500,0.6);
				
				// fade in active thumbnail
				thumb.fadeTo('fast',1).addClass('selected');
				
				// add a title for the clickable image
				image.attr('title','Next image >>');
			},
			onThumb : function(thumb) { // thumbnail effects goes here
				
				// fetch the thumbnail container
				var _li = thumb.parents('li');
				
				// if thumbnail is active, fade all the way.
				var _fadeTo = _li.is('.active') ? '1' : '0.6';
				
				// fade in the thumbnail when finnished loading
				thumb.css({display:'none',opacity:_fadeTo}).fadeIn(1500);
				
				// hover effects
				thumb.hover(
					function() { thumb.fadeTo('fast',1); },
					function() { _li.not('.active').children('img').fadeTo('fast',0.6); } // don't fade out if the parent is active
				)
			}
		});
	});
	
	
	///////////////////////////////////////////////////////////////////////////////////////////////
	//http://www.dustindiaz.com/input-element-css-woes-are-over/
	
	function changeInputs()
	{
	var els = document.getElementsByTagName('input');
	var elsLen = els.length;
	var i = 0;
	for ( i=0;i<elsLen;i++ )
	{
	if ( els[i].getAttribute('type') )
	{
	if ( els[i].getAttribute('type') == "text" || els[i].getAttribute('type') == "password" )
	els[i].className = 'text';
	else
	els[i].className = 'button';
	}
	}
	}
	changeInputs();
	
});
