/**************************************
 *
 * Pop-up script for Snabber,
 * By Niklas Bivald 2006-09-05 11:51
 * Version 0.0.9 [2006-09-20]
 *
 * Will make all images inside 'blockID' with suffix "__medium" and "__small" clickable and open
 * a popup displaying a larger version of the same image (either large/*__large.* or *__medium.* 
 * depending on which is available)
 **************************************/

// ID of the block 
//var blockID		= "adminDiv"; 
 
// Set to true if you want the cursor to change to a hand onMouseOver
var hand 		= true;   

// Should the popup have a toolbar? (yes/no)
var toolbar 	= "no";

// Should the popups have a menubar? (yes/no)
var menubar   	= "no";

// Should the popups have a location bar visible? (yes/no)
var locationbar = "no";

// Should the popups have a scrollbar? (yes, no, auto)
var scrollbar = "no";

// Should the popups be reziable? (yes, no) 
var resizable = "yes";

// Should we open a new window per pop-up? (false/true)
// true will open a new window per image,
// false will open the image in the old window if possible
var unique = false;

// Directory where the large version of the images lay...
var directory = "large";

// Error message (if the user is blocking popups)
var errorMessage 	= "Vi kunde inte öppna den stora version av bilden, spärrar du pop-ups?";
var snabberWindow 	= "";

function popupImage(url, size, type){
	if(size=="small"){
		 // Modify our url:
		 url  = url.replace('__small', '')
	}else{
		 // Modify our url:
		 url  = url.replace('__medium', '')
	}
	
	if (type == "landscape"){
		//var h 	= 500;
		var h 	= 650;
		var w 	= 650;
		var purl = "imgPopup.asp?type=landsc&url=";
	}else{
		var h 	= 650;
		var w 	= 650;
		//var w 	= 500;
		var purl = "imgPopup.asp?type=portr&url=";
	}	
	
	// 2) Add our subdirectory...
	furl = url.substr((url.lastIndexOf('/')+1));
	url  = url.substr(0,(url.lastIndexOf('/')+1)) + directory + '/' + furl;

	// Should we open a new popup per image?
	if(unique){
		// Open popup
		if( !this.open(purl + furl, furl, "height=" + (h) + ",width=" + (w) + ",toolbar=" + toolbar + ",menubar=" + menubar + " ,location=" + locationbar + ",scrollbars=" + scrollbar + ", resizable=" + resizable)  )
		  alert(errorMessage);
	}else{
		// Is the popup open? If so, change the url and size
		if(!snabberWindow.closed && snabberWindow.location){snabberWindow.location.href = purl + furl;
		// snabberWindow.resizeTo((w),(h));
		}else{
		// The popup is not open, open it
		snabberWindow = this.open(purl + furl, 'snabberPopup', "height=" + (h) + ",width=" + (w) + ",toolbar=" + toolbar + ",menubar=" + menubar + " ,location=" + locationbar + ",scrollbars=" + scrollbar + ", resizable=" + resizable); 
		}
		// Focus our popup
		snabberWindow.focus();
	}
}

function imageType(w, h){
	if (w > h){
		return "landscape";
	}
	return "portrait";
}
 
function doImages(size) {
	if (!document.getElementsByTagName || !document.getElementById) return false;

	// Get our images within the div with ID blockID (set by config)
	//var links = document.getElementById(blockID).getElementsByTagName('img');
	
	var links = document.getElementsByTagName('img');
  
	// Loop our images
	if(size=="small"){
		for (var i=0; i < links.length; i++) {
			if(links[i].src.indexOf('__small') > 0){	// If hand is true, we want to change the cursor to hand 	
				if(hand){ 	
					links[i].style.cursor = "pointer";		
				}
				links[i].className = "imgBorder";
				
				// Bind onClick	
				links[i].onclick = function(){		
					if(this.src.indexOf('__small') !== -1)			
						type = imageType(this.width, this.height); 
							
					//determining if landscape or portrait			
					popupImage(this.src, 'small', type);	 	
				}	
				//return false;
			}
		} 
	} else {
		for (var i=0; i < links.length; i++) {
			if(links[i].src.indexOf('__medium') > 0){	
				// If hand is true, we want to change the cursor to hand 	
				if(hand){ 	
					links[i].style.cursor = "pointer";	
				}
				links[i].className = "imgBorder";
				
				// Bind onClick	
				links[i].onclick = function(){		
					if(this.src.indexOf('__medium') !== -1)			
						type = imageType(this.width, this.height); 
						
						//determining if landscape or portrait			
						popupImage(this.src, 'medium', type);	 	
				}	
				//return false;
			}
		}
	}
}


