//document.ready Setup
$j(document).ready(
	function() {
		videoSetup();
		mediumImagesSetup();
		largeImagesSetup();
		videoImagesOverlapingSetup ()
		
		// when page loads simulate a "click" on the first image
		$j("#multimedia .items img[index='0']").click();
	}
);

function mediumImagesSetup(){
	$j("#multimedia .miniaturas").scrollable({
		onSeek:function(event,index){
			var selector= "#multimedia .items img[index='"+ index +"']";
			if ($j(selector).hasClass("active")) { return; }
			showMediumImage(selector);			
		}		
	});
	
	$j("#multimedia .items img").click(function() {
	
		// see if same thumb is being clicked
		if ($j(this).hasClass("active")) { return; }
		
		var api = $j("#multimedia .miniaturas").data("scrollable");	
		var indexToScroll = $j(this).attr("index");
		api.seekTo(parseInt(indexToScroll), 400);		

	});	
	
	$j("#multimedia img[rel]").overlay({
		closeOnClick: true,
		closeOnEsc: true,
		fixed: false,
		onClose: reLoadImageWrap,
		onLoad: loadThumbImage,
		top: "center",
		expose: { color: '#333', loadSpeed: 200, opacity: 0.9 }
	});
}

function showMediumImage(selector){
	
	// calculate large image's URL based on the thumbnail URL
	var url = $j(selector).attr("src").replace("/s/", "/m/");
	var alt = $j(selector).attr("alt");

	// get handle to element that wraps the image and make it semi-transparent
	var wrap = $j("#multimedia #image_wrap"); //.fadeTo("slow", 0)

	// the large image from www.flickr.com
	var img = new Image();

	// call this function after it's loaded
	img.onload = function() {

		// make wrapper fully visible

		// change the image
		wrap.find("img").attr("src", url);
		wrap.find("img").attr("alt", alt);
		$j('#multimedia #image_wrap h6').empty();
		$j('#multimedia #image_wrap h6').append(alt);
	};

	// begin loading the image
	img.src = url;

	//wrap.fadeTo("slow", 1);
	// activate item
	$j("#multimedia .items img").removeClass("active");
	$j(selector).addClass("active");

	// activate item
	$j("#multimedia .items img").parent().removeClass("active");
	$j(selector).parent().addClass("active");
}

//Images Overlay Gallery
function largeImagesSetup() {

	$j("#fotos .miniaturas").scrollable(
		{
			onSeek:function(event,index){				
				var selector= "#fotos .items img[index='"+ index +"']";
				if ($j(selector).hasClass("active")) { return; }
				showLargeImage(selector);			
			}
			
		}		
	);
	
	$j("#fotos .items img").click(function() {
		if ($j(this).hasClass("active")) { return; }		
		var api = $j("#fotos .miniaturas").data("scrollable");	
		var indexToScroll = $j(this).attr("index");
		api.seekTo(parseInt(indexToScroll), 400);
	
	});
}

function showLargeImage(selector){
	var url = $j(selector).attr("src").replace("/s/", "/l/");
	var alt = $j(selector).attr("alt");

	var wrap = $j("#fotos #image_wrap2");

	var img = new Image();

	img.onload = function() {

		wrap.find("img").attr("src", url);
		wrap.find("img").attr("alt", alt);
		$j('#fotos #image_wrap2 h6').empty();
		$j('#fotos #image_wrap2 h6').append(alt);
	};

	// begin loading the image
	img.src = url;

	$j("#fotos .items img").removeClass("active");
	$j(selector).addClass("active");

	$j("#fotos .items img").parent().removeClass("active");
	$j(selector).parent().addClass("active");	
}

//Images For Photo Tab Gallery - BuildingProject Only
function largeImagesTabSetup() {

	$j("#fotosTab .miniaturas").scrollable(
		{
			onSeek:function(event,index){				
				var selector= "#fotosTab .items img[index='"+ index +"']";
				if ($j(selector).hasClass("active")) { return; }
				showLargeImageTab(selector);			
			}
			
		}		
	);
	
	$j("#fotosTab .items img").click(function() {
		if ($j(this).hasClass("active")) { return; }		
		var api = $j("#fotosTab .miniaturas").data("scrollable");	
		var indexToScroll = $j(this).attr("index");
		api.seekTo(parseInt(indexToScroll), 400);
	
	});
}

function showLargeImageTab(selector){
	var url = $j(selector).attr("src").replace("/s/", "/l/");
	var alt = $j(selector).attr("alt");

	var wrap = $j("#fotosTab #image_wrap");

	var img = new Image();

	img.onload = function() {

		wrap.find("img").attr("src", url);
		wrap.find("img").attr("alt", alt);
		$j('#fotosTab #image_wrap h6').empty();
		$j('#fotosTab #image_wrap h6').append(alt);
	};

	// begin loading the image
	img.src = url;

	$j("#fotosTab .items img").removeClass("active");
	$j(selector).addClass("active");

	$j("#fotosTab .items img").parent().removeClass("active");
	$j(selector).parent().addClass("active");	
}


function reLoadImageWrap(){	
	var newUrlTumb = $j("#fotos #image_wrap2 img").attr("src");
	newUrlTumb = newUrlTumb.replace("/l/", "/s/");
	var api = $j("#multimedia .miniaturas").data("scrollable");	
	var indexToScroll = $j("#fotos img[src="+newUrlTumb+"]").attr("index");
	api.seekTo(parseInt(indexToScroll), 400);
}

function loadThumbImage(){
	var newUrlTumb = $j("#fotos #image_wrap2 img").attr("src");
	newUrlTumb = newUrlTumb.replace("/l/", "/s/");
	var api = $j("#fotos .miniaturas").data("scrollable");	
	var indexToScroll = $j("#fotos img[src="+newUrlTumb+"]").attr("index");
	api.seekTo(parseInt(indexToScroll), 400);
}

//Videos Gallery setup
function videoSetup() {
    $j("ul.yt_items").ytplaylist({
		holderId: 'video_wrap',
		playerWidth: 340,
		playerHeight: 255,
		addThumbs: true,
		showRelated: false
	});
}

function setPictureInGallery(image) {
	
		$j("#fotos #image_wrap2 img").attr("src", image.src.replace("/m/", "/l/"));
		$j("#fotos #image_wrap2 img").attr("alt", image.alt);
		$j("#fotos img").removeClass("active");
		$j("#fotos span").removeClass("active");
	
}

//Multimedia Fotos - Videos
function videoImagesOverlapingSetup () {
	$j('#image_wrap object').hide();	
	
	if(!$j('.media .videos').hasClass('disabled')){		
		$j('.media .videos').live('click', function() {	
			$j(this).addClass('current');
			$j('.media .fotos').removeClass('current');
			$j('#image_wrap, .miniaturas').hide();
			$j('.next, .prev').addClass('disabled');
			$j('.video_holder').show();	
		});
	}
	
	if(!$j('.media .fotos').hasClass('disabled')){	
		$j('.media .fotos').live('click', function() {		
			$j(this).addClass('current');
			$j('.media .videos').removeClass('current');
			$j('.video_holder').hide();
			$j('#image_wrap, .miniaturas').show();
			// when photo tab loads simulate a "click" on the first image
			$j("#multimedia .items img[index='0']").click();
			$j('.next').removeClass('disabled');
		});
	}
	
	
	
}
