$(document).ready(function(){
	if (top.location!= self.location) {
			top.location = self.location.href
	}
	
	 $(".homegrijsvlak").hover(
      function () {
        $(this).animate({ height: "165px" });
      }, 
      function () {
         $(this).animate({ height: "65px" });
      }
    );
	
	
//var news = $.get("/rss/webform1.aspx", function(data){
 // 		$("#nieuwscontent").html(data);
 // 		var newskop1 = $("#nieuwscontent h2:eq(0)").html();
 // 		var newscont1 = $("#nieuwscontent .body:eq(0)").html().substring(0,259);
 // 		$("#nieuwskop1").html(newskop1);
//		$("#nieuwstekst1").html(newscont1);
//		$(".nieuwstekst").css({'backgroundImage':'none'});
 //	}); 
	
	
	var oldselected = '#home';
	$("#menu a, div.homevlak, a.internal").click(function () {
		var whereto = $(this).attr("href");
     	selected = whereto;
		
		$("#menu a").removeClass('selected');
		if ($(this).hasClass('homevlak')||$(this).hasClass('internal')) {
			$("#menu a").each(function(i){
				if ($(this).attr("href")==whereto) {
				$(this).addClass('selected');
				}
			});
		} else {
			$(this).addClass('selected');
		}
		
		$("#backgroundImg").fadeOut('fast');
	 
     	
     	if (whereto=="#home") {
     		$.scrollTo({top:-230, left:0}, 2500,{easing:'easeOutBack'});
     	} else { 
     		var leftLoc = $(whereto).position();
     		if (oldselected!=null) var oldLoc = $(oldselected).position();
     		var width = $(window).width();
			var halfWidth = (width/2)*(1);
			var goto = (leftLoc.left-halfWidth);
			var gotoTop = leftLoc.top-186;
			if (leftLoc.top==230) { var que='yx'; } else { var que='xy';}
			if (oldLoc.top==230&&leftLoc.top==230) { var que='xy'; }
			if (leftLoc.top!=230) {
				$.scrollTo({top:gotoTop, left:goto}, 2000,{easing:'easeOutBack'});
			} else {
				$.scrollTo({top:-230, left:goto}, 2000,{easing:'easeOutBack'});
			}
		}
		
		if (whereto!="#home") {
			var background = 'images/bg'+whereto.replace('#','')+'.png';
			var img = new Image();
			img.onload = function() {
        		$("#backgroundImg").css({'background-image':'url("' + img.src + '")'});
       			$("#backgroundImg").fadeIn();
    		}
    		img.src = background;
    	}	
    	
    	var bannerimg = 'images/banner'+whereto.replace('#','')+'.png';
		var banner = new Image();
		banner.onload = function() {
        	$("#topbanner").attr("src",banner.src);	
    	}
    	banner.src = bannerimg;
    	
		oldselected = whereto;
		return false;
    });

	
	// Load Google Maps
	//load();
	
	
	$("#mytrafficpics").loopedSlider({ container:'.screens',slides:'.slides'});
	$("#isharephonepics").loopedSlider({ container:'.screens',slides:'.slides'});
	// Load anti-spam
	//if ($('a.email').length > 0){	
	//$.getScript('/webstate/js/jquery.mailto-1.1.js',function(){ 
		//$('.email').mailto();
	//});
	//}
	
	//resize_viewport();

	var selectedbutton = jQuery.url.attr("anchor");
	if (!selectedbutton) selectedbutton = "home";

	// start sequence
	selected = "#"+selectedbutton;
	var leftLoc = $("#"+selectedbutton).position();
	var width = $(window).width();
	var halfWidth = (width/2)*(1);
	var goto = (leftLoc.left-halfWidth)+485;	
	//$.scrollTo({top:-230, left:goto}, 1000,  {queue:true, onAfter:function(){ $("#home").css({marginTop:'0px'}) }} );
	$.scrollTo({top:-0, left:goto}, 1);
	
	
	// IE6 fix om positie te fixen
	var badBrowser = (/MSIE ((5\.5)|6)/.test(navigator.userAgent) && navigator.platform == "Win32");
    if (badBrowser) {
    	$('.header').fixedPosition({
 			vpos: 'top', // posible values: "top", "middle", "bottom". if it is null the original position is taken
 			hpos : 'center' // posible values: "left", "center", "right". if it is null the original position is taken
		}); 
    }
    
    $("#tbaloon").css({left:"-80px",top:"35px",width:"10px",height:"10px"});
    $("#tbird").css({left:"-50px",top:"35px"}).animate({left:"0px",top:"0px"},'slow', function() { 
    		$("#tbaloon").animate({left:"50px",top:"40px",width:"92px",height:"58px"});
    });
    
	getTwitters('tweet', { 
  	id: 'webstatebv', 
 	count: 5, 
  	enableLinks: true, 
  	ignoreReplies: false, 
  	clearContents: true,
  	template: '"%text%"<br><span>Webstate op %time%</span>'
	});
	
	getTwitters('hometweet', { 
  	id: 'webstatebv', 
 	count: 2, 
  	enableLinks: true, 
  	ignoreReplies: true, 
  	clearContents: true,
  	template: '"%text%"<br><span>Webstate op %time%</span>'
	});
	
	
	
});

var selected=null;
function resize_viewport() {
  $.event.remove( this, "resize", resize_viewport);
	if (selected) {
		if (selected=="#sharepoint") {
     		$.scrollTo({top:-230, left:0}, 1000);
     	} else { 
  			var leftLoc = $(selected).position();
     		var width = $(window).width();
			var halfWidth = (width/2)*(1);
			var goto = (leftLoc.left-halfWidth)+482;	
			$.scrollTo({top:-230, left:goto}, 1000);
		}
	}		
  $.event.add( this, "resize", resize_viewport);
}

var gmarkers = [];
var htmls = [];
var to_htmls = [];
var from_htmls = [];
var i=0;

// A function to create the marker and set up the event window
function createMarker(point,name,html) {
var marker = new GMarker(point);

// The info window version with the "to here" form open
to_htmls[i] = html + '<br />Route: <b>naar Webstate</b> of <a href="javascript:fromhere(' + i + ')">van Webstate</a>' +
   '<br />Start adres:<form action="http://maps.google.nl/maps" method="get" target="_blank">' +
   '<input type="text" SIZE=30 MAXLENGTH=40 name="saddr" id="saddr" value="" /><br />' +
   '<INPUT value="Route" TYPE="SUBMIT">' +
   '<input type="hidden" name="daddr" value="' + point.lat() + ',' + point.lng() + 
          // "(" + name + ")" + 
   '"/>';
// The info window version with the "to here" form open
from_htmls[i] = html + '<br />Route: <a href="javascript:tohere(' + i + ')">naar Webstate</a> of <b>van Webstate</b>' +
   '<br />Eind adres:<form action="http://maps.google.nl/maps" method="get"" target="_blank">' +
   '<input type="text" SIZE=30 MAXLENGTH=40 name="daddr" id="daddr" value="" /><br />' +
   '<INPUT value="Route" TYPE="SUBMIT">' +
   '<input type="hidden" name="saddr" value="' + point.lat() + ',' + point.lng() +
          // "(" + name + ")" + 
   '"/>';
// The inactive version of the direction info
html = html + '<br />Route: <a href="javascript:tohere('+i+')">Naar Webstate</a> - <a href="javascript:fromhere('+i+')">Van Webstate</a>';

GEvent.addListener(marker, "click", function() {
  marker.openInfoWindowHtml(html);
});

gmarkers[i] = marker;
htmls[i] = html;
i++;
return marker;
}

function tohere(i) {
gmarkers[i].openInfoWindowHtml(to_htmls[i]);
}
function fromhere(i) {
gmarkers[i].openInfoWindowHtml(from_htmls[i]);
}
      

function loadmaps() { 
if (GBrowserIsCompatible()) {       
      var map = new GMap2(document.getElementById("map"));
      map.addControl(new GLargeMapControl());
      map.addControl(new GMapTypeControl());
      map.setCenter(new GLatLng(52.067827,5.080426),14);
     var point = new GLatLng(52.066117,5.082625);
      
      var marker = createMarker(point,'Hotel de Prins','', 0)
      map.addOverlay(marker); 
    }
}

function StripHTMLAndTrim(text) 
      {
       var htmlStriper = /<(?:.|\s)*?>/g;
       text = text.replace(htmlStriper, " ");
   while (text.indexOf("  ") >= 0) 
           {
                text = text.replace("  ", " ");
            }
       return text.replace(/^\s+|\s+$/g, "");
   }
