	jQuery(function() {
	
	 	jQuery('.handOfMe').css("cursor", 'url(\'grabbing.cur\')');
	 // alert(jQuery('.handOfMe').css("cursor"));
	 
	 
		   jQuery('.handOfMe[class]').css("cursor", 'url(\'grab.cur\')');
		   if(navigator.appName=='Netscape'){
      jQuery('.handOfMe[class]').css("cursor", '-moz-grab');
      }
      if(navigator.appName=='Opera'){
        jQuery('.handOfMe').css("cursor", 'move');  
      }
	
        var globalHeightImg = 625;
        	var globalCCd = globalHeightImg;	
	    jQuery('.buttonBg').css('opacity','0.5');
     	var cc = jQuery(".handOfMe").children();
     	if(navigator.appName=='Opera'){
        jQuery('.handOfMe').css("cursor", 'move');  
      }
     	var docWidth = parseInt(jQuery(window).width());
     	var activeBox = null;  //активный открытый блок
		var ccdl = 0;
		jQuery('.item').each(function(){
            var fgr= jQuery(this).children('.itemInner').children(':first-child');
            var rtyrtyr=jQuery(this).children('.itemInner').children(':first-child').width(); 
    	    ccdl=ccdl+jQuery(this).children('.itemInner').children(':first-child').width();
    	    jQuery(this).css('width',rtyrtyr);
    	    jQuery(this).css('height',globalHeightImg);
    	    fgr.css({
                display:'block',
                height:globalHeightImg
            })
    	    ccdl=ccdl+1;
		});
		
		var ccdl2 = (-1)*ccdl+docWidth;
		ccdl2 = (ccdl2<0) ? ccdl2 :0;
		jQuery(".handOfMe").draggable({ axis: 'x' , containment: [ccdl2,0,0, 0] ,cursor:'grabbing'});
        jQuery(".handOfMe").css('width',$(document).width()*cc.length+'px');
        jQuery('.item').children('.add').each(function(){
            
            jQuery(this).click(function(){
            
            if(activeBox){  
                var theOner= 1;
                activeBox.children('.itemInner').children('img').each(function(){
                    if(theOner!=1){
                        jQuery(this).css('display','none');
                    }
                    theOner++;
                })      
                activeBox.children('.unadd').css('display','none');
           	    activeBox.children('.add').css('display','block').css('z-index','80');
                jQuery(".handOfMe").animate({
                    top:'0px'
                },'slow');
                activeBox.children('.itemInner').css('z-index','20');
                activeBox = null;
                jQuery('.blackMoment').css('display','none');
                jQuery(".handOfMe").draggable({ axis: 'x' , containment: [ccdl2,0,0, 0] ,cursor:'pointer'});
            }
            
                 jQuery('.reklame').css('display','none');
                var prevHeight = 0;
                var endHeight = 0;
                var finalHeight = 0;
                var count = jQuery(this).parent().children('img').length;
                var iz=1;
                jQuery(this).parent().children('.itemInner').children('img').each(function(){
                    jQuery(this).css('display','block');
                    jQuery(this).css('top',0);
                    jQuery(this).css('z-index',3);
                    if(iz>1){
                        jQuery(this).css('z-index',2);
                        jQuery(this).css('position',"absolute");
                        jQuery(this).css('top',0);
                        jQuery(this).animate({
                            top:parseInt(prevHeight)
                        },'slow',function(){
                            jQuery(this).css('position',"relative");
                            jQuery(this).css('top',0);
                            jQuery(this).css('margin-top','1px');
                            jQuery(this).css('z-index',3);
                            prevHeight += 1;
                        });
                    }
                
                
                    prevHeight +=  parseInt(jQuery(this).height());
                    endHeight +=  parseInt(jQuery(this).height());
                    if(iz<count){
                        finalHeight  +=  parseInt(jQuery(this).css('height'));
                    }
                    iz++;
                })
                jQuery(this).parent().css('height',(endHeight+90)+'px');	
                var ccd = ((-1)*jQuery(this).parent().outerHeight()+jQuery(window).height()-90);
                ccd = (ccd<=0) ? ccd :  0 ;
                globalCCd = ccd;
       	        jQuery(".handOfMe").draggable({ axis: 'y' , containment: [0,ccd,0, 0] ,cursor:'pointer'});
       	        activeBox = jQuery(this).parent();
       	        var currentPosition = activeBox.offset().left;
       	        if(currentPosition<0){
       	        
       	        //alert(jQuery('.handOfMe').css('left')+currentPosition);
       	            jQuery('.handOfMe').animate({
       	                left:parseInt(jQuery('.handOfMe').css('left'))+(currentPosition*(-1)+20)
       	            },'fast');
       	        }
       	        jQuery(this).css('display','none')
       	         if(navigator.appName!='Microsoft Internet Explorer'){
       	          jQuery('.blackMoment').css('display','block').css('position','absolute').css('z-index','23').css('opacity','0.5');
       	         }
       	        jQuery(this).parent().children('.unadd').css('display','block').css('z-index','80');
                endHeight = (endHeight==0) ? jQuery(this).parent().height() :endHeight ;
              //  jQuery(this).parent().children('.itemInner').css('position','absolute').css('position','absolute').css('z-index','25');
                jQuery(this).parent().children('.itemInner').css('position','absolute').css('z-index','79');
            });
            
            var countOfChildImage = 0;
            countOfChildImage = jQuery(this).parent().children('.itemInner').children('.views').length;
            //alert(countOfChildImage);
            if(countOfChildImage<2){
              jQuery(this).css('display','none');
            } 
            
        })
        jQuery('.item').children('.unadd').each(function(){
            jQuery(this).click(function(){
                var theOner= 1;
                jQuery(this).parent().children('.itemInner').children('img').each(function(){
                    if(theOner!=1){         
                        jQuery(this).css('display','none');
                    }
                    theOner++;
                })
                 
                 
                              
                jQuery(this).css('display','none');
               	jQuery(this).parent().children('.add').css('display','block').css('z-index','21');
                jQuery(".handOfMe").animate({
                    top:'0px'
                },'slow');
                jQuery(this).parent().children('.itemInner').css('z-index','20');
                activeBox = null;
                jQuery('.blackMoment').css('display','none');
                jQuery(".handOfMe").draggable({ axis: 'x' , containment: [ccdl2,0,0, 0] ,cursor:'pointer'});    
            })
        })
        jQuery('.blackMoment').click(function(){
            if(activeBox){  
                var theOner= 1;
                activeBox.children('.itemInner').children('img').each(function(){
                    if(theOner!=1){
                        jQuery(this).css('display','none');
                    }
                    theOner++;
                })      
                activeBox.children('.unadd').css('display','none');
           	    activeBox.children('.add').css('display','block').css('z-index','80');
                jQuery(".handOfMe").animate({
                    top:'0px'
                },'slow');
                activeBox.children('.itemInner').css('z-index','20');
                activeBox = null;
                jQuery('.blackMoment').css('display','none');
                jQuery(".handOfMe").draggable({ axis: 'x' , containment: [ccdl2,0,0, 0] ,cursor:'pointer'});
            }
        })
        
        
        
        jQuery(".reklame").children('a').children('img').load(function(){
        jQuery('.reklame').css({
            
            top:parseInt(globalHeightImg+90-parseInt(jQuery(".reklame").children('a').children('img').css('height')))
        })
        
       
        
        });
       
        
        jQuery('.reklame').click(function(){
            jQuery('.reklame').css('display','none');
        })

function handle(delta) {
      jQuery('.handOfMe').stop();
      
      var speedOfScroll = '2000';

        if (!(delta < 0)){
     
        // jQuery('.handOfMe').css('left',(parseInt(jQuery('.handOfMe').css('left') )+20)+'px');
     if(activeBox==null){
       var lengthTogo  = (parseInt(jQuery('.handOfMe').css('left') )+400);
      lengthTogo = (lengthTogo<0) ? lengthTogo :  0;
     
        jQuery('.handOfMe').animate({
       	                left: (lengthTogo+'px')
       	            },speedOfScroll); 
       	            
       }else{
       // alert('sfsdfsdf222');  
       var lengthTogo  = (parseInt(jQuery('.handOfMe').css('top') )+400);
      lengthTogo = (lengthTogo<0) ? lengthTogo :  0;
     
        jQuery('.handOfMe').animate({
       	                top: (lengthTogo+'px')
       	            },speedOfScroll); 
       
       
       }            
        }else{
     
        if(activeBox==null){
           var lengthTogo  = (parseInt(jQuery('.handOfMe').css('left') )-400);
           var lendwidth  = ccdl2;
           //alert(lendwidth+" - "+lengthTogo);
      lengthTogo = (lengthTogo<lendwidth) ? lendwidth :  lengthTogo;
     
        jQuery('.handOfMe').animate({
       	                left:(lengthTogo+'px')
       	            },speedOfScroll); 
        //  doc.style.left = parseInt(doc.style.left)+10+'px';
        
        }else{
          
       var lengthTogo  = (parseInt(jQuery('.handOfMe').css('top') )-400);
           var lendwidth  = globalCCd;
         // alert(lendwidth+" - "+lengthTogo);
      lengthTogo = (lengthTogo<lendwidth) ? lendwidth :  lengthTogo;
     
        jQuery('.handOfMe').animate({
       	                top: (lengthTogo+'px')
       	            },speedOfScroll); 
       
       
       } 
        }
}


function wheel(event){
        //alert('work2');
        var delta = 0;
        if (!event) /* For IE. */         
                event = window.event;
        if (event.wheelDelta) { /* IE/Opera. */
                delta = event.wheelDelta/120;
                /** In Opera 9, delta differs in sign as compared to IE.
                 */
                if (window.opera)
                        delta = -delta;
        } else if (event.detail) { /** Mozilla case. */
                /** In Mozilla, sign of delta is different than in IE.
                 * Also, delta is multiple of 3.
                 */
                delta = -event.detail/3;
        }
        
        if (delta)
                handle(delta);
       
        if (event.preventDefault)
                event.preventDefault();
	event.returnValue = false;
}




if (window.addEventListener)
        //** DOMMouseScroll is for mozilla. 
        window.addEventListener('DOMMouseScroll', wheel, false);
        
       
//** IE/Opera

window.onmousewheel = document.onmousewheel = wheel;
  
	});
