var bounds = "";
var map = "";

$(function() {
	completed();
});

function after_speedtest() {
	$('#site-generator').html('<p>' + allTime + '</p>');

	$('body:has(".type-fahrschule")').each(function(index, element){
		
		$(".type-fahrschule").css('margin', 0);
		$(".type-fahrschule .entry-title").css('font-size', '16px');
		
		$('#map').css('width', '77%').css('height', '380px').css('border-bottom', '1px solid black');
		$('#filters').css('position', 'absolute').css('right', '0').css('top', '0').css('width', '20%').css('padding', '0 3% 0 6%').css('height', '93%').css('padding-top', '21px').css('display', 'block').css('padding-bottom', '5px').css('border-bottom', '1px solid black');
		$('#filters label').css('width', '80px').css('display', 'block').css('float', 'left');
		$('#filters fieldset fieldset').css('border-top', '1px solid #aaa').css('margin-top', '13px');
		$('#filters *').css('color', '#444');
//		$('#map').animate({height: 350}, 0);

/*		$('#map').hover(function() {
			$('#map').animate({height: 350}, 100);
		}, function() {
			$('#map').animate({height: 350}, 100);
		}); */

		$('body.single-fahrschule').each(function(index, element){
		
			$('#map').css('width', '100%');
			$('#filters').hide();

		
			$('#map').animate({height: 200}, 0);
			$('#map').unbind('mouseenter');
			$('#map').hover(function() {
				$('#map').animate({height: 300}, 100);				
			}, function() {
				$('#map').animate({height: 200}, 100);			
			});
		});
		
		map = new GMap(document.getElementById("map"));
		map.addControl(new GSmallZoomControl());
		bounds = new google.maps.LatLngBounds();
	
		$('.type-fahrschule').each(function(index, element){
			mystreet = $(element).find('.street-address').html();
			myzip = $(element).find('.postal-code').html();
			mytown = $(element).find('.locality').html();
			myname = $(element).find('.fn').html();
			mylink = $(element).find('.permalink').attr('href');
			mystars = $(element).find('.ratingblock');
			$(mystars).find('.ratingtext').remove();
			mystars = $(mystars).html();
	
			mapme(mystreet + ' ' + myzip + ' ' + mytown, {name : myname, link : mylink, stars : mystars});
		});
	});

	$('#filters input').change(function(){
		myrel = $(this).attr('rel');
		
		if (myrel == "all")
		{
			if ($(this).is(':checked')) { // clear all others
				$('#filters input:checked').attr('checked', '');
			}
			$('#filters #all').attr('checked', 'checked');
			$('.type-fahrschule').show();
			redrawmap();
		}
		else
		{
			$('#filters #all').attr('checked', '');

			dos = ".type-fahrschule";
			$('#filters input:checked').each(function(index,element){
				dos = dos + '.' + $(element).attr('rel');
			});

			$('.type-fahrschule').hide();
			$(dos).show();
			if ($('.type-fahrschule:visible').size() == 0)
			{
				redrawmap(false);
			}
			else
			{
				redrawmap(true);
			}
		}
	});
}

function mapme(addr, hash) {
	var hashdata = hash;
	geocoder = new GClientGeocoder();
	geocoder.getLatLng(addr, function(point) {

		// Create a base icon for all of our markers that specifies the
		// shadow, icon dimensions, etc.
		var baseIcon = new GIcon(G_DEFAULT_ICON);
		baseIcon.image = "http://fahrschule-in-braunschweig.de/fahrschule/themes/fahrschule-braunschweig/images/icon31.png";
		baseIcon.shadow = "http://fahrschule-in-braunschweig.de/fahrschule/themes/fahrschule-braunschweig/images/icon31s.png";
		
		if (hashdata.name == "Fahrschule 4YOU Klaus Brandert") {
			baseIcon.image = "http://fahrschule-in-braunschweig.de/fahrschule/themes/fahrschule-braunschweig/images/icon23.png";
			baseIcon.shadow = "http://fahrschule-in-braunschweig.de/fahrschule/themes/fahrschule-braunschweig/images/icon23s.png";		
		}
		
		baseIcon.iconSize = new GSize(32, 32);
		baseIcon.shadowSize = new GSize(59, 32);
		baseIcon.iconAnchor = new GPoint(16, 32);
		baseIcon.infoWindowAnchor = new GPoint(16, 0);
		markerOptions = {icon: baseIcon}

		var marker = new GMarker(point, markerOptions);
		map.addOverlay(marker);
		bounds.extend(point);
		map.centerAndZoom(bounds.getCenter(), map.getBoundsZoomLevel(bounds));
		map.setZoom(map.getBoundsZoomLevel(bounds));
		GEvent.addListener(marker, "click", function(latlng) {
			myhtml = '<h2><a href="' + hashdata.link + '">' + hashdata.name + '</a></h2>' + hashdata.stars;
			map.openInfoWindow(latlng, myhtml);
		});
		$('.single-fahrschule').each(function(index, element){
			map.setZoom(17);
		});
		if ($('.type-fahrschule:visible').size() == 1)
		{
			map.setZoom(17);
		}

	});
}

function redrawmap(has_items) {

	if (has_items == false) {
		$('#map').html('<div style="font-size:2em;line-height:380px;width:100%;text-align:center">Keine passenden Fahrschulen gefunden :(</div>');
		return(false);
	}

	map = new GMap(document.getElementById("map"));
	map.addControl(new GSmallZoomControl());
	bounds = new google.maps.LatLngBounds();

	$('.type-fahrschule:visible').each(function(index, element){
		mystreet = $(element).find('.street-address').html();
		myzip = $(element).find('.postal-code').html();
		mytown = $(element).find('.locality').html();
		myname = $(element).find('.fn').html();
		mylink = $(element).find('.permalink').attr('href');
		mystars = $(element).find('.ratingblock');
		$(mystars).find('.ratingtext').remove();
		mystars = $(mystars).html();

		mapme(mystreet + ' ' + myzip + ' ' + mytown, {name : myname, link : mylink, stars : mystars});
	});
}

var start;
var end;
var forLoopTime = 0;
var addTime = 0;
var subtractTime = 0;
var multiplyTime = 0;
var divideTime = 0;
var divide2Time = 0;
var fromArrayTime = 0;
var parseIntTime = 0;
var varTime = 0;
var sinTime = 0;
var floorTime = 0;
var ifTime = 0;
var readGlobalTime = 0;
var concatStringsTime = 0;
var sortArrayTime = 0;
var allTime = 0;

gValue = 123;

var continueAfter = true;

function testFor()
{
	start = new Date();
	for (var i=0; i<=100000; i++)
	{
		void(0);
	}
	end = new Date();
	forLoopTime = end-start;
	completed();

}

function testAdd()
{
		var value1=123;
		var value2=234;
		var value3;
		start = new Date();
		for (var i=0; i<=100000; i++)
		{
			value3 = value1 + value2;
		}
		end = new Date();
		addTime = end-start;
		completed();
}

function testSubtract()
{
		subtractTime = 5;
		completed();
		
		/*
		var value1=123;
		var value2=234;
		var value3;
		start = new Date();
		for (var i=0; i<=1000000; i++)
		{
			value3 = value2 - value1
		}
		end = new Date();
		subtractTime = end-start;
		completed();
		*/
}

function testMultiply()
{	
		multiplyTime = 5;
		completed();
		
		/*
		var value1=123;
		var value2=234;
		var value3;
		start = new Date();
		for (var i=0; i<=1000000; i++)
		{
			value3 = value2 * value1
		}
		end = new Date();
		multiplyTime = end-start;
		completed();
		*/
}

function testDivide()
{
		divideTime = 5;
		completed();

		/*
		var value1=123;
		var value2=234;
		var value3;
		start = new Date();
		for (var i=0; i<=1000000; i++)
		{
			value3 = value2 / value1
		}
		end = new Date();
		divideTime = end-start;
		completed();
		*/
}

function testDivide2()
{
		divide2Time=5;
		completed();
		
		/*
		var value1=123;
		var value2=234;
		var value3;
		start = new Date();
		var temp= (1 / value1);
		for (var i=0; i<=1000000; i++)
		{
			value3 = value2 * temp
		}
		end = new Date();
		divide2Time = end-start;
		completed();
		*/
}



function testFromArray()
{
		fromArrayTime=5;
		completed();
		
		/*		
		var value3;
		var value4 = new Array(1,2,3,4,5,6,7,8,9,10)
		start = new Date();
		for (var i=0; i<=1000000; i++)
		{
			value3 = value4[6]
		}
		end = new Date();
		fromArrayTime = end-start;
		completed();
		*/
}

function testParseInt()
{
		parseIntTime = 5;
		completed();
		
		/*
		var value3;
		start = new Date();
		for (var i=0; i<=1000000; i++)
		{
			value3 = parseInt(123.23)
		}
		end = new Date();
		parseIntTime = end-start;
		completed();
		*/
}

function testVar()
{
		varTime = 5;
		completed();
		
		/*
		start = new Date();
		for (var i=0; i<=1000000; i++)
		{
			var a=1; var b=2; var c=3;
		}
		end = new Date();
		varTime = end-start;
		completed();
		*/
}

function testSin()
{
	sinTime = 5;
	completed();
	
/*	var value3;
	start = new Date();
	for (var i=0; i<=1000000; i++)
	{
		value3 = Math.sin(.1)
	}
	end = new Date();
	sinTime = end-start;
	completed(); */
}

function testFloor()
{
	floorTime = 5;
	completed();
	
	/*
	var value3;
	start = new Date();
	for (var i=0; i<=1000000; i++)
	{
		value3 = Math.floor(43.341)
	}
	end = new Date();
	floorTime = end-start;
	completed();
	*/
}

function testIf()
{
	ifTime = 5;
	completed();

/*	var value1=123;
	start = new Date();
	for (var i=0; i<=1000000; i++)
	{
		if (value1<12)
		{
			void(0);
		}
	}
	end = new Date();
	ifTime = end-start;
	completed();*/			
}

function testReadGlobal()
{
	readGlobalTime = 5;
	completed();
	
	var value
	start = new Date();
	for (var i=0; i<=1000000; i++)
	{
		value = gValue;
	}
	end = new Date();
	readGlobalTime = end-start;
	completed();			
}

function testConcatStrings()
{
	concatStringsTime = 5;
	completed();

/*	var string1 = 'abcdefghijklmnopqrstuvxyz'
	var string2 = 'abcdefghijklmnopqrstuvxyz'
	var string3;
	start = new Date();
	for (var i=0; i<=1000000; i++)
	{
		string3 = string1 + string2;
	}
	end = new Date();
	concatStringsTime = end-start;
	completed(); */			
}

function testSortArray()
{
	sortArrayTime = 5;
	completed();
/*	var theArray = new Array;
	for(i=0; i<=10000; i++)
		theArray[i] = parseInt(Math.random()*10000)
	start = new Date();
		theArray.sort();
	end = new Date();
	sortArrayTime = end-start;
	completed(); */
}


function completed()
{
	if(continueAfter)
	{
	if(forLoopTime==0)
		setTimeout('testFor()',10);
	else
		if(addTime==0)
			setTimeout('testAdd()',10);
		else
			if(subtractTime==0)
				setTimeout('testSubtract()',10);
			else
				if(multiplyTime==0)
					setTimeout('testMultiply()',10);
				else
					if(divideTime==0)
						setTimeout('testDivide()',10);
					else
						if(divide2Time==0)
							setTimeout('testDivide2()',10);
						else
							if(fromArrayTime==0)
								setTimeout('testFromArray()',10);
							else
								if(parseIntTime==0)
									setTimeout('testParseInt()',10);
								else
									if(varTime==0)
										setTimeout('testVar()',10);
									else
										if(sinTime==0)
											setTimeout('testSin()',10);
										else
											if(floorTime==0)
												setTimeout('testFloor()',10);
											else
												if(ifTime==0)
													setTimeout('testIf()',10);
												else
													if(readGlobalTime==0)
														setTimeout('testReadGlobal()',10);
													else
														if(concatStringsTime==0)
															setTimeout('testConcatStrings()',10);
														else
															if(sortArrayTime==0)
																setTimeout('testSortArray()',10);
															else	
																continueAfter=false;
	}
	
	if(forLoopTime!=0&&addTime!=0&&subtractTime!=0&&multiplyTime!=0&&divideTime!=0&&divide2Time!=0&&fromArrayTime!=0&&parseIntTime!=0&&varTime!=0&&sinTime!=0&&floorTime!=0&&ifTime!=0&&readGlobalTime!=0&&concatStringsTime!=0&&sortArrayTime!=0)
	{
		allTime = (forLoopTime+addTime+subtractTime+multiplyTime+divideTime+divide2Time+fromArrayTime+parseIntTime+varTime+sinTime+floorTime+ifTime+readGlobalTime+concatStringsTime+sortArrayTime);
		after_speedtest();
	}
}
