﻿function pathBuilder(){
    this.points = new Array()
    
    this.addVert = function(vert){
        this.points[this.points.length] = vert
    }
    
    this.toString = function(){
        if (this.points.length==0) return ''
        var path
        if (renderMode==enumRenderMode['vml']) {
            path = 'M '
            for (var x = 0;x < this.points.length;x++)
                path += Math.round(this.points[x][0]*2) + ',' + Math.round(this.points[x][1]*2) + (x == 0 ? ' L ':' ')
        } else if (renderMode==enumRenderMode['svg']){
            path=''
            for (var x = 0;x < this.points.length;x++)
                path += this.points[x] +' '
        }
        return path
    }
}

function cursorPos(e) {
    //returns mouse coordinates on screen
	var posx = 0
	var posy = 0
	if (e.pageX || e.pageY){
		posx = e.pageX
		posy = e.pageY
		currentMousePos=[posx,posy]
	} else if (e.clientX || e.clientY){
		posx = e.clientX + document.body.scrollLeft
			+ document.documentElement.scrollLeft
		posy = e.clientY + document.body.scrollTop
			+ document.documentElement.scrollTop
	    currentMousePos=[posx,posy]
	} 
	
	return currentMousePos
}

function elementPos(obj) {
    //gets elements location on screen (many assumptions made!)

    if (obj.getBoundingClientRect && browser.browser!='Opera') {
        //It would be much better if opera hadn't even bothered to try and implement this function as it's
        //nothing like any of the other browser's implementations
        //Opera calculates the offsets relative to the page, not the window.
        //Opera then adds on the border widths to the offsets too.
        //Finally, just to make sure its completely unusable it throws in + or - a few pixels so the 
        //calculation is completely useless
    
	    var curleft = obj.getBoundingClientRect().left + Math.max(document.documentElement.scrollLeft, document.body.scrollLeft)
	    var curtop = obj.getBoundingClientRect().top + Math.max(document.documentElement.scrollTop,  document.body.scrollTop)

        objCurrentStyle=getCurrentStyle(obj)
        if (parseInt(objCurrentStyle.borderLeftWidth)) curleft += parseInt(objCurrentStyle.borderLeftWidth)  //assumes borders are only defined in px
        if (parseInt(objCurrentStyle.borderTopWidth)) curtop += parseInt(objCurrentStyle.borderTopWidth)  //assumes borders are only defined in px
                
    } else {
        var curleft=curtop=0;
        if (document.documentElement.clientLeft) curleft = parseInt(document.documentElement.clientLeft)
        if (document.documentElement.clientTop) curtop = parseInt(document.documentElement.clientTop)

	        curleft += obj.offsetLeft 
	        curtop += obj.offsetTop

            var cObj=obj
	        cObj = cObj.offsetParent	            
	        while (cObj != document.body && cObj.offsetParent) {
		        curleft += cObj.offsetLeft 
		        curtop += cObj.offsetTop
		        if (browser.browser!='Opera'){
		            //Once again, opera has a mind of its own
		            objCurrentStyle=getCurrentStyle(cObj)
                    if (parseInt(objCurrentStyle.borderLeftWidth)) curleft += parseInt(objCurrentStyle.borderLeftWidth) //assumes borders are only defined in px
                    if (parseInt(objCurrentStyle.borderTopWidth)) curtop += parseInt(objCurrentStyle.borderTopWidth) //assumes borders are only defined in px
	            }
                cObj = cObj.offsetParent	            
	        }
	    objCurrentStyle=getCurrentStyle(obj)

        if (parseInt(objCurrentStyle.borderLeftWidth)) curleft += parseInt(objCurrentStyle.borderLeftWidth) //assumes borders are only defined in px
        if (parseInt(objCurrentStyle.borderTopWidth)) curtop += parseInt(objCurrentStyle.borderTopWidth) //assumes borders are only defined in px
	}

    
    //Firefox thinks that half pixels exist
	return [Math.round(curleft),Math.round(curtop)];
}

function getCurrentStyle(element){

    if (element.currentStyle)
        return element.currentStyle
    else
        return document.defaultView.getComputedStyle(element, '')
    
}

function cleanURL(url){
    //extracts the url from a css url unit
    var res = url
    if (res.substr(0,3).toLowerCase()=='url')
        res=trim(res.substr(3))
    else
        return ''
        
    if (res.length > 2 && res.substr(0,1)=='(' && res.substr(res.length-1,1)==')')
        res=trim(res.substring(1,res.length-1))
    else
        return ''
    
    if (res.length > 2 && res.substr(0,1)=='\'' && res.substr(res.length-1,1)=='\'')
        res=trim(res.substring(1,res.length-1))
    else if (res.length > 2 && res.substr(0,1)=='\"' && res.substr(res.length-1,1)=='\"')
        res=trim(res.substring(1,res.length-1))
    
    return res
}

function trim(value) {
    //strips spaces from beginning and end of string
   var temp = value;
   var obj = /^(\s*)([\W\w]*)(\b\s*$)/;
   if (obj.test(temp)) { temp = temp.replace(obj, '$2'); }
   var obj = / +/g;
   temp = temp.replace(obj, " ");
   if (temp == " ") { temp = ""; }
   return temp;
}

function insertAfter(newElement,targetElement) {
    var parent = targetElement.parentNode;

    if(parent.lastchild == targetElement) {
        parent.appendChild(newElement);
    } else {
        parent.insertBefore(newElement, targetElement.nextSibling);
    }
}

function formatString(){
    //see documentation for .net command String.Format

    if( arguments.length == 0 )
        return null

    var str = arguments[0]

    for(var i=1;i<arguments.length;i++){
        var re = new RegExp('\\{' + (i-1) + '\\}','gm');
        str = str.replace(re, arguments[i]);
    }
    return str

}