﻿var shoppingListWidth = 0;
var shoppingListHeight = 0;
var isRunning = false;
var animationBegun = false;
function dragStarted(sender, args) {
    if (isRunning === false) {
        isRunning = true;

        $('body').mousemove(dragMove)

        var hitList = $("#ctl00_PageContent_Control_hitlist1_RadGridHitList");
        var gridOffset = hitList.offset();
        $('body').append("<div id='hitlistBlocker' style='position: absolute;width: " + hitList.width() + "px;height: " + ($(document).height() - gridOffset.top) + "px;top: " + gridOffset.top + "px;left: " + gridOffset.left + "px; '></div>");
        if (!shoppingListWidth) {
            shoppingListWidth = $("#shoppingList").width() + 10;
            shoppingListHeight = $("#shoppingList").height();
        }
        setTimeout(
        function () {
            if (isRunning) {
                animationBegun = true;
                $("#shoppingList").addClass('shoppingBasket');
                $("#shoppingList").animate({
                    opacity: 0.7,
                    width: "50%",
                    height: "100px"
                }, 600);

                $("#shoppingListAddHere").show();
            }
        }, 500);
    }
}

function dragStopped(sender, args) {
    $('#hitlistBlocker').remove();
    $('body').unbind('mousemove');
    $('html').unbind('mousemove');
    isRunning = false;
    if (animationBegun) {
        animationBegun = false;
        $("#shoppingList").animate({
            opacity: 1.0,
            width: shoppingListWidth,
            height: shoppingListHeight
        }, 600);

        setTimeout(
        function () {
            $("#shoppingList").removeClass('shoppingBasket');
        }, 600);

        $("#shoppingListAddHere").hide();
    }
    var node = args.get_destinationHtmlElement();
    if (!isChildOf('shoppingList', node)) {
        args.set_cancel(true);
    }
}

function dragMove(event) {
    if (parseInt(event.screenY) <= 150) {
        $("html, body").animate({ scrollTop: 0 }, 200);
    }
}

function isChildOf(parentId, element) {
    while (element) {
        if (element.id && element.id.indexOf(parentId) > -1) {
            return true;
        }
        element = element.parentNode;
    }
    return false;
}

function DropTest(sender, eventArgs)
{
    var source = eventArgs.get_sourceNode();
    var target = eventArgs.get_htmlElement();
        
    if (target.tagName == "TEXTAREA" && source.get_value())
    {
        target.style.cursor = "default";
        target.value = target.value.replace(/(\s+$)/, '')
        var lAddText = "";
        if (target.value != "") {
            if (target.value.match(/(AND|OR)$/i)) {
                lAddText = " " + source.get_value() + " = ";
            }
            else if (target.value.match(/=$/)) {
                lAddText = " % AND " + source.get_value() + " = ";
            }
            else {
                lAddText = " AND " + source.get_value() + " = ";
            }
        }
        else {
            lAddText = source.get_value() + " = ";
        }
        target.value += lAddText;
        target.focus();
    }    
    return false;
}

function DoPrint()
{
    window.print(); 
}

 //rotate elements in hitlist for eldocs
function Rotate(contentList, imgId, linkList, linkId, containerId, direction, description) {
    var imgId = document.getElementById(imgId);
    var linkId = document.getElementById(linkId);
    var container = document.getElementById(containerId);
    var list = contentList.split('|');
    var linkList = linkList.split('|');
    var descriptionList = description.split('|');
    var stupidBrowsers = imgId.className;
    
    if (direction == 'left') {
        imgId.className = (imgId.className == 0) ? list.length-1 : imgId.className-1;
    }
    else {
        imgId.className = ++stupidBrowsers % list.length;
    }
    
    imgId.src = list[imgId.className];
    linkId.href = linkList[imgId.className];
    imgId.title = descriptionList[imgId.className];
    container.style.width = imgId.src.substring(imgId.src.indexOf('imgWidth=')+9, imgId.src.length) + 'px';
}

/*Functions for creating javascript-popup*/
var fTooltipIDName = "TooltipImageControl";

//create an image tooltip
function ShowImageTooltip(aSrcControl, e) {
	if ( e.clientX && e.clientY && document.getElementById && document.createDocumentFragment && document.createElement && document.appendChild ) {
		var lContainer = document.createDocumentFragment();
		var lMain = document.createElement('div');
		lMain.id = fTooltipIDName;
		lMain.style.position = 'absolute';
		lMain.style.border = '1px solid blue';
		lMain.style.padding = '2px';
		lMain.style.backgroundColor = '#ffffff';
		lMain.style.zIndex = '1000';
		lMain.style.opacity = 0; 
        lMain.style.MozOpacity = 0; 
        lMain.style.KhtmlOpacity = 0; 
        lMain.style.filter = "alpha(opacity=" + 0 + ")";

		var lImg = document.createElement('img');
		lImg.src = aSrcControl.src;
		lImg.style.padding = '0px';
		lImg.style.margin = '0px';
		lImg.style.display = 'block';
		lMain.appendChild(lImg);

	    //IE can't detect image size until after the object is added to the document (always returns 0), so make sure IE adds it invisible
	    if (lImg.width == 0) {
	        lMain.style.visibility = 'hidden';
	    }
		if ( (lImg.width > 5 && lImg.height > 5) || lImg.width == 0) {
			SetXYPos(lMain, e);
			lContainer.appendChild(lMain);
			aSrcControl.parentNode.appendChild(lContainer);
			opacity(fTooltipIDName, 0, 100, 400, e);
		}
		if (lImg.width < 5 && lImg.height < 5) {
			ClearTooltip();
		}
		
	}
}

//remove tooltip
function ClearTooltip() {
	if (document.getElementById) {
		var lDeadNodeWalking = document.getElementById(fTooltipIDName);
		if (lDeadNodeWalking) {
			lDeadNodeWalking.parentNode.removeChild(lDeadNodeWalking);
		}
	}
}

//adjusts the tooltip position after mouse
function MoveTooltip(e) {
	if (document.getElementById) {
		var lTooltipNode = document.getElementById(fTooltipIDName);
		if (lTooltipNode) {
			SetXYPos(lTooltipNode, e);
			lTooltipNode.style.visibility = 'visible';
		}
	}
}

function SetXYPos(aTarget, e) {
	var lOverflowY = document.documentElement.clientHeight - (e.clientY + aTarget.firstChild.height + 15);
	lOverflowY = lOverflowY <= 0 ? aTarget.firstChild.height + 10 : 0;
	
	var lOverflowX = document.documentElement.clientWidth - (e.clientX + aTarget.firstChild.width + 15);
	lOverflowX = lOverflowX <= 0 ? aTarget.firstChild.width + 20 : 0;

    if (e.pageX && e.pageY) {
		aTarget.style.left = ((e.pageX + 10) - lOverflowX) + 'px';
		aTarget.style.top = ((e.pageY + 10) - lOverflowY ) + 'px';
	}
	else {
		aTarget.style.left = ((e.clientX + document.documentElement.scrollLeft + 10) - lOverflowX) + 'px';
		aTarget.style.top = ((e.clientY + document.documentElement.scrollTop + 10) - lOverflowY ) + 'px';
	}
}

function opacity(id, opacStart, opacEnd, millisec, e) { 
    var speed = Math.round(millisec / 100); 
    var timer = 0; 
    for(i = opacStart; i <= opacEnd; i++) 
        {
        setTimeout("changeOpac(" + i + ")",(timer * speed)); 
        timer++; 
    } 
} 

//change the opacity for different browsers 
function changeOpac(opacity) { 
    var lTooltipNode = document.getElementById(fTooltipIDName); 
    if (lTooltipNode) {
        lTooltipNode.style.opacity = (opacity / 100); 
        lTooltipNode.style.MozOpacity = (opacity / 100); 
        lTooltipNode.style.KhtmlOpacity = (opacity / 100); 
        lTooltipNode.style.filter = "alpha(opacity=" + opacity + ")";
    }
}

//Functions for emulating text-overflow: elipsis in gecko
function initMozTextOverflow(obj) {

    function re_render() {
        doMozTextOverflow(obj);
    }
    setTimeout(re_render, 0);

}

function OnClientResponseError(sender, args) {
    args.set_cancelErrorAlert(true);
}

function closeToolTip() {
    if (Telerik.Web.UI.RadToolTip != null) {
        var tooltip = Telerik.Web.UI.RadToolTip.getCurrent();
        if (tooltip) {
            tooltip.hide();
        }
    }
}

