$(document).ready(function(){

	
	// this expects that the bbcode parse
	// substitutes this:
	//   [stock]AAPL[/stock]
	// with
	//   <stock class="trigger" id="{param}">{param}</stock>
	//
	// no hooks in the VB are used for this at all.
	
	
	//$(function () {


	function uberGetX(x,id){
	
		// get left of page
		var left = window.pageXOffset;   
	   
		// attempt to allow for width of the trigger text
		var pad = 50;
	
		//check if will be too close to left side
		if ((x - $(id).width()/2 - pad) <= left){
			return (x + pad - $(id).width()/2);
		} else {
			return (x - $(id).width()/2 - pad);
		}
	
	}
	
	function uberGetY(y,id){
	   
		// get top of page
		var top = window.pageYOffset;
	   
		// attempt to allow for height of the trigger text
		var pad = 20;
	
		//check if will be too close to top side
		if ((y - $(id).height() - pad) <= top){
			return (y + pad + 20);
		} else {
			return (y - $(id).height() - pad);
		}
	
	}



	var triggercounter = 0;
	var tickerstring = '';
	
	
	var triggercounter = 0;
	var tickerstring = '';

    $('.tickertrigger').each(function () {	

        var trigger = $(this);//$('.trigger', this);
        var triggerid = trigger.attr('id');  // get the current id .. originally a stock symbol
        triggerid = 'sym_' + triggercounter + '_' + triggerid;   // make the id what we want it to be
        triggercounter++
        tickerstring += triggerid + '||||';
    });
	
    triggercounter = 0;
	
    
    $.ajax({
    	url: '/forum/includes/ajax/tickersget.php?ts=' + tickerstring,
    	cache: false,
    	success: function(html){
    		$('body:last-child').append(html);
    		
    		
    		
    		
    		   $('.tickertrigger').each(function () {

    		        var distance = 25;
    		        var time = 250;
    		        var hideDelay = 250;

    		        var hideDelayTimer = null;

    		        var beingShown = false;
    		        var shown = false;

    		       // alert ($(this));
    		        
    		        var trigger = $(this);//$('.trigger', this);
    		 
    		        var triggerid = trigger.attr('id');  // get the current id .. originally a stock symbol
    		        triggerid = 'sym_' + triggercounter + '_' + triggerid;   // make the id what we want it to be
    		        trigger.attr('id',triggerid);   // set the element with the new id
    		        triggercounter++
    		        //tickerstring += triggerid + '||||';
    		        //alert ($('#pop_' + triggerid));       
    		        
    		        
    		        
    		        var info = $(document.getElementById('pop_' + triggerid)).css('opacity', 0);
					


    		        $([trigger.get(0), document.getElementById('pop_' + triggerid)]).mouseover(function (e) {

    		        	if (hideDelayTimer) clearTimeout(hideDelayTimer);
    		            if (beingShown || shown) {
    		                // don't trigger the animation again
    		                return;
    		            } else {
    		                // reset position of info box
    		                beingShown = true;

                            info.css({
                                top: uberGetY(e.pageY,document.getElementById('pop_' + triggerid)),
                                left: uberGetX(e.pageX,document.getElementById('pop_' + triggerid)),
                                display: 'block'
    		                }).animate({
    		                    top: '-=' + distance + 'px',
    		                    opacity: 1
    		                }, time, 'swing', function() {
    		                    beingShown = false;
    		                    shown = true;
    		                });
    		            }

    		            return false;
    		        }).mouseout(function () {
    		            if (hideDelayTimer) clearTimeout(hideDelayTimer);
    		            hideDelayTimer = setTimeout(function () {
    		                hideDelayTimer = null;
    		                info.animate({
    		                    top: '-=' + distance + 'px',
    		                    opacity: 0
    		                }, time, 'swing', function () {
    		                    shown = false;
    		                    info.css('display', 'none');
    		                });

    		            }, hideDelay);

    		            return false;
    		        });
    		    });
    		//});
    		
    		
    		
    		
    		
    		
    	}
    });       
    
    
	
 

});
