// JavaScript Document

function GetProjectInfo (id_project) {
	projectname = document.getElementById('ProjectName');
	projectname.style.visibility = "visible";
	
	document.getElementById('IdProjectSelected').value = id_project;	
	
	//----------------------------------------------------//
	url_image = 'p' + id_project + '-0.jpg';
	if (location.host == 'localhost') {
		url_image = 'http://localhost/croftpelletier/images/projects/normal_size/' + url_image;
	} else {
		url_image = 'http://' + location.host + '/images/projects/normal_size/' + url_image;
	}

	// pre-load the image:
	document.getElementById('PhotoTest').src = url_image;

	//----------------------------------------------------//	
	ixf.imgs = [url_image];
	//----------------------------------------------------//		
	proj_id = 'proj' + id_project;

	proj_name = document.getElementById(proj_id).name;
	document.getElementById('ProjectName').title = '';
	if (proj_name.length > 29) {
		document.getElementById('ProjectName').title = proj_name;
		proj_name = proj_name.substr(0,29) + '...';
	}
	document.getElementById('ProjectName').innerHTML = proj_name;
	//----------------------------------------------------//

	// activate presentation and fiche technique gifs:
	document.getElementById('presentation').style.display = "inline";
	document.getElementById('fiche_technique').style.display = "inline";
	
	crossfade(document.getElementById('ThePhoto'), url_image, '1', '')	
}

function OpenProjectSheet() {
	ProjectId = document.getElementById('IdProjectSelected').value;
	if (ProjectId == 0) {
		return;
	}
	
	if (location.host == 'localhost') {
		ProjectURL = 'http://localhost/croftpelletier/project_sheet.php?id=' + ProjectId;
	} else {
		ProjectURL = 'http://' + location.host + '/project_sheet.php?id=' + ProjectId;
	}
	window.location = ProjectURL;
}


function SelectTab(tab) {
	if (tab == 0) {
		if (has_css_class_name(document.getElementById('Tab1'),'TabSelected')) {
			remove_css_class_name(document.getElementById('Tab1'), 'TabSelected');
		}
		add_css_class_name(document.getElementById('Tab0'),'TabSelected');
		document.getElementById('ProjectDetails').style.display = 'block';
		document.getElementById('DataSheet').style.display = 'none';
	} else {
		if (has_css_class_name(document.getElementById('Tab0'),'TabSelected')) {
			remove_css_class_name(document.getElementById('Tab0'), 'TabSelected');
		}
		add_css_class_name(document.getElementById('Tab1'),'TabSelected');		
		document.getElementById('DataSheet').style.display = 'block';
		document.getElementById('ProjectDetails').style.display = 'none';		
	}
}

function PhotoSelected (element, index) {
		alert(element);
	}

function has_css_class_name(elem,cname) {
	return(elem&&cname)?new RegExp('\\b'+trim(cname)+'\\b').test(elem.className):false;
}

function remove_css_class_name(elem,cname) {
	if(elem&&cname&&elem.className) {
		cname=trim(cname);
		var old=elem.className;
		elem.className=elem.className.replace(new RegExp('\\b'+cname+'\\b'),'');
		return elem.className!=old;
	} else {
		return false;
	}
}

function add_css_class_name(elem,cname) {
	if(elem&&cname) {
		if(elem.className) {
			if(has_css_class_name(elem,cname)) {
				return false;
			} else {
				elem.className+=' '+trim(cname);
				return true;
			}
		} else {
			elem.className=cname;
			return true;
		}
	} else {
		return false;
	}
}

function trim(text) {
	if(typeof(text)=='undefined'||!text.toString) {
		return'';
	}
	return text.toString().replace(/^\s*|\s*$/g,'');
}

function thumbSelected (image_el, el, photo_name) {
	var mother = image_el.parentNode.parentNode;
	
	turn_off_all_minis(mother);
	// turn on selected mini:
	image_el.src = document.getElementById('MiniOn').src;

	// create a new div.
//	var contentObj = document.getElementById("MovingFrame");
//	var newdiv = document.createElement('div');	
//	newdiv.setAttribute('id', 'NewPhoto');
//	newdiv.style.width = contentObj.style.width;
//	newdiv.style.height = contentObj.style.height;
//	newdiv.style.marginLeft = contentObj.offsetWidth;
//	newdiv.style.display = 'block';
//
//	var obj = document.getElementById("ProjectPhotoHolder");
//	obj.appendChild(newdiv);
//	newdiv.style.src = el;	
	
//	current_photo = document.getElementById('ThePhoto').src;
//	SlidePhoto(mother, el);

	//define crosswipe (image-object, 'new src', 'seconds', 'direction'[, 'new alt text']) 
	
	crossfade(document.getElementById('ThePhoto'), el, '1', '')	
	
//	crosswipe(document.getElementById('ThePhoto'), el, '2', 'rl', '');

//	document.getElementById("MovingFrame").marginLeft = document.getElementById("ProjectPhotoHolder").offsetWidth;
////	document.getElementById('ThePhoto').src = el;	
//	SlidePhoto(1);
	
//	document.getElementById('ThePhoto').style.display = 'none';
//	document.getElementById('LoadingGifHolder').style.display = '';

//	document.getElementById('ThePhoto').src = el;
//	document.getElementById('ThePhoto').style.display = '';
//	document.getElementById('LoadingGifHolder').style.display = 'none';	

//	slideleft(document.getElementById('ThePhoto'));
//	SlidePhoto();
	
	
}

function turn_off_all_minis(parent) {
	var childImgs = parent.getElementsByTagName('img')
	var i, nownode;
	for (i=0; i < childImgs.length; i++) {
		nownode = childImgs[i];
		nownode.src = document.getElementById('MiniOff').src;
	}
	return '';
}


function SlidePhoto() {
	var obj = document.getElementById("ProjectPhotoHolder");
	var contentObj = document.getElementById("MovingFrame");
	var newObj = document.getElementById("NewPhoto");
	
	TargetX1 = -(contentObj.offsetWidth+536);

	x1 = parseInt(contentObj.style.marginLeft);
	x2 = parseInt(newObj.style.marginLeft);	

	if (x1 < TargetX1) {
		clearTimeout(t);
	} else {
		x1 = x1 - 50;
		x2 = x2 - 50;
		contentObj.style.marginLeft = x1 + 'px';
		newObj.style.marginLeft = x2 + 'px';
		t=setTimeout("SlidePhoto()",10);
	}

//	if (width == 0) {
//		width = obj.offsetHeight;
//	}
//	height = height + direction;
//	rerunFunction = true;
//	if(height>contentObj.offsetHeight){
//		height = contentObj.offsetHeight;
//		rerunFunction = false;
//	}
//	if(height<=1) {
//		height = 1;
//		rerunFunction = false;
//	}
//
//	obj.style.height = height + 'px';
//	var topPos = height - contentObj.offsetHeight;
//	if (topPos>0) topPos=0;
//	contentObj.style.top = topPos + 'px';
//	if(rerunFunction){
//		setTimeout('slideContent(' + inputId + ',' + direction + ')',dhtmlgoodies_timer);
//	} else {
//		if(height<=1) { 
//			obj.style.display='none'; 
//			if(objectIdToSlideDown && objectIdToSlideDown!=inputId) {
//				document.getElementById('dhtmlgoodies_a' + objectIdToSlideDown).style.display='block';
//				document.getElementById('dhtmlgoodies_a' + objectIdToSlideDown).style.visibility='visible';
//				slideContent(objectIdToSlideDown,dhtmlgoodies_slideSpeed);				
//			} else {
//				dhtmlgoodies_slideInProgress = false;
//			}
//		} else {
//			dhtmlgoodies_activeId = inputId;
//			dhtmlgoodies_slideInProgress = false;
//		}
//	}
}

