panel = new Array();
cachedCount = new Array();
var targetIDpre;
var targetID;
var targetWidth;
var targetCached;
var targetSubmit;
var targetDrag;
var targetCenter;
var targetViewPoint;
var targetRemainAfterSubmit;
var targetSubmitDivID;
var targetRequestString;
var targetHeight;
var targetjsCode;
var targetX;
var targetY;
var targetClose;



var i=0;


function submitCallback(obj)
{
	var response = obj.responseText;

	if (targetSubmitDivID)
	{
		/*			panel[targetSubmitDivID].destroy();

		var newdiv = document.createElement ("div");
		var html = '      <div class="hd">Loading...</div> <div class="bd"> <strong> Please Wait </strong> </div>';

		key_array = targetSubmitDivID.split(':');

		targetSubmitDivID = key_array[0]+":"+cachedCount[key_array[0]];
		cachedCount[key_array[0]] = cachedCount[key_array[0]]+ 1;

		newdiv.setAttribute('id',targetSubmitDivID);
		newdiv.innerHTML = html;
		document.body.appendChild(newdiv);
		*/
		document.getElementById(targetSubmitDivID).innerHTML = response;

		if (targetSubmitDivID == 'used-results-panel') {
			// Refresh the ad banner
			document.getElementById('banner-ad-top-right-iframe-id').contentWindow.location.reload(true);
		}

		//			init (targetSubmitDivID, targetWidth, targetCached, targetSubmit, targetDrag, targetCenter, targetViewPoint);

	}
	else
	{
		panel[targetID].destroy();

		var newdiv = document.createElement ("div");
		var html = '      <div id="loadingDiv" class="loadingDiv"></div>';

		key_array = targetID.split(':');

		targetID = key_array[0]+":"+cachedCount[key_array[0]];
		cachedCount[key_array[0]] = cachedCount[key_array[0]]+ 1;

		newdiv.setAttribute('id',targetID);
		newdiv.innerHTML = html;
		document.body.appendChild(newdiv);

		document.getElementById(targetID).innerHTML = response;
		init (targetID, targetWidth, targetCached, targetSubmit, targetDrag, targetCenter, targetViewPoint, targetHeight, targetX, targetY, targetClose);
	}

	if (document.getElementById('shortList')) {
		document.getElementById('shortList').style.display = "block";
	}

	//refreshSmartSearch();
}

function submitFailure(obj)
{
	alert("Submission failed: " + obj.status);
}


function createPanel(key,pwidth,submit_yn,drag_yn,center_yn,viewpoint_yn,pheight,px,py,pclose)
{
	if (drag_yn =='y')
	{
		drag_yn = true;
	}
	else
	{
		drag_yn = false;
	}

	if (center_yn == 'y')
	{
		center_yn = true;
	}
	else
	{
		center_yn = false;
	}

	if (viewpoint_yn)
	{
		viewpoint_yn = true;
	}
	else
	{
		viewpoint_yn = false;
	}
	
	if (px == null)
	{
		px=25;
	}

	if (py == null)
	{
		py=25;
	}

        if (pclose == null)
        {
                pclose = true;
        }
        else
        {
                pclose = false;
	}

	var handleCancel = function()
	{
		this.cancel();
	}

	var handleSubmit = function()
	{
		this.submit();
		if (!targetRemainAfterSubmit)
		{
			this.hide();
		}
	}

	if (pheight)
	{
		newPanel = new YAHOO.widget.Dialog(key,{ effect:{effect:YAHOO.widget.ContainerEffect.FADE,duration:0.25}, width: pwidth, height: pheight, fixedcenter: center_yn, constraintoviewport: viewpoint_yn, underlay:"none", close:pclose, visible:false, draggable: drag_yn, modal:true, x:px, y:py } );
	}
	else
	{
		newPanel = new YAHOO.widget.Dialog(key,{ effect:{effect:YAHOO.widget.ContainerEffect.FADE,duration:0.25}, width: pwidth, fixedcenter: center_yn, constraintoviewport: viewpoint_yn, underlay:"none", close:pclose, visible:false, draggable: drag_yn, modal:true, x:px, y:py } );
	}

	newPanel.callback.success = submitCallback;
	newPanel.callback.failure = submitFailure;

	var listeners = new YAHOO.util.KeyListener(document, { keys : 27 }, {fn:handleCancel,scope:newPanel,correctScope:true} );
	newPanel.cfg.queueProperty("keylisteners", listeners);

	if (submit_yn == 'y')
	{
		newPanel.cfg.queueProperty("buttons", [ { text:"Submit", handler:handleSubmit } ]);
	}

	var handleManual = function(type, args, obj)
	{
		alert("Manual submission of " + this.id + " detected");
	}

	newPanel.manualSubmitEvent.subscribe(handleManual, newPanel, true);
	newPanel.render();
	newPanel.show();
	panel[key] = newPanel;


	key_array = key.split(":");

	if (key_array[0] == 'manufacturer-panel') {
		if (document.getElementById("maker_ID")) {
			document.getElementById("maker_ID").options[document.getElementById("maker_selected_ID").value].selected = true;
		}

		if (document.getElementById("model_ID")) {
			document.getElementById("model_ID").options[document.getElementById("model_selected_ID").value].selected = true;
		}
	}

	if (key_array[0] == 'price-range-panel') {
		if (document.search_form.price_F) {
			document.search_form.to_price_F.options[document.search_form.to_price_selected_F.value].selected = true;
		}
	}

	if (key_array[0] == 'edit-car-panel') {
		if (document.search_form.maker_F) {
			document.search_form.maker_F.options[document.search_form.maker_selected_F.value].selected = true;
		}
	}

	if (key_array[0] == 'edit-car-panel') {
		if (document.search_form.model_F) {
			document.search_form.model_F.options[document.search_form.model_selected_F.value].selected = true;
		}
	}


	sizeMask();
}


function init(key,pwidth,cached_yn,submit_yn,drag_yn,center_yn,viewpoint_yn,pheight,px,py,pclose)
{
	//clearTimeout(ajaxTimeout);
	key_array = key.split(":");

	if(!panel[targetID])
	{
		createPanel(targetID,pwidth,submit_yn,drag_yn,center_yn,viewpoint_yn,pheight,px,py,pclose);
	}
	else if (cached_yn == 'n')
	{
		if (cachedCount[key_array[0]]==null)
		{
			cachedCount[key_array[0]] = 0;
		}

		createPanel(targetID,pwidth,submit_yn,drag_yn,center_yn,viewpoint_yn,pheight,px,py,pclose);
	}
	else
	{
		panel[targetID].show();
	}
}


function popUp(divID, submitDivID, divWidth, divCached, divSubmit, divDrag, divCenter, divViewpoint, divRemainAfterSubmit, requestString, divjsCode, divHeight, divX, divY, divClose)
//function popUp(divID, submitDivID, divWidth, divCached, divSubmit, divDrag, divCenter, divViewpoint, divRemainAfterSubmit, divWidthAfterSubmit, divCachedAfterSubmit, divSubmitAfterSubmit, divDragAfterSubmit, divCenterAfterSubmit, divViewpointAfterSubmit, requestString)
{
  
	//ajaxTimeout = setTimeout("hideMask(1);", 10000);
	
//	if (document.search_form) {
//		for (var i = 0; i < document.search_form.elements.length; i++) {
//			if (document.search_form.elements[i].type == 'select-one') {
//				document.search_form.elements[i].disabled = true;
//			}
//		}
//	}
	

	showMask();
	var newdiv = document.createElement ("div");
	//var html = '      <div id="loadingDiv" class="loadingDiv" style="width: '+YAHOO.util.Dom.getDocumentWidth()+' height: '+YAHOO.util.Dom.getDocumentHeight()+'"></div>';
	var html = '      <div id="loadingDiv" class="loadingDiv"></div>';

	targetID = divID+":"+(cachedCount[divID]-1);
	targetIDpre = divID;
	targetjsCode = divjsCode;



	if (!panel[targetID])
	{
		targetID = divID+":0";
		cachedCount[divID] = 1;
		newdiv.setAttribute('id',targetID);
		newdiv.innerHTML = html;
		document.body.appendChild(newdiv);

		targetSubmitDivID = submitDivID;
		targetWidth = divWidth;
		targetCached = divCached;
		targetSubmit = divSubmit;
		targetDrag = divDrag;
		targetCenter = divCenter;
		targetViewPoint = divViewpoint;
		targetRemainAfterSubmit = divRemainAfterSubmit;
		targetRequestString = requestString;
		targetHeight = divHeight;
		targetX = divX;
		targetY = divY;
                targetClose = divClose;


		ajaxpack.getAjaxRequest('/content/panels/'+divID+'.php', requestString, fetchPanel, 'txt');
	}
	else
	{
		// Destroy the old panel

		if (divCached!='y')
		{
			panel[targetID].destroy();
			targetID = divID+":"+cachedCount[divID];
			cachedCount[divID] = cachedCount[divID]+ 1;
			newdiv.setAttribute('id',targetID);
			newdiv.innerHTML = html;
			document.body.appendChild(newdiv);

			targetSubmitDivID = submitDivID;
			targetWidth = divWidth;
			targetCached = divCached;
			targetSubmit = divSubmit;
			targetDrag = divDrag;
			targetCenter = divCenter;
			targetViewPoint = divViewpoint;
			targetRemainAfterSubmit = divRemainAfterSubmit;
			targetRequestString = requestString;
			targetHeight = divHeight;
			targetX = divX;
			targetY = divY;
                        targetClose = divClose;

			ajaxpack.getAjaxRequest('/content/panels/'+divID+'.php', requestString, fetchPanel, 'txt');
		}
		else
		{
			targetSubmitDivID = submitDivID;
			targetWidth = divWidth;
			targetCached = divCached;
			targetSubmit = divSubmit;
			targetDrag = divDrag;
			targetCenter = divCenter;
			targetViewPoint = divViewpoint;
			targetRemainAfterSubmit = divRemainAfterSubmit;
			targetRequestString = requestString;
			targetHeight = divHeight;
                        targetX = divX;
                        targetY = divY;
                        targetClose = divClose;
			init (divID, divWidth, divCached, divSubmit, divDrag, divCenter, divViewpoint, '', divX, divY, divClose);
		}
	}

}


function fetchPanel()
{

	var myajax=ajaxpack.ajaxobj
	var myfiletype=ajaxpack.filetype
	if (myajax.readyState == 4){ //if request of file completed
		if (myajax.status==200 || window.location.href.indexOf("http")==-1)
		{ //if request was successful or running script locally
			if (myfiletype=="txt")
			{
				document.getElementById(targetID).innerHTML = myajax.responseText;
				init (targetID, targetWidth, targetCached, targetSubmit, targetDrag, targetCenter, targetViewPoint, targetHeight, targetX, targetY,targetClose);

				if (targetIDpre == 'autocheck-panel')
				{
					document.getElementById('autoCheckIFrame').style.height = eval(targetHeight.substr(0, targetHeight.indexOf('px'))-7)+'px';
				}

				if (targetIDpre == 'ajax-vehicle-details')
				{
					checkShortlist= targetRequestString.search(/addShortlist_F=Y/);

					if (checkShortlist>0)
					{
						ajaxpack.getAjaxRequest('/ajax-shortlist-add.php', targetRequestString, reloadShortList,'txt');
					}

				// RUN ANY JS CODE YOU WANT TO CALL AFTER POP UP LOADING
  				 if (targetjsCode)
			  	 {
                              
   				    var tokens= targetjsCode.split( "*" );
			
				    var i =0; 
				    for (i = 0; i < tokens.length; i++) 
				    {
					  var token = tokens[i].split("|");
					 
			              if (token[0]=='A')
			              {
						var no = token[2].indexOf('ondemand/');
						//var file = tokens[2].substr(no,tokens[2].length);
						var file = token[2].substr(0,no+8)
						var id = token[2].substr(no+8,token[2].length);
						var id = id.replace('.flv','');

						var s1 = new SWFObject(token[1],"mediaplayer","400","245","7");
						s1.addParam("allowfullscreen","false");
						s1.addVariable("width","400");
						s1.addVariable("height","245");
						s1.addVariable("streamer",file);
						s1.addVariable("file",'used-videos'+id);
						s1.addVariable('javascriptid','mediaplayer');
						s1.addVariable("image",token[3]);
				                s1.addVariable("type","rtmp");
						s1.addVariable('enablejs','true');
						s1.addParam("wmode", "transparent"); 
						s1.write("video");
        			      }
				      else if (token[0]=='AB')
                                      {
                                    		var s1 = new SWFObject(token[1],"mediaplayer","400","245","7");
                                    		s1.addParam("allowfullscreen","false");
                                    		s1.addVariable("width","400");
                                    		s1.addVariable("height","245");
                                    		s1.addVariable("file",token[2]);
                                    		s1.addVariable('javascriptid','mediaplayer');
                                    		s1.addVariable("image",token[3]);
                                    		s1.addVariable('enablejs','true');
                                   		s1.write("video");
                                      }

				      if (token[0]=='B')
				      {
						var so = new SWFObject(token[1], "mymovie", "670", "100", "8", "#ffffff");
					   	so.addParam("wmode", "transparent"); 
					   	so.addParam("flashvars", 'displayText='+token[2]);
						so.write("sponBanner");
				      }
				   }
				 }
				}

			}
			else
			{
				alert(myajax.responseXML)
			}
		} else {
			hideMask(myajax.status);
		}
	}
}


function panelSubmit(thisPanel) {



	thisPanel = thisPanel+":"+(cachedCount[thisPanel]-1);

	if (targetSubmitDivID == 'used-results-panel') {
		showSearchLoading();
	}


	panel[thisPanel].submit();

	if (!targetRemainAfterSubmit)
	{
		panel[thisPanel].hide();
	}
}


/*	function panelHide(thisPanel) {
//	alert (thisPanel);
//	 thisPaneld = thisPanel+":"+(cachedCount[thisPanel]-1);
//	 if (isUndefined(panel[thisPanel]))
//	 {
//	 alert  ("here");
//	 panel[thisPanel+":0"].hide();
//	 }
//	 else
//	 {
//	 alert ("here2");
panel[thisPanel].hide();
//	 }
}
*/

function panelHide(thisPanel) {
	var thisPaneld = thisPanel+":"+(cachedCount[thisPanel]-1);
	panel[thisPaneld].hide();	
	targetIDpre='';
	targetID='';
	targetWidth='';
	targetCached='';
	targetSubmit='';
	targetDrag='';
	targetCenter='';
	targetViewPoint='';
	targetRemainAfterSubmit='';
	targetSubmitDivID='';
	targetRequestString='';
	targetHeight='';
	targetX = '';
	targetY = '';
}

function panelHideUnCached(thisPanel) {
        var thisPaneld = thisPanel+":"+(cachedCount[thisPanel]-1);
        panel[thisPaneld].hide();
        targetIDpre='';
        targetID='';
        targetWidth='';
        targetCached='';
        targetSubmit='';
        targetDrag='';
        targetCenter='';
        targetViewPoint='';
        targetRemainAfterSubmit='';
        targetSubmitDivID='';
        targetRequestString='';
        targetHeight='';
        targetX = '';
        targetY = '';
}

function panelHideAll() {
	for(var panelName in panel) {
		if (panelName.substr(-2, 1) == ':') {
			if (document.getElementById(panelName)) {
				panel[panelName].hide();
			}
		}
	}
}


function buildMask() {
	mask = document.createElement("DIV");
	mask.setAttribute('id', 'mask');
	mask.setAttribute('class', 'mask');
	mask.setAttribute('className', 'mask');

	mask.innerHTML = "&nbsp;";

	mask.style.height = YAHOO.util.Dom.getDocumentHeight() + "px";
	mask.style.width  = YAHOO.util.Dom.getDocumentWidth()  + "px";

	document.body.appendChild(mask);

	document.getElementById('mask').onclick = function() {
	};

	mask2 = document.createElement("DIV");
	mask2.setAttribute('id', 'mask2');
	mask2.setAttribute('class', 'mask2');
	mask2.setAttribute('className', 'mask2');

	mask2.innerHTML = "&nbsp;";

	mask2.style.height = YAHOO.util.Dom.getDocumentHeight() + "px";
	mask2.style.width  = YAHOO.util.Dom.getDocumentWidth()  + "px";

	document.body.appendChild(mask2);

	document.getElementById('mask2').onclick = function() {
	};

	mask3 = document.createElement("DIV");
	mask3.setAttribute('id', 'mask3');
	mask3.setAttribute('class', 'mask2');
	mask3.setAttribute('className', 'mask2');

	mask3.innerHTML = "&nbsp;";

	mask3.style.height = YAHOO.util.Dom.getDocumentHeight() + "px";
	mask3.style.width  = YAHOO.util.Dom.getDocumentWidth()  + "px";

	document.body.appendChild(mask3);

	document.getElementById('mask3').onclick = function() {
	};
}

function showMask() {

	//document.body.addClass =  "masked";
	sizeMask();

	if (document.getElementById('banner-ad-top-right')) {
		document.getElementById('banner-ad-top-right').style.display = "none";
	}

	if (document.getElementById('mask2').style.display == "block") {

		document.getElementById('mask3').style.display = "block";

	} else if (document.getElementById('mask').style.display == "block") {

		document.getElementById('mask2').style.display = "block";

	} else {
		document.getElementById('mask').style.display = "block";
	}
}

function hideMask(reason) {
	if (reason == 404)
	{
		alert("Panel Not Found - Please try again later");
	} else if (!isNaN(reason)) {
		alert("Request Timed Out - Please try again");
	}

	if (document.getElementById('loadingDiv')) {
		document.getElementById('loadingDiv').style.display = "none";
	}

	if (document.getElementById('mask3').style.display == "block") {
		document.getElementById('mask3').style.display = "none";
	} else if (document.getElementById('mask2').style.display == "block") {
		document.getElementById('mask2').style.display = "none";
	} else {
		document.getElementById('mask').style.display = "none";
	}

	if (document.getElementById('banner-ad-top-right')) {
		document.getElementById('banner-ad-top-right').style.display = "block";
	}

}

function sizeMask() {


	if (document.getElementById('mask')) {
		document.getElementById('mask').style.height = YAHOO.util.Dom.getDocumentHeight()+"px";
		document.getElementById('mask').style.width = YAHOO.util.Dom.getDocumentWidth()+"px";
	}

	if (document.getElementById('mask2')) {
		document.getElementById('mask2').style.height = YAHOO.util.Dom.getDocumentHeight()+"px";
		document.getElementById('mask2').style.width = YAHOO.util.Dom.getDocumentWidth()+"px";
	}

	if (document.getElementById('mask3')) {
		document.getElementById('mask3').style.height = YAHOO.util.Dom.getDocumentHeight()+"px";
		document.getElementById('mask3').style.width = YAHOO.util.Dom.getDocumentWidth()+"px";
	}
}

function isUndefined(a) {
	return typeof a == 'undefined';
}
