var menuitems = [];
var x,y;
$(document).ready(function(){
	Modernizr.addTest('pointerevents',function(){ //really hacky method to find pointer events on HTML elements, designed to exclude Opera and IE9
		return document.documentElement.style.pointerEvents === '' && document.documentElement.style.OTransform !== '' && document.documentElement.style.textShadow === '';
	});
	Modernizr.addTest('svgcliphtml',function(){ //detect svg clipping of images, or the webkit equivalent
		try{
			var SVGNS = "http://www.w3.org/2000/svg";
		
			var s = document.createElementNS(SVGNS, "svg");
			var c = document.createElementNS(SVGNS, "clipPath");
			c.setAttribute("id", "clipPath");
			s.appendChild(c);
			var r = document.createElementNS(SVGNS, "rect");
			r.setAttribute("width", "0");
			c.appendChild(r);
			document.body.appendChild(s);
			
			var d = document.createElement('DIV');
			
			d.style.width = '100px';
			d.style.height = '100px';
			d.style.backgroundColor = '#000000';
			d.style.clipPath = 'url(#clipPath)';
			$('body').prepend(d);
					
			//var d = document.getElementById("d");		
			var dRect = d.getBoundingClientRect();
			var e = document.elementFromPoint(dRect.left + 10, dRect.top + 10);
			document.body.removeChild(s);
			document.body.removeChild(d);
		}catch(e){
			return false;
		}
		//alert("Browser supports clipPath for HTML: " + ((d != e) ? "Yes" : "No"));		
		return (d != e) || document.documentElement.style.webkitMaskImage === '';
	});

	if (!Modernizr.pointerevents){		
		initializePointers();			
	}
	if (!Modernizr.borderradius){
		$('#stock-info').wrapInner('<div class="text"></div>').append('<div class="bottom"></div>');
		$('#content').wrapInner('<div class="text"></div>').append('<div class="bottom"></div>');
	}
	if ($('#stock-info').length){
		$('#stock-info table').each(getStockSection);
	}
	/*if ($('#map-link').length){
		$('body').append('<div id="big-map"></div>');
		$('#big-map').dialog({
			autoOpen:false,
			modal:true,
			width:500
		});
		$('#big-map').append('<img src="images/map.gif" alt="map" />');
		$('#map-link').click(function(){
			
			$('#big-map').dialog('open');			
			return false;
		});
	}*/
});
function initializePointers(){
	$('nav ul li a').each(function(){
		var id = $(this).attr('id');
		var offset = $(this).offset();
		var left = offset.left;
		var top = offset.top;
		var width = $(this).outerWidth();
		var height = $(this).outerHeight();
		//var message = id+' left:'+left+' top:'+top+' '+width+'x'+height+'<br />';
		//$('#content').after(message);
		menuitems.push({id:id, left:left, top:top, width:width, height:height});
	});		
	$('header').mousemove(function(e){
		x = e.pageX;
		y = e.pageY;
		var n=0;
		//$('#content').html(x+','+y);
		while (n<menuitems.length){
			if (inMenuItem(menuitems[n])){				
				$('nav ul li a').removeClass('on');
				$('#'+menuitems[n].id).addClass('on');
				document.body.style.cursor = 'pointer';
				break;
			}else{
				$('nav ul li a').removeClass('on');
				document.body.style.cursor = 'default';
			}
			n++;
		}
	});
	$('header').click(function(e){
		x = e.pageX;
		y = e.pageY;
		var n=0;
		while (n<menuitems.length){
			if (inMenuItem(menuitems[n])){
				var url = $('#'+menuitems[n].id).attr('href');
				//$('#content').html(url);
				location.href = url;
				break;
			}
			n++;
		}
	});	
}
function inMenuItem(menuitem){
	if ((x >= menuitem.left && x<= menuitem.left + menuitem.width)&&(y >= menuitem.top && y<= menuitem.top + menuitem.height)){
		return true;
	}else{
		return false;
	}
}
function getStockSection(){
	var type = $(this).attr('data-type');
	var stocks = [];
	var codelist = [];
	$(this).find('tr').each(function(){
		stocks.push({id:$(this).attr('id'), code:$(this).attr('data-code')});
		codelist.push($(this).attr('data-code'));
	});
	var symbols = codelist.join(',');
	$.ajax({
		url:'get_stocks.php',
		cache:false,
		dataType:'JSON',
		data:{s:symbols, type:type},
		success:function(data){
			var id, price, changeid, change, direction;
			$.each(data, function(n, stockdata){
				id = '#'+stocks[n].id;
				price = (type == 'exchange' ? '$':'')+stockdata.value;
				
				$(id+' .price').html(price);
				$(id).attr('title','Last Change: '+stockdata.lastupdate);
				changeid = id+' .change';
				if ($(changeid).length){
					change = Number(stockdata.change);
					if (change < 0){
						direction = 'down';
					}else{
						direction = 'up';
					}
					$(changeid).html(stockdata.change).addClass(direction);
				}
			});
		}
	});
}
