
/**
 * Stop any JS Errors
**/

//window.onerror = function(){return true;}

/**
 * Extend jQuery to test for IE6 specifically
**/

$.browser.msie6 = function(){
   return ($.browser.msie && typeof document.body.style.maxHeight == 'undefined') ? true : false;
}

/**
 * Extend jQuery with custom methods
**/

$.extend({
   isIcon:function(el){
      return el.get(0).className.match(/icon/) ? true : false;
   },
   isSubNav:function(el){
      return el.get(0).className.match(/subnav/) ? true : false;
   },
   pngfix:function(el){
      if($.browser.msie6()){
         var bg = $(el).css('backgroundImage').replace(/^url\(|\)$/g,'');
         $(el).get(0).runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + bg + "',sizingMethod='scale')";      
         $(el).css({cursor:'pointer',backgroundImage:'none'});
      }
   }
});


/**
 * Site Control Object
 * This object encapsulates all functions and methods for the site
**/

var Site = {
   
   timers: Array(),   
   activePage:'home', 
    icons: Array(),
    animsCompleted:0,
   cache:Array(),
   
   /**
    * Function: init
    * @arams: string initWhat name of page to initialize
    * @return: nothing
   **/   

    init:function(initWhat){
   
      try{document.execCommand("BackgroundImageCache", false, true);}catch(e){}; // IE6 BG Image flicker Fix
   
      if (!$.browser.msie) $('body').css('opacity', 0.999999); // Firefox Opacity Flicker Fix      
      
      initWhat = initWhat.replace(/^\/|\/$/g,'');      
      
      this.activePage = initWhat;
   
        var inst = this;
      
      inst.setupLinks();      

      switch(this.activePage){
         
         case 'home':
             inst.getIconData();       
            inst.loadMain();      
         break;
         
         default:
            inst.attachMouseEvents(initWhat);
            inst.setHeadline();
            inst.setSectionHeaders();
            inst.setSubNav();
            inst.setActiveNav();
            
            $('#content-banner>img').show("drop", { direction: "left" }, 750);   
            
            if(this.activePage.match(/portal|resident/)){
               inst.setupPortalPage();            
            }   
         break;
      } 
    },

   /**
    * Function: loadMain
    * @params none
    * @return nothing
    **/

   loadMain:function(){
      
      /**
       * Render loading graphic into page
       **/
      
      var inst = this;      
       var ld = $('<img src="/images/ajax-loader2.gif" id="loader" />').css({position:'absolute',top:'45%',left:'40%',zIndex:5001});    
      $('#content').append(ld);  
      
      /**
       * Attach onload event for preloading images
      **/  

       $(window).bind('load',function(){   
          setTimeout(function(){
      
          /**
           * Loop through icons in DOM and make HTTP requests
           * for each.  This ensures that all are loaded before animation
           **/
      
             var len = inst.icons.length;
             var loader = $('#loader');
   
           if(!$.cookie('_jack_welcome')){ // Don't need a preload - we are already cached
                for(i=0;i<len;i++){
                   inst.imagePreload(inst.icons[i].src);
                inst.imagePreload(inst.icons[i].src.replace(/\.gif/,'-roll.gif'));
                }   
            }
   
           /**
            * Remove loading graphic and run effects
            **/
         
             loader.remove();
             inst.runEffects();
   
          },100);
       })
       .bind('unload',function(){
          inst = null;
       });   
   
   },

   setActiveNav:function(){
      var inst  = this;
      var me = $('#nav-'+this.activePage).find('img');
      if(me.length >0){
         var src = me.attr('src').replace(/\.gif/g,'-roll.gif');
           me.animate({opacity:.5},'slow',function(){
            me.attr('src', src);              
               me.animate({opacity:1},'slow');   
         });
      }   
   },
    
    getIconData:function(){
       
       var inst = this;         
       
       $('#hot-spots>ul>li').each(function(){
          var me = $(this);
        var img = me.find('img');
          inst.icons.push({
             id       : me.attr('id'),
             src      : img.attr('src'),
             width    : parseInt(img.css('width')),
             height   : parseInt(img.css('height')),
             top      : parseInt(me.css('top')),
             left     : parseInt(me.css('left'))
          });
       });          
      inst.icons.sort(function(){return Math.round(Math.random())-0.5});  // Sorts Randomly              
    },
    
   setHeadline:function(){
      $('.headline').each(function(){
         $(this).css({backgroundImage:'url(\'/images/headline.gif.php?style=normal&text='+escape($(this).text())+'\')'});
         $.pngfix(this);
      });      
      
      // Portal needs a custom headline      
      $('.portal .headline').css({backgroundImage:'url(\'/images/resident_portal_header.gif\')'});

   },
   setSectionHeaders:function(){
      var count = 0;
      var inst = this;
      $('.section-nav>ul>li').each(function(){
         
         count++;
         
         /**
          * Split the id at the '-' to get:
          * ar[0] = folder, ar[1] = fileprefix
          **/
         var ar = $(this).attr('id').split('-');
         var atag = $(this).find('a');
         var li = $(this);
         var header_text = $(this).find('a').text();
         var img_path = '/images/headline.gif.php?style=subnavNormal&text='+escape(header_text);
         var li_bg_path = '/images/icons/'+ar[0]+'/'+ar[1]+'.gif';
         var li_bg_path_roll = '/images/icons/'+ar[0]+'/'+ar[1]+'-roll.gif';
         var img_path_roll = '/images/headline.gif.php?style=subnavActive&text='+escape(header_text);
         
         inst.imagePreload(img_path);
         inst.imagePreload(img_path_roll);
         inst.imagePreload(li_bg_path_roll);
         
         atag.css({backgroundImage:'url(\''+img_path+'\')'});
         
         $.pngfix(atag);
         
         $('<img>').attr('src',li_bg_path_roll); // Preload roll state
         $('<img>').attr('src',img_path_roll);  // Preload roll state
         
         li
         .css({backgroundPosition:'350px 0px'})
         .animate({backgroundPosition:'-=350'},500 * count)
         .bind('click',{img:li_bg_path,source:li},Site.activateSubContent)
         .hover(
            function(){
               $(this).css({backgroundImage:'url(\''+li_bg_path_roll+'\')'});
               $(this).animate({backgroundPosition:'+=5'},100).animate({backgroundPosition:'-=5'},100);
               atag.css({backgroundImage:'url(\''+img_path_roll+'\')'});
               $.pngfix(atag);
            },
            function(){
               $(this).css({backgroundImage:'url(\'/images/icons/'+ar[0]+'/'+ar[1]+'.gif\')'});
               atag.css({backgroundImage:'url(\''+img_path+'\')'});
               $.pngfix(atag);
            }            
         );
      });
   },
   
   setSubNav:function(){

      $('#content-subnav>ul>li').each(function(){
         
         $(this).bind('click',function(e){
            e.stopPropagation();
            e.preventDefault();
            
            var id=$(this).find('a').attr('href').substr(1);
            $('#'+id).trigger('click',[$(this)]);
         });            
      });            
   },

    runEffects:function(){
       var inst = this;

       /**
        * Block interface area where icons are animating
       **/

      var content = $('#content');
      var pos = content.offset({border:false,padding:true,scroll:true,margin:true});

      $.blockUI.defaults.baseZ = 5000;
      $.blockUI.defaults.fadeOut = 100; 
       $.blockUI.defaults.overlayCSS = {  
           backgroundColor:'#000',  
           opacity:        0,
         top:pos.top+10 -document.documentElement.scrollTop,
         left:pos.left+10,
         width:content.width(),
         height:content.height()
       };
   
      $.blockUI({message:'', centerX:true,centerY:true, css: {backgroundColor:'transparent',border:'none'} });
   
      inst.attachMouseEvents();   
      
       inst.animateIcons(function(){
         if(!$.cookie('_jack_welcome')){
            $.cookie('_jack_welcome', 'complete', { expires: 1 });
            //inst.showWelcome();
            $.unblockUI();
         }else{
           $.unblockUI();   
         }         
      });  
      
      $('.icon').each(function(){
         var txt  = $(this).parent().parent().find('blockquote').text();
         if(txt ==''){
            $(this).parent().css({cursor:'default'});
         }
      });   
   
   },
  showWelcome:function(){
   
      var welcome = $('<div>').css({
      zIndex:10000,
      cursor:'pointer',
      opacity:.95,
      display:'none',
      top:330,
      left:document.documentElement.offsetWidth/2 - 400,
      width:793,
      height:228,
      position:'absolute'
      }).attr('title','Click to hide').append('<img src="/images/ready.'+($.browser.msie ? 'gif' : 'png')+'" alt="Welcome" />');
   
      $('body').append(welcome);

      welcome
      .click(function(){
         $(this)
         .stop() // Stop animations
         .hide('clip',{direction:'vertical'},500,function(){
            $(this).remove();
         });
      })
      .show('clip',{direction:'vertical'},500)
      .animate({left:'+=0'},30000).hide('clip',{direction:'vertical'},500,function(){
            $(this).remove();
         }
      );   
   
   },
    
    imagePreload:function(src){
      $.ajax({url: src, async:false, cache:true }); 
    }, 

    animateIcons:function(callback){
      var inst = this;
       var grp = $(inst.icons);

       for(i=0;i<grp.length;i++){
          inst.animateIcon(grp[i],i,callback);
       }      
   },     
    
    animateIcon:function(what, index, callback){
       var inst = this;
       var par = $('#'+what.id).parent();
       var pwid = par.width();
       var phgt = par.height();
       
       var ratio      = .1;   // Size ratio % for initial scale up
       var interval   = 250;   // Interval between image loads
       var speed      = 25;    // Animation Speed         

       inst.timers[index] = setTimeout(function(){

          var item = what;
         var nh = Math.ceil(((item.height*ratio)+item.height));
         var nw = Math.ceil(((item.width*ratio)+item.width));
        var diffy = Math.ceil((((item.height*ratio)+item.height) - item.height)/2);
        var diffx = Math.ceil((((item.width*ratio)+item.width) - item.width)/2); 

          $('#'+item.id)
         .css({height:item.height,width:item.width})
         .find('img').css({width:'100%',height:'100%'});

          $('#'+item.id)
          .animate({opacity:.85,height:nh,width:nw,top:'-='+diffy,left:'-='+diffx})
        .animate({opacity:1,height:item.height,width:item.width,top:item.top,left:item.left},function(){
         inst.animsCompleted++;
         
         if(inst.animsCompleted == inst.icons.length){
            callback.apply(inst); // Show welcome after icons are animated
         }         
         });

          },
          index * interval
       ); 
    },

   setupLinks:function(){
      
      $('a[href=\'/contact\']').bind('click',function(e){
          Site.loadPopup({
            template:'contact.partial',
            title:'Ready to get started?',
            afterRender:function(){               
               pageTracker._trackPageview('/contact');               
               $('#first-name').focus();               
               $('#sendform').bind('click',function(e){
                  Site.sendContact();
                  e.stopPropagation();
                  e.preventDefault();
                  return false;               
               });               
            }
         });
          e.stopPropagation();
          e.preventDefault();
          return false;
      });
      
      $('a[href=\'/movein/form\']').bind('click',function(e){
          Site.loadPopup({
            template:'movein.partial',
            title:'Let\'s Move!',
            afterRender:function(){               
               pageTracker._trackPageview('/movein/form');               
               $('#first-name').focus();               
               $('#sendmovein').bind('click',function(e){
                  Site.sendMovein();
                  e.stopPropagation();
                  e.preventDefault();
                  return false;               
               });               
            }
         });
          e.stopPropagation();
          e.preventDefault();
          return false;
      });
      
      $('a[href=\'/privacy\']').bind('click',function(e){
          Site.loadPopup({
            template:'privacy.partial',
            title:'Privacy Policy',
            afterRender:function(){               
               pageTracker._trackPageview('/privacy');               
               Site.setScroller($('#privacy-policy'));               
            }
         });
          e.stopPropagation();
          e.preventDefault();
          return false;         
      });
      
      $('a[href=\'/faq\']').bind('click',function(e){
          Site.loadPopup({
            template:'faq.partial',
            title:'FAQs',
            afterRender:function(){               
               pageTracker._trackPageview('/faq');               
               Site.setScroller($('#faq'));
            }
         });
          e.stopPropagation();
          e.preventDefault();
          return false;         
      });
      
      $('a[href=\'/in-the-news\']').bind('click',function(e){
          Site.loadPopup({
            template:'news.partial',
            title:'PRESS',
            afterRender:function(){               
               pageTracker._trackPageview('/in-the-news');               
               Site.setScroller($('#newstext'));
            }
         });
          e.stopPropagation();
          e.preventDefault();
          return false;         
      });
      
      $('#footer-nav>a').bind('click',function(e){
         if($(this).attr('href').match(/^http/i)){
            var pwin = window.open($(this).attr('href'));
            pageTracker._trackPageview('/external:'+$(this).attr('href').replace(/http:\/\//i,''));
            e.stopPropagation();
            e.preventDefault();
            return false;
         }
         return true;
      });      
      
   },

   attachMouseEvents:function(activePage){
   
     var activePage = typeof activePage =='undefined' ? 'home' : activePage;   

      $('#header-nav ul li img').each(function(){
         var me = $(this);
         me
            .bind('mouseover', function(e){
               me.parent().parent().css({backgroundColor: '#2d1d07'});
            })
            .bind('mouseout', function(e){
               me.parent().parent().css({backgroundColor: 'transparent'});
            });
      });

      $('img.roll').each(function(){      
         var me = $(this);

      // Ignore Current Page
      if(me.attr('src').match(activePage) && !$.isSubNav(me)){
         return;
      }
         
         // Preload Rollovers
         $('<img>').attr('src',me.attr('src').replace('.gif','-roll.gif'));
         
         // Attach Event for Rollover States
         me
            .bind('mouseover', function(e){   

              var evt = e;
               e.stopPropagation(); // Stop event from propagating to lower z-index items that overlap
               me.animate({opacity:.25},'fast',function(){   
   
              // Set Tooltip for icons
              if($.isIcon(me)){
               Site.Tooltip.remove();
               clearTimeout(Site.Tooltip.timer);
                Site.Tooltip.active = true;
                Site.Tooltip.render(evt);
              }  
            
              // Animate icon rollover
                  var src = me.attr('src').replace(/\.gif/g,'-roll.gif');
                  me.attr('src', src);              
                  me.animate({opacity:1},'slow');
               });
              }
            )
            .bind('mousemove',function(e){ // This only applies to icons
             if($.isIcon(me) && Site.Tooltip.active == true){               
               var pos = Site.Tooltip.getPos(e);               
               try{Site.Tooltip.node.stop();}catch(e){} // Stop Tooltip animation if not complete               
               if(pos) Site.Tooltip.node.animate({top:pos.top,left:pos.left});               
            }
         })
            .bind('mouseout', function(e){      
              var evt = e;
               e.stopPropagation(); // Stop event from propagating to lower z-index items that overlap
               me.stop(); // Stop animation if not complete
               me.css({opacity:1});
            
            // Animate icon rollover back
               var src = me.attr('src').replace(/-roll/g,'');
               me.attr('src', src);            

                // Hide Tooltip when mouse leaves
            if($.isIcon(me)){
                Site.Tooltip.active = false;
               Site.Tooltip.hide(evt);
            }
              }
          )
        .bind('click',function(e){         
           if($.isIcon(me) && !$.isSubNav(me)){
            if(Site.ContentBox.active) Site.ContentBox.remove();
            Site.ContentBox.render(e);
            var ico = '';
            try{
               var ico = ((($(e.target).attr('src').split(/\//)).pop()).split(/-/)).slice(0,1);
            }catch(e){}
            pageTracker._trackPageview('/home:'+ico)
           }               
          });
      });
   },
   activateSubContent:function(e,source){
      
      e.stopPropagation();
      e.preventDefault();
      
      if(typeof source == 'undefined') source = e.data.source;
      
      Site.Tooltip.remove();
      
      var me = e.target;
      while(me.nodeType==1 && !me.tagName.match(/li/i)){
         me = me.parentNode;
      }
      var li = $(me);
      var subhead_text = li.find('a').text();
      var content = li.find('blockquote>p').text();
      var inline_list = li.find('ul').clone(true);
      inline_list.addClass('subnav-copy-list');
      
      /**
       * Create a BG image path for our subheadline and change display
      **/
      $('.subhead').remove();
      $('.headline').show();
      var img_path = '/images/headline.gif.php?style=normal&text='+escape(subhead_text);
      var clone = $('.headline').hide().clone(true).addClass('subhead').css({backgroundImage:'url(\''+img_path+'\')'});
      $('.headline').before(clone);
      
      $.pngfix(clone);
      
      /**
       * Create new image from current image and append to content for 
       * move/scale effect
      **/
      var _img = $(source).find('img');
      var wid = _img.width();
      var hgt = _img.height();
      var pos = $(source).position(); // source is the calling <li> element
      var img = $('<img src="'+e.data.img.replace(/\.gif/,'-lg.gif')+'" />').css({position:'absolute',left:pos.left,top:pos.top,width:wid,height:hgt});

      /**
       * Fade our the banner as the image moves across it
      **/         
      $('#content-banner').find('img').fadeOut();
   
      /**
       * Add the image and hide the current content.
       * Then, animate the image to center of empty area and append
       * content from the current <li>
      **/
      
      $('#content-copy').css({height:'auto'}); // Give copy area an auto height
      
      $('#copy-area').find('.subnav-content, .subnav-copy-list').remove(); // Remove old data
      
      var cnt = $('#content');
      var div = $('<div>').addClass('subnav-content').text(content);
      cnt.append(img);
      
      $('#copy-area>p,#copy-area>.section-nav').hide();
      
      /**
       * Send image to middle of empty space
      **/
      img.animate({left:'690',top:'145',height:140,width:140},function(){
         var copy = $('#copy-area');
         copy.css({visibility:'hidden'}).append(div).append(inline_list); // Add content
         
         var chgt = $.browser.msie ? 290 : 340; // Offsets for vertical centering
         var sub = $('.subhead');
         sub.css({marginTop: (chgt - copy.height())/2}) // Move top margin of subheader to center content
         $.browser.msie6() ? sub.show() : sub.fadeIn(); // Show the new header, fadeIn() for IE6 is ugly         
         
         copy.css({visibility:'visible'}); // Make the Copy area visible again since we moved it         
         $.browser.msie6() ? copy.show() : copy.fadeIn(); // Show the new copy, fadeIn() for IE6 is ugly   
         
         /**
          * Move the subnavigation above the header (only once or we get an annoying flicker)
         **/         
         if(sub.prev().attr('id') != 'content-subnav'){
            sub.before($('#content-subnav'));
         }
      });

      /**
       * Show sub nav
      **/   
      
      $('#subnav-'+Site.activePage).fadeIn();
   
   },
   loadPopup:function(settings){

      $.blockUI.defaults.baseZ = 10000;
      $.blockUI.defaults.fadeOut = 100; 
       $.blockUI.defaults.overlayCSS = {  
           backgroundColor:'#000',  
           opacity:        0.15,
          textAlign: 'left'
       };      
             
      var div = $('<div id="popup"><div class="top"></div><div class="middle"><img title="Close" src="/images/headline.gif.php?style=small&text=x" class="close" /></div><div class="bottom"></div>');
      var wid = $(window).width();
      var hgt = $(window).height();
      
      /**
       * Run ajax call to get the content
      **/
      
      $.get('/_tmpl/'+settings.template+'.html', function(data){
                  
         div.find('.middle').append('<img id="popup-title" src="/images/headline.gif.php?style=popupHeader&amp;text='+escape(settings.title)+'" />').append($(data));
         div
         .find('.close').click(function(){
            div.remove(); 
            $.unblockUI();
         });         

         $.blockUI({message:div,  css: {position:'absolute',top:'-1000px',left:wid/2-335,width:'670px',textAlign:'left',backgroundColor:'transparent',border:'none'} });
         
         $('.blockMsg').animate({top:(hgt/2 - div.height()/2)});   
         
         settings.afterRender.apply(div,[div]);                  
   
      });            
   },
   sendContact:function(){
      
      var err = 0;
      
      var noemptyFields = Array(
         'email',
         'inquiry'         
      );
      
      for(i=0;i<noemptyFields.length;i++){
         var fld = $('#'+noemptyFields[i]);
         if(typeof fld.get(0).lastColor == 'undefined') fld.get(0).lastColor = fld.css('color');
         if(typeof fld.get(0).lastBorder == 'undefined') fld.get(0).lastBorder = fld.css('border');
         if(fld.val() == '' || fld.val() == 'Field cannot be blank'){
            fld
            .css({border:'1px solid #9F0016',color:'#9F0016'})
            .val('Field cannot be blank')
            .focus(function(){
               $(this).val('').css({color:fld.get(0).lastColor})
            });
            err++;            
         }else{
            fld.css({border:fld.get(0).lastBorder,color:fld.get(0).lastColor});
         }      
      }
      
      if(err == 0){

         var params = 'ajax=submitcontact&'+$('#contact-form').find('form').serialize();

         $.ajax({
            type   : "POST",
            url      : "/lib/ajax.php",
            dataType: 'text',
            async   : true,
            data   : params,
            success: function(updated){

               if(updated == 'SUCCESS'){            

                  pageTracker._trackPageview('/thanks.html');

                  document.write = function(s){
                     $(document.body).append(s);
                  }

                  var scr = $('<scr'+'ipt>');
                  scr.attr({ type:"text/javascript", src:"http://www.googleadservices.com/pagead/conversion.js"});
                  $(document.body).append(scr);

                  $('#popup-title').remove();
                  $('#contact-form').html('<div id="thanks"><img src="/images/movein-thanks.gif" alt="Thanks" width="516 height="73" /></div>');
                  
                  setTimeout(function(){
                     $('#popup').remove();
                     $.unblockUI();
                  },10000);

               }else{
                  alert('An error occurred submitting the form.  Please contact webmaster for support.\n' + updated);
               }
            }
         });               
      }      
   },
   sendMovein:function(){
      
      var err = 0;
      
      var noemptyFields = Array(
         'email',
         'inquiry'         
      );
      
      for(i=0;i<noemptyFields.length;i++){
         var fld = $('#'+noemptyFields[i]);
         if(typeof fld.get(0).lastColor == 'undefined') fld.get(0).lastColor = fld.css('color');
         if(typeof fld.get(0).lastBorder == 'undefined') fld.get(0).lastBorder = fld.css('border');
         if(fld.val() == '' || fld.val() == 'Field cannot be blank'){
            fld
            .css({border:'1px solid #9F0016',color:'#9F0016'})
            .val('Field cannot be blank')
            .focus(function(){
               $(this).val('').css({color:fld.get(0).lastColor})
            });
            err++;            
         }else{
            fld.css({border:fld.get(0).lastBorder,color:fld.get(0).lastColor});
         }      
      }
      
      if(err == 0){

         var params = 'ajax=submitmovein&'+$('#contact-form').find('form').serialize();

         $.ajax({
            type   : "POST",
            url      : "/lib/ajax.php",
            dataType: 'text',
            async   : true,
            data   : params,
            success: function(updated){

               if(updated == 'SUCCESS'){            

                  pageTracker._trackPageview('/movein/thanks.html');

                  $('#popup-title').remove();
                  $('#contact-form').html('<div id="thanks"><img src="/images/contact-thanks.gif" alt="Thanks" width="516 height="73" /></div>');
                  
                  setTimeout(function(){
                     $('#popup').remove();
                     $.unblockUI();
                  },10000);

               }else{
                  alert('An error occurred submitting the form.  Please contact webmaster for support.\n' + updated);
               }
            }
         });               
      }      
   },
   setupPortalPage:function(){
      
        $('#popouts>li')
           .bind('mouseover', function(e){   

            var evt = e;
            e.stopPropagation(); // Stop event from propagating to lower z-index items that overlap

            Site.Tooltip.remove();
            clearTimeout(Site.Tooltip.timer);
            Site.Tooltip.active = true;
            Site.Tooltip.render(evt);

             }
           )
           .bind('mousemove',function(e){ 
             if(Site.Tooltip.active == true){               
               var pos = Site.Tooltip.getPos(e);               
               try{Site.Tooltip.node.stop();}catch(e){} // Stop Tooltip animation if not complete               
               if(pos) Site.Tooltip.node.animate({top:pos.top,left:pos.left});               
            }
         })
           .bind('mouseout', function(e){   

            var evt = e;
            e.stopPropagation(); // Stop event from propagating to lower z-index items that overlap
            $(this).stop(); // Stop animation if not complete

            Site.Tooltip.active = false;
            Site.Tooltip.hide(evt);            
             }
         );      
   },
   setScroller:function(element){
      
      var element = element;
      
      var div = '<div id="scroll-more"><img src="/images/box-arrow-down.gif" id="scroll-arrow" alt="More" /></div>';
      
       $(element).after($(div));
      
      $('#scroll-arrow').mousedown(function(){
            var p = $(element);
            var t = p.get(0).scrollTop;                  
            var nt = t+100;   
            var img = $('#scroll-more>img');   

            if(img.css({cursor:'pointer'}).attr('src').match('up')){
               p.animate({scrollTop:0},function(){
                  img.attr({src:'/images/box-arrow-down.gif'});
               });
               return      
            }
      
            if(p.height() - nt < 100) {
               nt = p.height();
               p.animate({scrollTop:nt},function(){;
                  img.attr({src:'/images/box-arrow-up.gif'});   
            });
            }else{
               p.animate({scrollTop:nt},function(){;      
                  img.attr({src:'/images/box-arrow-down.gif'});
              });
            }   
      });      
   }
};
