﻿var PropertyDetail = new Class ({
    options : {},
    initialize : function ( options ) {
        this.setOptions(options);
		// create the image scroller
		if ( $('tile') ) {
			var scroll = new Scroller('tile', {
				link: 'cancel',
				duration: 2500,
				offset: {'x': -200, 'y': 0}, // some padding
				transition: Fx.Transitions.Quad.easeInOut
			});
			scroll.start(); 
		}
		if ($('paymentcalculator'))
		{
			this.validator = new FormValidator('paymentcalculator');
        }
 
		SqueezeBox.assign($$('a[rel=boxed]'));
		SqueezeBox.assign($$('a[rel=rboxed]'),{
			handler: 'iframe',
			size:{x:840,y:550}
		});
		
		this.thumbnails = $$('#property-thumbnails a img')
        this.thumbnails.each( function (el) {
            el.addEvent('click', function (e) {
                this.onPropertyImageClick(e,el);
            }.bind(this));
        }.bind(this));
        // hook up the mortgage calculator 
        
        if ( $('downpayment') ) {
            $('downpayment').addEvent('blur',function (e) {
                this.calculatePayment();
            }.bind(this));
            $('downpayment').addEvent('keyup',function (e) {
                if ( e.keyCode == 13 )
                    var e = new Event(e).stop();
                this.calculatePayment();
            }.bind(this));
            $('downpayment').addEvent('keypress',function (e) {
                if ( e.keyCode == 13 )
                    var e = new Event(e).stop();
                //this.calculatePayment();
            }.bind(this));
        }
        if ( $('rate') ) {
            $('rate').addEvent('blur',function (e) {
                this.calculatePayment();
            }.bind(this));
            $('rate').addEvent('keyup',function (e) {
                if ( e.keyCode == 13 )
                    var e = new Event(e).stop();
                this.calculatePayment();
            }.bind(this));
            $('rate').addEvent('keypress',function (e) {
                if ( e.keyCode == 13 )
                    var e = new Event(e).stop();
                //this.calculatePayment();
            }.bind(this));
        }
        if ( $('year') ) {
            $('year').addEvent('change',function (e) {
                this.calculatePayment();
            }.bind(this));
        }
        this.calculatePayment();
	    
	    if ( $('property-thumbnails') )
	    {
	        var images = $$('#property-thumbnails li');
	        if ( images.length > 0)
	        {
	            //$('tile-inner').setStyle('width',(images.length + 1) * images[0].getComputedSize().totalWidth);
	        }
	    }

	    
        if ( $('lnkBack') )
        {
            $('lnkBack').addEvent('click', function (e) {
				history.back();
            });
        }
    },




    calculatePayment : function() {
    
    if ($('listprice'))
       {
            var P = $('price').get('value').toFloat() 
            var dp = $('downpayment').get('value').toFloat();
            var n = $('year').get('value').toFloat();
            var i = $('rate').get('value').toFloat() / 100;   
            
            if ( ! isNaN(dp) ) P = P - dp;
            if ( isNaN(i) ) i = .06;
            i = i / 12; // monthly interest rate
            n = n * 12; // term in months
            var numerator = P * (i * Math.pow((1 + i),n));
            var denominator = Math.pow(1 + i,n) - 1;
            var result = numerator / denominator;
            //var result = (principal * Math.pow((1 + rate),(year * 12)))/ ( Math.pow((1+rate),(year * 12)) -1 );
           
            $('payment').set('text','$' + result.round(2));
       }
    },
    
    onPropertyImageClick : function(e,ctl) {
        var e = new Event(e).stop();
        // get the image.. follow the url for the a tag.
        var url = ctl.getProperty('href');
        $('propertyimage').set('src',ctl.get('src'));
        $('propertyimage').getParent().set('href',ctl.get('src'));
    }
});

PropertyDetail.implement(new Options, new Events);
