/*
================================================================================
======= RESIZE ALLA IMAGE WIDTH JAVASCRIPT
================================================================================
======= v. 2.0.1

 Author: Mago28 
        http://www.adolfo.trinca.name 
        mago28@email.it
*/

function resizeimages(){
  var max_width = 450;
  var close_image_text = "Click here to close";
  var open_image_text  = "Click here to open";
  var photo_title      = "Full Size Image";
  
  if (document.images) {
    for (i = 0; i < document.images.length; i++) {
       
		while ( !document.images[i].complete ){
             break;
		}
		if ( document.images[i].width > max_width ){
			var imagetoprocess = document.images[i];
			var original_width = imagetoprocess.width;
			var original_height = imagetoprocess.height;
	    
	    // resize image
			imagetoprocess.width = max_width;
	    imagetoprocess.height = Math.round(original_height * max_width / original_width); 
			
			// add an id
			if (imagetoprocess.id=="") {
			    imagetoprocess.id = "img"+i;
			}
			
			// foto title
			photo_title = (imagetoprocess.alt) ?imagetoprocess.alt: photo_title;
			
			// add onclick event
			imagetoprocess.onclick = function () {
				 newWindow(this, photo_title, original_width, original_height, close_image_text, "center");
			}
			
			// add cursor ( to add custom cursor use string: "cursor:url('zoomin.cur'),crosshair;")
			imagetoprocess.style.cssText = "cursor:pointer;" + imagetoprocess.style.cssText;
			
			// add image title
			imagetoprocess.title = open_image_text;
     }        
    }
  }
}

function newWindow(photo, winTitle, original_width, original_height, txtClose, center) {
	
	// browser detection
  var agt       = navigator.userAgent.toLowerCase();
	var is_ie     = ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1));
	
	var winOpts = "";
	if (center){
		var winLeft   =  (screen.width-original_width)/2;
		var winTop    =  (screen.height-original_height)/2;
		winOpts = "width="+original_width+",height="+original_height+",left="+winLeft+",top="+winTop
	}
	if ( original_width >= screen.width ){
       winOpts = winOpts + ",fullscreen,scrollbars=yes"
	}
		
	if(is_ie){
	     fotoWindow = window.open("","z","'"+winOpts+"'");
	}else{
	     fotoWindow = window.open("",winTitle,winOpts);
	}
	
	fotoWindow.document.open();
	fotoWindow.document.write(
		"<HTML>"+
		"<HEAD><TITLE>"+winTitle+"</TITLE></HEAD>"+
		"<BODY onBLUR='self.focus()'>"+
		"<p align='center'><img src="+photo.src+" onClick='self.close()' style='cursor:pointer;' title='"+txtClose+"'>"+
		"</BODY></HTML>");
	fotoWindow.document.close();
}

// add risize image to onload (page) function
var oldonload=window.onload;
if(typeof(oldonload)=='function'){
	window.onload=function(){oldonload();resizeimages();};
}else{
    window.onload=function(){resizeimages();};
}