function fixPng(img)
{
    try {
        var arVersion = navigator.appVersion.split('MSIE');
        var version = parseFloat(arVersion[1]);
        if ((version >= 5.5) && (version < 7) && (document.body.filters)){
            var pngImage = $(img);
            var visible = pngImage.visible();
            if(!visible) pngImage.show();
            var width = pngImage.getWidth();
            var height = pngImage.getHeight();
            if(!visible) pngImage.hide();
            var imgId = pngImage.id ? 'id="' + pngImage.id + '" ' : '';
            var imgClass = pngImage.className ? 'class="' + pngImage.className + '" ' : '';
            var imgTitle = pngImage.title ? 'title="' + pngImage.title    + '" ' : 'title="' + pngImage.alt + '" ';
            var imgStyle = 'display:inline-block;' + pngImage.style.cssText;
            var replacement = '' + imgId + imgClass + imgTitle
                                    + ' style="' + 'width:' + width
                                    + 'px; height:' + height
                                    + 'px;' + imgStyle + ';'
                                    + 'filter:progid:DXImageTransform.Microsoft.AlphaImageLoader'
                                    + '(src="' + myImage.src + '", sizingMethod="scale");">';
            pngImage.outerHTML = replacement;
        }
    } catch(e) {}
};


function window_load() {
		var cw = new Element('img',{id:'cw_image',src:'../images/campingworld.png'}).setStyle({width:'155px',height:'69px',position:'absolute',zIndex:100,marginTop:'10px',marginLeft:'668px'});
		$('showholder').insert(cw);
		fixPng($('cw_image'));
		
		var images = $A([
			'../images/08header7.jpg',
			'../images/08header4.jpg',
			'../images/08header5.jpg',
			'../images/08header6.jpg',
			'../images/08header2.jpg',
			'../images/08header3.jpg',
			'../images/mainimage_contact.jpg',
			'../images/08header1.jpg'
		]);
		images.shuffle();
		
		var ip = new ImagePreloader(images, function(aImages, nImages) {
			var ss = Slideshow;
			ss.images = $A(aImages);
			ss.container = $('showholder');
			ss.initialize();
			ss.start();
		});
	}
	Event.observe(window, 'load', window_load);
	
	var Slideshow = {
		images:[],
		container:$(document.getElementsByTagName('body')[0]),
		currentIndex:-1,
		initialize:function() {
			/*this.images.each(function(s,index) {
				s.id = 'slideshow_image_' + index;
				s.style.position = 'absolute';
				s.style.width = '833px';
				new Effect.Opacity($(s),{from:1,to:0,duration:0});
				this.insert(s);
			},this.container);*/
			for (s=0;s<this.images.length;s++) {
				this.images[s] = $(this.images[s]);
				this.images[s].id = 'slideshow_image_' + s;
				this.images[s].setStyle({position:'absolute',width:'833px'});
				new Effect.Opacity(this.images[s],{from:1,to:0,duration:0});
				this.container.insert(this.images[s]);
				
			}
		},
		start:function() {
			this.crossFade(this);
			//setInterval(this.crossFade,6000,SlideShow);
		},
		crossFade:function(ob) {
			//console.log(ob);
			new Effect.Parallel([
				((ob.currentIndex != -1) ? (new Effect.Opacity($('slideshow_image_'+ob.currentIndex),{sync:true,from:1,to:0})) : (new Effect.Appear($('showholder'),{sync:true}))),
				((ob.currentIndex+1 < ob.images.length) ? (new Effect.Opacity($('slideshow_image_'+(ob.currentIndex+1)),{sync:true,from:0,to:1})) : (new Effect.Appear($('slideshow_image_0'),{sync:true})))
			], {
				duration: 1,
				object:ob,
				afterFinish:function(o){
					//console.log(o.options.object);
					var b = o.options.object;
					//console.log(b);
					//o;// = this.object;
					if (b.currentIndex != b.images.length-1) {
						b.currentIndex = (b.currentIndex+1);
					} else {
						b.currentIndex = 0;
					}
					setTimeout(function(){
						b.crossFade(b);
					},2000);
				}
			});
		}
	};

function setPrefix(elmName) {
	if (document.getElementById != null) {
		return document.getElementById(elmName);
	}
	if (document.all != null) {
		return document.all[elmName];
	}
}
function hideDivPicture() {
	setPrefix("divPicture").style.display = 'none';
}
function loadDivPicture(filename, orgid, caption){
	pad = window.pageYOffset || document.documentElement.scrollTop;
	if (setPrefix("divPicture")) {
		setPrefix("divPicture").style.paddingTop = pad - 75 + 'px';
		setPrefix("divPicture").style.display = 'block';
		setPrefix("divPicture").innerHTML = '<table border="0" cellpadding="0" cellspacing="0" style="border:1px solid #000000"><tr valign="top"><td align="right" style="background:#ffffff"><img src="../images/close.gif" style="cursor:pointer" onclick="hideDivPicture();" /></td</tr><tr><td><table border="0" width="360" cellpadding="5" cellspacing="0" style="background:#f2f2f2; border:5px solid #ffffff"><tr><td><img src="../gallery/' + orgid + '/' + filename + '" style="cursor:pointer" onclick="hideDivPicture();" /></td></tr></table></td></tr><tr><td style="padding:10px; text-align:left; background:#ffffff">'+caption+'</td></tr></table>';
	} else {
		var pBody = document.getElementsByTagName('body')[0];
		divPicture = document.createElement('div');
		divPicture.style.position = 'absolute';
		divPicture.style.width = '837px';
		divPicture.style.top = posy + 'px';
		divPicture.style.left = posx + 'px';
		divPicture.style.paddingTop = '50px';
		divPicture.style.display = 'none';
		divPicture.style.zIndex = 1000;
		divPicture.id = 'divPicture';
		pBody.appendChild(divPicture);
		divPicture = setPrefix('divPicture');
		divPicture.style.display = 'block';
	}
}

function grayOut(vis, options) {
  // Pass true to gray out screen, false to ungray
  // options are optional.  This is a JSON object with the following (optional) properties
  // opacity:0-100         // Lower number = less grayout higher = more of a blackout 
  // zindex: #             // HTML elements with a higher zindex appear on top of the gray out
  // bgcolor: (#xxxxxx)    // Standard RGB Hex color code
  // grayOut(true, {'zindex':'50', 'bgcolor':'#0000FF', 'opacity':'70'});
  // Because options is JSON opacity/zindex/bgcolor are all optional and can appear
  // in any order.  Pass only the properties you need to set.
  var options = options || {}; 
  var zindex = options.zindex || 50;
  var opacity = options.opacity || 70;
  var opaque = (opacity / 100);
  var bgcolor = options.bgcolor || '#000000';
  var dark=document.getElementById('darkenScreenObject');
  if (!dark) {
    // The dark layer doesn't exist, it's never been created.  So we'll
    // create it here and apply some basic styles.
    // If you are getting errors in IE see: http://support.microsoft.com/default.aspx/kb/927917
    var tbody = document.getElementsByTagName("body")[0];
    var tnode = document.createElement('div');           // Create the layer.
        tnode.style.position='absolute';                 // Position absolutely
        tnode.style.top='0px';                           // In the top
        tnode.style.left='0px';                          // Left corner of the page
        tnode.style.overflow='hidden';                   // Try to avoid making scroll bars            
        tnode.style.display='none';                      // Start out Hidden
        tnode.id='darkenScreenObject';                   // Name it so we can find it later
    tbody.appendChild(tnode);                            // Add it to the web page
    dark=document.getElementById('darkenScreenObject');  // Get the object.
  }
  if (vis) {
    // Calculate the page width and height 
    if( document.body && ( document.body.scrollWidth || document.body.scrollHeight ) ) {
        var pageWidth = document.body.scrollWidth+'px';
        var pageHeight = document.body.scrollHeight+'px';
    } else if( document.body.offsetWidth ) {
      var pageWidth = document.body.offsetWidth+'px';
      var pageHeight = document.body.offsetHeight+'px';
    } else {
       var pageWidth='100%';
       var pageHeight='100%';
    }   
    //set the shader to cover the entire page and make it visible.
    dark.style.opacity=opaque;                      
    dark.style.MozOpacity=opaque;                   
    dark.style.filter='alpha(opacity='+opacity+')'; 
    dark.style.zIndex=zindex;        
    dark.style.backgroundColor=bgcolor;  
    dark.style.width= pageWidth;
    dark.style.height= pageHeight;
    dark.style.display='block';                          
  } else {
     dark.style.display='none';
  }
}

function createGallery(galleryid) {
	var tbody = document.getElementsByTagName("body")[0];
	
	var divGalleryBG = setPrefix('divGalleryBG');
	
	if (!divGalleryBG) {
		divGalleryBG = document.createElement('div');
		divGalleryBG.style.position = 'absolute';
		divGalleryBG.style.overflow = 'hidden';
		divGalleryBG.style.display = 'none';
		divGalleryBG.style.top = '0px';
		divGalleryBG.style.left = '0px';
		if (detectMacXFF2()) {
			divGalleryBG.style.background = 'url(../images/000000_80.png)';
		}
		else {
			divGalleryBG.style.backgroundColor = '#000000';
			divGalleryBG.style.opacity= .80;                      
			divGalleryBG.style.MozOpacity= .80;                   
			divGalleryBG.style.filter='alpha(opacity=80)';
		}
		divGalleryBG.style.zIndex = 2;
		divGalleryBG.id = 'divGalleryBG';
		tbody.appendChild(divGalleryBG);
		divGalleryBG = setPrefix('divGalleryBG');
	}
	
	// Calculate the page width and height 
    if( document.body && ( document.body.scrollWidth || document.body.scrollHeight ) ) {
        var pageWidth = (document.body.scrollWidth)+'px';
        var pageHeight = (document.body.scrollHeight)+'px';
    } else if( document.body.offsetWidth ) {
      var pageWidth = (document.body.offsetWidth)+'px';
      var pageHeight = (document.body.offsetHeight)+'px';
    } else {
       var pageWidth='100%';
       var pageHeight='100%';
    }   
	
	divGalleryBG.style.width = pageWidth;
	divGalleryBG.style.height = pageHeight;
	
	divGalleryBG.style.display = 'block';
	
	
	
		
	
	var pageHeight = window.innerHeight || document.documentElement.clientHeight;
	var pageScroll = window.pageYOffset || document.documentElement.scrollTop;
	var flashHeight = 610;
	var flashWidth = 900;
	//if (pageHeight < flashHeight) {
	//	flashWidth = flashWidth * (pageHeight / flashHeight);
	//	flashHeight = pageHeight;
	//}
	//alert(pageHeight);
	//alert(pageScroll);
	var posy = (pageHeight)/2 + (pageScroll) - flashHeight/2;
	if (posy < 0) {
		posy = 0;
	}
	//if ((posy + 700) > (pageHeight - pageScroll)) {
	//	posy = 0;
	//}
	var posx = (window.innerWidth || document.body.clientWidth)/2 - flashWidth/2;
	if (posx < 0) posx = 0;
	
	var divGallery = setPrefix('divGallery');
	
	if (!divGallery) {
		var divGallery = document.createElement('div');
		divGallery.style.position = 'absolute';
		divGallery.style.display = 'none';
		divGallery.style.zIndex = 999;
		divGallery.style.width = flashWidth + 'px';
		divGallery.style.height = flashHeight + 'px';
		divGallery.id = 'divGallery';
		tbody.appendChild(divGallery);
		divGallery = setPrefix('divGallery');
	}
	
	divGallery.style.top = posy + 'px';
	divGallery.style.left = posx + 'px';
	divGallery.innerHTML = 	'' + 
							'<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" width="'+flashWidth+'" height="'+flashHeight+'" id="gallery" align="middle">' +
							'<param name="allowScriptAccess" value="sameDomain" />' + 
							'<param name="allowFullScreen" value="false" />' +
							'<param name="WMODE" value="Transparent" />' +
							'<param name="movie" value="../swf/photogallery.swf?galleryid=' + galleryid + '" />' +
							'<param name="play" value="false" />' +
							'<param name="loop" value="false" />' +
							'<param name="quality" value="high" />' +
							'<param name="bgcolor" value="#000000" />' +
							'<embed src="../swf/photogallery.swf?galleryid=' + galleryid + '" play="false" loop="false" quality="high" bgcolor="#000000" width="'+flashWidth+'" height="'+flashHeight+'" name="gallery" align="middle" allowScriptAccess="sameDomain" allowFullScreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" wmode="transparent" />' +
							'</object>';

	
	
	divGallery.style.display = 'block';
	
	
}
	function closeGallery() {
		var divGalleryBG = setPrefix('divGalleryBG');
		var divGallery = setPrefix('divGallery');
	
	
	if (divGalleryBG) {
		divGalleryBG.style.display = 'none';
	}
	if (divGallery) {
		divGallery.style.display = 'none';
	}
	}
	
	function findPos(obj) {
	var curleft = curtop = 0;
	
	if (obj.offsetParent) {
		do {
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		}
		while (obj = obj.offsetParent);
		
	}
	
	return [curleft, curtop];
}

function detectMacXFF2() {
  var userAgent = navigator.userAgent.toLowerCase();
  if (/firefox[\/\s](\d+\.\d+)/.test(userAgent)) {
    var ffversion = new Number(RegExp.$1);
    if (ffversion < 3 && userAgent.indexOf('mac') != -1) {
      return true;
    }
  }
}

