(function($){
	var sansNavItem = function(element, options) {
		var instance = this;
		var LOADER_URL = '/templates/includes/2010/nav/area_loader.inc.php';
		
		this.element = element;
		this.elementTopic = '';
		this.elementRelation = '';
		this.hoverTimeOut = this.outTimeOut = null;
		
		this.loadContents = true;
		
		this.options = {
			"cache" : true,
			"duration" : 500
		};
		
		this.initAreaLoader = function() {
			var relation = instance.element.attr('rel');
			relation = relation.split(':');
			instance.elementRelation = relation[0];
			instance.elementTopic = relation[1];
			element.parent().hover(instance.mouseEnter, instance.mouseLeave);
		};
		
		this.mouseEnter = function() {
			clearTimeout(instance.hoverTimeOut);
			clearTimeout(instance.outTimeOut);
			instance.hoverTimeOut = setTimeout(function() {
				if (instance.loadContents !== true && $('#'+instance.elementRelation+'-'+instance.elementTopic).html() != '') {
					instance.element.parent().addClass('merchandise-act');
					$('#'+instance.elementRelation+'-'+instance.elementTopic+'-wrap').show();
					$('#'+instance.elementRelation+'-'+instance.elementTopic).fadeIn(instance.options.duration);
				} else if (instance.loadContents === true) {
					instance.loadAreaContents();
				}
			}, 200);
		};
		
		this.mouseLeave = function() {
			clearTimeout(instance.hoverTimeOut);
			instance.outTimeOut = setTimeout(function() {
				instance.element.parent().removeClass('merchandise-act');
				$('#'+instance.elementRelation+'-'+instance.elementTopic).fadeOut(instance.options.duration, function() {
					$('#'+instance.elementRelation+'-'+instance.elementTopic+'-wrap').hide();
				});
			}, 100);
		};
		
		this.loadAreaContents = function() {
			var date = new Date();
			var data = {
				"time" : date.getDate()+'-'+ date.getHours(),
				"topic" : instance.elementTopic
			};
			if (instance.options.cache === false) {
				data["cache"] = '0';
			}
			$.getJSON(
				myty.basePath + '/..' + LOADER_URL,
				data,
				instance.processResult
			);
		};
		
		this.processResult = function(result) {
			instance.loadContents = false;
			if (result.success === true) {
				instance.element.parent().addClass('merchandise-act');
				$('#'+instance.elementRelation+'-'+instance.elementTopic+'-wrap').show();
				var htmlContainer = $('#'+instance.elementRelation+'-'+instance.elementTopic);
				htmlContainer.html(result.content).fadeIn(instance.options.duration);
				if (typeof getContentValues != "undefined" && $.isFunction(getContentValues)) {
					getContentValues();
				}
				if (typeof initMytyLightbox != "undefined" && $.isFunction(initMytyLightbox)) {
					initMytyLightbox();
				}
				if( typeof mozile != "undefined" && $.isFunction(mozile.createEditors) ) {
					mozile.createEditors(".tyEditable","");
				}
			}
		};
		
		var init = function(options) {
			$.extend(instance.options, options);
			
			instance.initAreaLoader();
		};
		init(options);
	};
	
	$.fn.sansNavigation = function(options) {
		var options = options || {};
		
		return this.each(function() {
			var element = $(this);
			var instance = new sansNavItem(element, options);
			element.data('sansNavItem', instance);
		});
	};
})(jQuery);

