function initImage(photoID) {
	imageId = photoID;
	image = document.getElementById(imageId);
	setOpacity(image, 0);
	image.style.visibility = "visible";
	fadeIn(imageId,0);
}
function fadeIn(objId,opacity) {
	if (document.getElementById) {
		obj = document.getElementById(objId);
		if (opacity <= 100) {
			setOpacity(obj, opacity);
			opacity += 10;
			window.setTimeout("fadeIn('"+objId+"',"+opacity+")", 30);
		}
	}
}
function setOpacity(obj, opacity) {
	opacity = (opacity == 100)?99.999:opacity;
	// IE/Win
	obj.style.filter = "alpha(opacity:"+opacity+")";
	// Safari<1.2, Konqueror
	obj.style.KHTMLOpacity = opacity/100;
	// Older Mozilla and Firefox
	obj.style.MozOpacity = opacity/100;
	// Safari 1.2, newer Firefox and Mozilla, CSS3
	obj.style.opacity = opacity/100;
}
function MM_setTextOfLayer(objId,x,newText) { //v9.0
  with (document) if (getElementById && ((obj=getElementById(objId))!=null))
    with (obj) innerHTML = unescape(newText);
}
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_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 MM_changeProp(objId,x,theProp,theValue) { //v9.0
  var obj = null; with (document){ if (getElementById)
  obj = getElementById(objId); }
  if (obj){
    if (theValue == true || theValue == false)
      eval("obj.style."+theProp+"="+theValue);
    else eval("obj.style."+theProp+"='"+theValue+"'");
  }
}

var setScroll = function()
{
	if( $('scrollbar_content') && $('scrollbar_track') )
	{
		var scrollbar = new Control.ScrollBar('scrollbar_content','scrollbar_track');		
	}
}

var myArray = [];

var exwindow_factory = function(container,options)
{
	setTimeout( function() {	
		var window_fade = $('control_overlay');

		var window_header = new Element('div', { className: 'window_header' });
		var window_close = new Element('div',{ className: 'window_close' });
		var window_contents = new Element('div',{ className: 'window_contents' });
		var window_footer = new Element('div',{ className: 'window_footer' });

		var w = new Control.Modal(container.href, Object.extend({
			className: 'modal',
			fade: true,
			overlayOpacity: 0.50,
			closeOnClick: window_fade,
			insertRemoteContentAt: window_contents
		},options || {}));

		w.container.insert(window_header);
		window_header.insert(window_close);
		w.container.insert(window_contents);
		w.container.insert(window_footer);

		window_close.observe('click', function(e) {
			Control.Modal.close();
			e.stop();
		}, false);
	
		w.open();  
	}, 1000 );
};


var modal = function() 
{  
	var lightwindows = $$('.lightwindow');
	
	document.observe('lw:prev', lwnav);
	document.observe('lw:next', lwnav);
	
	var i = 1;
	
   	$$('.lightwindow').each( function(el) 
	{
		myArray.push(window_factory(el, {}, lightwindows, i));
		i++;
	});
}

var lwnav = function(e)
{
	var pos = e.memo.pos;
	setTimeout( function() { myArray[pos].open(); }, 1000 );
	
}

var window_factory = function(container,options, lightwindows, i)
{  
	var window_fade = $('control_overlay');

    var window_header = new Element('div',{  
    	className: 'window_header'  
    });                                     
    var window_close = new Element('div',{  
        className: 'window_close'  
    });                                    
    var window_contents = new Element('div',{  
        className: 'window_contents'  
    });	
    var window_footer = new Element('div',{  
        className: 'window_footer'  
    });	
    var window_previous = new Element('div',{  
        className: 'window_previous'  
    });
	if (i > 1)
	{
		var previous_link = new Element('a',{  
	        className: 'previous_link',
			href: lightwindows[i-2].href
		}).update("Prev");		
		
		previous_link.observe('click', function(e){
			Control.Modal.close();	
			document.fire('lw:prev', {"pos":i-2});	
			e.stop();	
		},false);	
	}

	
	var footer_info = new Element('span', {
		className: 'footer_info'
	}).update(i+" of "+lightwindows.length);

    var window_next = new Element('div',{  
        className: 'window_next'  
    });
	if (lightwindows.length > i)
	{
		var next_link = new Element('a', {
			className: 'next_link lightwindow',
			href: lightwindows[i].href
		}).update("Next");
		
		next_link.observe('click', function(e){
			Control.Modal.close();		
			document.fire('lw:next', {"pos":i});
			e.stop();	
		},false);
	}

    var w = new Control.Modal(container,Object.extend({  
        className: 'modal',
  		fade: true,
		overlayOpacity: 0.50,
   		closeOnClick: window_fade,
        insertRemoteContentAt: window_contents
    },options || {}));  
    w.container.insert(window_header);   
       window_header.insert(window_close);                                                                 
       w.container.insert(window_contents);
   	w.container.insert(window_footer);
   	if (lightwindows.length > i)
   	{
   		window_footer.insert(window_next);
   		window_next.insert(next_link);
   	}
   	if (i > 1) 
   	{
   		window_footer.insert(window_previous);
   		window_previous.insert(previous_link);		
   	}
   	window_footer.insert(footer_info);
	
  	window_close.observe('click', function(e){
	    Control.Modal.close();
		e.stop();
	}, false);
    return w;  
};

var slideshowLink = function()
{
	var exists = $$('body#gallery .picturesLeftClips .lightwindow img').length;
	if($('slideshow-link'))
	{
		if(!exists) { $('slideshow-link').hide(); }
		
		$('slideshow-link').observe('click', function(e) {

			if(!exists) { e.stop(); return; }
			
			// var window_loading = new Element('div',{
			// 	className: 'window_loading'
			// });
			
			var window_fade = $('control_overlay');
			var f = new Control.Modal($('slideshow-link'),Object.extend({  
		        className: 'slideshow-modal',
		  		fade: true,
				overlayOpacity: 0.99,
		   		closeOnClick: window_fade,
				iframe: true,
				afterClose: function() { 
					var id = this.container.id;
					this.destroy();
					$(id).remove();
				}
		    },{} || {}));
		
			var window_close = new Element('div',{  
		        className: 'slideshow-close'  
		    }).update('close slideshow');
		
			window_fade.insert(window_close);
		
			window_close.observe('click', function(e){
			    Control.Modal.close();
				e.stop();
			}, false);

			f.open();
			e.stop();
			
		}, false );
	}
}

var publishok = function()
{
	if($('publish-site-link'))
	{
		$('publish-site-link').observe( 'click', function( e ) {
			if(!confirm('Are you sure you want to publish all changes?'))
			{
				e.stop();
			}
		}, false );
	}
}

var pressOver = function()
{
	$('press-link').stopObserving('mouseover');
	$('nav_gallery').appear({
		afterFinish: function() 
		{
			$('press-link').observe('mouseout', function(e)
			{
				setTimeout(revertNav, 10000);						
			});
		}
	});
}

var hideGalleryNav = function()
{
	if($('nav_gallery'))
	{
		var galleryLink = $('nav_gallery');		

		if (window.location.pathname != '/page/gallery.html') {
			galleryLink.hide();	
			
			$('press-link').observe('mouseover', pressOver, false); 
		};
	}
}

var revertNav = function()
{
	$('press-link').stopObserving('mouseout');
	$('nav_gallery').fade({ 
		duration: 2.0, afterFinish: function()
		{
			$('press-link').observe('mouseover', pressOver, false); 
		} 
	});
};

function externalLinks() { $$('a').each( function(el) { if(el.getAttribute("href") && el.getAttribute("rel") == "external") { el.target="_blank"; } } ); }
function internalLinks() { $$('a').each( function(el) { if(el.getAttribute("href") && el.getAttribute("rel") == "internal") { el.target="_self"; } } ); }

document.observe('dom:loaded', hideGalleryNav);
document.observe('dom:loaded', externalLinks);
document.observe('dom:loaded', internalLinks);
document.observe('dom:loaded', setScroll);
document.observe('dom:loaded', modal);
document.observe('dom:loaded', slideshowLink);
document.observe('dom:loaded', publishok);
