// HEADER FUNCTIONS /////////////////////////////////////
 
var ddm;
$(function() {
/*	$(".mainNav li a .dropDown").mouseover(function() {
//	    $(this).children("ul").css({ left: "0px", top: "34px" });
        ddm = setTimeout(showMainNavDropDown, 1000);
	});
	$(".mainNav li a .dropDown").mouseout(function() {
        clearTimeout(ddm);
	});
*/	
	$(".trayFooter .tab").click(function() {
		if ($(this).hasClass("admin")) {
		    if ($(".trayBody").css("display") == "none") {
		        $(".trayBody .trayContent").hide();
		        $(".trayBody .adminButtons").show();
		        $(".trayBody").slideDown("normal");
		        $(".trayFooter .current span").css("background", "url('/images/tray/arrow_up.gif') no-repeat left 50%");
		    } else {
		        if ($(".trayBody .adminButtons").css("display") == "block") {
		            $(".trayBody").slideUp("normal");
		            $(".trayFooter .current span").css("background", "url('/images/tray/arrow_down.gif') no-repeat left 50%");
		        } else {
		            $(".trayBody .trayContent").hide();
		            $(".trayBody .adminButtons").show("normal");
		        }
		    }
		} else {
		    if ($(".trayBody").css("display") == "none") {
		        $(".trayBody .trayContent #currentUserPlaylist").css("visibility", "hidden");
		        $(".trayBody .trayContent").show();
		        $(".trayBody .adminButtons").hide();
		        $(".trayBody").slideDown("normal", function callback() {
		            $(".trayBody .trayContent #currentUserPlaylist").css("visibility", "visible");
		        });
		        $(".trayFooter .current span").css("background", "url('/images/tray/arrow_up.gif') no-repeat left 50%");
		    } else {
		        if ($(".trayBody .adminButtons").css("display") == "block") {
		            $(".trayBody .trayContent").show("normal");
		            $(".trayBody .adminButtons").hide();
		        } else {
		            $(".trayBody .trayContent #currentUserPlaylist").css("visibility", "hidden");
		            $(".trayBody").slideUp("normal");
		            $(".trayFooter .current span").css("background", "url('/images/tray/arrow_down.gif') no-repeat left 50%");
		        }
		    }
		}
	});

	$(".toggleText").each(function(){
		var txt = $(this).val();
		eval("$(this).focus(function() {if(this.value == \"" + txt + "\"){this.value = \"\";}}).blur(function() {if(this.value == \"\"){this.value = \"" + txt + "\";}});");
	});
		
	
	// TEMP FUNCTION ///////////////////////////////////////
	$(".box1Tabs .tab").click(function() {
		if($(this).attr("class").indexOf("current") == -1)
		{
			$(this).parent().children().removeClass("current");
			$(this).addClass("current");
			
			var parentParent = $(this).parent().parent();
			var count = (
				parentParent.children(".box1ContentOuter").children(".box1ContentInner").children(".tabContent").length
				+ parentParent.children(".tabContent").length
				);
			if(count > 1)
			{
				$(this).parent().parent().children(".box1ContentOuter").children(".box1ContentInner").children(".tabContent").hide();
				$(this).parent().parent().children(".tabContent").hide();
				$(".tabContent").eq($(".box1Tabs .tab").index(this)).fadeIn("normal");
			}
		}
	});
	
	$(".box1Header2 .viewOptions a").click(function() {
		$(this).parent().children().removeClass("current");
		$(this).addClass("current");
	});
});

function showMainNavDropDown() {
    $(".mainNav li ul").css({ left: "0px", top: "34px" });
}

// MY PLAYLIST IN TRAY //////////////////////////////////

$(function() {
	$(".myPlaylist .video .thumb img").mouseover(function(e) {
		//alert("cheese");
		$("body").append($(".myPlaylistTitle"));
		var newX = e.pageX + 10 +'px';
		var newY = e.pageY + 10 +'px';
		$(".myPlaylistTitle").eq($(".myPlaylist .video .thumb img").index(this)).css({left: newX, top: newY});
		$(".myPlaylistTitle").eq($(".myPlaylist .video .thumb img").index(this)).fadeIn("fast");
	});
	$(".myPlaylist .video .thumb img").mouseout(function() {
		$(".myPlaylistTitle").eq($(".myPlaylist .video .thumb img").index(this)).fadeOut("fast");
	});
	
	var widgetsVisible = 7;
	var widgetWidth = 86;
	var marginWidth = 5;
	var trayOffset = widgetWidth + (2 * marginWidth);
	
	$(".animateLeft").click(function() {
		var pos = $(this).parent().children(".video_window").children(".videos").css("left");
		var position = pos.substr(0,(pos.length-2));
		var newPosition = parseInt(position) + trayOffset;
		if (position < 0) {
			$(this).parent().children(".video_window").children(".videos").animate({
				left: newPosition
			}, "slow")
		}
		if ($(this).css("visibility") != 'hidden' && newPosition == 0) {
			$(this).css("visibility", "hidden")
		}
		if ($(this).parent().children(".animateRight").css("display") == 'none' && position < 0) {
			$(this).parent().children(".animateRight").css("display", "inline")
		}
	});
	
	$(".animateRight").click(function() {
		var pos = $(this).parent().children(".video_window").children(".videos").css("left");
		var position = pos.substr(0,(pos.length-2));
		var newPosition = parseInt(position) - trayOffset;
		var maxRight = 0 - (($(this).parent().children(".video_window").children(".videos").children(".video").length - widgetsVisible) * trayOffset);
		if (position > maxRight) {
			$(this).parent().children(".video_window").children(".videos").animate({
				left: newPosition
			}, "slow")
		}
		if ($(this).css("display") != 'none' && newPosition == maxRight) {
			$(this).css("display", "none")
		}
		if ($(this).parent().children(".animateLeft").css("visibility") != 'visible' && position > maxRight) {
			$(this).parent().children(".animateLeft").css("visibility", "visible")
		}
	});
});

// RELATED VIDEOS PANEL //////////////////////////////////

$(function() {
//	$(".nowPlaying .thumb").append('<div class="flag">Now Playing</div>');
	$(".nowPlaying .relatedVideosTitle").html('Now Playing');
/*
	$(".relatedVideos .video .thumb img").each(function(){
		this.onmouseover = function(e) {
			$("body").append($(".relatedVideosTitle"));
			if (!e) {
				e = window.event;
				var newX = e.clientX + 10 +'px';
				var newY = e.clientY + 10 +'px';
			} else {
				var newX = e.pageX + 10 +'px';
				var newY = e.pageY + 10 +'px';
			}
			$(".relatedVideosTitle").eq($(".relatedVideos .video .thumb img").index(this)).css({left: newX, top: newY});
			$(".relatedVideosTitle").eq($(".relatedVideos .video .thumb img").index(this)).fadeIn("fast");
			$(this).animate({
				width: '87px', marginTop: '0px'
			}, "fast");
		}
	});
	$(".relatedVideos .video .thumb img").mouseout(function() {
		$(".relatedVideosTitle").eq($(".relatedVideos .video .thumb img").index(this)).fadeOut("fast");
		$(this).animate({
			width: '52px', marginTop: '15px'
		}, "fast");
	});
*/	
	var widgets_visible = 5;
	var widget_width = 97;
	var margin_width = 18;
//	var offset = widget_width + (2 * margin_width);
	var offset = (widget_width + margin_width) * widgets_visible;
//	var offset = 970;
	var is_currently_scrolling = false;
	var npPos = 1;
	
	$(".relatedVideos .video").each(function(i){
		if ($(this).hasClass("nowPlaying")) {
			npPos = i;
		}
	});
	if (npPos >= widgets_visible) {
		var np_page = parseInt((npPos/widgets_visible));
		var new_position = 0 - (offset * np_page);
		var end_number = (np_page+1) * widgets_visible;
		var start_number = end_number - (widgets_visible - 1);
		$('.videosShowing').html("Showing "+start_number+"-"+end_number);
		$(".relatedVideos .video_window .videos").animate({
			left: new_position
			
		}, "normal")
	}
	
	$(".animate_left").click(function() {
		if(is_currently_scrolling)
			return;
		is_currently_scrolling = true;
	
		var pos = $(this).parent().children(".video_window").children(".videos").css("left");
		var position = pos.substr(0,(pos.length-2));
		var new_position = parseInt(position) + offset;
		if (position < 0) {
			var current_page = (Math.abs(new_position)/offset)+1;
			var end_number = current_page * widgets_visible;
			var start_number = end_number - (widgets_visible - 1);
			$('.videosShowing').html("Showing "+start_number+"-"+end_number);
			$(this).parent().children(".video_window").children(".videos").animate({
				left: new_position
			}, "slow", "", function(){is_currently_scrolling = false;})
		}
		if ($(this).css("visibility") != 'hidden' && new_position == 0) {
			$(this).css("visibility", "hidden")
		}
		if ($(this).parent().children(".animate_right").css("display") == 'none' && position < 0) {
			$(this).parent().children(".animate_right").css("display", "inline")
		}
	});
	
	$(".animate_right").click(function() {
		if(is_currently_scrolling)
			return;
		is_currently_scrolling = true;
	
		var pos = $(this).parent().children(".video_window").children(".videos").css("left");
		var position = pos.substr(0,(pos.length-2));
		var new_position = parseInt(position) - offset;
//		var max_right = 0 - (($(this).parent().children(".video_window").children(".videos").children(".video").length - widgets_visible) * offset);
		var num_widgets = $(this).parent().children(".video_window").children(".videos").eq($(".animate_right").index(this)).children(".video").length;
		var num_pages = Math.ceil(num_widgets/widgets_visible)
		var max_right = 0 - ((num_pages - 1) * offset);
		if (position > max_right) {
			var current_page = (Math.abs(new_position)/offset)+1;
			var end_number = current_page * widgets_visible;
			var start_number = end_number - (widgets_visible - 1);
			if(current_page == num_pages)
			    end_number = num_widgets;
			    
			$('.videosShowing').html("Showing "+start_number+"-"+end_number);
			$(this).parent().children(".video_window").children(".videos").animate({
				left: new_position
			}, "slow", "", function(){is_currently_scrolling = false;})
		}
		if ($(this).css("display") != 'none' && new_position == max_right) {
			$(this).css("display", "none")
		}
		if ($(this).parent().children(".animate_left").css("visibility") != 'visible' && position > max_right) {
			$(this).parent().children(".animate_left").css("visibility", "visible")
		}
	});
});

// RELATED VIDEOS 2 PANEL ////////////////////////////////

$(function() {
	var widgets_visible = 5;
	var widget_height = 96;
	var margin_height = 0;
	var offset = widget_height + (2 * margin_height);
	
	$(".relatedVideos2 .animateUp").click(function() {
		var pos = $(".videoWindow .videoList1").css("top");
		var position = pos.substr(0,(pos.length-2));
		var new_position = parseInt(position) + offset;
		if (position < 0) {
			$(".videoWindow .videoList1").animate({
				top: new_position
			}, "slow")
		}
		if ($(this).css("visibility") != 'hidden' && new_position == 0) {
			$(this).css("visibility", "hidden")
		}
		if ($(".relatedVideos2 .animateDown").css("visibility") == 'hidden' && position < 0) {
			$(".relatedVideos2 .animateDown").css("visibility", "visible")
		}
	});
	
	$(".relatedVideos2 .animateDown").click(function() {
		var pos = $(".videoWindow .videoList1").css("top");
		var position = pos.substr(0,(pos.length-2));
		var new_position = parseInt(position) - offset;
		var max_down = 0 - (($(".videoWindow .videoList1 .video").length - widgets_visible) * offset);
		if (position > max_down) {
			$(".videoWindow .videoList1").animate({
				top: new_position
			}, "slow")
		}
		if ($(this).css("visibility") != 'hidden' && new_position == max_down) {
			$(this).css("visibility", "hidden")
		}
		if ($(".relatedVideos2 .animateUp").css("visibility") != 'visible' && position > max_down) {
			$(".relatedVideos2 .animateUp").css("visibility", "visible")
		}
	});
});

// VIDEO BROWSER 2 PANEL ////////////////////////////////

var ogPos;
$(function() {
/*	$(".featuredExperts .videoList2 .video .thumb img").each(function(){
		this.onmouseover = function(e) {
			if ($(this).parent().parent().hasClass("growLeft")) {
				ogPos = $(this).parent().parent().parent(".videoList2").css("left");
				var position = (parseInt(ogPos.substr(0,(ogPos.length-2)))-51);
				$(this).parent().parent().parent(".videoList2").animate({
					left: position+'px'
				}, "fast");
			}
			$(this).parent().parent(".video").animate({
				width: '217px'
			}, "fast");
			$(this).animate({
				width: '204px', height: '154px'
			}, "fast");
		}
	});
	$(".featuredExperts .videoList2 .video .thumb img").mouseout(function() {
		if ($(this).parent().parent().hasClass("growLeft")) {
			$(this).parent().parent().parent(".videoList2").animate({
				left: ogPos
			}, "fast");
		}
		$(this).parent().parent(".video").animate({
			width: '166px'
		}, "fast");
		$(this).animate({
			width: '154px', height: '116px'
		}, "fast");
	});
*/	
	var widgets_visible = 5;
	var widget_width = 166;
	var margin_width = 32;
	var offset = (widget_width + margin_width) * widgets_visible;
	var is_currently_scrolling = false;
	
	$(".videoNav .buttonLeft2").click(function() {
		if(is_currently_scrolling)
			return;
		is_currently_scrolling = true;
		
		var pos = $(".videoWindow .videoList2").eq($(".videoNav .buttonLeft2").index(this)).css("left");
		var position = pos.substr(0,(pos.length-2));
		var new_position = parseInt(position) + offset;
		if (position < 0) {
			var current_page = Math.abs(new_position)/offset;
			$(this).parent().children(".viewIndicator").children("li").removeClass("current");
			$(this).parent().children(".viewIndicator").children("li").each(function(i) {
				if (current_page == i) {
					$(this).addClass("current");
				}
			});
			$(".videoWindow .videoList2").eq($(".videoNav .buttonLeft2").index(this)).animate({
				left: new_position
			}, "slow", "", function(){is_currently_scrolling = false;})
		}
	});
	
	$(".videoNav .buttonRight2").click(function() {
		if(is_currently_scrolling)
			return;
		is_currently_scrolling = true;
		
		var pos = $(".videoWindow .videoList2").eq($(".videoNav .buttonRight2").index(this)).css("left");
		var position = pos.substr(0,(pos.length-2));
		var new_position = parseInt(position) - offset;
		var num_widgets = $(".videoWindow .videoList2").eq($(".videoNav .buttonRight2").index(this)).children(".video").length;
		var num_pages = Math.ceil(num_widgets/widgets_visible)
		var max_right = 0 - ((num_pages - 1) * offset);
		
		if (position > max_right) {
			var current_page = Math.abs(new_position)/offset;
			$(this).parent().children(".viewIndicator").children("li").removeClass("current");
			$(this).parent().children(".viewIndicator").children("li").each(function(i) {
				if (current_page == i) {
					$(this).addClass("current");
				}
			});
			$(".videoWindow .videoList2").eq($(".videoNav .buttonRight2").index(this)).animate({
				left: new_position
			}, "slow", "", function(){is_currently_scrolling = false;})
		}
	});
});

// GUIDE IMAGE POP-UPS ///////////////////////////////////
$(function() {
	$(".guideIntro a img").each(function(){
		this.onmouseover = function(e) {
			if (!e) {
				e = window.event;
				var newX = e.clientX + 15 +'px';
				var newY = e.clientY + 10 +'px';
			} else {
				var newX = e.pageX + 15 +'px';
				var newY = e.pageY + 10 +'px';
			}
			$(".guideTopicTitle").eq($(".guideIntro a img").index(this)).css({left: newX, top: newY});
			$(".guideTopicTitle").eq($(".guideIntro a img").index(this)).fadeIn("normal");
		}
	});
	$(".guideIntro a img").mouseout(function() {
		$(".guideTopicTitle").eq($(".guideIntro a img").index(this)).fadeOut("fast");
	});
});

// POP UP BOX ////////////////////////////////////////////

//var pto;
//$(document).ready(function(){
//	$(".showPopUp").mouseover(function(e) {
//		if ($(".popUpBox").css("display") == 'none') {
//			showPopUp(e.pageX, e.pageY);
//		} else {
//			clearTimeout(pto);
//		}
//	});
//	$(".showPopUp").mouseout(function() {
//		pto = setTimeout(hidePopUp, 50);
//	});
//	$(".popUpBox").mouseover(function() {
//		clearTimeout(pto);
//	});
//	$(".popUpBox").mouseout(function() {
//		pto = setTimeout(hidePopUp, 0);
//	});
//});

//function showPopUp(xpos, ypos) {
//	newX = xpos + 20 +'px';
//	newY = ypos + 10 +'px';
//	$(".popUpBox").css({left: newX, top: newY});
//	$(".popUpBox").fadeIn("fast");
//}
//function hidePopUp() {
//	$(".popUpBox").hide();
//}

// FORGOTTEN PASSWORD POP UP BOX //////////////////////////

function showForgotPopUp(e)
{
	DisplayPopUp(e,$(".forgotPopUpBox"));
}

function SendToFriend(e)
{
	DisplayPopUp(e,$(".popUpBox2"));
}

// PLAYLIST POP UP BOX ///////////////////////////////////

function addToPlaylistPopUp(e, videoID)
{
	$('#addtoplaylist_video').val(videoID);
	
	DisplayPopUp(e,$(".addToPlaylistPopUp"));		
}

//Generic function to control the displaying of a popup near the button/link that invoked it.
function DisplayPopUp(e, popup) 
{
	if($(popup).css("display") == 'none')
	{
		// Calculate pageX/Y if missing and clientX/Y available
		if ( e.pageX == undefined && e.clientX != undefined ) {
			 var elm = document.documentElement, b = document.body;
			 e.pageX = e.clientX + (elm.scrollLeft || b.scrollLeft);
			 e.pageY = e.clientY + (elm.scrollTop || b.scrollTop);
		}
		
		newX = e.pageX + 20 +'px';
		newY = e.pageY + 10 +'px';
		$(popup).css({left: newX, top: newY});
		$(popup).fadeIn("fast");
	}
	else
	{
		$(popup).fadeOut("fast");
	}
}

// alert(document.domConfig);

////
//
// Print Debug
//
// param    value     string
// param    color     #FFFFFF or rgb(255,255,255)
// param    border    #FFFFFF or rgb(255,255,255)
//
////
printd = function(html_text, text_color, border_color) 
{
	var debug_id = "debugger_output";

	document.debug_div = document.getElementById(debug_id);
	if(!document.debug_div) 
	{
		// Initalize the debug script.
		document.debug_div = document.createElement("div");
		document.debug_div.id = "debugger_output";

		document.debug_div.style["width"] = "300px";
		document.debug_div.style["position"] = "absolute";
		document.debug_div.style["top"] = "10px";
		document.debug_div.style["right"] = "10px";
		document.debug_div.style["background"] = "#FFFFFF";
		document.debug_div.style["border"] = "1px solid #AEAEAE";
		document.debug_div.style["font"] = "11px Arial";
		document.debug_div.style["textAlign"] = "left";
		document.debug_div.style["zIndex"] = "9999";
	}

    var container = document.createElement("div");

	container.innerHTML = html_text;

	if(typeof html_text == "object") 
	{
		var object_elements = document.createElement("div");
		object_elements.style["margin"] = "0px 0px 0px 10px";
		object_elements.style[".padding"] = "0px 0px 0px 0px";
		object_elements.style["listStyleType"] = "none";

		for(var key in html_text) 
		{
			var element = document.createElement("span");
			
			element.style["margin"] = "0px 2px 0px 0px";
			element.style["padding"] = "0px 4px 0px 4px";
			
			// Store the value 
			try {
				element.object_value = html_text[key];
				element.object_key = key;
			} catch(e) {
				// Currently it can't seem to get "domConfig"
				// printd("Unable to get this element: " + key, "#FF0000", "#FF0000");
			}

			element.onclick = function() 
			{
				var red = Math.round((Math.random()*155)+100);
				var green = Math.round((Math.random()*155)+100);
				var blue = Math.round((Math.random()*155)+100);
				var color = "rgb("+red+","+green+","+blue+")";

				var new_debug_div = printd(this.object_value, "#000000", color);

				this.onmouseover = function() 
				{
					// new_debug_div.style["backgroundColor"] = "#FF0000";
				}

				this.onmouseout = function() 
				{
					/// new_debug_div.style["backgroundColor"] = "none";
				}

				this.style["backgroundColor"] = color + "";
			}

			element.onmouseover = function() 
			{
				this.style["background"] = "#E5EBF1";
				this.style["cursor"] = "pointer";
			}

			element.onmouseout = function() 
			{
				this.style["background"] = "none";
			}

			var foo = document.createTextNode(key);
			element.appendChild(foo);

			object_elements.appendChild(element);

			// Need to add some whitespace else it won't word wrap.
			var text_element = document.createTextNode("\n");
			object_elements.appendChild(text_element);
		}

		container.appendChild(object_elements);
	} 

	container.style["padding"] = "2px 3px 2px 4px";
	container.style["borderBottom"] = "1px solid #DEDEDE";
	container.style["color"] = "#000000";

	if(text_color != null) 
	{
		container.style["color"] = text_color;
	}

	if(Boolean(border_color) == true) 
	{
		container.style["borderLeft"] = "3px solid " + border_color;
	}

	document.debug_div.appendChild(container);
	document.body.appendChild(document.debug_div);

	return(container);
} 


