var clickLock=false;
var autoslide=true;
var tipgalspeed=400;
var galspeed=5000;
var galTime;
var max;
var move;
var max_move;
var width=498;
var start=false;
// timer function
function galleryTimer(){
    galTime=setTimeout('galleryTimer()',galspeed)
    if(autoslide && start){
        movenext();
    }
    start=true;
}

function movenext(){
       if(!clickLock){
           clickLock=true
           var pos=$('.window .image_reel').position();
           if(pos.left> -(max-width)){
               $('.window2 .image_reel').animate({left:pos.left-width+'px'},tipgalspeed)
               $('.window .image_reel').animate({left:pos.left-width+'px'},tipgalspeed,function(){
                    clickLock=false;
                    move++; 
                     $('.link .paging a').removeClass('active') 
                     $('.link .paging a:eq('+move+')').addClass('active') 
                    
               })
           }
           else {
               $('.window2 .image_reel').animate({left:0+'px'},tipgalspeed)
               $('.window .image_reel').animate({left:0+'px'},tipgalspeed,function(){
                clickLock=false;
                    move=0;
                     $('.link .paging a').removeClass('active') 
                     $('.link .paging a:eq('+move+')').addClass('active') 
                })
           }
       }
}
function moveprev(){
       if(!clickLock){
           clickLock=true
           var pos=$('.window .image_reel').position();
           if(pos.left< 0){
               $('.window2 .image_reel').animate({left:pos.left+width+'px'},tipgalspeed)
               $('.window .image_reel').animate({left:pos.left+width+'px'},tipgalspeed,function(){
                    clickLock=false;
                    move--; 
                     $('.link .paging a').removeClass('active') 
                     $('.link .paging a:eq('+move+')').addClass('active') 
                    
               })
           }
           else {
               $('.window2 .image_reel').animate({left:-(max-width)+'px'},tipgalspeed)
               $('.window .image_reel').animate({left:-(max-width)+'px'},tipgalspeed,function(){
                clickLock=false;
                    move=max_move;
                     $('.link .paging a').removeClass('active') 
                     $('.link .paging a:eq('+move+')').addClass('active') 
                })
           }
       }
}

$(document).ready(function(){
    max=$('.window .image_reel img').length*width;
	max2=$('.window .image_reel img').length*16;
    move=0;
    max_move=$('.window .image_reel img').length-1;
    $('.window .image_reel').css({width:max+"px"});
	$('.paging ul').css({width:max2+"px"});
    $('.window2 .image_reel').css({width:max+"px"});
    $('.link .paging a:eq(0)').addClass('active') 
    $('.link .paging a').click(function(e){
           e.preventDefault();
           if(!clickLock){
               clickLock=true
               var currentItem=$('.link .paging a').index(this);
               var pos=$('.window .image_reel').position();
               
               if(currentItem>move){
                   $('.window2 .image_reel').animate({left:pos.left-width*(currentItem-move)+'px'},tipgalspeed);
                   $('.window .image_reel').animate({left:pos.left-width*(currentItem-move)+'px'},tipgalspeed,function(){
                        clickLock=false;
                        move=currentItem; 
                        $('.link .paging a').removeClass('active') 
                        $('.link .paging a:eq('+move+')').addClass('active') 
                   })
               }
               else if(currentItem<move){
                   $('.window2 .image_reel').animate({left:pos.left+width*(move-currentItem)+'px'},tipgalspeed)
                   $('.window .image_reel').animate({left:pos.left+width*(move-currentItem)+'px'},tipgalspeed,function(){
                        clickLock=false;
                        move=currentItem; 
                        $('.link .paging a').removeClass('active') 
                        $('.link .paging a:eq('+move+')').addClass('active') 
                   })
               }
               else clickLock=false;
           }
       })
       
       $('.link .next2').click(function(e){
           e.preventDefault();
           movenext()
       })
       $('.link .previous2').click(function(e){
           e.preventDefault();
           moveprev()
       })
       
       $('.iPad').mouseenter(function(){
           autoslide=false;
       }).mouseleave(function(){
           autoslide=true;
       })
       
       
       if($('#homepagebanner').length)
       // run the timer
       galleryTimer();            
       
       
       
       
       // home page more
       var par=$(".content").parent();
       par.css({height:282});
       $(".content").css({height:243,position:'absolute',left:0,top:0});
       $(".contentHolder").css({height:210,overflow:'hidden'});
       j_maxh=$(".slideContent").height();
       $(".done").hide();
       
           $(".more").click(function() {
                var obj=$(this);
                $(".contentHolder").animate({height:(j_maxh+10)},500)
                $(".content").animate({height:j_maxh+30,'border-right': '5px solid #9B9B9B','border-bottom': '5px solid #9B9B9B',padding: '20px'},500,function(){
                    
                    obj.hide();
                    $(".done").show()   
					$(".showNewsHeight").css({height:j_maxh});
                    
                })
                

              });
       
           $(".done").click(function() {
                var obj=$(this);
                $(".contentHolder").animate({height:210},500)
                $(".content").animate({height:243, border: '0',padding: '20px 20px 15px 24px'},500,function(){
                      obj.hide();
                      $(".more").show()      
					  $(".showNewsHeight").css({height:282});
                    
                      
                });           
            });
		   
		   
	   
	   // news-listing page more
       var par2=$(".content2").parent();
       par2.css({height:460});
       $(".content2").css({height:425,position:'absolute',left:0,top:0});
       $(".contentHolder2").css({height:425,overflow:'hidden'});
       j_maxh=$(".slideContent").height();
       $(".done2").hide();
       
           $(".more2").click(function() {
                var obj=$(this);
                $(".contentHolder2").animate({height:(j_maxh+30)-40},500)
                $(".content2").animate({height:j_maxh+30},500,function(){
                    
                    obj.hide();
                    $(".done2").show()   
					$(".showNewsHeight").css({height:j_maxh+40});
                    
                })
                

              });
       
           $(".done2").click(function() {
                var obj=$(this);
                $(".contentHolder2").animate({height:425},500)
                $(".content2").animate({height:425},500,function(){
                      obj.hide();
                      $(".more2").show()      
					  $(".showNewsHeight").css({height:460});
                   
                });           
            });

})
