var Paths = {

	InferRootPath : function() {
	
		var subdirectory = window.location.pathname;
		subdirectory = subdirectory.substring(0, subdirectory.lastIndexOf("/"));
		if (subdirectory.indexOf("/id") > -1) {
			subdirectory = subdirectory.substring(0, subdirectory.indexOf("/id"));
		}
		if (subdirectory.indexOf("/profile") > -1) {
			subdirectory = subdirectory.substring(0, subdirectory.indexOf("/profile"));
		}
		return window.location.protocol + "//" + window.location.host + subdirectory;
		
	},

	Init : function() {
	
		var defaultPath = Paths.InferRootPath();
		this.RootPath = defaultPath;
		this.FilePath = defaultPath;
		this.StaticContentRoot = defaultPath;
		this.RegistrationHost = defaultPath;
		this.SearchHost = defaultPath;

		if (typeof(rootpath) != 'undefined')
		{
			this.RootPath = rootpath;
		}

		if (typeof(filepath) != 'undefined')
		{
			this.FilePath = filepath;
		}

		if (typeof(staticContentRoot) != 'undefined')
		{
			this.StaticContentRoot = staticContentRoot;
		}

		if (typeof(registrationHost) != 'undefined')
		{
			this.RegistrationHost = registrationHost;
		}

		if (typeof(searchHost) != 'undefined')
		{
			this.SearchHost = searchHost;
		}
	},

	Current : null

}



FontSizeSlider = {

    init : function(obj, width, caretWidth) //args: obj, width, caretWidth, onAfterResize
    {
        var slider = {
            dragging       : false,
            width          : width,
            percent        : 0,
            origFontSize   : 1,
            lineHeightRate : 0.416667,
            container      : obj,
            caret          : $('div', obj).slice(0, 1),
            caretWidth     : caretWidth
        }

        $(slider.caret).mousedown(function(){
            if(slider.dragging) {return;};
            slider.dragging = true;
        });

        $(document).mousemove(function(e){
            FontSizeSlider.onSliderMouseMove(e, slider);
        });

        $(document).mouseup(function(e){
            FontSizeSlider.onSliderMouseUp(e, slider);
        });
    },

    slideToStart : function(slider)
    {
        this.slideTo(0, slider)
    },

    slideToEnd : function(slider)
    {
        var X = slider.width - slider.caretWidth
        this.slideTo(X, slider);
    },

    slideTo : function(X, slider)
    {
        $(slider.caret).css('left', X + 'px');
        slider.percent = (X / slider.width);
    },

    onSliderMouseUp : function(e, slider)
    {
        if (slider.dragging)
        {
            slider.dragging = false;
            this.onAfterResize(slider);
        }
    },

    onSliderMouseMove : function(e, slider)
    {
        if (!slider.dragging) {return;};
        var MouseP = GetEventXY(e);
        var MouseX = MouseP[0];
        var MouseY = MouseP[1];
        var SliderP = GetPos(slider.container);
        SliderX = getRealLeft(slider.container);

        if (MouseX < (SliderX + (slider.caretWidth / 2))) // left of slider
        {
            this.slideToStart(slider);
        }

        else if (MouseX > (SliderX + slider.width - slider.caretWidth)) // right of slider
        {
            this.slideToEnd(slider);
        }

        else // inside slider
        {
            var X = (MouseX - SliderX - (slider.caretWidth / 2))
            this.slideTo(X, slider);
        }
    },

    onAfterResize : function(slider)
    {

        var NewSize = slider.origFontSize;
        if (slider.percent > 0)
        {
            NewSize = slider.origFontSize + (slider.origFontSize * slider.percent);
        }
        $('#ContentArea').css('font-size', NewSize + 'em');
        $('#ContentArea').css('line-height', NewSize + 'em');
        slider.dragging = false;
    }
}

// Returns the coordinates of and element
function GetEventXY(e) {
    var x, y;
    if (document.all)
    {
        x = event.clientX + document.body.scrollLeft;
        y = event.clientY + document.body.scrollTop;
    }
    else
    {
        x = e.pageX;
        y = e.pageY;
    }
    if (x < 0){
        x = 0;
    }
    if (y < 0){
        y = 0;
    }
    return [x, y];
}

//Returns the true offset position of an element
function GetPos(Obj) {
	var curleft = curtop = 0;
	if (Obj.offsetParent)
	{
		curleft = Obj.offsetLeft;
		curtop = Obj.offsetTop;

		while (Obj == Obj.offsetParent)
		{
			curleft += Obj.offsetLeft;
			curtop += Obj.offsetTop;
		}
	}
	return [curleft, curtop];
}

function getRealLeft(div)
{
	thisObj = div;
	xPos = getRealLeftByObj(div);
	return xPos;
}

// returns true left position of an element
function getRealLeftByObj(thisObj)
{
	xPos = 0;
	if(thisObj)
	{
		xPos = thisObj.offsetLeft;
		tempEl = thisObj.offsetParent;
		while(tempEl != null)
		{
			xPos += tempEl.offsetLeft;
			tempEl = tempEl.offsetParent;
		}
	}

	return xPos;

}