$(document).ready(function(){

	// flash inladen
	// als een div aangemaakt wordt met als class "flash",
	// en als inhoud het absolute pad naar de flash en de variable
	// wordt deze automatisch ingeladen
	$('.flash').each(function(){
		slideshowvars = $(this).find('.flashlink').html();
		slideshowheight = $(this).css('height');
		slideshowwidth = $(this).css('width');
		$(this).html("");
		$(this).flash({
	    	src: slideshowvars,
	    	width: slideshowwidth,
	    	height: slideshowheight,
			wmode: "transparent"
		});
	});
	// links in "_blank" i.p.v. via HTML i.v.m. valid XHTML
	$('.blankwindow').attr('target','_blank');




	$('.c_select').click(function(){
		select = $(this);
		display = select.find('.c_select_list').css('display');

		$('.c_select').each(function(){
			$(this).find('.c_select_list').hide();
		});

		select.find('.c_select_list').show();

		if (display=='block')
			select.find('.c_select_list').hide();
	});


	$('.c_select_item').click(function(){
		$(this).parents('.c_select').find('.c_select_showinput').html($(this).find('.c_select_name').html());
		$(this).parents('.c_select').find('.c_select_input').attr('value',$(this).find('.c_select_value').html());
	});



	$('.c_check_showinput').click(function(){
		if ($(this).parents('.c_check').find('.c_check_showinput').html()=='X')
		{
			$(this).parents('.c_check').find('.c_check_showinput').html('');
			$(this).parents('.c_check').find('.c_check_input').attr('checked','');
		}
		else
		{
			$(this).parents('.c_check').find('.c_check_showinput').html('X');
			$(this).parents('.c_check').find('.c_check_input').attr('checked','checked');
		}

	});


	$('.auto_blok_bmw').each(function(){
		if (!$(this).hasClass('noshow'))
		{
			foto = $(this).find('img').attr('src');
			model = $(this).find('.auto_model').html();
			uitvoering = $(this).find('.auto_uitvoering').html();
			prijs = $(this).find('.auto_prijs').html();
			kleur = $(this).find('.auto_kleur').html();
			kmstand = $(this).find('.auto_kmstand').html();
			datum_deel_1 = $(this).find('.auto_datum_deel_1').html();
			kenteken = $(this).find('.auto_kenteken').html();
			link = $(this).find('.auto_link a').attr('href');
			taal = $(this).find('.auto_lang').html();

			$(this).css('height','330px');
			$(this).css('width','249px');

			slideshowheight = $(this).css('height');
			slideshowwidth = $(this).css('width');

			$(this).html("");
			$(this).flash({
		    	src: "/public/flash/autos_bmw_"+taal+".swf?foto="+foto+"&model="+model+"&uitvoering="+uitvoering+"&prijs="+prijs+"&kleur="+kleur+"&kmstand="+kmstand+"&datum_deel_1="+datum_deel_1+"&kenteken="+kenteken+"&link="+link,
		    	width: slideshowwidth,
		    	height: slideshowheight
			});
		}
		else
		{
			$(this).hide();
		}
	});

	$('.auto_blok_mini').each(function(){
		foto = $(this).find('img').attr('src');
		model = $(this).find('.auto_model').html();
		uitvoering = $(this).find('.auto_uitvoering').html();
		prijs = $(this).find('.auto_prijs').html();
		kleur = $(this).find('.auto_kleur').html();
		kmstand = $(this).find('.auto_kmstand').html();
		datum_deel_1 = $(this).find('.auto_datum_deel_1').html();
		kenteken = $(this).find('.auto_kenteken').html();
		link = $(this).find('.auto_link a').attr('href');
		taal = $(this).find('.auto_lang').html();

		$(this).css('height','330px');
		$(this).css('width','249px');

		slideshowheight = $(this).css('height');
		slideshowwidth = $(this).css('width');

		$(this).html("");
		$(this).flash({
	    	src: "/public/flash/autos_mini_"+taal+".swf?foto="+foto+"&model="+model+"&uitvoering="+uitvoering+"&prijs="+prijs+"&kleur="+kleur+"&kmstand="+kmstand+"&datum_deel_1="+datum_deel_1+"&kenteken="+kenteken+"&link="+link,
	    	width: slideshowwidth,
	    	height: slideshowheight
		});
	});

	$('#offerte_aanvraag_knop').click(function(){
		merk = $('#occ_merk').html();
		model = $('#occ_model').html();
		uitvoering = $('#occ_uitvoering').html();
		kenteken = $('#occ_kenteken').html();

		if (model!='undefined')
			pageTracker._trackPageview('/analytics/offerte+aanvraag+'+merk+'+'+model+'+'+uitvoering+'+'+kenteken);
		else
			pageTracker._trackPageview('/analytics/offerte+aanvraag+'+merk+'+'+uitvoering+'+'+kenteken);
		$('#offerte_aanvraag').show();
	});

	$('.popupclose').click(function(){
		$(this).parents('.popup').find('input[type=text], textarea').each(function(){
			if ($(this).attr('readonly')!=true)
				$(this).val('');
		});

		$(this).parents('.popup').find('.popuperror').hide();

		$(this).parents('.popup').hide();
	});


	$('#tellafriend_knop').click(function(){
		merk = $('#occ_merk').html();
		model = $('#occ_model').html();
		uitvoering = $('#occ_uitvoering').html();
		kenteken = $('#occ_kenteken').html();

		if (model!='undefined')
			pageTracker._trackPageview('/analytics/tellafriend+'+merk+'+'+model+'+'+uitvoering+'+'+kenteken);
		else
			pageTracker._trackPageview('/analytics/tellafriend+'+merk+'+'+uitvoering+'+'+kenteken);
		$('#tellafriend').show();
	});

	$('.popup.witherror').show();

	$('.melding_ok_button').click(function(){
		$(this).parents('.melding').hide();
	});

	$('.fotothumb').mouseover(function(){
		foto = $(this).find('.fotopreload_small img').attr('src');
		$('#fotogroot img').attr('src',foto);

		foto = $(this).find('.fotopreload_large img').attr('src');
		$('#fotoorg img').attr('src',foto);
	});

	$('#fotogroot').click(function(){
		$('#fotoorg').show();
	});

	$('#fotoorg').click(function(){
		$('#fotoorg').hide();
	});

	$('#contactinfo_knop').click(function(){
		merk = $('#occ_merk').html();
		model = $('#occ_model').html();
		uitvoering = $('#occ_uitvoering').html();
		kenteken = $('#occ_kenteken').html();
		trackConversion(dealer_conversion_id, dealer_conversion_label); //using the global variables defined in the header of the detail page

		if (model!='undefined')
		{
			pageTracker._trackPageview('/analytics/dealerinfo+'+merk+'+'+model+'+'+uitvoering+'+'+kenteken);
		}
		else
		{
			pageTracker._trackPageview('/analytics/dealerinfo+'+merk+'+'+uitvoering+'+'+kenteken);
		}
		$('#contactinfo').show();
	});

	$(".verzend_knop").click(function(){
		merk = $('#occ_merk').html();
		model = $('#occ_model').html();
		uitvoering = $('#occ_uitvoering').html();
		kenteken = $('#occ_kenteken').html();
		var type = '';
		if ($(this).attr('id')=='tellafriend_verzend_knop')
			type = 'fiend+getellt';
		else if ($(this).attr('id')=='contactform_verzend_knop')
			type = 'contact+verzonden';

		if (type!='')
		{
			if (model!='undefined')
			{
				pageTracker._trackPageview('/analytics/'+type+'+'+merk+'+'+model+'+'+uitvoering+'+'+kenteken);
			}
			else
			{
				pageTracker._trackPageview('/analytics/'+type+'+'+merk+'+'+uitvoering+'+'+kenteken);
			}
		}
	});
	
	$('form').append('<input type="hidden" name="js_submit" value="oke" />');
});

function select_foto(f1,f2)
{
	$('#fotogroot img').attr('src',f1);
	$('#fotoorg img').attr('src',f2);
}

/**
 * Tracks an event to google adwords dynamicly
 * @param {Int} conversion_id
 * @param {String} conversion_label
 */

function trackConversion(conversion_id, conversion_label)
{
	var image = document.createElement('img');
	image.width = "1";
	image.height = "1";
	image.style.borderStyle = "none";
	image.style.display = "none";
	image.alt = "";
	image.src = "http://www.googleadservices.com/pagead/conversion/" + conversion_id + "/?label=" + conversion_label + "&guid=ON&script=0";
	document.body.appendChild(image);
}
