(function($) {

	$.fn.hoverDropdown = function (options) 
	{
		var settings = {
				'hoverClass' : 'hover',
				'closeTimeout' : 500
		};
		if (options) $.extend(settings, options);

		var openItem = function (item)
		{
			clearTimeout(item.data('timeout'));
			item.addClass(settings.hoverClass);
		};

		var closeItem = function (item)
		{
			if (!item.data('has_focus') && !item.data('is_over'))
			{
				item.data('timeout', setTimeout(function () { item.removeClass(settings.hoverClass); }, settings.closeTimeout));
			}
		};
		
		this.live("mouseenter", function (e)
		{
			openItem($(e.currentTarget).data('is_over', true));
		});
		
		this.live("mouseleave", function (e)
		{
			closeItem($(e.currentTarget).data('is_over', false));
		});
		
		this.live("focusin", function (e)
		{
			openItem($(e.currentTarget).data('has_focus', true))
		});
		
		this.live("focusout", function (e)
		{
			closeItem($(e.currentTarget).data('has_focus', false));
		});	  
	};

	$(document).ready(function() {
	
		/* Basic font replacement */
		// font: avenir demi + bold
		function initCufon()
		{
			Cufon.replace('#content h1', { fontFamily: 'font-cn'});
	
			Cufon.replace('.cuff-demicn', { fontFamily: 'font-cn'});
			Cufon.replace('.cuff',{ fontFamily: 'font'});
	
			Cufon.replace('.cuff-demicn-shadow', { fontFamily: 'font-cn', textShadow: '1px 1px #333' });
			Cufon.replace('.cuff-shadow', { fontFamily: 'font', textShadow: '1px 1px #333' });
			
			Cufon.replace('.cuff-demicn-white-shadow', { fontFamily: 'font-cn', textShadow: '1px 1px #FFF' });
			Cufon.replace('.cuff-white-shadow', { fontFamily: 'font', textShadow: '1px 1px #FFF' });
			
			$('#pressbox-loader').hide('fast');
			$('#press-box-animation').show('slow');
		}
		
		
		function refreshCufon()
		{
			Cufon.replace('#content .cuff-demicn', { fontFamily: 'font-cn'});
			Cufon.replace('#content .cuff',{ fontFamily: 'font'});
	
			Cufon.replace('#content .cuff-demicn-shadow', { fontFamily: 'font-cn', textShadow: '1px 1px #333' });
			Cufon.replace('#content .cuff-shadow', { fontFamily: 'font', textShadow: '1px 1px #333' });
			
			Cufon.replace('#content .cuff-demicn-white-shadow', { fontFamily: 'font-cn', textShadow: '1px 1px #FFF' });
			Cufon.replace('#content .cuff-white-shadow', { fontFamily: 'font', textShadow: '1px 1px #FFF' });
		}
	
		// Temp: draggable template menu
		$( "#tplNav" ).draggable();
	
		// init cufon when page is loaded
		initCufon();
	
		// hide ajax spinner when ajax stops
		// @TODO: it also refreshes cufon!
		$('<div id="ajax-spinner"></div>').appendTo("body").ajaxStop(function () {
	        $(this).hide().css({
	            position: "fixed",
	            left: "50%",
	            top: "50%"
	        });
			refreshCufon();
			
	    }).hide();	
	
		// convert all .ajax links to ajax get
		$("a.ajax").live("click", function (event) {
			event.preventDefault();
			$.get(this.href);
	
			$("#ajax-spinner").show().css({
		        position: "absolute",
		        left: event.pageX + 20,
		        top: event.pageY + 40
		    });
		});
	
		$("a.fancy-single-image").live('click', function() {
			jQuery.fancybox({
				'transitionIn'	: 'none',
				'transitionOut'	: 'none',
				'href'          : $(this).attr('href')
			});
			
			return false;
		});
		
		$("a.fancy-inline").live('click', function() {
			
			var href = $(this).attr('href');
			var target = $(href);
			target.show();
			
			jQuery.fancybox({
				'type'               : 'inline',
				'transitionIn'	     : 'none',
				'transitionOut'	     : 'none',
				'href'               : href,
				'hideOnContentClick' : false,
				'onClosed'           : function() { target.hide(); }
			});
			
			return false;
		});
		
		$("a.fancy-iframe").live('click', function() {
			jQuery.fancybox({
				'width'				: '50%',
				'height'			: '75%',
				'autoScale'			: true,
				'transitionIn'	    : 'none',
				'transitionOut'	    : 'none',
				'type'				: 'iframe',
				'href'              : $(this).attr('href')
			});
			
			return false;
		});
		
		$('a.fancy-ajax').live('click', function() {
			jQuery.fancybox({
				'type'           : 'iframe',
				'width'          : '5%',
				'height'         : '5%',
				'href'           : $(this).attr('href')
			});
				
			return false;
		});
		
		$('a.fancy-flash').live('click', function() {
			jQuery.fancybox({
				'type'               : 'iframe',
				'width'              : '95%',
				'height'             : '100%',
				'hideOnOverlayClick' : false,
				'transitionIn'	    : 'none',
				'transitionOut'	    : 'none',
				'href'               : $(this).attr('href')
			});
				
			return false;
		});
		
		var canClose = false;	
		function onFancyClose()
		{
			return window.canClose;
		}
		
		$('a.fancy-flash-no-close').live('click', function() {
			jQuery.fancybox({
				'type'               : 'iframe',
				'width'              : '95%',
				'height'             : '100%',
				'hideOnOverlayClick' : false,
				'transitionIn'	    : 'none',
				'transitionOut'	    : 'none',
				'onCleanup'          : onFancyClose,
				'href'               : $(this).attr('href')
			});
				
			return false;
		});
		
		$("div#flashes").each(function() {
			jQuery.fancybox({
				'type'               : 'inline',
				'href'               : 'div#flashes',
				'hideOnContentClick' : false,
				'transitionIn'	    : 'none',
				'transitionOut'	    : 'none',
				'onClosed'           : function() { $('div#flashes').hide(); }
			});
		});
	
		// login & minibacket hover box popup
		$('#login,#mini-basket').hoverDropdown();
		// disable "login" button click when jQuery is working properly
		$('#login h3 a').click(function(e) { e.preventDefault() });
	});

})( jQuery );
