/* 	---------------------------------------------------------
	BOUROULLEC GENERAL SCRIPT
	Alain Bellet
	www.notdefined.net

	FUNCTIONS
	---------------------------------------------------------
*/


// ----------------------------------------------------------
// General functions
// ----------------------------------------------------------

// ее Open window
function open_win(theURL,winName,features) {
  window.open(theURL,winName,features);
}


// ее used so switch the image for project detail
function swapImage(img_name,swap_img){
	if (document.images){
		 document.images[img_name].src = swap_img;
	}
}

// ее solve IE MAC missing array.push ...
if (typeof((new Array()).push) == "undefined") {
 function push() {
     for (var i = 0 ; i < arguments.length ; i++) {
         this[this.length] = arguments[i];
        }
    } Array.prototype.push = push;
}

// ее make a "find position" in an array
function get_pos(id,the_array){
	for (i = 0; i < the_array.length; i++){
		the_id = (the_array[i]);
		if (the_id == id){
			return i;
		}
	}
	return -1;
}

// ее show-hide a div
function show_hide(the_div,vis){
	if (document.getElementById){
		document.getElementById(the_div).style.visibility = vis;
	}
	else if (document.all)
	{
		document.all[the_div].style.visibility = vis;
	}
}

// ее set the bg color
function changeBG(whichColor){
	document.bgColor = whichColor;
}

// ее emulate an object
// used like toto = new getObj('div_toto');
function getObj(name)
{
  if (document.getElementById)
  {
        this.obj = document.getElementById(name);
        this.style = document.getElementById(name).style;
  }
  else if (document.all)
  {
        this.obj = document.all[name];
        this.style = document.all[name].style;
  }
  else if (document.layers)
  {
        this.obj = document.layers[name];
        this.style = document.layers[name];
  }
}


// ее find the x position of an object
function findPosX(obj)
{
	var curleft = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
	return curleft;
}

// ее find the y position of an object
function findPosY(obj)
{
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}


// ее get the vertical position of the scroll in pixels
function get_scroll_pos(){
	if(typeof window.pageXOffset != 'undefined'){
        X = window.pageXOffset;
        Y = window.pageYOffset;
    }else{
        if((!window.document.compatMode)||
          (window.document.compatMode == 'BackCompat')){
            X = window.document.body.scrollLeft;
            Y = window.document.body.scrollTop;
        }else{
            X = window.document.documentElement.scrollLeft;
            Y = window.document.documentElement.scrollTop;
        }
    }
	return (Y);
}

// ее get the height of the window
function get_window_height() {
  var myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myHeight = window.innerHeight;
  } else if( document.documentElement &&( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myHeight = document.body.clientHeight;
  }
  return myHeight;
}

// ее reload the page
function refresh_page(){
	document.location = "#";
}

// ----------------------------------------------------------
// remote frame related scripts
// ----------------------------------------------------------

// define some variables
var IFrameObj = "";

// ее functions to create the frame object and to load files into
function callToServer(remote_url) {
	  if (!document.createElement) {return true};
	  var IFrameDoc;
	  var URL = remote_url;

	  if (!IFrameObj && document.createElement) {
		// create the IFrame and assign a reference to the
		// object to our global variable IFrameObj.
		// this will only happen the first time
		// callToServer() is called
	   try {
		  var tempIFrame=document.createElement('iframe');
		  tempIFrame.setAttribute('id','RSIFrame');
		  tempIFrame.style.border='0px';
		  tempIFrame.style.width='0px';
		  tempIFrame.style.height='0px';
		  IFrameObj = document.body.appendChild(tempIFrame);

		  if (document.frames) {
			// this is for IE5 Mac, because it will only
			// allow access to the document object
			// of the IFrame if we access it through
			// the document.frames array
			IFrameObj = document.frames['RSIFrame'];
		  }
		} catch(exception) {
		  // This is for IE5 PC, which does not allow dynamic creation
		  // and manipulation of an iframe object. Instead, we'll fake
		  // it up by creating our own objects.
		  iframeHTML='\<iframe id="RSIFrame" style="';
		  iframeHTML+='border:0px;';
		  iframeHTML+='width:0px;';
		  iframeHTML+='height:0px;';
		  iframeHTML+='"><\/iframe>';
		  document.body.innerHTML+=iframeHTML;
		  IFrameObj = new Object();
		  IFrameObj.document = new Object();
		  IFrameObj.document.location = new Object();
		  IFrameObj.document.location.iframe = document.getElementById('RSIFrame');
		  IFrameObj.document.location.replace = function(location) {
			this.iframe.src = location;
		  }
		}
	  }

	if (navigator.userAgent.indexOf('Gecko') !=-1 && !IFrameObj.contentDocument) {
		// we have to give NS6 a fraction of a second
		// to recognize the new IFrame
		setTimeout('callToServer()',10);
		return false;
	 }

	if (IFrameObj.contentDocument) {
		// For NS6
		IFrameDoc = IFrameObj.contentDocument;
	} else if (IFrameObj.contentWindow) {
		// For IE5.5 and IE6
		IFrameDoc = IFrameObj.contentWindow.document;
	} else if (IFrameObj.document) {
		// For IE5
		IFrameDoc = IFrameObj.document;
	} else {
		return true;
	}

  IFrameDoc.location.replace(URL);
  return false;
}






// ----------------------------------------------------------
// collapsable menu code
// ----------------------------------------------------------


var supported = (document.getElementById || document.all);
var open_menu = new Array(); // to save all open Menus

// ее close all menu  // BUGY !!!!!!!
function close_all(){
	for (var i = 0 ; i < open_menu.length ; i++) {
		if (open_menu[i] == "undefined"){
		alert (open_menu[i]);
			swap_menu(open_menu[i]);
		}
	}
	// empty the array
	open_menu = new Array();
}

// ее master open-close function
// id : id of the obj.
// obj : object
// desire : the size of the open object (minimum) - used to force scroll

function swap_menu(id,obj,desire,img){
// show the to and reload tool
show_hide('stick','visible');
	if (supported) {
		// check if already in the array
		var get_position = get_pos(id,open_menu);

		if (get_position != -1){
			// CLOSE THE MENU //
			// hide the contextual infos
			show_hide('context_infos','hidden');
			// hide the content for better compatibility
			show_hide ('content','hidden');
			// void the display text
			document.getElementById("context_infos_text").innerHTML = "&nbsp;";
			// void the display image
			document.getElementById("context_infos_image").innerHTML = "&nbsp;";
			// remove from the open array
			delete open_menu[get_position];
			set_display(id,"none");
			if(img==true) swapImage('img_'+id,'img/f_close.gif');
			// refresh content
			force_refresh();
			show_hide ('content','visible');
		}else{
			// OPEN THE MENU //
			show_hide ('content','hidden');
			// add the id in the open array
			open_menu.push(id);
			set_display(id,"block");
			if(img==true) swapImage('img_'+id,'img/f_open.gif');
			// refresh content
 			force_refresh();
			show_hide ('content','visible');
			show_hide('context_infos','hidden');
			// call function to adjust scroll
			adjust_scroll(obj,desire);
		}
	}
}

function swap_menu_context(id,obj,desire,img){
	if (supported) {
		// check if already in the array
		var get_position = get_pos(id,open_menu);

		if (get_position != -1){
			// CLOSE THE MENU
			// hide the content for better compatibility
			show_hide ('context_infos','hidden');
			// remove from the open array
			delete open_menu[get_position];
			set_display(id,"none");
			if(img==true) swapImage('img_'+id,'img/f_close.gif');
			// refresh content
			show_hide ('context_infos','visible');
		}else{
			// OPEN THE MENU //
			show_hide ('context_infos','hidden');
			// add the id in the open array
			open_menu.push(id);
			set_display(id,"block");
			if(img==true) swapImage('img_'+id,'img/f_open.gif');
			show_hide ('context_infos','visible');
			adjust_scroll(obj,desire);
		}
	}
}

// ее try to scroll to display the content of the open menu
function adjust_scroll(clicked_obj,desired_scroll){
		click_pos =  findPosY(clicked_obj);
		scroll_y = get_scroll_pos();
		relative_position = click_pos-scroll_y;
		window_height = get_window_height();

		if (relative_position + desired_scroll > window_height) {
			to_move = (relative_position + desired_scroll) - window_height;
			if (to_move > relative_position) {
				to_move = relative_position -50 ;
			}
			window.scroll(0, scroll_y+to_move);
		}
}


// ее set the style (display) to none or block - to collapse it
function set_display(div_id,disp){
	if (document.getElementById){
		document.getElementById(div_id).style.display = disp;
	}
	else if (document.all)
	{
		document.all[div_id].style.display = disp;
	}

}

// ее move the credits to the bottom of the page to force refresh
function force_refresh(){
		if (document.getElementById){
			 obj = document.getElementById('spyloc');
		}
		else if (document.all)
		{
			obj = document.all['spyloc'];
		}
		bottom_loc = findPosY(obj);
		bottom_loc = bottom_loc + 500
		var x = new getObj('creditssite');
		x.style.top = bottom_loc + 'px';
}


// ----------------------------------------------------------
// auto scroll sticky layer
// based on http://www.quirksmode.org
// ----------------------------------------------------------

var menu;
var comp;
var theTop = 470;
var old = theTop;

// ее called on load to init the auto-scroll
function init_sticky_menu()
{
	menu = new getObj('stick'); // layer to move
	comp = new getObj('comp'); // fix layer to compare position
	movemenu();
}

// ее function called to scroll (with a 500 ms delay)
function movemenu()
{
	if (window.innerHeight){
		  pos = window.pageYOffset
	}
	else if (document.documentElement && document.documentElement.scrollTop)
	{
		pos = document.documentElement.scrollTop
	}
	else if (document.body)
	{
		  pos = document.body.scrollTop
	}

	if (pos < theTop) pos = theTop;
	else pos += 200;
	if (pos == old)
	{
		menu.style.top = pos;
	}
	old = pos;
	temp = setTimeout('movemenu()',200);
}




// ----------------------------------------------------------
// contextual infos and hiddden Load
// ----------------------------------------------------------

// ее position an object based an other
function set_context_infos(name,page){
	if (name != "just_display"){
		if (document.getElementById){
		   obj = document.getElementById(name);
		}
			 else if (document.all)
		{
		   obj = document.all[name];
		}
		var newY = findPosY(obj);

	}else{
	var newY = 300;
 	}
	var newX = 530  ; //findPosX(obj);

	// correction for IE MAC
	if ( (navigator.appName.indexOf("Explorer")!=-1) && (navigator.userAgent.indexOf("Mac")!=-1)) {
		newY += 13;
	}

	// check if top is visible, if not set it to top of window
	scroll_y = get_scroll_pos();
	relative_position = newY-scroll_y;
	if (relative_position<0) newY = scroll_y +3;


	var x = new getObj('context_infos');
	x.style.top = newY + 'px';
	x.style.left = newX + 'px';
	show_hide('context_infos','visible');

	// load in the remote frame
	document.getElementById("context_infos_text").innerHTML = "&nbsp;";
	document.getElementById("context_infos_image").innerHTML = "loading ...";
	document.getElementById("copyright_infos").innerHTML = "";
	document.getElementById("context_infos_text_long").innerHTML = "";
	callToServer(page);
}


// ее function called from the remote frame
// when the description is loaded
function display_description(theHtml){
		document.getElementById("context_infos_text").innerHTML = theHtml;
}

// when the long text is loaded
function display_description_long(theHtml){
		document.getElementById("context_infos_text_long").innerHTML = theHtml;
}
// ее function called from the remote frame
// when the image is loaded
var all_images_infos;

function display_image(theHtml,all_images_info){
		 all_images_infos = all_images_info;
		document.getElementById("context_infos_image").innerHTML = theHtml;
		// set the infos for the first image
		swap_object_image(0);
}

// ее function display the image and copyright infos
var img_zoom = false;

function swap_object_image(id){
	file = all_images_infos[id].file;
	img_large = all_images_infos[id].img_large;
	legende = all_images_infos[id].legende;
	copyright = all_images_infos[id].copyright;
	if (document.images){
		document['im'].src = "upload/medias_site/"+file;
	}
	if (copyright != "") {
		document.getElementById("copyright_infos").innerHTML = "&nbsp;&copy; "+copyright;
	}else{
		document.getElementById("copyright_infos").innerHTML = "";
	}

	if (img_large != "3") {
		document.getElementById("zoom").innerHTML = "<a href=\"#\" onClick=\"open_win('image_zoom.php?img="+img_large+"','img_zoom','width=200,height=200,scrollbars=no');return false;\" onFocus=\"if(this.blur)this.blur()\">zoom</a>";
		img_zoom = img_large;
	}else{
		img_zoom = false;
		document.getElementById("zoom").innerHTML = "&nbsp;";
	}
}

function view_zoom(){
	if (img_zoom){
		open_win('image_zoom.php?img='+img_zoom,'win_img_zoom','width=200,height=200,scrollbars=no');
	}else{
		document.getElementById("zoom").innerHTML = "no zoom for this picture"
	}
}


function update_obj_image(){
	setTimeout('adjust_ti_view_image()',100);
}

function adjust_ti_view_image(){
	// check if bottom is visible, if not scroll
	if (document.getElementById){
	   obj = document.getElementById('copyright_infos');
 	}
		 else if (document.all)
	{
	   obj = document.all['copyright_infos'];
 	}
	adjust_scroll(obj,60);
}


function display_pressfiles_list(theHtml){
		document.getElementById("liste_press_files").innerHTML = theHtml;
		// set the infos for the first image
}

function get_pressfiles_list(){
	password = document.press_form.the_password.value;
	//document.press_form.the_password.value = "";
	actual_content = document.getElementById("liste_press_files").innerHTML;
	document.getElementById("liste_press_files").innerHTML = "loading...<br>" + actual_content;
	callToServer('get_pressfiles_list.php?password='+password);
}

// SEARCH
function change_page(page,total)
{
	document.Frecherche.page.value = page;
	document.Frecherche.total.value = total;
	document.Frecherche.submit();
}

function display_project_search_result(theHtml){
		document.getElementById("liste_proj_result").innerHTML = theHtml;
}

function get_project_search_result(page,total){
	keyword = document.search_project_form.keyword.value;
	document.search_editor_form.liste_editeurs.selectedIndex = 0;
	if (page){
		callToServer('search_projets.php?keyword='+keyword+'&page='+page+'&total='+total);
	}else{
		callToServer('search_projets.php?keyword='+keyword);
		display_project_search_result('searching, please wait...');
	}
}

function get_editor_search_result(page,total){
	sel_index = document.search_editor_form.liste_editeurs.selectedIndex;
	if (sel_index != 0){
		editor = document.search_editor_form.liste_editeurs[sel_index].text;
		editor_id = document.search_editor_form.liste_editeurs[sel_index].value;
		if (page){
			callToServer('search_editor.php?id_editor='+editor_id+'&name_editor='+editor+'&page='+page+'&total='+total);
		}else{
			callToServer('search_editor.php?id_editor='+editor_id+'&name_editor='+editor);
			display_project_search_result('searching, please wait...');
		}
	}
}

// ----------------------------------------------------------
// MAILING LIST
// ----------------------------------------------------------


function envoi_form_subscribe()
{
  if (check_form_suscribe()) {
  document.fsuscribe.submit();
  document.fsuscribe.email.value = "";
  }
}


function check_form_suscribe()
{
		if (document.fsuscribe.email.value.indexOf("@") == -1 || document.fsuscribe.email.value.indexOf(".") == -1){
	  		 alert('You email adresse is not valid !');
			 document.fsuscribe.email.focus();
	  		 return false;
		}

	// si tout est ok
	return true;

}

//document.write ("<link href=\"write_css.css\" rel=\"stylesheet\" type=\"text/css\">");
//
//function set_bg_color(color){
//		document.bgColor = '#ffffff';
//}
//
//function init_onload(){
//set_bg_color('#ffffff');
//init_sticky_menu();
// if a project is forced to display (using id)
//
//}
