/**
*	File: hills_ford.js
*
*	Author: Matthew Lindley, GForces Web Management: www.gforces.co.uk
*	Purpose: House most of the custom javascript for the Hills Ford website
*
*/


/**
*	Stuff we always want to do onload
*/
document.observe( 'dom:loaded', function () {

	modelSearch.init('usedVehiclesType','usedVehiclesTypeInput');

});



/****************************************
*	Global stuff
****************************************/
Element.addMethods( 'SELECT', {

	addOption: function ( el, text, value, index ) {
		index = index || $(el).options.length;
		$(el).options[ index ] = new Option( text, value );
		return el;
	},

	empty: function ( el, offset ) {
		offset = parseInt( offset ) || 0;
		while ( el.options.length > offset ) {
			el.options[ el.options.length - 1 ] = null;
		}
		return el;
	},

	populate: function ( el, options ) {
		options.data.each( function ( option ) {
			option = $H( option );
			$(el).addOption( option.get( options.textName ), option.get( options.valueName ) );
		});
	}

});

Element.addMethods({

 

 addTbody: function ( el ) {

 

  el.immediateDescendants().wrap( new Element( 'tbody' ) );

 

 },

 

 stripe: function ( el, className ) {

 

  switch ( el.tagName.toLowerCase() ) {

 

   case 'table':

 

    tbody = el.immediateDescendants().find( function ( child ) {

     return child.tagName.toLowerCase() == 'tbody';

    });

 

    if ( tbody == false ) {

     el.addTbody();

    }

 

   break;

 

   case 'tbody':

    tbody = el;

   break;

 

   default:

    return false;

 

  }

 

  tbody.immediateDescendants().invoke( 'removeClassName', className ).each( function ( row, index ) {

   if ( index % 2 == 0 ) {

    row.addClassName( className );

   }

  });

 

 }

});





//Search Marque Model stuff

    var modelSearch = {

		init: function (element,field) {
			
			this.divCarImages = element;
			
		    //  Toggle actions for vehicle pictures
			$$('#' + this.divCarImages + ' a').invoke( 'observe', 'click', function ( ev ) {
	
				Event.stop( ev );		//  Stop link working
	
				el = ev.element();		//  Reference element
				el.toggleClassName( 'active' );		//  Add/remove active class
	
				$(field).value = getCarTypes(this.divCarImages);
				//getMakes();
	
			}.bind( this ) );
	
		}

    }
    

    
	function getCarTypes(divCarImages) {

		//  Get the rel property of all car image links with the class active
		return $$('#' + divCarImages + ' a').findAll( function ( el ) {
			return el.hasClassName( 'active' );
		}).pluck( 'rel' ).join( ',' );

	}
    
   function getMakes() {
        //marqueDetailId

        var target = baseHref + 'ajax.php';
        var params = 'type=getMakes&vehicleType=' + $F('usedVehiclesTypeInput');

        var myAjax = new Ajax.Request(target,
        {
            method: 'post',
            parameters: params,
            onSuccess: function (request) {

                var i;
                for(i = $('marqueDetailId').options.length - 1 ; i >= 0 ; i-- ) {
                    $('marqueDetailId').remove(i);
                }

                var outText = request.responseText;
                outText = outText.split(',');

                if ( outText.length > 0 ) {
                    for (i = 0 ; i < outText.length ; i++) {
						bits = outText[i].split(':');
                        //alert(outText[i]);
                        $('marqueDetailId').options[ $('marqueDetailId').options.length ] = new Option( bits[1], bits[0] );

                    }
                }
                
                getModels();

            },

            onFailure: function(request) {
                alert( 'request failed, please try again' );
            }
        });
    }
    
    function getModels() {
        //marqueDetailId

        var target = baseHref + 'ajax.php';
        var params = 'type=getMarqueModels&id=' + $('marqueDetailId').options[$('marqueDetailId').selectedIndex].value + '&vehicleType='+ $F('usedVehiclesTypeInput'); 

        var myAjax = new Ajax.Request(target,
        {
            method: 'post',
            parameters: params,
            onSuccess: function (request) {

                var i;
                for(i = $('model').options.length - 1 ; i >= 0 ; i-- ) {
                    $('model').remove(i);
                }

                var outText = request.responseText;
                outText = outText.split(',');

                if ( outText.length > 0 ) {
                    for (i = 0 ; i < outText.length ; i++) {

                        //alert(outText[i]);
                        $('model').options[ $('model').options.length ] = new Option( outText[i], outText[i] );

                    }
                }

            },

            onFailure: function(request) {
                alert( 'request failed, please try again' );
            }
        });
    }
 