var contact;
var photos =[];
var downloadInfoSheet;

var CAR = function(){
	return {
		initPhoto: function(id,photo,alt){
			var tip = Ext.id("photo_"+id);
			if(alt){
				Ext.QuickTips.register({text: '<img src="'+photo+'" style="width:360px;height:270px;" /><br />'+alt, target: tip, autoHide:true,hideOnClick:false, width:360});		
			}
			else{
				Ext.QuickTips.register({text: '<img src="'+photo+'" style="width:360px;height:270px;" /><br />', target: tip, autoHide:true,hideOnClick:false, width:360});		
			}
		},
		setDefaultPhoto: function(car,photo){
			document.getElementById("display_photo_"+car).setAttribute("src",photo);
			document.getElementById("display_link").setAttribute("href","gallery/"+photo.replace("/pictures/","").replace(".jpg","")+".html");
			window.scrollTo(window.scrollX,0);
		},
		initPhotos: function(car){
			CAR.car = car;
			ajax("/tools/photos_json.php?id="+car,null,function(foo){photos[CAR.car] = eval("("+foo+")");},CAR);
		},
		slideShowPlay: function(car){
			if(playing){
				photos[car].position = photos[car].position%photos[car].count; 
				//photos object will contain {urls} and {position} and {count}
				document.getElementById("display_photo_"+car).setAttribute("src",photos[car].urls[photos[car].position++]);
				slideShow = setTimeout(function(){CAR.slideShowPlay(car)},1600);
			}
			else{
				clearTimeout(slideShow);
			}
		},
		next: function(car){
			photos[car].position = (photos[car].position+1)%photos[car].count;
			document.getElementById("display_photo_"+car).setAttribute("src",photos[car].urls[photos[car].position]);
		},
		prev: function(car){
			photos[car].position--;
			if(photos[car].position == -1){
				photos[car].position = photos[car].count-1;
			}
			document.getElementById("display_photo_"+car).setAttribute("src",photos[car].urls[photos[car].position]);
		},
		contactInit: function(id){

			
			contact = new Ext.LayoutDialog(Ext.id(),{
												  autoCreate:true,
												  modal:true,
												  title:'Contact Dealer',
												  width:500,
												  height:500,
												  resizable:false,
												  center:{
													  
												  }
												  });
			
			var lo = contact.getLayout();
			lo.add('center', new Ext.ContentPanel(Ext.id(),{autoCreate:true,url:'/contact.php?id='+id}));
			var dealer_div = Ext.get('dealer_div');
			contact.show(dealer_div.dom);
		},
		sendContact : function(id){
			
				var tempGarage = garage.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);
				var comment = encodeURIComponent(getValue("comment_"+id));
			
			
			ajax("/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+"&comment="+comment,null,function(foo){eval(foo);});
			
		},
		generateInfoSheet: function(car){
			if(!downloadInfoSheet)	{
				downloadInfoSheet = new Ext.LayoutDialog(Ext.id(),{autoCreate:true,height:120,width:200,center:{autoTabs:false},modal:true});
				var layout = downloadInfoSheet.getLayout();
				layout.beginUpdate();
				layout.add('center',new Ext.ContentPanel('download_info_sheet',{autoCreate:true}));
				layout.endUpdate();
			}
			document.getElementById("download_info_sheet").innerHTML = "Generating Information Sheet... Please wait.";
			downloadInfoSheet.show();
			ajax("/generate-pdf.php","id="+car,function(foo){CAR.downloadInfoSheet(eval("("+foo+")"))});
			
		},
		downloadInfoSheet: function(obj){
			document.getElementById("download_info_sheet").innerHTML = "<a href='http://citrusstand.com/pdf/"+obj.stocknum+"_"+obj.vin+".pdf' target='_new' >Download</a>";
		}
	};
}();