
RadioReplace = new Class({
    initialize: function(){	
        this.list = $$('#ulSMkt li');
        var rep = new ReplaceRadio(this.list, 'li');
        this.divs = $$('div.dRdo');
        this.divs.each(function(div){ rep = new ReplaceRadio(div, 'div'); }.bind(this));
    }
});	

ReplaceRadio = new Class({
	initialize: function(els, type){	
		this.lastradio = null;		
		if(type=='li'){
			els.each(function(li){		
			    if (li.id == ''){
				    var lbl = li.getElement('label');var rdo = lbl.getElement('input');    			
				    rdo.setStyle('position', 'absolute');rdo.setStyle('left', '-50px');
				    if(rdo.checked){lbl.addClass('lChk');this.lastradio = lbl;}
				    else lbl.addClass('lUnchk');    				
				    rdo.addEvent('focus', this.focussed.bind(this, lbl));					
			    }
		    }.bind(this));
		} else if(type=='div'){ 
		    els.getElements('label').each(function(lbl){			
			    var rdo = lbl.getElement('input');		
			    rdo.setStyle('position', 'absolute');rdo.setStyle('left', '-50px');
			    if(rdo.checked){lbl.addClass('lChk');this.lastradio = lbl;}
			    else lbl.addClass('lUnchk');				
			    rdo.addEvent('focus', this.focussed.bind(this, lbl));
		    }.bind(this));				    
		}		
	},	
	focussed: function(lbl){
		if($defined(this.lastradio)){this.lastradio.removeClass('lChk');this.lastradio.addClass('lUnchk');}
		lbl.removeClass('lUnchk');lbl.addClass('lChk');this.lastradio = lbl;
	}	
});	

SearchSwitch = new Class({
	initialize: function(){	
	    this.hidden = $E('input', 'dSD1');this.min = $E('select', 'dSD3');this.max = $E('select', 'dSD4');
	    this.buy = $('aTobuy');this.let = $('aTolet');this.comm = $('aTocomm');this.over = $('aToover');
	    this.iscomm = $E('input', 'dSD2');
	    this.isover = $E('input', 'dSD3');
	    
	    this.minRents = ['100', '500','750','1,000','1,250','1,500','2,000','2,500','3,000','3,500','4,000','5,000'];
	    this.maxRents = ['750','1,000','1,250','1,500','2,000','2,500','3,000','3,500','4,000','5,000', '0'];
	  
	    this.minBuys = ['100,000','150,000','200,000','250,000','300,000','350,000','400,000','450,000','500,000','550,000','600,000','650,000','700,000','750,000','800,000','900,000','1,000,000','1,100,000','1,200,000','1,300,000','1,400,000','1,500,000','2,000,000']; 
	    this.maxBuys = ['150,000','200,000','250,000','300,000','350,000','400,000','450,000','500,000','550,000','600,000','650,000','700,000','750,000','800,000','900,000','1,000,000','1,100,000','1,200,000','1,300,000','1,400,000','1,500,000','2,000,000','2,500,000','5,000,000'];
	    	    
	    if(this.iscomm.value.toInt() == 0 && this.isover.value.toInt() == 0)
	    {
	        if(this.hidden.value.toInt() == 0) this.buy.addClass('aSel');
	        else this.setlet();
	    }
	    else
	    {
	       if(this.isover.value.toInt() != 0) this.over.addClass('aSel');
	       else this.comm.addClass('aSel');
	       
	       this.buy.removeClass('aSel');
	       this.let.removeClass('aSel');
	       this.hidden.value = 0; 
	    }
	    //this.buy.addEvent('click', function(e){
	    //    e = new Event(e).stop(); if(!this.buy.hasClass('aSel')) this.setbuy();
        //}.bind(this));
        
	    //this.let.addEvent('click',function(e) {
	    //    e = new Event(e).stop(); if(!this.let.hasClass('aSel')) this.setlet();
        //}.bind(this));
	},
	
	setlet: function(){
	    this.let.addClass('aSel');this.buy.removeClass('aSel');this.hidden.value = 1;
	    $$('#dSD3 select option').each(function(opt){opt.remove();});	
	    $$('#dSD4 select option').each(function(opt){opt.remove();});	
		var option = new Element('option').setProperty('value', '0').setProperty('selected', 'true').setHTML('Min. Monthly Rent'); 
        this.min.adopt(option);
        this.minRents.each(function(val){
	        option = new Element('option').setProperty('value', val).setHTML('&#163;' + val + ' pcm'); 
	        this.min.adopt(option);
		}.bind(this));	
		var option = new Element('option').setProperty('value', '0').setProperty('selected', 'true').setHTML('Max. Monthly Rent'); 
        this.max.adopt(option);
        this.maxRents.each(function(val){
	        if(val.toInt() > 0) option = new Element('option').setProperty('value', val).setHTML('&#163;' + val + ' pcm'); 
	        else option = new Element('option').setProperty('value', val).setHTML('&#163;5,000+ pcm'); 
	        this.max.adopt(option);
		}.bind(this));
	},
	
	setbuy: function(){
	    this.buy.addClass('aSel');this.let.removeClass('aSel');this.hidden.value = 0;
	    var mo = $$('#dSD3 select option');
	    if(mo[0].getText() != 'No Minimum')
	    {
	        mo.each(function(opt){opt.remove();});	
	        $$('#dSD4 select option').each(function(opt){opt.remove();});	
		    var option = new Element('option').setProperty('value', '0').setProperty('selected', 'true').setHTML('No Minimum'); 
            this.min.adopt(option);
            this.minBuys.each(function(val){
	            option = new Element('option').setProperty('value', val).setHTML('&#163;' + val); 
	            this.min.adopt(option);
		    }.bind(this));	
		    var option = new Element('option').setProperty('value', '0').setProperty('selected', 'true').setHTML('No Maximum'); 
            this.max.adopt(option);
            this.maxBuys.each(function(val){
	            option = new Element('option').setProperty('value', val).setHTML('&#163;' + val); 
	            this.max.adopt(option);
		    }.bind(this));
		}
	}
});	

ImageGallery = new Class({
	initialize: function(){	
	    this.holder = $('tdPImg');
	    if($defined(this.holder)){
	        this.iholder = $('dvGlImg');
		    this.thumbs = $$('div.dvGit a');
		    this.vis = -1;
    		
		    var loader = new Asset.image('/App_Themes/Alpha/Images/loader.gif', 
		        {id: 'iLoader', title:'Loading...', width:'32px', height:'32px'}).setStyle('border','none').setStyle('margin-top','160px');		
		    this.overlay = new Element("div",{ "id": "dvOverlay" }).adopt(loader);
	        this.overlay.injectTop(this.holder);
    	    
	        this.fx = {
			    overlay: this.overlay.effect('opacity', {duration: 400}).hide(),
			    outlay: this.overlay.effect('opacity', {duration: 300})
		    };
    				       
		    this.thumbs.each(function(a, index){	
			    a.addEvent('click', function(e){
	                e = new Event(e).stop(); 
	                this.getImage(index, a.getProperty('href'));
                }.bind(this));
		    }.bind(this));
		}
	},
	
	getImage: function(index, href){
	    if($defined(this['gal'+index])) this.showImage(index);
	    else
	    {
	        this.overlay.setStyle('height', (this.iholder.getElement('img').getSize().size.y -8));
	        this.fx.overlay.start(0, 0.7);
	        this.loadex = index;
	        var lCallback = this.gotImage.bind(this);
	        this['gal'+index] = new Asset.image(href, { onload:lCallback });
	    }	    	    
	},
	
	gotImage: function()
	{
	    this.showImage.delay(800, this, this.loadex);
	},
	
	showImage: function(index){
	    if(this.vis != index){	    
    	    this.vis = index;
	        this.fx.overlay.hide();	
	        this.iholder.getElement('img').remove();
	        this['gal'+index].injectTop(this.iholder);
	   }
	},
	
	doEffect: function(){
	    alert(1);
	}
	
});	

FormSwap = new Class({
    initialize: function(controls, divs){	//uses radios, only 1 allowed selected
        this.controls = controls;
        this.divs = divs;
        this.active = null;
                
        this.controls.each(function(input, index){	
            this.divs[index].oH = this.divs[index].getStyle('height');
            if(!input.checked) {this.divs[index].setStyle('display','none');this.divs[index].setStyle('height',0);}
            else this.active = index;
		    input.addEvent('click', function(){
                this.swap(index);
            }.bind(this));
	    }.bind(this));
    },
    swap: function(index){
        if(this.controls[index].checked){
            this.divs[this.active].effect('height').start(0);
            this.divs[index].setStyle('display','block');
            this.divs[index].effect('height').start(this.divs[index].oH);
            this.active = index;
        }
    }
});	

window.addEvent('domready', function(){
	var rdoRep = new RadioReplace();
	var srchSw = new SearchSwitch();
	var imGal = new ImageGallery();
});
