// name our window so that tool can reference site browser
window.name = "MyCanvas";

var windowArray = new Array();
var newWindow = 0;
function windowOpen(url, name, features) {
    var popupWorked = false;

    try {
        //Open the new window.
        newWindow = window.open(url, name, features);

        //Check to see if popup worked
        if (!newWindow)
            popupWorked = true;
        if (window.opera)
            if (!newWindow.opera)
            popupWorked = true;

        if (window.focus)
            newWindow.focus();

        //Store the window for later use.
        windowArray[name.toString()] = newWindow;
    }
    catch (err) {
        popupWorked = false;
    }

    return popupWorked;
}

function createNewProject() {
    windowOpen('/Flash/MyCanvas.aspx?createNewProject=true', 'tool', 'status=0,location=0,toolbar=0,menubar=0,resizable=1,scrollbars=1,titlebar=0');
}

function makeProduct(productId, themeCatId, themeId) {
	var locString = "/Flash/MyCanvas.aspx?createNewProject=true&productId=" + productId;

    if (themeId != undefined)
        locString += "&themeId=" + themeId;

    if (themeCatId != undefined)
        locString += "&themeCatId=" + themeCatId;

    // Get Tree Id and Person Id variables from window if they exist
    if (window.tid != undefined)
        locString += "&tid=" + tid;
    if (window.pid != undefined)
        locString += "&pid=" + pid;

    windowOpen(locString, 'tool', 'status=0,location=0,toolbar=0,menubar=0,resizable=1,scrollbars=1,titlebar=0');
}


function makeThemedProduct(categoryId, themeId) {
	var locString = "/Flash/MyCanvas.aspx?createNewProject=true&category=" + categoryId;

    if (themeId != undefined)
        locString += "&themeId=" + themeId;

    if (window.tid != undefined)
        locString += "&tid=" + tid;

    if (window.pid != undefined)
        locString += "&pid=" + pid;

    windowOpen(locString, 'tool', 'status=0,location=0,toolbar=0,menubar=0,resizable=1,scrollbars=1,titlebar=0');
}

// Width and height are optional
function makeProductWithCategory(categoryId, width, height) {
	var locString = "/Flash/MyCanvas.aspx?createNewProject=true&category=" + categoryId;

    if (width != undefined)
        locString += "&prefWidth=" + width;

    if (height != undefined)
        locString += "&prefHeight=" + height;

    // Get Tree Id and Person Id variables from window if they exist
    if (window.tid != undefined)
        locString += "&tid=" + tid;
    if (window.pid != undefined)
        locString += "&pid=" + pid;

    windowOpen(locString, 'tool', 'status=0,location=0,toolbar=0,menubar=0,resizable=1,scrollbars=1,titlebar=0');
}


////
// Code for draggable popups
function dragThis(moveElm, grabElm) {
    document.getElementById(grabElm).elmToMove = document.getElementById(moveElm);
    document.getElementById(grabElm).onmousedown = function(e) {
        document.elmToMove = this.elmToMove;
        e = e ? e : window.event;
        this.elmToMove.mouseStart = [e.clientX, e.clientY];
        var scrollY = 0;
        var scrollX = 0;
        if (document.documentElement && document.documentElement.scrollTop) { scrollY = document.documentElement.scrollTop; }
        else if (document.body && document.body.scrollTop) { scrollY = document.body.scrollTop; }
        if (document.documentElement && document.documentElement.scrollLeft) { scrollX = document.documentElement.scrollLeft; }
        else if (document.body && document.body.scrollLeft) { scrollX = document.body.scrollLeft; }
        this.elmToMove.scrollStart = [scrollX, scrollY];
        this.elmToMove.posStart = [Number(this.elmToMove.offsetLeft), Number(this.elmToMove.offsetTop)];
        this.elmToMove.isMoving = true;
    };
}

document.moveDraggableElm = function(elm, x, y, stopMoving) {
    var scrollX = 0;
    var scrollY = 0;
    if (document.documentElement && document.documentElement.scrollLeft) { scrollX = document.documentElement.scrollLeft; }
    else if (document.body && document.body.scrollLeft) { scrollX = document.body.scrollLeft; }
    if (document.documentElement && document.documentElement.scrollTop) { scrollY = document.documentElement.scrollTop; }
    else if (document.body && document.body.scrollTop) { scrollY = document.body.scrollTop; }
    scrollX = elm.scrollStart[0] - scrollX;
    scrollY = elm.scrollStart[1] - scrollY;
    elm.style.left = (x < 0 ? 0 : x) - elm.mouseStart[0] + elm.posStart[0] - scrollX + 'px';
    elm.style.top = (y < 0 ? 0 : y) - elm.mouseStart[1] + elm.posStart[1] - scrollY + 'px';
    elm.isMoving = typeof (stopMoving) == 'undefined' ? true : stopMoving;
};

if (window.addEventListener) {
    document.addEventListener('mousemove', function(e) { var elm = this.elmToMove; if (typeof (elm) != 'undefined' && elm.isMoving) { if (!e) { e = window.event; } document.moveDraggableElm(elm, e.clientX, e.clientY); } }, false);
    document.addEventListener('mouseup', function(e) { var elm = this.elmToMove; if (typeof (elm) != 'undefined' && elm.isMoving) { if (!e) { e = window.event; } document.moveDraggableElm(elm, e.clientX, e.clientY, false); } }, false);
} else {
    document.attachEvent('onmousemove', function(e) { var elm = document.elmToMove; if (typeof (elm) != 'undefined' && elm.isMoving) { if (!e) { e = window.event; } document.moveDraggableElm(elm, e.clientX, e.clientY); } });
    document.attachEvent('onmouseup', function(e) { var elm = document.elmToMove; if (typeof (elm) != 'undefined' && elm.isMoving) { if (!e) { e = window.event; } document.moveDraggableElm(elm, e.clientX, e.clientY, false); } });
}
//
////

////
// Find the absolute position of an div on a webpage
// This javascript is "free to use for any purpose" by Peter Paul Koch and Alex Tingle (http://blog.firetree.net/2005/07/04/javascript-find-position/  accessed: 13Oc09)
function findPosX(obj) {
    var curleft = 0;
    if (obj.offsetParent)
        while (1) {
        curleft += obj.offsetLeft;
        if (!obj.offsetParent)
            break;
        obj = obj.offsetParent;
    }
    else if (obj.x)
        curleft += obj.x;
    return curleft;
}

function findPosY(obj) {
    var curtop = 0;
    if (obj.offsetParent)
        while (1) {
        curtop += obj.offsetTop;
        if (!obj.offsetParent)
            break;
        obj = obj.offsetParent;
    }
    else if (obj.y)
        curtop += obj.y;
    return curtop;
}
//
////

////
// MyProjects share mouseover
// Handles 'more options' rollover popup
if (typeof (document.getElementsByClassName) == 'undefined') { document.getElementsByClassName = function(name, tag, node) { var elmsToReturn = []; if (typeof (node) == 'undefined') { node = document; } else if (typeof (node) == typeof ('string')) { node = document.getElementById(node); } if (typeof (tag) == 'undefined') { tag = '*'; } var elmsList = node.getElementsByTagName(tag); for (var a = 0; a < elmsList.length; a++) { if (elmsList[a].className) { var classesList = elmsList[a].className.split(' '); for (var b = 0; b < classesList.length; b++) { if (classesList[b] == name) { elmsToReturn.push(elmsList[a]); break; } } } } return (elmsToReturn); }; }
//class functions
function hasClassName(node, tempClass) { return node.className.match(new RegExp('(\\s|^)' + tempClass + '(\\s|$)')); }
function addClassName(node, tempClass) { if (!hasClassName(node, tempClass)) { node.className += " " + tempClass; return true; } return false; }
function removeClassName(node, tempClass) { if (hasClassName(node, tempClass)) { var tempExp = new RegExp('(\\s|^)' + tempClass + '(\\s|$)'); node.className = node.className.replace(tempExp, ''); return true; } return false; }

var listOfMoreOpts = document.getElementsByClassName('hideOpts');
for (var linkIndex = 0; linkIndex < listOfMoreOpts.length; linkIndex++) {
    listOfMoreOpts[linkIndex].onmouseover = function() { showOpts(this); };
    listOfMoreOpts[linkIndex].onmouseout = function() { hideOpts(this); };
}

function showOpts(currElm) {
    if (hasClassName(currElm, 'hideOpts')) {
        removeClassName(currElm, 'hideOpts');
        addClassName(currElm, 'showOpts');
    }
}
function hideOpts(currElm) {
    if (hasClassName(currElm, 'showOpts')) {
        removeClassName(currElm, 'showOpts');
        addClassName(currElm, 'hideOpts');
    }
}
//
////

// Handle disable divs that have long postbacks.
function disableDiv(divName) { setTimeout("disableDivTimer('" + divName + "');", 100); }
function disableDivTimer(divName) {
    var div = $get(divName);
    var ctls = div.getElementsByTagName('a');
    for (var i = 0; i < ctls.length; i++) ctls[i].disabled = true;

    ctls = div.getElementsByTagName('input');
    for (i = 0; i < ctls.length; i++) ctls[i].disabled = true;
}


// Use to disable submit buttons.
function disableButton(button, resetDelay) 
{ 
    // If page is not valid, return
    if (typeof(Page_ClientValidate) == 'function') {
        if (Page_ClientValidate() == false) {
            return false;
        }
    }
    
    // else continue
    button.oldonclick = button.onclick;
    button.oldinnerHTML = button.innerHTML;
    button.onclick = new Function("return false;");
    button.innerHTML = "<em>Working...</em>";
    setTimeout("enableButton('" + button.id + "');", resetDelay);
    eval(button.href);
}
function enableButton(buttonId) 
{ 
    var button = document.getElementById(buttonId); 
    if(button!=null)
    {
        button.onclick = button.oldonclick;
        button.innerHTML = button.oldinnerHTML;
    }
}

function showPopup(name) {
    $get(popupNameHid).value = name;

    var args = '';
    for (var i = 1; i < arguments.length; i++) {
        args += arguments[i].toString();
    }

    __doPostBack(popupPanel, args);
}

var prevDiv = null;
function toggleDiv(name, projId, parentDiv, projName) {
    if (projId) $get(projIdHid).value = projId;
    if (projName) $get('projectName').innerHTML = projName;
    var div = $get(name);
    // If parentDiv, set coordinates
    if (parentDiv) {
        var parDiv = $get(parentDiv);
        var parDivX = findPosX(parDiv);
        var parDivY = findPosY(parDiv);

        div.style.left = parDivX + 'px';
        div.style.top = parDivY + 'px';
    }

    if (prevDiv != null && prevDiv != div) {
        prevDiv.style.display = 'none';
    }

    div.style.display = div.style.display == 'none' ? '' : 'none';
    prevDiv = div;
}

var msgChanged = false;
function textChanged(val) {
    msgChanged = val != '';
}
function initMessage() {
    if (msgChanged) return;
    var proCheckbox = $get(ProCheckBox);
    var proHidePrice = $get(ProPricingCheckbox);
    var allowCopy = $get(allowCopyingCheck);
    var msgBox = $get(messageBox);

    if (proCheckbox && proCheckbox.checked) {
        if (allowCopy.checked) {
            msgBox.value = msgIsProCanCopy;
        } else if (proHidePrice && proHidePrice.checked) {
            msgBox.value = msgIsProCantOrderCantCopy;
        } else {
            msgBox.value = msgIsProCanOrderCantCopy;
        }
    } else {
        if (allowCopy.checked) {
            msgBox.value = msgNotProCanCopy;
        } else {
            msgBox.value = msgNotProCantCopy;
        }
    }
}
