
var Browse = function( ){
  var mTabView = null;
  var mTreeView = null;
  var mNodes = { };
  var mGarage = [ ];
  var splash;
  var mGarageTab = null;
  var mShowroomTab = null;
  var Hash = "";
  var cb;


 
    




  return {
    init : function(conf){
		Browse.initSitemap();
		Hash = window.location.hash;
		//Browse.initTabs(conf);
		Browse.initTree(conf);
		Browse.initTabs();
		Find.init();
		Layout.layout.getRegion("center").showPanel("showroom_div");
		mapInit();

		
    },
	initSitemap: function(){
		document.getElementById("sitemap").style.display="none";
	},
    initGarage : function(conf) {
		//this.updateGarage();		
    },
	initShowroom: function(){
		var s = window.location.hash;
		var s = s.split('&');
		if(s.length > 1){
			s = s[1].split('=');
			if(s[0] == "showroom"){
				CHICAGO.updateShowroom(s[1]);
			}
		}
		Layout.layout.getRegion("center").showPanel("showroom_div");
	},
    initTabs : function(conf){

		var garage = window.location.hash;
			  
		if(garage.length > 1){
			garage = garage.substr(1);
			garage = garage.split('&');
			garage = garage[0] || "garage=";
			garage = garage.split('=');
			if(garage.length == 2) {
				garage = garage[1].split(',');
				for(i=0;i<garage.length;i++){
					if(garage[i] != ""){
						Browse.addCar(garage[i]);
					}
				}
			} 
			
		} 
		else{
			garage = "";
		}


    },
    initTree : function(conf){
		var Tree = Ext.tree;
		
		tree = new Tree.TreePanel('car-tree', {
			animate:true, 
			loader: new Tree.TreeLoader({
            	dataUrl:'/tools/tree-nodes.php'
        	}),
        	enableDD:false,
        	containerScroll: true
		});
	
		// set the root node
		var root = new Tree.AsyncTreeNode({
			text: 'Inventory',
			draggable:false,
			id:'0',
			singleClickExpand: true,
			qtip:'Click to load "Full" Showroom',
			icon: '/img/car1.png',
			href: "javascript: CHICAGO.updateShowroom('status=2,4,5');"
		});
		
		tree.setRootNode(root);
				
		// render the tree
		tree.render();
		root.expand();
    },
	initSplash: function(){
				
			var loading = Ext.get('loading');
			var mask = Ext.get('loading-mask');
			mask.setOpacity(0);
			mask.shift({
				xy:loading.getXY(),
				width:loading.getWidth(),
				height:loading.getHeight(), 
				remove:true,
				duration:1,
				opacity:.5,
				easing:'bounceOut',
				callback : function(){
					loading.fadeOut({duration:1.2,remove:true});
					document.getElementById("h").style.display="none";
				}
			});
		/*
		splash = new Ext.BasicDialog(Ext.id(),
					{
						autoCreate: true,
						title: 'Welcome To Chicago Sports Cars - Please wait while the showroom is loading.',
						modal: true,
						height:600,
						width: 950,
						closable: true
					}
				
			);
		
		splash.setContentSize(950,600);
		

		document.getElementById(splash.body.id).innerHTML = "<img id='splash_img' src='/img/logo_950x600.png' class='png' height='600' width='950'  />";
		splash.show(document.body);
		*/
	},
	hideSplash: function(){
		//splash.hide();
		
	},
    addCar : function(id){
    if(!id) return;
	  	if(!inArray(mGarage,id)){
			mGarage[mGarage.length] = id;
			this.updateGarage( );
		}
    },
    closeCar : function(id){
      mGarage.remove(id);
      this.updateGarage( );
    },
	send : function(id){
		var tempGarage = mGarage.toString();
		tempGarage = tempGarage.split(',');
		tempGarage.remove(id)
		var first = getValue("first_"+id);
		var last = getValue("last_"+id);
		var email = getValue("email_"+id);
		var h_phone = getValue("h_phone_"+id);
		var w_phone = getValue("w_phone_"+id);
		var c_phone = getValue("c_phone_"+id);
		var pref_time = getValue("pref_time_"+id);
		var pref_num = getValue("pref_num_"+id);
		
		
		
		ajaj("/tools/send.php","id="+id+"&ids="+tempGarage.toString()+"&first="+first+"&last="+last+"&email="+email+"&h_phone="+h_phone+"&w_phone="+w_phone+"&c_phone="+c_phone+"&pref_time="+pref_time+"&pref_num="+pref_num);
	},
	updateGarage : function( ){
	    	var data = mGarage.toString( );
	    	CHICAGO.updateHash();
	    	createCookie('garage',data,1096);	 
		ajax2id("/listCars.php?","ids="+mGarage.toString( )+"&garage=garage","garage_div");
  	},
	showLastTab: function(){
		showTab(mGarage[mGarage.length-1]);
	},
	addCar: function(id){
			var CP = Ext.ContentPanel;

			Layout.layout.beginUpdate();
			if(!CHICAGO.openedCar(id)){
				opened[id] = Ext.id(id);
			}
			Layout.layout.add("center", new CP(opened[id], {autoCreate:true,title: id,closable:true}));
			CHICAGO.addToGarage(id);
			ajax("cars/car.php?id="+id,null,function(foo){Layout.layout.getRegion('center').getPanel(opened[id]).setContent(foo);initMap(id);if(garage.contains(id)){document.getElementById("add_to_garage_"+id).innerHTML = "&nbsp;";};Layout.layout.getRegion('center').getPanel(opened[id]).setTitle(document.getElementById("tab_title_"+id).value)});
			Layout.layout.endUpdate();
	},
	checkBack: function(){
		if(Hash != window.location.hash){
			window.location.reload();
		}
	}
  };
 }( );

