/*
 *  Copyright 2010 FSB Technology (UK) Ltd ("FSB")
 *
 *  The contents of this file are the property of FSB. You may not use the
 *  contents of this file without the express permission of FSB.
 *
 *
 */


(function($) {

    // Wait Plugin for jQuery
    // http://www.inet411.com
    // based on the Delay and Pause Plugin

    $.fn.wait = function(option, options) {
        milli = 1000;
        if (option && (typeof option == 'function' || isNaN(option)) ) {
            options = option;
        } else if (option) {
            milli = option;
        }
        // set defaults
        var defaults = {
            msec: milli,
            onEnd: options
        },
        settings = $.extend({},defaults, options);

        if(typeof settings.onEnd == 'function') {
            this.each(function() {
                setTimeout(settings.onEnd, settings.msec);
            });
            return this;
        } else {
            return this.queue('fx',
                function() {
                    var self = this;
                    setTimeout(function() {
                        $.dequeue(self);
                    },settings.msec);
                });
        }
    }

    /**
    * .animate() extension plugin
    * @author Alexander Farkas
    * v. 1.21
    */

    if(!document.defaultView || !document.defaultView.getComputedStyle){ // IE6-IE8
        var oldCurCSS = jQuery.curCSS;
        jQuery.curCSS = function(elem, name, force){
            if(name === 'background-position'){
                name = 'backgroundPosition';
            }
            if(name !== 'backgroundPosition' || !elem.currentStyle || elem.currentStyle[ name ]){
                return oldCurCSS.apply(this, arguments);
            }
            var style = elem.style;
            if ( !force && style && style[ name ] ){
                return style[ name ];
            }
            return oldCurCSS(elem, 'backgroundPositionX', force) +' '+ oldCurCSS(elem, 'backgroundPositionY', force);
        };
    }

    var oldAnim = $.fn.animate;
    $.fn.animate = function(prop){
        if('background-position' in prop){
            prop.backgroundPosition = prop['background-position'];
            delete prop['background-position'];
        }
        if('backgroundPosition' in prop){
            prop.backgroundPosition = '('+ prop.backgroundPosition;
        }
        return oldAnim.apply(this, arguments);
    };

    function toArray(strg){
        strg = strg.replace(/left|top/g,'0px');
        strg = strg.replace(/right|bottom/g,'100%');
        strg = strg.replace(/([0-9\.]+)(\s|\)|$)/g,"$1px$2");
        var res = strg.match(/(-?[0-9\.]+)(px|\%|em|pt)\s(-?[0-9\.]+)(px|\%|em|pt)/);
        return [parseFloat(res[1],10),res[2],parseFloat(res[3],10),res[4]];
    }

    $.fx.step. backgroundPosition = function(fx) {
        if (!fx.bgPosReady) {
            var start = $.curCSS(fx.elem,'backgroundPosition');

            if(!start){//FF2 no inline-style fallback
                start = '0px 0px';
            }

            start = toArray(start);

            fx.start = [start[0],start[2]];

            var end = toArray(fx.options.curAnim.backgroundPosition);
            fx.end = [end[0],end[2]];

            fx.unit = [end[1],end[3]];
            fx.bgPosReady = true;
        }
        //return;
        var nowPosX = [];
        nowPosX[0] = ((fx.end[0] - fx.start[0]) * fx.pos) + fx.start[0] + fx.unit[0];
        nowPosX[1] = ((fx.end[1] - fx.start[1]) * fx.pos) + fx.start[1] + fx.unit[1];
        fx.elem.style.backgroundPosition = nowPosX[0]+' '+nowPosX[1];

    };


    /**
    * Animate background plugin
    * requires .animate() library extension
    */

    $.fn.animateBackground = function(options){

        options = $.extend({}, $.fn.animateBackground.defaults, options);
        var $this = $(this);
        
        var maxPositionH = -(options.backgroundSize - options.windowSize) + 'px 0';
        var maxPositionV = '0px 0' + (-(options.backgroundSize - options.windowSize));

        if (options.direction == 'horizontal') {
            $this.animate({
                backgroundPosition: maxPositionH
            }, options.time, options.type, function (){
                $this.animate({
                    backgroundPosition: '0 0'
                }, 0);
                $this.animateBackground({
                    direction: options.direction,
                    backgroundSize: options.backgroundSize,
                    time: options.time,
                    type: options.type,
                    windowSize: options.windowSize
                });
            });
        }
    };

    $.fn.animateBackground.defaults = {
        direction: 'horizontal',
        backgroundSize: 200,
        time: 1000,
        type: 'linear',
        windowSize: 100
    };
    


/*
* hoverFlow - A Solution to Animation Queue Buildup in jQuery
* Version 1.00
*
* Copyright (c) 2009 Ralf Stoltze, http://www.2meter3.de/code/hoverFlow/
* Dual-licensed under the MIT and GPL licenses.
* http://www.opensource.org/licenses/mit-license.php
* http://www.gnu.org/licenses/gpl.html
*/

    $.fn.hoverFlow = function(type, prop, speed, easing, callback) {
        // only allow hover events
        if ($.inArray(type, ['mouseover', 'mouseenter', 'mouseout', 'mouseleave']) == -1) {
            return this;
        }

        // build animation options object from arguments
        // based on internal speed function from jQuery core
        var opt = typeof speed === 'object' ? speed : {
            complete: callback || !callback && easing || $.isFunction(speed) && speed,
            duration: speed,
            easing: callback && easing || easing && !$.isFunction(easing) && easing
        };

        // run immediately
        opt.queue = false;

        // wrap original callback and add dequeue
        var origCallback = opt.complete;
        opt.complete = function() {
            // execute next function in queue
            $(this).dequeue();
            // execute original callback
            if ($.isFunction(origCallback)) {
                origCallback.call(this);
            }
        };

        // keep the chain intact
        return this.each(function() {
            var $this = $(this);

            // set flag when mouse is over element
            if (type == 'mouseover' || type == 'mouseenter') {
                $this.data('jQuery.hoverFlow', true);
            } else {
                $this.removeData('jQuery.hoverFlow');
            }

            // enqueue function
            $this.queue(function() {
                // check mouse position at runtime
                var condition = (type == 'mouseover' || type == 'mouseenter') ?
                // read: true if mouse is over element
                $this.data('jQuery.hoverFlow') !== undefined :
                // read: true if mouse is _not_ over element
                $this.data('jQuery.hoverFlow') === undefined;

                // only execute animation if condition is met, which is:
                // - only run mouseover animation if mouse _is_ currently over the element
                // - only run mouseout animation if the mouse is currently _not_ over the element
                if(condition) {
                    $this.animate(prop, opt);
                // else, clear queue, since there's nothing more to do
                } else {
                    $this.queue([]);
                }
            });

        });
    };

})(jQuery);

