var stock_autocomplete = 0;

$(function(){
    $('.JS').show();
    $(".with-tooltip").simpletooltip();
    historyDate();
	//languageMenu();
    //setTickersMenu();
    //fixTables();
	
	$("#signup_username").bind('keydown keyup',function()
 	{
		$("#username-error").hide();
	});
	
	zIndexWorkaround();
	
	//$("#StockAutoComplete").autocompleteArray(["Allen","Albert","Alberto","Alladin"]);
	$("#StockAutoComplete").autocomplete($("#StockAutoCompleteURL").attr('href'), 
		{ 
			minChars:1,
			delay:10,
			width:300,
			formatItem:formatItem,
			maxItemsToShow:20,		
			onItemSelect: function (li) { 
				selectAutoComplete (li);
			},
			onFindValue: function (li) { 
				selectAutoComplete (li);
			}
		});

	$("#StockAutoCompleteButton").click( function () {
		var oSuggest = $("#StockAutoComplete")[0].autocompleter;
		oSuggest.findValue();
		return false;
	});
	
	
	$("#StockAutoComplete").focus( function (e) {
		if (stock_autocomplete == 0) {
			$("#StockAutoComplete").attr("value", "");
		};
		stock_autocomplete = 1;
	});
	
	$("#StockAutoComplete").keydown( function (e) {
		if (e.keyCode == 13) {
			var oSuggest = $("#StockAutoComplete")[0].autocompleter;
			oSuggest.findValue();
			return false;
		}
	});
});

function formatItem(row) {
	return row[0] + " - <i>" + row[1] + "</i>";
}


function selectAutoComplete(li)
{
	if (li == null) {
		var textArea = $("#StockAutoComplete")
		if (textArea.attr('value') != '') {
			//redirect to search: /search/stock/data/
			var searchurl = $("#StockSearchURL").attr('href');
			searchurl = searchurl.replace("##QUERY##",textArea.attr('value')) ;
			document.location = searchurl; 
		}
		return;
	};
	
	if( !!li.extra ) 
		var sValue = li.extra[0];
	else 
		var sValue = li.selectValue;
	
	var baseurl = $("#StockAutoCompleteBaseURL").attr('href');	
	baseurl = baseurl.replace("##TICKER##",sValue.replace('.','_')) ;
	
	document.location = baseurl; 
	//alert(baseurl);
}

$(document).ready( function ()
{
	var link = $("#TopDiscussedLinks a");
	link.click( function () {
		
		//$(this).css('border','solid 1px red');
		
		//do not refresh if the item is allready selected
		if ($(this).attr('class') == 'active'){
			return false; }
		
		var div = $("#TopDiscussedStocksContent") ;
		div.load($(this)[0].href);
	
		//reset the classes	
		$("#TopDiscussedLinks a").removeClass("active");
		$(this).addClass("active");
		$(this).blur();
		
		return false;
	});
});

$(document).ready( function ()
{
	var link = $("#TopContributersLinks a");
	link.click( function () {
		
		//$(this).css('border','solid 1px red');
		
		//do not refresh if the item is allready selected
		if ($(this).attr('class') == 'active'){
			return false; }
		
		var div = $("#TopContributersContent") ;
		div.load($(this)[0].href);
	
		//reset the classes	
		$("#TopContributersLinks a").removeClass("active");
		$(this).addClass("active");
		$(this).blur();
		
		return false;
	});
});

$(document).ready( function ()
{
	var link = $("#SectorVotesLinks a");
	link.click( function () {
		
		//$(this).css('border','solid 1px red');
		
		//do not refresh if the item is allready selected
		if ($(this).attr('class') == 'active'){
			return false; }
		
		var div = $("#SectorVotesContent") ;
		div.load($(this)[0].href);
	
		//reset the classes	
		$("#SectorVotesLinks a").removeClass("active");
		$(this).addClass("active");
		$(this).blur();
		
		return false;
	});
});


$(document).ready( function ()
{
	var link = $("#SelectStockForVoting a");
	link.click( function () {
		
		var div = $("#StockListForVoting") ;
		div.load($(this)[0].href);

		return false;
	});
});



function resetTopDiscussed(e){
	//alert('daar gaan we');
	
	
	return false;
}

function isIE()
{
    //if(navigator.userAgent.match(/MSIE \d\.\d+/))
    //    return true;
    return true;
}

function zIndexWorkaround()
{
    // If the browser is IE,
    if(isIE())
    {
        /*
        ** For each div with class menu (i.e.,
        ** the thing we want to be on top),
        */
		
        $("div.UserBubble").parents().each(function() {
            var p = $(this);
            var pos = $(this).css("position");
            // If it's positioned,
			
            if(pos == "relative" ||
               pos == "absolute" ||
               pos == "fixed")
            {
                /*
                ** Add the "on-top" class name when the
                ** mouse is hovering over it,
                */
                p.mouseover(function() {
                    $(this).addClass("on-top");
                });
                // And remove it when the mouse leaves.
                p.mouseout(function() {
                    $(this).removeClass("on-top");
                });
            }
        });
    }
}

function historyDate() {

		xOffset = 20;
		yOffset = 20;

	$("div.component-block ol.history li").hover(function(e){
		var text = $(this).find('strong').text(); //this.t = this.title;

		this.title = "";
        $("body").append("<div id='simpleTooltip' style='position: absolute; z-index: 100; display: none;'>" + text + "</div>");
		$("#simpleTooltip")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px")
			.fadeIn("fast");
	},
	function(){
		this.title = this.text;
		$("#simpleTooltip").remove();
	});
	$("div.component-block ol.history li").mousemove(function(e){
		$("#simpleTooltip")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px");
	});

}

function languageMenu() {
    var docH = $('body').height();
    var coverMenu = '<div id="coverMenu" style="height:'+docH+'px"></div>';

        $('#listLanguages li:gt(0)').hide();
        $('#personalMenu li:last-child').css('border-right','1px solid #939393').css('padding-right','10px');
        $('#listLanguages li:eq(0)').addClass('no-active');
        
        $('#listLanguages li.drop').click(function(){

            if ( $(this).hasClass('no-active') ) {
                $(this).removeClass('no-active');
                $(this).parent().addClass('border-it-dark');
                    if($.browser.msie && $.browser.version == "6.0") {
                        $(this).parent().css('border','1px solid #999');
                    }
                $('#listLanguages li:gt(0)').show();
                if ( $('#coverMenu').length == 0) $(coverMenu).appendTo('body');
                closeMenu();
            }
            else {
                $(this).addClass('no-active');
                $(this).parent().removeClass('border-it-dark');
                if($.browser.msie && $.browser.version == "6.0") {
                        $(this).parent().css('border','1px solid #fff');
                    }
                $('#listLanguages li:gt(0)').hide();
                $('#coverMenu').remove();
            }
        return false;
        });
        

}

function closeMenu () {
    $('#coverMenu').click(function(){
        $(this).remove();
        $('#listLanguages').removeClass('border-it-dark');
        if($.browser.msie && $.browser.version == "6.0") {
            $("#listLanguages").css('border','1px solid #fff');
        }
        $('#listLanguages li:eq(0)').addClass('no-active');
        $('#listLanguages li:gt(0)').hide();
        return false;
    });
}
function setTickersMenu() {
    var widthMenu = 0;
    if ( $('ul#listTickers').length > 0 ) {
        widthMenu = $("ul#listCurrences").width();
        $('ul#listTickers').css('right',widthMenu+10 +'px');
        if ($.browser.msie && $.browser.version == "6.0") {
            $('ul#listTickers').css('right',widthMenu+62 +'px');
        }
    }
}
function fixTables() {
    if ($('table').length > 0)  {
        $('table').each(function(){
            $(this).find('tr:last').children().css('background-image','none');
        });
    }
    $('ul#listLanguages ul li:last-child').css('background-image','none');
}




/**
*
*	simpleTooltip jQuery plugin, by Marius ILIE
*	visit http://dev.mariusilie.net for details
*
**/
(function($){ $.fn.simpletooltip = function(){
	return this.each(function() {
		var text = $(this).attr("title");
		$(this).attr("title", "");
		if(text != undefined) {
			$(this).hover(function(e){
				var tipX = e.pageX + 12;
				var tipY = e.pageY + 12;
				$(this).attr("title", "");
				$("body").append("<div id='simpleTooltip' style='position: absolute; z-index: 100; display: none;'>" + text + "</div>");
				if($.browser.msie) var tipWidth = $("#simpleTooltip").outerWidth(true)
				else var tipWidth = $("#simpleTooltip").width()
				$("#simpleTooltip").width(tipWidth);
				$("#simpleTooltip").css("left", tipX).css("top", tipY).fadeIn("medium").css("background","#ffffff");
			}, function(){
				$("#simpleTooltip").remove();
				$(this).attr("title", text);
			});
			$(this).mousemove(function(e){
				var tipX = e.pageX + 12;
				var tipY = e.pageY + 12;
				var tipWidth = $("#simpleTooltip").outerWidth(true);
				var tipHeight = $("#simpleTooltip").outerHeight(true);
				if(tipX + tipWidth > $(window).scrollLeft() + $(window).width()) tipX = e.pageX - tipWidth;
				if($(window).height()+$(window).scrollTop() < tipY + tipHeight) tipY = e.pageY - tipHeight;
				$("#simpleTooltip").css("left", tipX).css("top", tipY).fadeIn("medium");
			});
		}
	});
}})(jQuery);


/* hack for display errors */
function showErrors()
{
	var i;
	elements = $('.ErrorMessage,.SuccessMessage')

	
	for (i = 0; i < elements.length; i++)
	{
		if ( jQuery.trim($(elements[i]).text()) != '')
		{
			$(elements[i]).show();
		}
	}
}

$(document).ready(
	function()
	{
		showErrors();	
	}
);


