
/**
 * Initialization functions
 * Version 1.1 - May 14th 2011
 */

// Define all needed bjects
var carid = ''; // Current car
var brandmodels = new Object();
var listcontent = '';
var listrefresh = false;

/**
 * Functions for displaying different pages, these will be called from iframe pages to solve AJAX browser history issue
 */

// Function to load the main, list or detail content
function load( page ) {
	document.getElementById('xml_history_vehicles').src = occasionscriptroot + 'show_' + page + '.htm';
}

// Display master page, hide other pages
function main() {
	// Define parameters
	var fields = 'merk,model';
	var params = 'sort=merk,model&fields=' + fields + '&output=xml&from=0&count=1000&files=' + files;
	var xsl = xslmain;
	// Load file
	loadFile( occasionroot + xmlparsefile + '?xml=' + xmldatafile + '&xsl=' + xsl , 'xml_content_vehicles' , params , setSearchFormBrand );
	// Reset form
	document.getElementById('xml_content_vehicles_search_form').reset();
}

// Display vehicle list, hide other pages
function list() {
	if ( listrefresh || listcontent.length < 1000 ) listcontent = '';
	listrefresh = false;
	if ( listcontent != '' ) {
		document.getElementById('xml_content_vehicles').innerHTML = listcontent;
		return true;
	}
	// Define parameters
	var xsl = xsllist;
	var fields = 'klantnummer,aantal_afbeeldingen,voertuignr_hexon,merk,model,type,kenteken,bouwjaar,tellerstand,kleur,verkoopprijs_particulier,merkgarantie,bovag_garantie,garantie_maanden';
	var params = 'fields=' + fields + '&output=xml&files=' + files;
	params += '&' + getSearchFormParams();
	// Load file
	loadFile( occasionroot + xmlparsefile + '?xml=' + xmldatafile + '&xsl=' + xsl , 'xml_content_vehicles' , params , listbuttons );
	//console.log( occasionroot + xmlparsefile + '?xml=' + xmldatafile + '&xsl=' + xsl );
	//console.log( params );
}

// Display vehicle detail, hide other pages
function detail() {
	// Set content in var
	listcontent = document.getElementById('xml_content_vehicles').innerHTML;
	// Define parameters
	var fields = '*'; // Request all fields
	var params = 'fields=' + fields + '&output=xml&from=0&count=1&selection=voertuignr_hexon:' + carid;
	params += '&files=' + files;
	var xsl = xsldetail;
	// Load file
	loadFile( occasionroot + xmlparsefile + '?xml=' + xmldatafile + '&xsl=' + xsl , 'xml_content_vehicles' , params , sendtofriendhide );
}

/**
 * Searchform manipulation functions
 */

// Function to populate select field
function setSearchFormSelection( obj , arr , valueprefix , selected ) {
	// Clear select field
	for ( var i in obj ) obj.remove(i);
	// Add empty value
	var el = document.createElement('option');
	el.text = '';
	el.value = '';
	DOMaddElement( obj , el );
	// Add all other values from array
	var found = false;
	for ( var i in arr ) {
		var el = document.createElement('option');
		el.text = i;
		el.value = valueprefix + i;
		if ( i.toUpperCase() == selected.toUpperCase() ) el.selected = 'selected';
		if ( i != 'undefined' ) DOMaddElement( obj , el );
	}
}

// Create a parameter list of current searchform values
function getSearchFormParams() {
	var params = 'selection=';
	// Vehicle specifications
	params += 'verkocht:n,nieuw_voertuig:n';
	if ( el = document.getElementById('xml_content_vehicles_search_other') ) params += ',' + el.value;
	if ( el = document.getElementById('xml_content_vehicles_search_brand') ) params += ',' + el.value;
	if ( el = document.getElementById('xml_content_vehicles_search_model') ) params += ',' + el.value;
	if ( el = document.getElementById('xml_content_vehicles_search_carrossery') ) params += ',' + el.value;
	if ( el = document.getElementById('xml_content_vehicles_search_fuel') ) params += ',' + el.value;
	if ( el = document.getElementById('xml_content_vehicles_search_transmission') ) params += ',' + el.value;
	if ( el = document.getElementById('xml_content_vehicles_search_btw') ) params += ',' + el.value;
	// Vehicle sorting
	if ( el = document.getElementById('xml_content_vehicles_search_sort') ) params += '&' + el.value;
	// Vehicle count
	if ( el = document.getElementById('xml_content_vehicles_search_from') ) params += '&from=' + el.value;
	if ( el = document.getElementById('xml_content_vehicles_search_count') ) params += '&count=' + el.value;
	// Return complete string
	return params;
}

// Function to set correct brands in list
function setSearchFormBrand() {
	el = document.getElementById('xml_content_vehicles_search_brand');
	setSearchFormSelection( el , brandmodels , 'merk:' , defaultbrand );
	// Run first model selection
	setSearchFormModel( document.getElementById('xml_content_vehicles_search_brand') );
}

// Function to set correct models in list
function setSearchFormModel( obj ) {
	el = document.getElementById('xml_content_vehicles_search_model');
	setSearchFormSelection( el , brandmodels[obj.value.replace('merk:','')] , 'model:' , '' );
}

/**
 * Miscellaneous functions
 */

// Function to get any querystring parameter
function gup( name ) {
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
  	return "";
  else
  	return results[1];
}

// Function to show the correct pagination buttons
var listfrom;
var listresult;
var listcount;
var listtotal;
function listbuttons() {

	// Always reset from to 0 for next search
	document.getElementById('xml_content_vehicles_search_from').value = 0;

	// Return if field not found
	if ( !document.getElementById('listfrom') ) return;

	// Get values
	listfrom = parseInt( document.getElementById('listfrom').value );
	listresult = parseInt( document.getElementById('listresult').value );
	listcount = parseInt( document.getElementById('listcount').value );
	listtotal = parseInt( document.getElementById('listtotal').value );

	// Hide or show buttons
	document.getElementById('buttonnext').style.display='';
	document.getElementById('buttonprevious').style.display='';
	if ( listfrom == 0 ) {
		document.getElementById('buttonprevious').style.display='none';
	}
	if ( listfrom + listcount >= listtotal ) {
		document.getElementById('buttonnext').style.display='none';
	}
	if ( listcount * 1 >= listtotal ) {
		document.getElementById('button1').style.display='none';
		document.getElementById('button2').style.display='none';
	}
	if ( listcount * 2 >= listtotal ) {
		document.getElementById('button3').style.display='none';
	}
	if ( listcount * 3 >= listtotal ) {
		document.getElementById('button4').style.display='none';
	}
	if ( listcount * 4 >= listtotal ) {
		document.getElementById('button5').style.display='none';
	}
	if ( listcount * 5 >= listtotal ) {
		document.getElementById('button6').style.display='none';
	}
	if ( listcount * 6 >= listtotal ) {
		document.getElementById('button7').style.display='none';
	}
	if ( listcount * 7 >= listtotal ) {
		document.getElementById('button8').style.display='none';
	}
	if ( listcount * 8 >= listtotal ) {
		document.getElementById('button9').style.display='none';
	}
	if ( listcount * 9 >= listtotal ) {
		document.getElementById('button10').style.display='none';
	}
	if ( listcount * 10 >= listtotal ) {
		document.getElementById('button11').style.display='none';
	}
	if ( listcount * 11 >= listtotal ) {
		document.getElementById('button12').style.display='none';
	}
	if ( listcount * 12 >= listtotal ) {
		document.getElementById('button13').style.display='none';
	}
	if ( listcount * 13 >= listtotal ) {
		document.getElementById('button14').style.display='none';
	}
	if ( listcount * 14 >= listtotal ) {
		document.getElementById('button15').style.display='none';
	}
	var current = ( listfrom + listcount ) / listcount;
	document.getElementById('button'+current).className='current';
}

// Function to go to the previous page in the list
function listprevious() {

	// Refresh list
	listrefresh = true;

	// Set new from
	var newfrom = listfrom - listcount;
	if ( newfrom < 0 ) newfrom = 0;
	document.getElementById('xml_content_vehicles_search_from').value = newfrom;

	// Reload list
	list();

}

// Function to go to the next page in the list
function listnext() {

	// Refresh list
	listrefresh = true;

	// Set new from
	var newfrom = listfrom + listcount;
	document.getElementById('xml_content_vehicles_search_from').value = newfrom;

	// Reload list
	list();
}

// Function to go to any page in the list
function listgo( to ) {

	// Refresh list
	listrefresh = true;

	// Set new from
	var newfrom = ( to - 1 ) * listcount;
	document.getElementById('xml_content_vehicles_search_from').value = newfrom;

	// Reload list
	list();
}

/**
 * Functions to show vehicle list and detail info
 */

// Vehicle list rollover effect
function vehicleListOver( el ) {
	el.backupimage = el.style.backgroundImage;
	el.style.backgroundImage = 'url(/files/website/img/occasions/vehicle_list_bg.png)';
}
function vehicleListOut( el ) {
	el.style.backgroundImage = el.backupimage;
}

// Show or hide popup box for sending e-mail to any address
function sendtofriendshow() {
	if( el = document.getElementById('xml_content_vehicles_sendtofriend') ) {
		el.style.display = 'block';
		document.getElementById('xml_content_vehicles_sendtofriend_to').select();
	}
}
function sendtofriendhide() {
	if( el = document.getElementById('xml_content_vehicles_sendtofriend') ) el.style.display = 'none';
}

// Function to popup the photo
function photolarge( el ) {
	window.open( el.src , 'photolarge' , 'height=500,width=660' );
}

// Functions to slide the photo
var speed = 5;
var factor;
var running = false;
function photogo( nr ) {
	if ( running ) return;
	running = true;
	var id = 'xml_content_vehicles_detail_photos_large';
	var el = document.getElementById( id );
	var oldx = parseFloat(el.style.left);
	if ( el.style.left == '' ) oldx = 0;
	var newx = -320 * ( nr - 1 );
	if ( oldx > newx ) factor = -speed;
	if ( oldx < newx ) factor = speed;
	movephoto( oldx , newx );
	running = false;
}
function movephoto( from , to ) {
	if ( from == to ) return;
	newpos = from + factor + ( to - from ) / Math.abs(factor);
	if ( newpos > to && factor > 0 ) newpos = to;
	if ( newpos < to && factor < 0 ) newpos = to;
	document.getElementById('xml_content_vehicles_detail_photos_large').style.left = newpos + 'px';
	setTimeout( 'movephoto(' + newpos + ',' + to + ')' , Math.abs(factor) );
}

