$(document).ready(function(){
	setupMenu();
	setupMediaBox();
    boRolodex(-1);
})
var mt;
function setupMenu() {
	$("ul.nav li ul").parent().find("a:first").addClass("drop");
	$("ul.nav li").mouseenter(function() {
		clearTimeout(mt);
		$("ul.subnav").hide();
		$(this).find("ul.subnav").show();
	});	
	$("#nav").mouseleave(function() {
		mt = setTimeout("closeSub()",500);
	});
	$("ul.subnav li").unbind();
}
function closeSub() {
	$("ul.subnav").hide();
}

// Rolodex
var ori=0;var busy=false;
function boRolodex(nr) {	
	$("ul.rolodexholder").each(function() { 
		var id = $(this).attr("id");
		var ih = parseInt($("#"+id+" .rolodexitem").css("height"));
		$(this).find("li.rolodexitem").each(function (i) {				
			var rolotext = $(this).find(".rolotext").text();
			$("#"+id+" li.rolodexitem .rolotitle").eq(i).attr("id",id+i);
			$("#"+id+" li.rolodexitem .rolotitle").eq(i).mouseenter(function() {
				if ($(".rolodexitem:animated").length == 0) { 					
					clearTimeout(busy);
					var currtitle = $(this).attr("id");
					if (!$(this).hasClass("on")) {
						// close all open items
						$("ul.rolodexholder .on").each(function() {
							var oh = parseInt($(this).parent().parent().height())-ih;
							$(this).parent().parent().animate({"height": "-="+oh+"px"}, "slow",function() {
								$(this).css("height","24px");
							});
							$(this).removeClass("on");
						});
						// open current item
						//busy = setTimeout("openRolo('"+currtitle+"',"+ih+")",500);
						openRolo(currtitle,ih)
					}
				}
			});
			// close all items after each item's height has been determained 
			$("#"+id+" li.rolodexitem").eq(i).css("height",ih+"px");		
		});
		// so you didn't see the opened items while page is loading
		$("#"+id).css("visibility","visible");		
		// set initial active item
		ori=$("#"+id+" li.rolodexitem .rolotitle").eq(nr);		
	});	
	boTabList();
}

function openRolo(currtitle,ih) {
	nh = parseInt($("#"+currtitle).parent().find(".rolotext").height())+ih;
	var curritem = $("#"+currtitle).parent().parent();
	// open active item
	$("#"+currtitle).addClass("on");
	curritem.animate({"height": "+="+nh+"px"}, "slow");
}

function boTabList() {
	$(".listtab").hide();
	$(".listtab:first").show();
	$(".dayswitch a").click(function() {
		$(".listtab").show();
		$(this).parent().parent().hide();
	});
}

var rollin;var itemcount = 0; var itemwidth = 0;var lpos = 0;
var myVideos=new Array(); var myImages=new Array();goodtogo = false;
var waiting = 5000;
function setupMediaBox() {
	var nlpos = 0;
	itemcount = $(".mediaitem").length-1;
	itemwidth = $(".mediaitem:first").width();
	$(".mediaitem").each(function(i) {
		var videocode = $(this).find(".video").html();		
		if (jQuery.trim(videocode) != "") {
			$("#thumbs .thumb:eq("+i+")").addClass("vid");
			$(".media:eq("+i+") img").attr("src","/images/mask_video.png").css("cursor","pointer").click(function() {
				clearTimeout(rollin);
				goodtogo = false;
				//$(this).parent().parent().find(".title").hide();
				$(".mediaitem:eq("+i+")").unbind();
				//var videocode = '<object width="460" height="288"><param name="movie" value="'+videocontent+'"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><param name="wmode" value="transparent"></param><embed wmode="transparent" src="'+videocontent+'" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="460" height="288"></embed></object>';
				myVideos.push(videocode);
				var imagecode = $(".media:eq("+i+")").html();
				myImages.push(imagecode);
				$(this).parent().html(videocode).addClass("hasvid");
			});
		}
		$(".mediaitem:eq("+i+")").mouseenter(function() {
			clearTimeout(rollin);
			goodtogo = false;
			hideTitle($(this));			
		});
		$(".mediaitem:eq("+i+")").mouseleave(function() {
			clearTimeout(rollin);
			goodtogo = false;
			showTitle($(this));			
		});
		$("#thumbs .thumb:eq("+i+")").fadeIn("slow").click(function(i) {
			if ($("#strip:animated").length == 0) {
				clearTimeout(rollin);			
				goodtogo = false;
				nlpos = $(this).prevAll().length;
				$(".hasvid").each(function(j) {
					var imagecode = myImages[j];
					$(this).html(imagecode).removeClass("hasvid");
					$(this).parent().find(".title").css("top","0px").show();
					
					$(this).parent().mouseenter(function() {
						clearTimeout(rollin);			
						goodtogo = false;
						hideTitle($(this));	
					});
					$(this).parent().mouseleave(function() {
						showTitle($(this));
					});
					
					$(this).find("img").click(function() {
						clearTimeout(rollin);
						goodtogo = false;
						$(this).parent().parent().find(".title").hide();
						$(this).parent().parent().unbind();
						var videocode = myVideos[j];
						$(this).parent().html(videocode).addClass("hasvid");
					});
					
				});			
				if (nlpos > lpos) {
					goodtogo = false;
					$("#strip").dequeue().fadeTo("fast",0.6).animate({"left": "-="+((nlpos-lpos)*itemwidth)+"px"}, "slow", function() { 
						$(this).fadeTo("fast",1,function() {
							lpos = nlpos;
							$("#thumbs div").removeClass("on");
							$("#thumbs .thumb:eq("+nlpos+")").addClass("on");
							var linkthru = $(".bottomlink:eq(" + nlpos + ")").html();
							/*$("#order a").attr("href",linkthru);*/
							$("#order").html(linkthru);			
							goodtogo = true;
							rollin = setTimeout("rollMedia("+nlpos+")",waiting); 
						});						
					});
				} else if (nlpos < lpos) {
					goodtogo = false;
					$("#strip").dequeue().fadeTo("fast",0.6).animate({"left": "+="+((lpos-nlpos)*itemwidth)+"px"}, "slow", function() { 
						$(this).fadeTo("fast",1,function() {
							lpos = nlpos;
							$("#thumbs div").removeClass("on");
							$("#thumbs .thumb:eq("+nlpos+")").addClass("on");
							var linkthru = $(".bottomlink:eq(" + nlpos + ")").html();
							/*$("#order a").attr("href",linkthru);*/
							$("#order").html(linkthru);				
							goodtogo = true;							
							rollin = setTimeout("rollMedia("+nlpos+")",waiting);
						});						 
					});
				}
			}
		});		
	});
var linkthru = $(".bottomlink:eq(" + nlpos + ")").html();
	/*$("#order a").attr("href",linkthru);*/
	$("#order").html(linkthru);
	$("#thumbs .thumb:eq(0)").addClass("on");
	goodtogo = true;	
	rollin = setTimeout("rollMedia("+0+")",waiting);
}

function rollMedia(clpos) {	
	clearTimeout(rollin);
	if (goodtogo == true && itemcount > 0) {
		goodtogo = false;
		if (clpos < itemcount) {		
			$("#strip").fadeTo("fast",0.6).animate({"left": "-="+itemwidth+"px"}, "slow", function() { 
				$(this).fadeTo("fast",1,function() {
					clpos++;
					lpos = clpos;			
					$("#thumbs div").removeClass("on");
					$("#thumbs .thumb:eq("+clpos+")").addClass("on");
					var linkthru = $(".bottomlink:eq(" + clpos + ")").html();
					/*$("#order a").attr("href",linkthru);*/
					$("#order").html(linkthru);
					goodtogo = true;
					rollin = setTimeout("rollMedia("+clpos+")",waiting); 
				});				
			});
		} else {		
			$("#strip").fadeTo("fast",0.6).animate({"left": "+="+(itemcount*itemwidth)+"px"}, "slow", function() { 
				$(this).fadeTo("fast",1,function() {
					lpos = 0;
					$("#thumbs div").removeClass("on");
					$("#thumbs .thumb:eq("+lpos+")").addClass("on");
					var linkthru = $(".bottomlink:eq(0)").html();
					/*$("#order a").attr("href",linkthru);*/
					$("#order").html(linkthru);				
					goodtogo = true;
					rollin = setTimeout("rollMedia("+lpos+")",waiting);
				});				 
			});
		}			
	} else { return; }
}

function showTitle(obj) {
	if ($("#strip:animated").length == 0) {							
		if (obj.find(".title").css("top") == "-60px") {
			obj.find(".title").dequeue().animate({"top": "+="+60+"px"}, "slow", function() { 					
				goodtogo = true;
				rollin = setTimeout("rollMedia("+lpos+")",1000); 
			});
		}
	}
}

function hideTitle(obj) {
	if ($("#strip:animated").length == 0) {
		obj.find(".title").dequeue().animate({"top": "-="+60+"px"}, "slow");
	}
}

// genres on agenda page
$(document).ready(function () {
    $("ul.genres").delegate("li", "click", function () {
        $(this).find("ul").toggle();
        $(this).toggleClass("on");
        if ($(this).hasClass(".on")) {
            $(this).undelegate();
        } else { $(this).delegate(); }
    });
    $("ul.genres li li").unbind();
})


/* ------------------------------------------------ */
/* Popup */
/* ------------------------------------------------ */
var newwindow;
function popup(url, width, height) {
    var top;
    var left;

    var dependent;
    var directories;
    var fullscreen;
    var location;
    var menubar;
    var resizable;

    var scrollbars;
    var status;
    var toolbar;

    dependent = 'no';
    directories = 'no';
    fullscreen = 'no';
    location = 'no';
    menubar = 'no';
    resizable = 'no';

    scrollbars = 'yes';
    status = 'yes';
    toolbar = 'yes';


    if (windowHeight() > 0) {
        top = windowHeight() / 2 - parseInt(height) / 2;
    }
    if (windowWidth() > 0) {
        left = windowWidth() / 2 - parseInt(width) / 2;
    }

    var properties = 'width=' + width + ',height=' + height + ',top=' + top + ',left=' + left + ',scrollbars=yes';

    newwindow = window.open(url, 'name', properties);
    newwindow.focus();
    return;
}

function windowHeight() {
    var height;
    if (self.innerWidth) {
        height = self.innerHeight;
    }
    else if (document.documentElement && document.documentElement.clientWidth) {
        height = document.documentElement.clientHeight;
    }
    else if (document.body) {
        height = document.body.clientHeight;
    }
    else height = 0;

    //alert(height);
    return height;
}

function windowWidth() {
    var widht;
    if (self.innerWidth) {
        width = self.innerWidth;
    }
    else if (document.documentElement && document.documentElement.clientWidth) {
        width = document.documentElement.clientWidth;
    }
    else if (document.body) {
        width = document.body.clientWidth;
    }
    else width = 0;

    //alert(width);
    return width;
}


// lightview style pop up

$(document).ready(function () {
    $("#bopop").appendTo($('#site'));
})

var pid;
function hidePop() { document.getElementById('bopop').style.visibility = 'hidden'; document.getElementById(pid).style.display = 'none'; ie6Form(); }
function showPop(nid) {
    if (document.getElementById('bopop').style.visibility == 'visible') { hidePop(); }
    getPageSize();
    document.getElementById('fade').style.height = pageHeight;
    document.getElementById(nid).style.display = 'block';
    document.getElementById('bopop').style.visibility = 'visible';
    pid = nid;
    ie6Form();
}
var pageWidth, pageHeight;
function getPageSize() {
    var xScroll, yScroll;
    if (window.innerHeight && window.scrollMaxY) { xScroll = window.innerWidth + window.scrollMaxX; yScroll = window.innerHeight + window.scrollMaxY; }
    else if (document.body.scrollHeight > document.body.offsetHeight) { xScroll = document.body.scrollWidth; yScroll = document.body.scrollHeight; }
    else { xScroll = document.body.offsetWidth; yScroll = document.body.offsetHeight; }
    var windowWidth, windowHeight;
    if (self.innerHeight) {	// all except Explorer
        if (document.documentElement.clientWidth) { windowWidth = document.documentElement.clientWidth; }
        else { windowWidth = self.innerWidth; }
        windowHeight = self.innerHeight;
    }
    else if (document.documentElement && document.documentElement.clientHeight) { windowWidth = document.documentElement.clientWidth; windowHeight = document.documentElement.clientHeight; }
    else if (document.body) { windowWidth = document.body.clientWidth; windowHeight = document.body.clientHeight; }
    if (yScroll < windowHeight) { pageHeight = windowHeight; } else { pageHeight = yScroll; }
    if (xScroll < windowWidth) { pageWidth = xScroll; } else { pageWidth = windowWidth; }
    return [pageWidth, pageHeight];
}

function ie6Form() {
    if (document.all) {
        var version = /MSIE \d+.\d+/; var brwsr = navigator.appVersion.match(version);
        if (("" + brwsr).indexOf("6") > 0) { $("td.page *:input").toggle(); }
    }
}
