var DDSPEED = 10;
var DDTIMER = 15;
var OFFSET = -2;
var ZINT = 100;

function ddMenu(id,d){
  var h = document.getElementById(id + '-ddheader');
  var c = document.getElementById(id + '-ddcontent');
  clearInterval(c.timer);
  if(d == 1){
    clearTimeout(h.timer);
    c.style.display = 'block';
    if(c.maxh && c.maxh <= c.offsetHeight){return}
    else if(!c.maxh){
      c.style.left = (h.offsetWidth + OFFSET) + 'px';
      c.style.height = 'auto';
      c.maxh = c.offsetHeight;
      c.style.height = '0px';
    }
    ZINT = ZINT + 1;
    c.style.zIndex = ZINT;
    c.timer = setInterval(function(){ddSlide(c,1)},DDTIMER);
  }else{
    h.timer = setTimeout(function(){ddCollapse(c)},50);
  }
}

function ddCollapse(c){
  c.timer = setInterval(function(){ddSlide(c,-1)},DDTIMER);
}

function cancelHide(id){
  var h = document.getElementById(id + '-ddheader');
  var c = document.getElementById(id + '-ddcontent');
  clearTimeout(h.timer);
  clearInterval(c.timer);
  if(c.offsetHeight < c.maxh){
    c.timer = setInterval(function(){ddSlide(c,1)},DDTIMER);
  }
}

function ddSlide(c,d){
  var currh = c.offsetHeight;
  var dist;
  if(d == 1){
    dist = Math.round((c.maxh - currh) / DDSPEED);
  }else{
    dist = Math.round(currh / DDSPEED);
  }
  if(dist <= 1 && d == 1){
    dist = 1;
  }
  c.style.height = currh + (dist * d) + 'px';
  c.style.opacity = currh / c.maxh;
  c.style.filter = 'alpha(opacity=' + (currh * 100 / c.maxh) + ')';
  if(currh > (c.maxh - 2) && d == 1){
    clearInterval(c.timer);
  }else if(dist < 1 && d != 1){
    clearInterval(c.timer);
    c.style.display = 'none';
  }
}

// jQuery Slideshow
(function($) {
	$.fn.mainSimpleSlideShow = function(options) {

		var settings = {
			// user editable settings
			autoRun: false,
			delay: 6000,
			dissolve: 70,
			showCaptions: true,
			centerImage: true
		};

		var sys = {
			// function parameters
			version: '2.1.1',
			elem: null,
			slides: [],
			captions: [],
			current: 0,
			timer: null,
			pause: false
		};

		/* 
			sets a slides opacity
		***************************************/
		function setOpacity(o) {
			if (parseFloat(o.t) > 1) {
				o.t = 1.0;
				return;
			}
			$(o).css({
				"opacity": parseFloat(o.t),
				"MozOpacity": parseFloat(o.t),
				"filter": "alpha(opacity=" + (parseFloat(o.t) * 100) + ")"
			});
		}

		/* 
			sets the slide and handles dissolves
		***************************************/
		function setSlide() {
			var o = parseFloat(sys.slides[sys.current].t);
			var x = sys.slides[sys.current + 1] ? sys.current + 1 : 0;
			var no = parseFloat(sys.slides[x].t);
			o -= 0.05;
			no += 0.05;
			$(sys.slides[x]).css({
				"display": "block"
			});
			sys.slides[sys.current].t = parseFloat(o);
			sys.slides[x].t = parseFloat(no);
			setOpacity(sys.slides[sys.current]);
			setOpacity(sys.slides[x]);
			if (sys.captions[sys.current] && sys.captions[x]) {
				$(sys.captions[x]).css({
					"display": "block"
				});
				sys.captions[sys.current].t = parseFloat(o);
				sys.captions[x].t = parseFloat(no);
				setOpacity(sys.captions[sys.current]);
				setOpacity(sys.captions[x]);
			}
			if (o <= 0) {
				$(sys.slides[sys.current]).css({
					"display": "none"
				});
				if (sys.captions[x]) {
					$(sys.captions[x]).css({
						"display": "block"
					});
					$(sys.captions[sys.current]).css({
						"display": "none"
					});
				}
				sys.current = x;
				if (!sys.pause) {
					sys.timer = setTimeout(setSlide, settings.delay);
				}
			} else {
				if (!sys.pause) {
					sys.timer = setTimeout(setSlide, settings.dissolve);
				}
			}
		}

		/* 
			pauses the slideshow
		***************************************/
		/*function pause() {
			sys.pause = true;
			if (sys.timer) {
				clearTimeout(sys.timer);
				sys.timer = null;
			}
			$(sys.elem).find("div.main_slideshow_pause_wrapper").fadeIn("fast");
		}

		/* 
			resumes the slideshow
		***************************************/
		/*function resume() {
			sys.pause = false;
			$(sys.elem).find("div.main_slideshow_pause_wrapper").fadeOut("fast");
			sys.timer = setTimeout(setSlide, 0);
		}*/

		/* 
			initialize the slideshow & slides
		***************************************/
		function init() {

			// need to make sure the positioning is set for the main container.
			// it needs to be set to anything other than STATIC
			if ($(sys.elem).css("position") === "" || $(sys.elem).css("position") === "static") {
				$(sys.elem).css("position", "relative");
			}

			// create our main slideshow wrapper
			var i, wrapper = $("<div>").css({
				"display": "block",
				"width": "0px",
				"height": "0px",
				"position": "absolute",
				"overflow": "hidden",
				"cursor": "pointer"
			}).addClass("main_slideshow_wrapper"),
			pauseBlock = $("<div>").css({
				"display": "none",
				"position": "absolute",
				"z-index": "10"
			//}).html("Paused").addClass("main_slideshow_pause_wrapper");
			}).addClass("main_slideshow_pause_wrapper");

			// loop through the images and initialize some settings
			$(sys.elem).find("img").each(function() {
				sys.slides.push($(this).get(0));
			}).css({
				"position": "absolute",
				"top": "0px",
				"left": "0px"
			}).wrapAll($(wrapper));
			/*$(sys.elem).find("div.main_slideshow_wrapper").append(pauseBlock).bind("mouseenter", function() {
				pause();
			}).bind("mouseleave", function() {
				resume();
			});*/
			$(sys.elem).find("br").css({
				"display": "none"
			});

			$(sys.elem).css({
				"display": "block"
			});

			// center the image? we need the images to have the display set to none in order to start the transitions
			// but in order to get the width and height in Internet Explorer, they need to be visible... (or display=block)
			$(sys.elem).find("img").each(function() {
				var i = this;
				if (settings.centerImage) {
					$(i).css({
						"display": "none",
						"top": settings.centerImage ? parseInt(($(sys.elem).height() - $(i).attr("height")) / 2, 10) + "px": "0px",
						"left": settings.centerImage ? parseInt(($(sys.elem).width() - $(i).attr("width")) / 2, 10) + "px": "0px"
					});
				} else {
					$(i).css({
						"display": "none"
					});
				}
			});

			// captions?
			for (i = 0; i < sys.slides.length; i++) {
				sys.slides[i].t = 0.0;
				if (settings.showCaptions) {
					if ($(sys.slides[i]).attr("alt") !== "") {
						var cap = $("<span>").css({
							"position": "absolute",
							"display": "none",
							"width": "100%",
							"height": "auto"
						}).addClass("main_slideshow_caption_wrapper").html($(sys.slides[i]).attr("alt"));
						$(sys.slides[i]).after(cap);
						sys.captions.push($(sys.slides[i]).next("span"));
						sys.captions[i].t = 0.0;
					} else {
						sys.captions.push(null);
						sys.captions[i].t = 0.0;
					}
				}
			}

			// make the first slide and/or caption visisable
			if (sys.slides.length > 0) {
				$(sys.slides[0]).css({
					"display": "block"
				});
				sys.slides[0].t = 1.0;
				if (sys.captions[0]) {
					$(sys.captions[0]).css({
						"display": "block"
					});
				}
				// show our main elem, if it was hidden
				$(sys.elem).find(".main_slideshow_wrapper").css({
					"width": $(sys.elem).width(),
					"height": $(sys.elem).height()
				});
			}

		}

		function start() {
			sys.timer = setTimeout(setSlide, settings.delay);
		}

		/* 
			set up any user passed variables
		***************************************/
		if (options) {
			$.extend(settings, options);
		}

		/* 
			begin
		***************************************/
		return this.each(function() {
			sys.elem = this;
			init();
			if (settings.autoRun) {
				start();
			}
		});

	};
	
	//Hover Intent
		$.fn.hoverIntent = function (f,g) {
		var cfg = {
			sensitivity: 7,
			interval: 100,
			timeout: 0
		};
		
		cfg = $.extend (cfg,g? {
			over: f,
			out: g
			}
			:f
		);
		
		var cX,cY,pX,pY;
		var track = function (ev) {
			cX = ev.pageX;
			cY = ev.pageY;
		};
		
		var compare = function (ev,ob) {
			ob.hoverIntent_t = clearTimeout (ob.hoverIntent_t);
			if((Math.abs(pX-cX)+Math.abs(pY-cY))<cfg.sensitivity){
				$(ob).unbind("mousemove",track);
				ob.hoverIntent_s = 1;return cfg.over.apply(ob,[ev]);
				} else {
					pX = cX;
					pY = cY;
					ob.hoverIntent_t = setTimeout (function(){
						compare(ev,ob);},
						cfg.interval);}
		};
		
		var delay = function (ev,ob) {
			ob.hoverIntent_t = clearTimeout (ob.hoverIntent_t);
			ob.hoverIntent_s = 0;
			return cfg.out.apply(ob,[ev]);
		};
		
		var handleHover = function(e) {
			var p = (e.type=="mouseover"?e.fromElement:e.toElement)||e.relatedTarget;while(p&&p!=this) {
				try{p=p.parentNode;}catch(e){p=this;}
				} if(p==this){return false;}
				var ev = jQuery.extend({},e);
				var ob = this;
				if (ob.hoverIntent_t){
					ob.hoverIntent_t = clearTimeout(ob.hoverIntent_t);
					} if (e.type=="mouseover"){
						pX = ev.pageX;
						pY = ev.pageY;
						$(ob).bind("mousemove",track);
						if (ob.hoverIntent_s!=1){
							ob.hoverIntent_t = setTimeout(function(){
								compare(ev,ob);
							},
							cfg.interval);
						}
					} else {
						$(ob).unbind("mousemove",track);
						if (ob.hoverIntent_s==1){
							ob.hoverIntent_t = setTimeout(function(){
								delay(ev,ob);
							},
							cfg.timeout);
						}
					}
				};
				return this.mouseover(handleHover).mouseout(handleHover);
			};
		}
	)

(jQuery);


// IE5.5+ PNG Alpha Fix v2.0 Alpha: Background Tiling Support
// (c) 2008-2009 Angus Turnbull http://www.twinhelix.com

// This is licensed under the GNU LGPL, version 2.1 or later.
// For details, see: http://creativecommons.org/licenses/LGPL/2.1/

var IEPNGFix = window.IEPNGFix || {};

IEPNGFix.tileBG = function(elm, pngSrc, ready) {
	// Params: A reference to a DOM element, the PNG src file pathname, and a
	// hidden "ready-to-run" passed when called back after image preloading.

	var data = this.data[elm.uniqueID],
		elmW = Math.max(elm.clientWidth, elm.scrollWidth),
		elmH = Math.max(elm.clientHeight, elm.scrollHeight),
		bgX = elm.currentStyle.backgroundPositionX,
		bgY = elm.currentStyle.backgroundPositionY,
		bgR = elm.currentStyle.backgroundRepeat;

	// Cache of DIVs created per element, and image preloader/data.
	if (!data.tiles) {
		data.tiles = {
			elm: elm,
			src: '',
			cache: [],
			img: new Image(),
			old: {}
		};
	}
	var tiles = data.tiles,
		pngW = tiles.img.width,
		pngH = tiles.img.height;

	if (pngSrc) {
		if (!ready && pngSrc != tiles.src) {
			// New image? Preload it with a callback to detect dimensions.
			tiles.img.onload = function() {
				this.onload = null;
				IEPNGFix.tileBG(elm, pngSrc, 1);
			};
			return tiles.img.src = pngSrc;
		}
	} else {
		// No image?
		if (tiles.src) ready = 1;
		pngW = pngH = 0;
	}
	tiles.src = pngSrc;

	if (!ready && elmW == tiles.old.w && elmH == tiles.old.h &&
		bgX == tiles.old.x && bgY == tiles.old.y && bgR == tiles.old.r) {
		return;
	}

	// Convert English and percentage positions to pixels.
	var pos = {
			top: '0%',
			left: '0%',
			center: '50%',
			bottom: '100%',
			right: '100%'
		},
		x,
		y,
		pc;
	x = pos[bgX] || bgX;
	y = pos[bgY] || bgY;
	if (pc = x.match(/(\d+)%/)) {
		x = Math.round((elmW - pngW) * (parseInt(pc[1]) / 100));
	}
	if (pc = y.match(/(\d+)%/)) {
		y = Math.round((elmH - pngH) * (parseInt(pc[1]) / 100));
	}
	x = parseInt(x);
	y = parseInt(y);

	// Handle backgroundRepeat.
	var repeatX = { 'repeat': 1, 'repeat-x': 1 }[bgR],
		repeatY = { 'repeat': 1, 'repeat-y': 1 }[bgR];
	if (repeatX) {
		x %= pngW;
		if (x > 0) x -= pngW;
	}
	if (repeatY) {
		y %= pngH;
		if (y > 0) y -= pngH;
	}

	// Go!
	this.hook.enabled = 0;
	if (!({ relative: 1, absolute: 1 }[elm.currentStyle.position])) {
		elm.style.position = 'relative';
	}
	var count = 0,
		xPos,
		maxX = repeatX ? elmW : x + 0.1,
		yPos,
		maxY = repeatY ? elmH : y + 0.1,
		d,
		s,
		isNew;
	if (pngW && pngH) {
		for (xPos = x; xPos < maxX; xPos += pngW) {
			for (yPos = y; yPos < maxY; yPos += pngH) {
				isNew = 0;
				if (!tiles.cache[count]) {
					tiles.cache[count] = document.createElement('div');
					isNew = 1;
				}
				var clipR = Math.max(0, xPos + pngW > elmW ? elmW - xPos : pngW),
					clipB = Math.max(0, yPos + pngH > elmH ? elmH - yPos : pngH);
				d = tiles.cache[count];
				s = d.style;
				s.behavior = 'none';
				s.left = (xPos - parseInt(elm.currentStyle.paddingLeft)) + 'px';
				s.top = yPos + 'px';
				s.width = clipR + 'px';
				s.height = clipB + 'px';
				s.clip = 'rect(' +
					(yPos < 0 ? 0 - yPos : 0) + 'px,' +
					clipR + 'px,' +
					clipB + 'px,' +
					(xPos < 0 ? 0 - xPos : 0) + 'px)';
				s.display = 'block';
				if (isNew) {
					s.position = 'absolute';
					s.zIndex = -999;
					if (elm.firstChild) {
						elm.insertBefore(d, elm.firstChild);
					} else {
						elm.appendChild(d);
					}
				}
				this.fix(d, pngSrc, 0);
				count++;
			}
		}
	}
	while (count < tiles.cache.length) {
		this.fix(tiles.cache[count], '', 0);
		tiles.cache[count++].style.display = 'none';
	}

	this.hook.enabled = 1;

	// Cache so updates are infrequent.
	tiles.old = {
		w: elmW,
		h: elmH,
		x: bgX,
		y: bgY,
		r: bgR
	};
};


IEPNGFix.update = function() {
	// Update all PNG backgrounds.
	for (var i in IEPNGFix.data) {
		var t = IEPNGFix.data[i].tiles;
		if (t && t.elm && t.src) {
			IEPNGFix.tileBG(t.elm, t.src);
		}
	}
};
IEPNGFix.update.timer = 0;

if (window.attachEvent && !window.opera) {
	window.attachEvent('onresize', function() {
		clearTimeout(IEPNGFix.update.timer);
		IEPNGFix.update.timer = setTimeout(IEPNGFix.update, 100);
	});
}

