// zoom.js
// initiate on body or dependent components
function DOMready(fn, dependencies){
  var counter = 0;
  var collections = {};
  var timer = setInterval(
    function(){
      var ready = false;
      counter++;
      if (typeof document.getElementsByTagName != 'undefined'
        && (document.getElementsByTagName('body')[0] || document.body)){
        ready = true;
        if (typeof dependencies == 'object'){
          for (var i in dependencies){
            if (dependencies[i] == 'id' && !document.getElementById(i)){
              ready = false;
              break;
            } else if (dependencies[i] == 'tag'){
              var len = document.getElementsByTagName(i).length;
              if (typeof collections[i] == 'undefined' || collections[i] != len || len < 1){
                collections[i] = len;
                ready = false;
                break;
              }
            }
          }
        }
        if (ready){
          clearInterval(timer);
          fn();
        }
      }
      if (counter >= 40){
        clearInterval(timer);
      }
    }, 250  
  ); //end setInterval()
}
/* standard functions */
function createXMLHttpRequest( ) {
      try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) {}
      try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) {}
      try { return new XMLHttpRequest( ); } catch(e) {}
     // alert("XMLHttpRequest not supported");
      return null;
}

function $(id) {
	if( document.getElementById && document.getElementById(id) ){
		return document.getElementById(id);
	}else{
		//alert( 'id not found: ' + id );
	}
}
/* end standard functions */

function $$(tagname){ //returns an array of tag objects
  if( document.getElementsByTagName && document.getElementsByTagName(tagname) ){
    return document.getElementsByTagName(tagname);
  }else{
    //alert( 'id not found: ' + id );
  }
}
// stop default click behavior

function cancelClick(e){
  if( window.event && window.event.returnValue ){ window.event.returnValue = false; }
  if( e && e.preventDefault ){ e.preventDefault(); }
}
function cancelClickSafari(){ return false; }
function addEvent( obj, type, fn ) {
	if (obj.addEventListener) {
		obj.addEventListener( type, fn, false );
		EventCache.add(obj, type, fn);
	}
	else if (obj.attachEvent) {
		obj["e"+type+fn] = fn;
		obj[type+fn] = function() { obj["e"+type+fn]( window.event ); }
		obj.attachEvent( "on"+type, obj[type+fn] );
		EventCache.add(obj, type, fn);
	}
	else {
		obj["on"+type] = obj["e"+type+fn];
	}
}

var EventCache = function(){
	var listEvents = [];
	return {
		listEvents : listEvents,
		add : function(node, sEventName, fHandler){
			listEvents.push(arguments);
		},
		flush : function(){
			var i, item;
			for(i = listEvents.length - 1; i >= 0; i = i - 1){
				item = listEvents[i];
				if(item[0].removeEventListener){
					item[0].removeEventListener(item[1], item[2], item[3]);
				};
				if(item[1].substring(0, 2) != "on"){
					item[1] = "on" + item[1];
				};
				if(item[0].detachEvent){
					item[0].detachEvent(item[1], item[2]);
				};
				item[0][item[1]] = null;
			};
		}
	};
}();
addEvent(window,'unload',EventCache.flush);

function removeEvent(obj, evType, fn, useCapture){
    if (obj.removeEventListener){
      obj.removeEventListener(evType, fn, useCapture);
      return true;
	}else if (obj.detachEvent){
      var r = obj.detachEvent("on"+evType, fn);
      return r;
    }else{
      elm[ 'on' + evType ] = null;
    }
}


clickInfo = {

	highlightOn: function(){
		var textArea = $('topMessageSpan');
		 textArea.innerHTML = 'CLICK IMAGE TO SEE LARGER IMAGE';
	},
	
	highlightOff: function(){
		var textArea = $('topMessageSpan');
		textArea.innerHTML = 'move mouse over images';
	},

	init: function() {
		var divArray = document.getElementsByTagName( 'div' );
		for( i = 0; i < divArray.length; i++ ){
			var imgDivTag = divArray[ i ] ;
			if ( imgDivTag.className.match( /navLink/ )) { 
				clickInfo.addEvent( imgDivTag, 'mouseover', clickInfo.highlightOn, true );
				clickInfo.addEvent( imgDivTag, 'mouseout', clickInfo.highlightOff, true );
				clickInfo.addEvent( imgDivTag, 'click', clickInfo.seeLargeImage, true );
			}
		}
		var LimagePlaceHolder = $('Limage');
		clickInfo.addEvent( LimagePlaceHolder, 'click', clickInfo.closeLargeImage, true );
		clickInfo.addEvent( LimagePlaceHolder, 'mouseover', clickInfo.largeImageMsg, true );
		var slideShow = $('slideShow');
		clickInfo.addEvent( slideShow, 'click', clickInfo.slideShow, true );
	},
	
	slideShow: function(){
		clickInfo.closeLargeImage();
		var slideShow = document.createElement('iFrame');
		var showInfo = $('slideShow');
		slideShow.width = '594px'; //'800px' ;
		slideShow.height = '474px';//'720px';
		slideShow.style.border = '10px inset #222' ;
		slideShow.id = 'showFrame';
		if( window.event ){
			var IEsucksWindow = window.open( '', 'IEsucks', 'width=840 height=760' ); 
			var IEsucksContent = '<iframe src="' + showInfo.title + '" frameborder="1" height="474" width="594"></iframe><div align=center id=closeIEsucksAss onclick="window.close()" style="text-decoration:underline;font-size:1.2em;cursor:pointer">Close this window</div>';
			IEsucksWindow.document.write( IEsucksContent );
		}else{
			var showHere = $('LimageContainer');
			var childImg = $('Limage');
			showHere.appendChild( slideShow );
			slideShow.src = showInfo.title;
			showInfo.innerHTML = 'Close Slide Show';
			removeEvent( showInfo, 'click', clickInfo.slideShow, true );
			clickInfo.addEvent( showInfo, 'click', clickInfo.closeShow, true );			
		}
	},
/*

<div style="text-align:center;width:594px;"><iframe frameborder="0" marginheight="0" marginwidth="0" scrolling="no" src="http://www.bubbleshare.com/album/87224.f7be913a59e/mini?mute=true&amp;amp;autoplay=true&amp;amp;size=580x435&amp;amp;interval=2&amp;amp;style=square" style="width: 594px; height: 474px;"></iframe><span style="font-family:arial,helvetica,sans-serif;font-size:9px">
<a href="http://www.bubbleshare.com/album/87224.f7be913a59e" style="font-size:100%;">This album</a> is powered by
<a href="http://www.bubbleshare.com" style="font-size:100%;">BubbleShare</a>
 - <a href="http://www.bubbleshare.com/album/87224.f7be913a59e/share#add_to_blog" style="font-size:100%;">Add to my blog</a>
</span></div>
*/
	closeShow: function(){
		var showHere = $('showFrame');
		var showInfo = $('slideShow');
		$('LimageContainer').removeChild( showHere );
		showInfo.innerHTML = 'ZOOM slide show!';
		removeEvent( showInfo, 'click', clickInfo.closeShow, true );
		clickInfo.addEvent( showInfo, 'click', clickInfo.slideShow, true );
	},

	largeImageMsg: function(){
		var textArea = $('topMessageSpan');
		textArea.innerHTML = 'CLICK ON IMAGE TO GO BACK';
	},
	
	seeLargeImage: function(e){
		var targ = window.event ? window.event.srcElement : e ? e.target : null ;
		if( !targ ) return ;
		if (targ.nodeType == 3) // defeat Safari bug
			targ = targ.parentNode;

		clickInfo.closeLargeImage();

		var imagePathOrig = targ.src;
		var imageNameReg = /(.*?)(_?[SM]?)(.[jJgGpP][pPiInN][gGfF])$/;
		var imageNameArray = imageNameReg.exec( imagePathOrig );
		var LimagePath = imageNameArray[1] + '_L' + imageNameArray[3] ;

		var windowWidth, windowHeight;
		if( window.innerWidth ){
			windowWidth = (window.innerWidth - 50) + 'px' ;
			windowHeight = (window.innerHeight - 100) + 'px' ;
		}else{
			windowWidth = (document.body.clientWidth - 50) + 'px';
			windowHeight = (document.body.clientHeight - 100) + 'px';
		}		
		if( window.event ){
			var IEsucksWindow = window.open( '', 'IEsucks', '' ); 
			var IEsucksContent = '<img src=' + LimagePath + ' style="max-height:' + windowHeight + '; max-width:' + windowWidth + ';" onclick="window.close()">';
			IEsucksWindow.document.write( IEsucksContent );
		}else{
			var LimagePlaceHolder = $( 'Limage' );
			LimagePlaceHolder.src = LimagePath ;
			LimagePlaceHolder.style.maxHeight = windowHeight;
			LimagePlaceHolder.style.maxWidth = windowWidth;
			LimagePlaceHolder.style.visibility = 'visible';		
		}
	},
	
	closeLargeImage: function(){
		
		if( typeof IEsucksWindow != 'undefined' ) { IEsucksWindow.close(); }
		
		var Limage = $( 'Limage' );
		Limage.src = 'images/spacer.gif';		
		Limage.style.visibility = 'hidden';		
		
		var textArea = $('topMessageSpan');
		textArea.innerHTML = 'move mouse over images';
	},

	addEvent: function( elm, evType, fn, useCapture ) {
		if(elm.addEventListener){
			elm.addEventListener( evType, fn, useCapture );
			return true;
		} else if( elm.attachEvent) {
			var r = elm.attachEvent( 'on' + evType, fn );
			EventCache.add( elm, evType, fn );
			return r;
		} else {
			elm[ 'on' + evType ] = fn;
		}
	}

};

DOMready( clickInfo.init, { 'slideShow' : 'id' } );
