// JavaScript Document

/*
function load_data(id) {
	//MM_swapImage('imgloader','',image_data[id]['filename_full'],1);
	// TODO:
	// immediately remove first image so it doesn't hang around
	// start loading new image
	$("#current_url").html( image_data[id]['url'] );
	$("#current_name").html( image_data[id]['name'] );
	$("#current_description").html( image_data[id]['html'] );
	$("#current_index").html( image_data[id]['index'] );
	$("#imgloader").attr("src",image_data[id]['filename_full']);
	
	
	return false;
}
*/
// These will be set by the generated code in the body
max_index = 0;
current_index = 0;

function img_loading() {
	// kill the current displayed image
	$("#imgloader").attr("src",'/images/portfolio/loading.png');
	$("#imgloader").attr("alt",'Loading image, please wait.');	
}

$.fn.unwrap = function(expr) {
  return this.each(function(){
     $(this).parents(expr).eq(0).after(this).remove();
  });
};

function load_project(id,section) {
	if(section=="video") return true; // TODO: get this working with video too 
	
	$.post("/script/ajax.php",
		{ action:"load_project", proj_id: id, section_id: section }, function(xml) {
			
			// watch out for any "special" links (like using js to open new windows) which should
			// be wrapped in an .unwrapme block
			$(".unwrapme").unwrap("a");
			$(".unwrapme").remove();
			
			
			$("#current_description").html( $("html",xml).text() );
			
			// set up the current URL link
			if(!($("#current_url_link").length > 0)) { // if a link doesn't currently exist...
				if($("url_link",xml).text().length > 0) { // but there needs to be one created
					$("#current_url").wrap("<a id='current_url_link' class='ig_url' target='_blank' href='" + $("url_link",xml).text() + "'></a>");
				}
			}
			else { // if there's already a link tag...
				if($("url_link",xml).text().length > 0) { // and it should be updated
					$("#current_url_link").attr("href", $("url_link",xml).text() );
				}
				else { // otherwise, if it should be removed...
					//alert("removing url link");
					$("#current_url").unwrap("a");
				}
			}
			$("#current_url").html( $("url",xml).text() );
			
			

			// set up the current name link
			if(!($("#current_name_link").length > 0)) { // if a link doesn't currently exist...
				if($("name_link",xml).text().length > 0) { // but there needs to be one created
					$("#current_name").wrap("<a id='current_name_link' class='ig_client' target='_blank' href='" + $("name_link",xml).text() + "'></a>");
				}
			}
			else { // if there's already a link tag...
				if($("name_link",xml).text().length > 0) { // and it should be updated
					$("#current_name_link").attr("href", $("name_link",xml).text() );
				}
				else { // otherwise, if it should be removed...
					//alert("removing name link");
					$("#current_name").unwrap("a");
				}
			}
			$("#current_name").html( $("name",xml).text() );

			
			
			
			if(section=="video") {
				
				$("image",xml).each(function(id) {
					image = $(this);
					load_video( $("file",image).text() );
				 });
			}
			else {	
				// kill the current displayed image
				img_loading();
		
				image_data = [];
				var tmpcount = 0;
				$("image",xml).each(function(id) {
					tmpcount++;
					
					image = $(this);
					var tmp = [];
					tmp['file'] = $("file",image).text();
					tmp['desc'] = $("desc",image).text();
					tmp['url'] = $("url_link",image).text();
					id = $("id",image).text();
					
					image_data[ id ] = tmp;
				});
				
				current_index = 0;
				var display_current_index = current_index+1;
				if(display_current_index < 10) display_current_index = "0" + "" + display_current_index;
				max_index = tmpcount-1;
				var display_max_index = max_index+1;
				if(display_max_index < 10) display_max_index = "0" + "" + display_max_index;
				
				$(".ig_current").html( display_current_index );
				$(".ig_total").html( display_max_index );
				
				load_data(current_index); // load the first image from the new data
			}
		}
	); 

	return false;	
}

function load_video(file) {
	/*
	var tmp = "<script language='javascript'>\
		AC_FL_RunContent(\
			'codebase', 'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0',\
			'width', '623',\
			'height', '350',\
			'src', 'momentum_player',\
			'quality', 'high',\
			'pluginspage', 'http://www.macromedia.com/go/getflashplayer',\
			'align', 'middle',\
			'play', 'true',\
			'loop', 'true',\
			'scale', 'showall',\
			'wmode', 'window',\
			'devicefont', 'false',\
			'id', 'imgloader',\
			'bgcolor', '#ffffff',\
			'name', 'momentum_player',\
			'menu', 'true',\
			'allowFullScreen', 'false',\
			'allowScriptAccess','sameDomain',\
			'movie', '/new/video/player/momentum_player',\
			'salign', '',\
			'flashVars','file=" + file + "'\
			); //end AC code\
</script>";
*/
var tmp = "<object classid='clsid:d27cdb6e-ae6d-11cf-96b8-444553540000' codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0' width='623' height='350' id='imgloader' align='middle'> 	<param name='allowScriptAccess' value='sameDomain' /> 	<param name='allowFullScreen' value='false' /> 	<param name='flashVars' value='file=" + file + "' /> 	<param name='movie' value='momentum_player.swf' /><param name='quality' value='high' /><param name='bgcolor' value='#ffffff' /><embed src='momentum_player.swf' quality='high' bgcolor='#ffffff' width='623' height='350' name='momentum_player' align='middle' allowScriptAccess='sameDomain' allowFullScreen='false' type='application/x-shockwave-flash' pluginspage='http://www.macromedia.com/go/getflashplayer' flashVars='file=" + file + "' /></object>";
	//$("#main_image_content").html(tmp);	
	$("#main_image_content").html("");
	$("#main_image_content").html(tmp);
	return false;
}
function load_data(ind) {
	if(ind < 0 || ind > max_index) return false;
	
	// kill the current displayed image
	img_loading();
			
	$("#imgloader").attr("src",image_data[ind]['file']);
	//$("#imgloader_link").attr("href",image_data[ind]['url']);
	$("#imgloader").attr("alt",image_data[ind]['desc']);
	var display_ind = ind + 1;
	if(display_ind < 10) display_ind = "0" + "" + display_ind;
	//$("#current_index").html( display_ind );
	$(".ig_current").html( display_ind );
	current_index = ind;

	return false;
}
function load_next_data() {
	if(current_index < max_index) {
		current_index++;
		load_data(current_index);
	}
	return false;
}
function load_prev_data() {
	if(current_index > 0) {
		current_index--;
		load_data(current_index);
	}
	return false;
}

// add the styles into the scrolling gallery field that were left out for 
// compatibility with non-JS browsers
$(document).ready(function(){
      //  $('.stepcarousel').css('position','relative');
      //  $('.stepcarousel').css('overflow','hidden');
      //  $('.stepcarousel').css('height','107px');
      //  $('.stepcarousel .belt').css('position','absolute');
       // $('.stepcarousel .belt').css('left','0');
       // $('.stepcarousel .belt').css('top','0');

    });

	stepcarousel.setup({
		galleryid: 'mygallery', //id of carousel DIV
		beltclass: 'belt', //class of inner "belt" DIV containing all the panel DIVs
		panelclass: 'panel', //class of panel DIVs each holding content
		statusvars: ['statusA', 'statusB', 'statusC'], //register 3 variables that contain current panel (start), current panel (last), and total panels
		contenttype: ['inline'] //content setting ['inline'] or ['external', 'path_to_external_file']
	});

