/**
 * This is part of a patch to address a jQueryUI bug.  The bug is responsible
 * for the inability to scroll a page when a modal dialog is active. If the content
 * of the dialog extends beyond the bottom of the viewport, the user is only able
 * to scroll with a mousewheel or up/down keyboard keys.
 *
 * @see http://bugs.jqueryui.com/ticket/4671
 * @see https://bugs.webkit.org/show_bug.cgi?id=19033
 * @see /views_ui.module
 * @see /js/jquery.ui.dialog.min.js
 *
 * This javascript patch overwrites the $.ui.dialog.overlay.events object to remove
 * the mousedown, mouseup and click events from the list of events that are bound
 * in $.ui.dialog.overlay.create
 *
 * The original code for this object:
 * $.ui.dialog.overlay.events: $.map('focus,mousedown,mouseup,keydown,keypress,click'.split(','),
 *  function(event) { return event + '.dialog-overlay'; }).join(' '),
 *
 */

(function ($, undefined) {
  if ($.ui && $.ui.dialog) {
    $.ui.dialog.overlay.events = $.map('focus,keydown,keypress'.split(','),
                                 function(event) { return event + '.dialog-overlay'; }).join(' ');
  }
}(jQuery));
;
Drupal.behaviors.frontPage = {
  attach: function() {
    var current = 1;
    var speed = 0.3;
    $(".frontpage-slider").cycleCarousel({
      'speed': speed,
      auto: true,
      autoTimeOut: 7000,
      autoPassPeriod: 20000,
      onEndChange: function(oldIndex, newIndex) {
        var options = $(".frontpage-slider").data("cycleCarousel");
        $(".slider-pager span").removeClass("current");
        current = newIndex;
        var i = 0;
        $(".slider-pager span").each(function() {
          if (i == newIndex) {
            $(this).addClass("current");
          }
          i++;
        });
        options.speed = speed;
      },
      easingFunction: "linear"
    });
    current = 0;
    $(".slider-pager span").each(function(ind) {
      $(this).click(function() {
        var options = $(".frontpage-slider").data("cycleCarousel");
        options.speed = speed / Math.abs(current - ind);
        $(".frontpage-slider").cycleCarousel("go", ind);
      });
    });
    /*SEO hacks*/
    /*$(".block-text-3 .item, .block-text-4 .item, .block-text-2").each(function() {
      var $this = $(this);
      var url = $("h2 a, h3 a", $this).attr("href");
      $(".item-image img, .read-more", $this).click(function() {
        document.location.href = url;
      })
    });*/
  }
};
/*!
 * jQuery cycleCarousel plugin
 *
 * @author Ilya Gruzinov (ilya.gruzinov@gmail.com)
 */
(function($) {
    var pluginName = "cycleCarousel";
    var cssAttached = false;

    function getOptions($carousel) {
        var options = $carousel.data(pluginName);
        if (!options.cache || !options.cacheChildren) {
            var elements = [];
            var leftElements = [];
            var rightElements = [];
            $carousel.children("li").not(".cycle-carousel-clone").each(function() {
                var $this = $(this);
                var element = {
                    'self': $this
                };
                element.width = $this.width();
                element.height = $this.height();
                elements.push(element);
            });
            options.elements = elements;
            $carousel.children("li.cycle-carousel-clone-right").each(function() {
                var $this = $(this);
                var element = {
                    'self': $this
                };
                element.width = $this.width();
                element.height = $this.height();
                rightElements.push(element);
            });
            options.rightElements = rightElements;
            $carousel.children("li.cycle-carousel-clone-left").each(function() {
                var $this = $(this);
                var element = {
                    'self': $this
                };
                element.width = $this.width();
                element.height = $this.height();
                leftElements.push(element);
            });
            options.leftElements = leftElements;
        }
        if (!options.cache || !options.cacheSizes) {
            var left = 0;
            var leftWidth = 0;
            for(var i = 0; i< options.leftElements.length; i++) {
                options.leftElements[i].width = options.leftElements[i].self.width();
                options.leftElements[i].height = options.leftElements[i].self.height();
                options.leftElements[i].left = left;
                left += options.leftElements[i].width;
                leftWidth += options.leftElements[i].width;
            }
            options.leftWidth = leftWidth;
            var elementsWidth = 0;
            for(var i = 0; i< options.elements.length; i++) {
                options.elements[i].width = options.elements[i].self.width();
                options.elements[i].height = options.elements[i].self.height();
                options.elements[i].left = left;
                left += options.elements[i].width;
                elementsWidth += options.elements[i].width;
            }
            options.elementsWidth = elementsWidth;
            var rightWidth = 0;
            for(var i = 0; i< options.rightElements.length; i++) {
                options.rightElements[i].width = options.rightElements[i].self.width();
                options.rightElements[i].height = options.rightElements[i].self.height();
                options.rightElements[i].left = left;
                left += options.rightElements[i].width;
                rightWidth += options.rightElements[i].width;
            }
            options.rightWidth = rightWidth;
        }
        return options;
    }
    function resize($carousel) {
        var options = $carousel.data(pluginName);
        //$carousel.css("width", "");
        //$carousel.children().remove(".cycle-carousel-clone");
        $.data(pluginName, null);
        init($carousel, options);
    }
    function go($carousel, elementNumber, notPauseCarousel) {
        $carousel.stop(true, true);

        var options = getOptions($carousel);
        
        var direction = false;
        var length1;
        var length2;
        var rightLength = 0;
        var leftLength = 0;

        var queue = [];

        if (elementNumber == "next") {
            elementNumber = "+1";
        } else  if (elementNumber == "prev") {
            elementNumber = "-1";
        }

        if ((elementNumber.toString().charAt(0) == "+") || (elementNumber.toString().charAt(0) == "-")) {
            if (elementNumber.toString().charAt(0) == "+") {
                elementNumber = options.current + parseInt(elementNumber);
                while (elementNumber >= options.elements.length ) {
                    queue.push("right");
                    elementNumber -= options.elements.length;
                }
            } else {
                elementNumber = options.current + parseInt(elementNumber);
                while (elementNumber < 0 ) {
                    queue.push("left");
                    elementNumber += options.elements.length;
                }

            }
        }
        if ((elementNumber >= (options.elements.length)) || (elementNumber < 0) || (elementNumber == options.current && queue.length == 0)) {
            return; // no action
        }
        queue.push(elementNumber);
        
        if (options.onStartChange) {
            options.onStartChange.apply($carousel, [options.current, elementNumber, direction]);
        }
        var current = options.current;
        var first = true;
        for (var i in queue) {
            var action = queue[i];
            var duration = 0;
            if (action == "left") {
                //var duration = Math.abs(options.elements[current].left - options.elements[0].left) * options.speed;
                if (current == 0) {
                    if (!first) {
                        $carousel.animate({left: (-1 *  options.rightElements[0].left) + "px"}, 1);
                        duration = Math.abs(options.elements[0].left - options.rightElements[0].left) * options.speed
                    } else {
                        duration = 0;
                    }
                } else {
                    duration = Math.abs(options.elements[0].left - options.elements[current].left) * options.speed;
                }
                if (duration) {
                    $carousel.animate({
                        left: (-1 *  options.elements[0].left) + "px"
                    }, duration, options.easingFunction);
                    $carousel.delay(1);
                }
                current = 0;
            } else if (action == "right") {
                if (current == options.elements.length - 1) {
                    if (!first) {
                        $carousel.animate({left: (-1 *  options.leftElements[options.leftElements.length - 1].left) + "px"}, 1);
                        duration = Math.abs(options.elements[0].left - options.rightElements[0].left) * options.speed
                    } else {
                        duration = 0;
                    }
                } else {
                    duration = Math.abs(options.elements[options.elements.length - 1].left - options.elements[current].left) * options.speed;
                }
                if (duration) {
                    $carousel.animate({
                        left: (-1 *  options.elements[options.elements.length - 1].left) + "px"
                    }, duration, options.easingFunction);
                    $carousel.delay(1);
                }
                current = options.elements.length - 1;
            } else {
                if (current == 0 && queue[0] == "left") {
                    $carousel.animate({
                        left: (-1 *  options.rightElements[0].left) + "px"
                    }, 1);
                    duration =  Math.abs(options.elements[action].left - options.rightElements[0].left) * options.speed;
                } else if (current == options.elements.length - 1 && queue[0] == "right") {
                    $carousel.animate({
                        left: (-1 *  options.leftElements[options.leftElements.length - 1].left) + "px"
                    }, 1);
                    duration =  Math.abs(options.elements[action].left - options.leftElements[options.leftElements.length - 1].left) * options.speed;
                } else {
                    duration = Math.abs(options.elements[current].left - options.elements[action].left) * options.speed;
                }
                if (duration) {
                    $carousel.animate({
                        "left" : (-1 * options.elements[action].left) + "px"
                    }, duration, options.easingFunction, function() {
                        if (options.onEndChange) {
                            options.onEndChange.apply($carousel, [options.current, action, direction]);
                        }
                        options.current = action;
                    });
                } 
                
                break;
            }
            first = false;
        }

        if (notPauseCarousel){
        //no any actions now
        } else {
            if (options.auto) {
                autoStart($carousel);
            } else {
                autoStop($carousel);
            }
        }

    }

    function release($carousel, isResizeEvent) {
        autoStop($carousel);

        var options = $carousel.data(pluginName);
        //$carousel.css("width", "");
        //$carousel.children().remove(".cycle-carousel-clone");
        $carousel.data(pluginName, null);
        $carousel.css("width", "");
        $carousel.css("left", "");
        $carousel.children().css("width", "");
        $carousel.children().remove(".cycle-carousel-clone");
        if (isResizeEvent) {
            $carousel.unwrap();
            $carousel.removeClass("cycle-carousel");
        }
    }

    
    function init($carousel, opts, isResizeEvent) {
        var elements = [];
        var leftElements = [];
        var rightElements = [];

        //extend options by default
        var defaults = {
            speed: 1,
            current: 0,
            cache: true,
            cacheChildren: true,
            cacheSizes: true,
            easingFunction : "linear",
            passResize: false,
            auto: false,
            autoTimeOut: 1000,
            autoPassPeriod: 5000

        }
        var options = $.extend(defaults, opts);

        if (!cssAttached) {
            cssAttached = true;
            $("<style>\
div.cycle-carousel-container {\
    overflow: hidden;\
    padding: 0px;\
    margin: 0px;\
    border: 0px;\
}\
div.cycle-carousel-container > ul.cycle-carousel {\
    overflow: hidden;\
    position: relative;\
    padding: 0px;\
    margin: 0px;\
    border: 0px;\
}\
div.cycle-carousel-container > ul.cycle-carousel > li {\
    display: block;\
    position: relative;\
    float: left;\
    padding: 0px;\
    margin: 0px;\
    border: 0px;\
}\
            </style>")
            .attr({
                type: "text/css",
                rel:"stylesheet"
            })
            .appendTo("head");
        }

        $carousel.children("li").each(function(){
            var $this = $(this);
            var item = {
                self: $this,
                width: $this.width(),
                left: $this.left
            }
            elements.push(item);
            //save width
            $this.css("width", item.width);

        });
        
        if (isResizeEvent) {
            $carousel.wrap('<div class="cycle-carousel-container" style="height: 10px"></div>');
            $carousel.addClass("cycle-carousel");
        }
        //add item to right
        var containerWidth = $carousel.parent().width();
        
        var w = elements[elements.length - 1].width;
        var i = 0;
        while (w < containerWidth + 50) { //why 50? Because our slicer make error in CSS and UL is lesser then LI
            var item = elements[i % elements.length];
            var cloned = {
                self: item.self.clone(),
                width: item.width,
                height: item.height
            }
            cloned.self.addClass("cycle-carousel-clone");
            cloned.self.addClass("cycle-carousel-clone-right");
            rightElements.push(cloned);
            $carousel.append(cloned.self);
            w += cloned.width;
            i++;

        }
        
        var rightWidth = w - elements[elements.length - 1].width;
        //add item to left
        w = elements[elements.length - 1].width;
        i = 0;
        while (w < containerWidth + 50) { //why 50? Because our slicer make error in CSS and UL is lesser then LI
            var item = elements[elements.length - (i % elements.length) - 1];
            var cloned = {
                self: item.self.clone(),
                width: item.width,
                height: item.height
            }
            cloned.self.addClass("cycle-carousel-clone");
            cloned.self.addClass("cycle-carousel-clone-left");
            leftElements.push(cloned);
            $carousel.prepend(cloned.self);
            w += cloned.width;
            i++;
        }

        //left item width is reversed. change back
        for(i = 0; i < (leftElements.length / 2); i++) {
            var tmp = leftElements[i].width;
            leftElements[i].width = leftElements[leftElements.length - i - 1].width;
            leftElements[leftElements.length - i - 1].width = tmp;

        }


        var left = 0;
        var leftWidth = 0;
        for(var i = 0; i< leftElements.length; i++) {
            leftElements[i].left = left;
            left += leftElements[i].width;
            leftWidth += leftElements[i].width;
        }
        var elementsWidth = 0;
        
        options.leftWidth = leftWidth;
        for(var i = 0; i< elements.length; i++) {
            elements[i].left = left;
            left += elements[i].width;
            elementsWidth += elements[i].width;
        }
        options.elementsWidth = elementsWidth;
        var rightWidth = 0;
        for(var i = 0; i< rightElements.length; i++) {
            rightElements[i].left = left;
            left += rightElements[i].width;
            rightWidth += rightElements[i].width;
        }
        options.rightWidth = rightWidth;
        //alert((leftWidth + elementsWidth + rightWidth) + "px");
        setTimeout(function() {
            $carousel.css("width", (leftWidth + elementsWidth + rightWidth + 500) + "px");
            $carousel.css("left", "-" + options.elements[options.current].left + "px");
            $carousel.parent().css("height", "");
        }, 10);

        options.elements = elements;
        options.leftElements = leftElements;
        options.rightElements = rightElements;
        options.containerWidth = containerWidth;
        $carousel.data(pluginName, options);
        if (!options.passResize) {
            var options = $carousel.data(pluginName);
            options.passResize = true;
            var lockResize = false;
            $(window).resize(function() {
                if (lockResize) {
                    return false;
                }
                var options = $carousel.data(pluginName);
                $carousel.parent().css("height", $carousel.parent().height() + "px");
                release($carousel, false);
                init($carousel, options, false);
                lockResize = false;
                return false;
                lockResize = false;
                return false;
                
            });
        }
        if (options.auto) {
            autoStart($carousel);
        }
    }

    function autoStart($carousel, startNow) {
        var options = $carousel.data(pluginName);
        autoStop($carousel);
        var passPeriod = startNow ? 1 : options.autoPassPeriod ;
        options._autoPassInterval = setTimeout(function () {
            autoStop($carousel);
            go($carousel, "next", true);
            options._autoInterval = setInterval(function() {
                go($carousel, "next", true);
            }, options.autoTimeOut);
        }, passPeriod);
        
    }

    function autoStop($carousel) {
        var options = $carousel.data(pluginName);
        if (options._autoInterval) {
            clearInterval(options._autoInterval);
            options._autoInterval = false;
        }
        if (options._autoPassInterval) {
            clearInterval(options._autoPassInterval);
            options._autoPassInterval = false;

        }

    }

    $.fn.cycleCarousel = function(options, arg2, arg3) {
        var args = arguments;
        return this.each(function() {
            var $this = $(this);
        
            var command = args.length ? args[0] : null;
            if (command == "go") {
                var elementNumber = args.length < 2 ? "next" : args[1]
                return go($this, elementNumber);
            } else {
                init($this, command, true);
                return ;
            }
        });
    }

    //Hack to incorrect function which was not work with "BIG" numbers.
    if ($().jquery < "1.5.1")     {
        // Get fixed version from jQuery 1.5.1
        $.fx.prototype.cur = function() {
            if ( this.elem[this.prop] != null && (!this.elem.style || this.elem.style[this.prop] == null) ) {
                return this.elem[ this.prop ];
            }

            var parsed,
            r = jQuery.css( this.elem, this.prop );
            // Empty strings, null, undefined and "auto" are converted to 0,
            // complex values such as "rotate(1rad)" are returned as is,
            // simple values such as "10px" are parsed to Float.
            return isNaN( parsed = parseFloat( r ) ) ? !r || r === "auto" ? 0 : r : parsed;
        };
    }

}(jQuery)) ;

;

