/* SHOWS FLOATING DIV WITH PASSED CONTENT, PARAMETERS AND CLOSE LINK */
//ex.:
//var content = document.getElementById("Description").value;
//showFloatDiv('DescriptionFloat', 480, '', 100, 168, 'absolute', content, 'Close Preview');

function showFloatDiv(id,w,h,t,l,pos,content,header,appendTo) { 
	//DO NOT REPLACE IF HTML TAGS ARE PRESENT:
	if(content && content.indexOf("<br>")==-1){
			var strReplaceAll = content; 
			var intIndexOfMatch = strReplaceAll.indexOf("\n"); 
			while (intIndexOfMatch != -1){
				// Relace out the current instance.
				strReplaceAll = strReplaceAll.replace( "\n", "<br>" )
				// Get the index of any next matching substring.
				intIndexOfMatch = strReplaceAll.indexOf( "\n" ); 
			} 
			content = strReplaceAll;
	}

	var div = document.createElement("div");
	div.id = id;
	div.style.border = "1px solid #CCC";
	div.style.background = "white";
	div.style.position = pos;
	div.className = "drag";
	if(w!=''){div.style.width = w+"px";}
	if(h!=''){div.style.height = h+"px";}
	if(t!=''){div.style.top = t+"px";}
	if(l!=''){div.style.left = l+"px";}	
	
	var descriptionArray = new Array();
	descriptionArray.push("<table width='100%'><tr><td bgColor=gray height=20 align=center>");
	descriptionArray.push("<a class='content-big-bw' href='JavaScript:hideFloatDiv(\""+id+"\");'>");
	descriptionArray.push("<img src='http://www.worldincatalog.com/images/crossed.gif' border=0 height=15>"+header+"</a></td></tr><tr><td>");
	descriptionArray.push(content);
	descriptionArray.push("</td></tr></table>");
	
	var str = descriptionArray.join(' ');
	div.innerHTML = str;   
	
	if(appendTo!=''){
		var parent = document.getElementById(appendTo);
		if(parent){parent.appendChild(div)}
		else{var body = document.getElementsByTagName('body').item(0);
			body.appendChild(div);}
	}else{
		var body = document.getElementsByTagName('body').item(0);
		body.appendChild(div);}	
}
function hideFloatDiv(id,parentId) {
	var div = document.getElementById(id);
	var body = document.getElementsByTagName("body").item(0);
	if(div){
		if(parentId!='' || parentId!=null){
			var parent = document.getElementById(parentId);
			if(parent){
				parent.removeChild(div);
			}else{body.removeChild(div);}
		}else{
			body.removeChild(div);
		}
	}
}

/////////////////////////////////////////////////////////////////////////////
//NEW VERSION////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
function initFloatPopupBox(content,w,h,t,l,header,pos,appendTo) {
   
    //create random id
    var id = "floatPopupBox_"+Math.floor(Math.random()*1001);

    //convert breaks to html if any
    content = handleContentHTMLInput(content);

    //get size
    var width  = (w!=null && w!='') ? ((w.toString().indexOf("%")==-1) ? parseInt(w)+"px" : w) : "250px";
    var height = (h!=null && h!='') ? ((h.toString().indexOf("%")==-1) ? parseInt(h)+"px" : h) : "300px";

    //set default positions
    var moveShadow = 10;
    var defaultTopLeft = 100;
	
	var topPosFix = typeof window.pageYOffset != 'undefined' ?
    window.pageYOffset:document.documentElement && document.documentElement.scrollTop ?
    document.documentElement.scrollTop: document.body.scrollTop?document.body.scrollTop:0;

    //get positions
    var top  = (t!=null && t!='') ? (topPosFix + parseInt(t))+"px" : (topPosFix + defaultTopLeft)+"px";
    var left = (l!=null && l!='') ? parseInt(l)+"px" : defaultTopLeft+"px";
    var topShadow  = (parseInt(top) + moveShadow)+"px";
    var leftShadow = (parseInt(left)+moveShadow)+"px";

    //create floating div
    var div             = document.createElement("div");
    div.id                 = id;
    div.className            = "dragFPB";   
    div.style.position         = pos;
    div.style.width           = width;
    div.style.height          = height;
    div.style.top             = top;
    div.style.left            = left;
	
	//TEMP FIX: when onscroll initialized, div does not drag for first time
	//div.style.overflow		  = "auto";
	//div.onscroll			  = doNotMove;
	//div.onmouseup	          = doMove;

    //create shadow
    var divShadow            = document.createElement("div");
    divShadow.id            = id+"_Shadow";
    divShadow.className        = "dragFPBShadow";
    divShadow.style.position     = pos;
    divShadow.style.width     = width;
    divShadow.style.height    = height;
    divShadow.style.top       = topShadow;
    divShadow.style.left      = leftShadow;

	//create iframe tail: IE5.5-6 fix for overlapping objects (select boxes)
    var iframe  = null;
	var agt		= navigator.userAgent.toLowerCase();
    if(agt.indexOf("msie 6.")!=-1 || agt.indexOf("msie 5.")!=-1){
		iframe             	  	  = document.createElement("iframe");
		iframe.id                 = id+"_Iframe";
		iframe.src 				  = "";
		iframe.frameBorder		  = 0;
		iframe.className          = "dragFPBTail";   
		iframe.style.position     = pos;
		iframe.style.width        = width;
		iframe.style.height       = height;
		iframe.style.top          = top;
		iframe.style.left         = left;
	}

    div.onmousemove          = fixShadowPosition; //attach shadow
 
    //content array
    var descriptionArray = new Array();
    descriptionArray.push("<table id='"+id+"_Content' width='100%' border='0' cellpadding='1' cellspacing='1'><tr><td bgColor='gray' height='20' align='center' style='cursor:move;'>");
    descriptionArray.push("<a class='closelink' href='JavaScript:hideFloatPopupBox(\""+id+"\");'>");
    descriptionArray.push("<img src='../images/crossed.gif' border='0' height='15'>"+header+"</a>");
    descriptionArray.push("</td></tr><tr><td style='height:auto;background-color:#fff;'>"+content+"</td></tr></table>");
  
    var str = descriptionArray.join(' ');
    div.innerHTML = str; 
	
	
  
    //append to parent
    var parent = document.getElementsByTagName('body').item(0);
    if(appendTo!=null&&appendTo!=''){
        var parentCandidate = document.getElementById(appendTo);
        if(parentCandidate){ parent = parentCandidate; }
    }
    if(parent){ parent.appendChild(div); parent.appendChild(divShadow); if(iframe!=null){ parent.appendChild(iframe); } }  
	
	fixOverlap(id,id+"_Content");//TESTING
	
}

function fixOverlap(divId,tableId){
	var d = document.getElementById(divId);
	var t = document.getElementById(tableId);
	if(d && t && parseInt(d.style.height) < parseInt(d.style.height)){
		//TEMP FIX: when onscroll initialized, div does not drag for first time
		//alert(parseInt(d.style.height)+' '+parseInt(d.style.height))
		d.style.overflow	= "auto";
		d.onscroll			= doNotMove;
		d.onmouseup	        = doMove;
	}
}

function handleContentHTMLInput(content){
    if(content && content.indexOf("<br>")==-1){//convert plain text to html
            var strReplaceAll = content;
            var intIndexOfMatch = strReplaceAll.indexOf("\n");
            while (intIndexOfMatch != -1){
                // replace out the current instance.
                strReplaceAll = strReplaceAll.replace( "\n", "<br>" )
                // get the index of any next matching substring.
                intIndexOfMatch = strReplaceAll.indexOf( "\n" );
            }
            content = strReplaceAll;
    }
  return content;
}

function hideFloatPopupBox(id) {
    var divs = new Array(id,id+"_Shadow",id+"_Iframe");
    for(var i=0;i<divs.length;i++){
        var d = document.getElementById(divs[i]);
        if(d){d.parentNode.removeChild(d);}
    }
}

function fixShadowPosition(){
    var shadowEl = document.getElementById(this.id+"_Shadow");
	var iframeEl = document.getElementById(this.id+"_Iframe");
    if(shadowEl){
        shadowEl.style.top  = (parseInt(this.style.top)+10)+"px";
        shadowEl.style.left = (parseInt(this.style.left)+10)+"px";
    }
	if(iframeEl){
		iframeEl.style.top  = parseInt(this.style.top)+"px";
        iframeEl.style.left = parseInt(this.style.left)+"px";
    }
}

/////////////////////////////////////////////////////////////////////////////
/////////////////////////DRAG FOR FLOAT POPUP BOX////////////////////////////
/////////////////////////////////////////////////////////////////////////////

var ieDrag = document.all;
var ns6Drag = document.getElementById && !document.all;
var dragapproved=false;
var allowDragging=true;
var zDrag, xDrag, yDrag;
var ff=true;

function move(e) {
  if (dragapproved) {
    zDrag.style.left=ns6Drag? temp1+e.clientX-xDrag: temp1+event.clientX-xDrag;
    zDrag.style.top=ns6Drag? temp2+e.clientY-yDrag : temp2+event.clientY-yDrag;
    return false;
  }
}


function drags(e) {//	SEE inc/drag.js used in store/item/ to avoid overriding
  try{
	  if (!ieDrag&&!ns6Drag)
	  return;
	  var firedobj = ns6Drag? e.target : event.srcElement;
	  var topelement = ns6Drag? "HTML" : "BODY";
	  while (firedobj.tagName != topelement&&firedobj.className != "dragFPB") {
		firedobj = ns6Drag? firedobj.parentNode : firedobj.parentElement;
	  }
	 
	  if (firedobj.className == "dragFPB" && allowDragging == true) {  
		dragapproved = true;
		zDrag = firedobj;
		temp1 = parseInt(zDrag.style.left+0);
		temp2 = parseInt(zDrag.style.top+0);
		xDrag = ns6Drag? e.clientX: event.clientX;
		yDrag = ns6Drag? e.clientY: event.clientY;   
		document.onmousemove=move;
		return false; 
	  }
  }catch(e){}
}

function doNotMove(){ dragapproved=false; allowDragging=false; }
function doMove(){ dragapproved=false; allowDragging=true; }

document.onmousedown=drags;
document.onmouseup=new Function("dragapproved=false; allowDragging=true;");