var HH = {};


HH.flash = function(settings) {
	Common.log('HH.flash() called');
	if (!settings['callback']) {
		settings.callback = function() {
			if (Cube.elementNotExists('#noflash')) {
				jQuery('#noscript').remove();
				jQuery('#content').prepend('<div id="noflash"><a href="http://get.adobe.com/flashplayer"><img src="/static/img/lang_' + lang.currentLanguage() + '/no_flash.png" alt="" /></a></div>');
				jQuery('.flash').remove();
			}
		};
	}
	Cube.flash.add(settings);
};



HH.flash.audio = function(id, src) {
	HH.flash({
		id: 'swf_audio_' + id,
		src: '/static/flash/music/player_mp3_mini.swf',
		width: 200,
		height: 20,
		flash_vars: {
			mp3: src
		},
		parameters: {
			bgcolor: '000000'
		}
	});
};



HH.flash.video = function(id, src, img) {
	HH.flash({
		id: 'swf_video_' + id,
		src: '/static/flash/player.swf',
		width: 460,
		height: 250,
		flash_vars: {
			img_src: img,
			vid_src: src
		}
	});
};



HH.navigation = function() {
	Common.log('HH.navigation() called');
	if (!Cube.flash.hasVersion(Cube.flash.defaultRequiredVersion())) {
		return;
	}
	jQuery('#mast_navigation').html('<div id="mast_navigation_menu"></div>');
	HH.flash({
		id: 'mast_navigation_menu',
		src: '/static/flash/nav.swf',
		width: 750,
		height: 70,
		flash_vars: {
			lang: Cube.lang.currentLanguage(),
			current: jQuery('#mast_navigation').attr('class')
		},
		transparent: true,
		callback: function() {
			return;
		}
	});
};


HH.langSwitcher = function() {
	Common.log('HH.langSwitcher() called');
	jQuery('#language_switch ul').css('visibility', 'hidden');
	jQuery('#language_switch p, #language_switch ul').hover(function() {
		jQuery('#language_switch ul').css('visibility', 'visible');
	}, function() {
		jQuery('#language_switch ul').css('visibility', 'hidden');
	});
};


HH.sound = function() {
	Common.log('HH.sound() called');

	var _controlTurnOn = function() {
		jQuery('#sound_control').html('<a href="#unmute" id="unmute_sounds">Turn background sounds on</a>');
		jQuery('#unmute_sounds').click(function() {
			Cookie.del('mute_sounds');
			_controlTurnOff();
			return false;
		});
	};

	var _controlTurnOff = function() {
		jQuery('#sound_control').html('<a href="#mute" id="mute_sounds">Turn background sounds off</a>');
		jQuery('#mute_sounds').click(function() {
			Cookie.set('mute_sounds', true, 30);
			_controlTurnOn();
			return false;
		});
	};

	var _setupSound = function() {
		jQuery('body').append('<div id="bg_sounds_container" style="visibility: hidden;"></div>');
		jQuery('#links_container').append('<div id="sound_control"></div>');
		if (Cookie.get('mute_sounds')) {
			_controlTurnOn();
		} else {
			_controlTurnOff();
		}
	};

	var _playSound = function(obj) {
		Cube.onready(function() {
			if (!obj['src']) {
				return;
			}
			if (Cookie.get('mute_sounds')) {
				return;
			}
			Common.log('HH.sound.play(' + obj.src + ') called');
			var id = (new Date()).getTime();
			if (typeof(obj['id']) === 'string') {
				id = obj['id'];
			}
			jQuery('#bg_sounds_container').append('<div id="bg_soundclip_' + id + '"></div>');
			HH.flash({
				id: 'bg_soundclip_' + id,
				src: '/static/flash/sound_clip.swf',
				width: 1,
				height: 1,
				flash_vars: {
					src: '/assets/audio/website/' + obj.src + '.mp3',
					repeat: Common.getValue(obj['repeat'], 0),
					interval: Common.getValue(obj['interval'], 30)
				},
				callback: function() {
					return;
				}
			});
		});
	};

	var _stopSound = function(id) {
		Common.log('HH.sound.play(' + id + ') called');
		jQuery('#flash_bg_soundclip_' + id).remove();
	};

	var _stopAllSounds = function() {
		jQuery('#bg_sounds_container').remove();
		_setupSound();
	};

	Cube.onready(function() {
		_setupSound();
	});

	return {
		play:    _playSound,
		stop:    _stopSound,
		stopAll: _stopAllSounds
	};
}();


HH.disableRightClick = function() {
	Common.log('HH.disableRightClick() called');
	jQuery(document).bind('contextmenu', function(e) {
		return false;
	});
};


HH.disableImageDrag = function() {
	Common.log('HH.disableImageDrag() called');
	jQuery('a').find('img').each(function() {
		jQuery(this).addClass('is_draggable');
	});
	jQuery('img[class!=is_draggable]').bind('mousedown', function(e) {
		jQuery(this).bind('mousemove', function(e) {
			return false;
		});
		return false;
	});
};


HH.rolloverSounds = function() {
	Common.log('HH.rolloverSounds() called');
	jQuery('#mast_links .pos a').hover(function() {
		HH.sound.play({
			id:  'pos_rollover',
			src: 'pos_rollover'
		});
	}, function() {
		HH.sound.stop('pos_rollover');
	});
	jQuery('#novel .logo a').hover(function() {
		HH.sound.play({
			id:  'novel_entertainment',
			src: 'novel_entertainment'
		});
	}, function() {
		HH.sound.stop('novel_entertainment');
	});
};


HH.chartbeatTracker = function() {
	Common.log('HH.chartbeatTracker() called');
	window['_sf_startpt']      = _sf_startpt = (new Date()).getTime();
	window['_sf_async_config'] = {uid: 1848, domain: 'horridhenry.me'};
	jQuery.getScript('http://static.chartbeat.com/js/chartbeat.js');
};


HH.flashCheck = function() {
	if (!Cube.flash.hasVersion(Cube.flash.defaultRequiredVersion())) {
		if (Cube.elementExists('#missing_tech')) {
			jQuery('#missing_tech').html('<div><p>You don\'t have the correct version of flash to see parts of my site. <a href="http://get.adobe.com/flashplayer" target="_blank">Click here to get the newest version.</a></p></div>');
		}
	}
};


HH.iTrack = function(type, value) {
	jQuery(function() {
		value = value || '';
		console.log('HH.iTrack(' + type + ', ' + value + ')');
		if (!jQuery('#i_track').length) {
			jQuery('body').append('<div id="i_track" style="position: absolute; left: 0; top: -999px; width: 1px; height: 1px; overflow: hidden;"></div>');
		}
		var random = Math.floor(Math.random() * 999999999);
		jQuery('#i_track').html('<img src="/api/stat?t=' + type + '&amp;v=' + value + '&amp;r=' + random + '" alt="" />');
	});
};


HH.swfLoader = function() {
	jQuery('div.swf').each(function() {
		var id = jQuery(this).find('div').attr('id');
		var config_id = id.substr(6);
		var config = FLASH[config_id];
		if (!config) {
			Common.error('Config for bbcode flash id: ' + id + ' doesn\'t exist');
			return;
		}
		if (!config['swf'] || !config['width'] || !config['height']) {
			Common.error('Missing swf, width, or height config for bbcode flash id: ' + id);
			return;
		}
		var props = {
			id: id,
			src: config.swf,
			width: config.width,
			height: config.height,
			flash_vars: Common.getValue(config['flash_vars'], {})
		};
		if (config['class_name']) {
			props.attributes = {
				'class': config.class_name
			};
		}
		HH.flash(props);
	});
};


Cube.onready(function() {
	Cube.track.start('UA-1894610-21');
	Cube.externalLink.add('a.buy_button');
	Cube.lang.setAllowedLanguages('en', 'fr', 'de', 'es', 'it');
	Cube.lang.setCurrentLanguage(jQuery('html').attr('lang'));
	Cube.flash.setDefaultRequiredVersion('9');

	HH.navigation();
	HH.langSwitcher();
	HH.disableRightClick();
	HH.disableImageDrag();
	HH.rolloverSounds();
	HH.chartbeatTracker();
	HH.flashCheck();
	HH.swfLoader();
});