function preparePage() {
	define_all_hovers();
	preload_all_images();
	setup_portfolio_menu();
}

// ------------------------------------------------------------------------------------------

jQuery.fn.exists = function(){return jQuery(this).length>0;}

function setup_portfolio_menu() {
	var i=1;
		
	while ($('img#mi_'+ i).exists()) {
		
		$('a#mt_'+ i).mouseover(function() {
			$('img#mi_' + this.id.substr(3,1)).attr(
				'src',
					$('img#mi_' + this.id.substr(3,1)).attr('hoverSRC')
			);
			$('a#mt_' + this.id.substr(3,1)).css(
					'color','#000000'
				);
		});
		$('a#mt_' + i).mouseout(function() {
			$('img#mi_' + this.id.substr(3,1)).attr(
					'src',
						$('img#mi_' + this.id.substr(3,1)).attr('normalSRC')
				);
			$('a#mt_' + this.id.substr(3,1)).css(
					'color','#ffffff'
				);
		});
		$('img#mi_'+ i).mouseover(function() {
			$('a#mt_' + this.id.substr(3,1)).css(
				'color','#000000'
			);
		});
		$('img#mi_' + i).mouseout(function() {
			$('a#mt_' + this.id.substr(3,1)).css(
					'color','#ffffff'
				);
		});
		
		i++;
	}
}

//script to impose maxlength to a textarea in a form
function imposeMaxLength(Object, MaxLen) {
  return (Object.value.length < MaxLen);
}

function opacity(id, opacStart, opacEnd, millisec) { 
    //speed for each frame 
    var speed = Math.round(millisec / 100); 
    var timer = 0; 

    //determine the direction for the blending, if start and end are the same nothing happens 
    if(opacStart > opacEnd) { 
        for(i = opacStart; i >= opacEnd; i--) { 
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed)); 
            timer++; 
        } 
    } else if(opacStart < opacEnd) { 
        for(i = opacStart; i <= opacEnd; i++) 
            { 
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed)); 
            timer++; 
        } 
    } 
} 

//change the opacity for different browsers 
function changeOpac(opacity, id) { 
    var object = document.getElementById(id).style; 
    object.opacity = (opacity / 100); 
    object.MozOpacity = (opacity / 100); 
    object.KhtmlOpacity = (opacity / 100); 
    object.filter = "alpha(opacity=" + opacity + ")"; 
} 

function highlightthumb(argName, argState) {
	var elem = document.getElementById(argName);

	if (argState == "on") {
		elem.style.borderColor = "FFFFFF";
	} else {
		elem.style.borderColor = "E6A352";
	}
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_nbGroup(event, grpName) { //v6.0
  var i,img,nbArr,args=MM_nbGroup.arguments;
  if (event == "init" && args.length > 2) {
    if ((img = MM_findObj(args[2])) != null && !img.MM_init) {
      img.MM_init = true; img.MM_up = args[3]; img.MM_dn = img.src;
      if ((nbArr = document[grpName]) == null) nbArr = document[grpName] = new Array();
      nbArr[nbArr.length] = img;
      for (i=4; i < args.length-1; i+=2) if ((img = MM_findObj(args[i])) != null) {
        if (!img.MM_up) img.MM_up = img.src;
        img.src = img.MM_dn = args[i+1];
        nbArr[nbArr.length] = img;
		}
	}
  } else if (event == "over") {
    document.MM_nbOver = nbArr = new Array();
    for (i=1; i < args.length-1; i+=3) if ((img = MM_findObj(args[i])) != null) {
      if (!img.MM_up) img.MM_up = img.src;
      img.src = (img.MM_dn && args[i+2]) ? args[i+2] : ((args[i+1])? args[i+1] : img.MM_up);
      nbArr[nbArr.length] = img;
    }
  } else if (event == "out" ) {
    for (i=0; i < document.MM_nbOver.length; i++) {
      img = document.MM_nbOver[i]; img.src = (img.MM_dn) ? img.MM_dn : img.MM_up; }
  } else if (event == "down") {
    nbArr = document[grpName];
    if (nbArr)
      for (i=0; i < nbArr.length; i++) { img=nbArr[i]; img.src = img.MM_up; img.MM_dn = 0; }
    document[grpName] = nbArr = new Array();
    for (i=2; i < args.length-1; i+=2) if ((img = MM_findObj(args[i])) != null) {
      if (!img.MM_up) img.MM_up = img.src;
      img.src = img.MM_dn = (args[i+1])? args[i+1] : img.MM_up;
      nbArr[nbArr.length] = img;
	}
  }
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function portfolio_nav_toggle(elem, status) {
	MM_swapImage('navcat-' + elem,'','graphics-new/portfolio-nav/' + elem + '.' + status + '.gif',1);
	MM_swapImage('photo-' + elem,'','graphics-new/portfolio-photos/' + elem + '.' + status + '.jpg',1);
}







//========================================================================================================================
//========================================================================================================================

/*
* preloads the corresponding hover-images that end in _hv for all images that
* end in _nm
*/
function preload_all_images() {
	var preloaded = new Array(), img, imgList, mtchd;
	var regg = /(.*)(_nm\.)([^\.]{3,4})$/
	// var prel = new Array(), img, imgList, imgsrc, mtchd;
	imgList = document.body.getElementsByTagName('img');
	for ( var i = 0; img = imgList[i]; i++) {
		if (img.src.match(regg)) {
			mtchd = img.src.match(regg);
			img.hoverSRC = mtchd[1] + '_hv.' + mtchd[3];
			if (img.hoverSRC) {
				preload_image(img.hoverSRC);
			}
		}
	}

	function preload_image(imgSrc) {
		preloaded[preloaded.length] = new Image();
		preloaded[preloaded.length - 1].src = imgSrc;
	}
}

//========================================================================================================================
//========================================================================================================================


//========================================================================================================================
//========================================================================================================================

function define_all_hovers() {
	var regex = /(.*)(_nm\.)([^\.]{3,4})$/
	var preloaded_images = new Array(), img, imgList, inputList, parts;
	imgList = document.body.getElementsByTagName('img');
	inputList = document.body.getElementsByTagName('input');

	//image tag
 	for ( var i = 0; img = imgList[i]; i++) {
 		if (!img.hoverIsSetup && img.src.match(regex)) {
			parts = img.src.match(regex);

			// define sources
			img.normalSRC = img.src;
			img.hoverSRC = parts[1] + '_hv.' + parts[3];
			img.hoverIsSetup = true;

			// set initial state of this button
			button_off(img);
			
			// preload this button's hover image
			preload_image(img.hoverSRC);
		}
	}

 	//input tag
 	for ( var i = 0; img = inputList[i]; i++) {
 		if (!img.hoverIsSetup && img.src.match(regex)) {
			parts = img.src.match(regex);

			// define sources
			img.normalSRC = img.src;
			img.hoverSRC = parts[1] + '_hv.' + parts[3];
			img.hoverIsSetup = true;

			// set initial state of this button
			button_off(img);
			
			// preload this button's hover image
			preload_image(img.hoverSRC);
		}
	}
 	
	// preload an image
	function preload_image(imgSrc) {
		preloaded_images[preloaded_images.length] = new Image();
		preloaded_images[preloaded_images.length - 1].src = imgSrc;
	}
}


function menu_reset(menu) {
	var btn, img, menuitems;

	for ( var i = 0; btn = document.menus[menu][i]; i++) {
		img = document.getElementById(menu + '_' + btn);
		button_off(img);
	}
}


function button_off(img) {
	img.src = img.normalSRC;
	img.onmouseout = menu_item_normal;
	img.onmouseover = menu_item_hover;
}

function button_on(img) {
	img.src = img.hoverSRC;
	img.onmouseout = menu_item_hover;
	img.onmouseover = menu_item_hover;
}


function menu_item_normal() {
	this.src = this.normalSRC;
}
function menu_item_hover() {
	this.src = this.hoverSRC;
}

function menu_click(menu, btn) {
	menu_reset(menu);
	button_on(document.getElementById(menu + '_' + btn));

	var func = menu + '_' + btn + '_click';
	if (typeof (this[func]) == 'function') {
		this[func]();
	}

	return false;
}

//========================================================================================================================
//========================================================================================================================




