jQuery.fn.corner = function(options) {
    
        
        var settings = {
          tl: { radius: 20 },
          tr: { radius: 20 },
          bl: { radius: 20 },
          br: { radius: 20 },
          antiAlias: true,
          validTags: ["div"] };
        if ( options && typeof(options) != 'string' )
            jQuery.extend(settings, options);


    return this.each(function() {
        new curvyObject(settings,this).applyCorners();
        //new curvyCorners(settings, this ).applyCornersToAll();
    });   
            
};


/****************************************************************
*                                                              *
*  curvyCorners                                                *
*  ------------                                                *
*                                                              *
*  This script generates rounded corners for your divs.        *
*                                                              *
*  Version 1.2.10                                              *
*  Modified by Stefan Holmberg - for jquery usage              *
*  http://www.aspcode.net                                      *
*                                                              *
*  Version 1.2.9                                               *
*  Copyright (c) 2006 Cameron Cooke                            *
*  By: Cameron Cooke and Tim Hutchison.                        *
*                                                              *
*                                                              *
*  Website: http://www.curvycorners.net                        *
*  Email:   info@totalinfinity.com                             *
*  Forum:   http://www.curvycorners.net/forum/                 *
*                                                              *
*                                                              *
*  This library is free software;
you can redistribute         *
*  it and/or modify it under the terms of the GNU              *
*  Lesser General Public License as published by the           *
*  Free Software Foundation;
either version 2.1 of the         *
*  License, or (at your option) any later version.             *
*                                                              *
*  This library is distributed in the hope that it will        *
*  be useful, but WITHOUT ANY WARRANTY;
without even the       *
*  implied warranty of MERCHANTABILITY or FITNESS FOR A        *
*  PARTICULAR PURPOSE. See the GNU Lesser General Public       *
*  License for more details.                                   *
*                                                              *
*  You should have received a copy of the GNU Lesser           *
*  General Public License along with this library;
*
*  Inc., 59 Temple Place, Suite 330, Boston,                   *
*  MA 02111-1307 USA                                           *
*                                                              *
****************************************************************/





function curvyObject()

{
    this.box = arguments[1];
    this.settings = arguments[0];
    this.topContainer = null;
    this.bottomContainer = null;
    this.masterCorners = new Array();
    this.contentDIV = null;
    var boxHeight = get_style(this.box, "height", "height");
    var boxWidth = get_style(this.box, "width", "width");
    var borderWidth = get_style(this.box, "borderTopWidth", "border-top-width");
    var borderColour = get_style(this.box, "borderTopColor", "border-top-color");
    var boxColour = get_style(this.box, "backgroundColor", "background-color");
    var backgroundImage = get_style(this.box, "backgroundImage", "background-image");
    var boxPosition = get_style(this.box, "position", "position");
    var boxPadding = get_style(this.box, "paddingTop", "padding-top");
    this.boxHeight = parseInt(((boxHeight != "" && boxHeight != "auto" && boxHeight.indexOf("%") == -1)? boxHeight.substring(0, boxHeight.indexOf("px")) : this.box.scrollHeight));
    this.boxWidth = parseInt(((boxWidth != "" && boxWidth != "auto" && boxWidth.indexOf("%") == -1)? boxWidth.substring(0, boxWidth.indexOf("px")) : this.box.scrollWidth));
    this.borderWidth = parseInt(((borderWidth != "" && borderWidth.indexOf("px") !== -1)? borderWidth.slice(0, borderWidth.indexOf("px")) : 0));
    this.boxColour = format_colour(boxColour);
    this.boxPadding = parseInt(((boxPadding != "" && boxPadding.indexOf("px") !== -1)? boxPadding.slice(0, boxPadding.indexOf("px")) : 0));
    this.borderColour = format_colour(borderColour);
    this.borderString = this.borderWidth + "px" + " solid " + this.borderColour;
    this.backgroundImage = ((backgroundImage != "none")? backgroundImage : "");
    this.boxContent = this.box.innerHTML;
    if(boxPosition != "absolute") this.box.style.position = "relative";
    this.box.style.padding = "0px";
    
    var isIE = navigator.userAgent.toLowerCase().indexOf("msie") > -1;
    
    if(isIE && boxWidth == "auto" && boxHeight == "auto") this.box.style.width = "100%";
    if(this.settings.autoPad == true && this.boxPadding > 0)
    this.box.innerHTML = "";
    this.applyCorners = function()
    
    {
        for(var t = 0;
        t < 2;
        t++)
        
        {
            switch(t)
            
            {
                case 0:
                if(this.settings.tl || this.settings.tr)
                
                {
                    var newMainContainer = document.createElement("DIV");
                    newMainContainer.style.width = "100%";
                    newMainContainer.style.fontSize = "1px";
                    newMainContainer.style.overflow = "hidden";
                    newMainContainer.style.position = "absolute";
                    newMainContainer.style.paddingLeft = this.borderWidth + "px";
                    newMainContainer.style.paddingRight = this.borderWidth + "px";
                    var topMaxRadius = Math.max(this.settings.tl ? this.settings.tl.radius : 0, this.settings.tr ? this.settings.tr.radius : 0);
                    newMainContainer.style.height = topMaxRadius + "px";
                    newMainContainer.style.top = 0 - topMaxRadius + "px";
                    newMainContainer.style.left = 0 - this.borderWidth + "px";
                    this.topContainer = this.box.appendChild(newMainContainer);
                    
                }
                
                break;
                case 1:
                if(this.settings.bl || this.settings.br)
                
                {
                    var newMainContainer = document.createElement("DIV");
                    newMainContainer.style.width = "100%";
                    newMainContainer.style.fontSize = "1px";
                    newMainContainer.style.overflow = "hidden";
                    newMainContainer.style.position = "absolute";
                    newMainContainer.style.paddingLeft = this.borderWidth + "px";
                    newMainContainer.style.paddingRight = this.borderWidth + "px";
                    var botMaxRadius = Math.max(this.settings.bl ? this.settings.bl.radius : 0, this.settings.br ? this.settings.br.radius : 0);
                    newMainContainer.style.height = botMaxRadius + "px";
                    newMainContainer.style.bottom = 0 - botMaxRadius + "px";
                    newMainContainer.style.left = 0 - this.borderWidth + "px";
                    this.bottomContainer = this.box.appendChild(newMainContainer);
                    
                }
                
                break;
                
            }
            
            
        }
        
        if(this.topContainer) this.box.style.borderTopWidth = "0px";
        if(this.bottomContainer) this.box.style.borderBottomWidth = "0px";
        var corners = ["tr", "tl", "br", "bl"];
        for(var i in corners)
        
        {
            if(i > -1 < 4)
            
            {
                var cc = corners[i];
                if(!this.settings[cc])
                
                {
                    if(((cc == "tr" || cc == "tl") && this.topContainer != null) || ((cc == "br" || cc == "bl") && this.bottomContainer != null))
                    
                    {
                        var newCorner = document.createElement("DIV");
                        newCorner.style.position = "relative";
                        newCorner.style.fontSize = "1px";
                        newCorner.style.overflow = "hidden";
                        if(this.backgroundImage == "")
                        newCorner.style.backgroundColor = this.boxColour;
                        else
                        newCorner.style.backgroundImage = this.backgroundImage;
                        switch(cc)
                        
                        {
                            case "tl":
                            newCorner.style.height = topMaxRadius - this.borderWidth + "px";
                            newCorner.style.marginRight = this.settings.tr.radius - (this.borderWidth*2) + "px";
                            newCorner.style.borderLeft = this.borderString;
                            newCorner.style.borderTop = this.borderString;
                            newCorner.style.left = -this.borderWidth + "px";
                            break;
                            case "tr":
                            newCorner.style.height = topMaxRadius - this.borderWidth + "px";
                            newCorner.style.marginLeft = this.settings.tl.radius - (this.borderWidth*2) + "px";
                            newCorner.style.borderRight = this.borderString;
                            newCorner.style.borderTop = this.borderString;
                            newCorner.style.backgroundPosition = "-" + (topMaxRadius + this.borderWidth) + "px 0px";
                            newCorner.style.left = this.borderWidth + "px";
                            break;
                            case "bl":
                            newCorner.style.height = botMaxRadius - this.borderWidth + "px";
                            newCorner.style.marginRight = this.settings.br.radius - (this.borderWidth*2) + "px";
                            newCorner.style.borderLeft = this.borderString;
                            newCorner.style.borderBottom = this.borderString;
                            newCorner.style.left = -this.borderWidth + "px";
                            newCorner.style.backgroundPosition = "-" + (this.borderWidth) + "px -" + (this.boxHeight + (botMaxRadius + this.borderWidth)) + "px";
                            break;
                            case "br":
                            newCorner.style.height = botMaxRadius - this.borderWidth + "px";
                            newCorner.style.marginLeft = this.settings.bl.radius - (this.borderWidth*2) + "px";
                            newCorner.style.borderRight = this.borderString;
                            newCorner.style.borderBottom = this.borderString;
                            newCorner.style.left = this.borderWidth + "px"
                            newCorner.style.backgroundPosition = "-" + (botMaxRadius + this.borderWidth) + "px -" + (this.boxHeight + (botMaxRadius + this.borderWidth)) + "px";
                            break;
                            
                        }
                        
                        
                    }
                    
                    
                }
                
                else
                
                {
                    if(this.masterCorners[this.settings[cc].radius])
                    
                    {
                        var newCorner = this.masterCorners[this.settings[cc].radius].cloneNode(true);
                        
                    }
                    
                    else
                    
                    {
                        var newCorner = document.createElement("DIV");
                        newCorner.style.height = this.settings[cc].radius + "px";
                        newCorner.style.width = this.settings[cc].radius + "px";
                        newCorner.style.position = "absolute";
                        newCorner.style.fontSize = "1px";
                        newCorner.style.overflow = "hidden";
                        var borderRadius = parseInt(this.settings[cc].radius - this.borderWidth);
                        for(var intx = 0, j = this.settings[cc].radius;
                        intx < j;
                        intx++)
                        
                        {
                            if((intx +1) >= borderRadius)
                            var y1 = -1;
                            else
                            var y1 = (Math.floor(Math.sqrt(Math.pow(borderRadius, 2) - Math.pow((intx+1), 2))) - 1);
                            if(borderRadius != j)
                            
                            {
                                if((intx) >= borderRadius)
                                var y2 = -1;
                                else
                                var y2 = Math.ceil(Math.sqrt(Math.pow(borderRadius,2) - Math.pow(intx, 2)));
                                if((intx+1) >= j)
                                var y3 = -1;
                                else
                                var y3 = (Math.floor(Math.sqrt(Math.pow(j ,2) - Math.pow((intx+1), 2))) - 1);
                                
                            }
                            
                            if((intx) >= j)
                            var y4 = -1;
                            else
                            var y4 = Math.ceil(Math.sqrt(Math.pow(j ,2) - Math.pow(intx, 2)));
                            if(y1 > -1) this.drawPixel(intx, 0, this.boxColour, 100, (y1+1), newCorner, -1, this.settings[cc].radius);
                            if(borderRadius != j)
                            
                            {
                                for(var inty = (y1 + 1);
                                inty < y2;
                                inty++)
                                
                                {
                                    if(this.settings.antiAlias)
                                    
                                    {
                                        if(this.backgroundImage != "")
                                        
                                        {
                                            var borderFract = (pixelFraction(intx, inty, borderRadius) * 100);
                                            if(borderFract < 30)
                                            
                                            {
                                                this.drawPixel(intx, inty, this.borderColour, 100, 1, newCorner, 0, this.settings[cc].radius);
                                                
                                            }
                                            
                                            else
                                            
                                            {
                                                this.drawPixel(intx, inty, this.borderColour, 100, 1, newCorner, -1, this.settings[cc].radius);
                                                
                                            }
                                            
                                            
                                        }
                                        
                                        else
                                        
                                        {
                                            var pixelcolour = BlendColour(this.boxColour, this.borderColour, pixelFraction(intx, inty, borderRadius));
                                            this.drawPixel(intx, inty, pixelcolour, 100, 1, newCorner, 0, this.settings[cc].radius, cc);
                                            
                                        }
                                        
                                        
                                    }
                                    
                                    
                                }
                                
                                if(this.settings.antiAlias)
                                
                                {
                                    if(y3 >= y2)
                                    
                                    {
                                        if (y2 == -1) y2 = 0;
                                        this.drawPixel(intx, y2, this.borderColour, 100, (y3 - y2 + 1), newCorner, 0, 0);
                                        
                                    }
                                    
                                    
                                }
                                
                                else
                                
                                {
                                    if(y3 >= y1)
                                    
                                    {
                                        this.drawPixel(intx, (y1 + 1), this.borderColour, 100, (y3 - y1), newCorner, 0, 0);
                                        
                                    }
                                    
                                    
                                }
                                
                                var outsideColour = this.borderColour;
                                
                            }
                            
                            else
                            
                            {
                                var outsideColour = this.boxColour;
                                var y3 = y1;
                                
                            }
                            
                            if(this.settings.antiAlias)
                            
                            {
                                for(var inty = (y3 + 1);
                                inty < y4;
                                inty++)
                                
                                {
                                    this.drawPixel(intx, inty, outsideColour, (pixelFraction(intx, inty , j) * 100), 1, newCorner, ((this.borderWidth > 0)? 0 : -1), this.settings[cc].radius);
                                    
                                }
                                
                                
                            }
                            
                            
                        }
                        
                        this.masterCorners[this.settings[cc].radius] = newCorner.cloneNode(true);
                        
                    }
                    
                    if(cc != "br")
                    
                    {
                        for(var t = 0, k = newCorner.childNodes.length;
                        t < k;
                        t++)
                        
                        {
                            var pixelBar = newCorner.childNodes[t];
                            var pixelBarTop = parseInt(pixelBar.style.top.substring(0, pixelBar.style.top.indexOf("px")));
                            var pixelBarLeft = parseInt(pixelBar.style.left.substring(0, pixelBar.style.left.indexOf("px")));
                            var pixelBarHeight = parseInt(pixelBar.style.height.substring(0, pixelBar.style.height.indexOf("px")));
                            if(cc == "tl" || cc == "bl")
                            {
                                pixelBar.style.left = this.settings[cc].radius -pixelBarLeft -1 + "px";
                                
                            }
                            
                            if(cc == "tr" || cc == "tl")
                            {
                                pixelBar.style.top = this.settings[cc].radius -pixelBarHeight -pixelBarTop + "px";
                                
                            }
                            
                            switch(cc)
                            
                            {
                                case "tr":
                                pixelBar.style.backgroundPosition = "-" + Math.abs((this.boxWidth - this.settings[cc].radius + this.borderWidth) + pixelBarLeft) + "px -" + Math.abs(this.settings[cc].radius -pixelBarHeight -pixelBarTop - this.borderWidth) + "px";
                                break;
                                case "tl":
                                pixelBar.style.backgroundPosition = "-" + Math.abs((this.settings[cc].radius -pixelBarLeft -1) - this.borderWidth) + "px -" + Math.abs(this.settings[cc].radius -pixelBarHeight -pixelBarTop - this.borderWidth) + "px";
                                break;
                                case "bl":
                                pixelBar.style.backgroundPosition = "-" + Math.abs((this.settings[cc].radius -pixelBarLeft -1) - this.borderWidth) + "px -" + Math.abs((this.boxHeight + this.settings[cc].radius + pixelBarTop) -this.borderWidth) + "px";
                                break;
                                
                            }
                            
                            
                        }
                        
                        
                    }
                    
                    
                }
                
                if(newCorner)
                
                {
                    switch(cc)
                    
                    {
                        case "tl":
                        if(newCorner.style.position == "absolute") newCorner.style.top = "0px";
                        if(newCorner.style.position == "absolute") newCorner.style.left = "0px";
                        if(this.topContainer) this.topContainer.appendChild(newCorner);
                        break;
                        case "tr":
                        if(newCorner.style.position == "absolute") newCorner.style.top = "0px";
                        if(newCorner.style.position == "absolute") newCorner.style.right = "0px";
                        if(this.topContainer) this.topContainer.appendChild(newCorner);
                        break;
                        case "bl":
                        if(newCorner.style.position == "absolute") newCorner.style.bottom = "0px";
                        if(newCorner.style.position == "absolute") newCorner.style.left = "0px";
                        if(this.bottomContainer) this.bottomContainer.appendChild(newCorner);
                        break;
                        case "br":
                        if(newCorner.style.position == "absolute") newCorner.style.bottom = "0px";
                        if(newCorner.style.position == "absolute") newCorner.style.right = "0px";
                        if(this.bottomContainer) this.bottomContainer.appendChild(newCorner);
                        break;
                        
                    }
                    
                    
                }
                
                
            }
            
            
        }
        
        var radiusDiff = new Array();
        radiusDiff["t"] = Math.abs(this.settings.tl.radius - this.settings.tr.radius)
        radiusDiff["b"] = Math.abs(this.settings.bl.radius - this.settings.br.radius);
        for(z in radiusDiff)
        
        {
            if(z == "t" || z == "b")
            
            {
                if(radiusDiff[z])
                
                {
                    var smallerCornerType = ((this.settings[z + "l"].radius < this.settings[z + "r"].radius)? z +"l" : z +"r");
                    var newFiller = document.createElement("DIV");
                    newFiller.style.height = radiusDiff[z] + "px";
                    newFiller.style.width = this.settings[smallerCornerType].radius+ "px"
                    newFiller.style.position = "absolute";
                    newFiller.style.fontSize = "1px";
                    newFiller.style.overflow = "hidden";
                    newFiller.style.backgroundColor = this.boxColour;
                    switch(smallerCornerType)
                    
                    {
                        case "tl":
                        newFiller.style.bottom = "0px";
                        newFiller.style.left = "0px";
                        newFiller.style.borderLeft = this.borderString;
                        this.topContainer.appendChild(newFiller);
                        break;
                        case "tr":
                        newFiller.style.bottom = "0px";
                        newFiller.style.right = "0px";
                        newFiller.style.borderRight = this.borderString;
                        this.topContainer.appendChild(newFiller);
                        break;
                        case "bl":
                        newFiller.style.top = "0px";
                        newFiller.style.left = "0px";
                        newFiller.style.borderLeft = this.borderString;
                        this.bottomContainer.appendChild(newFiller);
                        break;
                        case "br":
                        newFiller.style.top = "0px";
                        newFiller.style.right = "0px";
                        newFiller.style.borderRight = this.borderString;
                        this.bottomContainer.appendChild(newFiller);
                        break;
                        
                    }
                    
                    
                }
                
                var newFillerBar = document.createElement("DIV");
                newFillerBar.style.position = "relative";
                newFillerBar.style.fontSize = "1px";
                newFillerBar.style.overflow = "hidden";
                newFillerBar.style.backgroundColor = this.boxColour;
                newFillerBar.style.backgroundImage = this.backgroundImage;
                switch(z)
                
                {
                    case "t":
                    if(this.topContainer)
                    
                    {
                        if(this.settings.tl.radius && this.settings.tr.radius)
                        
                        {
                            newFillerBar.style.height = topMaxRadius - this.borderWidth + "px";
                            newFillerBar.style.marginLeft = this.settings.tl.radius - this.borderWidth + "px";
                            newFillerBar.style.marginRight = this.settings.tr.radius - this.borderWidth + "px";
                            newFillerBar.style.borderTop = this.borderString;
                            if(this.backgroundImage != "")
                            newFillerBar.style.backgroundPosition = "-" + (topMaxRadius + this.borderWidth) + "px 0px";
                            this.topContainer.appendChild(newFillerBar);
                            
                        }
                        
                        this.box.style.backgroundPosition = "0px -" + (topMaxRadius - this.borderWidth) + "px";
                        
                    }
                    
                    break;
                    case "b":
                    if(this.bottomContainer)
                    
                    {
                        if(this.settings.bl.radius && this.settings.br.radius)
                        
                        {
                            newFillerBar.style.height = botMaxRadius - this.borderWidth + "px";
                            newFillerBar.style.marginLeft = this.settings.bl.radius - this.borderWidth + "px";
                            newFillerBar.style.marginRight = this.settings.br.radius - this.borderWidth + "px";
                            newFillerBar.style.borderBottom = this.borderString;
                            if(this.backgroundImage != "")
                            newFillerBar.style.backgroundPosition = "-" + (botMaxRadius + this.borderWidth) + "px -" + (this.boxHeight + (topMaxRadius + this.borderWidth)) + "px";
                            this.bottomContainer.appendChild(newFillerBar);
                            
                        }
                        
                        
                    }
                    
                    break;
                    
                }
                
                
            }
            
            
        }
        
        if(this.settings.autoPad == true && this.boxPadding > 0)
        
        {
            var contentContainer = document.createElement("DIV");
            contentContainer.style.position = "relative";
            contentContainer.innerHTML = this.boxContent;
            contentContainer.className = "autoPadDiv";
            var topPadding = Math.abs(topMaxRadius - this.boxPadding);
            var botPadding = Math.abs(botMaxRadius - this.boxPadding);
            if(topMaxRadius < this.boxPadding)
            contentContainer.style.paddingTop = topPadding + "px";
            if(botMaxRadius < this.boxPadding)
            contentContainer.style.paddingBottom = botMaxRadius + "px";
            contentContainer.style.paddingLeft = this.boxPadding + "px";
            contentContainer.style.paddingRight = this.boxPadding + "px";
            this.contentDIV = this.box.appendChild(contentContainer);
            
        }
        
        
    }
    
    this.drawPixel = function(intx, inty, colour, transAmount, height, newCorner, image, cornerRadius)
    
    {
        var pixel = document.createElement("DIV");
        pixel.style.height = height + "px";
        pixel.style.width = "1px";
        pixel.style.position = "absolute";
        pixel.style.fontSize = "1px";
        pixel.style.overflow = "hidden";
        var topMaxRadius = Math.max(this.settings["tr"].radius, this.settings["tl"].radius);
        if(image == -1 && this.backgroundImage != "")
        
        {
            pixel.style.backgroundImage = this.backgroundImage;
            pixel.style.backgroundPosition = "-" + (this.boxWidth - (cornerRadius - intx) + this.borderWidth) + "px -" + ((this.boxHeight + topMaxRadius + inty) -this.borderWidth) + "px";
            
        }
        
        else
        
        {
            pixel.style.backgroundColor = colour;
            
        }
        
        if (transAmount != 100)
        setOpacity(pixel, transAmount);
        pixel.style.top = inty + "px";
        pixel.style.left = intx + "px";
        newCorner.appendChild(pixel);
        
    }
    
    
}

function insertAfter(parent, node, referenceNode)

{
    parent.insertBefore(node, referenceNode.nextSibling);
    
}

function BlendColour(Col1, Col2, Col1Fraction)

{
    var red1 = parseInt(Col1.substr(1,2),16);
    var green1 = parseInt(Col1.substr(3,2),16);
    var blue1 = parseInt(Col1.substr(5,2),16);
    var red2 = parseInt(Col2.substr(1,2),16);
    var green2 = parseInt(Col2.substr(3,2),16);
    var blue2 = parseInt(Col2.substr(5,2),16);
    if(Col1Fraction > 1 || Col1Fraction < 0) Col1Fraction = 1;
    var endRed = Math.round((red1 * Col1Fraction) + (red2 * (1 - Col1Fraction)));
    if(endRed > 255) endRed = 255;
    if(endRed < 0) endRed = 0;
    var endGreen = Math.round((green1 * Col1Fraction) + (green2 * (1 - Col1Fraction)));
    if(endGreen > 255) endGreen = 255;
    if(endGreen < 0) endGreen = 0;
    var endBlue = Math.round((blue1 * Col1Fraction) + (blue2 * (1 - Col1Fraction)));
    if(endBlue > 255) endBlue = 255;
    if(endBlue < 0) endBlue = 0;
    return "#" + IntToHex(endRed)+ IntToHex(endGreen)+ IntToHex(endBlue);
    
}

function IntToHex(strNum)

{
    base = strNum / 16;
    rem = strNum % 16;
    base = base - (rem / 16);
    baseS = MakeHex(base);
    remS = MakeHex(rem);
    return baseS + '' + remS;
    
}

function MakeHex(x)

{
    if((x >= 0) && (x <= 9))
    
    {
        return x;
        
    }
    
    else
    
    {
        switch(x)
        
        {
            case 10: return "A";
            case 11: return "B";
            case 12: return "C";
            case 13: return "D";
            case 14: return "E";
            case 15: return "F";
            
        }
        
        
    }
    
    
}

function pixelFraction(x, y, r)

{
    var pixelfraction = 0;
    var xvalues = new Array(1);
    var yvalues = new Array(1);
    var point = 0;
    var whatsides = "";
    var intersect = Math.sqrt((Math.pow(r,2) - Math.pow(x,2)));
    if ((intersect >= y) && (intersect < (y+1)))
    
    {
        whatsides = "Left";
        xvalues[point] = 0;
        yvalues[point] = intersect - y;
        point = point + 1;
        
    }
    
    var intersect = Math.sqrt((Math.pow(r,2) - Math.pow(y+1,2)));
    if ((intersect >= x) && (intersect < (x+1)))
    
    {
        whatsides = whatsides + "Top";
        xvalues[point] = intersect - x;
        yvalues[point] = 1;
        point = point + 1;
        
    }
    
    var intersect = Math.sqrt((Math.pow(r,2) - Math.pow(x+1,2)));
    if ((intersect >= y) && (intersect < (y+1)))
    
    {
        whatsides = whatsides + "Right";
        xvalues[point] = 1;
        yvalues[point] = intersect - y;
        point = point + 1;
        
    }
    
    var intersect = Math.sqrt((Math.pow(r,2) - Math.pow(y,2)));
    if ((intersect >= x) && (intersect < (x+1)))
    
    {
        whatsides = whatsides + "Bottom";
        xvalues[point] = intersect - x;
        yvalues[point] = 0;
        
    }
    
    switch (whatsides)
    
    {
        case "LeftRight":
        pixelfraction = Math.min(yvalues[0],yvalues[1]) + ((Math.max(yvalues[0],yvalues[1]) - Math.min(yvalues[0],yvalues[1]))/2);
        break;
        case "TopRight":
        pixelfraction = 1-(((1-xvalues[0])*(1-yvalues[1]))/2);
        break;
        case "TopBottom":
        pixelfraction = Math.min(xvalues[0],xvalues[1]) + ((Math.max(xvalues[0],xvalues[1]) - Math.min(xvalues[0],xvalues[1]))/2);
        break;
        case "LeftBottom":
        pixelfraction = (yvalues[0]*xvalues[1])/2;
        break;
        default:
        pixelfraction = 1;
        
    }
    
    return pixelfraction;
    
}

function rgb2Hex(rgbColour)

{
    try
    {
        var rgbArray = rgb2Array(rgbColour);
        var red = parseInt(rgbArray[0]);
        var green = parseInt(rgbArray[1]);
        var blue = parseInt(rgbArray[2]);
        var hexColour = "#" + IntToHex(red) + IntToHex(green) + IntToHex(blue);
        
    }
    
    catch(e)
    {
        alert("There was an error converting the RGB value to Hexadecimal in function rgb2Hex");
        
    }
    
    return hexColour;
    
}

function rgb2Array(rgbColour)

{
    var rgbValues = rgbColour.substring(4, rgbColour.indexOf(")"));
    var rgbArray = rgbValues.split(", ");
    return rgbArray;
    
}

function setOpacity(obj, opacity)

{
    var isSafari = ((navigator.userAgent.toLowerCase().indexOf('safari')!=-1)&&(navigator.userAgent.toLowerCase().indexOf('mac')!=-1))?true:false;
    opacity = (opacity == 100)?99.999:opacity;
    if(isSafari && obj.tagName != "IFRAME")
    
    {
        var rgbArray = rgb2Array(obj.style.backgroundColor);
        var red = parseInt(rgbArray[0]);
        var green = parseInt(rgbArray[1]);
        var blue = parseInt(rgbArray[2]);
        obj.style.backgroundColor = "rgba(" + red + ", " + green + ", " + blue + ", " + opacity/100 + ")";
        
    }
    
    else if(typeof(obj.style.opacity) != "undefined")
    
    {
        obj.style.opacity = opacity/100;
        
    }
    
    else if(typeof(obj.style.MozOpacity) != "undefined")
    
    {
        obj.style.MozOpacity = opacity/100;
        
    }
    
    else if(typeof(obj.style.filter) != "undefined")
    
    {
        obj.style.filter = "alpha(opacity:" + opacity + ")";
        
    }
    
    else if(typeof(obj.style.KHTMLOpacity) != "undefined")
    
    {
        obj.style.KHTMLOpacity = opacity/100;
        
    }
    
    
}

function inArray(array, value)

{
    for(var i = 0;
    i < array.length;
    i++)
    {
        if (array[i] === value) return i;
        
    }
    
    return false;
    
}

function inArrayKey(array, value)

{
    for(key in array)
    {
        if(key === value) return true;
        
    }
    
    return false;
    
}

function format_colour(colour)

{
    var returnColour = "#ffffff";
    if(colour != "" && colour != "transparent")
    
    {
        if(colour.substr(0, 3) == "rgb")
        
        {
            returnColour = rgb2Hex(colour);
            
        }
        
        else if(colour.length == 4)
        
        {
            returnColour = "#" + colour.substring(1, 2) + colour.substring(1, 2) + colour.substring(2, 3) + colour.substring(2, 3) + colour.substring(3, 4) + colour.substring(3, 4);
            
        }
        
        else
        
        {
            returnColour = colour;
            
        }
        
        
    }
    
    return returnColour;
    
}

function get_style(obj, property, propertyNS)

{
    var isSafari = ((navigator.userAgent.toLowerCase().indexOf('safari')!=-1)&&(navigator.userAgent.toLowerCase().indexOf('mac')!=-1))?true:false;
    try
    
    {
        if(obj.currentStyle)
        
        {
            var returnVal = eval("obj.currentStyle." + property);
            
        }
        
        else
        
        {
            if(isSafari && obj.style.display == "none")
            
            {
                obj.style.display = "";
                var wasHidden = true;
                
            }
            
            var returnVal = document.defaultView.getComputedStyle(obj, '').getPropertyValue(propertyNS);
            if(isSafari && wasHidden)
            
            {
                obj.style.display = "none";
                
            }
            
            
        }
        
        
    }
    
    catch(e)
    
    {
        
    }
    
    return returnVal;
    
}

function newCurvyError(errorMessage)

{
    return new Error("curvyCorners Error:\n" + errorMessage)
    
}


/**
 * @author Alexandre Magno
 * @desc Center a element with jQuery
 * @version 1.0
 * @example
 * $("element").center({
 *
 *      vertical: true,
 *      horizontal: true
 *
 * });
 * @obs With no arguments, the default is above
 * @license free
 * @param bool vertical, bool horizontal
 * @contribution Paulo Radichi
 *
 */
jQuery.fn.center = function(params) {

        var options = {

            vertical: true,
            horizontal: true

        }
        op = jQuery.extend(options, params);

   return this.each(function(){

        //initializing variables
        var $self = jQuery(this);
        //get the dimensions using dimensions plugin
        var width = $self.width();
        var height = $self.height();
        //get the paddings
        var paddingTop = parseInt($self.css("padding-top"));
        var paddingBottom = parseInt($self.css("padding-bottom"));
        //get the borders
        var borderTop = parseInt($self.css("border-top-width"));
        var borderBottom = parseInt($self.css("border-bottom-width"));
        //get the media of padding and borders
        var mediaBorder = (borderTop+borderBottom)/2;
        var mediaPadding = (paddingTop+paddingBottom)/2;
        //get the type of positioning
        var positionType = $self.parent().css("position");
        // get the half minus of width and height
        var halfWidth = (width/2)*(-1);
        var halfHeight = ((height/2)*(-1))-mediaPadding-mediaBorder;
        // initializing the css properties
        var cssProp = {
            position: 'absolute'
        };

        if(op.vertical) {
            cssProp.height = height;
            cssProp.top = '50%';
            cssProp.marginTop = halfHeight;
        }
        if(op.horizontal) {
            cssProp.width = width;
            cssProp.left = '50%';
            cssProp.marginLeft = halfWidth;
        }
        //check the current position
        if(positionType == 'static') {
            $self.parent().css("position","relative");
        }
        //aplying the css
        $self.css(cssProp);


   });

};

/**
 * jQuery.timers - Timer abstractions for jQuery
 * Written by Blair Mitchelmore (blair DOT mitchelmore AT gmail DOT com)
 * Licensed under the WTFPL (http://sam.zoy.org/wtfpl/).
 * Date: 2009/10/16
 *
 * @author Blair Mitchelmore
 * @version 1.2
 *
 **/

jQuery.fn.extend({
    everyTime: function(interval, label, fn, times) {
        return this.each(function() {
            jQuery.timer.add(this, interval, label, fn, times);
        });
    },
    oneTime: function(interval, label, fn) {
        return this.each(function() {
            jQuery.timer.add(this, interval, label, fn, 1);
        });
    },
    stopTime: function(label, fn) {
        return this.each(function() {
            jQuery.timer.remove(this, label, fn);
        });
    }
});

jQuery.extend({
    timer: {
        global: [],
        guid: 1,
        dataKey: "jQuery.timer",
        regex: /^([0-9]+(?:\.[0-9]*)?)\s*(.*s)?$/,
        powers: {
            // Yeah this is major overkill...
            'ms': 1,
            'cs': 10,
            'ds': 100,
            's': 1000,
            'das': 10000,
            'hs': 100000,
            'ks': 1000000
        },
        timeParse: function(value) {
            if (value == undefined || value == null)
                return null;
            var result = this.regex.exec(jQuery.trim(value.toString()));
            if (result[2]) {
                var num = parseFloat(result[1]);
                var mult = this.powers[result[2]] || 1;
                return num * mult;
            } else {
                return value;
            }
        },
        add: function(element, interval, label, fn, times) {
            var counter = 0;
            
            if (jQuery.isFunction(label)) {
                if (!times) 
                    times = fn;
                fn = label;
                label = interval;
            }
            
            interval = jQuery.timer.timeParse(interval);

            if (typeof interval != 'number' || isNaN(interval) || interval < 0)
                return;

            if (typeof times != 'number' || isNaN(times) || times < 0) 
                times = 0;
            
            times = times || 0;
            
            var timers = jQuery.data(element, this.dataKey) || jQuery.data(element, this.dataKey, {});
            
            if (!timers[label])
                timers[label] = {};
            
            fn.timerID = fn.timerID || this.guid++;
            
            var handler = function() {
                if ((++counter > times && times !== 0) || fn.call(element, counter) === false)
                    jQuery.timer.remove(element, label, fn);
            };
            
            handler.timerID = fn.timerID;
            
            if (!timers[label][fn.timerID])
                timers[label][fn.timerID] = window.setInterval(handler,interval);
            
            this.global.push( element );
            
        },
        remove: function(element, label, fn) {
            var timers = jQuery.data(element, this.dataKey), ret;
            
            if ( timers ) {
                
                if (!label) {
                    for ( label in timers )
                        this.remove(element, label, fn);
                } else if ( timers[label] ) {
                    if ( fn ) {
                        if ( fn.timerID ) {
                            window.clearInterval(timers[label][fn.timerID]);
                            delete timers[label][fn.timerID];
                        }
                    } else {
                        for ( var fn in timers[label] ) {
                            window.clearInterval(timers[label][fn]);
                            delete timers[label][fn];
                        }
                    }
                    
                    for ( ret in timers[label] ) break;
                    if ( !ret ) {
                        ret = null;
                        delete timers[label];
                    }
                }
                
                for ( ret in timers ) break;
                if ( !ret ) 
                    jQuery.removeData(element, this.dataKey);
            }
        }
    }
});

jQuery(window).bind("unload", function() {
    jQuery.each(jQuery.timer.global, function(index, item) {
        jQuery.timer.remove(item);
    });
});
// Infowin class for displaying a miniature info window. Does not
// respond to any events - so you should show and remove the
// overlay yourself as necessary.

function Infowin(latlng, html) {
    this.latlng_ = latlng;
    this.html_ = html;
    this.prototype = new GOverlay();

    // Creates the DIV representing the infowindow
    this.initialize = function(map) {
        var div = $('<div />');
        div.css({
            position : 'absolute',
            width : 234
        }).appendTo(map.getPane(G_MAP_FLOAT_PANE))
    
        this.map_ = map;
        this.div_ = div;

        this.update(html);
    }

    this.update = function(html){
        this.html_ = html;
        
        this.div_.empty();
        
        $('<div />').css({
            'background-image' : 'url(/static/images/infow-top.png)',
            height : 14,
            padding: '0 0 0 0'
        }).appendTo(this.div_);

        var content = $('<div/>').addClass('infowin-content').css({
            'position' : 'relative',
            'overflow' : 'hidden',
            'max-height' : 100,
            'top' : -5
        }).html(html);
        
        $('<div />').css({
            'background-image' : 'url(/static/images/infow-bottom.png)',
            'background-position' : 'bottom left',
            'padding' : '0 10px 25px 10px'
        }).append(content).appendTo(this.div_);
        
        this.redraw(true);
    }
    
    // Remove the main DIV from the map pane
    this.remove = function() {
      this.div_.remove();
    }

    // Copy our data to a new instance
    this.copy = function() {
      return new Infowin(this.latlng_, this.html_);
    }

    // Redraw based on the current projection and zoom level
    this.redraw = function(force) {
        if (!force) return;

        var point = this.map_.fromLatLngToDivPixel(this.latlng_);

        // Now position our DIV based on the DIV coordinates of our bounds
        this.div_.css({
            left : point.x - 108,
            top : point.y - this.div_.height() - 22
        });
    }
}

// holder til en hver tid overlayet som vises på kartet
var path;
var path_zoomlevel;

function renderPath(map) {


    if (path) {
        map.removeOverlay(path);
    }

    var zoom = map.getZoom();
    opera.postError('Zoomlevel: ' + zoom);
    if (zoom <= 9 ) {
        path = generatePath(bergensbanen1km);
        opera.postError('Redering 1km');
    } 
    else if (zoom <= 12) {
        path = generatePath(bergensbanen250m);
        opera.postError('Redering 250m');
    }
    else if (zoom <= 13) {
        path = generatePath(bergensbanen50m);
        opera.postError('Redering 50m');
    }
    else if (zoom <= 14) {
        path = generatePath(bergensbanen10m);
        opera.postError('Redering 10m');
    }
    else if (zoom <= 15) {
        path = generatePath(bergensbanen1m);
        opera.postError('Redering 1m');
    }

    map.addOverlay(path);
    
}

function generatePath(points) {
    var p = new Array();
    for(var i=0;i<points.length;i++) {
        p.push(new GLatLng(points[i].lat, points[i].lon));
    }
    var l = new GPolyline(p, "#d52b1e", 8);
    GEvent.addListener(l, 'click', function(p) {
        pathClick( p.lat(), p.lng() );
    });

    return l;
}





var defaultCenter = new GLatLng(60.0182, 7.7398);
var defaultZoom = 8;
var frame = 1;
var frame_last = 52108;
var geoFrame = new Object();
var playing = 0;
var kontroll_timer;
var sta = new Array();

var map;

$(document).ready( function() {

    // Create the map, with defailt map and controls
    map = new GMap2(document.getElementById("map"));
    map.setMapType(G_NORMAL_MAP);
    map.setCenter(defaultCenter, defaultZoom);
    map.setUIToDefault();


    $("#blackout").show();
    $("#splash").corner().center().show();

    $("#splash").click(function() {

        $("#splash").fadeOut("normal");
        $("#blackout").hide(); 
        map.setCenter(new GLatLng(60.3914, 5.5975), 11);
        
        $("#image").attr('src', 'http://bb.quai.no:8080/320/' + frame);    
        $("#kontroll").corner().draggable().show();
        $("#slider").slider({
            min:0,
            max:320,
            start: function(e,ui) {
                $("#kontroll").draggable('destroy'); 
                if (playing) {
                    $(document).stopTime('nextframe');
                }
            },
            stop: function(e,ui) {
                $("#kontroll").draggable(); 
                if (playing) {
                    $("#button_play").trigger('click');
                }
            },
            slide: function(e,ui) {
                var newf = Math.floor( (frame_last / 320) * ui.value);
                if (newf == 0) { newf = 1 }
                document.getElementById('altslide').style.left = (4 + ui.value) + 'px';  
                if (newf != frame) {
                    frame = newf;
                    updateFrame();
                    updateTrain();
                 }
            },
            change: function(e,ui) {
                frame = Math.floor( (frame_last / 320) * ui.value);
                if (frame == 0) { frame = 1 }
                document.getElementById('altslide').style.left = (4 + ui.value) + 'px';  
                updateFrame();
                updateTrain();
            }
        });

        $("#button_start").click(function() { frame=1; updateFrame(); updateSlide(); updateTrain(); });
        $("#button_end").click(function() { frame=frame_last; updateFrame(); updateSlide(); updateTrain(); });
        

        $("#button_play").click(function() {
            $(document).stopTime('nextframe');
            playing = 1;
            $(document).everyTime(500,"nextframe", function() {
                frame = frame + 1;
                updateFrame();
                updateSlide();
                updateTrain();
                if (frame == frame_last) { $("#button_stop").trigger('click'); }
            });
        });

        $("#button_fastforward").click(function() { frame = frame + 10; updateFrame(); updateSlide(); updateTrain(); });
        $("#button_rewind").click(function() { frame = frame - 10; updateFrame(); updateSlide(); updateTrain(); });
        $("#button_stop").click(function() { playing = 0; $(document).stopTime('nextframe'); });

        // Autorun! ;P
        $("#button_play").trigger('click');
    });

    // Render the path
    GEvent.addListener(map, 'zoomend', function(a,b) { 
        renderPath(map);
        isTrainOutsideViewport();
    }); renderPath(map);

    GEvent.addListener(map, 'moveend', function() { isTrainOutsideViewport(); });
    showStation();
//    showWikipedia();
    updateTrain();

});
function updateFrame() {
    $("#image").attr('src', 'http://bb.quai.no:8080/320/' + frame);
    $("#timecode").text( getTimecode(frame) );
}

function isTrainOutsideViewport() {

    var t = train.getLatLng();
    var b = map.getBounds();

    if (!b.containsLatLng(t)) {
        $("#outofviewport").show();
        $("#outofviewport").click(function() {
            map.setCenter(train.getLatLng());
        });
        
    } else {
        $("#outofviewport").hide();
    }

}

function updateSlide() {
    $("#slider").slider('value', (320/frame_last) * frame);
}

function showStation() {
    var baseIcon = new GIcon(G_DEFAULT_ICON);
    baseIcon.iconSize = new GSize(32,37);
    baseIcon.shadowSize = new GSize(0,0);
    baseIcon.iconAnchor = new GPoint(16, 37);
    baseIcon.infoWindowAnchor = new GPoint(27,6);
    var li = new GIcon(baseIcon);
    li.image = '/static/images/markers/tram.png';
    var mo = { icon: li };

    for(var i=0;i<station.length;i++) {
        var s = station[i];
        sta[i] = new GMarker(new GLatLng(s.lat, s.lon), mo);
        sta[i].station = s;
        GEvent.addListener(sta[i], "click", function() {
            // Bygg litt html

            var html = "<b>" + this.station.name + "</b>";

            if(this.station["høyde"]) {
                html = html + " (" + this.station["høyde"] + " <acronym title='meter over havet'>moh</acronym>.)";
            }

            html = html + '<br/><br/>';

            if ( this.station["sted"] ) {
                html = html + "Sted: " + this.station["sted"];
            }

            if ( this.station["kommune"] ) {
                html = html + " i " + this.station["kommune"] + " kommune";
            }

            html = html + '<br/>';

            if ( this.station["distanse"] ) {
                html = html + "Distanse til Oslo S: " + this.station["distanse"] + "km<br/>";
            }

            if ( this.station["åpnet"] ) {
                html = html + "Åpnet år: " +  this.station["åpnet"] + "<br/>";
            }

            html = html + '<br/>Kilde: <a href="http://no.wikipedia.org/wiki/' + this.station.wp + '" target="_blank">Wikipedia</a>';

            this.openInfoWindowHtml(html);
        });
        map.addOverlay(sta[i]);
    }
}

var w = new Array();
function showWikipedia() {
    var baseIcon = new GIcon(G_DEFAULT_ICON);
    baseIcon.shadow = "";
    baseIcon.iconSize = new GSize(17,16);
    baseIcon.shadowSize = new GSize(0,0);
    baseIcon.iconAnchor = new GPoint(8, 8);
     baseIcon.infoWindowAnchor = new GPoint(9, 2);
    var li = new GIcon(baseIcon);
    li.image = '/static/images/wikicon-small.png';
    var mo = { icon: li };

    for(var i=0;i<wikipedia.length;i++) {
        w[i] = new GMarker(new GLatLng(wikipedia[i].lat, wikipedia[i].lng), mo);
        map.addOverlay(w[i]);
    }
}

var train;
function updateTrain() {
    var baseIcon = new GIcon(G_DEFAULT_ICON);
    baseIcon.iconSize = new GSize(25,15);
    baseIcon.shadowSize = new GSize(0,0);
    baseIcon.iconAnchor = new GPoint(12, 7);
    baseIcon.infoWindowAnchor = new GPoint(27,6);
    var li = new GIcon(baseIcon);
    li.image = '/static/images/trainsmall.png';
    var mo = { icon: li };

    if (geoFrame[frame]) {
        var s = geoFrame[frame];
        if (train) {
            map.removeOverlay(train);
        }
        train = new GMarker(new GLatLng(s.lat, s.lon), mo);
        map.addOverlay(train);

        $("#meter").text(geoFrame[frame].elevation);
        if ($("#kommune").attr('src') != '/static/images/kommuner/'+geoFrame[frame].kommune+'.png') {
            $("#kommune").attr('src', '/static/images/kommuner/'+geoFrame[frame].kommune+'.png'); 
            $("#kommune").attr('alt', 'Kommuneskjold for ' + geoFrame[frame].kommune); 
            $("#kommune").attr('title', 'Kommune: ' + geoFrame[frame].kommune); 
        }
        if ($("#fylke").attr('src') != '/static/images/fylker/'+geoFrame[frame].fylke+'.png') {
            $("#fylke").attr('src', '/static/images/fylker/'+geoFrame[frame].fylke+'.png'); 
            $("#fylke").attr('alt', 'Fylkeskjold for ' + geoFrame[frame].fylke); 
            $("#fylke").attr('title', 'Fylke: ' + geoFrame[frame].fylke); 
        }

    } else {
       $.getJSON("http://bb.quai.no:8080/geoframe/" + frame + "?_callback=?",
            function(data){
                geoFrame = data;
                if (geoFrame[frame]) {
                    updateTrain();
                }
            }
        );
    }
    isTrainOutsideViewport();
}


// If window.opera is not defined, mock the opera.postError function.
// TODO: Log to a hidden buffer?
if (!window.opera) {
    opera = { postError: function(a) { } };
}

function getTimecode(frame) {

    var sec = (frame-1) / 2;
    var timecode = '';

    var hour = Math.floor(sec / (60*60));
    sec = sec - ( 60*60*hour );
    if (hour < 10) {
        timecode = '0' + hour + ':';
    } else {
        timecode = hour + ':';
    }


    var min = Math.floor(sec / 60);
    sec = sec - ( 60*min );
    if (min < 10) {
        timecode = timecode + '0' + min + ':';
    } else {
        timecode = timecode + min + ':';
    }

    if (sec < 10) {
        timecode = timecode + '0' + Math.floor(sec);
    } else {
        timecode = timecode + Math.floor(sec);
    }

    if (sec == Math.floor(sec)) {
        timecode = timecode + '.0';
    } else {
        timecode = timecode + '.5';
    }
    
    return timecode;
}

function pathClick(a,b) {
    $.getJSON("http://bb.quai.no:8080/reverse/" + a + "/" + b + "?_callback=?",
        function(data){
            frame = data.id;
            updateTrain();
            updateFrame();
            updateSlide();
        }
    );
}

