﻿// JScript File
function clearTextField(pTextField, content)
{
    if (pTextField.value == content)
        pTextField.value = '';
}

function setTextField(pTextField, content)
{
    if (pTextField.value.length <=0)
        pTextField.value = content;
}

function switchImage(pImage, jsName)
{
    if (pImage != null)
        pImage.src = eval(jsName + ".src");
}

function SwitchObject(id, state)
{
    var pObj = GetElementById(id);
    if (pObj)
        if (state != null && state != undefined)
        {
            if (state)
                pObj.style.display = 'block'
            else
                pObj.style.display = 'none'
        }
        else
        {
            if (pObj.style.display == 'block')
                pObj.style.display = 'none'
            else
                pObj.style.display = 'block'
        }
    
}

function navigate(url)
{
    window.location.href = url;
}

function openWindow(url, windowname, options)
{
    if (options == null || options == undefined)
        options = '';
       
    if (windowname == null || windowname == undefined)
    {
        d = new Date();
        windowname = 'popup' + d.getTime();
    }
    window.open(url, windowname, options);
}

function navigateFromDropDown(pDropDown, basePath)
{
    if (pDropDown)
    {
        if (!basePath)
            basePath = '';
            
        var selected = pDropDown[pDropDown.selectedIndex].value;
        if (selected.length > 0)
            window.location.href = basePath + selected;
    }
}

function GetElementById(id) 
{
    var elem;
    
    if (document.getElementById) 
        // standard browsers
        elem = document.getElementById(id);
    else if (document.all)  
        // IE 4
        elem = document.all[id];
    
    return elem;
}

function SetObjectValue(object, filename)
{
    var pObj = GetElementById(object);
    if (pObj)
        pObj.value = filename;
}

function ScreenDimensions()
{
    var dimensions = new Object()

    if (parseInt(navigator.appVersion)>3) {
        if (navigator.appName=="Netscape") 
        {
            dimensions.Width = window.innerWidth-16;
            dimensions.Height = window.innerHeight-16;
        }
        if (navigator.appName.indexOf("Microsoft")!=-1) 
        {
            dimensions.Width = document.body.offsetWidth-20;
            dimensions.Height = document.body.offsetHeight-20;
        }
    }
    
    return dimensions;
}

function GetPosition(x, y)
{
    var position = new Object();
    position.X = x;
    position.Y = y;
    
    return position;
}

function GetObjectPosition(objName)
{
    var obj = GetElementById(objName);
    
    if (obj)
        return GetPosition(obj.style.left, obj.style.top)
    
    return GetPosition(0,0);
}

function GetMousePosition(event)
{
    var position = new Object();
    
    if (event)
        if (event.pageX) 
            return GetPosition(event.pageX, event.pageY);
        else 
        { 
            // IE, Opera
            var root = document.documentElement || document.body;
            return GetPosition(event.clientX - root.scrollLeft, event.clientY - root.scrollTop);
        }
    
    return GetPosition(0,0);
}

function ShowZoneElements(zoneID, pageID, basePath)
{
    if (basePath == undefined  || basePath == null)
        basePath = '';
        
    window.open(basePath + 'popup-cms-parts.aspx?zid=' + zoneID + '&pid=' + pageID, 'puze', 'width=800,height=400,scrollbars=1,resizable=1');
}

function EditPartBrowseAssets(objID, iDir, basePath)
{
    if (basePath == undefined  || basePath == null)
        basePath = '';
        
    window.open(basePath + 'popup-asset-browser.aspx?obj=' + objID + '&dir=' + iDir, 'puba','width=800,height=400,scrollbars=1,resizable=1');
}

function EditPartErrorMessage(partID, message)
{
    var pMsg = GetElementById(partID + "_MSG");
    if (pMsg)
    {
        if (message.length > 0) 
        {
            pMsg.style.display = "block";
            pMsg.innerHTML = message;
        }
        else
            pMsg.style.display = "none";
    }
}

function EditPartEvent(partID, event)
{
    EditPart(partID, GetMousePosition(event));
}

function EditPartEventXY(partID, x, y)
{
    EditPart(partID, GetPosition(x,y));
}

function EditPartEventXYWH(partID, x, y, w, h)
{
    pPart = EditPart(partID, GetPosition(x,y));
    var partStyle = pPart.style || pPart;
    
    partStyle.left = x + "px";
    partStyle.top = y + "px";
    partStyle.width = w ;
    partStyle.height = h ;
}

function EditPart(partID, position)
{
    pPart = GetElementById(partID);
    
    if (pPart == null) return;
     
    dimensions = ScreenDimensions();
    
    var partStyle = pPart.style || pPart;
    
    var width = (pPart.offsetWidth || pPart.width || 100); 
    var height = (pPart.offsetHeight || pPart.height || 100);
    
    var newX = position.X;
    var newY = position.Y;
    
    if (newY <= 0) newY = 10;
    if (newX <= 0) newx = 10;
    if ((newY + height) > dimensions.Height) newY = dimensions.Height - 10 - height;
    if ((newX + width) > dimensions.Width) newY = dimensions.Width - 10 - width;
    
    partStyle.top = newY + "px";
    partStyle.left = newX + "px";
    
    partStyle.visibility = "visible";
    
    return pPart;
}

function GetPartDimensions(partID)
{
    var dimensions = new Object();
    
    var pPart = GetElementById(partID);
    if (partID)
    {
        var partStyle = pPart.style || pPart;
        
        dimensions.X = partStyle.left;
        dimensions.Y = partStyle.top;
        dimensions.W = (pPart.offsetWidth || pPart.width || 100);
        dimensions.H = (pPart.offsetHeight || pPart.height || 100);
    }
    
    return dimensions;
}

function AddDimensionsToURL(partID)
{
    var d = GetPartDimensions(partID);
    
    url = "pd=" + d.X + "|" + d.Y + "|" + d.W + "|" + d.H;
    
    var pForm = GetElementById('aspnetForm');
    if (pForm)
        if (pForm.action.indexOf('?') >= 0)
        {
            if (pForm.action.indexOf('?pd=') >= 0 || pForm.action.indexOf('&pd=') >= 0)
            {
                pds = (pForm.action.indexOf('?pd=') <= 0) ? pForm.action.indexOf('&pd=') : pForm.action.indexOf('?pd=');
                pde = (pForm.action.indexOf('&', pds+1) <= 0) ? pForm.action.length : pForm.action.indexOf('&', pds+1);
                
                newaction = pForm.action.substring(0, pds+1) + url + pForm.action.substring(pde, pForm.action.length);
                
                pForm.action = newaction;
            }
            else
                pForm.action += "&" + url;
        }
        else
            pForm.action += "?" + url;
}

function ClosePart(partID, event)
{
    pPart = GetElementById(partID);
    
    if (pPart == null)
        return;
        
    var partStyle = pPart.style || pPart;
    partStyle.visibility = "hidden";
    
    EditPartErrorMessage(partID, "");
}

var dragobject =
{
    handleObj : null,
    containerObj : null,
    processing : false,
    offsetx : null, 
    offsety : null,
    x : null,
    y : null,
    
    initialize:function()
    {
        // Set Up Event captures
        document.onmousedown = this.startdrag;
        document.onmouseup = this.stopall;
    },
   
    stopall:function(e)
    {
        this.processing = false;
        document.onmousemove = null;
    },
    
    startdrag:function(e)
    {
        var eventObj = window.event ? window.event : e;
        
        // Find object clicked on
        this.handleObj = window.event ? event.srcElement : e.target
        
        // check if it is a drag handle
        if(this.handleObj.className)
        {
            if (this.handleObj.className == "dragHandle" || this.handleObj.className == "resizeHandle")
            {
                // Find container object
                this.containerObj = this.handleObj.parentNode;
                
                while (this.containerObj != null && this.containerObj.className != "dragContainer")
                    this.containerObj = this.containerObj.parentNode;
                
                if (this.containerObj == null)
                    return;
                    
                // Start Dragging
                this.processing = true;
                
                if (isNaN(parseInt(this.containerObj.style.left)))
				    this.containerObj.style.left=0;
    				
			    if (isNaN(parseInt(this.containerObj.style.top)))
				    this.containerObj.style.top=0;
    			
			    this.offsetx = parseInt(this.containerObj.style.left);
			    this.offsety = parseInt(this.containerObj.style.top);
			    this.x = eventObj.clientX;
			    this.y = eventObj.clientY;
    			
			    if (eventObj.preventDefault)
				    eventObj.preventDefault();
    		
    		    if (this.handleObj.className == "dragHandle")
		            document.onmousemove = dragobject.drag;
		            
		        if (this.handleObj.className == "resizeHandle")
		        {
		            document.onmousemove = dragobject.resize;
		            //alert(this.containerObj.style.height);
                }
		    }
        }
    },
    
    resize:function(e)
    {
        var eventObj = window.event? window.event : e;
		
		if (this.processing)
		{
		    try
		    {
		        if (this.containerObj.style.width && eventObj.clientX & this.offsetx)
		            this.containerObj.style.width =  eventObj.clientX - this.offsetx  + "px";
      
		        if (this.containerObj.style.height && eventObj.clientY & this.offsety)
		            this.containerObj.style.height = eventObj.clientY - this.offsety  + "px";
		    }
		    catch (err) {}
		    
		    return false
		}
    },
    
    drag:function(e)
	{
		var eventObj = window.event? window.event : e;
		
		if (this.processing)
		{
		    this.containerObj.style.left = this.offsetx + eventObj.clientX - this.x + "px";
		    this.containerObj.style.top = this.offsety + eventObj.clientY - this.y + "px";
		    return false
		}
	}
    
}

dragobject.initialize();