/*
all AJAX actions
*/
var FotoliaAjaxActions = {
    obj: Object,

    init: function() {
        if (
            !document.getElementById ||
            !document.createElement ||
            !document.getElementsByTagName
        ) {
            return;
        }

        var i,j;

        // add events to Ajax links
        var current = document.getElementsByTagName('a');
        var curLen = current.length;


        for (j = 0; j < curLen; j++) {
            // must redefine regexp because of reuse
            var regexp = new RegExp('^http://[A-Za-z0-9_\\.]+/Ajax/([A-Za-z0-9]+)', 'gi');
            var results = regexp.exec(current[j].href);
            if (!results) {
                continue;
            }

            // replace onclick
            current[j].onclick = FotoliaAjaxActions.basicClick;
        }
    },

    basicClick: function(e) {
        url = this.href;

        var pars = '';
        var myAjax = new Ajax.Request();

        myAjax.setOptions(
            {
                method: 'get',
                parameters: pars,
                onComplete: FotoliaAjaxActions.XHRResponseTextCallback
            }
        );

        // reference to source
        myAjax.sourceObject = this;

        myAjax.request(url);

        return false;
    },

    radioClick: function(e) {

        url = this.href;

        var pars = '';
        var myAjax = new Ajax.Request();
        myAjax.setOptions(
            {
                method: 'get',
                parameters: pars,
                onComplete: FotoliaAjaxActions.XHRResponseTextCallback
            }
        );

        // reference to source
        myAjax.sourceObject = this;

        myAjax.request(url);

        return true;
    },

    XHRResponseTextCallback: function(XHR, eJSON, origin) {
        try {
            eval(XHR.responseText);
        } catch (e) {
            var s = '';
            for (i in e) {
                s += i + ' = ' + e[i] + "\n";
            }
        }
    }
}

var FotoliaTooltip = {
    init: function() {
        if (
            !document.getElementById ||
            !document.createElement ||
            !document.getElementsByTagName
        ) {
            return;
        }

        var current = document.getElementsByTagName('img');
        var curLen = current.length;

        for (var j = 0; j < curLen; j++) {
            // we must redefine regexps (JS bug)
            var regexp_photos_mini = new RegExp('\/[0-9\/-]+\/(30|110)_F_');

            if (!current[j].src.match(regexp_photos_mini)) {
                continue;
            }

            var regexp = new RegExp('/pics.looking-for-singles.com');
            var test = current[j].src
            var largeSrc = current[j].src.replace(regexp, '/rakovina91.com');

            current[j].setAttribute('largeSrc', largeSrc);

            var title = current[j].getAttribute('title');
            // remove title
            current[j].setAttribute('title', '');

            var tip = '<div class="tool"><img src="' + largeSrc + '" /></div>';
            
        if (
            test==largeSrc
        ) {
            new Tooltip(current[j], tip, {'html': true, 'classname': 'FotoliaTooltipObject'});
        }
            
        }
    }
}

/*
Zoom on 400px images

based on http://valid.tjp.hu/zoom2/index_en.html + events and crop on server
create several FotoliaZoom object on onload events
*/
var FotoliaZooms = {
    containers: new Array(),

    init: function() {

        if (
            !document.getElementById ||
            !document.createElement ||
            !document.getElementsByTagName
        ) {
            return;
        }

        var current = document.getElementsByTagName('img');
        var curLen = current.length;

        var zoom_present = false;

        for (var j = 0; j < curLen; j++) {
            var regexp_photos_mini = new RegExp('\/(jpg|photos_mini)\/.*\/(300|400)_F_');

            if (!current[j].src.match(regexp_photos_mini)) {
                continue;
            }

            FotoliaZooms.createNewZoom(current[j]);
            zoom_present = true;
        }

        if (zoom_present) {
            addEvent(window.document, 'click', function() { FotoliaZooms.reinit(); });
        }
    },

    createNewZoom: function(img)
    {
        // zoom ratio for each new click
        if (zoom_ratio = img.getAttribute('zoom_ratio')) {
            zoom_ratio = Number(zoom_ratio);
            if (zoom_ratio == 0) {
                return;
            }
        } else {
            return;
        }

        if (zoom_depth_max = img.getAttribute('zoom_depth_max')) {
            zoom_depth_max = Number(zoom_depth_max);
        } else {
            return;
        }

        var zoom = new FotoliaZoom(img, zoom_ratio, zoom_depth_max);
        zoom.create();
    },

    reinit: function()
    {
        for (var i = 0, l = FotoliaZooms.containers.length; i < l; i++) {
            var container = FotoliaZooms.containers[i];

            // reinit
            if (typeof(container.originalSrc) == 'undefined') {
                continue;
            }

            container.img.src = container.originalSrc;

            container.img.style.left = '0px';
            container.img.style.top = '0px';
            container.img.width = container.w;
            container.img.height = container.h;

            container.FotoliaZoom.zoomDepth = 1;

            container.baseX = 0;
            container.baseY = 0;

            container.baseLeft = 0;
            container.baseTop = 0;

            container.FotoliaZoom.zoomDepth = 1;
        }
    }
}


function FotoliaZoom(img, zoom_ratio, zoom_depth_max)
{
    this.img = img;

    // zoom ratio for each new click
    this.zoomRatio = zoom_ratio
    this.zoomDepthMax = zoom_depth_max + 1;

    // clip border
    this.bordersize = 1;

    // clip border
    this.outbordersize = 1;

    // ratio of the clip box
    this.ratio = 1 / this.zoomRatio;

    this.zoomDepth = 0;

    this.loading = false;
}

FotoliaZoom.prototype.create = function()
{
    // add events, only on jpg
    var regexp_tb = new RegExp('\/(jpg|photos_mini)\/.+\/(300|400)_F_');

    if (!this.img.src.match(regexp_tb)) {
        return;
    }

    // create container
    var div = document.createElement('div');
    div.id = 'FotoliaZoom:' + this.img.src;

    div.style.width = this.img.width + 'px';
    div.style.height = this.img.height + 'px';

    var newImage = this.img.cloneNode(false);
    newImage.originalSrc = newImage.src;
    newImage.id = 'img:' + this.img.src;
    newImage.setAttribute('title', '');

    // create clip
    var divClip = document.createElement('div');

    Element.setStyle(
        div,
        {
            position: 'relative',
            overflow: 'hidden'
        }
    );

    Element.setStyle(
        divClip,
        {
            position: 'absolute',
            width: Math.round(this.img.width * this.ratio) + 'px',
            height: Math.round(this.img.height * this.ratio) + 'px',
            border: '1px solid #777777',
            borderWidth: Math.round(this.bordersize) + 'px'
        }
    );

    Element.hide(divClip);

    // append child
    div.appendChild(newImage);
    div.appendChild(divClip);

    // replace original image
    this.img.parentNode.replaceChild(div, this.img);

    Element.addClassName(div, 'FotoliaZoom');
    Element.addClassName(divClip, 'FotoliaZoomClip');

    // name references
    this.container = div;
    this.container.img = newImage;
    this.container.clip = divClip;

    this.container.FotoliaZoom = this;

    addEvent(this.container, 'mouseover', this.onmouseover);
    addEvent(this.container, 'mousemove', this.onmousemove);
    addEvent(this.container, 'mouseout', this.onmouseout);
    addEvent(this.container, 'click', this.onclick);

    FotoliaZooms.containers.push(this.container);
}

// for these functions, 'this' is the container
FotoliaZoom.prototype.onmousemove = function(e)
{
    var xCord;
    var yCord;

    if (document.captureEvents) {
        xCord = e.pageX;
        yCord = e.pageY;
    } else if (window.event.clientX) {
        xCord = window.event.clientX + document.documentElement.scrollLeft;
        yCord = window.event.clientY + document.documentElement.scrollTop;
    }

    // reference to clip
    var clip = this.clip;

    var left = Math.ceil(xCord - this.xCord - clip.w / 2);
    var top = Math.ceil(yCord - this.yCord - clip.h / 2);

    if (left < 0) {
        left = 0;
    } else if (left >= (this.w - clip.w)) {
        left = this.w - clip.w - this.FotoliaZoom.bordersize * 2;
    }

    if (top < 0) {
        top = 0;
    } else if (top >= (this.h - clip.h)) {
        top = this.h - clip.h - this.FotoliaZoom.bordersize * 2;
    }

    if (isNaN(top)) {
        top = 0;
    }

    if (isNaN(left)) {
        left = 0;
    }

    Element.setStyle(
        clip,
        {
            top: top + 'px',
            left: left + 'px'
        }
    );
}

FotoliaZoom.prototype.onmouseover = function(e)
{
    if (this.FotoliaZoom.hideClipTimeout) {
        window.clearTimeout(this.FotoliaZoom.hideClipTimeout);
    }

    if (this.FotoliaZoom.zoomDepth >= this.FotoliaZoom.zoomDepthMax) {
        return;
    } else if (this.loading) {
        return;
    } else {
        if (Element.visible(this.clip)) {
            return;
        }

        this.FotoliaZoom.showClip(this);
    }
}

FotoliaZoom.prototype.showClip = function(container)
{
    var clip = container.clip;

    if (typeof(container.xCord) == 'undefined' || container.xCord == 0) {
        var pos = Position.cumulativeOffset(container);
        container.xCord = pos[0];
        container.yCord = pos[1];

        var dims = Element.getDimensions(container);
        container.w = dims['width'];
        container.h = dims['height'];

        var clipDims = Element.getDimensions(clip);
        clip.w = clipDims['width'];
        clip.h = clipDims['height'];
    }

    if (container.FotoliaZoom.hideClipTimeout) {
        window.clearTimeout(container.FotoliaZoom.hideClipTimeout);
    }

    Element.show(clip);
}

FotoliaZoom.prototype.onmouseout = function(e)
{
    if (this.FotoliaZoom.hideClipTimeout) {
        window.clearTimeout(this.FotoliaZoom.hideClipTimeout);
    }

    this.FotoliaZoom.hideClipTimeout = window.setTimeout('var container = $("' + this.id + '"); container.FotoliaZoom.hideClip(container);', 100);
}

FotoliaZoom.prototype.hideClip = function(container)
{
    Element.hide(container.clip);
}

FotoliaZoom.prototype.reinit = function(container)
{
    // reinit
    if (typeof(container.originalSrc) == 'undefined') {
        return;
    }

    container.img.src = container.originalSrc;

    container.img.style.left = '0px';
    container.img.style.top = '0px';
    container.img.width = container.w;
    container.img.height = container.h;

    container.baseX = 0;
    container.baseY = 0;

    container.baseLeft = 0;
    container.baseTop = 0;

    container.FotoliaZoom.zoomDepth = 1;
    container.loading = false;
}

FotoliaZoom.prototype.onloadZoomImage = function(container)
{
    var img = container.img;

    Element.hideExt(img);

    window.setTimeout(
        function() {
            img.width = img.zoomImage.width;
            img.height = img.zoomImage.height;
            img.style.left = 0;
            img.style.top = 0;

            img.src = img.zoomImage.src;
            Element.showExt(img);
        },
        0);

    container.loading = false;

    if (container.FotoliaZoom.zoomDepth < container.FotoliaZoom.zoomDepthMax) {
        container.FotoliaZoom.showClip(container);
    }
}

FotoliaZoom.prototype.onclick = function(e)
{
    if (this.loading) {
        Event.stop(e);
        return;
    }

    if (this.FotoliaZoom.zoomDepth == 0) {
        // first call

        this.originalSrc = this.img.src;

        this.baseX = 0;
        this.baseY = 0;

        this.baseLeft = 0;
        this.baseTop = 0;

        this.FotoliaZoom.zoomDepth = 1;
    }

    if (this.onloadZoomImageTimeout) {
        window.clearTimeout(this.onloadZoomImageTimeout);
    }

    if (this.FotoliaZoom.zoomDepth >= this.FotoliaZoom.zoomDepthMax) {
        this.FotoliaZoom.reinit(this);
        this.FotoliaZoom.showClip(this);
    } else {
        var pos = Position.positionedOffset(this.img);
        var clipPos = Position.positionedOffset(this.clip);
        var dims = Element.getDimensions(this.img);

        var currentZoomRatio = Math.pow(this.FotoliaZoom.zoomRatio, this.FotoliaZoom.zoomDepth - 1);

        // we must remember where we are this.based on orginal image

        this.baseX = ((0 - this.baseLeft) + clipPos[0] + this.FotoliaZoom.bordersize + this.clip.w / 2) / currentZoomRatio;
        this.baseY = ((0 - this.baseTop) + clipPos[1] + this.FotoliaZoom.bordersize + this.clip.h / 2) / currentZoomRatio;

        this.baseLeft = (this.w / 2) - (this.baseX * this.FotoliaZoom.zoomRatio * currentZoomRatio);
        this.baseTop = (this.h / 2) - (this.baseY * this.FotoliaZoom.zoomRatio * currentZoomRatio);


        // request new image (zoom + crop) and display it
        var zoomImage = new Image();

        zoomImage.src = 'http://download.fotolia111.com/Content/Zoom/' + (this.FotoliaZoom.zoomRatio * currentZoomRatio) + '/' + this.baseX + '/' + this.baseY + '/?path=' + escape(this.originalSrc);
        zoomImage.targetImg = this.img;
        zoomImage.container = this;

        this.img.zoomImage = zoomImage;

        this.loading = true;

        zoomImage.onload = function (e) {
            this.container.onloadZoomImageTimeout = window.setTimeout('var container = $("' + this.container.id + '"); container.FotoliaZoom.onloadZoomImage(container);', 10);
        };

        // and now we compute for browser zoom

        if (pos[0] == 0 && dims['width'] == this.w) {
            // no zoom for the current image
            currentZoomRatio = 1;
        }

        // dot position which will be the new center (based on current image displayed, dynamix src)
        var x = ((0 - pos[0]) + clipPos[0] + this.FotoliaZoom.bordersize + this.clip.w / 2) / currentZoomRatio;
        var y = ((0 - pos[1]) + clipPos[1] + this.FotoliaZoom.bordersize + this.clip.h / 2) / currentZoomRatio;

        // multiply dims
        var w = this.w * this.FotoliaZoom.zoomRatio * currentZoomRatio;
        var h = this.h * this.FotoliaZoom.zoomRatio * currentZoomRatio;

        // translate origin: x,y must be on the center
        var l = (this.w / 2) - (x * this.FotoliaZoom.zoomRatio * currentZoomRatio);
        var t = (this.h / 2) - (y * this.FotoliaZoom.zoomRatio * currentZoomRatio);

        // bound origins
        if (l > 0) {
            l = 0;
        } else if (l + w < this.w) {
            l = this.w - w;
        }

        if (t > 0) {
            t = 0;
        } else if (t + h < this.h) {
            t = this.h - h;
        }

        // browser zoom
        Element.setStyle(
            this.img,
            {
                position: 'absolute',
                left: Math.round(l) + 'px',
                top: Math.round(t) + 'px'
            }
        );

        // zoom with current image (browser zoom)
        this.img.width = w;
        this.img.height = h;

        this.FotoliaZoom.zoomDepth++;

        this.FotoliaZoom.hideClip(this);
    }

    Event.stop(e);
}


/*
Main object
*/
var Fotolia = {

    init: function()
    {
        // init all objects
        FotoliaZooms.init();
        FotoliaTooltip.init();
        FotoliaAjaxActions.init();
        Fotolia.initHiddenForJs();
        Fotolia.testFiltersCheckBox();
    },

    testFiltersCheckBox: function() {
        var c = document.getElementsByTagName('input');
        for (var i = 0, l = c.length; i < l; i++) {
            switch (c[i].id) {
                case 'filter:content_type:photo':
                    Event.observe(c[i], 'click', Fotolia.filtersEventBox);
                    break;
                case 'filter:content_type:illustration':
                    Event.observe(c[i], 'click', Fotolia.filtersEventBox);
                    break;
                case 'filter:content_type:vector':
                    Event.observe(c[i], 'click', Fotolia.filtersEventBox);
                    break;
                case 'filter:content_type:all':
                    Event.observe(c[i], 'click', Fotolia.filtersEventBox);
                    break;
            }
        }
    },

    filtersEventBox: function(e) {
        var elm = Event.element(e);
        switch (elm.id) {
            case 'filter:content_type:photo':
                if (elm.checked) {
                    $('filter:content_type:all').checked = false;
                }
                break;
            case 'filter:content_type:illustration':
                if (elm.checked) {
                    $('filter:content_type:all').checked = false;
                }
                break;
            case 'filter:content_type:vector':
                if (elm.checked) {
                    $('filter:content_type:all').checked = false;
                }
                break;
            case 'filter:content_type:all':
                if (elm.checked) {
                    $('filter:content_type:photo').checked = false;
                    $('filter:content_type:illustration').checked = false;
                    $('filter:content_type:vector').checked = false;
                }
                break;
        }
    },

    initHiddenForJs: function() {
        var c = document.getElementsByTagName('input');
        for (var i = 0, l = c.length; i < l; i++) {
            if (c[i].className == 'js_hidden') {
                Element.hideExt(c[i]);
            }
        }
    },

    debug: function(e)
    {
        alert('Error:');
        alert(e);
    },

    changeMainClassName: function (className)
    {
        var b = document.getElementsByTagName('body')[0];
        b.className = className;
    }
}

Event.onDOMReady(function() {
    Fotolia.init();
});

FotoliaDownloadContent = {

    init: function()
    {
        if(!$('shoppingcart_download')) {
            return false;
        }

        // set licenses_possibilities
        FotoliaShoppingCart.setLicensePossibilities();

        // set event on can and cannot link
        FotoliaShoppingCart.initLicensePossibilitiesBox();

        // retrieve contents selected licenses
        FotoliaDownloadContent.retrieveContentsSelectedLicenses();
    },

    retrieveContentsSelectedLicenses: function() {
        var contents_licenses = new Array();

        var c = document.getElementsByTagName('input');
        for (var i = 0, l =  c.length; i < l; i++) {
            if (c[i].id && c[i].id.match(/^contents_licenses:[0-9]+$/) && c[i].type == 'hidden') {

                var matches = /^contents_licenses:([0-9]+)$/.exec(c[i].id);
                content_id = matches[1];
                license_abbr = c[i].value;

                contents_licenses[content_id] = license_abbr;
            }
        }

        FotoliaShoppingCart.contents_licenses = contents_licenses;
    },

    setDownloadButtonsActions: function()
    {
        switch(this.id) {
            case '':
                alert(_('Error'));
                break;

            default:
                alert('No action for : '  + this.id);
        }

        return false;
    }
}

Event.onDOMReady(function() {
    FotoliaDownloadContent.init();
});

FotoliaShoppingCart = {

    xCord: 0,                // @Number: x pixel value of current cursor position
    yCord: 0,                // @Number: y pixel value of current cursor position
    currentTooltip: Object,

    licenses_possibilities: new Array(),
    contents_licenses: new Array(),

    init: function()
    {
        if(!$('totalPrice')) {
            return false;
        }

        // set licenses_possibilities
        FotoliaShoppingCart.setLicensePossibilities();

        // retrieve contents selected licenses
        FotoliaShoppingCart.retrieveContentsSelectedLicenses();

        // set event on can and cannot link
        FotoliaShoppingCart.initLicensePossibilitiesBox();

        FotoliaShoppingCart.initCheckoutButtons();
    },

    retrieveContentsSelectedLicenses: function() {
        var contents_licenses = new Array();

        var c = document.getElementsByTagName('input');
        for (var i = 0, l =  c.length; i < l; i++) {
            if(c[i].id && c[i].id.match(/^(.+):[0-9]+:(.+)$/) && c[i].type == 'radio' && c[i].checked == true) {
                var matches = /:([0-9]+):(.+)$/.exec(c[i].id);
                content_id = matches[1];
                license_abbr = matches[2];

                contents_licenses[content_id] = license_abbr;
            }
        }

        FotoliaShoppingCart.contents_licenses = contents_licenses;
    },

    setLicensePossibilities: function()
    {
        var licenses_possibilities = new Array();
        licenses_possibilities["S"] = new Array();
        licenses_possibilities["S"]["CAN"] = new Array();
        licenses_possibilities["S"]["CAN"][0] = _("Use the Work in all print  and electronic or online  materials, and web pages");
        licenses_possibilities["S"]["CAN"][1] = _("Modify image or use in derivative works");
        licenses_possibilities["S"]["CAN"][2] = _("Make back up copies");
        licenses_possibilities["S"]["CANNOT"] = new Array();
        licenses_possibilities["S"]["CANNOT"][0] = _("Resell original image");
        licenses_possibilities["S"]["CANNOT"][1] = _("Use image in any way that violates local or federal laws");
        licenses_possibilities["S"]["CANNOT"][2] = _("Distribute copies of image to friends, family, or other organizations");

        licenses_possibilities["X"] = new Array();
        licenses_possibilities["X"]["CAN"] = new Array();
        licenses_possibilities["X"]["CAN"][0] = _("Modify the image or integrate it into derived works");
        licenses_possibilities["X"]["CAN"][1] = _("Use, reproduce, or display images in goods and services intended for resale and distribution");
        licenses_possibilities["X"]["CAN"][2] = _("Make a backup copy of the image");
        licenses_possibilities["X"]["CANNOT"] = new Array();
        licenses_possibilities["X"]["CANNOT"][0] = _("Resell original image");
        licenses_possibilities["X"]["CANNOT"][1] = _("Use image in any way that violates local or federal laws");

        licenses_possibilities["E"] = new Array();
        licenses_possibilities["E"]["CAN"] = new Array();
        licenses_possibilities["E"]["CAN"][0] = _("Exclusively lock out all future sales of this image");
        licenses_possibilities["E"]["CAN"][1] = _("Use the Work in all print  and electronic or online  materials, and web pages");
        licenses_possibilities["E"]["CAN"][2] = _("Use, reproduce, or display images in goods and services intended for resale and distribution");
        licenses_possibilities["E"]["CAN"][3] = _("Make back up copies");
        licenses_possibilities["E"]["CAN"][4] = _("Modify image or use in derivative works");
        licenses_possibilities["E"]["CANNOT"] = new Array();
        licenses_possibilities["E"]["CANNOT"][0] = _("Resell original image");
        licenses_possibilities["E"]["CANNOT"][1] = _("Use image in any way that violates local or federal laws");

        licenses_possibilities["V"] = new Array();
        licenses_possibilities["V"]["CAN"] = new Array();
        licenses_possibilities["V"]["CAN"][0] = _("@todo");
        licenses_possibilities["V"]["CAN"][1] = _("@todo");
        licenses_possibilities["V"]["CAN"][2] = _("@todo");
        licenses_possibilities["V"]["CANNOT"] = new Array();
        licenses_possibilities["V"]["CANNOT"][0] = _("Resell original image");
        licenses_possibilities["V"]["CANNOT"][1] = _("Use image in any way that violates local or federal laws");

        FotoliaShoppingCart.licenses_possibilities = licenses_possibilities;
    },

    initLicensePossibilitiesBox: function() {

        // assign cannot action
        var c = document.getElementsByTagName('a');
        for (var i = 0, l = c.length; i < l; i++) {
            if (c[i].id && (c[i].id.match(/^can_do:[0-9]+$/) || c[i].id.match(/^cannot_do:[0-9]+$/))) {
                addEvent(c[i], 'mouseover', this.setLicensePossibilitiesBoxAction);
                addEvent(c[i], 'mousemove', this.tipMove);
                addEvent(c[i], 'mouseout', this.tipOut);
            }
        }
    },

    // retrieve index of ID
    retrieveIndex: function(e) {
        var matches = /:([0-9]+)$/.exec(e.id);
        return matches[1];
    },

    setLicensePossibilitiesBoxAction: function() {
        // get current license
        var standard_license_abbr = new Array('S', 'M', 'L', 'XL', 'XXL');
        var content_id = FotoliaShoppingCart.retrieveIndex(this);
        var selected_license_abbr = FotoliaShoppingCart.contents_licenses[content_id];

        // detect action
        var type;
        if (this.id.match(/^can_do:[0-9]+$/)) {
            type = 'can';
        } else if (this.id.match(/^cannot_do:[0-9]+$/)) {
            type = 'cannot';
        } else if (this.id.match(/^see_contract:[0-9]+$/)) {
            type = 'contract';
        }

        var test = standard_license_abbr.indexOf(selected_license_abbr);
        if (test != -1) {
            selected_license_abbr = 'S';
        }

        switch (type) {
            case 'can':
                FotoliaShoppingCart.displaylicensetooltip('CAN', selected_license_abbr);
                break;

            case 'cannot':
                FotoliaShoppingCart.displaylicensetooltip('CANNOT', selected_license_abbr);
                break;

            case 'contract':
                break;

            default:

        }
    },

    // display a tooltip and create div if not exist
    displaylicensetooltip: function(type, license_abbr) {

        // detect standart license
        var tooltip_id = 'FotoliaLicenseTooltip_' + license_abbr + '_' +  type;
        var licenses_possibilities= FotoliaShoppingCart.licenses_possibilities[license_abbr][type];

        // test if tooltip exist
        if (!$(tooltip_id)) {

            // extract value
            var tooltip_value = '<ul>';
            for (var i = 0, l = licenses_possibilities.length; i < l; i++) {
                tooltip_value += '<li>' + licenses_possibilities[i] + '</li>';
            }
            tooltip_value += '</ul>';

            // create object
            var new_tooltip = new Object;
            new_tooltip = document.createElement('div');
            Element.update( new_tooltip, tooltip_value);
            new_tooltip.id = tooltip_id;

            document.getElementsByTagName('body')[0].appendChild(new_tooltip);
            new_tooltip.style.top = '0';
            new_tooltip.className = 'license-tooltip static-model';
            new_tooltip.style.position = 'absolute';
            FotoliaShoppingCart.currentTooltip = new_tooltip;
        } else {
            var c = document.getElementById(tooltip_id);
            FotoliaShoppingCart.currentTooltip = c;
        }

        FotoliaShoppingCart.currentTooltip.style.visibility = 'visible';

    },

    // update x and y coord from event position
    updateXY: function(e) {
        if (document.captureEvents) {
            FotoliaShoppingCart.xCord = e.pageX;
            FotoliaShoppingCart.yCord = e.pageY;
        } else if ( window.event.clientX ) {
            FotoliaShoppingCart.xCord = window.event.clientX + document.documentElement.scrollLeft;
            FotoliaShoppingCart.yCord = window.event.clientY + document.documentElement.scrollTop;
        }
    },

    // move the tooltip
    tipMove: function(e) {
        if (e) {
            FotoliaShoppingCart.updateXY(e);
        }

        var scrX = Number(FotoliaShoppingCart.xCord);
        var scrY = Number(FotoliaShoppingCart.yCord);
        var tp = parseInt(scrY + 15);
        var lt = parseInt(scrX);

        var oWidth = 200;

        minlt = oWidth / 2;
        maxlt = parseInt(document.documentElement.clientWidth) + parseInt(document.documentElement.scrollLeft) - oWidth / 2;

        lt = Math.min(maxlt, lt);
        lt = Math.max(minlt, lt);

        tlt = lt - oWidth / 2;

        if (parseInt(document.documentElement.clientHeight + document.documentElement.scrollTop) < parseInt(FotoliaShoppingCart.currentTooltip.offsetHeight + tp)) {
            ttp = tp - parseInt(FotoliaShoppingCart.currentTooltip.offsetHeight) - 25;
        } else {
            ttp = tp;
        }

        if (FotoliaShoppingCart.currentTooltip.style) {
            FotoliaShoppingCart.currentTooltip.style.left = tlt + 'px';
            FotoliaShoppingCart.currentTooltip.style.top = ttp + 'px';
        }
    },

    // hide the tooltip when the mouse cursor is going out of the link area
    tipOut: function(e) {
        FotoliaShoppingCart.currentTooltip.style.visibility = 'hidden';
    },

    // update shopping cart total price
    updateTotalPrice: function(total_price_string)
    {
        $('totalPrice').firstChild.nodeValue = total_price_string;
    },

    // update shopping cart total price
    updateNbItem: function(nbItem)
    {
        $('nbItem').firstChild.nodeValue = nbItem;
    },

    // update shopping cart remaining credits
    updateRemainingCredits: function(remaining_credits, remaining_credits_string)
    {
        $('remaining_credits').update(remaining_credits_string);

        if (remaining_credits < 0) {
            Element.showInline($('buy_credit_link'));
        } else {
            Element.hideInline($('buy_credit_link'));
        }
    },

    updateShoppingcartNbContents: function(nbContents)
    {
        $('shoppingcart:nbcontents').update(nbContents);
    },

    // remove content in shopping cart and make somes actions in the action from Shopping cart
    removeShoppingcartContents: function(content_id)
    {
        if($('totalPrice')) {

            Element.hideExt($('selected_license_infos:' + content_id));
            Element.hideExt($('no_selected_license_infos:' + content_id));
            Element.showExt($('remove_from_shopping_cart:' + content_id));

            // update content license choise
            var c = $('licenses_choice:'+ content_id).getElementsByTagName('tr');
            for (var i = 0, l = c.length; i < l; i++) {
                var license_line = c[i];
                license_line.className = null;
            }

            var c = $('licenses_choice:'+ content_id).getElementsByTagName('input');
            for (var i = 0, l = c.length; i < l; i++) {
                var license_line = c[i];
                license_line.checked = false;
            }

            Element.hideExt($('remove_content_link:'+ content_id));

            if ($('no_license:'+ content_id)) {
                $('no_license:'+ content_id).style.visibility = 'hidden';
                //Element.hideExt($('no_license:'+ content_id));
            }
        }
    },

    updateShoppingcartData: function(nb_contents, total_price_string, remaining_credits, remaining_credits_string, nb_valid_contents)
    {
        if (typeof(nb_valid_contents) == 'undefined') {
            nb_valid_contents = nb_contents;
        }

        // update shopping cart prices
        if($('totalPrice')) {
            FotoliaShoppingCart.updateNbItem(nb_valid_contents);
            FotoliaShoppingCart.updateTotalPrice(total_price_string);
            FotoliaShoppingCart.updateRemainingCredits(remaining_credits, remaining_credits_string);
        }

        if($('shoppingcart:nbcontents')) {
            FotoliaShoppingCart.updateShoppingcartNbContents(nb_contents);
        }
    },

    // update content in shopping cart and make somes actions in the action from Shopping cart
    chooseLicenseInShoppingcart: function(content_id, license_abbr, license_name, license_new_real_price)
    {
        if ($('totalPrice')) {
            // update contents licenses array
            FotoliaShoppingCart.contents_licenses[content_id] = license_abbr;

            // update content licence infos
            Element.showExt($('selected_license_infos:' + content_id));
            Element.hideExt($('no_selected_license_infos:' + content_id));
            Element.hideExt($('remove_from_shopping_cart:' + content_id));

            // update no content license
            if ($('no_license:'+ content_id)) {
                //Element.hideExt($('no_license:'+ content_id));
                $('no_license:'+ content_id).style.visibility = 'hidden';
            }

            // reset remove shoppingcart button
            var remove_content_link = $('remove_content_link:'+ content_id);
            Element.showExt(remove_content_link);
            if (remove_content_link.old_onclick) {
                remove_content_link.reEnabledOnclickTimeout();
                remove_content_link.className = null;
            }

            // update content license choise
            var c = $('licenses_choice:'+ content_id).getElementsByTagName('tr');
            for (var i = 0, l = c.length; i < l; i++) {
                var license_line = c[i];
                if (license_line.id == 'licence_line:' + content_id + ':' + license_abbr) {
                    license_line.className = 'selected_license';
                } else {
                    license_line.className = null;
                }
            }

            // update license properies
            selected_license_price = $('selected_license_price:' + content_id);
            selected_license_name = $('selected_license_name:' + content_id);

            Element.update(selected_license_price, license_new_real_price);
            Element.update(selected_license_name, translateLicense(license_name));
        }
    },

    initCheckoutButtons: function()
    {
        // set simple action
        var action_buttons = new Array(
            'checkout_button'
        );

        for (var i = 0, l =  action_buttons.length; i < l; i++) {
            if ($(action_buttons[i])) {
                button = $(action_buttons[i]);
                button.onclick = FotoliaShoppingCart.setButtonsActions;
            }
        }

        // set ajax action on tr and radio
        FotoliaShoppingCart.initTrLicense();
        FotoliaShoppingCart.initRadioLicense();
    },

    initTrLicense: function()
    {
        // set color change on tr
        var tr_licenses = document.getElementsByTagName('tr');
        var license_abbr = new Array();
        var content_id = new Array();

        for (var i = 0, l = tr_licenses.length; i < l; i++) {
            if (tr_licenses[i].id && tr_licenses[i].id.match(/^licence_line:[0-9]+:(.*)+$/)) {
                tr_licenses[i].type = 'tr';
                tr_licenses[i].onclick = FotoliaShoppingCart.setLicenseToContent;
            }
        }
    },

    initRadioLicense: function()
    {
        // add events to Radio input
        var radio_licenses = document.getElementsByTagName('input');

        for (var i = 0, l = radio_licenses.length; i < l; i++) {
            if (radio_licenses[i].type != 'radio') {
                continue;
            }

            if (radio_licenses[i].id && radio_licenses[i].id.match(/^license:[0-9]+$/)) {
                radio_licenses[i].onclick = FotoliaShoppingCart.setLicenseToContent;
            }
        }
    },

    setLicenseToContent: function(e)
    {
        // @todo tagname...
        switch (this.type) {
            case 'tr':
                var matches = /^licence_line:([0-9]+):(.+)$/.exec(this.id);
                var content_id = matches[1];
                var license_abbr = matches[2];

                //check radio
                radio_license = $('license:' +  content_id + ':' + license_abbr);
                $('license:' +  content_id + ':' + license_abbr).checked = true;

                break;

            case 'radio':
                var regexp = new RegExp('^license:([0-9]+)', 'g');
                var results = regexp.exec(current[j].name);
                var content_id = results[1];
                var license_abbr = this.value;
                break;

            default:
                break;

        }

        url = '/Ajax/ChooseLicenseInShoppingCart/?content_id=' + content_id + '&license=' + license_abbr;

        var pars = '';
        var myAjax = new Ajax.Request();
        myAjax.setOptions(
            {
                method: 'get',
                parameters: pars,
                onComplete: FotoliaAjaxActions.XHRResponseTextCallback
            }
        );

        // reference to source
        myAjax.sourceObject = this;

        myAjax.request(url);

        return true;
    },

    setButtonsActions: function()
    {
        var checkbox_contract_accept = $('contract_accept');

        switch (this.id) {
            case 'checkout_button':
                if (checkbox_contract_accept.checked) {
                    return true;
                }

                alert(_('Please check the box to accept contract and continue'));
                break;

            default:
                //alert('No action for : '  + this.id);
        }

        return false;
    }
}

Event.onDOMReady(function() {
    FotoliaShoppingCart.init();
});

