$(function() {
	var enableWidgetCustomisation = true;
	if (enableWidgetCustomisation) {
		/* Set up widget board and palette */
		var wb = new WidgetBoard('.widget_board', {
			getWidgetSetUrl: '/applications/widgets/get_widget_preferences.rm',
			setWidgetSetUrl: '/applications/widgets/set_widget_preferences.rm'
		});
		wb.onLoadWidgetSet(function(widgetSet) {
			var palette = new WidgetPalette(wb, '.widget_palette_container', widgetSet.availableWidgets);
			
			// add slide up / down of palette functionality to customise button
			$('.widget_palette_panel').hide();
			$('a#widget-customise-button').click(function() {
				if ($('.widget_palette_panel').is(':visible')) {
					try{_gaq.push(['_trackEvent', 'Widget palette', 'close', document.location.href + ' - ' + $('title').html().replace(/\'/g,"")]);}catch(e){}
					$('.widget_palette_panel').slideUp('slow');
					
				} else {
					try{_gaq.push(['_trackEvent', 'Widget palette', 'open', document.location.href + ' - ' + $('title').html().replace(/\'/g,"")]);}catch(e){}
					$('.widget_palette_panel').slideDown('slow');
				}
				return false;
			});
		})
		
		wb.onUserAddWidget(function(widgetDefinition) {
			$.get('/applications/my_content/log_action.rm', {'action_type': 'add_widget', 'detail': widgetDefinition.widget})
		})
		wb.onRemoveWidget(function(widgetName) {
			$.get('/applications/my_content/log_action.rm', {'action_type': 'remove_widget', 'detail': widgetName})
		})
		
		//apply help text to widget customisation button
		$('#widget-customise-button').after('<div class="customise-button-help"><div>'+$('#widget-customise-button').attr("title")+'<span></span></div></div>').removeAttr("title");
		$('#widget-customise-button span').hover(
			function(){
				$('.customise-button-help').show();
			},
			function(){
				$('.customise-button-help').hide();
			}
		);
	} else {
		/* add top padding to compensate for no top controls, and run widget initialisers on static versions */
		$('.widget_panel').css({'padding-top': '15px'}).each(function() {
			var classNames = $(this).attr('class').split(/\s+/);
			for (var i = 0; i < classNames.length; i++) {
				var match;
				if (match = classNames[i].match(/^widget_panel_(.*)/)) {
					var widgetName = match[1];
					if (WidgetInitializers[widgetName]) {
						WidgetInitializers[widgetName]({}, this);
					}
				}
			}
		})
	}
	
	// forcibly add press release widget on mobile version
	if (document.documentElement.clientWidth < 600) {
		var widgetPlaceholder = $('<div></div>');
		$('.widget_board').prepend(widgetPlaceholder);
		wb.loadWidget({widget: 'press_release'}, widgetPlaceholder);
	}
	
	loadfeatures();
});


function loadfeatures(){
	//load homepage features
	var homepage_features = $('#homepage_features li');

	$('.features-tabbed-banner ul.tab-content-list li.active').show();
	
	if(homepage_features.length > 1){
		$('#homepage_features').css('height', $('#homepage_features li.active .right_stuff').height()+'px');
		
		$('#homepage_features_tabs li a').click(function(eventObj){
			homepage_feature_clicked(this);
			eventObj.preventDefault();
		});
	};
}

function homepage_feature_clicked(tab_link){
	var selected = jQuery(tab_link).attr('href');
	jQuery('#homepage_features').css('height', jQuery(selected).height()+'px');
	jQuery('#homepage_features li.active').fadeOut(function(){
		jQuery(this).removeClass('active');
	});
	jQuery(selected).fadeIn(function(){
		jQuery(selected).addClass('active');
	});	
	
	jQuery('#homepage_features_tabs li a.active').removeClass('active');
	jQuery(tab_link).addClass('active');
};
	
function loadSWFMovInTab(selector){
	jQuery(selector).each(function(){
		var this_filename = jQuery(this).find("input[name='filename']").val();
		var this_duration = jQuery(this).find("input[name='duration']").val();
		var this_width = jQuery(this).find("input[name='width']").val();
		var this_height = jQuery(this).find("input[name='height']").val();
		var this_image = jQuery(this).find("input[name='image']").val();
		var this_container_id = jQuery(this).attr('id');
		
		var flashvars = {
			file: this_filename,
			skin: "/swf/jwplayer/skins/kf.swf",
			controlbar: "over",
			lightcolor: "AEBA33",
			backcolor:"333333",
			image: this_image,
			duration: this_duration
		};
		var params = {
			menu: "false",
			allowfullscreen: "true",
			allowscriptaccess: "always"
		};
		swfobject.embedSWF("/swf/jwplayer/player.swf", 
		this_container_id, this_width, this_height, "9.0.0", false, flashvars, params, false);
	});
}
function loadSWFAudioInTab(selector){
	jQuery(selector).each(function(){
		var this_filename = jQuery(this).find("input[name='filename']").val();
		var this_duration = jQuery(this).find("input[name='duration']").val();
		var this_container_id = jQuery(this).attr('id');
		
		var flashvars = {
			file: this_filename,
			skin: "/swf/jwplayer/skins/kf_audio.swf",
			lightcolor: "AEBA33",
			backcolor:"333333",
			duration: this_duration
		};
		var params = {
			menu: "false",
			allowscriptaccess: "always"
		};
		swfobject.embedSWF("/swf/jwplayer/player.swf", 
		this_container_id, "250", "27", "9.0.0", false, flashvars, params, false);
	});
}

