// JavaScript Document

function sel_hrst(id)
{
	if(id != "-")
	{


		new Ajax.Request('php/json/index.php?sec=hrst&id='+id, {
		  method:'get',
		  onCreate: function()
		  {
			  	$("auto_model").update();
				$("auto_model").enable();
				elem = new Element('option',{'value' : '-'});
				elem.update("Bitte Warten...");
				$("auto_model").insert(elem);

		  },
		  onSuccess: function(transport)
			{
				var json = transport.responseText.evalJSON(true);
				$("auto_model").update();
				$("auto_model").enable();
				$("auto_motor").update();
				$("auto_motor").disable();
				$("selected_cat").hide();
				$("auto_motor").insert(new Element('option',{'value' : '-'}).update('Bitte Motor ausw&auml;hlen'));
				$("auto_model").insert(new Element('option',{'value' : '-'}).update('Bitte Modell ausw&auml;hlen'));
				i='cars';
				if(typeof(json[i]) == "object")
				{
					for(var x in json[i])
					{
						if(json[i][x]['au_id'] != undefined)
						{
							$("auto_model").insert(new Element('option',{'value' : json[i][x]['au_id']}).update((json[i][x]['au_name'])));
						}
					}
				}

			}
		});


	}
	else
	{
		$("auto_model").update();
		$("auto_model").enable();
		$("auto_motor").update();
		$("auto_motor").enable();
		$("auto_motor").disable();
		$("auto_motor").insert(new Element('option',{'value' : '-'}).update('Bitte Motor ausw&auml;hlen'));
		$("auto_model").insert(new Element('option',{'value' : '-'}).update('Bitte Modell ausw&auml;hlen'));
	}

}

function sel_hrst_top(id)
{
	if(id != "-")
	{
		new Ajax.Request('php/json/index.php?sec=hrst&id='+id, {
		  method:'get',
		  onCreate: function()
		  {
			  	$("auto_model_top").update();
				$("auto_model_top").enable();
				elem = new Element('option',{'value' : '-'});
				elem.update("Bitte Warten...");
				$("auto_model_top").insert(elem);

		  },
		  onSuccess: function(transport)
			{
				var json = transport.responseText.evalJSON(true);
				$("auto_model_top").update();
				$("auto_model_top").enable();
				$("auto_motor_top").update();
				$("auto_motor_top").disable();
				$("auto_motor_top").insert(new Element('option',{'value' : '-'}).update('Bitte Motor ausw&auml;hlen'));
				$("auto_model_top").insert(new Element('option',{'value' : '-'}).update('Bitte Modell ausw&auml;hlen'));
				i='cars';
				if(typeof(json[i]) == "object")
				{
					for(var x in json[i])
					{

						if(json[i][x]['au_id'] != undefined)
						{
							$("auto_model_top").insert(new Element('option',{'value' : json[i][x]['au_id']}).update((json[i][x]['au_name'])));
						}
					}
				}

			}
		});


	}
	else
	{
		$("auto_model_top").update();
		$("auto_model_top").enable();
		$("auto_motor_top").update();
		$("auto_motor_top").enable();
		$("auto_motor_top").disable();
		$("auto_motor_top").insert(new Element('option',{'value' : '-'}).update('Bitte Motor ausw&auml;hlen'));
		$("auto_model_top").insert(new Element('option',{'value' : '-'}).update('Bitte Modell ausw&auml;hlen'));
	}

}

function sel_mdl(id)
{
	if(id != "-")
	{

		new Ajax.Request('php/json/index.php?sec=model&id='+id, {
		  method:'get',
		  onCreate: function()
		  {
			  	$("auto_motor").update();
				$("auto_motor").enable();
				elem = new Element('option',{'value' : '-'});
				elem.update("Bitte Warten...");
				$("auto_motor").insert(elem);

		  },
		  onSuccess: function(transport)
			{
				var json = transport.responseText.evalJSON(true);

				if(json != "none")
				{
					$("auto_motor").update();
					$("auto_motor").enable();
					$('selected_cat').hide();
					$("auto_motor").insert(new Element('option',{'value' : '-'}).update('Bitte Motor ausw&auml;hlen'));
					i='cars';
					if(typeof(json[i]) == "object")
					{
						for(var x in json[i])
						{
							if(json[i][x]['mt_id'] != undefined)
							{
								$("auto_motor").insert(new Element('option',{'value' : json[i][x]['mt_id']}).update((json[i][x]['mt_name'])));
							}
						}
					}
				}
				else
				{
					$("auto_motor").update();
					$("auto_motor").enable();
					$('selected_cat').hide();
					$("auto_motor").insert(new Element('option',{'value' : '-'}).update('Keine Motoren gefunden!'));
				}
			}
		});
	}
	else
	{
		$("auto_motor").update();
		$("auto_motor").enable();
		$("auto_motor").insert(new Element('option',{'value' : '-'}).update('Bitte Motor ausw&auml;hlen'));
	}
}

function sel_mdl_top(id)
{
	if(id != "-")
	{

		new Ajax.Request('php/json/index.php?sec=model&id='+id, {
		  method:'get',
		  onCreate: function()
		  {
			  	$("auto_motor_top").update();
				$("auto_motor_top").enable();
				elem = new Element('option',{'value' : '-'});
				elem.update("Bitte Warten...");
				$("auto_motor_top").insert(elem);

		  },
		  onSuccess: function(transport)
			{
				var json = transport.responseText.evalJSON(true);
				if(json != "none")
				{
					$("auto_motor_top").update();
					$("auto_motor_top").enable();
					$("auto_motor_top").insert(new Element('option',{'value' : '-'}).update('Bitte Motor ausw&auml;hlen'));
					i='cars';
					if(typeof(json[i]) == "object")
					{
						for(var x in json[i])
						{
							if(json[i][x]['mt_id'] != undefined)
							{
								$("auto_motor_top").insert(new Element('option',{'value' : json[i][x]['mt_id']}).update((json[i][x]['mt_name'])));
							}
						}
					}
				}
				else
				{
					$("auto_motor_top").update();
					$("auto_motor_top").enable();
					$("auto_motor_top").insert(new Element('option',{'value' : '-'}).update('Keine Motoren gefunden!'));
				}
			}
		});
	}
	else
	{
		$("auto_motor_top").update();
		$("auto_motor_top").enable();
		$("auto_motor_top").insert(new Element('option',{'value' : '-'}).update('Bitte Motor ausw&auml;hlen'));
	}
}

/**
 *
 * @access public
 * @return void
 **/
function car_selection()
{
	Effect.toggle('car_selector','Blind',{'duration':0.2});
}

/**
 *
 * @access public
 * @return void
 **/
function this_cat(id,name,cat_id)
{
	new Ajax.Request('php/json/index.php?sec=auto_cat&id='+id, {
					 onSuccess: function(transport)
					 {
						 var json = transport.responseText.evalJSON(true);
						 document.location.href = json[0]+'/'+json[1]+'/'+json[2]+'/'+name+'/Alle-Hersteller-m'+id+'-c'+cat_id+'-Alle.html'; 
					 }}
		  );
}

function get_cat()
{
	if(!isNaN($('auto_motor').value))
	{
		document.location.href = $('auto_hrst').options[$('auto_hrst').selectedIndex].innerHTML.replace(/ /g,'-')+'/'+$('auto_model').options[$('auto_model').selectedIndex].innerHTML.replace(/ /g,'-')+'/'+$('auto_motor').options[$('auto_motor').selectedIndex].innerHTML.replace(/ /g,'-')+'_m'+$('auto_motor').value+'.html';
	}
	else
	{
		alert("Bitte einen Motor Auswählen");
	}
}

function get_cat_top()
{
	if(!isNaN($('auto_motor_top').value))
	{
		document.location.href = $('auto_hrst_top').options[$('auto_hrst_top').selectedIndex].innerHTML.replace(/ /g,'-')+'/'+$('auto_model_top').options[$('auto_model_top').selectedIndex].innerHTML.replace(/ /g,'-')+'/'+$('auto_motor_top').options[$('auto_motor_top').selectedIndex].innerHTML.replace(/ /g,'-')+'_m'+$('auto_motor_top').value+'.html';
	}
	else
	{
		alert("Bitte einen Motor Auswählen");
	}
}






/**
 *
 * @access public
 * @return void
 **/
function highlight_on_1(id){
	$('pp_'+id).style.backgroundColor = '#115E97';
	$('low_f_'+id).style.backgroundColor = '#937115';
	$('low_r_'+id).style.backgroundColor = '#566474';
	$('ax_f_'+id).style.backgroundColor = '#937115';
	$('ax_r_'+id).style.backgroundColor = '#566474';
	$('preis_'+id).style.backgroundColor = '#115E97';

	$('pp_'+id).style.color = '#EEEEEE';
	$('low_f_'+id).style.color = '#EEEEEE';
	$('low_r_'+id).style.color = '#EEEEEE';
	$('ax_f_'+id).style.color = '#EEEEEE';
	$('ax_r_'+id).style.color = '#EEEEEE';
	$('preis_'+id).style.color = '#EEEEEE';
	$('link_'+id).style.color = '#FF0000';

}

function highlight_off_1(id){
	$('pp_'+id).style.backgroundColor = '#CAE4F9';
	$('low_f_'+id).style.backgroundColor = '#F1DB9E';
	$('low_r_'+id).style.backgroundColor = '#E2E6E9';
	$('ax_f_'+id).style.backgroundColor = '#F1DB9E';
	$('ax_r_'+id).style.backgroundColor = '#E2E6E9';
	$('preis_'+id).style.backgroundColor = '#CAE4F9';

	$('pp_'+id).style.color = '#000000';
	$('low_f_'+id).style.color = '#000000';
	$('low_r_'+id).style.color = '#000000';
	$('ax_f_'+id).style.color = '#000000';
	$('ax_r_'+id).style.color = '#000000';
	$('preis_'+id).style.color = '#000000';
	$('link_'+id).style.color = '#990000';

}

function highlight_on_2(id){
	$('pp_'+id).style.backgroundColor = '#115E97';
	$('low_f_'+id).style.backgroundColor = '#937115';
	$('low_r_'+id).style.backgroundColor = '#566474';
	$('ax_f_'+id).style.backgroundColor = '#937115';
	$('ax_r_'+id).style.backgroundColor = '#566474';
	$('preis_'+id).style.backgroundColor = '#115E97';

	$('pp_'+id).style.color = '#EEEEEE';
	$('low_f_'+id).style.color = '#EEEEEE';
	$('low_r_'+id).style.color = '#EEEEEE';
	$('ax_f_'+id).style.color = '#EEEEEE';
	$('ax_r_'+id).style.color = '#EEEEEE';
	$('preis_'+id).style.color = '#EEEEEE';
	$('link_'+id).style.color = '#FF0000';



}

function highlight_off_2(id){
	$('pp_'+id).style.backgroundColor = '#A3C9E0';
	$('low_f_'+id).style.backgroundColor = '#EDBB54';
	$('low_r_'+id).style.backgroundColor = '#BAC4CD';
	$('ax_f_'+id).style.backgroundColor = '#EDBB54';
	$('ax_r_'+id).style.backgroundColor = '#BAC4CD';
	$('preis_'+id).style.backgroundColor = '#A3C9E0';

	$('pp_'+id).style.color = '#000000';
	$('low_f_'+id).style.color = '#000000';
	$('low_r_'+id).style.color = '#000000';
	$('ax_f_'+id).style.color = '#000000';
	$('ax_r_'+id).style.color = '#000000';
	$('preis_'+id).style.color = '#000000';
	$('link_'+id).style.color = '#990000';

}


/**
 *
 * @access public
 * @return void
 **/
function changeDisplay()
{
	if($('lieferanschrift').style.display == "none")
	{
		$('lieferanschrift').style.display = "block";
		return true;
	}
	else
	{
		$('lieferanschrift').style.display = "none";
		return true;
	}

}

/**
 *
 * @access public
 * @return void
 **/
function update_count()
{
	new Ajax.Request('php/json/index.php?sec=update_blue', {
		method:'get',
		onSuccess: function(transport)
		{
			var json = transport.responseText.evalJSON(true);
			if(json[0] != "empty")
			{
				$('blue_box').fade({'duration':0.2,from: 1, to: 0.3});
				window.setTimeout("$('blue_box').update('<img src=\"html/img/minibasket.png\" alt=\"Warenkorb:\" style=\"float:left;\"/><a href=\"warenkorb.html\" style=\"color:#FFF\"><b>"+json[0]+"</b> Artikel im Warenkorb - Gesamt "+json[1]+" &euro; - Zum Warenkorb</a>');",250);
				window.setTimeout("$('blue_box').appear({'duration':0.2});",300);
			}
			else
			{
				$('blue_box').fade({'duration':0.2,from: 1, to: 0.3});
				window.setTimeout("$('blue_box').update('"+json[1]+"');",250);
				window.setTimeout("$('blue_box').appear({'duration':0.2});",300);
			}
		}
	});
}

/**
 *Array ( [basket_art_id] => 178 [ac] => basket_add [basket_amount] => Array ( [0] => 1 [1] => 1 ) [basket_car] => 775 [basket_cost] => 616.00
 * @access public
 * @return void
 **/
function add_basket()
{
	$('ac').value = 'basket_add_js';
	new Ajax.Request('warenkorb.html', {
		method:'post',
		parameters:  $('basket_add').serialize(true),
		onCreate: function()
		{
			Effect.Fade('added', { 'duration': 0.0 });
			Effect.Appear('wait', { 'duration': 0.2 });
			Effect.Appear('wait_black',{'duration':0.2});
		},
		onFailure: function(transport)
		{
			$('msg').innerHTML = "<h3>Ein Fehler ist aufgetreten, bitte versuchen Sie es erneut!</h3>";
			window.setTimeout("Effect.Fade('wait', { 'duration': 0.2 });",210);
			window.setTimeout("Effect.Appear('error', { 'duration': 0.5 });",210);
		},
		onSuccess: function(transport)
		{
			//alert(transport.responseText);
			if(transport.responseText == "basket_added")
			{
				window.setTimeout("Effect.Fade('wait', { 'duration': 0.2 });",210);
				window.setTimeout("Effect.Appear('added', { 'duration': 0.5 });",210);
				update_count();
			}
			else if(transport.responseText == "basket_car")
			{
				$('msg').innerHTML = "<h3>Bitte w&auml;hlen Sie Ihren Motor aus!</h3>";
				window.setTimeout("Effect.Fade('wait', { 'duration': 0.2 });",210);
				window.setTimeout("Effect.Appear('error', { 'duration': 0.5 });",210);
			}
		}
	});
	return false;


}

/**
 *
 * @access public
 * @return void
 **/
function update_basket(id,amount)
{
	if(amount == "0")
	{
		delete_artikel(id);
	}
	else if(amount != "" && (!isNaN(id) && !isNaN(amount)))
	{
		new Ajax.Request('php/json/index.php?sec=basket_update&amount='+amount+'&id='+id, {
			method:'get',
			onSuccess: function(transport)
			{
				if(transport.responseText != "empty")
				{
					var json = transport.responseText.evalJSON(true);
					$('gesamtbetrag').fade({'duration':0.3,from: 1, to: 0.3 });
					$('basket_show_gesamt_'+id).fade({'duration':0.3,from: 1, to: 0.3});
					window.setTimeout("$('gesamtbetrag').innerHTML = '<u><b>"+json['gesamt']+" &euro;</b></u><br />"+json['mwst']+" &euro;';",450);
					window.setTimeout("$('basket_show_gesamt_"+id+"').innerHTML = '"+json['artikel']+"&euro;';",450);
					window.setTimeout("$('gesamtbetrag').appear({'duration':0.25});",500);
					window.setTimeout("$('basket_show_gesamt_"+id+"').appear({'duration':0.25});",500);
				}
				update_count();
			}
		});
	}
}

/**
 *
 * @access public
 * @return void
 **/
function delete_artikel(id)
{
	new Ajax.Request('php/json/index.php?sec=basket_delete&id='+id, {
		method:'get',
		onSuccess: function(transport)
		{
			update_count();
			if(transport.responseText != "empty")
			{
				var json = transport.responseText.evalJSON(true);
				$('gesamtbetrag').innerHTML = "<u><b>"+json['gesamt']+" &euro;</b></u><br />"+json['mwst']+" &euro;";
				Effect.BlindUp('basket_entry_'+id,{'duration':0.3});
				document.setTimeout("$('basket_entry_"+id+"').remove();",350);

			}
			else if(transport.responseText == "empty")
			{
				Effect.BlindUp('my_basket',{'duration':0.2});
				window.setTimeout("$('my_basket').className = 'basket_empty';",350);
				window.setTimeout("$('my_basket').innerHTML = 'Der Warenkorb ist leer';",350);
				window.setTimeout("Effect.Appear('my_basket');",500);
			}
		}
	});
}

/**
 *
 * @access public
 * @return void
 **/
function country_sel(id)
{
	$('knt_ct').value = id;
	$('knt_ct_liefer').value = id;
	new Ajax.Request('php/json/index.php?sec=change_country&id='+id, {
		method:'get',
		onSuccess: function(transport)
		{
			var json = transport.responseText.evalJSON(true);
			$('added').appear({'duration':0.3});
			$('wait_black').appear({'duration':0.3});
			$('gesamtbetrag').fade({'duration':0.3,from: 1, to: 0.3 });
			window.setTimeout("$('gesamtbetrag').innerHTML = '<u><b>"+json['gesamt']+" &euro;</b></u><br />"+json['mwst']+" &euro;';",450);
			window.setTimeout("$('gesamtbetrag').appear({'duration':0.25});",500);

			if($('vor_betrag'))
			{
				$('vor_betrag').fade({'duration':0.3,from: 1, to: 0.3 });
				window.setTimeout("$('vor_betrag').innerHTML = '"+json['betrag']+" &euro;'",450);
				window.setTimeout("$('vor_betrag').appear({'duration':0.25});",500);
				$('pay_betrag').fade({'duration':0.3,from: 1, to: 0.3 });
				window.setTimeout("$('pay_betrag').innerHTML = '"+json['payment']+" &euro;'",450);
				window.setTimeout("$('pay_betrag').appear({'duration':0.25});",500);
			}

			if($('vrs_name'))
			{
				$('vrs_name').fade({'duration':0.3,from: 1, to: 0.3 });
				$('vrs_betrag').fade({'duration':0.3,from: 1, to: 0.3 });
				$('vrs_gesamt').fade({'duration':0.3,from: 1, to: 0.3 });
				window.setTimeout("$('vrs_name').innerHTML = '"+json['versand_name']+" &euro;'",450);
				window.setTimeout("$('vrs_name').appear({'duration':0.25});",500);
				window.setTimeout("$('vrs_betrag').innerHTML = '"+json['versand']+" &euro;'",450);
				window.setTimeout("$('vrs_betrag').appear({'duration':0.25});",500);
				window.setTimeout("$('vrs_gesamt').innerHTML = '"+json['versand']+" &euro;'",450);
				window.setTimeout("$('vrs_gesamt').appear({'duration':0.25});",500);
			}

			update_count();

			if($('basket_artikel').style.display == 'none')
			{
				show_artikel();
			}

		}
	});

}


function basket_fade()
{
	$('added').fade({'duration':0.2});
	$('wait_black').fade({'duration':0.2});
}

/**
 *
 * @access public
 * @return void
 **/
function error_fade(){
	$('error').fade({'duration':0.2});
	$('wait_black').fade({'duration':0.2});
}

/**
 *
 * @access public
 * @return void
 **/
function motor_selected(bool)
{

	if(bool == true)
	{
		if($('sel_basket_car').value != "-")
		{
			$('motoren').hide();
			$('motor').show();
			$('basket_car').value = $('sel_basket_car').value;
			$('basket_car_desc').innerHTML = $('sel_basket_car').options[$('sel_basket_car').selectedIndex].text;
			$('motor_info').hide();

		}
	}
	else
	{
		$('motoren').show();
		$('motor').hide();
		$('motor_info').show();
	}
}

/**
 *
 * @access public
 * @return void
 **/
function show_artikel()
{
	Effect.toggle('basket_artikel','blind',{'duration':0.3});
	Effect.toggle('show_img','blind',{'duration':0.3});
	Effect.toggle('hide_img','blind',{'duration':0.3});
}

/**
 *
 * @access public
 * @return void
 **/

function show_big_img(image)
{
	bild = $('show_img');
	bild.src = 'sportfahrwerk-bild/full/' + image;
	
	$('wait_black').appear({'duration':0.1});
	$('circle_ball').show();
	
	$('wait_black').onclick = (function(){hide_big_img()}).bind(this);
	
	bild.onload = (function()
	{
		breite = bild.width;
		hoehe = bild.height;
		
		var width = window.innerWidth || (window.document.documentElement.clientWidth || window.document.body.clientWidth);
		var height = window.innerHeight || (window.document.documentElement.clientHeight || window.document.body.clientHeight);
	/*
		if(width < breite || height < hoehe)
		{	
			if(width <= height)
			{
				bild.src = 'sportfahrwerk-bild/max'+(width-20)+'/'+image;
			}
			else
			{
				bild.src = 'sportfahrwerk-bild/max'+(height-20)+'/'+image;
			}
				
				bild.onload(function()
								 {
							        
									breite = bild.width;
									hoehe = bild.height;
									
									$('show_img_div').style.width = breite+'px';
									$('show_img_div').style.height = hoehe+'px';
								
									$('show_img_div').style.marginTop = '-'+((hoehe+50)/2)+'px';
									$('show_img_div').style.marginLeft = '-'+((breite+50)/2)+'px';
								
									$('show_img_div').appear({'duration':0.2});
									$('circle_ball').hide();
								
									$('close_btn').style.position = 'absolute';
									$('close_btn').style.right = '25px';
									$('close_btn').style.top = '0px;';
								 }).bind(this);
		}
		else
		{*/
			$('show_img_div').style.width = breite+'px';
			$('show_img_div').style.height = hoehe+'px';
		
			$('show_img_div').style.marginTop = '-'+((hoehe+50)/2)+'px';
			$('show_img_div').style.marginLeft = '-'+((breite+50)/2)+'px';
		
			$('show_img_div').appear({'duration':0.2});
			$('circle_ball').hide();
		
			$('close_btn').style.position = 'absolute';
			$('close_btn').style.right = '25px';
			$('close_btn').style.top = '0px;';
		//}

	}).bind(this);

}

function hide_big_img()
{
	$('show_img_div').fade({'duration':0.2});
	$('wait_black').fade({'duration':0.2});
	$('circle_ball').hide();
}

