$(document).ready(function(){
    $('div.aiPhotoList').each(function(){
       
        this.activateCurPhoto = function()
        {
            var currPhotoItem = this.currItem;
            if(currPhotoItem!=undefined) {
                currPhotoItem.find('img').animate({
                    opacity:1.0
                });
               __this = this;
                setTimeout(function(){
                    $(__this).find('.comment').html((currPhotoItem.find('img').attr('alt')!=undefined)?currPhotoItem.find('img').attr('alt'):"&nbsp;");
                    $(__this).find('.author').html((currPhotoItem.find('img').attr('author')!=undefined)?"ÔÎÒÎ: "+currPhotoItem.find('img').attr('author'):"&nbsp;");
                    $(__this).find('.agency').html((currPhotoItem.find('img').attr('agency')!=undefined)?currPhotoItem.find('img').attr('agency'):"&nbsp;");
                    
                    $(__this).find('.comment').animate({
                        opacity:1.0
                    },300);
                      
                     
                     $.preloadImages(currPhotoItem.find('img').attr('big'), function () {
                         
                        //alert(currPhotoItem.find('img').attr('big'));
                        $(__this).find('.bigphoto').find('img').css({'opacity':'0'});
                        $(__this).find('.bigphoto').find('img').attr('src',currPhotoItem.find('img').attr('big'));
                       
                        $(__this).find('.bigphoto').find('img').attr('alt',currPhotoItem.find('img').attr('alt'));
                        $(__this).find('.bigphoto').find('img').attr('title',currPhotoItem.find('img').attr('podskazka'));
                        
                        $(__this).find('.bigphoto a').attr('href',currPhotoItem.find('img').attr('bighref'));

//                        $(__this).find('.bigphoto').find('img').click(function(){
//                            var url = currPhotoItem.find('img').attr('bighref');
//                            $(location).attr('href',url);
//                        });
                        
                        $(__this).find('.bigphoto').find('img').animate({
                            opacity:1.0
                        });
                    });                 
                     
                },500);                
            }
        }
        this.deactivateCurPhoto = function()
        {
            var currPhotoItem = this.currItem;
           
             currPhotoItem.find('img').removeClass('active');
            if(currPhotoItem!=undefined) {
                currPhotoItem.find('img').animate({
                    opacity:0.4
                });
                $(this).find('.comment').animate({
                    opacity:0
                },500);                
                $(this).find('.bigphoto').find('img').animate({
                    opacity:0
                });
            }
        }

        var last_timeout;
        this.rotate = function(){   
        
           if($(this).attr('isRotate')!=1){}
           else
               {
                this.activateNext();
                var _this = this;
                        last_timeout=setTimeout(function(){
                        _this.rotate();
                    },$(_this).attr('rotationDelay'));
            
               }
        }
        
        this.activateNext = function(){          
           this.deactivateCurPhoto();
           while(1)
                {
                    this.currItem = this.currItem.next();
                    if(this.currItem.attr('class')=='photoItem') {break;}

                    else if(this.currItem.attr('class')=='emptyPhotoItem'){continue;}
                    else{
                        this.currItem = $(this).find('.photolist').find(' div.photoItem:first');
                        
                        break;
                    }
           }
           this.activateCurPhoto();          
        }
        this.activatePrev = function(){

            if(this.currItem!=undefined) this.deactivateCurPhoto();            
                while(1)
                {
                    this.currItem = this.currItem.prev();
                    if(this.currItem.attr('class')=='photoItem') break;

                    else if(this.currItem.attr('class')=='emptyPhotoItem')continue;
                    else{
                        this.currItem = $(this).find('.photolist').find(' div.photoItem:last');
                        break;
                    }
                }            
            this.activateCurPhoto();
            
        }

        //--------------toggle listener----------------
        var _this = this;
        $(this).find('.toggleActivate').click(function(){
            if($(_this).attr('isRotate')=="1"){
                $(_this).attr('isRotate','0');
                $(_this).find('.toggleActivate').find('a').html("play");
            }
            else{
                $(_this).attr('isRotate','1');
                _this.activateNext();
                $(_this).find('.toggleActivate').find('a').html("stop");

            }
            return false;
        });
        //--------------click image listener----------------
        var _this = this;
        $(this).find('.photolist .photoItem img').click(function(){
            //alert(1);
            if( $(_this).attr('isRotate')==1) 
            {
                $(_this).attr('isRotate','0');
                clearTimeout(last_timeout);
            }
            $(_this).find('.toggleActivate').find('a').html("play");
            if($(this).parent().offset().left==(_this.currItem).offset().left&&
                $(this).parent().offset().top==(_this.currItem).offset().top)return;
            _this.deactivateCurPhoto();
            _this.currItem = $(this).parent().parent();
            _this.activateCurPhoto();
            
            return false;
        });
        
        //--------------click bigimage listener----------------
        $(this).find('.big_image .bigphoto img').click(function(){
            
           if( $(_this).attr('isRotate')==0) 
           {
              // alert($(_this).attr('isRotate'));
                $(_this).attr('isRotate','1');
                _this.rotate();
           }
           else 
               {
                    $(_this).attr('isRotate','0');
                    clearTimeout(last_timeout);
               }
           
        });
        //-----------------init------------------
//            //------preload images---------------
//            $(this).find('.photolist').find(' div.photoItem  img').each(function(){
//               var img = new Image();
//               img.src = $(this).attr('big');
//            });
            //------set first active image


        
        this.currItem =  $(this).find('.photolist').find(' div.photoItem  img.active').parent().parent();
        
        if(this.currItem.attr('class')!='photoItem'){
            this.currItem = $(this).find('.photolist').find(' div.photoItem:first');
        }
        if(this.currItem.attr('class')!='photoItem'){
            alert('error in photolist control.can\'t set first active item');
        }else{
               // this.currItem.find('img').attr('class','active');
        }
            //------try to rotate
        if($(this).attr('isRotate')==1){
                 var _this = this;
                        setTimeout(function(){
                        _this.rotate();
                    },$(_this).attr('rotationDelay'));   
        }
    });

    jQuery.preloadImages = function () {
        if (typeof arguments[arguments.length - 1] == 'function') {
            var callback = arguments[arguments.length - 1];
        } else {
            var callback = false;
        }
        if (typeof arguments[0] == 'object') {
            var images = arguments[0];
            var n = images.length;
        } else {
            var images = arguments;
            var n = images.length - 1;
        }
        var not_loaded = n;
        for (var i = 0; i < n; i++) {
            jQuery(new Image()).load(function() {
                if (--not_loaded < 1 && typeof callback == 'function') {
                    callback();
                }
            }).attr('src', images[i]);
        }
}



});
