var tabPos = 0;
var currTab = 0;
var garage = new Array();
var opened = new Array();
var showroom = "";
var msgCt;


Array.prototype.remove=function(s){
  for(i=0;i<this .length;i++){
    if(s==this[i]) this.splice(i, 1);
  }
}

Array.prototype.contains=function(s){
	var ret = false;
  for(i=0;i<this .length;i++){
    if(s==this[i]) ret= true;
  }
  return ret;
}

/*
function createTabEl(label) {
  var el = document.createElement('span');
  el.className = 'chicago-tab';
  el.innerHTML = label;
  return el;
}
*/

var CHICAGO = function(){
	return{
		addCar: function(id){
			var CP = Ext.ContentPanel;

			Layout.layout.beginUpdate();
			if(!CHICAGO.openedCar(id)){
				opened[id] = Ext.id(id);
			}
			//CHICAGO.updateHash();
			
			Layout.layout.add("center", new CP(opened[id], {autoCreate:true,title: id,closable:true}));
		
			
			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.getRegion('center').getActivePanel().el.dom.parentNode.scrollTop = 0;});
			//autohide from ajax function removed --> setTimeout(CAR.collapseWest,1500);
			Layout.layout.endUpdate();

			CAR.initPhotos(id);
		},

		updateShowroom: function(query){
			showroom = query;
			CHICAGO.updateHash();
			Layout.layout.getRegion("center").getPanel("showroom_div").setUrl('showroom.php?'+showroom+'&idlist='+garage.toString());	
			Layout.layout.getRegion("center").getPanel("showroom_div").fireEvent('activate');
			Layout.layout.getRegion("center").showPanel("showroom_div");


		},
		resetGarage: function(){
			Layout.layout.getRegion("center").getPanel("garage_div").fireEvent('activate');
			Layout.layout.getRegion("center").showPanel("garage_div");
		},
		addToGarage: function(id){
			if(!inArray(garage,id)){
				garage[garage.length] = id;
			}
			CHICAGO.updateGarage();
		},
		removeFromGarage: function(id){
			garage.remove(id);
			CHICAGO.updateGarage();
		},
		updateGarage:function(){
			CHICAGO.updateHash();
			Layout.layout.getRegion("center").getPanel("garage_div").setUrl("garage.php?ids="+garage.toString()+"&idlist="+garage.toString());
			Layout.layout.getRegion("center").getPanel("showroom_div").setUrl('showroom.php?'+showroom+'&idlist='+garage.toString());

		},
		openedCar:function(id){
			for(var key in opened){
				if(key == id){
					return true;
				}
			}
			return false;
		},
		updateHash: function(){			
			window.location.hash = "#garage="+garage.toString()+"&showroom="+showroom+"&open=";
			Hash = window.location.hash;
		}
	};
	
}();


function findTabPos(id){
		var x = document.getElementById("tabs_td").getElementsByTagName("div");
		
		var tempPos = tabPos;
		for(i=0;i<x.length;i++){
			if(x[i].id == id){
				tempPos = i;	
			}
		}
		
		if(tempPos >= x.length-2){
			return x.length-3;
		}
		else if(tempPos == tabPos+1 || tempPos == tabPos+2){
			return tabPos;
		}
		else{
			return tempPos;	
		}
}


function redrawTabs(){
	var x = document.getElementById("tabs_td").getElementsByTagName("div");
	
	for(i=0;i<x.length;i++){
		x[i].style.display = "none";
	}
	if(x.length > 0){
		for(i=tabPos;i < tabPos+3 && i< x.length;i++){
			if(i >=0){
				x[i].style.display = "block";
				x[i].style.cssFloat = "left";
				x[i].style.styleFloat = "left";
			}
		}	
	}
}

function moveLeft(){
	if(tabPos > 0){
		tabPos--;
		redrawTabs();
	}
}

function moveRight(){
	x = document.getElementById("tabs_td").getElementsByTagName("div");
	if(tabPos < x.length-3){
		tabPos++;
		redrawTabs();
	}
}




function closeTab(id){
	var x = document.getElementById("tabs_td");
	var y = document.getElementById("tabs_content_td");
	var tab = document.getElementById(""+id+"");
	var content = document.getElementById(""+id+"_div");
	x.removeChild(tab);
	y.removeChild(content);
	
	x = document.getElementById("tabs_td").getElementsByTagName("div");
	
	Browse.closeCar(id);
	
	if(id == currTab){
		showTab("showroom");
	}
	if(x.length > 0){
		if(tabPos > x.length-3){
			tabPos--;	
		}
		redrawTabs();
	}
	maps[id] = null;

	
}

