$(document).ready(function() {
	
	$("li, button").click(function() {
		if ($(this).attr("href")) {
			document.location = $(this).attr("href");
		} else if ($(this).attr("action")) {
			$("INPUT[name=action]").val($(this).attr("action"));
		} else if ($(this).attr("ajax")) {
			$(this).parent().children("li").removeClass("on");
			$(this).addClass("on");
			uri = $(this).attr("ajax");
			
			if ($(this).attr("holder")) {
				holder = $(this).attr("holder");
			} else {
				holder = "#main_content";
			}
			
			$.get(uri, function(html){
				$(holder).html(html);
			})
		}
	});
	
	/*
	min_height = 0;
	$("div.three-column-layout div.column div.content").each(function(e) {
		console.log($(this).height());
		
		if ($(this).height()>min_height)
			min_height = $(this).height();
	});	

	$("div.three-column-layout div.column div.content").each(function(e) {
		$(this).height(min_height);
	});
	*/
	
	$("div").click(function() {
		if ($(this).attr("rel")) {
			if ($(this).attr("rel")=="video") {
				document.location = '/video/view/';
			}
		}
	});
	
	
	$("div.vertically-scrollable").each(function() {
		
		content = $(this).html();
		//alert(content);
		$(this).css("height", "200px");
		$(this).css("overflow-y", "hidden");

		html = '<img class="arrow-vertical-up" src="/_templates/frontend/images/icons/arrow-vertical-up.gif" />';
		html+= '<img class="arrow-vertical-down" src="/_templates/frontend/images/icons/arrow-vertical-down.gif" />';
		html+= '<div class="viewport" style="width: 92%; height: 200px; overflow-y:hidden;">';
		html+= '<div class="viewport-content" style="width: 92%; height: 2000px">';
		html+= content;
		html+= '</div>';
		html+= '</div>';

		$(this).html(html);

		var top = 0;

		$(this).children("img.arrow-vertical-up").click(function() {
			if (top<0) {
				top=top+220;
				$(this).parent().children("div.viewport").children("div.viewport-content").animate({marginTop: top}, 1000);
			}
		});
		
		$(this).children("img.arrow-vertical-down").click(function() {
			top=top-220;
			$(this).parent().children("div.viewport").children("div.viewport-content").animate({marginTop: top}, 1000);
		});
		
	});	
	
	
	$("div.horiz-scrollable").each(function() {
		
		content = $(this).html();

		$(this).css("overflow-x", "hidden");
		
		html = '<table border="0" width="100%">';
		html+= '<tr>';
		html+= '<td class="previous_button_holder" align="left" style="padding: 5px;">';
		html+= '	<img src="/_templates/frontend/images/icons/one-column-arrow-left.gif" border="0" class="previous-button" />';
		html+= '</td>';
		html+= '<td>';
		html+= '<div class="viewport" style="height: 160px; width: 630px; overflow-x:hidden;">';
		html+= '<div class="viewport-content" style="height: 160px; width: 10000px;">';
		html+= content;
		html+= '</div>';
		html+= '</div>';
		html+= '</td>';
		html+= '<td class="next_button_holder" align="right" style="padding: 5px;">';
		html+= '	<img src="/_templates/frontend/images/icons/one-column-arrow-right.gif" border="0" class="next-button" />';
		html+= '</td>';
		html+= '</tr>';
		html+= '</table>';
		
		$(this).html(html);
	
		left = 0;
		page = 0;
	
		
		$("table tr:first td:first img.previous-button").click(function() {
			if (left<0) {
				var viewportOffset = left+$("table tr td div.viewport").width();
				$("table tr td div.viewport div.viewport-content").animate({marginLeft: viewportOffset }, 1000);
				left = viewportOffset;
			}
			page=page-1;
			update_video_paging(page);
		});
	
		
		$("table tr:first td:last img.next-button").click(function() {
			var viewportOffset = left-$("table tr td div.viewport").width()
			$("table tr td div.viewport div.viewport-content").animate({marginLeft: viewportOffset }, 1000);
			left = viewportOffset;
			page=page+1;
			update_video_paging(page);
		});
		
		update_video_paging(0);
	});
	
	
	$("h1.logo").click(function() {
		document.location = "/";
	});
	
	
	/*
	var nav_on = false;
	var sub_nav_on = false;

	$("#header ul li").mouseover(function() {
		
		
		if (!nav_on && !$(this).hasClass("on")) {
			$(this).addClass("on");
			subnav = $(this).children("ol");
			
			if (subnav) {
				subnav.slideDown();
			}
			
			nav_on = true;
		} 
	
	});
	
	$("#header ul li").mouseout(function() {
		if (!$(this).hasClass("on") && !sub_nav_on) {
			nav_on = false;
			$("#header ul li ol").slideUp();
			
			$("#header ul li").removeClass("on")
		}
	});
	
	
	$("#header ul li ol").mouseover(function() {
		sub_nav_on = true;
	});
	
	$("#header ul li ol").mouseout(function() {
		sub_nav_on = false;
	});
	*/
});

function update_video_paging(page) {
	if($("#video_paging") && page>=0) {
		total_videos = $("INPUT[name=total_videos]").val();
		if (total_videos<=((page*4)+4)) {
			of_videos = total_videos
		} else {
			of_videos = ((page*4)+4);
		}
		showing = (page*4)+1;
		if (showing>=of_videos) {
			showing = of_videos;
		} 
		$("#video_paging").text("Showing videos "+showing+"-"+of_videos+" of "+total_videos);
	}
}

$(document).keypress(function(e){
	if(e.keyCode==27){
		$.unblockUI();
	}
});


function feature_popup(url) {
	//alert(url);
	$.get(url, function(html) {
		$("#feature-popup").html(html);
		$("#feature-popup").fadeIn(500);
	});
}

function feature_popup_small(url) {
	feature_popup(url);
	$("#feature-popup").width("30%");
	$("#feature-popup").height("150px");
}

function add_as_friend(url) {
	$.get(url, {action: 'add-as-friend'}, function(html) {  });
	$("#friend-text").text("Friend Requested");
}

function feature_popup_close() {
	$("#feature-popup").fadeOut(500);
}

function UI_popup(id) {
	  $.blockUI({ 
		  message: $(id),
		  css: { 
          border: 'none', 
          padding: '15px', 
          backgroundColor: '#000', 
          '-webkit-border-radius': '10px', 
          '-moz-border-radius': '10px', 
          opacity: 1, 
          color: '#fff' 
      } }); 
}
