var skinwrap = function($j) {
window.skin = (function($) {
var DEPENDENCIES = {
	tooltip: { js: ["resources/javascript/jquery.tooltip.pack.js"] },
	flash: { js: ["resources/javascript/swfobject.js"] },
	menu: { js: ["resources/javascript/columnizer.js"] }
};

var jsInit = false;
var jQuery = $;

var baseUrl = '';
var clientIds = null;
var clientControls = null;

var animatedLogo = false;
var windowLoaded = false;
var flashLoaded = false;

$(window).load(windowLoad);

var fn = {
	init: function(url, ids, isHome) {
		baseUrl = url;
		clientIds = ids;
		animatedLogo = isHome;
		initDependencies();
		initClientControls();
		if (jsInit)
			initPage();
		jsInit = true;
	},
	flashInit: logo_flashInit,
	flashCall: logo_flashCall
};

$(function() {
	if (jsInit)
		initPage();
	jsInit = true;
});

function initDependencies() {
    for (var i in DEPENDENCIES) {
        for (var j in DEPENDENCIES[i]) {
            if (!isArray(DEPENDENCIES[i][j])) {
                DEPENDENCIES[i][j] = resolveUrl(DEPENDENCIES[i][j]);
            }
            else {
                for (var k = 0; k < DEPENDENCIES[i][j].length; k++)
                    DEPENDENCIES[i][j][k] = resolveUrl(DEPENDENCIES[i][j][k]);
            }
        }
    }
}

function initClientControls() {
	if (clientIds != null)
	{
	    clientControls = {};
	    $.each(clientIds, function(i, n) {
	        clientControls[i] = $("#" + n);
	    });
	}
}

function initPage() {
	initLogo();
	
	jsLoader.load({
				key: "columns",
				append: "jQuery",
				url: DEPENDENCIES.menu.js[0],
				loadCheck: "jQuery.fn.makeacolumnlists",
				complete: function() {
					initMenu();
				}
			})
	//initMenu();
	
	
	initVideoList();
	/*ensure(DEPENDENCIES.tooltip, function() {
		$("#membersOnly").tooltip({ track: true, delay: 0, showURL: false, fade: 200, left: 0 });
	});*/
}

$(document).keydown(keypress);
var keys = [65, 83, 83, 79];
var currentkey = 0;
var keytimeout = null;
function keypress(e) {
	clearTimeout(keytimeout);
	if (e.keyCode == keys[currentkey]) {
		currentkey++;
		if (currentkey == keys.length) {
			showLogin();
			currentkey = 0;
		}
		else {
			keytimeout = setTimeout(function() { currentkey = 0; }, 5000);
		}
	}
	else {
		currentkey = 0;
	}
}
	
function showLogin() {
	$("#loginWrap").toggle("normal");
}

function initMenu() {
	var timer = null;
	var mb = $("<li id='menubutton'><div class='menubuttonright'><div class='menubuttonleft'><div class='menubuttonmid'></div></div></div></li>").appendTo("ul#MainMenu");
	var mol = $("ul#MainMenu").offset().left;
	var curMenuItem = null;
	var curPage = $("#MainMenuCurrentItem");

	revertMenu();
	$("ul#MainMenu").addClass("enhanced");
	$("ul#MainMenu > li:not(#menubutton)").hover(function() { onOver(this, true); }, function() { onOut(this, true); });
	$("ul#MainMenu ul li").hover(function() { onOver(this, false); }, function() { onOut(this, false); });
	
	//$("ul ul ul").makeacolumnlists();

	function onOver(item, topLevel) {
		clearTimeout(timer);
		var m = $(item);
		var sameItem = m.length && curMenuItem != null && (m.length == curMenuItem.length) && (m.get(0) == curMenuItem.get(0));
		if (topLevel && !sameItem) {
			animateButton(m);
		}
		var sm = $("ul:first", m).stop(true, true);
		sm.slideDown(150);
	}

	function onOut(item, topLevel) {
		var m = $(item);
		$("ul", m).stop(true, true).slideUp(150);
		var sameItem = m.length && curPage != null && (m.length == curPage.length) && (m.get(0) == curPage.get(0));
		if (topLevel && !sameItem) {
			clearTimeout(timer);
			timer = setTimeout(revertMenu, 3000);
		}
	}

	function revertMenu() {
		var item = curPage;
		if (item.parents("li:last").length == 1)
		{
			item = item.parents("li:last");
		}
		if (item.length == 0) {
			item = $("ul#MainMenu > li").filter(":first");
		}
		animateButton(item);
	}

	function animateButton(menuItem) {
		curMenuItem = menuItem;
		var l = menuItem.offset().left;
		var w = menuItem.outerWidth();
		var ml = mb.offset().left;
		var i = 5;
		var k = '-=5px';
		if (ml > l) { i = -5; k = '+=5px'; }
		mb.stop(true, true).animate({ left: l-mol+i, width: w }, 125).animate({ left: k }, 100);
	}
}

function initVideoList() {
	var timer = null;
	var videos = $("div#videos");
	var vl = $("ul#videolist", videos);
	var vdw = $('<div id="videodescriptionwrap"><div id="videoprev"></div><div id="videonext"></div><div id="videodescription"></div></div>');
	var vd = $("#videodescription", vdw);
	var vp = $("#videoprev", vdw);
	var vn = $("#videonext", vdw);
	videos.after(vdw);
	var cv = vl.find("li").hover(function() {
		stopVideoLoop();
		changeVideo(this);
	}, function() {
		startVideoLoop();
	}).filter(":lt(2)").addClass("visible").filter(":first").addClass("selected");
	vd.text($("> a", cv).attr("title"));
	
	startVideoLoop();
	
	vp.click(previousSet);
	vn.click(nextSet);
	
	function changeVideo(video) {
		var v = $(video);
		if (cv == null || (cv.length && cv.length == v.length && cv.get(0) != v.get(0)))
		{
			cv = v;
			v.addClass("selected").siblings("li").removeClass("selected");
			vd.stop().fadeTo(200, 0, function() {
				$(this).text($("> a", v).attr("title")).fadeTo(200, 1, function() {
					if($.browser.msie)
						this.style.removeAttribute('filter');
				});
			});
		}
	}
	
	function startVideoLoop() {
		timer = setInterval(nextVideo, 10000);
	}
	
	function stopVideoLoop() {
		clearInterval(timer);
	}
	
	function nextVideo() {
		var nv = $("+li", cv);
		if (nv.length > 0) {
			if (nv.attr("class").indexOf("visible") == -1) {
				nextSet();
			}
			else {
				changeVideo(nv);
			}
		}
	}
	
	function nextSet() {
		stopVideoLoop();
		$("li", vl).removeClass("visible").removeClass("selected");
		vl.animate({ left: "-=100%" }, 500, "swing", function() {
			vl.append($("li:lt(2)", this)).css("left", 0);
			changeVideo($("li:lt(2)", this).addClass("visible").filter(":first"));
			startVideoLoop();
		});
	}

	function previousSet() {
		stopVideoLoop();
		var av = $("li", vl).removeClass("visible").removeClass("selected").length;
		vl.prepend($("li:gt(" + (av - 3) + ")", vl).addClass("visible"));
		vl.css("left", "-100%");
		vl.animate({ left: "0" }, 500, "swing", function() {
			changeVideo($("li:first", this));
			startVideoLoop();
		});
	}
}

function initLogo() {
	if (animatedLogo) {
		ensure(DEPENDENCIES.flash, function() {
			if (swfobject.hasFlashPlayerVersion("9.0.0")) {
				var m = resolveUrl("resources/flash/ALPAlogo.swf");
				var att = { data: m, width: "960", height: "100" };
				var par = { quality: "high", wmode: "opaque" };
				var id = "flashlogo";
				var f = swfobject.createSWF(att, par, id);
				window.flashlogo = $("#flashlogo").get(0);
			}
			else {
				logo_fadeOut();
			}
		});
	}
}

function playLogo(play) {
	$("#flashlogo")[0].Play();
}

function windowLoad() {
	windowLoaded = true;
	if (animatedLogo) {
		if (flashLoaded || !animatedLogo) {
			playLogo();
		}
	}
	else {
		logo_showImage();
	}
}

function logo_flashInit() {
	flashLoaded = true;
	if (windowLoaded) {
		playLogo();
	}
}

function logo_flashCall() {
	setTimeout(logo_fadeOut, 1);
}

function logo_fadeOut() {
	$("#imagelogo").fadeIn("slow", function() {
		$("#flashlogowrap").remove();
	});
	swoop_fadeIn();
}

function logo_showImage() {
	$("#imagelogo").show();
	$("#swoop").show();
	$("#flashlogowrap").remove();
}

function image_fadeIn() {
	$("#imagelogo").fadeIn("slow", function() {
		$("#flashlogowrap").remove();
	});
	swoop_fadeIn();
}

function swoop_fadeIn() {
	$("#swoop").fadeIn("slow");
}

function resolveUrl(relativeUrl) {
    var url = relativeUrl;
    if (baseUrl != undefined && baseUrl != null) {
        url = baseUrl + relativeUrl;
    }
    return url;
}

function isArray(obj) {
	if (obj.constructor.toString().indexOf("Array") == -1) {
		return false;
	}
	else {
 		return true;
	}
}

return fn;

})($j);
};