/// JavaScript Document


var boxes = new Array();
var groups = new Array();
var groupsposition = new Array();

var imgx = 0;
var imgy = 0;
var imgw = 0;
var imgh = 0;
var imgx2 = 0;
var imgy2 = 0;

//function loadImages()
//{
// onload="loadImages();"
image1 = new Image(); image1.src = "/images/location/wall.gif";
image2 = new Image(); image2.src = "/images/location/wall-red.gif";
image3 = new Image(); image3.src = "/images/location/wall-blue.gif";
image4 = new Image(); image4.src = "/images/location/wall-green.gif";
image5 = new Image(); image5.src = "/images/location/wall-orange.gif";
//}

function setMainPosition()
{
     var fp = document.getElementById('floorplan');
  
     if(fp) 
     {
          imgx = findPosX(fp);
          imgy = findPosY(fp);
          imgw=fp.width;
          imgh=fp.height;	
	
          //set upper left and lower left 
          imgx2 = imgx+imgw;
          imgy2 = imgy+imgh;
     }
}

function checkBounds(coord, excludeindex)
{
     //check to see if the coords fall within another coord

     for ( var i=0, len=boxes.length; i<len; ++i )
     {    

        if(boxes[i] && excludeindex!=i)
     	  {
               if((coord[0]>=boxes[i][0] && coord[0]<=boxes[i][2] && coord[1]>=boxes[i][1] && coord[1]<=boxes[i][3]) ||   
		          (coord[2]>=boxes[i][0] && coord[2]<=boxes[i][2] && coord[3]>=boxes[i][1] && coord[3]<=boxes[i][3]) ||			 
			      ((coord[0]+15)>=boxes[i][0] && (coord[0]+15)<=boxes[i][2] && coord[1]>=boxes[i][1] && coord[1]<=boxes[i][3]) ||
			      (coord[0]>=boxes[i][0] && coord[0]<=boxes[i][2] && (coord[1]+20)>=boxes[i][1] && (coord[1]+20)<=boxes[i][3]) )        
               {
		            //now that we have an overlapping we will stack the media
			        //get the group that this one is in
			        var group = boxes[i][4];
			   
                    return group;
               }
	      }
     }
     return -1;
}


var groupcount=0;
var lastgroupos=100;

function writeHTML(position, posx, posy, 
topx, topy, 
botx, boty, 
group, ret, id, title, description, mediatypename, haveimg, descoffset, iconpath, size)
{

	return writeHTML(position, posx, posy, 
topx, topy, 
botx, boty, 
group, ret, id, title, description, mediatypename, haveimg, descoffset, iconpath, size, 1, 0);
	
}

function writeHTML(position, posx, posy, 
topx, topy, 
botx, boty, 
group, ret, id, title, description, mediatypename, haveimg, descoffset, iconpath, size, sethtml)
{

	return writeHTML(position, posx, posy, 
topx, topy, 
botx, boty, 
group, ret, id, title, description, mediatypename, haveimg, descoffset, iconpath, size, sethtml, 0);
	
}

function writeHTML(position, posx, posy, 
topx, topy, 
botx, boty, 
group, ret, id, title, description, mediatypename, haveimg, descoffset, iconpath, size, sethtml, showdescriptionbox)
{

	var addfirst=0;
	if(ret>=0) 
	{
		if(groups[ret][0]==id && sethtml==0) addfirst=1;
	}

	//<![CDATA[
	if(ret>=0 && addfirst==0) 
	{ //]]>
		//if this box is within another wall box then group it under the box its coordinates are within
		 if(sethtml==1) boxes[position] = [topx, topy, botx, boty, ret];
		 
		 //now position the box below the last bar in the group	 
		if(sethtml==1) 
		{
			if(showdescriptionbox==1) document.write('<div id="desc_'+id+'" class="walls">');
			else document.write('<div style="visibility: hidden;z-index:50;" id="desc_'+id+'" class="walls">');
		}
		if(sethtml==1) document.write('<table width="350" height="90"><tr><td height="3" align="right" colspan="2"><img src="/images/location/close.gif" onClick="toggleGroup(0, '+ret+');"/></td></tr>');
		//if(sethtml==1) document.write('<tr><td width="75" valign="top" align="center"><a href="/s/sell?action=1&id='+id+'" class="arial10blue">');
		if(sethtml==1) document.write('<tr><td width="75" valign="top" align="center"><a href="'+mediaservlet+'?action='+mediaaction+'&'+mediaidparam+'='+id+'" class="arial10blue">');

		if(sethtml==1)
		{
			//if(haveimg==1) document.write('<img width="75" height="75" border="0" src="/s/file?action=1&id='+id+'&amp;num=1&amp;size=3"/><br/><br/>');
			if(haveimg==1) document.write('<img width="75" height="75" border="0" src="'+fileservlet+'?action='+fileaction+'&id='+id+'&amp;num=1&amp;size=3"/><br/><br/>');
			else document.write('<img width="75" height="75" border="0" src="/images/location/nomedia.gif"/><br/><br/>');
		}
		
		//if(sethtml==1) document.write('</a></td><td valign="top"><b><a href="/s/sell?action=1&id='+id+'" class="arial10blue">'+title+'</a></b><br/><span class="arial8">MLI: '+id+'</span><br/><span class="arial8">Media Type: '+mediatypename+'</span><br/><span class="arial8">Size: '+size+'</span></td></tr></table>');
		if(sethtml==1) document.write('</a></td><td valign="top"><b><a href="'+mediaservlet+'?action='+mediaaction+'&'+mediaidparam+'='+id+'" class="arial10blue">'+title+'</a></b><br/><span class="arial8">MLI: '+id+'</span><br/><span class="arial8">Media Type: '+mediatypename+'</span><br/><span class="arial8">Size: '+size+'</span></td></tr></table>');
		if(sethtml==1) document.write('</div>');

		//multi icon path
		 if(sethtml==1) document.write('<div style="z-index:200;" id="w_'+id+'">');
		if(iconpath=='') iconpath='/images/location/wallmulti.gif';
		 if(sethtml==1) document.write('<img onMouseOver="toggleGroup(1, '+ret+');" src="'+iconpath+'" style="visibility: hidden;" border="0" id="wall_'+id+'"/>');
		 if(sethtml==1) document.write('</div>');
		 if(sethtml==0)	{ writeWall(posy, posx, imgx, imgy, id);	} 
	
		
		// if(sethtml==0) { alert(groups[ret].length+'-'+descoffset+'-'+posy); writeDesc(posy+(descoffset*groups[ret].length), posx, imgx, imgy, id); }
		 if(sethtml==0) 
		 { 
		 	var i=0;

			//get the pos y from the last
			for(i=0;i<groups[ret].length;i++)
			{				
				if(groups[ret][i]==id) break;
			}
		 	writeDesc(posy+(descoffset*i), posx, imgx, imgy, id); 
		 }
	
		 if(sethtml==1) groups[ret][groups[ret].length]=id;			 
	}
	else
	{
		if(sethtml==1) 
		{
			if(showdescriptionbox==1) document.write('<div id="desc_'+id+'" class="walls">');
			else document.write('<div style="visibility: hidden;z-index:50;" id="desc_'+id+'" class="walls">');
		}
		if(sethtml==1) document.write('<table width="350" height="80"><tr><td height="3" align="right" colspan="2"><img src="/images/location/close.gif" onClick="toggleGroup(0, '+ret+');"/></td></tr>');
		//if(sethtml==1) document.write('<tr><td width="78" valign="top" align="center"><a href="/s/sell?action=1&id='+id+'" class="arial10blue">');
		if(sethtml==1) document.write('<tr><td width="78" valign="top" align="center"><a href="'+mediaservlet+'?action='+mediaaction+'&'+mediaidparam+'='+id+'" class="arial10blue">');
		
		if(sethtml==1) 
		{
			//if(haveimg==1) document.write('<img width="75" height="75" border="0" src="/s/file?action=1&id='+id+'&amp;num=1&amp;size=3"/><br/><br/>');
			if(haveimg==1) document.write('<img width="75" height="75" border="0" src="'+fileservlet+'?action='+fileaction+'&id='+id+'&amp;num=1&amp;size=3"/><br/><br/>');
			else document.write('<img width="75" height="75" border="0" src="/images/location/nomedia.gif"/><br/><br/>');
		}
		
		//if(sethtml==1) document.write('</a></td><td valign="top"><b><a href="/s/sell?action=1&id='+id+'" class="arial10blue">'+title+'</a></b><br/><span class="arial8">MLI: '+id+'</span><br/><span class="arial8">Media Type: '+mediatypename+'</span><br/><span class="arial8">Size: '+size+'</span></td></tr></table>');
		if(sethtml==1) document.write('</a></td><td valign="top"><b><a href="'+mediaservlet+'?action='+mediaaction+'&'+mediaidparam+'='+id+'" class="arial10blue">'+title+'</a></b><br/><span class="arial8">MLI: '+id+'</span><br/><span class="arial8">Media Type: '+mediatypename+'</span><br/><span class="arial8">Size: '+size+'</span></td></tr></table>');

		
		if(sethtml==1) document.write('</div>');
	
		 //if not in a group then wrtie then wall
		 if(sethtml==1) document.write('<div style="z-index:100;" id="w_'+id+'">');

		 if(iconpath=='') iconpath='/images/location/wall.gif';
		 if(sethtml==1) document.write('<img onMouseOver="toggleGroup(1, '+groupcount+');" src="'+iconpath+'" style="visibility: hidden;" border="0" id="wall_'+id+'"/>');
		 if(sethtml==1) document.write('</div>');
	

		 if(sethtml==0)	{ writeWall(posy, posx, imgx, imgy, id);	} 
		 if(sethtml==0)	{ writeDesc(posy, posx, imgx, imgy, id); }
		 
		 if(sethtml==1) ret=groupcount;
		 if(sethtml==1) groups[groupcount]=[id];	
		 if(sethtml==1) groupsposition[groupcount]=[posx, posy, topx, topy, botx, boty];	

		 //always add the groupcount since we track this on each iteration
		 groupcount=groupcount+1; 
	}
}




function writeWall(posy, posx, imgx, imgy, id)
{

     var w = document.getElementById('wall_'+id);
     w.style.position = 'absolute';
     w.style.top = posy+imgy-7;
     w.style.left = posx+imgx-7;	
     w.style.visibility = 'visible';
}

//get the description and set its position
function writeDesc(posy, posx, imgx, imgy, id)
{
     var desc = document.getElementById('desc_'+id);
     desc.style.position = 'absolute';
     desc.style.top = posy+imgy;
     desc.style.left = posx+imgx+10;
}

function toggleGroup( iState, showgroup) // 1 visible, 0 hidden
{

    //loop through the groups and toggle all in a group
	for ( var i=0, len4=groups.length; i<len4; i++ )
    {
		for ( var x=0, len5=groups[i].length; x<len5; x++ )
        {
		     if(showgroup==i) toggleBox('desc_'+groups[i][x], iState);
			 else toggleBox('desc_'+groups[i][x], 0);
		}		
	}
}

function toggleBox(szDivID, iState) // 1 visible, 0 hidden
{
    if(document.layers)	   //NN4+
    {
       document.layers[szDivID].visibility = iState ? "show" : "hide";
    }
    else if(document.getElementById)	  //gecko(NN6) + IE 5+
    {
        var obj = document.getElementById(szDivID);
        if(obj) obj.style.visibility = iState ? "visible" : "hidden";
    }
    else if(document.all)	// IE 4
    {
        document.all[szDivID].style.visibility = iState ? "visible" : "hidden";
    }
}
