	var $j = jQuery.noConflict();
	var headline_count;
	var headline_interval;
	var old_headline = 0;
	var current_headline=0;
	var blnResizing = false;
	var map = null;
	var markers = MultiDimensionalArray(100,0);
	
	var markersFull = [];
	var markerids = [];
	var strCheckedCategory = null;
	var markerIcons = []
	var currentBubbleMarker;
	
	function meld(text)
	{
//		console.log(text);
	}
	
	
	function viewMarker(intCompany, intId)
	{
		$j.post("/viewcounter.php", { companyId: intCompany, locationId: intId }, function(data) {
			// do nothing
		});
	}
	
	function MultiDimensionalArray(iRows,iCols)
	{
	var i;
	var j;
	   var a = new Array(iRows);
	   for (i=0; i < iRows; i++)
	   {
	       a[i] = new Array(iCols);
	       for (j=0; j < iCols; j++)
	       {
	           a[i][j] = "";
	       }
	   }
	   return(a);
	} 
	
	var fncMouseOverMarker = function()
	{
		
		var cards = $j.cookie('discountcard').split('+');
		var markerId = findMarker($j(this).attr('company') + '_' + $j(this).attr('location'));
		meld(markerId);
		if(markerId != null)
		{
			var theMarker = markersFull[parseInt(markerId)];
			
			for( k in cards)
			{
				if($j.stringarray.has(theMarker.cards, cards[k]))
				{
					
					var markerIcon = markerIcons[parseInt(markerId)];
					meld(theMarker);
					meld(markerIcon)
//					markerIcon.image = theMarker.getIcon().image.replace('icon', 'over');
//					alert(theMarker.getIcon().image);
					
					map.removeOverlay(theMarker);
					map.addOverlay(theMarker);
					bubbleOver.openOnMarker(theMarker,'<img src="' + theMarker.getIcon().image + '" style="float:left;" /><div style="margin-top: 2px; padding-left: 10px; width: 180px; overflow:hidden"><font style="display: block; color: #f09; font-weight: bold; text-align: left; height: 18px; width: 500px; overflow: hidden;">' + $j(this).find('strong').html() + '</font></div>');
					bubble.hide();
					viewMarker($j(this).attr('company'), $j(this).attr('location'));
					return;
				}
			}
			
			
		        
		        
		}
	};
	
	var fncMouseOutMarker = function()
	{
		var cards = $j.cookie('discountcard').split('+');
		var markerId = findMarker($j(this).attr('company') + '_' + $j(this).attr('location'));
		meld(markerId);
		if(markerId != null)
		{
			var theMarker = markersFull[parseInt(markerId)];
		
			
			for( k in cards)
			{
				if($j.stringarray.has(theMarker.cards, cards[k]))
				{
					
					var markerIcon = markerIcons[parseInt(markerId)];
					meld(theMarker);
					meld(markerIcon)
//					markerIcon.image = theMarker.getIcon().image.replace('over', 'icon');
					meld(theMarker.getIcon());
//					map.removeOverlay(theMarker);
//					map.addOverlay(theMarker);
					bubbleOver.hide();
				}
			}
			
		}
	};
	
	var setBannerClickLastFour = false;
	var fncGetNewBanners = function()
	{
		
			var fncClickBanner = function() {
				var strUrl = $j(this).attr('href');
				$j.post("/bannerclick.php", { companyId: $j(this).attr('companyId'), bannerId: $j(this).attr('bannerId')}, function() {
				});
			};
			
			var counter = 0;
			var lis = new Array(8);
			var ids = new Array(8);
			
			var first = function()
			{
				ids[counter] = $j(this).find('a.simply-scroll-over').attr('bannerid');
				lis[counter++] = $j(this);
				
			}
			var second = function()
			{
				if(counter++ < 4)
				{
					$j(this).html(lis[counter + 3].html());
				}
			}
			var newBanner = function()
			{
				
				var arrArgs;
				if(map)
				{
					arrArgs = { swlat: map.getBounds().getSouthWest().lat(), nelat: map.getBounds().getNorthEast().lat(), nelon: map.getBounds().getNorthEast().lng(), swlon: map.getBounds().getSouthWest().lng(), categories: strCheckedCategory, cards: $j.cookie('discountcard'), zoom: map.getZoom(), first: ids[4], second: ids[5], third: ids[6], fourth: ids[7] }
				}
				else
				{
					arrArgs = { home: 'true', categories: strCheckedCategory, cards: $j.cookie('discountcard'), first: ids[4], second: ids[5], third: ids[6], fourth: ids[7] }
				}
				$j.post("/banner.php", arrArgs, function(data)
						{
					
							lis[4].html(data.first);
							lis[5].html(data.second);
							lis[6].html(data.third);
							lis[7].html(data.fourth);
							if(setBannerClickLastFour)
							{
								lis[4].find("a").click(fncClickBanner);
								lis[5].find("a").click(fncClickBanner);
								lis[6].find("a").click(fncClickBanner);
								lis[7].find("a").click(fncClickBanner);
								
							}
							setBannerClickLastFour = true;
						}, "json");
				
			}
			
			$j('#scroller li').each(first);
			if(counter == 8)
			{
				counter = 0;
				$j('#scroller li').each(second);
				
				newBanner();
			}
			else
			{
				setTimeout("fncGetNewBanners()", 500);
			}
		
	}

    function checkprisk(priority)
    {
        
        switch(parseInt(priority))
        {
            case 5:
                return true;
            case 4:
            case 3:
                return true;
            case 2:
                if(map.getZoom() >= 13) return true;
            case 1:
                if(map.getZoom() >= 14) return true;


        }
        return false;
    }
	
	function getNewMarkers()
	{
		
		$j.post("/markers.php", { swlat: map.getBounds().getSouthWest().lat(), nelat: map.getBounds().getNorthEast().lat(), nelon: map.getBounds().getNorthEast().lng(), swlon: map.getBounds().getSouthWest().lng(), zoom: map.getZoom() }, function(data)
		{
			var categories = new Object();
			var disccards = new Object();
			var cardString = $j.cookie('discountcard');
			if(cardString)
			{
				var cards = $j.cookie('discountcard').split('+');
			}
			else
			{
				var cards = [];
			}
			
			$j('.category_check').each(function() {
				categories[$j(this).attr('nr')] = 0;
			});
			
			$j('.discount_check').each(function() {
				disccards[$j(this).attr('nr')] = 0;
			});
			
			for(test in data.locations)
			{
				
				var location = data.locations[test];
				if(!markerExists(location.companyid + "_" + location.id))
				{
					
					number = location.category;
					if(markers[number])
					{
						i = markers[number].length;
					}
					else
					{
						i = 0;
						markers[number] = new Array();
					}
					
					j = markersFull.length;
					
					markersFull[j] = createMarker(location.lat, location.lon, '', location.html, number, location.companyid, location.id, location.name);
                    markersFull[j].priority = location.priority;
					if(location.cards)
					{
						markersFull[j].cards = location.cards.replace('_', '+');
					}
					else
					{
						markersFull[j].cards = "";
					}
					markers[number][i] = markersFull[j];
					markerids[j] = location.companyid + "_" + location.id;
					if($j('#category_check_' + number).is(':checked') && checkprisk(markerids[j].priority))
					{
						
						for( k in cards)
						{
							if($j.stringarray.has(markers[number][i].cards, cards[k]))
							{
								map.addOverlay(markers[number][i]);
							}
						}
						
						
					}
				}
				var arrlocationcards;
				//location.cards = '1_2';
				if(location.cards)
				{
					arrlocationcards = location.cards.split('_');
					
					
					if(location.cards.length > 2)
					{
						for(dcard =0; dcard < arrlocationcards.length; dcard++)
						{
							//alert(arrlocationcards[dcard]);
							if(disccards[arrlocationcards[dcard]])
							{
								disccards[arrlocationcards[dcard]]++;
								//console.log(location.id + '_' + location.companyid + 'plus' + arrlocationcards[dcard] );
							}
							else
							{
								disccards[arrlocationcards[dcard]] = 1;
								
							}
						}
					}
					else
					{
						if(disccards[location.cards])
						{
							disccards[location.cards]++;
							
						}
						else
						{
							disccards[location.cards] = 1;
							
						}
					}
				}
				
				if(categories[location.category])
				{
					categories[location.category]++;
					
				}
				else
				{
					categories[location.category] = 1;
					
				}
				
			}
			
			for(category in categories)
			{
				$j('#category_count_' + category).text('(' + categories[category] + ')');
			}
			
			for(card in disccards)
			{
				$j('#discount_count_' + card).text('(' + disccards[card] + ')');
			}

            var blnShowMarker = false;
            for(i = 0; i < markers.length; i++)
            {
                 for(j=0; j < markers[i].length; j++)
                 {
                     blnShowMarker = false;
                     if($j('#category_check_' + i).is(':checked') && checkprisk(markers[i][j].priority))
                     {
                        for( k in cards)
                        {
                            if($j.stringarray.has(markers[i][j].cards, cards[k]))
                            {
                                 blnShowMarker = true;
                            }
                        }
                     }


                     if(blnShowMarker)
                     {
                          map.addOverlay(markers[i][j]);
                     }
                     else
                     {
                         map.removeOverlay(markers[i][j]);
                     }

                 }
             }
			
		}, "json");
		var strCategories;
		var arrCategories = [];
		$j('.category_check:checked').each(function() {
			arrCategories[$j(this).attr('nr')] = $j(this).attr('nr');
		});
		strCategories = arrCategories.join('+');
		
		
		$j.post("/links.php", { swlat: map.getBounds().getSouthWest().lat(), nelat: map.getBounds().getNorthEast().lat(), nelon: map.getBounds().getNorthEast().lng(), swlon: map.getBounds().getSouthWest().lng(), categories: strCategories, cards: $j.cookie('discountcard'), zoom: map.getZoom() }, function(data)
		{
			$j('#discount_links').html(data);
			$j('.studentenkortingen-in-de-buurt').mouseover(fncMouseOverMarker);
			$j('.studentenkortingen-in-de-buurt1').mouseover(fncMouseOverMarker);
			$j('.studentenkortingen-in-de-buurt').mouseout(fncMouseOutMarker);
			$j('.studentenkortingen-in-de-buurt1').mouseout(fncMouseOutMarker);
			$j('.paging a').click(fncChangePage);
			
		});
	}

	var fncChangePage = function()
	{
		

		var arrId = $j(this).attr('href').split('#');
		var strId = arrId[1];
		var intId = strId.substr(5);
		
		$j('.page').hide();
		$j('#links_page_' + intId).show();
		$j(this).blur();
		
		$j.post('/pager.php', { current: intId, total: $j('#paging_container').attr('nrofpages')}, function(data) {
			$j('#paging_container').html(data);
			$j('.paging a').click(fncChangePage);
		});
		
		return false;
	}
	
	function checkEmail(strEmail) {
		if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(strEmail))
		{
			return true;
		}
		return false;
	}
	
	
	function gmapsAvailable()
	{
		if($j("input[name='lat']").attr('value') == parseFloat($j("input[name='lat']").attr('value')))
		{
			return true;
		}
		
		return false;
	}
	
	function markerExists(identification)
	{
		for(search in markerids)
		{
			if(markerids[search] == identification)
			{
				
				return true;
			}
		}
		
		
		return false;
	}
	
	function findMarker(identification)
	{
		
		for(search in markerids)
		{
			
			if(markerids[search] == identification)
			{
				
				return search;
			}
		}
		return null;
	}
	
	 function loadGMaps() {
		 
	 	if(gmapsAvailable())
	 	{
      		if (GBrowserIsCompatible()) {
      		
      			var setLat = parseFloat($j("input[name='lat']").attr('value'));
		        var setLon = parseFloat($j("input[name='lon']").attr('value'));
		      	var zoom = parseInt($j("input[name='zoom']").attr('value'));
		      	var cards = $j.cookie('discountcard').split('+');
		      	var i = [];
		      	var mapBounds = new GLatLngBounds();
		      	
      			if(!setLon) setLon = 0;
		        map = new GMap2(document.getElementById("google"));
		        map.setCenter(new GLatLng(setLat, setLon), zoom);
		        
		        map.addControl(new SK_MapsControl()); 
		        var topRight = new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(200,32));
//		        bubble = new EBubble(map, "http://www.studentenkorting.nl/templates/includes/menu.png",new GSize(200,50 total image size), new GSize(340,270 contents size), new GPoint(30,30 start position), new GPoint(120,356 position of marker));   
		        bubbleOver = new EBubble(map, "/images/mapcontrol/infobar.png",new GSize(225,30), new GSize(221,26), new GPoint(4,4), new GPoint(15,4), false);
		        bubble = new EBubble(map, "/images/mapcontrol/infowindow.png",new GSize(400,150), new GSize(385,109), new GPoint(9,9), new GPoint(200,150), true);   
		           
				$j('.googlemarker').each(function googleMarker()
				{
					if(parseInt($j(this).attr('googlelon')) == 0)
					{
						return true;
					}
					var number = parseInt($j(this).attr('number'));
					if(number > 100) number = 100;
					
					if(i[number] == null)
					{
						i[number] = 0;
					}
					
					j = markersFull.length;
					
					var arrIdentification = $j(this).attr('identification').split('_');
					                               
					markersFull[j] = createMarker($j(this).attr('googlelat'), $j(this).attr('googlelon'), '', $j(this).html(), number, arrIdentification[0], arrIdentification[1], $j(this).attr('companyname'));
					markersFull[j].cards = $j(this).attr('cards').replace('_', '+');
					markers[number][i[number]] = markersFull[j];
					markerids[j] = $j(this).attr('identification');                   
					                                     
					markerExists($j(this).attr('identification'));
					
					markers[number][i[number]].priority = $j(this).attr('priority');
					if($j('#category_check_' + number).is(':checked'))
					{
						
						
						
						for( k in cards)
						{
							if($j.stringarray.has(markersFull[j].cards, cards[k]))
							{
								if(checkprisk(markers[number][i[number]].priority))
                                {
                                    map.addOverlay(markers[number][i[number]]);
                                }
								mapBounds.extend(new GLatLng($j(this).attr('googlelon'), $j(this).attr('googlelat')));
								recognized = true;
							}
						}
						
						
					}
					i[number]++;
					
					return true;
				});
		
		
				map.setCenter(mapBounds.getCenter(), map.getBoundsZoomLevel(mapBounds));
		
				GEvent.addListener(map, 'moveend', function() {
					getNewMarkers();
			
        		});
		
				$j('.gmnoprint span').css('display', 'none');
                getNewMarkers();
      		}
      	}
    }
	
	
	function start_image_rotate(id)
	{
		$j(id).innerfade({ 
		speed: 'slow', 
		timeout: 6000, 
		type: 'sequence', 
		containerheight: '155px'
	});
		
	}
	
	function open_bubbleOver(marker, strCompanyName, html, intCompanyId, intLocationId)
	{
		if(currentBubbleMarker != marker)
		{
			bubbleOver.openOnMarker(marker,'<img id="bubbleOverImage" src="' + marker.getIcon().image + '" style="float:left; cursor: pointer;" /><div style="margin-top: 2px; padding-left: 10px; width: 180px; overflow:hidden"><font style="display: block; color: #f09; font-weight: bold; text-align: left; height: 18px; width: 500px; overflow: hidden;">' + strCompanyName + '</font></div>');
		}
		
		$j('#bubbleOverImage').mouseout(function() {
			bubbleOver.hide();
		});
		
		$j('#bubbleOverImage').click(function() {
	          bubbleOver.hide();			
			currentBubbleMarker = marker;
			bubble.openOnMarker(marker,html);
	          viewMarker(intCompanyId, intLocationId);

		});
		
	}
	
	
	function hide_bubble()
	{
		bubble.hide();
		currentBubbleMarker = null;
	}
	
	function createMarker(lon,lat,name,html, count, intCompanyId, intLocationId, strCompanyName) {
        
        //eigen icoontje
  		
        var icon = new GIcon();
        icon.image = "/images/maps/map_icon_" + count + ".png";
                	
        
        icon.iconSize = new GSize(22, 22);

        icon.iconAnchor = new GPoint(14, 29);
		icon.infoWindowAnchor = new GPoint(11, 0);
  		
  		
  		 		
  		point = (new GLatLng(lat,lon));
  		var marker = new GMarker(point, icon);
  		marker.infoHtml = html;
        
        GEvent.addListener(marker, "click", function() {
          //marker.openInfoWindowHtml(html);
        	bubble.openOnMarker(marker,html);
			currentBubbleMarker = marker;
          viewMarker(intCompanyId, intLocationId);
          	$j('#closeBubble').click(function() {
          		bubble.hide();
          	});
        });
        
        GEvent.addListener(marker, "mouseover", function() {
            //marker.openInfoWindowHtml(html);
        	open_bubbleOver(marker, strCompanyName, html, intCompanyId, intLocationId);
          });
        var i = markerIcons.length;
       
        markerIcons[i] = icon;
       
        return marker;
     }
	
	
	
	$j(document).ready(
		function()
		{
		
//			hCarousel = new UI.Carousel("horizontal_carousel");

			loadGMaps();
			$j('#postnav').focus(function() {
				if($j(this).val() == 'postcode / plaats')
				{
					$j(this).val('');
				}
				
			});
			
			$j('#postnav').blur(function() {
				if($j(this).val() == '')
				{
					$j(this).val('postcode / plaats');
				}
				
			});
			$j('#keyword').focus(function() {
				if($j(this).val() == 'trefwoord')
				{
					$j(this).val('');
				}
				
			});
			
			$j('#keyword').blur(function() {
				if($j(this).val() == '')
				{
					$j(this).val('trefwoord');
				}
				
			});
			
			$j("#scroller").simplyScroll({
				autoMode: 'loop',
				frameRate: 30,
				speed: 1,
				loopFunction: fncGetNewBanners
			});
			
			var setScrollerClick = false;
			var fncSimplyScrollIn = function()
			{
				$j(".simply-scroll .simply-scroll-clip").css("height", "254px");
				$j(".simply-scroll").css("height", "254px");
//				$j(".breadcrumbs").css("margin-top", "-115px");
				$j(".simply-scroll-over").each(function() { $j(this).hide(); });
				$j(this).find(".simply-scroll-over").show();
				if(!setScrollerClick)
				{
					$j(".simply-scroll a").click(function() {
						var strUrl = $j(this).attr('href');
						$j.post("/bannerclick.php", { companyId: $j(this).attr('companyId'), bannerId: $j(this).attr('bannerId')}, function() {
						});
					});
					setScrollerClick = true;
				}
			};
			
			var fncSimplyScrollOut = function()
					{
				$j(".simply-scroll .simply-scroll-clip").css("height", "34px");
				$j(".simply-scroll").css("height", "30px");
//				$j(".breadcrumbs").css("margin-top", "-14px");
				
						$j(this).hide();
					};
			
			
			$j(".simply-scroll .simply-scroll-list li").mouseover(fncSimplyScrollIn);
			
			$j(".simply-scroll-over").mouseout(fncSimplyScrollOut);
			
			$j('.studentenkortingen-in-de-buurt').mouseover(fncMouseOverMarker);
			$j('.studentenkortingen-in-de-buurt1').mouseover(fncMouseOverMarker);
			$j('.studentenkortingen-in-de-buurt').mouseout(fncMouseOutMarker);
			$j('.studentenkortingen-in-de-buurt1').mouseout(fncMouseOutMarker);
						$j('.paging a').click(fncChangePage);
			
			$j('.meerinfotext').hide();
			
			var selectedPackage = function()
			{
				$j('.package_div').hide();
				$j('#package_' + $j('#selectPackage_DropDown').val()).show();
			
			};
			
			// Fixes
			if($j.browser.opera){$j(".form .form_button").css({position: "relative", top: "-8px"});}
			$j("img").pngFix();
			$j("body").css("font-size-adjust","none");
			
			selectedPackage();
			$j('#selectPackage_DropDown').change(selectedPackage);
			$j('#package_DropDown').change(selectedPackage);
			
			$j('#searchbox').focus(function()
			{
				var searchtext = $j('#searchbox').val();
				
				if(searchtext == 'Vul trefwoord in')
				{
					$j('#searchbox').val('');
					
					$j('#searchbox').blur(function() 
					{
						if($j('#searchbox').val()=="")
						{
							$j('#searchbox').val(searchtext);
						}
					});
				}
			
			});
			
			
			var fncAddMarkers = function(markers)
			{
				var i;
				var j;
				var cardString = $j.cookie('discountcard');
				if(cardString)
				{
					var cards = $j.cookie('discountcard').split('+');
				}
				else
				{
					var cards = [];
				}
				var recognized;
				for(i = 0; i < markers.length; i++)
				{
					recognized = false;
                    if(checkprisk(markers[i].priority))
                    {
					for( j in cards)
					{
						if($j.stringarray.has(markers[i].cards, cards[j]))
						{
							map.addOverlay(markers[i]);
							recognized = true;
						}
					}
                    }
					if(!recognized)
					{
						map.removeOverlay(markers[i]);
					}
				}
				
			};
			
			var fncRemoveMarkers = function(markers)
			{
				var i;
				for(i = 0; i < markers.length; i++)
				{
					map.removeOverlay(markers[i]);
				}
			}
			
			var fncUpdateCategories = function()
			{
				var counter;
				
				if($j(this).is(':checked'))
				{
					strCheckedCategory = $j.stringarray.add(strCheckedCategory, $j(this).attr('nr'));
					fncAddMarkers(markers[$j(this).attr('nr')]);
				}
				else
				{
					strCheckedCategory = $j.stringarray.remove(strCheckedCategory, $j(this).attr('nr'));
					fncRemoveMarkers(markers[$j(this).attr('nr')]);
				}
				$j(this).blur();
				getNewMarkers();
				
			};
			
			
			$j('.category_check').click(fncUpdateCategories);
			
			$j('.category_check').each(function() {
				
				if($j(this).is(':checked'))
				{
					strCheckedCategory = $j.stringarray.add(strCheckedCategory, $j(this).attr('nr'));
					
				}
				else
				{
					strCheckedCategory = $j.stringarray.remove(strCheckedCategory, $j(this).attr('nr'));
					
				}
			});
			
			
			$j('.discount_check').click(function () {
				var cookieoptions = {
						path: '/',
						expires: 1000
					};
				if($j(this).is(':checked'))
				{
					$j.cookiearray.add('discountcard', $j(this).attr('nr'), cookieoptions);
					
				}
				else
				{
					$j.cookiearray.remove('discountcard', $j(this).attr('nr'), cookieoptions);
				}
				
				$j('.category_check').each(fncUpdateCategories);
			});
			
			fncGetNewBanners();
			$j('#welkom').css('background-image', 'url(' + $j('#welkom img').attr('src') + ')');
			$j('#welkom img').hide();
			setTimeout("$j('#welkom_message').fadeIn(2000)", 1000);
			
		});
	
