﻿var openedWin= false;
var curTopDragWin=null;
var curLeftDragWin=null;

function jstrprintf() 
{
    len = arguments.length;
    if (len == 0) { return; }
    if (len == 1) { return arguments[0]; }
    
    var result;
    var regexstr;
    var replstr;
    var formatstr;
    var re;
    
    result = "";
    regexstr = "";
    replstr = "";
    formatstr = arguments[0];
    
    for (var i=1; i<arguments.length; i++) 
    {
        replstr += String(i+100) + arguments[i]  + String(i + 100);
        regexstr += String(i+100) + "(.*)" + String(i+100);
    }
    re = new RegExp(regexstr);
    var result;
    result = replstr.replace(re, formatstr);
    
    return result;
}

function AddPx(num) {
    return String(num) + "px";
}

function findParentDiv(obj) {
    while (obj) {
        if (obj.tagName.toUpperCase() == "DIV") {
            return obj;
        }
        
        if (obj.parentElement) {
            obj = obj.parentElement;
        }
        else {
            return null;
        }
    }
    return null;
}

function findParentTagById(obj, parentname) {
    while (obj) {
        if (obj.id.match(parentname)) {
            return obj;
        }
        
        if (obj.parentElement) {
            obj = obj.parentElement;
        }
        else {
            return null;
        }
    }
    return null;
}

// Now for the real thing

var topZ = 200;
var startX;
var startY;

function closeWindow(id)
{
    if (id=="sizechart_drag_win")
    {
        curTopDragWin=null;
        curLeftDragWin=null;
    }
    EliminateLayersDrag(id);
}

function EliminateLayersDrag(id)
{

    if (id=="needinfo_drag_win")
    {
        if (!openedWinMoreInfo) return;
        openedWinMoreInfo=false;
    }
    if (id=="proddetail_drag_win")
    {
        if (!openedWinDetails) return;
        openedWinDetails=false;
    }
    else if (id=="sizechart_drag_win")
    {
        if (!openedWinSizeChart) return;
        openedWinSizeChart=false;
    }
    else if (id=="tl_drag_win")
    {
        if (!openedWinTL) return;
        openedWinTL=false;
    }
    
	//var el = document.body;
	var el = document.getElementById("allContainer");
	
	if (el.hasChildNodes()) 
	   { 
		 var n=el.childNodes.length;
		for (var i = n-1; i >= 0; i--) 
	     { 	
			if (el.childNodes[i].getAttribute)
			{
		        var cldID = el.childNodes[i].getAttribute("id");
		        if (cldID==id )
		        {
		            el.removeChild(el.childNodes[i]); 
		        }
				else if (cldID==id )
		        {
		            el.removeChild(el.childNodes[i]); 
		        }
			}
	     } 
	   }
}

function CreateDropdownWindow(caption, canMove, theWidth, innerHTML, startX, startY, ID, topZ, hContent) 
{
    var newdiv;
    var title="";
    
    newdiv = document.createElement("div");
    newdiv.id = String(ID);
    newdiv.className = "divDragTitle";
    newdiv.style.width = AddPx(theWidth);
    newdiv.style.height = AddPx(42);
    newdiv.style.left = AddPx(startX);
    newdiv.style.top = AddPx(startY);
    newdiv.style.zIndex = topZ;
    //'<table><tr><td>$1</td>' +
    
    if (caption.indexOf("immagini",0)>=0)
        title = '<img alt="" src="'+caption+'" />';
    else
        title = caption;
	newdiv.innerHTML = jstrprintf(
        '<table><tr><td id="td_title'+ID+'" class="first">'+title +'</td>' + 
        '<td class="second">' +
        '<div class="divTitleButton" id="dragButton$2" ' + 
        '><a href="javascript:closeWindow(\'$2\');" onclick="javascript:closeWindow(\'$2\');" >' +
        '<img alt="close window" onclick="javascript:closeWindow(\'$2\');" title="close window" onmouseout="javascript:this.src=\''+action_bnt_imgpath+'/x.gif\';" onmouseover="javascript:this.src=\''+action_bnt_imgpath+'x_on.gif\';" src="'+action_bnt_imgpath+'x.gif" />'+
        '</a></div></td>' +
        '</tr></table>'+'<!--[if lte IE 6.5]><iframe></iframe><![endif]-->',
        caption, ID);
	
    if (canMove==true)
    {
        // IE doesn't support addEventListener, so check for its presence
        if (newdiv.addEventListener) {
            // firefox, etc.
            newdiv.addEventListener("mousemove", function(e) { return mouseMove(e) }, true);
            newdiv.addEventListener("mousedown", function(e) { return mouseDown(e) }, true);
            newdiv.addEventListener("mouseup", function(e) { return mouseUp(e) }, true);
        }
        else 
        {
            // IE
            newdiv.attachEvent("onmousemove", function(e) { return mouseMove(e) });
            newdiv.attachEvent("onmousedown", function(e) { return mouseDown(e) });
            newdiv.attachEvent("onmouseup", function(e) { return mouseUp(e) });
        }
    }
    //document.body.appendChild(newdiv);
    var container = document.getElementById("allContainer");
    container.appendChild(newdiv);
    
    var newdiv2;
    newdiv2 = document.createElement("div");
    newdiv2.id =String(ID);
    newdiv2.className = "divDragContent";
    newdiv2.style.width = AddPx((theWidth+2));
    newdiv2.style.height = AddPx(hContent);
    newdiv2.style.left = AddPx(startX);
    newdiv2.style.top = AddPx(startY + 42);
    newdiv2.style.zIndex = topZ;
    if (innerHTML!="") {
        newdiv2.innerHTML = innerHTML+'<!--[if lte IE 6.5]><iframe></iframe><![endif]-->';
    }
    
    if (canMove==true)
    {
        if (newdiv2.addEventListener) {
            // firefox, etc.
            newdiv2.addEventListener("mousedown", function(e) { return contentMouseDown(e) }, true);
        }
        else {
            // IE
            newdiv2.attachEvent("onmousedown", function(e) { return contentMouseDown(e) });
        }
    }
    //document.body.appendChild(newdiv2);
    container.appendChild(newdiv2);
    
    // Save away the content DIV into the title DIV for 
    // later access, and vice versa
    newdiv.content = newdiv2;
    newdiv2.titlediv = newdiv;

    var tt=document.getElementById("td_title"+ID);
    tt.style.width=(theWidth-24)+"px";
        
//   topZ += 1;
//    startX += 25;
//    startY += 25;
    
}

function toggleContentWin(id) {
    var elem = document.getElementById("dragContent" + String(id));
    var img = document.getElementById("dragButton" + String(id));

    if (elem.style.display == "none") {
        // hidden, so unhide
        elem.style.display = "block";
        
        // Change the button's image
        img.src = "buttontop.gif";
}
    else {
        // showing, so hide
        elem.style.display = "none";

        // Change the button's image
        img.src = "buttonbottom.gif";
    }
}

// Drag methods
var dragObjTitle = null;
var dragOffsetX = 0;
var dragOffsetY = 0;

function contentMouseDown(e) {
    // Move the window to the front
    // Use a handy trick for IE vs FF
    var dragContent = e.srcElement || e.currentTarget;
    if ( ! dragContent.id.match("dragContent")) {
        dragContent = findParentTagById(dragContent, "dragContent");
    }
    if (dragContent) {
        dragContent.style.zIndex = topZ;
        dragContent.titlediv.style.zIndex = topZ;
        topZ++;
    }
}

function mouseDown(e) {
    // These first two lines are written to handle both FF and IE
    var curElem = e.srcElement || e.target;
    var dragTitle = e.currentTarget || findParentDiv(curElem);
    if (dragTitle) {
        if (dragTitle.className != 'divDragTitle') {
            return;
        }
    }
    
    // Start the drag, but first make sure neither is null
    if (curElem && dragTitle) {
    
        // Attach the document handlers. We don't want these running all the time.
        addDocumentHandlers(true);
    
        // Move this window to the front.
        dragTitle.style.zIndex = topZ;
        dragTitle.content.style.zIndex = topZ;
        topZ++;
    
        // Check if it's the button. If so, don't drag.
        if (curElem.className != "divTitleButton") {
            
            // Save away the two objects
            dragObjTitle = dragTitle;
            
            // Calculate the offset
            dragOffsetX = e.clientX - 
                dragTitle.offsetLeft;
            dragOffsetY = e.clientY - 
                dragTitle.offsetTop;
                
            // Don't let the default actions take place
            if (e.preventDefault) {
                e.preventDefault();
            }
            else {
                document.onselectstart = function () { return false; };
                e.cancelBubble = true;
                return false;
            }
        }
    }
}

function mouseMove(e) {
    // If not null, then we're in a drag
    if (dragObjTitle) {
    
        if (!e.preventDefault) {
            // This is the IE version for handling a strange
            // problem when you quickly move the mouse
            // out of the window and let go of the button.
            if (e.button == 0) {
                finishDrag(e);
                return;
            }
        }
    
        dragObjTitle.style.left = AddPx(e.clientX - dragOffsetX);
        dragObjTitle.style.top = AddPx(e.clientY - dragOffsetY);
        dragObjTitle.content.style.left = AddPx(e.clientX - dragOffsetX);
        dragObjTitle.content.style.top = AddPx(e.clientY - dragOffsetY + 42);
        if (e.preventDefault) {
            e.preventDefault();
        }
        else {
            e.cancelBubble = true;
            return false;
        }
    }
}

function mouseUp(e) {
    if (dragObjTitle) {
        finishDrag(e);
    }
}

function finishDrag(e) {
    var finalX = e.clientX - dragOffsetX;
    var finalY = e.clientY - dragOffsetY;
    if (finalX < 0) { finalX = 0 };
    if (finalY < 0) { finalY = 0 };

    dragObjTitle.style.left = AddPx(finalX);
    dragObjTitle.style.top = AddPx(finalY);
    if (dragObjTitle.id=="sizechart_drag_win")
    {
        curTopDragWin=finalY;
        curLeftDragWin=finalX;
    }
    dragObjTitle.content.style.left = AddPx(finalX);
    dragObjTitle.content.style.top = AddPx(finalY + 42);
    
    // Done, so reset to null
    dragObjTitle = null;
    addDocumentHandlers(false);
    if (e.preventDefault) {
        e.preventDefault();
    }
    else {
        document.onselectstart = null;
        e.cancelBubble = true;
        return false;
    }
}

function addDocumentHandlers(addOrRemove) {
    if (addOrRemove) {
        if (document.body.addEventListener) {
            // firefox, etc.
            document.addEventListener("mousedown", function(e) { return mouseDown(e) }, true);
            document.addEventListener("mousemove", function(e) { return mouseMove(e) }, true);
            document.addEventListener("mouseup", function(e) { return mouseUp(e) }, true);
        }
        else {
            // IE
            document.onmousedown = function() { mouseDown(window.event) } ;
            document.onmousemove = function() { mouseMove(window.event) } ;
            document.onmouseup = function() { mouseUp(window.event) } ;
        }
    }
    
}



