var cms = {
};
cms.config = {
    'debug': true                      // EDIT turn log on / off
};
cms.init = function () {
    $(function () {
        cms.template.init();
        if (cms.template.config.collection) {
            cms.collection.init();
        }
        log(cms);
    });
};
cms.template = {
    'config': {
        'home': false,
        'collection': false,
        'bgImagewidth': 2395,
        'bgImageOffsetStart': 560,
        'bgImageOffset': 560 + 400
    }
};
cms.template.init = function () {
    var websiteOffset = $('#website').offset(),
        imageOffset
        els2Fade = [];
    els2Fade.push($('#teaser .teaser'));
    els2Fade.push($('#teaser .teaserContent'));
    els2Fade.push($('#teaser h3'));
    els2Fade.push($('#teaser img'));
    els2Fade.push($('#navLeft li'));
    els2Fade.push($('#navTopLeft ul'));
    els2Fade.push($('#navTopRight ul'));
    els2Fade.push($('#header'));
    els2Fade.push($('#logo'));
    if ($.browser.msie && this.config.home) {
        $('#header').css({'background' :'none #750c2a'});
        $('#teaser .teaser').css({'background': 'none #000'});
        $('#navLeft .active a').css({'background': 'none #000'});
        $('#navLeft a').each(function () {
            var w = $(this).width() + parseInt($(this).css('paddingLeft')) + parseInt($(this).css('paddingRight')),
                h = $(this).height() + parseInt($(this).css('paddingTop')) + parseInt($(this).css('paddingBottom')),
                t = $(this).parent().parent().css('paddingTop'),
                c = ($(this).parent().parent().hasClass('active')) ? '#000' : 'none transparent';
                el = document.createElement('span');
            $(el).css({
                'display': 'block',
                'width': w,
                'height': h,
                'background': c,
                'position': 'absolute',
                'top': t,
                'right': 0,
                'z-index': 0
            }).
            addClass('ie');
            $(this).
                css('background', 'none transparent').
                parent().
                parent().
                prepend(el);
            els2Fade.push($(el));
        });
        $('#teaser .teaser').each(function () {
            var w = $(this).width() + parseInt($(this).css('paddingLeft')) + parseInt($(this).css('paddingRight')),
                h = $(this).height() + parseInt($(this).css('paddingTop')) + parseInt($(this).css('paddingBottom')),
                el = document.createElement('span');
            $(el).css({
                'display': 'block',
                'width': w,
                'height': h,
                'background': '#000',
                'position': 'absolute',
                'top': 0,
                'left': 0,
                'z-index': 0
            }).
            addClass('ie');
            $(this).
                css('background', 'none transparent').
                prepend(el);
            els2Fade.push($(el));
        });
        $('#header').each(function () {
            var w = $(this).width() + parseInt($(this).css('paddingLeft')) + parseInt($(this).css('paddingRight')),
                h = $(this).height() + parseInt($(this).css('paddingTop')) + parseInt($(this).css('paddingBottom')),
                el = document.createElement('span');
            $(el).css({
                'display': 'block',
                'width': w,
                'height': h,
                'background': '#750c2a',
                'position': 'absolute',
                'top': 0,
                'left': 0,
                'z-index': 0
            }).
            addClass('ie').
            addClass('header');
            $(this).
                css('background', 'none transparent').
                prepend(el);
            els2Fade.push($(el));
        });
    }
    if (this.config.home) {
        imageOffset = -Math.ceil(this.config.bgImageOffsetStart - websiteOffset.left) + 'px';
        $(els2Fade).each(function () {
            $(this).css('opacity', 0);
        });
        window.setTimeout('cms.template.fadeInHome(els2Fade)', 1000);
    } else {
        imageOffset = -Math.ceil(this.config.bgImageOffset - websiteOffset.left) + 'px';
    }
    $('body').css('backgroundPosition', imageOffset + ' top');
    this.bindEvents();
};
cms.template.fadeInHome = function (els2Fade) {
    $(els2Fade).each(function () {

        if ($(this).hasClass('ie') && $(this).hasClass('header')) {
                $(this).animate({'opacity': 1}, 3000);
            $(this).animate(
            {
                'opacity': .8
            },{
                'duration': 3000,
                'complete': function () {

                }
            });
        }

        else if ($(this).hasClass('ie')) {
            $('#navLeft a').
                css({'position': 'relative', 'opacity': 0, 'z-index': 1}).
                animate({'opacity': 1}, 3000).
                hover(function (){
                    ieHelper = $(this).parent().parent().find('.ie');
                    $(this).parent().parent().find('.ie').css('background', '#000');
                }, function (){
                    $(this).parent().parent().find('.ie').css('background', 'none transparent');
                });
            $(this).animate(
            {
                'opacity': .7
            },{
                'duration': 3000,
                'complete': function () {

                }
            });
        }
        else {
            $(this).animate(
            {
                'opacity': 1
            },{
                'duration': 3000,
                'complete': function () {
                   if ($.browser.msie) {

                   }
                }
            });
        }
    });
};
cms.template.bindEvents = function () {
    // collection startpage and first collection
    $('#moduleWrapper352, #moduleWrapper106').each(function () {
        $('body').css({
            'background': 'url(/_media/themes/1/layout/hg_hw11und12_2395x1200px_1.jpg) #814c60'
        });
    });
    $('#moduleWrapper263').each(function () {
        $('body').css({
            'background': 'url(/_media/themes/1/layout/lanius_hg_fs2011.jpg) #f0e2d1'
        });
    });
    $('#moduleWrapper108').each(function () {
        $('body').css({
            'background': 'url(/_media/themes/1/layout/hgstart_HW09-10_1.jpg) #562126'
        });
    });
    //$('#moduleWrapper110, #moduleWrapper106').each(function () {
    $('#moduleWrapper110').each(function () {
        $('body').css({
            'background': 'url(/_media/themes/1/layout/hggalerieHW10-11_1.jpg) #9f988f'
        });
    });
    $('#moduleWrapper109').each(function () {
        $('body').css({
            'background': 'url(/_media/themes/1/layout/hggalerieFS2010_1.jpg) #97aaa7'
        });
    });
    $('#buttonLoginSubmit').
        css('display', 'none');
    $('#linkLoginSubmit').
        css('display', 'block').
        click(function () {
            $(this).parent().submit();
        });
    $('#buttonSearchSubmit').
        css('display', 'none');
    $('#linkSearchSubmit').
        css('display', 'block').
        click(function () {
            $(this).parent().submit();
        });
    $('#inputLoginUser_de input, #inputLoginPass_de input, #inputLoginUser_en input, #inputLoginPass_en input').click(function () {
        $(this).css('background', 'url(/_media/themes/1/layout/bgLogin.gif) no-repeat 0 -120px');
    });
    $('#inputTextSearch').click(function () {
        $(this).css('background', 'url(/_media/themes/1/layout/bgSearch.gif) no-repeat 0 -30px');
    });
};
cms.collection = {
    'curIdx': 0
};
cms.collection.config = {
    'zoom': {
        'zoomWidth': 180,               // EDIT width
        'zoomHeight': 180,              // EDIT height
        'leftOffset': 700,              // EDIT position (absolute to parent layer)
        'topOffset': 230,               // EDIT position (absolute to parent layer)
        'nodelist': []                  // AUTO the large images will be stored here
    },
    'imagelistL': {
        'imageSrcs': []                // AUTO image sources
    },
    'imagelistM': {
        'imageSrcs': [],                // AUTO image sources
        'imageWidths': [],              // AUTO image widths
        'imageHeights': [],             // AUTO image heights
        'nodelist': []
    },
    'imagelistS': {
        'visibleWidth': 801,            // EDIT width of ul's parent layer
        'margin': 9,                    // EDIT margin between the list items
        'border': 1,                    // EDIT border active item
        'animationTime': 1000,           // EDIT time in ms the list takes to reposition
        'count': 0,                     // AUTO number of images in list
        'curIdx': 0,                    // AUTO current image idx
        'maxOffset': 0,                 // AUTO max negative ul's offset
        'width': 99,                    // AUTO ul's width (added image widths and margins)
        'curOffset': 0,                 // AUTO ul's current negative offset
        'imageSrcs': [],                // AUTO image sources
        'imageWidths': [],              // AUTO image widths
        'imageHeights': [],             // AUTO image heights
        'imageOffsetsPrev': [],         // AUTO the offsets the ul needs to display the image adjacent to the prev end of the visible area
        'imageOffsetsCenter': [],       // AUTO the offsets the ul needs to display the image centered the visible area
        'imageOffsetsNext': [],         // AUTO the offsets the ul needs to display the image adjacent to the next end of the visible area
        'imageOffsetsPrevEdge': [],     // AUTO the offsets of the image edge facing prev end
        'imageOffsetsNextEdge': [],     // AUTO the offsets of the image edge facing next end
        'visibles': [],                 // AUTO idx of the completely visible images
        'nodelist': [],                 // AUTO generated nodes
        'articleHTML': []               // AUTO article descriptions
    },
    'curIdx': 0,
    'count': 0
};
cms.collection.init = function () {
    if (cms.template.config.collection) {
        this.config.count = this.count;
        this.config.imagelistS.count = this.count;
        this.config.curIdx = this.config.imagelistS.curIdx;
        this.setWidth();
        this.setMaxOffset();
        this.setImageOffsets();
        this.setVisibles();
        this.preloadImages();
        this.buildHTML();
        this.insertZoomLink();
        this.insertArticleDescription();
        this.bindEvents();
    }
};
cms.collection.setCurIdx = function (idx) {
    var config = this.config.imagelistS;
    $('#imageS' + config.curIdx).css('opacity', 0.5);
    $('#imageParentS' + config.curIdx).attr('class', 'passive');
    this.curIdx = idx;
    this.config.curIdx = idx;
    this.config.imagelistS.curIdx = idx;
    this.setCurOffset(config.imageOffsetsCenter[idx]);
    //this.insertZoomLink();
    //this.insertArticleDescription();
    $('#imageS' + idx).css('opacity', 1);
    $('#imageParentS' + idx).attr('class', 'current');
};
cms.collection.setCurOffset = function (offset) {
    var config = this.config.imagelistS;
    if (offset <= 0 && offset > config.maxOffset) {
        config.curOffset = offset;
    }
    else {
        if (offset > 0) {
            config.curOffset = 0;
        }
        else {
            config.curOffset = config.maxOffset;
        }
    }
    this.setVisibles();
    this.moveList();
};
cms.collection.setWidth = function () {
    var config = this.config.imagelistS,
        width = 0,
        i;
    for (i = 0; i < config.count; i++) {
        width += config.imageWidths[i];
        width += config.border * 2;
        width += config.margin;
    }
    width -= config.margin;
    config.width = width;
};
cms.collection.setImageOffsets = function () {
    var config = this.config.imagelistS,
        width = 0,
        i;
    for (i = 0; i < config.count; i++) {
        config.imageOffsetsPrev.push(width);
        config.imageOffsetsCenter.push(width);
        config.imageOffsetsCenter[i] += config.visibleWidth / 2;
        config.imageOffsetsCenter[i] -= config.imageWidths[i] / 2;
        config.imageOffsetsNext.push(width);
        config.imageOffsetsNext[i] += config.visibleWidth - config.imageWidths[i] - config.border * 2;
        config.imageOffsetsPrevEdge.push(width);
        config.imageOffsetsNextEdge.push(width);
        config.imageOffsetsNextEdge[i] -= config.imageWidths[i] + config.border * 2;
        width -= config.imageWidths[i];
        width -= config.margin;
        width -= config.border * 2;
    }
};
cms.collection.setMaxOffset = function () {
    var config = this.config.imagelistS;
    config.maxOffset = - (config.width - config.visibleWidth);
    if (this.count < 8) {
        config.maxOffset = 0;
        $('#imageListSPrevTrigger, #imageListSNextTrigger').css({'opacity': 0.4, 'cursor': 'default'});
    }
};
cms.collection.setVisibles = function () {
    var config = this.config.imagelistS,
        first = false,
        i;
    config.visibles = [];
    for (i = 0; i < this.config.count; i++) {
        if (config.imageOffsetsPrevEdge[i] <= config.curOffset && !first) {
            first = true;
        }
        if (config.imageOffsetsNextEdge[i] < config.curOffset - config.visibleWidth) {
            break;
        }
        if (first) {
            config.visibles.push(i);
        }
    }
};
cms.collection.triggerNextImage = function () {
    var selector = '#imageS' + (this.config.curIdx + 1);
    if (this.config.curIdx < this.config.count) {
        $(selector).trigger('click');
    }
};
cms.collection.triggerPrevImage = function () {
    var selector = '#imageS' + (this.config.curIdx - 1);
    if (this.config.curIdx > 0) {
        $(selector).trigger('click');
    }
};
cms.collection.triggerNextVisibles = function () {
    var config = this.config.imagelistS,
        lastVisibleIdx = config.visibles[config.visibles.length - 1],
        newOffset = (lastVisibleIdx !== this.config.count - 1) ? config.imageOffsetsPrev[lastVisibleIdx + 1] : config.maxOffset;
    this.setCurOffset(newOffset);
};
cms.collection.triggerPrevVisibles = function () {
    var config = this.config.imagelistS,
        firstVisibleIdx = config.visibles[0],
        newOffset = (firstVisibleIdx) ? config.imageOffsetsNext[firstVisibleIdx - 1] : 0;
    this.setCurOffset(newOffset);
};
cms.collection.moveList = function () {
    $('#imageListS ul').animate(
        {
            'left': cms.collection.config.imagelistS.curOffset
        },
        {
            'duration': this.config.imagelistS.animationTime,
            'easing': 'easeOutCirc',
            'complete': function () {
                cms.collection.insertZoomLink();
                cms.collection.insertArticleDescription();
            }
        }
    );
};
cms.collection.insertZoomLink = function () {
    var config = this.config,
        zoomLink;
    if (!config.zoom.nodelist[config.curIdx]) {
        zoomLink = document.createElement('a');
        $(zoomLink).
            attr('href', config.imagelistL.imageSrcs[config.curIdx]).
            attr('id', 'zoomLink' + config.curIdx).
            css({
                'position': 'absolute',
                'left': (420 - config.imagelistM.imageWidths[config.curIdx]) / 2 + 'px',
                'top': (420 - config.imagelistM.imageHeights[config.curIdx]) / 2 + 'px',
                'display': 'block'
            }).
            append(config.imagelistM.nodelist[config.curIdx]);
        config.zoom.nodelist[config.curIdx] = zoomLink;
    }
    else {
        zoomLink = config.zoom.nodelist[config.curIdx];
    }
    if($(zoomLink).attr('id') !== $('#imageM a').attr('id')) {
        $(zoomLink).css('opacity', 0);
        $('#imageM').
            empty().
            append(zoomLink);
        $(zoomLink).
            animate({'opacity': 1}, 'slow', function() {
                $(this).jqzoom(config.zoom);
        });
    }
};
cms.collection.insertArticleDescription = function () {
    var config = cms.collection.config.imagelistS;
    $('#collectionArticleDescription').
        empty();
    if (config.articleHTML[config.curIdx]) {
        $('#collectionArticleDescription').
            append(config.articleHTML[config.curIdx]);
    }
};
cms.collection.preloadImages = function () {
    var config = this.config,
        i;
    function preloadImageS(idx) {
        var config = cms.collection.config.imagelistS,
            image,
            opacity = (idx === 0) ? 1 : 0.75;

        image = new Image();
        $(image).
            attr({'src': config.imageSrcs[idx], 'alt': '', 'id': 'imageS' + idx}).
            css('opacity', 0).
            data('idx', idx);
        if (image.complete || image.readyState === 'complete') {
            $('#imageParentS' + idx).css({'background': 'none #fff'});
            $(image).
                animate({'opacity': opacity}, 300);
        }
        else {
            $(image).load(function () {
                $('#imageParentS' + idx).css({'background': 'none #fff'});
                $(image).
                    animate({'opacity': opacity}, 300);
            });
        }
        config.nodelist[idx] = (image);
    }
    function preloadImageM(idx) {
        var config = cms.collection.config.imagelistM,
            image;

        image = new Image();
        $(image).
            attr({'src': config.imageSrcs[idx], 'alt': '', 'id': 'imageM' + idx}).
            css('opacity', 0);
        if (image.complete || image.readyState === 'complete') {
            $('#imageParentM' + idx).css({'background': 'none transparent'});
            $(image).
                animate({'opacity': 1}, 300);
        }
        else {
            $(image).load(function () {
                $('#imageParentM' + idx).css({'background': 'none transparent'});
                $(image).
                    animate({'opacity': 1}, 300);
            });
        }
        config.nodelist[idx] = (image);
    }
    for (i = 0; i < config.count; i++) {
        preloadImageS(i);
        preloadImageM(i);
    }
};
cms.collection.buildHTML = function () {
    var config = this.config,
        ul = document.createElement('ul'),
        li,
        liId,
        cssClass,
        i;
    $(ul).css('width', config.imagelistS.width + config.imagelistS.margin);
    for (i = 0; i < config.count; i++) {
        li = document.createElement('li');
        liId = 'imageParentS' + i;
        cssClass = (i === 0) ? 'current' : 'passive';
        $(li).
            attr({'id': liId, 'class': cssClass}).
            data('idx', i).
            append(cms.collection.config.imagelistS.nodelist[i]).
            appendTo($(ul));
    }
    $('#imageListS').append(ul);
};
cms.collection.bindEvents = function () {
    $('#imageListMNextTrigger').click(function () {
        cms.collection.triggerNextImage();
    });
    $('#imageListMPrevTrigger').click(function () {
        cms.collection.triggerPrevImage();
    });
    $('#imageListSNextTrigger').click(function () {
        cms.collection.triggerNextVisibles();

    });
    $('#imageListSPrevTrigger').click(function () {
        cms.collection.triggerPrevVisibles();
    });
    $('#imageListS img').each(function () {
        $(this).
            click(function () {
                if ($(this).data('idx') !== cms.collection.curIdx) {
                    cms.collection.setCurIdx($(this).data('idx'));
                }
            }).
            hover(
                function () {
                    if ($(this).data('idx') !== cms.collection.curIdx) {
                        $('#imageS' + $(this).data('idx')).css('opacity', 0.9);
                    }
                },
                function () {
                    if ($(this).data('idx') !== cms.collection.config.curIdx) {
                        $('#imageS' + $(this).data('idx')).css('opacity', 0.75);
                    }
                }
            );
    });
};

/**
 * Cookie plugin
 *
 * Copyright (c) 2006 Klaus Hartl (stilbuero.de)
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 *
 */

/**
 * Create a cookie with the given name and value and other optional parameters.
 *
 * @example $.cookie('the_cookie', 'the_value');
 * @desc Set the value of a cookie.
 * @example $.cookie('the_cookie', 'the_value', { expires: 7, path: '/', domain: 'jquery.com', secure: true });
 * @desc Create a cookie with all available options.
 * @example $.cookie('the_cookie', 'the_value');
 * @desc Create a session cookie.
 * @example $.cookie('the_cookie', null);
 * @desc Delete a cookie by passing null as value. Keep in mind that you have to use the same path and domain
 *       used when the cookie was set.
 *
 * @param String name The name of the cookie.
 * @param String value The value of the cookie.
 * @param Object options An object literal containing key/value pairs to provide optional cookie attributes.
 * @option Number|Date expires Either an integer specifying the expiration date from now on in days or a Date object.
 *                             If a negative value is specified (e.g. a date in the past), the cookie will be deleted.
 *                             If set to null or omitted, the cookie will be a session cookie and will not be retained
 *                             when the the browser exits.
 * @option String path The value of the path atribute of the cookie (default: path of page that created the cookie).
 * @option String domain The value of the domain attribute of the cookie (default: domain of page that created the cookie).
 * @option Boolean secure If true, the secure attribute of the cookie will be set and the cookie transmission will
 *                        require a secure protocol (like HTTPS).
 * @type undefined
 *
 * @name $.cookie
 * @cat Plugins/Cookie
 * @author Klaus Hartl/klaus.hartl@stilbuero.de
 */

/**
 * Get the value of a cookie with the given name.
 *
 * @example $.cookie('the_cookie');
 * @desc Get the value of a cookie.
 *
 * @param String name The name of the cookie.
 * @return The value of the cookie.
 * @type String
 *
 * @name $.cookie
 * @cat Plugins/Cookie
 * @author Klaus Hartl/klaus.hartl@stilbuero.de
 */

jQuery.cookie = function (name, value, options) {
    var expires,
        date,
        path,
        domain,
        secure,
        cookieValue,
        cookies,
        cookie,
        i;
    if (typeof value !== 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }

        if (options.expires && (typeof options.expires === 'number' || options.expires.toUTCString)) {
            if (typeof options.expires === 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        // CAUTION: Needed to parenthesize options.path and options.domain
        // in the following expressions, otherwise they evaluate to undefined
        // in the packed version for some reason...
        path = options.path ? '; path=' + (options.path) : '';
        domain = options.domain ? '; domain=' + (options.domain) : '';
        secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        cookieValue = null;
        if (document.cookie && document.cookie !== '') {
            cookies = document.cookie.split(';');
            for (i = 0; i < cookies.length; i++) {
                cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) === (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};

/*
 * jQuery Easing v1.3 - http://gsgd.co.uk/sandbox/jquery/easing/
 *
 * Uses the built in easing capabilities added In jQuery 1.1
 * to offer multiple easing options
 *
 * TERMS OF USE - jQuery Easing
 *
 * Open source under the BSD License.
 *
 * Copyright © 2008 George McGinley Smith
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice, this list of
 * conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright notice, this list
 * of conditions and the following disclaimer in the documentation and/or other materials
 * provided with the distribution.
 *
 * Neither the name of the author nor the names of contributors may be used to endorse
 * or promote products derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 *  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
 *  GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 *  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 * OF THE POSSIBILITY OF SUCH DAMAGE.
 *
*/

// t: current time, b: begInnIng value, c: change In value, d: duration
jQuery.easing['jswing'] = jQuery.easing['swing'];

jQuery.extend( jQuery.easing,
{
    def: 'easeOutQuad',
    swing: function (x, t, b, c, d) {
        //alert(jQuery.easing.default);
        return jQuery.easing[jQuery.easing.def](x, t, b, c, d);
    },
    easeInQuad: function (x, t, b, c, d) {
        return c*(t/=d)*t + b;
    },
    easeOutQuad: function (x, t, b, c, d) {
        return -c *(t/=d)*(t-2) + b;
    },
    easeInOutQuad: function (x, t, b, c, d) {
        if ((t/=d/2) < 1) return c/2*t*t + b;
        return -c/2 * ((--t)*(t-2) - 1) + b;
    },
    easeInCubic: function (x, t, b, c, d) {
        return c*(t/=d)*t*t + b;
    },
    easeOutCubic: function (x, t, b, c, d) {
        return c*((t=t/d-1)*t*t + 1) + b;
    },
    easeInOutCubic: function (x, t, b, c, d) {
        if ((t/=d/2) < 1) return c/2*t*t*t + b;
        return c/2*((t-=2)*t*t + 2) + b;
    },
    easeInQuart: function (x, t, b, c, d) {
        return c*(t/=d)*t*t*t + b;
    },
    easeOutQuart: function (x, t, b, c, d) {
        return -c * ((t=t/d-1)*t*t*t - 1) + b;
    },
    easeInOutQuart: function (x, t, b, c, d) {
        if ((t/=d/2) < 1) return c/2*t*t*t*t + b;
        return -c/2 * ((t-=2)*t*t*t - 2) + b;
    },
    easeInQuint: function (x, t, b, c, d) {
        return c*(t/=d)*t*t*t*t + b;
    },
    easeOutQuint: function (x, t, b, c, d) {
        return c*((t=t/d-1)*t*t*t*t + 1) + b;
    },
    easeInOutQuint: function (x, t, b, c, d) {
        if ((t/=d/2) < 1) return c/2*t*t*t*t*t + b;
        return c/2*((t-=2)*t*t*t*t + 2) + b;
    },
    easeInSine: function (x, t, b, c, d) {
        return -c * Math.cos(t/d * (Math.PI/2)) + c + b;
    },
    easeOutSine: function (x, t, b, c, d) {
        return c * Math.sin(t/d * (Math.PI/2)) + b;
    },
    easeInOutSine: function (x, t, b, c, d) {
        return -c/2 * (Math.cos(Math.PI*t/d) - 1) + b;
    },
    easeInExpo: function (x, t, b, c, d) {
        return (t==0) ? b : c * Math.pow(2, 10 * (t/d - 1)) + b;
    },
    easeOutExpo: function (x, t, b, c, d) {
        return (t==d) ? b+c : c * (-Math.pow(2, -10 * t/d) + 1) + b;
    },
    easeInOutExpo: function (x, t, b, c, d) {
        if (t==0) return b;
        if (t==d) return b+c;
        if ((t/=d/2) < 1) return c/2 * Math.pow(2, 10 * (t - 1)) + b;
        return c/2 * (-Math.pow(2, -10 * --t) + 2) + b;
    },
    easeInCirc: function (x, t, b, c, d) {
        return -c * (Math.sqrt(1 - (t/=d)*t) - 1) + b;
    },
    easeOutCirc: function (x, t, b, c, d) {
        return c * Math.sqrt(1 - (t=t/d-1)*t) + b;
    },
    easeInOutCirc: function (x, t, b, c, d) {
        if ((t/=d/2) < 1) return -c/2 * (Math.sqrt(1 - t*t) - 1) + b;
        return c/2 * (Math.sqrt(1 - (t-=2)*t) + 1) + b;
    },
    easeInElastic: function (x, t, b, c, d) {
        var s=1.70158;var p=0;var a=c;
        if (t==0) return b;  if ((t/=d)==1) return b+c;  if (!p) p=d*.3;
        if (a < Math.abs(c)) { a=c; var s=p/4; }
        else var s = p/(2*Math.PI) * Math.asin (c/a);
        return -(a*Math.pow(2,10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )) + b;
    },
    easeOutElastic: function (x, t, b, c, d) {
        var s=1.70158;var p=0;var a=c;
        if (t==0) return b;  if ((t/=d)==1) return b+c;  if (!p) p=d*.3;
        if (a < Math.abs(c)) { a=c; var s=p/4; }
        else var s = p/(2*Math.PI) * Math.asin (c/a);
        return a*Math.pow(2,-10*t) * Math.sin( (t*d-s)*(2*Math.PI)/p ) + c + b;
    },
    easeInOutElastic: function (x, t, b, c, d) {
        var s=1.70158;var p=0;var a=c;
        if (t==0) return b;  if ((t/=d/2)==2) return b+c;  if (!p) p=d*(.3*1.5);
        if (a < Math.abs(c)) { a=c; var s=p/4; }
        else var s = p/(2*Math.PI) * Math.asin (c/a);
        if (t < 1) return -.5*(a*Math.pow(2,10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )) + b;
        return a*Math.pow(2,-10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )*.5 + c + b;
    },
    easeInBack: function (x, t, b, c, d, s) {
        if (s == undefined) s = 1.70158;
        return c*(t/=d)*t*((s+1)*t - s) + b;
    },
    easeOutBack: function (x, t, b, c, d, s) {
        if (s == undefined) s = 1.70158;
        return c*((t=t/d-1)*t*((s+1)*t + s) + 1) + b;
    },
    easeInOutBack: function (x, t, b, c, d, s) {
        if (s == undefined) s = 1.70158;
        if ((t/=d/2) < 1) return c/2*(t*t*(((s*=(1.525))+1)*t - s)) + b;
        return c/2*((t-=2)*t*(((s*=(1.525))+1)*t + s) + 2) + b;
    },
    easeInBounce: function (x, t, b, c, d) {
        return c - jQuery.easing.easeOutBounce (x, d-t, 0, c, d) + b;
    },
    easeOutBounce: function (x, t, b, c, d) {
        if ((t/=d) < (1/2.75)) {
            return c*(7.5625*t*t) + b;
        } else if (t < (2/2.75)) {
            return c*(7.5625*(t-=(1.5/2.75))*t + .75) + b;
        } else if (t < (2.5/2.75)) {
            return c*(7.5625*(t-=(2.25/2.75))*t + .9375) + b;
        } else {
            return c*(7.5625*(t-=(2.625/2.75))*t + .984375) + b;
        }
    },
    easeInOutBounce: function (x, t, b, c, d) {
        if (t < d/2) return jQuery.easing.easeInBounce (x, t*2, 0, c, d) * .5 + b;
        return jQuery.easing.easeOutBounce (x, t*2-d, 0, c, d) * .5 + c*.5 + b;
    }
});

/*
 *
 * TERMS OF USE - EASING EQUATIONS
 *
 * Open source under the BSD License.
 *
 * Copyright © 2001 Robert Penner
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice, this list of
 * conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright notice, this list
 * of conditions and the following disclaimer in the documentation and/or other materials
 * provided with the distribution.
 *
 * Neither the name of the author nor the names of contributors may be used to endorse
 * or promote products derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 *  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
 *  GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 *  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 * OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 */

/*
 * JQZoom Evolution 1.0.1 - Javascript Image magnifier
 *
 * Copyright (c) Engineer Renzi Marco(www.mind-projects.it)
 *
 * $Date: 12-12-2008
 *
 *  ChangeLog:
 *
 * $License : GPL,so any change to the code you should copy and paste this section,and would be nice to report this to me(renzi.mrc@gmail.com).
 */
(function($) {
    $.fn.jqzoom = function(options)
    {
        var settings = {
            zoomType: 'standard', //standard/reverse/innerzoom
            zoomWidth: 200,     //zoomed width default width
            zoomHeight: 200,        //zoomed div default width
            xOffset: 10,        //zoomed div default offset
            yOffset: 0,
            position: "right" ,//zoomed div default position,offset position is to the right of the image
            lens:true, //zooming lens over the image,by default is 1;
            lensReset : false,
            imageOpacity: 0.2,
            title : true,
            alwaysOn: false,
            showEffect: 'fadein',
            hideEffect: 'fadeout',
            fadeinSpeed: 'fast',
            fadeoutSpeed: 'fast',
            preloadImages :true,
            showPreload: true,
            preloadText : 'Loading zoom',
            preloadPosition : 'center'   //bycss
        };

            //extending options
            options = options || {};
            $.extend(settings, options);


        return this.each(function()
        {
            var a = $(this);
            var aTitle = a.attr('title'); //variabile per memorizzare il titolo href
            $(a).removeAttr('title');
            $(a).css('outline-style','none');


            var img = $("img", this);
            var imageTitle = img.attr('title');
            img.removeAttr('title');    //variabile per memorizzare il titolo immagine


            var smallimage = new Smallimage( img );

            //imageborder
            var btop = 0;
            var bleft = 0;

            var loader = null;     //variabile per memorizzare oggetto loader
            loader = new Loader();

            var ZoomTitle = (trim(aTitle).length > 0) ? aTitle :
            (trim(imageTitle).length > 0) ? imageTitle : null;  //setting zoomtitle
            var ZoomTitleObj = new zoomTitle();

            var largeimage = new Largeimage( a[0].href );

            var lens = new Lens();
            var lensdata = {};
            //lensborder



            var largeimageloaded = false;
            var scale = {}; //rapporto tra immagine grande e piccola scale.x/scale.y
            var stage = null; // quadrato che mostra l'immagine ingrandita
            var running = false; // running = true quando si verifica l'evento che mostra lo zoom(adesso mouseover).
            var mousepos = {};
            var firstime = 0;
            var preloadshow = false;
            var isMouseDown = false;
            var dragstatus = false
            //loading smallimagedata
            smallimage.loadimage();

            //ritorna false al click dell href
            $(this).click(function(){return false;});

            //se settato alwaysOn attivo lo Zoom e lo mostro.

            //attivo al mouseover
            $(this).hover(function(e)
            {
                mousepos.x = e.pageX;
                mousepos.y  = e.pageY;
                activate();
            },function()
            {
                deactivate();
            });


            //ALWAYS ON
            if(settings.alwaysOn)
            {
                setTimeout(function(){activate();},150);
            }


            function activate()
            {

                if ( !running ) {

                    //finding border
                    smallimage.findborder();

                    running = true;

                    //rimuovo il titolo al mouseover
                    imageTitle = img.attr('title');
                    img.removeAttr('title');
                    aTitle = a.attr('title');
                    $(a).removeAttr('title');

                    //se non cè creo l'oggetto largeimage
                    if (!largeimage || $.browser.safari) {
                        largeimage = new Largeimage( a[0].href );
                    }

                    //se l'immagine grande non è stata caricata la carico
                    if(!largeimageloaded || $.browser.safari)
                    {
                        largeimage.loadimage();
                    }else
                    {
                    //after preload
                        if(settings.zoomType != 'innerzoom')
                        {
                            stage = new Stage();
                            stage.activate();
                        }
                        lens = new Lens;
                        lens.activate();
                    }

                    //hack per MAC
                /*  if($.browser.safari)
                    {
                        if(settings.zoomType != 'innerzoom') //se innerzoom non mostro la finestra dello zoom
                        {
                            stage = new Stage();
                            stage.activate();
                        }
                        if($('div.jqZoomPup').length <= 0)
                        {
                        lens = new Lens();
                        }
                        //if(settings.zoomType == 'innerzoom'){lens = new Lens()};
                        lens.activate();
                        (settings.alwaysOn) ? lens.center() : lens.setposition(null);
                    }
                    */
                    a[0].blur();
                    //alert($('div.jqZoomPup').length);
                    return false;
                }




            }

            function deactivate()
            {
                if(settings.zoomType == 'reverse' &&  !settings.alwaysOn)
                {
                    img.css({'opacity' : 1});
                }

                if(!settings.alwaysOn)
                {
                    //resetting parameters
                    running = false;
                    largeimageloaded = false;
                    $(lens.node).unbind('mousemove');
                    lens.remove();
                    if($('div.jqZoomWindow').length >0)
                    {
                        stage.remove();
                    }
                    if($('div.jqZoomTitle').length > 0)
                    {
                        ZoomTitleObj.remove();
                    }
                    //resetting title
                    img.attr('title',imageTitle);
                    a.attr('title',aTitle);
                    $().unbind();

                    a.unbind('mousemove');
                    //resetto il parametro che mi dice che è la prima volta che mostor lo zoom
                    firstime = 0;
                    //remove ieiframe
                    if(jQuery('.zoom_ieframe').length > 0)
                    {
                        jQuery('.zoom_ieframe').remove();
                    }
                }else
                {
                    if(settings.lensReset)
                    {
                        switch(settings.zoomType)
                        {
                            case 'innerzoom':
                            largeimage.setcenter();
                            break;
                            default:
                            lens.center();
                            break;
                        }
                    }
                }

                //non so se serve da provare
                if(settings.alwaysOn)
                {
                    activate();
                }
            };





        //smallimage
        function Smallimage( image )
        {
            this.node = image[0];
            smallimagedata = {};
                smallimagedata.w = $( this.node ).width();
                smallimagedata.h = $( this.node ).height();


                //non viene fatta assegnazione alla variabile globale
                smallimagedata.h = $( this.node ).height();
                smallimagedata.pos = $( this.node ).offset();
                smallimagedata.pos.l = $( this.node ).offset().left;
                smallimagedata.pos.t = $( this.node ).offset().top;
                smallimagedata.pos.r = smallimagedata.w + smallimagedata.pos.l;
                smallimagedata.pos.b = smallimagedata.h + smallimagedata.pos.t;
            this.loadimage = function() {
                this.node.src = image[0].src;
            };
            this.findborder = function()
            {
                var bordertop = '';
                bordertop = $(img).css('border-top-width');
                btop = '';
                var borderleft = '';
                borderleft = $(img).css('border-left-width');
                bleft = '';
                /*if($.browser.msie)
                {
                    var temp = bordertop.split(' ');

                    bordertop = temp[1];
                    var temp = borderleft.split(' ');
                    borderleft = temp[1];
                }*/

                if(bordertop)
                {
                    for(i=0;i<3;i++)
                    {
                        var x = [];
                        x = bordertop.substr(i,1);

                        if(isNaN(x) == false)
                        {
                            btop = btop +''+ bordertop.substr(i,1);
                        }else
                        {
                            break;
                        }
                    }
                }

                if(borderleft)
                {
                    for(i=0;i<3;i++)
                    {
                        if(!isNaN(borderleft.substr(i,1)))
                        {
                            bleft = bleft + borderleft.substr(i,1)
                        }else
                        {
                            break;
                        }
                    }
                }
                btop = (btop.length > 0) ? eval(btop) : 0;
                bleft = (bleft.length > 0) ? eval(bleft) : 0;


            }
            this.node.onload = function()
            {
                //setto il cursor e la posizione dell'href


                a.css({'cursor':'crosshair','display':'block'});

                if(a.css('position')!= 'absolute' && a.parent().css('position'))
                {
                    a.css({'cursor':'crosshair','position':'relative','display':'block'});
                }
                if(a.parent().css('position') != 'absolute')
                {
                    a.parent().css('position','relative');
                    //a.css('position','relative');
                }
                else{
                //a.css('position','relative');
                }
                if($.browser.safari || $.browser.opera)
                {
                    $(img).css({position:'absolute',top:'0px',left:'0px'});
                }
                /*if(a.css('position')!= 'absolute' && a.parent().css('position'))
                {
                    a.css({'cursor':'crosshair','position':'relative','display':'block'});
                }
                if(a.parent().css('position') != 'absolute')
                {
                    alert('in');
                    a.parent().css('position','relative');
                    //a.css('position','relative');
                }
                else{
                //a.css('position','relative');
                }*/



                /*
                if(a.parent().css('position') != 'relative' && a.css('position') != 'absolute')
                {
                a.css({'cursor':'crosshair','position':'relative','display':'block'});
                }*/

                //al docuemnt ready viene caricato l'src quindi viene azionato l'onload e carico tutti i dati
                smallimagedata.w = $( this ).width();
                smallimagedata.h = $( this ).height();


                //non viene fatta assegnazione alla variabile globale
                smallimagedata.h = $( this ).height();
                smallimagedata.pos = $( this ).offset();
                smallimagedata.pos.l = $( this ).offset().left;
                smallimagedata.pos.t = $( this ).offset().top;
                smallimagedata.pos.r = smallimagedata.w + smallimagedata.pos.l;
                smallimagedata.pos.b = smallimagedata.h + smallimagedata.pos.t;

                //per sicurezza setto l'altezza e la width dell'href
                a.height(smallimagedata.h);
                a.width(smallimagedata.w);


                //PRELOAD IMAGES
                if(settings.preloadImages)
                {
                    largeimage.loadimage();
                }



            };



            return this;
        };



        //Lens
        function Lens()
        {


            //creating element and adding class
            this.node = document.createElement("div");
            $(this.node).addClass('jqZoomPup');

            this.node.onerror = function() {
                $( lens.node ).remove();
                lens = new Lens();
                lens.activate() ;
            };




            //funzione privata per il caricamento dello zoom
            this.loadlens = function()
            {


                switch(settings.zoomType)
                {
                    case 'reverse':
                        this.image = new Image();
                        this.image.src = smallimage.node.src; // fires off async
                        this.node.appendChild( this.image );
                        $( this.node ).css({'opacity' : 1});
                    break;
                    case 'innerzoom':

                        this.image = new Image();
                        this.image.src = largeimage.node.src; // fires off async
                        this.node.appendChild( this.image );
                        $( this.node ).css({'opacity' : 1});
                    break
                    default:
                    break;
                }



                switch(settings.zoomType)
                {
                    case 'innerzoom':
                        lensdata.w = smallimagedata.w;
                        lensdata.h = smallimagedata.h;
                    break;
                    default:
                        //console.log(scale.x);
                        lensdata.w = (settings.zoomWidth)/scale.x;
                        lensdata.h = (settings.zoomHeight)/scale.y;
                    break;
                }

            $( this.node ).css({
                    width: lensdata.w + 'px',
                    height: lensdata.h + 'px',
                    position: 'absolute',
                    /*cursor: 'crosshair',*/
                    display: 'none',
                    //border: '1px solid blue'
                    borderWidth: 1+'px'
                });
            a.append(this.node);
            }
            return this;
        };

        Lens.prototype.activate = function()
        {
            //carico la lente
            this.loadlens();

            switch(settings.zoomType)
            {
                case 'reverse':
                    img.css({'opacity' : settings.imageOpacity});

                    (settings.alwaysOn) ? lens.center() : lens.setposition(null);
                    //lens.center();
                    //bindo ad a il mousemove della lente
                    a.bind( 'mousemove', function(e)
                    {
                        mousepos.x = e.pageX;
                        mousepos.y = e.pageY;
                        lens.setposition( e );
                    });
                break;
                case 'innerzoom':

                    //  lens = new Lens();
                    //  lens.activate();

                    $( this.node ).css({top : 0 ,left: 0});
                    if(settings.title)
                    {
                        ZoomTitleObj.loadtitle();
                    }

                    largeimage.setcenter();

                    a.bind( 'mousemove', function(e)
                    {
                        mousepos.x = e.pageX;
                        mousepos.y = e.pageY;
                        largeimage.setinner( e );

                    /*if(settings.zoomType == 'innerzoom' && running)
                    {
                        $(a).mousemove(function(){
                            if($('div.jqZoomPup').length <= 0)
                            {
                                lens = new Lens();
                                lens.activate();
                            }
                        });
                    }*/

                        /*if($('div.jqZoomPup').length <= 0)
                            {
                                lens = new Lens();
                                lens.activate();
                            }*/

                    });
                break;
                default:
                    /*$(document).mousemove(function(e){
                    if(isMouseDown && dragstatus != false){
                    lens.setposition( e );
                    }
                    });
                    lens.center()


                    dragstatus = 'on'
                    $(document).mouseup(function(e){
                    if(isMouseDown && dragstatus != false){
                        isMouseDown = false;
                        dragstatus = false;

                    }
                    });

                    $(this.node).mousedown(function(e){
                    $('div.jqZoomPup').css("cursor", "move");
                    $(this.node).css("position", "absolute");

                // set z-index
                    $(this.node).css("z-index", parseInt( new Date().getTime()/1000 ));
                    if($.browser.safari)
                    {
                        $(a).css("cursor", "move");
                    }
                    isMouseDown    = true;
                    dragstatus = 'on';
                    lens.setposition( e );
                    });
                    */


                    (settings.alwaysOn) ? lens.center() : lens.setposition(null);

                    //bindo ad a il mousemove della lente
                    $(a).bind( 'mousemove', function(e)
                    {

                        mousepos.x = e.pageX;
                        mousepos.y = e.pageY;
                        lens.setposition( e );
                    });

                break;
            }


            return this;
        };

        Lens.prototype.setposition = function( e)
        {


            if(e)
            {
                mousepos.x = e.pageX;
                mousepos.y  = e.pageY;
            }

            if(firstime == 0)
            {
                var lensleft = (smallimagedata.w)/2 - (lensdata.w)/2 ;
                var lenstop = (smallimagedata.h)/2 - (lensdata.h)/2 ;
                //ADDED

                $('div.jqZoomPup').show()
                if(settings.lens)
                {
                    this.node.style.visibility = 'visible';
                }
                else
                {
                    this.node.style.visibility = 'hidden';
                    $('div.jqZoomPup').hide();
                }
                //ADDED
                firstime = 1;

            }else
            {
                var lensleft = mousepos.x - smallimagedata.pos.l - (lensdata.w)/2 ;
                var lenstop = mousepos.y - smallimagedata.pos.t -(lensdata.h)/2 ;
            }


                //a sinistra
                if(overleft())
                {
                    lensleft = 0  + bleft;
                }else
                //a destra
                if(overright())
                {
                    if($.browser.msie)
                    {
                    lensleft = smallimagedata.w - lensdata.w  + bleft + 1  ;
                    }else
                    {
                    lensleft = smallimagedata.w - lensdata.w  + bleft - 1  ;
                    }


                }

                //in alto
                if(overtop())
                {
                    lenstop = 0 + btop ;
                }else
                //sotto
                if(overbottom())
                {

                    if($.browser.msie)
                    {
                    lenstop = smallimagedata.h - lensdata.h  + btop + 1 ;
                    }else
                    {
                    lenstop = smallimagedata.h - lensdata.h - 1 + btop  ;
                    }

                }
                lensleft = parseInt(lensleft);
                lenstop = parseInt(lenstop);

                //setto lo zoom ed un eventuale immagine al centro
                $('div.jqZoomPup',a).css({top: lenstop,left: lensleft });

                if(settings.zoomType == 'reverse')
                {
                    $('div.jqZoomPup img',a).css({'position': 'absolute','top': -( lenstop - btop +1) ,'left': -(lensleft - bleft +1)  });
                }

                this.node.style.left = lensleft + 'px';
                this.node.style.top = lenstop + 'px';

                //setto l'immagine grande
                largeimage.setposition();

                function overleft() {
                    //console.log(mousepos.x);
                    //console.log(lensdata);
                    //console.log(bleft);
                    //console.log(smallimagedata.pos.l);
                    return mousepos.x - (lensdata.w +2*1)/2  - bleft < smallimagedata.pos.l;
                }

                function overright() {

                    return mousepos.x + (lensdata.w + 2* 1)/2  > smallimagedata.pos.r + bleft ;
                }

                function overtop() {
                    return mousepos.y - (lensdata.h + 2* 1)/2  - btop < smallimagedata.pos.t;
                }

                function overbottom() {
                    return mousepos.y + (lensdata.h + 2* 1)/2    > smallimagedata.pos.b + btop;
                }

            return this;
        };


        //mostra la lente al centro dell'immagine
        Lens.prototype.center = function()
        {
            $('div.jqZoomPup',a).css('display','none');
            var lensleft = (smallimagedata.w)/2 - (lensdata.w)/2 ;
            var lenstop = (smallimagedata.h)/2 - (lensdata.h)/2;
            this.node.style.left = lensleft + 'px';
            this.node.style.top = lenstop + 'px';
            $('div.jqZoomPup',a).css({top: lenstop,left: lensleft });

            if(settings.zoomType == 'reverse')
            {
                /*if($.browser.safari){
                    alert('safari');
                    alert(2*bleft);
                    $('div.jqZoomPup img',a).css({'position': 'absolute','top': -( lenstop - btop +1) ,'left': -(lensleft - 2*bleft)  });
                }else
                {*/
                    $('div.jqZoomPup img',a).css({'position': 'absolute','top': -(lenstop - btop + 1) ,'left': -( lensleft  - bleft +1)   });
                //}
            }

            largeimage.setposition();
            if($.browser.msie)
            {
                $('div.jqZoomPup',a).show();
            }else
            {
                setTimeout(function(){$('div.jqZoomPup').fadeIn('fast');},10);
            }
        };


        //ritorna l'offset
        Lens.prototype.getoffset = function() {
            var o = {};
            o.left = parseInt(this.node.style.left) ;
            o.top =  parseInt(this.node.style.top) ;
            return o;
        };

        //rimuove la lente
        Lens.prototype.remove = function()
        {

            if(settings.zoomType == 'innerzoom')
            {
                $('div.jqZoomPup',a).fadeOut('fast',function(){/*$('div.jqZoomPup img').remove();*/$(this).remove();});
            }else
            {
                //$('div.jqZoomPup img').remove();
                $('div.jqZoomPup',a).remove();
            }
        };

        Lens.prototype.findborder = function()
        {
            var bordertop = '';
            bordertop = $('div.jqZoomPup').css('borderTop');
            //alert(bordertop);
            lensbtop = '';
            var borderleft = '';
            borderleft = $('div.jqZoomPup').css('borderLeft');
            lensbleft = '';
            if($.browser.msie)
            {
                var temp = bordertop.split(' ');

                bordertop = temp[1];
                var temp = borderleft.split(' ');
                borderleft = temp[1];
            }

            if(bordertop)
            {
                for(i=0;i<3;i++)
                {
                    var x = [];
                    x = bordertop.substr(i,1);

                    if(isNaN(x) == false)
                    {
                        lensbtop = lensbtop +''+ bordertop.substr(i,1);
                    }else
                    {
                        break;
                    }
                }
            }

            if(borderleft)
            {
                for(i=0;i<3;i++)
                {
                    if(!isNaN(borderleft.substr(i,1)))
                    {
                        lensbleft = lensbleft + borderleft.substr(i,1)
                    }else
                    {
                        break;
                    }
                }
            }


            lensbtop = (lensbtop.length > 0) ? eval(lensbtop) : 0;
            lensbleft = (lensbleft.length > 0) ? eval(lensbleft) : 0;
        }

        //LARGEIMAGE
        function Largeimage( url )
        {
            this.url = url;
            this.node = new Image();

            /*if(settings.preloadImages)
            {
                preload.push(new Image());
                preload.slice(-1).src = url ;
            }*/

            this.loadimage = function()
            {


                if(!this.node)
                this.node = new Image();

                this.node.style.position = 'absolute';
                this.node.style.display = 'none';
                this.node.style.left = '-5000px';
                this.node.style.top = '10px';
                loader = new Loader();

                if(settings.showPreload && !preloadshow)
                {
                    loader.show();
                    preloadshow = true;
                }

                document.body.appendChild( this.node );
                this.node.src = this.url; // fires off async
            }

            this.node.onload = function()
            {
                this.style.display = 'block';
                var w = Math.round($(this).width());
                var h = Math.round($(this).height());

                this.style.display = 'none';

                //setting scale
                //console.log(w);
                //console.log(smallimagedata.w);
                scale.x = (w / smallimagedata.w);
                scale.y = (h / smallimagedata.h);





                if($('div.preload').length > 0)
                {
                    $('div.preload').remove();
                }

                largeimageloaded = true;

                if(settings.zoomType != 'innerzoom' && running){
                    stage = new Stage();
                    stage.activate();
                }

                if(running)
                {
                //alert('in');
                lens = new Lens();

                lens.activate() ;

                }
                //la attivo

                if($('div.preload').length > 0)
                {
                    $('div.preload').remove();
                }
            }
            return this;
        }


        Largeimage.prototype.setposition = function()
        {
            this.node.style.left = Math.ceil( - scale.x * parseInt(lens.getoffset().left) + bleft) + 'px';
            this.node.style.top = Math.ceil( - scale.y * parseInt(lens.getoffset().top) +btop) + 'px';
        };

        //setto la posizione dell'immagine grande nel caso di innerzoom
        Largeimage.prototype.setinner = function(e) {
            this.node.style.left = Math.ceil( - scale.x * Math.abs(e.pageX - smallimagedata.pos.l)) + 'px';
            this.node.style.top = Math.ceil( - scale.y * Math.abs(e.pageY - smallimagedata.pos.t)) + 'px';
            $('div.jqZoomPup img',a).css({'position': 'absolute','top': this.node.style.top,'left': this.node.style.left  });
        };


        Largeimage.prototype.setcenter = function() {
            this.node.style.left = Math.ceil(- scale.x * Math.abs((smallimagedata.w)/2)) + 'px';
            this.node.style.top = Math.ceil( - scale.y * Math.abs((smallimagedata.h)/2)) + 'px';


            $('div.jqZoomPup img',a).css({'position': 'absolute','top': this.node.style.top,'left': this.node.style.left  });
        };


        //STAGE
        function Stage()
        {
            //creating element and class
            this.node = document.createElement("div");
            $(this.node).addClass('jqZoomWindow');

            $( this.node )
                .css({
                    position: 'absolute',
                    width: Math.round(settings.zoomWidth) + 'px',
                    height: Math.round(settings.zoomHeight) + 'px',
                    display: 'none',
                    zIndex: 10000,
                    overflow: 'hidden'
                });
            this.node.style.left = cms.collection.config.zoom.leftOffset + 'px';
            this.node.style.top = cms.collection.config.zoom.topOffset + 'px';
            return this;
        }


        Stage.prototype.activate = function()
        {

            if ( !this.node.firstChild )
                    this.node.appendChild( largeimage.node );


            if(settings.title)
            {
                ZoomTitleObj.loadtitle();
            }
            //iw
            $('#collectionArticleDescription').fadeOut('fast');
            var iwContainer = document.getElementById('website');
            iwContainer.appendChild( this.node );


            switch(settings.showEffect)
            {
                case 'show':
                    $(this.node).show();
                break;
                case 'fadein':
                    $(this.node).fadeIn(settings.fadeinSpeed);
                break;
                default:
                    $(this.node).show();
                break;
            }

            $(this.node).show();

            if ($.browser.msie && $.browser.version < 7) {
            this.ieframe = $('<iframe class="zoom_ieframe" frameborder="0" src="#"></iframe>')
              .css({ position: "absolute", left:this.node.style.left,top:this.node.style.top,zIndex: 99,width:settings.zoomWidth,height:settings.zoomHeight })
              .insertBefore(this.node);
             };


            largeimage.node.style.display = 'block';
        }

        Stage.prototype.remove = function() {
            switch(settings.hideEffect)
            {
                case 'hide':
                    $('.jqZoomWindow').remove();
                break;
                case 'fadeout':
                    $('.jqZoomWindow').fadeOut(settings.fadeoutSpeed);
                break;
                default:
                    $('.jqZoomWindow').remove();
                break;
            }
            //iw
            $('#collectionArticleDescription').fadeIn('fast');
        }

        function zoomTitle()
        {

            this.node =  jQuery('<div />')
                .addClass('jqZoomTitle')
                .html('' + ZoomTitle +'');

            this.loadtitle = function()
            {
                if(settings.zoomType == 'innerzoom')
                {
                    $(this.node)
                    .css({position: 'absolute',
                          top: smallimagedata.pos.b +3,
                          left: (smallimagedata.pos.l+1),
                          width:smallimagedata.w
                          })
                    .appendTo('body');
                }else
                {
                    $(this.node).appendTo(stage.node);
                }
            };
        }

        zoomTitle.prototype.remove = function() {
            $('.jqZoomTitle').remove();
        }


        function Loader()
        {

            this.node = document.createElement("div");
            $(this.node).addClass('preload');
            $(this.node).html(settings.preloadText);//appendo il testo

            var iwContainer = document.getElementById('website');

            $(this.node )
                .appendTo($('#imageM'))
                .css('visibility','hidden');



            this.show = function()
            {

                //setting position
                $(this.node).css({
                            'top': '160px',
                            'left': '0px',
                            'display': 'block',
                            'width': '100%',
                            'text-align': 'center',
                            'visibility':'visible',
                            'z-index': '10000',
                            'position': 'relative'
                            });
            }
            return this;
        }

        Loader.prototype.getoffset = function()
        {
            var o = null;
            o = $('div.preload').offset();
            return o;
        }

        });
    }
})(jQuery);
function trim(stringa) {
    while (stringa.substring(0,1) == ' '){
        stringa = stringa.substring(1, stringa.length);
    }
    while (stringa.substring(stringa.length-1, stringa.length) == ' '){
        stringa = stringa.substring(0,stringa.length-1);
    }
    return stringa;
}
window.log = function (item) {
    if (cms.config.debug) {
        if (window.console) {
            window.console.log(item);
        }
    }
}
$(function () {
    var country_links = $('#store_locator_change_country').find('a'),
        country_select = $(document.createElement('select')),
        first_option = $(document.createElement('option')).text('Bitte wählen');
    country_select.
        append(first_option);
    country_links.each(function () {
        var link_text = $(this).text(),
            link_href = $(this).attr('href'),
            option = $(document.createElement('option'));
        option.
            text(link_text).
            bind('click', function () {location.href = link_href}).
            appendTo(country_select);
    });
    $('#store_locator_change_country').html(country_select);
});

