// ----------------------------------------------------------
// FUCK YOU ALL
// ----------------------------------------------------------



//  used to switch the image for project detail
function swapImage(img_name,swap_img){
	if (document.images){
		 document.images[img_name].src = swap_img;
	}
}

//  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;
	}
}


//  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')){   //IE
            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;
}





// ----------------------------------------------------------
// 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

	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 right image
			show_hide('imgholder','hidden');
			// hide the content for better compatibility
			show_hide('content','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
			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('imgholder','hidden');
			// call function to adjust scroll
			adjust_scroll(obj,desire);
		}
	}
}



function put_img(img_name,img_text){
if (supported) {
var pos1=get_scroll_pos();
show_hide ('imgholder','visible');
document.getElementById("imgholder").style.top = pos1+ 200 ;
document.getElementById("imgholder").innerHTML = "<img src=" + img_name + " width=420  border=0> <br>" +img_text ;
scroll(0,pos1);
				}
}
//  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 + 450
		var x = new getObj('creditssite');
		x.style.top = bottom_loc + 'px';
}




