$(function(){
    $('.arrows a').each(function(i,e){
        var root = $(this), mask = $('.hidden_content'), page = $('.long_content'), dir = root.hasClass('top')?'top':'bot', speed = 70, h = page.height(), h2 = mask.height(), time;
        if(h<=h2)
        {
            $('.arrows a').hide();
            return false;
        }
        root.bind('mouseup mouseleave', function(){
            if(time)
                time = clearInterval(time);
        }).mousedown(function(){
            if(time)
                time = clearInterval(time);
            var pos = page.position(),offset = 0, new_pos;
            time = setInterval(function(){
                if(dir=='top')
                    offset+=speed;
                else
                    offset-=speed;
                new_pos = pos.top+offset
                if(new_pos >= speed || new_pos*-1 >= h-h2+speed)
                {
                    root.trigger('mouseup');
                    return false;
                }
                page.css('top',new_pos);
            }, 50);
        });
    });
    
    function randomizeEffect(n)
    {
        var i = (Math.random()*10)%n;
        return i.toFixed();
    }
    
    var images = $('.main_image .image,#flashcontent .image');
    var n = images.size();
    
    var effects = ['fade','blind','clip','fold','slide']
    var e=4;
    var imTime;
    var _ani = false;
    var preloader = $('<div/>',{'class':'preloader_dir'}).appendTo('body');
   images.each(function(){
       var root = $(this);
      var img = $('<img/>',{'class':'preloader'}).css({'width':0,'height':0});
      var src = root.css('backgroundImage').replace(/^url\([\'\"]?([^\'\"]+)[\'\"]?\)$/, '$1');
      img.attr('src',src);
      img.appendTo(preloader);
   });
   images.parent().addClass('Loader');
    preloader.onImagesLoaded(function(){
        images.eq(0).show();
		images.parent().removeClass('Loader');
        var img = images.filter(':visible').addClass('showed');
        
        var i = images.index(img),next=i+1;
        imTime = setInterval(function(){
            if(_ani || n == 1)
                return;
            _ani = true;
            if(next == n)
                next = 0;
            var prev = img.css('zIndex',1).removeClass('showed');
            //e = randomizeEffect(effects.length);
            img = images.eq(next).css('zIndex',2).show(effects[e],{duration:2000,easing:'easeOutCubic'},function(){
                prev.hide();
                _ani = false;
            }).addClass('showed');
            next++;
        },3000);
    });
    $('#domy .preview').mouseenter(function(){
        var root = $(this);
        var desc = root.find('.desc');
        var h = desc.outerHeight();
        desc.css({'bottom':-h,'opacity':0}).show();
        desc.animate({'bottom':0,'opacity':1},300);
    }).mouseleave(function(){
        var root = $(this);
        var desc = root.find('.desc');
        var h = desc.outerHeight();
        desc.animate({'bottom':-h,'opacity':0},300,function(){desc.hide();});
    }).find('a').click(function(){
        var root = $(this);
        var href = root.attr('href');
        var popup = window.open(href,'','width=920,height=720')
        return false;
    });
    
});

(function($){
jQuery.fn.onImagesLoaded = function(_cb) { 
          return this.each(function() {

            var $imgs = (this.tagName.toLowerCase()==='img')?$(this):$('img',this),
                _cont = this,
                    i = 0,
            _done=function() {
              if( typeof _cb === 'function' ) _cb(_cont);
            };
            if( $imgs.length ) {
              $imgs.each(function() {
                var _img = this,
                _checki=function(e) {
                  if((_img.complete) || (_img.readyState=='complete'&&e.type=='readystatechange') )
                  {
                    if( ++i===$imgs.length ) _done();
                  }
                  else if( _img.readyState === undefined ) // dont for IE
                  {
                    $(_img).attr('src',$(_img).attr('src')); // re-fire load event
                  }
                }; // _checki \\
                $(_img).bind('load readystatechange', function(e){_checki(e);});
                _checki({type:'readystatechange'}); // bind to 'load' event...
              });
            } else _done();
          });
        };
})(jQuery);
