// function to provide possibility for movable ads in Internet Explorer
function getAdByIndex(index)
{
	// jump out if not allowed to show ads
	if (window.noShowAds)
		return;
	var curAd = jQuery('div.ad[id]:eq('+ index +')');
	if (curAd.size()) {
		document.write('<span class="' + curAd.attr('class').replace(/\bad\b/g, 'ad_tmp') + ' ' + curAd.attr('id') + '"></span>');
		document.write(unescape(curAd.text()));
	}
}

jQuery(document).ready(function() {
	try {
		// get the links with rel is external and add the property target
		jQuery('a[rel=external]').attr({target:'_blank'});
		// add link tracking and target to external urls
		if (location.hostname.match(/^(www\.)?(refdag|rd)\.nl$/)) {
		    jQuery('a[rel=external]:not([href*=doubleclick]), a[href^=http://]:not([href*=doubleclick]):not([href^=http://www.refdag.nl]):not([href^=http://www.rd.nl]):not([href^=http://rd.nl])').click(function() {
		    	if (!jQuery(this).attr('target'))
					jQuery(this).attr({target:'_blank'});
		        _gaq.push(['_trackPageview', jQuery(this).attr('href').replace(/^https?:\/\/+([^\/]+)(?:\:80)?\/?(.*)$/, function(wholeMatch, hostName, uri){
		            return '/extern/' + hostName + '/' + ((!uri) ? '' : uri.replace(/[^0-9a-z_-]+/gi, '_'));
		        })]);
		    });
		} else {
		    jQuery('a[rel=external]:not([href*=doubleclick]), a[[href^=http://]:not([href*=doubleclick]):not([href^=http://' + location.hostname + '])').click(function() {
		    	if (!jQuery(this).attr('target'))
					jQuery(this).attr({target:'_blank'});
		        _gaq.push(['_trackPageview', jQuery(this).attr('href').replace(/^https?:\/\/+([^\/]+)(?:\:80)?\/?(.*)$/, function(wholeMatch, hostName, uri){
		            return '/extern/' + hostName + '/' + ((!uri) ? '' : uri.replace(/[^0-9a-z_-]+/gi, '_'));
		        })]);
		    });
		}
		// local PDFs	
		jQuery('a[href$=".pdf"][href^=http://]:not([href*=' + location.hostname + '])').click(function() {
			_gaq.push(['_trackPageview', jQuery(this).attr('href')]);
		});
		// track all links in the site menu
		jQuery('ul#nav_site_sf a').click(function() {
			_gaq.push(['_trackEvent', 'Sitemenu', 'Clicked', jQuery(this).text()]);
		});
		// track the tabs and articles below it
		if (location.pathname == '/') {
			jQuery('div#rotate > ul > li > a').each(function() {
			    jQuery(this).click(function() {
			        _gaq.push(['_trackEvent', 'Homepagina tabs', 'Clicked', 'Tab: ' + jQuery('span', jQuery(this)).text()]);
			    });
			    jQuery('div' + jQuery(this).attr('href') + ' a').click(function() {    
			        _gaq.push(['_trackEvent', 'Homepagina tabs', 'Clicked', jQuery(this).text()]);
			    });
			});
		}
	} catch(error) {
	}
});

// ad stuff
jQuery(window).load(function() {
	// jump out if not allowed to show ads
	if (window.noShowAds)
		return;
	// modify page to show skyscraper on small screens
	/* LW 20100726 - For IE we must add 21 pixels to the window width for the scrollbar width. In FF the scrollbars width is included */ 
    var windowWidth = (jQuery.browser.msie) ? parseInt(jQuery(window).width()+21) : jQuery(window).width();
    if (jQuery(window).width() && (jQuery(document).width() > windowWidth)){
		jQuery('#top_nav_div').width(document.body.scrollWidth);
		jQuery('#site_frame_div').css({marginLeft:'0px'});
		window.scrollBy(10, 0); 
		jQuery('div.skyscraper_rightside').css({right:'-120px'});
	}
	// define leaderboard functions
    function leaderboardShow(){
        jQuery('#leaderboard_room').show();
        jQuery('#leaderboard').show();
        jQuery('#leaderboard div.ad').show();
        /* keep attached to bottom */
        jQuery(window).scroll(function(){
            jQuery('#leaderboard').css({
                top:        (jQuery(window).height() - jQuery('#leaderboard').height() + jQuery(document).scrollTop()) + 'px'
            });
        });
        return true;
    }
    function leaderboardCheck(){
        if (!jQuery('#leaderboard').size())
            return true; // nothing to show
        if (jQuery('#leaderboard.closed').size())
            return true; // closed by user
        if (jQuery('#leaderboard:visible').size())
            return true; // already visible
		if (jQuery('div#leaderboard div.ad').css('display') != 'none')
			return leaderboardShow();
        return false;
    }
    // move ads to their location
    jQuery('#ads_tmp_store div:has(span.ad_tmp)').each(function(){
        var zoneSelector = '#' + jQuery('> span.ad_tmp', this).attr('class').replace(/^.*\b(ad_\d_\d+_\d\d+|insertAd_\d+)\b.*$/, '$1');
		if (jQuery(zoneSelector + ' > div').size())
			zoneSelector += ' > div';
		jQuery(zoneSelector).text('');
        jQuery('> :not(script):not(span.ad_tmp)', this).each(function(){
            jQuery(this).remove().appendTo(jQuery(zoneSelector));
        });
    });
    // display ads
    function checkAd(){
		// repair in-article ads
		jQuery('div.ad[id^=insertAd_]').each(function(){
			if (!jQuery(this).parent('div.ad').size())
				jQuery(this).wrap('<div class="ad"></div>');
			jQuery(this).show();
		});
		// check display
        jQuery('div.ad:hidden:has(div)').each(function(){
            var adDiv = jQuery(this);
            if (jQuery('div object, div embed', adDiv).size()) {
                adDiv.show();
            } else {
                jQuery('div a img', adDiv).each(function(){
                    if ((jQuery(this).get(0).width > 1) || jQuery(this).get(0).fileSize && (jQuery(this).get(0).fileSize > 100))
                        adDiv.show();
                });
            }
        });
        leaderboardCheck();
    };
    checkAd();
    setTimeout(function(){ checkAd(); }, 1500);
    setTimeout(function(){ checkAd(); }, 5000);
    setTimeout(function(){ checkAd(); }, 10000);
    setTimeout(function(){ checkAd(); }, 15000);
});
jQuery(window).load(function() {
    // handle auto_play
    if (location.search && location.search.match(/[?&]auto_play=(1|true|yes)/) && $f && $f()) {
        try {
            $f().play();
            var multimediaTop = jQuery('#' + $f().id()).offset().top;
            if (multimediaTop < jQuery('body').scrollTop() || multimediaTop > (jQuery(window).scrollTop() + jQuery(window).height()) || (multimediaTop + jQuery('#' + $f().id()).height()) > (jQuery(window).scrollTop() + jQuery(window).height()))
                window.scrollTo(0, jQuery('#' + $f().id()).offset().top - 80);
        } catch(error) {
        }
    }
}); 

function fbs_click(shareUrl) {	
	var u = location.href;
	var t = document.title;
	window.open('http://www.facebook.com/sharer.php?u='+encodeURIComponent(shareUrl),'sharer','toolbar=0,status=0,width=626,height=436');
	return false;
}

