////////////////////////////////////////////////////////////////////////////////
// Gallery.js
//

	// -- Initialize ----------------------------------------------------------
	$(document).ready( function() {
	
		// Gallery?
		if( $("#gallery").length > 0 )
		{
			initializeGallery();
			setTimeout( doAutoLoad, 3000 );
		}
	});
	// ------------------------------------------------------------------------
	// -- Gallery -------------------------------------------------------------
	var imgCurrent = "";
	var ani_speed = 750;
	var autoLoad;
	function initializeGallery()
	{
		//
		/*var ctr = 0;
		var group_width = 0;
		$(".grouped").each( function() {
			
			group_width = 0;
			$(this).find(".cimg").each( function() {
				group_width += $(this).width()+100;
			});
			//alert( group_width );
			$(this).css( "width", group_width );
			
		});*/
		//$(".grouped").each( function() { $(this).css( "width", "auto" ); });
		
		// Gallery Images
		$("#gallery .cimg").each( function() {
		
			$(this).click( function() {
			
				// Scroll Container
				autoLoad = "";
				var curPos = $("#gallery_container").position().left;
				var imgElem = $(this);
				var imgPos = $(this).position().left;
				var imgWidth = $(this).width();
				var imgHeight = $(this).height();
			
				// Reset Previous Image
				if( imgCurrent != "" )
				{
					var wid = imgCurrent.width() / imgCurrent.height() * 100;
					var oparts = imgCurrent.attr( "rel" ).split( "-" );					
					
					imgCurrent.parent().find("div.desc").hide( "fast" );
					imgCurrent.parent().animate( { width: wid }, ani_speed, "swing" );
					imgCurrent.animate( { width: wid, height: 100, marginTop: 152 }, ani_speed/1.5, "swing", function() {
					
						var imgCurrentRel = imgCurrent.attr( "rel" )
						if( imgElem.attr( "rel" ) != imgCurrent.attr( "rel" ) )
						{
							$(this).attr( "src", $(this).attr( "src" ).replace( $(this).attr( "rel" ), "thumbs/"+ $(this).attr( "rel" ) ) );
							$("#ref_"+ oparts[0] ).attr( "src", $("#ref_"+ oparts[0] ).attr( "src" ).replace( imgCurrent.attr( "rel" ), "thumbs/"+ imgCurrent.attr( "rel" ) ) );
						}
							imgCurrent = "";
							
						if( imgElem.attr( "rel" ) != imgCurrentRel )
							imgElem.click();
					
					});
					/*$(".cimg").each( function() {
						
						if( $(this).attr( "rel" ) != imgCurrent.attr( "rel" ) )
							$(this).animate( { marginTop: 0 }, ani_speed );
						
					});*/
					//alert( "thumbs/"+ imgCurrent.attr( "rel" ) );
					//alert( new_css );
					
					//$("#iref_"+ oparts[0] ).parent().css( "backgroundImage", new_css );
					
				//	alert( $("#iref_"+ oparts[0] ).css( "backgroundImage" ) );
					
					$("#iref_"+ oparts[0] ).parent().animate( { height: 25}, ani_speed/1.5 );
					$("#iref_"+ oparts[0] ).animate( { width: wid, height: 25 }, ani_speed/1.5 );
					$("#ref_"+ oparts[0] ).animate( { width: wid, height: 100 }, ani_speed/1.5 );
					
					return;
				}
			
				
				
				var ctr = 0;
				var i = new Image();
					i.src = imgElem.attr( "src" ).replace( "thumbs/", "" );
				
				var moveTo = ($(this).position().left + $(this).parent().parent().position().left) - ((982/2) - (imgWidth/2));
				//alert( "img: "+ $(this).position().left +" group: "+ $(this).parent().parent().position().left +" moveto: "+ moveTo );
				$("#gallery_container").animate( { left: moveTo*-1 }, ani_speed, "swing", function() {
					
					// Reset Previous Image
					if( imgCurrent != "" )
					{
						//alert( imgCurrent.attr( "src" ).replace( imgCurrent.attr( "rel" ), "thumbs/"+ imgCurrent.attr( "rel" ) ) );
						//alert( "not first: "+ ctr );
						var wid = imgCurrent.width() / imgCurrent.height() * 100;
						var oparts = imgCurrent.attr( "rel" ).split( "-" );
						
						imgCurrent.parent().animate( { width: wid }, ani_speed, "swing" );
						imgCurrent.animate( { width: wid, height: 100, marginTop: 152 }, ani_speed, "swing", function() {
						
							$(this).attr( "src", $(this).attr( "src" ).replace( $(this).attr( "rel" ), "thumbs/"+ $(this).attr( "rel" ) ) );
							imgCurrent = "";
							imgElem.click();
						
						});
						$("#ref_"+ oparts[0] ).attr( "src", $("#ref_"+ oparts[0] ).attr( "src" ).replace( imgCurrent.attr( "rel" ), "thumbs/"+ imgCurrent.attr( "rel" ) ) );
						/*$(".cimg").each( function() {
							
							if( $(this).attr( "rel" ) != imgCurrent.attr( "rel" ) )
								$(this).animate( { marginTop: 0 }, ani_speed );
							
						});*/
						//alert( "thumbs/"+ imgCurrent.attr( "rel" ) );
						//alert( new_css );
						
						//$("#iref_"+ oparts[0] ).parent().css( "backgroundImage", new_css );
						
					//	alert( $("#iref_"+ oparts[0] ).css( "backgroundImage" ) );
						
						$("#iref_"+ oparts[0] ).parent().animate( { height: 25}, ani_speed );
						$("#iref_"+ oparts[0] ).animate( { width: wid, height: 25 }, ani_speed );
						$("#ref_"+ oparts[0] ).animate( { width: wid, height: 25 }, ani_speed );
						
						//$("#gallery_container img").animate( { marginTop: 0 }, ani_speed );
						/*$("#gallery_container").animate( { marginTop: 152 }, ani_speed, "swing", function() {
						
				
						});*/
						//imgCurrent.attr( "src", imgCurrent.attr( "src" ).replace( imgCurrent.attr( "rel" ), "thumbs/"+ imgCurrent.attr( "rel" ) ) );
						
						//imgCurrent = "";
						//$(this).click();
					}
					else
					{
					//	alert( "first"+ ctr );
						// Swap Image
						var h = i.height;
						var w = i.width;
						//alert( w );
						var parts = imgElem.attr( "rel" ).split( "-" );
						var adjustLeft = (w - imgElem.width()) / 2 + imgElem.parent().position().left;
						imgElem.css( "height", 100 );
						imgElem.attr( "src", i.src );
						// Increase Size
						// .. Image
						imgElem.parent().animate( { width: w }, ani_speed, "swing" );
						imgElem.animate( { height: h, width: w, marginTop: 0 }, ani_speed, "swing", function() {
							
							// Description
							//$(this).parent().find("div .desc").css( "visibility", "visible" );
							var desc_elem = $(this).parent().find("div.desc");
								desc_elem.hide();
								desc_elem.css( "visibility", "visible" );
								desc_elem.show( "slow" );
							
						});
						//alert($("#iref_"+ parts[0] ).parent().css( "backgroundImage" ) );
						var cur_css = $("#ref_"+ parts[0] ).attr( "src" );
						
						$("#iref_"+ parts[0] ).parent().animate( { height: 50}, ani_speed );
						$("#iref_"+ parts[0] ).animate( { width: w, height: 50 }, ani_speed );
						$("#ref_"+ parts[0] ).attr( "src", cur_css.replace( "thumbs/", "" ) );
						$("#ref_"+ parts[0] ).animate( { width: w, height: h }, ani_speed );
						// .. Container
						$("#gallery_container").animate( { left: $("#gallery_container").position().left - adjustLeft}, ani_speed);
						/*$(".cimg").each( function() {
							
							if( $(this).attr( "rel" ) != imgElem.attr( "rel" ) )
								$(this).animate( { marginTop: 152 }, ani_speed );
							
						});*/
						
						imgCurrent = imgElem;
					}
					ctr++;
				});
				
				// Resize Images
			
			});
		
		});
		
		// Gallery Groups
		var ctr = 0;
		var num = $(".gg_hook").length;
		var rnd = Math.round( Math.random() * num );
		$(".gg_hook").each( function() {
		
			$(this).click( function() {
				$("#group_"+ $(this).attr( "rel" ) +" img:first").click();
				autoLoad = "";
			});
			
			if( ctr == rnd )
				autoLoad = $(this);
			
			ctr++;
		});	
		
	}
	function doAutoLoad()
	{
		if( autoLoad != "" )
			autoLoad.click();
	}
	// ------------------------------------------------------------------------

//
//
////////////////////////////////////////////////////////////////////////////////