/*TopMenu*/
$(document).ready(function(){
	$('#topmenu li').filter(function(index){
		return $('ul', this).length;
	}).addClass('sublink');
	$('#topmenu li').hover(
		function() {
			$(this).addClass("selected");
			$(this).find('ul').stop(true, true);
			$(this).find('ul').show('fast');
		},
		function() {
			$(this).find('ul').hide();
			$(this).removeClass("selected");
		}
	);
});

/*Scroller*/
	$(function() {
		//scrollpane parts
		var scrollPane = $( ".scroll-pane" ),
			scrollContent = $( "#scroll-content" );
		
		//build slider
		var scrollbar = $( ".scroll-bar" ).slider({
			slide: function( event, ui ) {
				if ( scrollContent.width() > scrollPane.width() ) {
					scrollContent.css( "margin-left", Math.round(
						ui.value / 100 * ( scrollPane.width() - scrollContent.width() )
					) + "px" );
				} else {
					scrollContent.css( "margin-left", 0 );
				}
			}
		});

		var h=0;
			$("#scroll-content").find("li").each(function(){
			h+=$(this).width();
		}).end().width(h);
		
		//append icon to handle
		var handleHelper = scrollbar.find( ".ui-slider-handle" )
		.mousedown(function() {
			scrollbar.width( handleHelper.width() );
		})
		.mouseup(function() {
			scrollbar.width( "100%" );
		})
		.append( "<span><b class='thide'>&lt;...&gt;</b></span>" )
		.wrap( "<div class='ui-handle-helper-parent'></div>" ).parent();
		
		//change overflow to hidden now that slider handles the scrolling
		scrollPane.css( "overflow", "hidden" );
		
		//size scrollbar and handle proportionally to scroll distance
		function sizeScrollbar() {
			var remainder = scrollContent.width() - scrollPane.width();
			var proportion = remainder / scrollContent.width();
			var handleSize = scrollPane.width() - ( proportion * scrollPane.width() );
			scrollbar.find( ".ui-slider-handle" ).css({
				width: handleSize,
				"margin-left": -handleSize / 2
			});
			handleHelper.width( "" ).width( scrollbar.width() - handleSize );
		}
		
		//reset slider value based on scroll content position
		function resetValue() {
			var remainder = scrollPane.width() - scrollContent.width();
			var leftVal = scrollContent.css( "margin-left" ) === "auto" ? 0 :
				parseInt( scrollContent.css( "margin-left" ) );
			var percentage = Math.round( leftVal / remainder * 100 );
			scrollbar.slider( "value", percentage );
		}
		
		//if the slider is 100% and window gets larger, reveal content
		function reflowContent() {
				var showing = scrollContent.width() + parseInt( scrollContent.css( "margin-left" ), 10 );
				var gap = scrollPane.width() - showing;
				if ( gap > 0 ) {
					scrollContent.css( "margin-left", parseInt( scrollContent.css( "margin-left" ), 10 ) + gap );
				}
		}
		
		//change handle position on window resize
		$( window ).resize(function() {
			resetValue();
			sizeScrollbar();
			reflowContent();
		});
		//init scrollbar size
		setTimeout( sizeScrollbar, 10 );//safari wants a timeout
	});
/*End Scroller*/

/*LoginUserDialog*/
$(function() {
	$( '#dialoguser' ).dialog({
		autoOpen: false,
		width: 320
	});

	$('#loginuser').click(function(){
		$('#dialoguser').dialog('open');
		return false;
	});
});

/*PollDialog*/
$(function() {
	$( "#polldialog" ).dialog({
		autoOpen: false,
		width: 450
	});

	$('#polllink a').click(function(){
		$('#polldialog').dialog('open');
		return false;
	});
});

/* Slidedown */
$.fn.Button=function(sel)
{
	var closed=true,
		th=this;
	$(sel).hide();
	$(this).removeClass('selected').click(function(){
		$(sel).slideToggle("slow");
		$(this).toggleClass("selected");
		closed=!closed;
		return false;
	});
	$(document).click(function(e){
		if(closed)
			return;
		var target=e.target||e.srcElement;
		while(target)
		{
			var ret=false;
			$(sel).each(function(){
				if(ret=target==this)
					return false;
			});
			if(ret)
				return;
			target=target.parentNode;
		}
		$(sel).hide('slow');
		$(th).removeClass('selected');
		closed=true;
	});
	return this;
}

/* Tabs */
$(document).ready(function(){
	var tabContainers = $('#alphpanel .tabcont');
		tabContainers.hide().filter(':first').show();
								
		$('#alphpanel .tabmenu a').click(function () {
			tabContainers.hide();
			tabContainers.filter(this.hash).show();
			$('#alphpanel .tabmenu a').removeClass('selected');
			$(this).addClass('selected');
			return false;
		}).filter(':first').click();
});

$(document).ready(function(){
	var tabContainers = $('#btabs .tabcont');
		tabContainers.hide().filter(':first').show();
								
		$('#btabs .tabmenu a').click(function () {
			tabContainers.hide();
			tabContainers.filter(this.hash).show();
			$('#btabs .tabmenu a').removeClass('selected');
			$(this).addClass('selected');
			return false;
		}).filter(':first').click();
});

$(document).ready(function(){
	var tabContainers = $('#awtabs .tabcont');
		tabContainers.hide().filter(':first').show();
								
		$('#awtabs .tabmenu a').click(function () {
			tabContainers.hide();
			tabContainers.filter(this.hash).show();
			$('#awtabs .tabmenu a').removeClass('selected');
			$(this).addClass('selected');
			return false;
		}).filter(':first').click();
});

$(document).ready(function() {
	$('#tocoms').click(function(){
	$('#toaddcom').animate({scrollTop:0}, 'slow');
	return false;
	});
});

/* Favorites */
function addBookmark(title, url) {
	if (document.all)
		window.external.AddFavorite(url,title);
	else
		window.sidebar.addPanel(title,url,'');
}

/*UserInfo*/
$(document).ready(function(){
	$('.cominf').hover(
		function() {
			$(this).find('.cominfbox').stop(true, true);
			$(this).find('.cominfbox').show('fast');
		},
		function() {
			$(this).find('.cominfbox').hide();
			$(this).removeClass("selected");
		}
	);
});
