﻿/**
 * This file contains all Javascript related to the page elements editor.
 */

// Reserve page administration elements namespace.
if (!window.Administration) {
	window.Administration = {};
}

Administration.TAG_DROPDOWN_ID = "TextElTagSelect";
Administration.TAG_MAPPING = { "kop1": "h1", "kop2": "h2", "kop3": "h3", "kop4": "h4" };

/**
 * This function updates the selected tag whenever a style is changed to a
 * style defined in TAG_MAPPING.
 */
Administration.SyncTextElementTag = function(obj) {
	var id = obj.get_id();
	var tagId = id.substring(0, id.lastIndexOf("_") + 1) + Administration.TAG_DROPDOWN_ID;
	var tagObj = $find(tagId);

	if (!tagObj) return;
	var selected = Administration.trim(obj.get_text().toLowerCase());
	if (Administration.TAG_MAPPING[selected]) {
		var itm = tagObj.findItemByValue(Administration.TAG_MAPPING[selected]);
		if (itm) itm.select();
	}
}

/**
 * Trims a string (copied from http://blog.stevenlevithan.com/archives/faster-trim-javascript)
 */
Administration.trim = function (str) {
	var	str = str.replace(/^\s\s*/, ''),
		ws = /\s/,
		i = str.length;
	while (ws.test(str.charAt(--i)));
	return str.slice(0, i + 1);
}

function alertCommand(dockableObject, command)
{
var adminpanel = document.getElementById(command.Name)
if (adminpanel != null)
{
alert(adminpanel.value + " " + command.Name);
}
else
{
alert("Allowa " + command.Name);    
}
}

function enterValue(target, value)
{
var target = document.getElementById(target) 
if (target != null)
{
target.value = value;
}
}

function test()
{
alert('hoi');
}

function dockStateChanged(dockableObject, command)
{
alert('dockableObject id  = ' + dockableObject.id)
alert('ParentDockingZone id  = ' + dockableObject.ParentDockingZone.id)
alert('Position id  = ' + dockableObject.ParentDockingZone.GetPosition(dockableObject))
}

function myMax(anumber, another)
{
if (anumber > another)
{
return anumber;
}
return another;
}
/*
function ResizeTextArea(txtBox)
{
lines = txtBox.value.split("\n").length;
if (lines > 5)
{
txtBox.setAttribute("rows",lines +1);
}
else
{
txtBox.setAttribute("rows",5);
}
}
*/
function ResizeTextArea(txtBox)
{
    var lvmaxLength = txtBox.getAttribute('maxLength')
    if((lvmaxLength > 0) && (txtBox.value.length > lvmaxLength)){txtBox.value = txtBox.value.substring(0,lvmaxLength); return false};
    
    nCols = txtBox.cols;
    sVal = txtBox.value;
    nVal = sVal.length;
    nRowCnt = 4;
    for (i=0;i<nVal;i++)
        { 
        if (sVal.charAt(i).charCodeAt(0) == 10) 
            { 
                nRowCnt +=1; 
            } 
        }
    nRowCnt = Math.ceil(nRowCnt);
    txtBox.rows = nRowCnt;
    //window.defaultStatus = nRowCnt + ' x ' + nCols + ' (' + nVal + ') ' + sVal.charAt(i) ;
}

function bold(target,unbold)
{
    var target = document.getElementById(target) 
    if (target != null)
    {
        if (unbold != 'true')
        {
            if (target.style.fontWeight == 'bold')
            {
                target.style.fontWeight = 'normal';
            }
            else
            {
                target.style.fontWeight = 'bold';
            }
        }
        else
        {
            target.style.fontWeight = 'normal';
        }        
    }
}

function switchView(Sender, btnStrId, btnTxtId)
{
    var iFrame = document.getElementById('IfrCntEdt');
    var btnStr = document.getElementById(btnStrId);
    var btnTxt = document.getElementById(btnTxtId);
    if (iFrame && btnStr && btnTxt)
        {
        if (Sender == btnTxt)
            {
            iFrame.src = iFrame.getAttribute('txtview');
            btnTxt.style.display = 'none';
            btnStr.style.display = 'inline';
            }
        else
            {
            iFrame.src = iFrame.getAttribute('strview');
            btnStr.style.display = 'none';
            btnTxt.style.display = 'inline';
            }
        }
}

var m_Selected = null;
function ElemStyleChange(Sender, pvEvent)
{
    var tmp = Sender;     

    while (tmp && ((!tmp.tagName) || (tmp.tagName.toLowerCase() != 'div')))
        {
        tmp = tmp.parentNode;
        }

    if (tmp)
        Sender = tmp;

    switch (pvEvent) {
        case "focus" : Sender.className = "core_element_focus";
                       if (m_Selected && Sender != m_Selected) { m_Selected.className = "core_element_out"; }
                       m_Selected = Sender;
                       break;
        case "blur" :  Sender.className = "core_element_out";
                       if (m_Selected) { m_Selected.className = "core_element_out"; m_Selected=null; }                       
                       break;
        case "over" :  if (Sender != m_Selected) Sender.className = "core_element_over";
                       break;
        case "out"  :  if (Sender != m_Selected) Sender.className = "core_element_out";
                       break; 
        default     :  alert(pvEvent);
                       break;
    }
}


