var selected = Math.random() * 100,
		sponsors = {"6":{"weight":0.5,"min":0,"max":50},"9":{"weight":0.25,"min":50,"max":75},"7":{"weight":0.25,"min":75,"max":100}},
		sponsor;

if ( sponsors ) {
	for ( var s in sponsors ) {
		if ( selected > sponsors[s].min && selected < sponsors[s].max ) {
			sponsor = getQueryString('s', s);
		}
	}
}

function getQueryString(key, default_value) {
	if ( default_value == null ) default_value = "";
	
	key = key.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
	var regex = new RegExp("[\\?&]"+key+"=([^&#]*)");
	var qs = regex.exec(window.location.href);
	
	if( qs == null || typeof sponsors[qs[1]] == 'undefined' )
		return default_value;
	else
		return qs[1];
}

// Preform sponsor javascript operations when page is ready.
$(function(){
	if ( sponsors ) {
		
		// Look for querystrings
		//var qs = window.location.search.substring(1).split('&');
		//var params = {};
		
		//$.each(qs, function(x, value){
		//	var param = value.split('=');
		//	params[param[0]] = parseInt(param[1]);
		//});
		
		// If a sponsor querystring is set, override the one we've selected.
		//if ( typeof params['s'] !== 'undefined' && typeof sponsors[params['s']] !== 'undefined' ) {
		//	typeof params['s'];
		//	sponsor = params['s'];
		//}
		
		// Load current sponsor in sponsor blocks.
		$('.block-sponsor-region-target').each(function(){
			var self = $(this);
			$.get(Drupal.settings.basePath +'sponsor/ajax/region/'+ self.attr('title') +'/'+ sponsor, function(data){
				$('.block-sponsor-region-target-inner', self).fadeOut(500, function(){
					$(this).html(data).fadeIn(500);
				});
			});
		});
	
		// Add an "s" (sponsor) query string to the playlist so we play the sponsors video in any video advertisement zone.
		// Note: 	For some reason, Internet Explorer swfobject loads before this has a chance to run.
		// 				So, we need to add a line in swfobject_api module
		//				@see swfobject_api/swfobject_api.js - line 13
	
		/* if ( typeof Drupal.settings.swfobject_api != 'undefined' ) { // This doesn't work.
			Note: For query strings, jw flv required ? → %3F, = → %3D, & → %26
			@see: http://developer.longtailvideo.com/trac/wiki/FlashVars
		
			Drupal.settings.swfobject_api.files.player.flashVars.file += '%3Fs%3D'+ sponsor;
		} */
	}
});