function showsubmenu(nr) {
	$('#' + nr).toggle();
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

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];}
}

//ddmenu code
menuhover = function() {
	if (document.getElementById("ddmenu")) {
		var sfEls = document.getElementById("ddmenu").getElementsByTagName("li");
		for (var i=0; i<sfEls.length; i++) {
			sfEls[i].onmouseover=function() {
				this.className+=" ddmenuhover";
			}
			sfEls[i].onmouseout=function() {
				this.className=this.className.replace(new RegExp(" ddmenuhover\\b"), "");
			}
		}
	}
}
if (window.attachEvent) window.attachEvent("onload", menuhover);


// img drop shadows
function addShadows(){
	// add page level drop shadow
	$('#page-wrap').wrap('<div id="outer-wrap"></div>');
	$('#page-wrap').wrap('<div class="owds"></div>');
	$('#page-wrap').wrap('<div class="owds"></div>');
	$('#page-wrap').wrap('<div class="owds"></div>');
	$('#page-wrap').wrap('<div class="owds"></div>');
	
	// loop through images with dropshadow classes
	$("[class*=dropshadow]").each(function(i){
		var align = $(this).attr('align');
		var floating = $(this).css('float');
		if(floating=='left'||floating=='right'||align=='left'||align=='right'){
			$(this).css({float:'none'}).removeAttr('align');
		}
		$(this).removeAttr('hspace').removeAttr('vspace').removeAttr('border');
		var padding = ($(this).hasClass("dropshadowgap")||$(this).hasClass("dropshadowgapcaption"))?7:0;
		var cwidth = $(this).width();
		var cheight = $(this).height();
		$(this).wrap('<div class="imgframe"></div>');
		$(this).parent('.imgframe').css({width:''+cwidth+'px',padding:''+padding+'px'});
		if (($(this).hasClass("dropshadowcaption")||$(this).hasClass("dropshadowgapcaption"))&&($(this).attr('alt')||$(this).attr('title'))) {
			if ($(this).attr('title')){
				var credit = $(this).attr("title");
				$(this).after('<p class="imgcredit"></p>');
				$(this).siblings('p.imgcredit').text(credit);
				var creditheight = ($(this).attr('alt'))?($(this).siblings('p.imgcredit').height()+7):($(this).siblings('p.imgcredit').height()+14);
			} else {
				var creditheight = 0;
			}
			if ($(this).attr('alt')){
				var caption = $(this).attr("alt");
				$(this).after('<p class="imgcaption"></p>');
				$(this).siblings('p.imgcaption').text(caption);
				var captionheight = $(this).siblings('p.imgcaption').height()+14;
			} else {
				var captionheight = 0;
			}
			cheight+=(captionheight+creditheight);
			if ($(this).hasClass("dropshadowgapcaption")){
				cheight+=padding;
			}
		} else if ($(this).hasClass("dropshadowgap")){
			cheight+=(2*padding);
		}
		cwidth+=(2*padding);
		$(this).parent('.imgframe').wrap('<div class="ds1"><div class="ds2"><div class="ds3"><div class="ds4"><div class="ds5"><div class="ds6"></div></div></div></div></div></div>');
		$(this).parent('.imgframe').parents('div[class^=ds]').css({width:''+(cwidth+2)+'px',height:''+(cheight+2)+'px'});
		if(align=='left'||floating=='left'){
			$(this).parent('.imgframe').parents('.ds1').css({float:'left',marginRight:'1ex'});
		} else if(align=='right'||floating=='right'){
			$(this).parent('.imgframe').parents('.ds1').css({float:'right',marginLeft:'1ex'});
		}
		$(this).parent('.imgframe').css({height:''+(cheight-(2*padding))+'px'});
		$(this).css({position:'relative',top:'0px',left:'0px'});
	});
};

function addSlideshows() {

	// Start Slideshow ul - li
	$("ul.slideshow li>a").dblclick(function() {
		return true;
	});
	
	$("ul.slideshow").each(function (i) {
		var slideshow_max_height;
		var slideshow_max_caption_height;
		var slideshow_max_width;
		var float_css;
		
		var floating = $(this).css('float');
		if (floating=='left') {
			$(this).css('float','none');
			$(this).removeAttr('align');
		} else {
			$(this).css('float','none');
			$(this).removeAttr('align');
		}
		
		$(this).children("li").css({top:''+$(this).css('top')+'',left:''+$(this).css('left')+''});
		
		$(this).children("li").children("img").each( function() {
			var cwidth = $(this).width();
			var cheight = $(this).height();
			var padding = 0;
		
			if ($(this).height() > slideshow_max_height || !slideshow_max_height) {
				slideshow_max_height = cheight;
			}
			if ($(this).width() > slideshow_max_width || !slideshow_max_width) {
				slideshow_max_width = cwidth;
			}	
			
		});
		
		$(this).children("li").children("img").each( function() {
		
			var cwidth = slideshow_max_width;
			var cheight = slideshow_max_height;
			var padding = 0;		
		
			// handle captions and credits
			if ($(this).attr('title')) {
				var credit = $(this).attr("title");
				$(this).after('<p class="imgcredit"></p>');
				$(this).siblings('p.imgcredit').text(credit);
				$(this).siblings('p.imgcredit').css({width:''+(cwidth-8)+'px'});
				var creditheight = ($(this).attr('alt'))?($(this).siblings('p.imgcredit').height()+7):($(this).siblings('p.imgcredit').height()+14);
			} else {
				var creditheight = 0;
			}
			if ($(this).attr('alt')) {
				var caption = $(this).attr("alt");
				$(this).after('<p class="imgcaption"></p>');
				$(this).siblings('p.imgcaption').text(caption);
				$(this).siblings('p.imgcaption').css({width:''+(cwidth-8)+'px'});
				var captionheight = $(this).siblings('p.imgcaption').height()+14;
			} else {
				var captionheight = 0;
			}
			captionheight=(captionheight+creditheight);
			
			if (captionheight > slideshow_max_caption_height || !slideshow_max_caption_height) {
				slideshow_max_caption_height = captionheight;
			}		
		
			if ($(this).height() < (slideshow_max_height - 2)) {
				//alert($(this).height() + " " + slideshow_max_height + " " + Math.round((slideshow_max_height - $(this).height()) / 2));
				$(this).css({marginTop:''+Math.round((slideshow_max_height - $(this).height()) / 2)+'px'});
			}
			if ($(this).width() < (slideshow_max_width - 2)) {
				//alert($(this).width() + " " + slideshow_max_width + " " + Math.round((slideshow_max_width - $(this).width()) / 2));
				$(this).css({marginLeft:''+Math.round((slideshow_max_width - $(this).width()) / 2)+'px'});
			}
			
			if (($(this).height() < (slideshow_max_height - 2)) && ($(this).width() < (slideshow_max_width - 2))) {
				$(this).css({border:'1px solid #cccccc'});
			}
			
		});
		
		var cwidth = slideshow_max_width;
		var cheight = slideshow_max_height + slideshow_max_caption_height;
		$(this).children('li').css({height:''+cheight+'px'});
		var padding = 0;
		
		$(this).css({width:''+cwidth+'px',height:''+cheight+'px'});
		
		cheight = cheight + 28;

		if (floating=='left') {
			float_css = "float: left; margin-right: 1ex";
		} else if (floating=='right') {
			float_css = "float: right; margin-left: 1ex";
		}
		$(this).wrap('<div class="slideframe"></div>');
		$(this).children().children("img").wrap('<div class="imgframe"></div>');
		$(this).parent('.slideframe').wrap('<div class="ds1"><div class="ds2"><div class="ds3"><div class="ds4"><div class="ds5"><div class="ds6"></div></div></div></div></div></div>');
		$(this).parent('.slideframe').parents('div[class^=ds]').css({width:''+(cwidth+2)+'px',height:''+(cheight+2)+'px'});
		if(floating=='left'){
			$(this).parent('.slideframe').parents('.ds1').css({float:'left',marginRight:'1ex'});
		} else if(floating=='right'){
			$(this).parent('.slideframe').parents('.ds1').css({float:'right',marginLeft:'1ex'});
		}
		$(this).children().children('.imgframe').css({width:''+cwidth+'px',height:''+(slideshow_max_height-(2*padding))+'px',top:'0px',left:'0px'});
		$(this).parent('.slideframe').css({width:''+cwidth+'px',height:''+(cheight-(2*padding))+'px'});
		$(this).css({position:'relative',top:'0px',left:'0px'});
		$(this).after('<div class="prev_next_buttons"><a class="prev disabled" href="#">Previous</a><span class="count">' + "1 / " + $(this).children("li").length + '</span><a class="next" href="#">Next</a></div>');
	});	
	
	$("ul.slideshow li").hide();
	$("ul.slideshow li:nth-child(1)").show();
	$("ul.slideshow li:nth-child(1)").addClass('active');
	
	$("a.next").click(function() {
		var siblings = $(this).parent().parent().children('ul.slideshow').children('li');
	    var visibleSiblings = siblings.filter('.active');
		var nextLi = visibleSiblings.next();
		var button = $(this);
		var prev_button = $(this).parent().children('a.prev');
		var temp_last = false;

		if (visibleSiblings.is(':last-child') || $(this).hasClass('disabled')) {
			return false;
		}

		if (visibleSiblings.not(':first-child') && visibleSiblings.hasClass('disabled')) {
			return false;
		}
		
		if (nextLi.is(':last-child')) {
			$(this).addClass('disabled');
			temp_last = true;
		} else {
			$(this).addClass('disabled');
		}
		
		prev_button.addClass('disabled');
		
		temp_width = visibleSiblings.width();
		visibleSiblings.css({zIndex:'1'});
		visibleSiblings.removeClass('active');
		nextLi.addClass('active');
		nextLi.css({left:''+temp_width+'px',zIndex:'2',borderLeft:'1px solid #cccccc'});
		nextLi.show();
		visibleSiblings.animate({left:'-'+temp_width},1000);
		nextLi.animate({left:0},1000,"",function() {
			nextLi.css({borderLeft:'none'});
			visibleSiblings.hide();
			visibleSiblings.css({left:0});
			if (temp_last != true) {
				button.removeClass('disabled');
			}
			prev_button.removeClass('disabled');
		});
		siblings.each(function(i) {
			if($(this).is(".active")) {
				$(this).parent().parent().children("div.prev_next_buttons").children("span.count").html(i + 1 + " / " + siblings.length);
			}
		});

	    return false;
	});
	
	$("a.prev").click(function() {
		var siblings = $(this).parent().parent().children('ul.slideshow').children('li');
	    var visibleSiblings = siblings.filter('.active');
		var prevLi = visibleSiblings.prev();
		var button = $(this);
		var next_button = $(this).parent().children('a.next');
		var temp_first = false;

		if (visibleSiblings.is(':first-child') || $(this).hasClass('disabled')) {
			return false;
		}				
		
		if (visibleSiblings.not(':last-child') && visibleSiblings.hasClass('disabled')) {
			return false;
		}
		
		
		if (prevLi.is(':first-child')) {
			$(this).addClass('disabled');
			temp_first = true;
		} else {
			$(this).addClass('disabled');
		}
		
		next_button.addClass('disabled');
		
		temp_width = visibleSiblings.width();
		visibleSiblings.css({zIndex:'1'});
		visibleSiblings.removeClass('active');
		prevLi.addClass('active');
		prevLi.css({left:'-'+temp_width+'px',zIndex:'2',borderRight:'1px solid #cccccc'});
		prevLi.show();
		visibleSiblings.animate({left:temp_width},1000);
		prevLi.animate({left:0},1000,"",function() {
			prevLi.css({borderRight:'none'});
			visibleSiblings.hide();
			visibleSiblings.css({left:0});
			if (temp_first == false) {
				button.removeClass('disabled');
			}
			next_button.removeClass('disabled')
		});
		siblings.each(function(i) {
			if($(this).is(".active")) {
				$(this).parent().parent().children("div.prev_next_buttons").children("span.count").html(i + 1 + " / " + siblings.length);
			}
		});
		
	    return false;
	});
  
};

// Cross-browser implementation of element.addEventListener()
function addListener(element, type, expression, bubbling) {
	bubbling = bubbling || false;
	if(window.addEventListener) { // Standard
		element.addEventListener(type, expression, bubbling);
		return true;
	} else if(window.attachEvent) { // IE
		element.attachEvent('on' + type, expression);
		return true;
	} else return false;
}


$(document).ready(function() {

	// Show Hide
	$("ul.show_hide li a").each(function() {
		$($(this).attr("href")).addClass("show_hide_content");
	});
	$(".show_hide_content").hide();
	$(".show_hide_content:first").show();
	
	$("ul.show_hide li a").click(function() {
		$(".show_hide_content").slideUp();
		$($(this).attr("href")).slideDown();
		return false;
	});
	
});

addListener(window, 'load', addShadows);
addListener(window, 'load', addSlideshows);