/*!
 * Undefine.ca jQuery JavaScript Library version 1.0
 *
 * Development Team: Son Pham
 * http://www.letusdoweb.com/
 *
 * Date: April 17 22:30:00 2010 -0500
 */
var UND = {
	resources: {
		// All labels or texts managed in javascript		
		fr: {
			more: "Suite",
			news: "Nouvelles",
			works: "Oeuvres",
			relatedWorks: "Autres Oeuvres"
		},
		en: {
			more: "More",
			news: "News",
			works: "Works",
			relatedWorks: "Related Works"
		}
	},
	init: function() {
		/*global Cufon */	
		Cufon.replace('h1, h2, h3');
		Cufon.replace('.langItems, .title',{ hover: true });
		Cufon.replace('.langItems > .langItem');
		Cufon.replace('.langItems > .activeLang');
		Cufon.replace('#siteIdentity');
		
		/*global $, window */
		$(".newWindow").live("click", function() {
			window.open($(this).attr("href"));
			return false;
		});
		
		$(".browseMediaJs").live("click", function(e) {
			var $li = $("#mediasList li"),
				idx = $li.index($(e.target).parents("li:first"));
			UND.generateFeatureImage(idx);
			$("#musicPlayer").hide();
			$("#spotLight").show();
			return false;
		});
		
		var $homeLink = $("#homePageLink");
		if ($homeLink.length) {
			if (this.getPageLang() === "fr") {
				$homeLink.attr("href", $homeLink.attr("href")+"?lang=fr");
			}
		}
		
		this.setWindowHeight();
		$(window).resize(function() {
			UND.setWindowHeight();					  
		});
		
		$(".audio").live("click", function() {
			var $link = $(this).find("a");
			$link.click(function() {
				return false;					 
			});
			$("#spotLight").empty().hide();
			$("#musicPlayer").show();
			UND.generateAudio($link);
			$("#audioTrackName").html($link.text());
		});
	},
	getPageLang: function() {
		var lang = "en", htmlLang = $("html").attr("lang");
		if (htmlLang) {
			if (htmlLang.indexOf("en") === -1) {
				lang = "fr";	
			}
		}
		return lang;
	},
	getText: function(resourceKey) {
		var text = "",
			lang = "en";
	
		lang = this.getPageLang();	
		if (typeof(this.resources[lang]) === "object") {
			if (typeof(this.resources[lang][resourceKey]) !== "undefined") {
				text = this.resources[lang][resourceKey];
			}
		}
		return text;
	},
	setNewsLabel: function() {
		$("#news h4").html(this.getText("news"));	
	},
	
    // the slideshow object
	slideShow: {
		delay: 4000,
		speed: 2000,
		slideshow: null,
		slidingBox: null,
		imgRatio: 0,
		prevWidth: 0,
		startTime: 0,
		timeDone: 0,
		resizeTimer: null,
	
		init: function(delay) {
			// set the delay if there's a valid one pass
			delay = parseInt(delay, 10);
			if (!isNaN(delay) && delay > 0) {
				this.delay = delay * 1000;
			}
			
			this.slideshow = $('#slideshow');
			$('#pageWrapper').after(this.slideshow);
			this.slidingBox = $('#slidingBox');
			
			
			// get all the images with their link if they have one
			var images = $('#slidingBox img');
			var goodElements = [];
			
			images.each(function(idx, image) {
				image = $(image);
				var parent = image.parent()[0];
				if (parent && parent.tagName == 'A') {
					var newParent = $(parent).clone().empty();
					image.detach();
					image.wrap(newParent);
					goodElements.push(image.parent());
				} else {
					goodElements.push(image);
					image.detach();
				}
			});
			
			// clear all the rest
			this.slidingBox.empty();
			
			// reput the images/links
			$(goodElements).appendTo(this.slidingBox);
			
			// wrap them in div with class unit
			this.slidingBox.children().wrap('<div class="unit" />');
			
			// remove the title attribute to prevent tooltip
			$('#slidingBox a').removeAttr('title');
			
			// get the aspect ratio from the first image and resize images in function
			if ($('div.unit').length > 1) {
				var imgs = $('div.unit img');
				if (imgs.length > 0) {
					var img = imgs[0];
					$(img).removeAttr("width").removeAttr("height").css({ width: "", height: "" });
					this.imgRatio = img.width / img.height;
// 					$(img).parent().addClass();
					this.resize();
					this.prevWidth = this.slideshow.width();
					
					// bind resize window event
					$(window).resize(function() {
						UND.slideShow.onresize();
					});
					
					// start the slideshow
                    var $loader = $("#logoLoader");
                    $loader.fadeOut(function() {
                        UND.slideShow.slideshow.fadeIn();
                        UND.slideShow.next();
                    });
				}
			}
		},
		
		resize: function() {
			var winRatio = this.slideshow.width() / this.slideshow.height();
			if (this.imgRatio > winRatio) {
				$('div.unit').removeClass('other');
			} else {
				$('div.unit').addClass('other');
			}
		},
		
		onresize: function() {
			// stop the slideshow if it's running and get the animation remaining time
			if (this.startTime !== 0) {
				this.slidingBox.stop();
				this.timeDone = new Date().getTime() - this.startTime;
				this.startTime = 0;
			}
	
			this.resize();
	
			// correct the position in function of the new size
			if (this.timeDone !== 0) {
				this.slidingBox.css('left', this.slidingBox.position().left * this.slideshow.width() / this.prevWidth);
				this.prevWidth = this.slideshow.width();
				
				// for some browser on Mac, continue the animation after each resize iteration
				
			    // For the others, wait 250 ms without resize to continue the animation
				if ((navigator.userAgent.indexOf('Windows') !== -1) || (navigator.userAgent.indexOf('Chrome') !== -1)) {
					clearTimeout(this.resizeTimer);
					this.resizeTimer = setTimeout(function(){
						UND.slideShow.slide();
					}, 250);
				} else {
					this.slide();
				}
			}
		},
	
		slide: function() {
			// get the remaining time
			var delay = this.speed - this.timeDone;
			this.startTime = new Date().getTime();
			// start/continue the animation
			this.slidingBox.animate({"left": "-="+ (this.slideshow.width() + this.slidingBox.position().left) + "px"}, delay, "easeOutQuad", function() {
				UND.slideShow.step();
			});
		},
		
		step: function() {
			// reset the timers
			this.startTime = 0;
			this.timeDone = 0;
			// remove first image and put it at the end
			$($('div.unit')[0]).detach().appendTo(this.slidingBox);
			// reset the slidingBox to the initial position
			this.slidingBox.css('left', 0);
			// program the next animation
			this.next();
		},
		
		next: function() {
			setTimeout(function() {
				UND.slideShow.slide();
			}, this.delay);
		}
	},

	loadHomePage: function(baseURL, imagesList, timer) {
		var html = "<ul id='slideShow'>",
			counter = 0, loadedCounter = 0, zIndex = 10,
			$window = $(window), $img,
			winW = $window.width(), winH = $window.height(),
			docH = $(document).height(), finalH = winH > docH?winH:docH,
			i, style, $slideShow, data, lnk, src;
					
		for (i=0;i<imagesList.length;i++) {
			if (i === 0) { 
				style += ";left: 0px";
			} else {
				style += ";left: "+winW+"px";
			}
			html += "<li id='image_"+i+"' style='"+style+"'>";
			if (imagesList[i].indexOf(";") !== -1) {
				data = imagesList[i].split(";");
				if (data.length > 1) {
					lnk = data[0];
					src = data[1];
					html += "<a href='"+lnk+"'>";
					html += "<img src='"+src+"'/>";
					html += "</a>";
				}			
			} else {
				html += "<img src='"+imagesList[i]+"'/>";	
			}
			html += "</li>";
			counter++;
		}
		
		html += "</ul>";

		if (imagesList.length > 0) {
			$slideShow = $("#slideShow");
			if ($slideShow.length) { $slideShow.remove(); }
			$slideShow = $(html);			
			$("body").append($slideShow).width(winW);
			
			$slideShow.find("img").each(function() {
				$(this).load(function() {
					$img = $(this);
			
					if ($img.height() < finalH) {
						$img.removeAttr("style").height(finalH);
					}
					$img.width(winW);	
					
					loadedCounter++;
					if (counter === loadedCounter) {
						UND.startSlideShow($slideShow, winW, counter, timer);	
					}
				});
			});
		}
	},
	setSlideShowResolution: function() {
		var $window = $(window),
			winW = $window.width(), winH = $window.height(),
			docH = $(document).height(), finalH = winH > docH?winH:docH,
			$slideShow = $("#slideShow"), ratio = 1.6, newDimension, left;
		
		
		if ($slideShow.length) {
			if (winW < 960 && winH < 600) {
				$slideShow.find("img").each(function() {
					$(this).removeAttr("style").removeAttr("height").removeAttr("width");
				});
				$.data($slideShow.get(0),"scrolled",true);
				$("body").removeAttr("style").css("overflow","auto");
			} else if (winW <= 960 && winH > 600) {
				$("body").css("overflow","hidden");
				newDimension = Math.ceil(winH*ratio);

				$slideShow.find("img").each(function() {
					$(this).width(newDimension).height(winH);
				});
				$.data($slideShow.get(0),"scrolled",true);
			} else if (winW > 960 && winH <= 600) {
				$("body").css("overflow","hidden");
				newDimension = Math.ceil(winW/ratio);
				if ($.data($slideShow.get(0),"scrolled")) {
					winW += 20;	
				}
				$slideShow.find("img").each(function() {
					$(this).width(winW).height(newDimension);
				});
			} else {
				$("body").css("overflow","hidden");
				newDimension = Math.ceil(winW/ratio);
				if ($.data($slideShow.get(0),"scrolled")) {
					winW += 20;	
				}
				$slideShow.find("img").each(function() {
					$(this).width(winW).height(newDimension);
				});
			}
			$slideShow.find("img").each(function() {
				left = Math.abs(parseInt($(this).css("left"), 10));						 
				if (left !== winW) {
					$(this).css("left","-"+left+"px");	
				}
			});
		}
		return winW;
	},
	startSlideShow: function($slideShow, width, counter, timer) {
		var $loader = $("#logoLoader");
		$loader.fadeOut(function() {
			$("body").removeClass("init");
			$slideShow.fadeIn(function() {
				$("#pageWrapper").removeAttr("style");						   
			}).css("opacity",1);
			UND.animateSlideShow($slideShow, 0, width, counter, timer);		
		});
	},
	animateSlideShow: function($slideShow, idx, width, counter, timer) {	
		var $childs = $slideShow.children(),
			$li = $childs.eq(idx),
			$liNext, $liPrev, interval;

		idx++;
		
		if (idx>counter-1) {
			idx=0;	
		}

		$liNext = $childs.eq(idx);
		$liNext.css("left",width);

		interval = timer*1000;
		/*global setTimeout */
		setTimeout(function() {
			if (winHasBeenResized) {
				winHasBeenResized = false;
				width = UND.setSlideShowResolution();
			}

			if (idx > 0) {
				$liPrev = $childs.eq(idx-1);
			}
			if ($liPrev) {
				$liPrev.css("zIndex", 1)	
			}
			$li.animate({left: -(width)+"px"}, 2000, "easeOutQuad", function() {			
				UND.animateSlideShow($slideShow, idx, width, counter, timer);
			}).css("zIndex", 0);
			$liNext.animate({left: "0px"}, 2000).css("zIndex", 3);
		}, interval);
	},
	setWindowHeight: function() {
		var $window = $(window);
		if (!$("#home").length) {
            $("#pageWrapper").css("min-height", $window.height()+"px");
        }
	},
	toggleMenu: function() {
		var $subMenu = $("#menu ul.subMenu");
		$("#menu li.menuItem").hover(function() { 
			$subMenu.each(function() {
				$(this).hide();					   
			});
			$("#sub"+$(this).attr("id")).show();
		},function() { 
			$subMenu.hide();
			$("#sub"+$(this).attr("id")).hide(); 
		});
		$('li > span', $subMenu).mouseout(function() {
			$subMenu.hide();										
		});
	},
	toggleFlyOut: function(selector) {
		var $element, $flyOut;
		$(selector).each(function() {
			$element = $(this);
						
			$element.mouseover(function() {
				window.secNavMouseOn = true;

				$flyOut = $("#flyOut1");
					
				$flyOut.show().mouseenter(function() {
					window.secNavMouseOn = true;											 
				});
			}).mouseout(function() {
				window.secNavMouseOn = false;
				setTimeout(function() {
					if (!window.secNavMouseOn) {
						$("#flyOut1").hide();
					}
				}, 500);
			});

		});
	},
	generateLinkInfos: function() {
		var $navSecondar = $("#navigation_secondary"),
			hashId, $menuItems, hash, loadHash, $hash,
			$default, html = "",
			$item, label, menuId, $text, $br,
			$flyOutTitle, $flyOut;
			
		if ($navSecondar.length) {
			$menuItems = $(".menu_item");
			if ($menuItems.length) {
				// detect hash in URL
				/*global location */
				hash = location.hash;
				loadHash = false;
				if (hash.length) {
					$hash = $(hash);
					hashId = $hash.attr("id");
					if ($hash.length) {
						loadHash = true;	
					}
				}
		
				$menuItems.each(function(idx) {
					$item = $(this);
					label = "[link "+idx+"]";
					menuId = "menu-id-"+idx;
					
					if ($item.attr("id") !== "") {
						menuId = $item.attr("id");	
					}
					
					$text = $item.find(".menu_text:first");					
					if ($text.length) {
						label = $text.text();
					}
					
					// delete WP generated BR
					$br = $($text).next();
					if ($br) {
						if ($br.get(0)) {
							if ($br.get(0).nodeName	=== "BR") {
								$br.remove();
							}
						}
					}
					
					html += "<li><a href='#"+menuId+"'>"+label+"<\/a><\/li>";
					
					// default content
					if ($item.hasClass("menu_item_default") && !loadHash) {
						$item.show();
						$default = $item;							
						var defaultItem = $item.find(".menu_text:first");
						if (defaultItem.length) {
							defaultItem.hide();
							$default = null;
						} 
					} else if (hashId == menuId  && loadHash) {
						$item.show();
						$default = $item;
					}
				});

				$navSecondar.prepend(html);
				
				$flyOutTitle = $("#flyOutTitle");
				$flyOut = $("#flyOut1");
				
				// set heading
				$flyOutTitle.html($("li:first", $flyOut).text());
				
				// set default selected item
				if ($default) {
					$("a[href='#"+$default.attr("id")+"']", $flyOut).addClass("active");
				}

				
				if ($("#flyOut1 > li").length === 1) {
					$flyOutTitle.addClass("flyOutSingle");
					$(".flyOut a").unbind("mouseover");
				} else {
					// bind click menu items
					$("a", $navSecondar).live("click", function() {
						var $lnk = $(this), $flyOut = $("#flyOut1"),
							contentId, $content;
						
						$("a", $navSecondar).each(function() {
							$(this).removeClass("active");						   
						});
						$lnk.addClass("active");
						
						$menuItems.each(function() {
							var lnkId = $lnk.attr("href").split("#")[1];
							if (lnkId !== "") {
								contentId = "#"+lnkId;
								$content = $(contentId);
								if ($content.length) {
									if ($(this).attr("id") === lnkId) {
										// update selected item label
										$flyOutTitle.text($lnk.text());
										
										if (lnkId === "works") {
											if ($("#works li").length>2) {
												$("#content").addClass("showWorksJs");	
											} else {
												$("#content").removeClass("showWorksJs");
											}
										} else {
											$("#content").removeClass("showWorksJs");
										}
										$content.show();
										$flyOut.fadeOut();
									} else {
										$(this).hide();
									}
								}
							}
						});
						
						return false;																   
					});			
				}
				if ($("#pageWrapper").height() < $(window).height()) {
					this.setContentHeight();
				}
			} 
		} 
	},
	addWorksToSubMenu: function(label) {
		var $flyOut = $("#navigation_secondary"), $works;
		
		this.generateLinkInfos();
		this.toggleFlyOut(".flyOut");
	
		if ($flyOut.length) {			
			// add link
			$flyOut.children().eq(0).after("<li class='itemWorksJs'><a href='#works'>"+label+"</a></li>");
			// remove auto generated link
			$works = $flyOut.find("li:last");
			if ($works.html().indexOf("works") !== -1) {
				$works.remove();
			}
			this.getSubMenuWidth();
		}
	},
	getSubMenuWidth: function() {
		var $subMenu = $("#navigation_secondary"), width = 0,
			moreItems = [], toAddMore = false, $content = $("#contentPrimary"),
			defaultWidth = 420, html;
		
		if ($subMenu.length && $content.length) {
			if ($content.width() > 0) {
				defaultWidth = $content.width();
			}
			$subMenu.children().each(function() {				
				width += $(this).width();
				if (width+125>defaultWidth) {
					toAddMore = true;
					moreItems.push($(this));
					$(this).remove();
				}
			});
		}
		if (toAddMore) {
			html = "<li id='sub-menu1' class='flyOut'>\n";
			html += "<a id='flyOutTitle' href='#flyOut1'>"+this.getText("more")+"<\/a>\n";
			html += "<ul id='flyOut1'>\n";
			$.each(moreItems, function(idx, value) {
				html += $("<div></div>").append(value).html();							   
			});
			html += "</ul>";
			$subMenu.append(html);
			this.toggleFlyOut("#flyOutTitle");

			$subMenu.children(":not(:last)").mouseover(function() {
				$("#flyOut1").hide();													 
			});
		}
	},
	buildMediaPlayer: function() {
		var i, html = "",
			hasMedia = false,
			hasAudio = false,
			$medias = $("#medias"),
			videos, head, url, clipsJs,
			audios, lnk, audioId, $nav, ul;
			
		window.mediaLI = "";
		
		if (window.imagesList) {
			if (window.imagesList.length > 0) {
				hasMedia = true;
				for (i=0;i<window.imagesList.length;i++) {							
					window.mediaLI += "<li class='image'>"+window.imagesList[i]+"</li>";
				}
			}
		}
		if (window.videosList) {
			if (window.videosList.length > 0) {
				hasMedia = true;
				videos = window.videosList;

				/*global document */
				head = document.getElementsByTagName('head').item(0);

				for (i=0;i<videos.length;i++) {
					url = "http://vimeo.com/api/v2/video/";
					url += videos[i]+".json?callback=UND.addVideo";
					clipsJs = document.createElement('script');
					clipsJs.setAttribute('type', 'text/javascript');
					clipsJs.setAttribute('src', url);
					head.appendChild(clipsJs);
					window.mediaLI += "<li id='"+videos[i]+"' class='vimeo'><a href='http://vimeo.com/"+videos[i]+"'  class='browseMediaJs' rel='"+videos[i]+"'></a></li>";
				}
			}
		}
		if (window.audiosList) {
			if (window.audiosList.length > 0) {
				hasMedia = true;
				hasAudio = true;
				audios = window.audiosList;
								
				for (i=0;i<audios.length;i++) {
					audioId = $.getRandomNumber(3);
					lnk = audios[i].replace(/audioId/, audioId);
					window.mediaLI += "<li class='icons audio'>"+lnk+"</li>";					
				}
			}
		}
		
		if (hasMedia) {
			ul = "<ul id='mediasList' class='page_gallery'>";
			ul += window.mediaLI;
			ul += "</ul>";
		
			$nav = $("<div id='mediasNavigation'></div>");
			$nav.append(ul);
			
			$medias.append($nav);
			this.generateMediasNavigation(true);
			
			window.mediasTimer = setTimeout(function() {
				UND.generateFeatureImage();
				/*global clearTimeout */
				clearTimeout(window.mediasTimer);
			}, 10);
		}
		
		if (hasAudio) {
			$(".audio a", $medias).each(function() {
				$(this).parent().attr("title", $(this).text());
			});
		}
	},
	generateMediasNavigation: function(isImages) {
		var $mediasList,
			$items,
			$nav,
			itemsPerPage = 5, itemWidth, nbPages;

	
		if (isImages) {
				$mediasList = $("#mediasList");
				$items = $("li", $mediasList);
				$nav = $("#mediasNavigation");

			if ($items.length > itemsPerPage) {
				$nav.addClass("icons sliderMode disableLeft").append("<a class='mediasNav navLeftJs'>slide left</a><a class='mediasNav navRightJs'>slide right</a>");
				if ($mediasList.parent().attr("id") !== "mediasMask") {
					$mediasList.wrap("<div id='mediasMask'></div>");
				}
				itemWidth = $mediasList.children(":first").outerWidth(true);
				$mediasList.width(itemWidth*$items.length);
				nbPages = Math.ceil($items.length/itemsPerPage);
				$.data($nav.get(0),"currentPage", 1);
				
				this.bindMediasNavigation({nav:$nav, mediasList:$mediasList,items:$items, itemWidth:itemWidth, nbPages:nbPages, itemsPerPage:itemsPerPage});
				
			} else {
				$nav.removeClass("icons").removeClass("sliderMode");
				$(".mediasNav", $nav).remove();
			}
		} else {
			$mediasList = $("#audioTracks");
			$items = $("li", $mediasList);
			$nav = $("#audioNavigation");

			if ($items.length > itemsPerPage) {
				$nav.addClass("icons sliderMode disableLeft").append("<a class='mediasNav navLeftJs'>slide left</a><a class='mediasNav navRightJs'>slide right</a>");
				if ($mediasList.parent().attr("id") !== "audioMask") {
					$mediasList.wrap("<div id='audioMask'></div>");
				}
				itemWidth = $mediasList.children(":first").outerWidth(true);
				$mediasList.width(itemWidth*$items.length);
				nbPages = Math.ceil($items.length/itemsPerPage);
				$.data($nav.get(0),"currentPage", 1);
				
				this.bindMediasNavigation({nav:$nav, mediasList:$mediasList,items:$items, itemWidth:itemWidth, nbPages:nbPages, itemsPerPage:itemsPerPage});
				
			} else {
				$nav.removeClass("icons").removeClass("sliderMode");
				$(".mediasNav", $nav).remove();
			}
		}
	},
	bindMediasNavigation: function(params) {
		var currentPage = $.data(params.nav.get(0),"currentPage"),
			$navLeft = params.nav.find(".navLeftJs"),
			$navRight = params.nav.find(".navRightJs");
		
		$navLeft.unbind("click").click(function() {
			currentPage--;
			UND.showMediasNavigationPage({params:params, navLeft:$navLeft, navRight:$navRight, pageToGo:currentPage});
			return false;						
		});

		$navRight.unbind("click").click(function() {
			currentPage++;
			UND.showMediasNavigationPage({params:params, navLeft:$navLeft, navRight:$navRight, pageToGo:currentPage});
			return false;						
		});
	},
	showMediasNavigationPage: function(paramsObject) {
		$.data(paramsObject.params.nav.get(0),"currentPage", paramsObject.pageToGo);
		if (paramsObject.pageToGo === 1 || paramsObject.pageToGo === 0) {
			paramsObject.pageToGo = 1;
			paramsObject.params.nav.addClass("disableLeft").removeClass("disableRight");
			UND.bindMediasNavigation(paramsObject.params);
			paramsObject.navLeft.unbind("click");
		} else if (paramsObject.pageToGo === paramsObject.params.nbPages) {
			paramsObject.params.nav.addClass("disableRight").removeClass("disableLeft");
			UND.bindMediasNavigation(paramsObject.params);
			paramsObject.navRight.unbind("click");
		} else {
			paramsObject.params.nav.removeClass("disableLeft").removeClass("disableRight");
			UND.bindMediasNavigation(paramsObject.params);
		}
		paramsObject.params.mediasList.stop().animate({left:-(paramsObject.pageToGo-1)*paramsObject.params.itemWidth*paramsObject.params.itemsPerPage+"px"});
	},
	addVideo: function(data) {
		var $ul = $("#mediasList"),
			$link = $("#"+data[0].id+" a");
			
		$link.attr("href",data[0].url).append("<img src='"+data[0].thumbnail_small+"' alt=''><span class='icons iconPlay'> </span>");
		
		//$ul.append("<li class='vimeo'><a rel='"+data[0].id+"' href='"+data[0].url+"' class='browseMediaJs'><img src='"+data[0].thumbnail_small+"' alt=''><span class='icons iconPlay'>play</span></a></li>");
		this.generateMediasNavigation();

		window.videosTimer = setTimeout(function() {
			UND.generateFeatureImage();
			clearTimeout(window.videosTimer);
		}, 10);
	},
	generateFeatureImage: function(idx) {
		var $ul = $("#mediasList"),
			$spotLight, $medias, $media,
			$link, mediaType, flashvars, attributes, params, movie,
			title = "", hasCredit = false, html = "";
		
		if ($ul.length) {
			$spotLight = $("#spotLight");
			$spotLight.empty();
			
			$medias = $ul.children();

			if ($medias.length > 0) {
				if (!idx) { idx = 0; }
				
				$media = $medias.eq(idx);
				
				mediaType = $media.get(0).className;
				$link = $("a",$media);
				
				switch(mediaType) {
					case "image":
						if ($link.attr("title")) {
							title = $link.attr("title");
							hasCredit = true;
						}
						html = "<a href='"+$link.attr("href")+"' class='newWindow'><img src='"+$link.attr("rel")+"' alt='' title='"+$link.attr("title")+"' /></a>";
					
						if (hasCredit) {
							html += "<br>" + title;		
						}
						
						$spotLight.prepend(html);
						
						break;
						
					case "vimeo":
						$spotLight.append("<div id='phVimeo'></div>");
						
						flashvars = {};
						attributes = {};
						params = {
							wmode: "transparent",
							allowScriptAccess: "always"				
						};
	
						movie = "http://vimeo.com/moogaloop.swf?clip_id="+$link.attr("rel")+"&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=1";
	
						params.align = "middle";
						params.play = false;
						params.loop = true;
						params.scale = "showall";
						params.devicefont = false;
						params.id = "phVimeo";
						params.bgcolor = "#ffffff";
						params.name = "phVimeo";
						params.menu = true;
						params.allowFullScreen = true;
						params.movie = movie;
						/*global swfobject*/
						swfobject.embedSWF(movie, "phVimeo", 420, 315, "9", "/wp-content/themes/sandbox/expressinstall.swf", flashvars, params, attributes);
	
						break;
				}
				if ($medias.length === 1) {
					$ul.remove();	
				}
			} 
		}
	},
	generateAudio: function($audio) {
		var fWidth = 290,
			fHeight = 24,
			mp3 = $audio.attr("href"),
			elementId = "playerHolder",
			flashvars = {
				playerID:elementId,
				soundFile: mp3
			},
			movie = "http://www.undefine.ca/wp-content/themes/sandbox/swf/player-mp3.swf",
			attributes = {},
			params = {
				wmode: "transparent",
				width:  fWidth,
				height: fHeight,
				movie: movie
			};

		swfobject.embedSWF(movie, elementId, fWidth, fHeight, "9", "/wp-content/themes/sandbox/expressinstall.swf", flashvars, params, attributes);
	
	},
	setContentHeight: function() {
		var $item, itemH, tallest = 0;

		$(".leftHalf .menu_item").each(function() {
			$item = $(this);
			$item.show();
			itemH = $item.height();
			if (itemH>tallest) {
				tallest = $item.height();
			}
			if (!$item.hasClass("menu_item_default")) {
				$item.hide();
			}
		});
		
		$(".leftHalf .menu_item").each(function() {
			$(this).height(tallest);
		});			
	}
};
$(function() {
	UND.init();		   
});
//*****************************************************
//  jQuery function: $.getRandomNumber()
//
//	developed by: Son Pham
//
//  description: will generate a random number (defaults 1-999)
//
//	scenario: You need to create unique random ID
//
//	Requirement: jQuery 1.3.2+
//
//	usage:  1) $.getRandomNumber(); // 1-999
//			2) $.getRandomNumber(5); // 1-99999
//
//	params:
//	@1 numberLength (integer): number range
//*****************************************************	
/*global jQuery */
jQuery.getRandomNumber = function(numberLength) {
	if (isNaN(numberLength)) { numberLength = 3; }
	var strLength = "1";
	for (var i=0; i<numberLength; i++) {
		strLength += i;
	}
	return Math.floor(Math.random()*parseInt(strLength,10));
};
