//  Copyright (c) 2009 Michalis Adamidis
//
//  Permission is hereby granted, free of charge, to any person obtaining a copy
//  of this script file (the "Software"), to deal in the Software without restriction,
//  including without limitation the rights to use, copy, modify, merge, publish,
//  distribute, sublicense, and/or sell copies of the Software, and to permit
//  persons to whom the Software is furnished to do so,
//  subject to the following conditions:
//
//  The above copyright notice and this permission notice shall be included in
//  all copies or substantial portions of the Software.
//
//  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//  THE SOFTWARE.

function createColoredBackgroundSimple(element, pad, round, color)
{
    // get the offsets needed for position calcing
    var backOffset  = $jQ(container).offset();
    var originals   = $jQ(element);

    // the selector may result in multiple elements beeing found
    for (var i = 0; i < originals.length; i++)
    {
        var origOffset  = $jQ(originals[i]).offset();

        // first calc the positions of the corners
        var left        = origOffset.left - backOffset.left - pad;
        var top         = origOffset.top - backOffset.top - pad;
        var width       = $jQ(element).width() + 2 * pad;
        var height      = $jQ(element).height() + 2 * pad;

        // now draw the rect
        var r = paper.rect(left, top, width, height, round).attr({fill: color, opacity: 0.9, "stroke-width": 0});
    }
}

function createBackgroundSimple(element, pad, round)
{
    createColoredBackgroundSimple(element, pad, round, "#FFF")
}

function createColoredBackground(element, pad, ul, ur, bl, br, color)
{
    // get the offsets needed for position calcing
    var backOffset  = $jQ(container).offset();
    var originals   = $jQ(element);

    // the selector may result in multiple elements beeing found
    for (var i = 0; i < originals.length; i++)
    {
        var origOffset  = $jQ(originals[i]).offset();

        // first calc the positions of the corners
        var left        = origOffset.left - backOffset.left - pad;
        var top         = origOffset.top - backOffset.top - pad;
        var right       = left + $jQ(element).width() + 2 * pad;
        var bottom      = top + $jQ(element).height() + 2 * pad;

        // now calc additional positions for the round corners
        var leftTop     = left + ul;
        var rightTop    = right - ur;
        var bottomRight = bottom - br;
        var leftBottom  = left + bl;
        var topLeft     = top + ul;
        var bottomLeft  = bottom - bl;

        // create svg path string
        var pathstring = "M" + ((ul == 0) ? left : leftTop) + " " + top;
        pathstring += "L" + ((ur == 0) ? right : rightTop) + " " + top;
        if (ur != 0)
        {
            pathstring += "Q" + right + " " + top + " " + right + " " + (top + ur);
        }
        
        pathstring += "L" + right + " " + ((br == 0) ? bottom : bottomRight);
        if (br != 0)
        {
            pathstring += "Q" + right + " " + bottom + " " + (right - br) + " " + bottom;
        }
        pathstring += "L" + ((bl == 0) ? left : leftBottom) + " " + bottom;
        if (bl != 0)
        {
            pathstring += "Q" + left + " " + bottom + " " + left + " " + (bottom - bl);
        }
        pathstring += "L" + left + " " + ((ul == 0) ? top : topLeft);
        if (ul != 0)
        {
            pathstring += "Q" + left + " " + top + " " + leftTop + " " + top;
        }
        pathstring += "Z";

        // now start the path and depending on corners draw the path
        var p = paper.path(pathstring).attr({fill: color, opacity: 0.9, "stroke-width": 0});
    }
}

function createBackground(element, pad, ul, ur, bl, br)
{
    createColoredBackground(element, pad, ul, ur, bl, br, "#FFF");
}

function createBackLinkBackground(element)
{
    createColoredBackground(element, 0, 16, 16, 0, 0, "#002454");
}


function createColoredMenuBackground(element, pad, bl, br, color)
{
    // create local raphael environment
    var localContainer      = $jQ("#" + element.id + " > .menuSubBack");
    var contentContainer    = $jQ("#" + element.id + " > .menuSub");
    if (localContainer.length > 0 && contentContainer.length > 0)
    {
        var contentHeight = contentContainer.outerHeight();
        localContainer.height(contentHeight + pad);
        var localPaper      = Raphael(localContainer[0], localContainer.width(), localContainer.height());

        // first calc the positions of the corners
        var left        = 0;
        var top         = 0;
        var right       = localContainer.width();
        var bottom      = contentHeight + pad;

        // now calc additional positions for the round corners
        var bottomRight = bottom - br;
        var bottomLeft  = bottom - bl;
        var leftBottom  = left + bl;
        var rightBottom = right - br;

        // create svg path string
        var pathstring = "M" + left + " " + top;
        pathstring += "L" + right + " " + top;
        pathstring += "L" + right + " " + ((br == 0) ? bottom : bottomRight);
        if (br != 0)
        {
            pathstring += "Q" + right + " " + bottom + " " + rightBottom + " " + bottom;
        }
        pathstring += "L" + ((bl == 0) ? left : leftBottom) + " " + bottom;
        if (bl != 0)
        {
            pathstring += "Q" + left + " " + bottom + " " + left+ " " + bottomLeft;
        }
        pathstring += "L" + left + " " + top;
        pathstring += "Z";

        // now start the path and depending on corners draw the path
        var p = localPaper.path(pathstring).attr({fill: color, "stroke-width": 0});

        $jQ(element).fadeOut("fast");
    }
}

function createOfferBackground(element)
{
    createColoredOfferBackground(element, 0, 16, 16, "#002454");
}


function createColoredOfferBackground(element, pad, ul, ur, color)
{
    // create local raphael environment
    var localContainer      = $jQ("#" + element.id + " > .Back");
    var contentContainer    = $jQ("#" + element.id + " > .Content");
    if (localContainer.length > 0 && contentContainer.length > 0)
    {
        var contentHeight   = contentContainer.outerHeight();
        localContainer.height(contentHeight + pad);
        var localPaper      = Raphael(localContainer[0], localContainer.width(), localContainer.height());

        // first calc the positions of the corners
        var left        = 0;
        var top         = 0;
        var right       = localContainer.width();
        var bottom      = contentHeight + pad;

        // now calc additional positions for the round corners
        var topLeft     = top + ul;
        var topRight    = top + ur;
        var leftTop     = left + ul;
        var rightTop    = right - ur;

        // create svg path string
        var pathstring  = "M" + right + " " + bottom;
        pathstring      += "L" + left + " " + bottom;
        pathstring      += "L" + left + " " + ((ul == 0) ? top : topLeft);
        if (ul != 0)
        {
            pathstring  += "Q" + left + " " + top + " " + leftTop + " " + top;
        }
        pathstring      += "L" + ((ur == 0) ? right : rightTop) + " " + top;
        if (ur != 0)
        {
            pathstring  += "Q" + right + " " + top + " " + right + " " + topRight;
        }
        pathstring      += "L" + right + " " + bottom;
        pathstring      += "Z";

        // now start the path and depending on corners draw the path
        var p = localPaper.path(pathstring).attr({fill: color, "stroke-width": 0});
    }
}
