/**
 * Auto A2Z
 * 
 * Presents the JavaScript framework for the Auto A2Z application
 * 
 * @author Ollie Maitland
 * @copyright Byng Systems LLP
 */
var AutoApp = new Class(
{
	
	/**
	 * Holds meta data on this application
	 * 
	 */
	meta	: {
		'name'	    : 'Auto A2Z',
		'version'   : '0.1.0',
		'author'    : 'ollie@byng-systems.com',
		'copyright' : 'Byng Systems LLP',
		'date'  	: '2008-04-07'
	},
	
	icons : {
		'loader' : '/html/images/icons/ajax-loader.gif'
	},
	
	getUi : function ()
	{
		if (!this.ui) this.ui = new AutoUi();
		return this.ui;
	},
	
	loadEnvironment : function ()
	{	
		this.addEvent('alert', function (msg, msgType) {			
			alert (msgType + ': '+ msg);
		});
		
		
		if ($('az-search')) {
			// create the search behaviour
			this.search = new AutoSearch($('az-search'));
		}

	
		$$('.az-search_tab li a').addEvent('click', function(e) {
			e.stop();
			var searchType = e.target.getProperty('accesskey');
			e.target.getParent('ul').getElements('li').removeClass('current');
			e.target.getParent('li').addClass('current');	
			document.getElement('#az-search input.search_type').set('value', searchType);
			document.getElement('.az-search .az-pane').setProperty('class', 'az-pane').addClass('az-search_type_' + searchType);
		}.bind(this));
		
		if ($('features')) {
			// add the features rotation
			var ui = new AutoUi();
			ui.features($('features'));
		};

		if ($('az-valuation')) {
			// create the search behaviour
			this.search = new AutoSearch($('az-valuation'));
		};
		
		// attach behaviour to car of the day pod
		var cotd = $('az-cotd_container');
		if (cotd) {
			this.getUi().cotd( cotd );
		}
	
		// locate the postcode popup	
		var popup = $('az-popup_postcode');
		if (popup) {
			// get the links that require a postcode			
			document.getElements('.az-require_postcode').addEvent('click', function(e){
				e.stop();
			
				var anchor = ($(e.target).get('tag') == 'a' ? $(e.target) : $(e.target).getParent('a'));
				
				popup.getElement('h2').set('text', anchor.getProperty('title'));					
			
				if (popup.getStyle('display') == 'block') {
					return popup.setStyle('display', 'none');
				} else {
					var pos = anchor.getCoordinates($('az-container'));
					popup.setStyles({left: pos.right, top: pos.bottom}).setStyle('display', 'block');
					popup.getElement('form input[type=text]').focus();
				}
				
				// set active link on the form
				popup.store('destination', anchor);
			});
			
			// override the form event
			popup.getElement('form').addEvent('submit', function(e){
				e.stop();
				var anchor = popup.retrieve('destination');
				var postcode = $(e.target).getElement('input[type=text]').get('value');
				if (postcode == '') {
					alert('Please enter a postcode')
				} else {
					window.location.href = (anchor.getProperty('href') + '&crit[postcode]=' + postcode);
				}
			});
			
			// attach the popup close behaviour
			$('az-popup_postcode_close').addEvent('click', function(){
				popup.setStyle('display', 'none');
			});
		}
		
		// add tool tips to .tip
		$$('a.tips').each(function(element,index) {
				     var content = element.get('title').split('::');
				     element.store('tip:title', content[0]);
				     element.store('tip:text', content[1]);
		  });
		
		var ToolTips = new Tips($$('.tips'),{'offsets' : {'y' : -25, 'x' : -220}, 'showDelay' : 300, 'hideDelay' : 10});
		
		if ($('yoursearchresults')) {
			if ($('basicpane')) {
			$('basicpane').addClass('hide');			
			$('advancepane').setStyle('display', 'block');	
			$$('.az-search').addClass('az-blank_bg');
			};
		};
		
		if ($('az-close_error')) {
			 $('az-close_error').addEvent('click', function(){
				$$('.az-popup_error').setStyle('display', 'none');					
			 });
		}
		
		if ($('az-advertisers')) {
			var boxes = $('az-advertisers').getElements('.az-box');
			var newBoxes = [];
			boxes.each(function(box) {
				newBoxes.push(box.clone());
			});
			// sort randomly
			newBoxes.sort(function (){ return (Math.round(Math.random())-0.5); } );
			boxes.each(function (dest, key) {
				var el = newBoxes[key].replaces(dest);
				if (key % 2) el.addClass('az-box_dark');
				else el.removeClass('az-box_dark').addClass('az-box_light');
			});
		}
	},
	
	submitPlateQuery : function (loc)
	{
		term = document.getElementById('regno').value.toUpperCase();
		term = term.replace(' ', '+');
		loc = 'http://www.theprivateplatecompany.co.uk/private-plate/search/' + term
		document.location = loc;
	},
	
	addSearchParam : function (input)
	{
		window.location = window.location + '&' + input.getAttribute('name') + '=' + input.value;
	}
	
});

// implement ByngApp methods on AutoApp
AutoApp.implement(ByngApp);

// Set the Auto A2Z application
Byng.setApp( new AutoApp );

// Set the input handler
Byng.setInput( new ByngInput );

// Set the input handler
Byng.setTransit( new ByngTransit );

// load the Byng User Interface object
Byng.setUi ( new ByngUI( UI_FLAG_AJAX ) );

// load the environment when loaded
window.addEvent('domready', Byng.app.loadEnvironment.bind(Byng.app));