/* ==================

Author: David Patchin
Email: dpatchin@gmail.com


================== */
$(function (){
	console.log("Starting home_gallery.js");
	
	var temp = 0;
	
	//$('#home_gallery').replaceWith('<div id="#home_gallery">'+$('#home_gallery').html()+'</div');
	var gallery = $('#home_gallery');					// Caching gallery
	var content = $("#content");
	gallery.find("img").css("display", "none");			// Hiding <img> before gallery is sized to keep from stretching
	
	temp = content.width() - 30;						// Assigning gallery width to fill #content div
	var gallery_width = temp+"px";
	
	temp = Math.round(temp * 9/16);						// Assigning gallery height based off of width (16:9 ratio)
	var gallery_height = temp + "px";
	
	gallery.addClass("medium_bottom_corners medium_top_corners");	// Giving rounded corners
	
	var panels = gallery.children("div");				// Caching panels
	
			console.log(panels);
	panels.wrapAll('<div class="items" />');			// Wrapping the items with a <div>
	
	//console.log(gallery);
	
	gallery.css({
		"position"		: "relative",
		"background" 	: "black",
		"overflow"		: "hidden",
		"border"		: "1px solid #666",
		"width"			: gallery_width,
		"height"		: gallery_height
	});
	
	gallery.children("div").css({
		"width"		: "20000em",
		"position"	: "absolute"
	});
	
	//$('<div class="gallery_navigation"></div>').insertAfter(gallery);
	gallery.append('<div class="gallery_navigation"></div>');
	//$('<a class="next browse right">A</a>').insertAfter(gallery);
	//$('<a class="prev browse left">P</a>').insertAfter(gallery);
	
	gallery.hide(1, function () {			// initially hide Gallery
		
		panels.each(function () {			//setup the panels
			
			var image = $(this).find("img").get(0);//$(this).children("img").attr("src");
			var image_source = $(image).attr("src");
			$(image).remove();
			console.log(image_source);

			var html = '<div class="text">'+$(this).html()+'</div';
			$(this).html(html);
			
			
			//$(this).replaceWith('<div>'+html+'</div');	// Replacing <li>'s with <div>'s
	
			$(this).addClass("medium_bottom_corners medium_top_corners");	// Giving rounded corners
		
			$(this).css({
				"background" 	: "url("+image_source+") top center no-repeat",
				"border-right"	: "4px solid #000",
				"width"			: gallery_width,
				"height"		: gallery_height,
				"margin-bottom"	: "0",
				"float"			: "left"
			});
		
			var text_box = $(this).children(".text");
			
			text_box.addClass('small_box_shadow');
		
			text_box.children('h3').addClass('small_text_shadow_light');
			
			text_box.addClass("medium_top_corners");
		
			//console.log(this);
		
		});
		
	});
	
	console.log("Starting gallery...");
	
	gallery.fadeIn("slow", function () { startGallery(gallery); } );
	
	//Other stuuf 
	$("#apple img[rel]").overlay({effect: 'apple'});
});

function startGallery (gallery) {
	
	gallery.scrollable({
		circular	: true, 
		mousewheel	: false, 
		easing		: 'easeOutSine', 
		speed		: 1200,
		keyboard	: true
	}).autoscroll({ 
		autoplay	: true,
		interval	: 10000 
	}).navigator('.gallery_navigation');
}
	
