﻿//SynPortal所使用Jquery插件的集合
/*
*This kit includes SimpleModal,PrettyCheckboxes,Jqzoom , flyout and crossSlide
*
/*
 * SimpleModal 1.4.1 - jQuery Plugin
 * http://www.ericmmartin.com/projects/simplemodal/
 * Copyright (c) 2010 Eric Martin (http://twitter.com/ericmmartin)
 * Dual licensed under the MIT and GPL licenses
 * Revision: $Id: jquery.simplemodal.js 261 2010-11-05 21:16:20Z emartin24 $
 */
(function (d) {
    var k = d.browser.msie && parseInt(d.browser.version) === 6 && typeof window.XMLHttpRequest !== "object", m = d.browser.msie && parseInt(d.browser.version) === 7, l = null, f = []; d.modal = function (a, b) { return d.modal.impl.init(a, b) }; d.modal.close = function () { d.modal.impl.close() }; d.modal.focus = function (a) { d.modal.impl.focus(a) }; d.modal.setContainerDimensions = function () { d.modal.impl.setContainerDimensions() }; d.modal.setPosition = function () { d.modal.impl.setPosition() }; d.modal.update = function (a, b) {
        d.modal.impl.update(a,
b)
    }; d.fn.modal = function (a) { return d.modal.impl.init(this, a) }; d.modal.defaults = { appendTo: "body", focus: true, opacity: 50, overlayId: "simplemodal-overlay", overlayCss: {}, containerId: "simplemodal-container", containerCss: {}, dataId: "simplemodal-data", dataCss: {}, minHeight: null, minWidth: null, maxHeight: null, maxWidth: null, autoResize: false, autoPosition: true, zIndex: 1E3, close: true, closeHTML: '<a class="modalCloseImg" title="Close"></a>', closeClass: "simplemodal-close", escClose: true, overlayClose: false, position: null,
        persist: false, modal: true, onOpen: null, onShow: null, onClose: null
    }; d.modal.impl = { d: {}, init: function (a, b) {
        var c = this; if (c.d.data) return false; l = d.browser.msie && !d.boxModel; c.o = d.extend({}, d.modal.defaults, b); c.zIndex = c.o.zIndex; c.occb = false; if (typeof a === "object") {
            a = a instanceof jQuery ? a : d(a); c.d.placeholder = false; if (a.parent().parent().size() > 0) {
                a.before(d("<span></span>").attr("id", "simplemodal-placeholder").css({ display: "none" })); c.d.placeholder = true; c.display = a.css("display"); if (!c.o.persist) c.d.orig =
a.clone(true)
            } 
        } else if (typeof a === "string" || typeof a === "number") a = d("<div></div>").html(a); else { alert("SimpleModal Error: Unsupported data type: " + typeof a); return c } c.create(a); c.open(); d.isFunction(c.o.onShow) && c.o.onShow.apply(c, [c.d]); return c
    }, create: function (a) {
        var b = this; f = b.getDimensions(); if (b.o.modal && k) b.d.iframe = d('<iframe src="javascript:false;"></iframe>').css(d.extend(b.o.iframeCss, { display: "none", opacity: 0, position: "fixed", height: f[0], width: f[1], zIndex: b.o.zIndex, top: 0, left: 0 })).appendTo(b.o.appendTo);
        b.d.overlay = d("<div></div>").attr("id", b.o.overlayId).addClass("simplemodal-overlay").css(d.extend(b.o.overlayCss, { display: "none", opacity: b.o.opacity / 100, height: b.o.modal ? f[0] : 0, width: b.o.modal ? f[1] : 0, position: "fixed", left: 0, top: 0, zIndex: b.o.zIndex + 1 })).appendTo(b.o.appendTo); b.d.container = d("<div></div>").attr("id", b.o.containerId).addClass("simplemodal-container").css(d.extend(b.o.containerCss, { display: "none", position: "fixed", zIndex: b.o.zIndex + 2 })).append(b.o.close && b.o.closeHTML ? d(b.o.closeHTML).addClass(b.o.closeClass) :
"").appendTo(b.o.appendTo); b.d.wrap = d("<div></div>").attr("tabIndex", -1).addClass("simplemodal-wrap").css({ height: "100%", outline: 0, width: "100%" }).appendTo(b.d.container); b.d.data = a.attr("id", a.attr("id") || b.o.dataId).addClass("simplemodal-data").css(d.extend(b.o.dataCss, { display: "none" })).appendTo("body"); b.setContainerDimensions(); b.d.data.appendTo(b.d.wrap); if (k || l) b.fixIE()
    }, bindEvents: function () {
        var a = this; d("." + a.o.closeClass).bind("click.simplemodal", function (b) { b.preventDefault(); a.close() });
        a.o.modal && a.o.close && a.o.overlayClose && a.d.overlay.bind("click.simplemodal", function (b) { b.preventDefault(); a.close() }); d(document).bind("keydown.simplemodal", function (b) { if (a.o.modal && b.keyCode === 9) a.watchTab(b); else if (a.o.close && a.o.escClose && b.keyCode === 27) { b.preventDefault(); a.close() } }); d(window).bind("resize.simplemodal", function () {
            f = a.getDimensions(); a.o.autoResize ? a.setContainerDimensions() : a.o.autoPosition && a.setPosition(); if (k || l) a.fixIE(); else if (a.o.modal) {
                a.d.iframe && a.d.iframe.css({ height: f[0],
                    width: f[1]
                }); a.d.overlay.css({ height: f[0], width: f[1] })
            } 
        })
    }, unbindEvents: function () { d("." + this.o.closeClass).unbind("click.simplemodal"); d(document).unbind("keydown.simplemodal"); d(window).unbind("resize.simplemodal"); this.d.overlay.unbind("click.simplemodal") }, fixIE: function () {
        var a = this, b = a.o.position; d.each([a.d.iframe || null, !a.o.modal ? null : a.d.overlay, a.d.container], function (c, h) {
            if (h) {
                var g = h[0].style; g.position = "absolute"; if (c < 2) {
                    g.removeExpression("height"); g.removeExpression("width"); g.setExpression("height",
'document.body.scrollHeight > document.body.clientHeight ? document.body.scrollHeight : document.body.clientHeight + "px"'); g.setExpression("width", 'document.body.scrollWidth > document.body.clientWidth ? document.body.scrollWidth : document.body.clientWidth + "px"')
                } else {
                    var e; if (b && b.constructor === Array) {
                        c = b[0] ? typeof b[0] === "number" ? b[0].toString() : b[0].replace(/px/, "") : h.css("top").replace(/px/, ""); c = c.indexOf("%") === -1 ? c + ' + (t = document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop) + "px"' :
parseInt(c.replace(/%/, "")) + ' * ((document.documentElement.clientHeight || document.body.clientHeight) / 100) + (t = document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop) + "px"'; if (b[1]) { e = typeof b[1] === "number" ? b[1].toString() : b[1].replace(/px/, ""); e = e.indexOf("%") === -1 ? e + ' + (t = document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft) + "px"' : parseInt(e.replace(/%/, "")) + ' * ((document.documentElement.clientWidth || document.body.clientWidth) / 100) + (t = document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft) + "px"' } 
                    } else {
                        c =
'(document.documentElement.clientHeight || document.body.clientHeight) / 2 - (this.offsetHeight / 2) + (t = document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop) + "px"'; e = '(document.documentElement.clientWidth || document.body.clientWidth) / 2 - (this.offsetWidth / 2) + (t = document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft) + "px"'
                    } g.removeExpression("top"); g.removeExpression("left"); g.setExpression("top",
c); g.setExpression("left", e)
                } 
            } 
        })
    }, focus: function (a) { var b = this; a = a && d.inArray(a, ["first", "last"]) !== -1 ? a : "first"; var c = d(":input:enabled:visible:" + a, b.d.wrap); setTimeout(function () { c.length > 0 ? c.focus() : b.d.wrap.focus() }, 10) }, getDimensions: function () { var a = d(window); return [d.browser.opera && d.browser.version > "9.5" && d.fn.jquery < "1.3" || d.browser.opera && d.browser.version < "9.5" && d.fn.jquery > "1.2.6" ? a[0].innerHeight : a.height(), a.width()] }, getVal: function (a, b) {
        return a ? typeof a === "number" ? a : a === "auto" ? 0 :
a.indexOf("%") > 0 ? parseInt(a.replace(/%/, "")) / 100 * (b === "h" ? f[0] : f[1]) : parseInt(a.replace(/px/, "")) : null
    }, update: function (a, b) { var c = this; if (!c.d.data) return false; c.d.origHeight = c.getVal(a, "h"); c.d.origWidth = c.getVal(b, "w"); c.d.data.hide(); a && c.d.container.css("height", a); b && c.d.container.css("width", b); c.setContainerDimensions(); c.d.data.show(); c.o.focus && c.focus(); c.unbindEvents(); c.bindEvents() }, setContainerDimensions: function () {
        var a = this, b = k || m, c = a.d.origHeight ? a.d.origHeight : d.browser.opera ?
a.d.container.height() : a.getVal(b ? a.d.container[0].currentStyle.height : a.d.container.css("height"), "h"); b = a.d.origWidth ? a.d.origWidth : d.browser.opera ? a.d.container.width() : a.getVal(b ? a.d.container[0].currentStyle.width : a.d.container.css("width"), "w"); var h = a.d.data.outerHeight(true), g = a.d.data.outerWidth(true); a.d.origHeight = a.d.origHeight || c; a.d.origWidth = a.d.origWidth || b; var e = a.o.maxHeight ? a.getVal(a.o.maxHeight, "h") : null, i = a.o.maxWidth ? a.getVal(a.o.maxWidth, "w") : null; e = e && e < f[0] ? e : f[0]; i = i && i <
f[1] ? i : f[1]; var j = a.o.minHeight ? a.getVal(a.o.minHeight, "h") : "auto"; c = c ? a.o.autoResize && c > e ? e : c < j ? j : c : h ? h > e ? e : a.o.minHeight && j !== "auto" && h < j ? j : h : j; e = a.o.minWidth ? a.getVal(a.o.minWidth, "w") : "auto"; b = b ? a.o.autoResize && b > i ? i : b < e ? e : b : g ? g > i ? i : a.o.minWidth && e !== "auto" && g < e ? e : g : e; a.d.container.css({ height: c, width: b }); a.d.wrap.css({ overflow: h > c || g > b ? "auto" : "visible" }); a.o.autoPosition && a.setPosition()
    }, setPosition: function () {
        var a = this, b, c; b = f[0] / 2 - a.d.container.outerHeight(true) / 2; c = f[1] / 2 - a.d.container.outerWidth(true) /
2; if (a.o.position && Object.prototype.toString.call(a.o.position) === "[object Array]") { b = a.o.position[0] || b; c = a.o.position[1] || c } else { b = b; c = c } a.d.container.css({ left: c, top: b })
    }, watchTab: function (a) {
        var b = this; if (d(a.target).parents(".simplemodal-container").length > 0) {
            b.inputs = d(":input:enabled:visible:first, :input:enabled:visible:last", b.d.data[0]); if (!a.shiftKey && a.target === b.inputs[b.inputs.length - 1] || a.shiftKey && a.target === b.inputs[0] || b.inputs.length === 0) {
                a.preventDefault(); b.focus(a.shiftKey ? "last" :
"first")
            } 
        } else { a.preventDefault(); b.focus() } 
    }, open: function () { var a = this; a.d.iframe && a.d.iframe.show(); if (d.isFunction(a.o.onOpen)) a.o.onOpen.apply(a, [a.d]); else { a.d.overlay.show(); a.d.container.show(); a.d.data.show() } a.o.focus && a.focus(); a.bindEvents() }, close: function () {
        var a = this; if (!a.d.data) return false; a.unbindEvents(); if (d.isFunction(a.o.onClose) && !a.occb) { a.occb = true; a.o.onClose.apply(a, [a.d]) } else {
            if (a.d.placeholder) {
                var b = d("#simplemodal-placeholder"); if (a.o.persist) b.replaceWith(a.d.data.removeClass("simplemodal-data").css("display",
a.display)); else { a.d.data.hide().remove(); b.replaceWith(a.d.orig) } 
            } else a.d.data.hide().remove(); a.d.container.hide().remove(); a.d.overlay.hide(); a.d.iframe && a.d.iframe.hide().remove(); setTimeout(function () { a.d.overlay.remove(); a.d = {} }, 10)
        } 
    } 
    }
})(jQuery);
/*
Accordion - Super simple javascript accordion jQuery plugin
http://www.unwrongest.com/projects/accordion/
*/
(function($) {
    $.fn.extend({
        accordion: function() {
            return this.each(function() {
                if ($(this).data('accordiated'))
                    return false;
                $.each($(this).find('ul, li>div'), function() {
                    $(this).data('accordiated', true);
                    $(this).hide();
                });
                $.each($(this).find('a:not(.foo)'), function() {
                    $(this).click(function(e) {
                        activate(e.target);
                        return void (0);
                    });
                });

                var active = false;
                if (location.hash)
                    active = $(this).find('a[href=' + location.hash + ']')[0];
                else if ($(this).find('li.current'))
                    active = $(this).find('li.current a')[0];

                if (active) {
                    activate(active, 'toggle', 'parents');
                    $(active).parents().show();
                }

                function activate(el, effect, parents) {
                    $(el)[(parents || 'parent')]('li').toggleClass('active').siblings().removeClass('active').children('ul, div').slideUp('fast');
                    $(el).siblings('ul, div')[(effect || 'slideToggle')]((!effect) ? 'fast' : null);
                }

            });
        }
    });
})(jQuery);

/* ------------------------------------------------------------------------
prettyCheckboxes
	
Developped By: Stephane Caron (http://www.no-margin-for-errors.com)
Inspired By: All the non user friendly custom checkboxes solutions ;)
Version: 1.0.1
	
Copyright: Feel free to redistribute the script/modify it, as
long as you leave my infos at the top.
------------------------------------------------------------------------- */

jQuery.fn.prettyCheckboxes = function(settings) {
    settings = jQuery.extend({
        checkboxWidth: 17,
        checkboxHeight: 17,
        className: 'prettyCheckbox',
        display: 'list'
    }, settings);

    $(this).each(function() {
        // Find the label
        $label = $('label[for="' + $(this).attr('id') + '"]');

        // Add the checkbox holder to the label
        $label.prepend("<span class='holderWrap'><span class='holder'></span></span>");

        // If the checkbox is checked, display it as checked
        if ($(this).is(':checked')) { $label.addClass('checked'); };

        // Assign the class on the label
        $label.addClass(settings.className).addClass($(this).attr('type')).addClass(settings.display);

        // Assign the dimensions to the checkbox display
        $label.find('span.holderWrap').width(settings.checkboxWidth).height(settings.checkboxHeight);
        $label.find('span.holder').width(settings.checkboxWidth);

        // Hide the checkbox
        $(this).addClass('hiddenCheckbox');

        // Associate the click event
        $label.bind('click', function() {
            $('input#' + $(this).attr('for')).triggerHandler('click');

            if ($('input#' + $(this).attr('for')).is(':checkbox')) {
                $(this).toggleClass('checked');
                $('input#' + $(this).attr('for')).checked = true;
            } else {
                $toCheck = $('input#' + $(this).attr('for'));

                // Uncheck all radio
                $('input[name="' + $toCheck.attr('name') + '"]').each(function() {
                    $('label[for="' + $(this).attr('id') + '"]').removeClass('checked');
                });

                $(this).addClass('checked');
                $toCheck.checked = true;
            };
        });

        $('input#' + $label.attr('for')).bind('keypress', function(e) {
            if (e.keyCode == 32) {
                if ($.browser.msie) {
                    $('label[for="' + $(this).attr('id') + '"]').toggleClass("checked");
                } else {
                    $(this).trigger('click');
                }
                return false;
            };
        });
    });
};

checkAllPrettyCheckboxes = function(caller, container) {
    if ($(caller).is(':checked')) {
        // Find the label corresponding to each checkbox and click it
        $(container).find('input[type=checkbox]:not(:checked)').each(function() {
            $('label[for="' + $(this).attr('id') + '"]').trigger('click');
            if ($.browser.msie) {
                $(this).attr('checked', 'checked');
            } else {
                $(this).trigger('click');
            };
        });
    } else {
        $(container).find('input[type=checkbox]:checked').each(function() {
            $('label[for="' + $(this).attr('id') + '"]').trigger('click');
            if ($.browser.msie) {
                $(this).attr('checked', '');
            } else {
                $(this).trigger('click');
            };
        });
    };
};


/*
* 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: 'show',
            hideEffect: 'hide',
            fadeinSpeed: 'fast',
            fadeoutSpeed: 'slow',
            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);
            var smallimagedata = {};
            //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 : "Zoom";  //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];

                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:
                            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() {
                    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
                    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() {

                var leftpos = smallimagedata.pos.l;
                var toppos = smallimagedata.pos.t;
                //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'
				});

                //fa il positionamento
                switch (settings.position) {
                    case "right":

                        leftpos = (smallimagedata.pos.r + Math.abs(settings.xOffset) + settings.zoomWidth < screen.width)
				? (smallimagedata.pos.l + smallimagedata.w + Math.abs(settings.xOffset))
				: (smallimagedata.pos.l - settings.zoomWidth - Math.abs(settings.xOffset));

                        topwindow = smallimagedata.pos.t + settings.yOffset + settings.zoomHeight;
                        toppos = (topwindow < screen.height && topwindow > 0)
				? smallimagedata.pos.t + settings.yOffset
				: smallimagedata.pos.t;

                        break;
                    case "left":

                        leftpos = (smallimagedata.pos.l - Math.abs(settings.xOffset) - settings.zoomWidth > 0)
				? (smallimagedata.pos.l - Math.abs(settings.xOffset) - settings.zoomWidth)
				: (smallimagedata.pos.l + smallimagedata.w + Math.abs(settings.xOffset));

                        topwindow = smallimagedata.pos.t + settings.yOffset + settings.zoomHeight;
                        toppos = (topwindow < screen.height && topwindow > 0)
				? smallimagedata.pos.t + settings.yOffset
				: smallimagedata.pos.t;

                        break;
                    case "top":

                        toppos = (smallimagedata.pos.t - Math.abs(settings.yOffset) - settings.zoomHeight > 0)
				? (smallimagedata.pos.t - Math.abs(settings.yOffset) - settings.zoomHeight)
				: (smallimagedata.pos.t + smallimagedata.h + Math.abs(settings.yOffset));


                        leftwindow = smallimagedata.pos.l + settings.xOffset + settings.zoomWidth;
                        leftpos = (leftwindow < screen.width && leftwindow > 0)
				? smallimagedata.pos.l + settings.xOffset
				: smallimagedata.pos.l;

                        break;
                    case "bottom":


                        toppos = (smallimagedata.pos.b + Math.abs(settings.yOffset) + settings.zoomHeight < $('body').height())
				? (smallimagedata.pos.b + Math.abs(settings.yOffset))
				: (smallimagedata.pos.t - settings.zoomHeight - Math.abs(settings.yOffset));


                        leftwindow = smallimagedata.pos.l + settings.xOffset + settings.zoomWidth;
                        leftpos = (leftwindow < screen.width && leftwindow > 0)
				? smallimagedata.pos.l + settings.xOffset
				: smallimagedata.pos.l;

                        break;
                    default:

                        leftpos = (smallimagedata.pos.l + smallimagedata.w + settings.xOffset + settings.zoomWidth < screen.width)
				? (smallimagedata.pos.l + smallimagedata.w + Math.abs(settings.xOffset))
				: (smallimagedata.pos.l - settings.zoomWidth - Math.abs(settings.xOffset));

                        toppos = (smallimagedata.pos.b + Math.abs(settings.yOffset) + settings.zoomHeight < screen.height)
				? (smallimagedata.pos.b + Math.abs(settings.yOffset))
				: (smallimagedata.pos.t - settings.zoomHeight - Math.abs(settings.yOffset));

                        break;
                }

                this.node.style.left = leftpos + 'px';
                this.node.style.top = toppos + 'px';
                return this;
            }


            Stage.prototype.activate = function() {

                if (!this.node.firstChild)
                    this.node.appendChild(largeimage.node);


                if (settings.title) {
                    ZoomTitleObj.loadtitle();
                }



                document.body.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;
                }
            }

            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

                $(this.node)
				.appendTo("body")
				.css('visibility', 'hidden');



                this.show = function() {
                    switch (settings.preloadPosition) {
                        case 'center':
                            loadertop = smallimagedata.pos.t + (smallimagedata.h - $(this.node).height()) / 2;
                            loaderleft = smallimagedata.pos.l + (smallimagedata.w - $(this.node).width()) / 2;
                            break;
                        default:
                            var loaderoffset = this.getoffset();
                            loadertop = !isNaN(loaderoffset.top) ? smallimagedata.pos.t + loaderoffset.top : smallimagedata.pos.t + 0;
                            loaderleft = !isNaN(loaderoffset.left) ? smallimagedata.pos.l + loaderoffset.left : smallimagedata.pos.l + 0;
                            break;
                    }

                    //setting position
                    $(this.node).css({
                        top: loadertop,
                        left: loaderleft,
                        position: 'absolute',
                        visibility: 'visible'
                    });
                }
                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;
}


/*
* JQuery FlyOut
* author: Jolyon Terwilliger - Nixbox Web Consulting
* website: http://nixbox.com/projects/jquery-image-flyout/
*
* Dual licensed under the MIT and GPL licenses:
*   http://www.opensource.org/licenses/mit-license.php
*   http://www.gnu.org/licenses/gpl.html
*
* version 0.21 (July 21, 2008)
* version 0.22 (July 22, 2008) 
notes: minor reordering to loadingSrc logic.
* version 0.23 (August 15, 2008)
added: config options for loadingText and closeTip to facilitate locale.
Thanks Tony for the nudge.
* version 0.24 (Oct 2, 2008) 
added: customize start location and size of flyout, if different 
from thumb link. Thanks to Jake Kronika for this patch.
* version 1.0 (Oct 11, 2008) 
added: support for final flyout location via destElement and destPadding: 
define a fixed container anywhere in the document and the pic will 
fly to that location, regardless of viewport position. 
fixed: clicking on open source link no longer reopens same image. 
added: 4 callbacks for start and finish of flyOut and putAway animations. 
fixed: putAway function to put back to correct location, in case 
thumb has moved (page or div scroll, etc)
* version 1.1 (Nov 16, 2008)
fixed: Opera 9.5+ doesn't report window.height() correctly - patched with code
from jquery Bug 3117:  http://dev.jquery.com/ticket/3117
note: once this is patched in jQuery core, or fixed in Opera
this may eventually be removed.
added: when flyOut image is completed, a customizable class 
(default to 'shown') is appended to the thumb image container
so an external event can trigger the click to close any open
elements. See demo page for example.
* version 1.2 (Feb 13, 2011)
added: inOpacity, outOpacity, loaderOpacity and fullSizeImage
fixed: fixed start and animation positioning if loader has border
fixed blank space at bottom of image by adding line-height:0px to flyout
added full closure to plugin to allow cross-library compatitibility
ran through jsLint and fixed various issues
removed Opera hack, problem was fixed in version 10 of Opera
*/

/**
* @author Jolyon Terwilliger (jolyon@nixbox.com)
*
* JQuery.flyout() method provides an alternate means of loading and displaying linked
* images using JQuery animation and easing.
*
* flyout() takes a single object argument:  $('.thumbs a').flyout({param:setting, etc..})
*
* Settings:
*
* @param {integer} outSpeed Speed in milliseconds for the flyout animation - default: 1000
*
* @param {integer} inSpeed Speed in milliseconds for the flyback animation - default: 500
*
* @param {string} outEase The easing method to use for the flyout animation - default: swing
*
* @param {string} inEase The easing method to use for the flyback animation - default: swing
*			
* @param {string} loadingSrc The image file to use while an image is being loaded prior to flyout
*						default: none
*						
* @param {string} loader The ID for the created flyout div that contains the sub-content
*						this is currently only useful for multiple skinnings via CSS
*						default: 'loader'
*
* @param {integer} loaderZIndex The CSS z-index for the flyout
*						default: 500
*
* @param {float} loaderOpacity Set the starting opacity for the loader
*						default: 0.5 (50% opacity)
*
* @param {integer} widthMargin The left and right margin space for the final flyout
*						this value is effectively divided between the left and right margin
*						default: 40
*			
* @param {integer} heightMargin The top and bottom margin space for the final flyout
*						this value is effectively divided between the top and bottom margin
*						default: 40
*
* @param {string} loadingText Text shown when image is loading
*
* @param {string} closeTip Tip text for image alt/title tags
*
* @param {string} destElement The destination container - overrides height and widthMargins
*						specified in CSS notation - e.g. "#picContainer"
*						also overrides fullSizeImage option
*						default: none
*
* @param {integer} destPadding Number of pixels to pad when flying out to destElement
*						default: 10
*
* @param {integer} startOffsetX Horizontal offset added to thumb left value for start of flyout animation
*						Hint: can be negative.
*						default: 0
*
* @param {integer} startOffsetY Vertical offset added to thumb top value for start of flyout animation.
*						default: 0
*
* @param {integer} startHeight Overrides starting height of flyout animation.
*						default: 0  (uses thumb image height by default)
*
* @param {integer} startWidth Overrides starting width of flyout animation.
*						default: 0  (uses thumb image width by default)
*
* @param {string} flyOutStart Function to run at start of flyout animation.
*						default: none
*
* @param {string} flyOutFinish Function to run at finish of flyout animation.
*						default: none
*
* @param {string} putAwayStart Function to run at start of putaway animation.
*						default: none
*
* @param {string} putAwayFinish Function to run at finish of putaway animation.
*						default: none
*
* @param {float} inOpacity Opacity for image flyout at start of flyout animation.
*						default: 1.0
*
* @param {float} outOpacity Opacity for image flyout at end of flyout animation.
*						default: 1.0
*
* @param {boolean} fullSizeImage Enlarges image to full size, even if it extends beyond
*						boundary of the window when true. Overridden by destElement option.
*						default: false
*
* Example: $('.thumbs a').flyout();
* standard flyouts applied to all a > img elements with the 'thumbs' class. 
* 
* Example: $('.thumbs a').flyout({loadingSrc:'images/thumb-loading.gif',
*								outEase:'easeOutCirc',
*								inEase:'easeOutBounce'});
* Creates flyouts  with different ease in and ease out and a loading animation image is specified
*
* For more details see: http://nixbox.com/projects/jquery-image-flyout/
*/

//console.log('Image Flyout');
(function ($) {
    jQuery.fn.extend({ flyout: function (options) {

        var shown = false;
        var animating = false;
        var bigimg = new Image();
        var subType = 'img';
        var $holder, $thumb, $loader, tloc, th, tw, adjustTop, adjustLeft;

        this.click(function () {
            if (animating === true) { return false; }

            if (shown) { putAway(this); }
            else { flyOut(this); }

            return false;
        });

        var o = jQuery.extend({
            outSpeed: 1000,
            inSpeed: 500,
            outEase: 'swing',
            inEase: 'swing',
            loadingSrc: null,
            loader: 'loader',
            loaderZIndex: 500,
            loaderOpacity: 0.5,
            widthMargin: 40,
            heightMargin: 40,
            loadingText: "Loading...",
            closeTip: " - Click here to close",
            destPadding: 20,
            startOffsetX: 0,
            startOffsetY: 0,
            startHeight: 0,
            startWidth: 0,
            flyOutStart: function () { },
            flyOutFinish: function () { },
            putAwayStart: function () { },
            putAwayFinish: function () { },
            shownClass: 'shown',
            inOpacity: 1.0,
            outOpacity: 1.0,
            fullSizeImage: false
        }, options);

        // parseInt for easy mathing
        function getInt(arg) {
            var myint = parseInt(arg, 10);
            return (isNaN(myint) ? 0 : myint);
        }

        function flyOut(it) {
            animating = true;

            $holder = $(it);
            $thumb = $('img', it);
            bigimg = new Image();
            var sL = $(window).scrollLeft();
            var sT = $(window).scrollTop();
            tloc = $thumb.offset();
            tloc.left += o.startOffsetX;
            tloc.top += o.startOffsetY;
            th = (o.startHeight > 0 ? o.startHeight : $thumb.height());
            tw = (o.startWidth > 0 ? o.startWidth : $thumb.width());

            $loader = $('<div></div>').attr('id', o.loader)
							.appendTo('body')
							.css({ 'position': 'absolute',
							    'top': -9999,
							    'left': -9999,
							    'height': th,
							    'width': tw,
							    'opacity': o.loaderOpacity,
							    'display': 'block',
							    'z-index': o.loaderZIndex,
							    'line-height': '0px'
							});

            adjustTop = getInt($loader.css('borderTopWidth')) + getInt($loader.css('paddingTop')) - getInt($thumb.css('borderTopWidth')) - getInt($thumb.css('paddingTop'));
            adjustLeft = getInt($loader.css('borderLeftWidth')) + getInt($loader.css('paddingLeft')) - getInt($thumb.css('borderLeftWidth')) - getInt($thumb.css('paddingLeft'));

            $loader.css({ 'top': tloc.top - adjustTop, 'left': tloc.left - adjustLeft });

            if (o.loadingSrc) {
                $('#' + o.loader).append($('<img/>')
								.load(function () {
								    $(this)
											.css({ 'position': 'relative',
											    'top': th / 2 - (this.height / 2),
											    'left': tw / 2 - (this.width / 2)
											})
											.attr('alt', o.loadingText);
								})
									.attr('src', o.loadingSrc)
								);
            }
            else {
                $('#' + o.loader).css('background-color', '#000')
								.append($('<span></span>')
										  	.text(o.loadingText)
											.css({ 'position': 'relative',
											    'top': '2px',
											    'left': '2px',
											    'color': '#FFF',
											    'font-size': '9px'
											})
									 	);
            }

            $(bigimg).load(function () {
                var imgtag = $('<img/>').attr('src', $holder.attr('href')).attr('title', $thumb.attr('title') + o.closeTip).attr('alt', $thumb.attr('alt') + o.closeTip).height(th).width(tw);

                o.flyOutStart.call(it);

                var max_x, max_y, $dest;

                if (o.destElement) {
                    $dest = $(o.destElement);
                    max_x = $dest.innerWidth() - (o.destPadding * 2);
                    max_y = $dest.innerHeight() - (o.destPadding * 2);
                }
                else {
                    max_x = $(window).width() - o.widthMargin;
                    max_y = $(window).height() - o.heightMargin;
                    /*					if ($.browser.opera) {
                    var wh = document.getElementsByTagName('html')[0].clientHeight;
                    }
                    else {
                    var wh = $(window).height();
                    }
                    max_y = wh-o.heightMargin;
                    */
                }

                var width = bigimg.width, height = bigimg.height;

                var x_dim = max_x / width, y_dim = max_y / height;

                if (x_dim <= y_dim) { y_dim = x_dim; }
                else { x_dim = y_dim; }

                var dl, dt, dw, dh;

                if (o.fullSizeImage) {
                    dw = width;
                    dh = height;
                }
                else {
                    dw = Math.round(width * x_dim), dh = Math.round(height * y_dim);
                    if (dw > width) { dw = width; }
                    if (dh > height) { dh = height; }
                }

                if (o.destElement) {
                    var dPos = $dest.offset();
                    dl = Math.round(($dest.outerWidth() / 2) - (dw / 2) + dPos.left);
                    dt = Math.round(($dest.outerHeight() / 2) - (dh / 2) + dPos.top);
                }
                else {
                    dl = Math.round(($(window).width() / 2) - (dw / 2) + sL);
                    dt = Math.round(($(window).height() / 2) - (dh / 2) + sT);
                    /*					if ($.browser.opera) 
                    var wh = document.getElementsByTagName('html')[0].clientHeight;
                    else 
                    var wh = $(window).height();
                    dt = Math.round((wh/2)-(dh/2)+sT);
                    */
                }

                $('#' + o.loader).empty().css({ 'opacity': o.inOpacity, 'top': tloc.top - adjustTop, 'left': tloc.left - adjustLeft }).append(imgtag).width('auto').height('auto').animate({ top: dt, left: dl, opacity: o.outOpacity }, { duration: o.outSpeed, queue: false, easing: o.outEase });
                $('#' + o.loader + ' ' + subType).animate({ height: dh, width: dw, opacity: o.outOpacity }, o.outSpeed, o.outEase,
				function () {
				    o.flyOutFinish.call(it);
				    shown = it;
				    $holder.addClass(o.shownClass);
				    animating = false;
				    $('#' + o.loader + ' ' + subType).click(function () { putAway(null); });
				});
            });
            bigimg.src = $holder.attr('href');
        }


        function putAway(next) {
            if (animating === true || shown === false) { return false; }
            o.putAwayStart.call(shown);

            animating = true;

            // check $thumb loc again, in case it moved...
            tloc = $thumb.offset();
            tloc.left += o.startOffsetX - adjustLeft;
            tloc.top += o.startOffsetY - adjustTop;

            $('#' + o.loader).animate({ top: tloc.top, left: tloc.left, opacity: o.inOpacity }, { duration: o.inSpeed, queue: false, easing: o.inEase });
            $('#' + o.loader + ' ' + subType).animate({ height: th, width: tw, opacity: o.inOpacity },
				o.inSpeed, o.inEase,
				function () {
				    $('#' + o.loader).css('display', 'none').remove();
				    o.putAwayFinish.call(shown);
				    animating = false;
				    bigimg = null;
				    if (next && next !== shown) {
				        shown = false;
				        flyOut(next);
				    }
				    shown = false;
				    $holder.removeClass(o.shownClass);
				});
        }

        return this; // never break the chain

    }
    });

})(jQuery);

/*
 * Copyright 2007-2009 by Tobia Conforto <tobia.conforto@gmail.com>
 *
 * This program is free software; you can redistribute it and/or modify it under the terms of the GNU General
 * Public License as published by the Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the
 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
 * for more details.
 *
 * You should have received a copy of the GNU General Public License along with this program; if not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * Versions: 0.1    2007-08-19  Initial release
 *                  2008-08-21  Re-released under GPL v2
 *           0.1.1  2008-09-18  Compatibility with prototype.js
 *           0.2    2008-10-15  Linkable images, contributed by Tim Rainey <tim@zmlabs.com>
 *           0.3    2008-10-22  Added option to repeat the animation a number of times, then stop
 *           0.3.1  2008-11-11  Better error messages
 *           0.3.2  2008-11-11  Fixed a couple of CSS bugs, contributed by Erwin Bot <info@ixgcms.nl>
 *           0.3.3  2008-12-14  Added onclick option
 *           0.3.4  2009-03-12  Added shuffle option, contributed by Ralf Santbergen <ralf_santbergen@hotmail.com>
 *           0.3.5  2009-03-12  Fixed usage of href parameter in 'Ken Burns' mode
 */

jQuery.fn.crossSlide = function(opts, plan) {
	var self = this,
			self_width = this.width(),
			self_height = this.height();

	// generic utilities
	function format(str) {
		for (var i = 1; i < arguments.length; i++)
			str = str.replace(new RegExp('\\{' + (i-1) + '}', 'g'), arguments[i]);
		return str;
	}

	function abort() {
		arguments[0] = 'crossSlide: ' + arguments[0];
		throw format.apply(null, arguments);
	}

	// first preload all the images, while getting their actual width and height
	(function(proceed) {

		var n_loaded = 0;
		function loop(i, img) {
			// for (i = 0; i < plan.length; i++) but with independent var i, img (for the closures)
			img.onload = function(e) {
				n_loaded++;
				plan[i].width = img.width;
				plan[i].height = img.height;
				if (n_loaded == plan.length)
					proceed();
			}
			img.src = plan[i].src;
			if (i + 1 < plan.length)
				loop(i + 1, new Image());
		}
		loop(0, new Image());

	})(function() {  // then proceed

		// utility to parse "from" and "to" parameters
		function parse_position_param(param) {
			var zoom = 1;
			var tokens = param.replace(/^\s*|\s*$/g, '').split(/\s+/);
			if (tokens.length > 3) throw new Error();
			if (tokens[0] == 'center')
				if (tokens.length == 1)
					tokens = ['center', 'center'];
				else if (tokens.length == 2 && tokens[1].match(/^[\d.]+x$/i))
					tokens = ['center', 'center', tokens[1]];
			if (tokens.length == 3)
				zoom = parseFloat(tokens[2].match(/^([\d.]+)x$/i)[1]);
			var pos = tokens[0] + ' ' + tokens[1];
			if (pos == 'left top'      || pos == 'top left')      return { xrel:  0, yrel:  0, zoom: zoom };
			if (pos == 'left center'   || pos == 'center left')   return { xrel:  0, yrel: .5, zoom: zoom };
			if (pos == 'left bottom'   || pos == 'bottom left')   return { xrel:  0, yrel:  1, zoom: zoom };
			if (pos == 'center top'    || pos == 'top center')    return { xrel: .5, yrel:  0, zoom: zoom };
			if (pos == 'center center')                           return { xrel: .5, yrel: .5, zoom: zoom };
			if (pos == 'center bottom' || pos == 'bottom center') return { xrel: .5, yrel:  1, zoom: zoom };
			if (pos == 'right top'     || pos == 'top right')     return { xrel:  1, yrel:  0, zoom: zoom };
			if (pos == 'right center'  || pos == 'center right')  return { xrel:  1, yrel: .5, zoom: zoom };
			if (pos == 'right bottom'  || pos == 'bottom right')  return { xrel:  1, yrel:  1, zoom: zoom };
			return {
				xrel: parseInt(tokens[0].match(/^(\d+)%$/)[1]) / 100,
				yrel: parseInt(tokens[1].match(/^(\d+)%$/)[1]) / 100,
				zoom: zoom
			};
		}

		// utility to compute the css for a given phase between p.from and p.to
		// phase = 1: begin fade-in,  2: end fade-in,  3: begin fade-out,  4: end fade-out
		function position_to_css(p, phase) {
			switch (phase) {
				case 1:
					var pos = 0;
					break;
				case 2:
					var pos = fade_ms / (p.time_ms + 2 * fade_ms);
					break;
				case 3:
					var pos = 1 - fade_ms / (p.time_ms + 2 * fade_ms);
					break;
				case 4:
					var pos = 1;
					break;
			}
			return {
				left:   Math.round(p.from.left   + pos * (p.to.left   - p.from.left  )),
				top:    Math.round(p.from.top    + pos * (p.to.top    - p.from.top   )),
				width:  Math.round(p.from.width  + pos * (p.to.width  - p.from.width )),
				height: Math.round(p.from.height + pos * (p.to.height - p.from.height))
			};
		}

		// check global params
		if (! opts.fade)
			abort('missing fade parameter.');
		if (opts.speed && opts.sleep)
			abort('you cannot set both speed and sleep at the same time.');
		// conversion from sec to ms; from px/sec to px/ms
		var fade_ms = Math.round(opts.fade * 1000);
		if (opts.sleep)
			var sleep = Math.round(opts.sleep * 1000);
		if (opts.speed)
			var speed = opts.speed / 1000,
					fade_px = Math.round(fade_ms * speed);

		// set container css
		self.empty().css({
			overflow: 'hidden',
			padding: 0
		});
		if (! self.css('position').match(/absolute|relative|fixed/))
			self.css({ position: 'relative' });
		if (! self.width() || ! self.height())
			abort('container element does not have its own width and height');

		// random sorting
		if (opts.shuffle)
			plan.sort(function() {
				return Math.random() - 0.5;
			});

		// prepare each image
		for (var i = 0; i < plan.length; ++i) {

			var p = plan[i];
			if (! p.src)
				abort('missing src parameter in picture {0}.', i + 1);

			if (speed) { // speed/dir mode

				// check parameters and translate speed/dir mode into full mode (from/to/time)
				switch (p.dir) {
					case 'up':
						p.from = { xrel: .5, yrel: 0, zoom: 1 };
						p.to   = { xrel: .5, yrel: 1, zoom: 1 };
						var slide_px = p.height - self_height - 2 * fade_px;
						break;
					case 'down':
						p.from = { xrel: .5, yrel: 1, zoom: 1 };
						p.to   = { xrel: .5, yrel: 0, zoom: 1 };
						var slide_px = p.height - self_height - 2 * fade_px;
						break;
					case 'left':
						p.from = { xrel: 0, yrel: .5, zoom: 1 };
						p.to   = { xrel: 1, yrel: .5, zoom: 1 };
						var slide_px = p.width - self_width - 2 * fade_px;
						break;
					case 'right':
						p.from = { xrel: 1, yrel: .5, zoom: 1 };
						p.to   = { xrel: 0, yrel: .5, zoom: 1 };
						var slide_px = p.width - self_width - 2 * fade_px;
						break;
					default:
						abort('missing or malformed "dir" parameter in picture {0}.', i + 1);
				}
				if (slide_px <= 0)
					abort('picture number {0} is too short for the desired fade duration.', i + 1);
				p.time_ms = Math.round(slide_px / speed);

			} else if (! sleep) { // full mode

				// check and parse parameters
				if (! p.from || ! p.to || ! p.time)
					abort('missing either speed/sleep option, or from/to/time params in picture {0}.', i + 1);
				try {
					p.from = parse_position_param(p.from)
				} catch (e) {
					abort('malformed "from" parameter in picture {0}.', i + 1);
				}
				try {
					p.to = parse_position_param(p.to)
				} catch (e) {
					abort('malformed "to" parameter in picture {0}.', i + 1);
				}
				if (! p.time)
					abort('missing "time" parameter in picture {0}.', i + 1);
				p.time_ms = Math.round(p.time * 1000)
			}

			// precalculate left/top/width/height bounding values
			if (p.from)
				jQuery.each([ p.from, p.to ], function(i, from_to) {
					from_to.width  = Math.round(p.width  * from_to.zoom);
					from_to.height = Math.round(p.height * from_to.zoom);
					from_to.left   = Math.round((self_width  - from_to.width)  * from_to.xrel);
					from_to.top    = Math.round((self_height - from_to.height) * from_to.yrel);
				});

			// append the image (or anchor) element to the container
			var elm;
			if (p.href)
				elm = jQuery(format('<a href="{0}"><img src="{1}"/></a>', p.href, p.src));
			else
				elm = jQuery(format('<img src="{0}"/>', p.src));
			if (p.onclick)
				elm.click(p.onclick);
			elm.appendTo(self);
		}
		speed = undefined;  // speed mode has now been translated to full mode

		// find images to animate and set initial css attributes
		var imgs = self.find('img').css({
			position: 'absolute',
			visibility: 'hidden',
			top: 0,
			left: 0,
			border: 0
		});

		// show first image
		imgs.eq(0).css({ visibility: 'visible' });
		if (! sleep)
			imgs.eq(0).css(position_to_css(plan[0], 2));

		// create animation chain
		var countdown = opts.loop;
		function create_chain(i, chainf) {
			// building the chain backwards, or inside out

			if (i % 2 == 0) {
				if (sleep) {

					// still image sleep

					var i_sleep = i / 2,
							i_hide = (i_sleep - 1 + plan.length) % plan.length,
							img_sleep = imgs.eq(i_sleep),
							img_hide = imgs.eq(i_hide);

					var newf = function() {
						img_hide.css('visibility', 'hidden');
						setTimeout(chainf, sleep);
					};

				} else {

					// single image slide

					var i_slide = i / 2,
							i_hide = (i_slide - 1 + plan.length) % plan.length,
							img_slide = imgs.eq(i_slide),
							img_hide = imgs.eq(i_hide),
							time = plan[i_slide].time_ms,
							slide_anim = position_to_css(plan[i_slide], 3);

					var newf = function() {
						img_hide.css('visibility', 'hidden');
						img_slide.animate(slide_anim, time, 'linear', chainf);
					};

				}
			} else {
				if (sleep) {

					// still image cross-fade

					var i_from = Math.floor(i / 2),
							i_to = Math.ceil(i / 2) % plan.length,
							img_from = imgs.eq(i_from),
							img_to = imgs.eq(i_to),
							from_anim = {},
							to_init = { visibility: 'visible' },
							to_anim = {};

					if (i_to > i_from) {
						to_init.opacity = 0;
						to_anim.opacity = 1;
					} else {
						from_anim.opacity = 0;
					}

					var newf = function() {
						img_to.css(to_init);
						if (from_anim.opacity != undefined)
							img_from.animate(from_anim, fade_ms, 'linear', chainf);
						else
							img_to.animate(to_anim, fade_ms, 'linear', chainf);
					};

				} else {

					// cross-slide + cross-fade

					var i_from = Math.floor(i / 2),
							i_to = Math.ceil(i / 2) % plan.length,
							img_from = imgs.eq(i_from),
							img_to = imgs.eq(i_to),
							from_anim = position_to_css(plan[i_from], 4),
							to_init = position_to_css(plan[i_to], 1),
							to_anim = position_to_css(plan[i_to], 2);

					if (i_to > i_from) {
						to_init.opacity = 0;
						to_anim.opacity = 1;
					} else {
						from_anim.opacity = 0;
					}
					to_init.visibility = 'visible';

					var newf = function() {
						img_from.animate(from_anim, fade_ms, 'linear');
						img_to.css(to_init);
						img_to.animate(to_anim, fade_ms, 'linear', chainf);
					};

				}
			}

			// if the loop option was requested, push a countdown check
			if (opts.loop && i == plan.length * 2 - 2) {
				var newf_orig = newf;
				newf = function() {
					if (--countdown) newf_orig();
				}
			}

			if (i > 0)
				return create_chain(i - 1, newf);
			else
				return newf;
		}
		var animation = create_chain(plan.length * 2 - 1, function() { return animation(); });

		// start animation
		animation();

	});

	return self;
};

