var lastHS = "";
var bgwidth = 1280;
var bgheight = 1080;

function hotSpot(spotid, spot_content){
	spot = $('#' + spotid);
	$(spot_content).css('top', $(spot).offset().top-3).css('left',  ($(spot).offset().left-3));
	if ($(spot_content).is(':hidden') || (lastHS==spotid)) $(spot_content).slideToggle('fast', setHotSpotState(spot, spot_content));
	tid = spotid.split("-");
	curHS = getHS(tid[1]);
	//lc = labelColor(curHS.color);
	lc = "#666666";

	hsHTML = "<div id='pswatch'><div id='swatchcolor'></div><div id='swatch'><img src='rsrcs/images/pbox-swatch-border.png' width='38' height='38' /></div><div id='description'><h2>" + curHS.desc + "</h2><h3>" + curHS.tvcode + "</h3></div><div id='tab'><a href='#' onclick='addHStoProjectBox(\""+ tid[1] + "\"); return false'><img src='rsrcs/images/pbox-swatch-add-tab.png' width='115' height='35' /></a></div></div>";

	$(spot_content).html( hsHTML);
	
	$("div#pswatch div#swatchcolor").css('background-color', curHS.color);
	$("div#pswatch div#description h2").css('color', lc);
	lastHS=spotid;
}

function addHStoProjectBox(hsid){
	contentRef = '#hotspot-show';
	curHS = getHS(hsid);
	tspot = $('#' + hsid);
	addColorSwatchToProjectBox( curHS.color.toUpperCase().substring(1), curHS.desc,  curHS.color.toUpperCase(), curHS.tvcode)
	$(contentRef).slideToggle('fast', setHotSpotState(tspot, contentRef));
}

function setHotSpotState(hotspot, ref) {
    $("#hotspots li").removeClass("active",1000);
    //$("#hotspots li").css("zIndex", 3);
	if ($(ref).is(':hidden')) {
        var state = "closed";
        $(hotspot).addClass("active");
    } else {
        var state = "open";
    }
}
	
function labelColor(hexcolor) {
	dark = '#000000';
	light = '#FFFFFF';
	hc = hexcolor.substr(1);
	if (h2d(hc) >( 0xffffff/4)) {
		rv = dark;
	} else {
		rv = hexcolor;
	}
	//override to grey
	rv = "#666666";
	return rv;
}

function h2d(h) {return parseInt(h,16);}

function makeHotSpots(){
	var tmphtml = "";
	for (var h = 0; h < hotSpotsJSON.hotspots.length; h++) {
	    try {
		    curHS = hotSpotsJSON.hotspots[h];
		    if (curHS.section == section) {
			    tmphtml = tmphtml + "<li id='HS-" +curHS.hsID+ "'></li>";
		    }
	    }
	    catch (err) {
	    }	
	}
	$('#hotspots').html(tmphtml);
	for(var h=0; h < hotSpotsJSON.hotspots.length; h++){	
		curHS = hotSpotsJSON.hotspots[h];
		//tpcnt = parseInt((curHS.top/bgheight) * 100) + "%";
		//lpcnt =  parseInt((curHS.left/bgwidth) * 100) + "%";
		tpcnt = curHS.top  + "px";
		lpcnt = curHS.left + "px";
	    try {
		    if (curHS.section == section) {
			    $("li#HS-" + curHS.hsID).css("top", tpcnt ).css("left", lpcnt ).css("background-color", curHS.color);		
		    }
	    }
        catch (err) {
	    }	
	}
}

var hsMap = {};

function getHS(tid) {
	return hsMap[tid];
};
