
var ViewPropertyController = Class.create({
  
  initialize:function()
  {
    this.initGallery();
    this.initScrollableThumbs();
  },
  
  initGallery:function()
  {
    var i = 0;
    var thumbEl;
    $$('#photo-gallery a').each(function(thumbEl) {
      thumbEl.observe('click', function(e) {
        e.stop();
        
        $('photo-big').src = thumbEl.href;
      });
    }.bind(this));
  },
  
  initScrollableThumbs:function()
  {
    // setup scroll left/right buttons
    $('scroll-left').observe('click', this.scrollLeftClicked.bind(this));
    $('scroll-right').observe('click', this.scrollRightClicked.bind(this));
    
    // init animation
    this.propertiesScrollLeft = 0;
    this.propertiesScrollTimer = false;
    this.propertiesScrollMaxTime = 300;
    this.propertiesScrollEl = document.getElementById('properties-wrapper');
    this.propertiesScrollMin = 0;
    var firstPropertyEl = $('properties').firstDescendant();
    if (!firstPropertyEl) {
      this.propertiesScrollMax = 0;
    } else {
      var firstPropertyElWidth = firstPropertyEl.getWidth();
      firstPropertyElWidth += parseInt(firstPropertyEl.getStyle('margin-right').replace(/[^0-9-]/g, ''));
      
      this.propertiesScrollMax = $('properties').childElements().length * firstPropertyElWidth;
      this.propertiesScrollMax -= $('properties-wrapper').getWidth();
    }
    
    // scroll to current property
    $$('#properties a').each(function (propertyEl) {
      if (propertyEl.hasClassName('current')) {
        this.scrollToPropertyEl(propertyEl, false);
        
        if (this.readCookie('property-scroll-down-on-load')) {
          this.pageScrollTo(this.readCookie('property-scroll-down-on-load'), false);
          this.eraseCookie('property-scroll-down-on-load');
        }
      }
    }.bind(this));
    
    // setup scrolling onclick (this way the scroll should have started, probably finished, by the time the browser starts rending new page)
    $$('#properties a').each(function (propertyEl) {
      propertyEl.observe('click', function(clickEvent) {
        this.scrollToPropertyEl(propertyEl, true);
        this.pageScrollTo($('properties').cumulativeOffset().top - 10, true);
        this.createCookie('property-scroll-down-on-load', this.pageScrollTop, 0);
      }.bind(this));
    }.bind(this));
    $$('a.prev-property, a.next-property').each(function (linkEl) {
      linkEl.observe('click', function(clickEvent) {
        this.pageScrollTo($('properties').cumulativeOffset().top - 10, true);
        this.createCookie('property-scroll-down-on-load', this.pageScrollTop, 0);
      }.bind(this));
    }.bind(this));
  },
  
  scrollLeftClicked:function(e)
  {
    e.stop();
    this.propertyScrollTo(-500, true);
  },
  
  scrollRightClicked:function(e)
  {
    e.stop();
    this.propertyScrollTo(500, true);
  },
  
  scrollToPropertyEl:function(propertyEl, animate)
  {
    var currentPropertyXOffset = propertyEl.positionedOffset()[0] + (propertyEl.getWidth() / 2);
    var scrollTo = currentPropertyXOffset - ($('properties-wrapper').getWidth() / 2);
    this.propertyScrollTo(scrollTo - this.propertiesScrollLeft, animate);
  },
  
  propertyScrollTo:function(scrollDelta, animate)
  {
    this.propertiesScrollLeft += scrollDelta;
    this.propertiesScrollAnimStart = new Date().getTime();
    this.propertiesScrollAnimStartLeft = this.propertiesScrollEl.scrollLeft;
    
    if (this.propertiesScrollLeft <= this.propertiesScrollMin) {
      this.propertiesScrollLeft = this.propertiesScrollMin;
      $('scroll-left').hide();
    } else {
      $('scroll-left').show();
    }
    
    if (this.propertiesScrollLeft >= this.propertiesScrollMax) {
      this.propertiesScrollLeft = this.propertiesScrollMax;
      $('scroll-right').hide();
    } else {
      $('scroll-right').show();
    }
    
    if (!animate) {
      this.propertiesScrollEl.scrollLeft = this.propertiesScrollLeft;
      return;
    }
    
    if (!this.propertiesScrollTimer) {
      this.propertiesScrollTimer = setInterval(this.propertyScrollAnimateStep.bind(this), 15);
    }
  },
  
  propertyScrollAnimateStep:function()
  {
    var elapsedTime = new Date().getTime() - this.propertiesScrollAnimStart;
    var animDelta = Math.sin(elapsedTime /  this.propertiesScrollMaxTime);
    
    if (animDelta <= 0.99) {
      this.propertiesScrollEl.scrollLeft = this.propertiesScrollAnimStartLeft + ((this.propertiesScrollLeft - this.propertiesScrollAnimStartLeft) * animDelta);
    } else {
      clearInterval(this.propertiesScrollTimer);
      this.propertiesScrollTimer = false;
      this.propertiesScrollEl.scrollLeft = this.propertiesScrollLeft;
    }
  },
  
  pageScrollTo:function(scrollTop, animate)
  {
    this.pageScrollTop = scrollTop;
    this.pageScrollAnimStart = new Date().getTime();
    this.pageScrollAnimStartTop = document.viewport.getScrollOffsets().top;
    
    if (this.pageScrollTop <= this.pageScrollAnimStartTop) {
      this.pageScrollTop = this.pageScrollAnimStartTop;
      return;
    }
    
    if (!animate) {
       window.scrollBy(0, this.pageScrollTop - document.viewport.getScrollOffsets().top);
      return;
    }
    
    if (!this.pageScrollTimer) {
      this.pageScrollTimer = setInterval(this.pageScrollAnimateStep.bind(this), 15);
    }
  },
  
  pageScrollAnimateStep:function()
  {
    var elapsedTime = new Date().getTime() - this.pageScrollAnimStart;
    var animDelta = Math.sin(elapsedTime /  this.propertiesScrollMaxTime);
    
    if (animDelta <= 0.99) {
      var offset = (this.pageScrollAnimStartTop + ((this.pageScrollTop - this.pageScrollAnimStartTop) * animDelta)) - document.viewport.getScrollOffsets().top;
      window.scrollBy(0, offset);
    } else {
      clearInterval(this.pageScrollTimer);
      this.pageScrollTimer = false;
      window.scrollBy(0, this.pageScrollTop - document.viewport.getScrollOffsets().top);
    }
  },
  
  createCookie:function(name,value,days) {
  	if (days) {
  		var date = new Date();
  		date.setTime(date.getTime()+(days*24*60*60*1000));
  		var expires = "; expires="+date.toGMTString();
  	}
  	else var expires = "";
  	document.cookie = name+"="+value+expires+"; path=/";
  },
  
  readCookie:function(name) {
  	var nameEQ = name + "=";
  	var ca = document.cookie.split(';');
  	for(var i=0;i < ca.length;i++) {
  		var c = ca[i];
  		while (c.charAt(0)==' ') c = c.substring(1,c.length);
  		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  	}
  	return null;
  },
  
  eraseCookie:function(name) {
  	this.createCookie(name,"",-1);
  }
});

var viewPropertyController = false;
document.observe('dom:loaded', function() {
  viewPropertyController = new ViewPropertyController();
});
