var _garage = window.IPBoard;

_garage.prototype.garage = {
	
	init: function()
	{
		Debug.write("Initializing garage.js");
		
		document.observe("dom:loaded", function(){
			ipb.garage.initEvents();			
		
		});
	},
	initEvents: function()
	{        
		ipb.delegate.register(".vehicle_share", ipb.garage.viewShareBox);
        
        if( $('make_id') )
        {
            $('make_id').observe('change', ipb.garage.changeMake);    
        }		
		
  		$$('.tab_toggle').each( function(elem){
		  //$(elem).observe('click', ipb.garage.changeTab );
	       });         		
	},
    
   	changeMake: function(e)
	{
	    var make_id = $F('make_id');

    	url = ipb.vars['base_url'] + "app=garage&amp;module=ajax&amp;section=vehicle&amp;do=get_models&amp;secure_key=" + ipb.vars['secure_hash'] + "&make_id=" + make_id;
    	url = url.replace( /&amp;/g, '&' );
    
    	new Ajax.Request( url,
    					  {
    						method: 'GET',
    						onSuccess: function (t )
    						{
    							dropdown = t.responseText;
                                
                                if( dropdown )
                                {
                                    $('model_wrap').update( dropdown );    
                                }    							
    						},
    						onException: function( f,e ){ alert( "Exception: " + e ) },
    						onFailure: function( t ){ alert( "Failure: " + t.responseText ) }
    					  } );       
    },
        
	viewShareBox: function(e,elem)
	{
	    vid = elem.id.match('share_([0-9a-z]+)')[1];
        
		if( Object.isUndefined(vid) ){ return; }
		
		if( parseInt(vid) == 0 ){
			return false;
		} 		
		
		Event.stop(e);
		
		var _url = ipb.vars['base_url'] + '&app=garage&module=ajax&section=vehicle&do=share&secure_key=' + ipb.vars['secure_hash'] + '&id=' + vid;	
		vehicle_share = new ipb.Popup( 'vehicle_share', {type: 'pane', modal: false, w: '600px', h: '400px', ajaxURL: _url, hideAtStart: false, close: '.cancel' } );
	},
	 
	changeTab: function(e)
	{		
	    // Taken from profile.js, all credit goes to IPS.
        Event.stop(e);
		elem = Event.findElement(e, 'li');
		
		alert(elem);
		
		if( !elem.hasClassName('tab_toggle') || !elem.id ){ return; }
		id = elem.id.replace('tab_link_', '');
		if( !id || id.blank() ){ return; }
		if( !$('tab_content') ){ return; }
		
		if( ipb.garage.activeTab == id )
		{
			return;
		}
		
		oldTab = ipb.garage.activeTab;
		ipb.garage.activeTab = id;
		
		// OK, we should have an ID. Does it exist already?
	
		if( !$('tab_' + id ) )
		{
			new Ajax.Request( ipb.vars['base_url'] + 'app=garage&module=ajax&section=vehicle&do=change_tab&id=' + ipb.garage.vehicleID + '&tab=' + id + '&secure_key=' + ipb.vars['secure_hash'],
							{
								method: 'post',
								onSuccess: function(t)
								{
								    if( t.responseText == 'nopermission' )
									{
										alert( ipb.lang['no_permission'] );
										return;
									}
									
									if( t.responseText != 'error' )
									{
										newdiv = new Element('div', { 'id': 'tab_' + id } ).hide();
										newdiv.update( t.responseText );
										
										$('tab_content').insert( newdiv );
										
										new Effect.Parallel( [
											new Effect.BlindUp( $('tab_' + oldTab), { sync: true } ),
											new Effect.BlindDown( $('tab_' + ipb.garage.activeTab), { sync: true } )
										], { duration: 0.4, afterFinish: function(){ 	// Re-execute JS for various things in posts
											ipb.garage.executeJavascript( $( newdiv ) ); } } );
										
										
									}
									else
									{
										alert( ipb.lang['action_failed'] );
										return;
									}
								}
							});
		}
		else
		{
			new Effect.Parallel( [
				new Effect.BlindUp( $('tab_' + oldTab), { sync: true } ),
				new Effect.BlindDown( $('tab_' + ipb.garage.activeTab), { sync: true } )
			], { duration: 0.4 } );
		}
		
		$$('.tab_toggle').each( function(otherelem){
			$(otherelem).removeClassName('active');
		});
		
		$(elem).addClassName('active');
		
	}     			
}
ipb.garage.init();
