// sansibar.de JavaScript Document
(function($){
	var tyResizeHandler = {
		MIN_DOCUMENT_WIDTH : 1070,
		DOCUMENT_WIDTH_THRESHOLD : 1190,
		DOCUMENT_CONDITIONAL_LEFT_PADDING : 90,
		GRIDSIZE : 90,
		
		tolerance : 20,
		sizeBefore : 0,
		timeout : null,
		
		events : {
			'onReady' : null,
			'onBeforeResize' : [],
			'onResizeComplete' : []
		},
		animations : {},
		animationIDs : [],
		defaultDuration : 300,
		defaultEasing : 'swing',
		animationDelay : 200,
		
		lastUniqueID : 0,
		
		isAnimating : false,
		
		init : function() {
			tyResizeHandler.sizeBefore = $('body').width();
			
			$(window).resize( tyResizeHandler.resize );
			$(window).load( function() { tyResizeHandler.resize(true); } );
			if ($.isFunction( tyResizeHandler.events.onReady ) ) {
				$(document).ready( tyResizeHandler.events.onReady );
			}
		},
		
		bind : function(uniqueID, type, func) {
			if (tyResizeHandler.events[type]) {
				tyResizeHandler.events[type][uniqueID] = func;
			}
		},
		
		executeCallback : function(uniqueID, type, direction) {
			var type = type || 'null';
			var uniqueID = uniqueID || 'null';
			var callbackFunction = null;
			
			if (tyResizeHandler.events[type]) {
				if (tyResizeHandler.events[type][uniqueID]) {
					callbackFunction = tyResizeHandler.events[type][uniqueID];
				} else {
					var uniqueIDParts = uniqueID.split(':');
					if (tyResizeHandler.events[type][ uniqueIDParts[1] ]) {
						callbackFunction = tyResizeHandler.events[type][ uniqueIDParts[1] ];
					}
				}
				
				if ($.isFunction(callbackFunction)) {
					callbackFunction.call(tyResizeHandler, direction);
				}
			}
		},
		
		addAnimation : function(id, element, parameters, options, callback, additionals) {
			tyResizeHandler.animations[id] = {
				"uniqueID" : id,
				"element" : element,
				"parameters" : parameters,
				"options" : options,
				"callback" : callback,
				"additionals" : additionals
			};
			tyResizeHandler.animationIDs.push(id);
		},
		
		animate : function(currentAnimation) {
			var currentAnimation = currentAnimation || false;
			
			tyResizeHandler.isAnimating = true;
			
			if (currentAnimation === false && tyResizeHandler.animationIDs.length) {
				for (var idx = 0; idx < tyResizeHandler.animationIDs.length; idx++) {
					if (tyResizeHandler.animations[ tyResizeHandler.animationIDs[idx] ]) {
						currentAnimation = tyResizeHandler.animations[ tyResizeHandler.animationIDs[idx] ];
						break;
					} else {
						tyResizeHandler.animationIDs.splice(0, 1);
					}
				}
			}
			
			if (currentAnimation) {
				if (currentAnimation.element.length) {
					tyResizeHandler.executeCallback(currentAnimation.uniqueID, 'onBeforeResize', currentAnimation.additionals.direction);
					
					if ($.isFunction(currentAnimation.parameters)) {
						currentAnimation.parameters.call(
							currentAnimation.element, 
							$.extend(
								{
									"duration" : tyResizeHandler.defaultDuration,
									"easing" : tyResizeHandler.defaultEasing
								},
								currentAnimation.options,
								{
									"complete" : function() {
										tyResizeHandler.animationComplete(currentAnimation);
									}
								}
							)
						);
					} else {
						currentAnimation.element.animate(
							currentAnimation.parameters,
							$.extend(
								{
									"duration" : tyResizeHandler.defaultDuration,
									"easing" : tyResizeHandler.defaultEasing
								},
								currentAnimation.options,
								{
									"complete" : function() {
										tyResizeHandler.animationComplete(currentAnimation);
									}
								}
							)
						);
					}
				} else {
					tyResizeHandler.animationComplete(currentAnimation);
				}
			} else {
				tyResizeHandler.isAnimating = false;
			}
		},
		
		animationComplete : function(currentAnimation){
			if (tyResizeHandler.animations[ currentAnimation.uniqueID ]) {
				delete tyResizeHandler.animations[ currentAnimation.uniqueID ];
				tyResizeHandler.animationIDs.splice(0, 1);
			}
			
			tyResizeHandler.executeCallback(currentAnimation.uniqueID, 'onResizeComplete', currentAnimation.additionals.direction);
			
			if ($.isFunction( currentAnimation.callback )) {
				currentAnimation.callback.call(tyResizeHandler);
			}
			
			setTimeout(function() {
				tyResizeHandler.animate(tyResizeHandler.animations[ tyResizeHandler.animationIDs[0] ]);
			}, tyResizeHandler.animationDelay);
		},
		
		resize : function(first) {
			var docWidth = $('body').width();
			var first = first || false;
			
			if (tyResizeHandler.sizeBefore - docWidth > tyResizeHandler.tolerance || docWidth - tyResizeHandler.sizeBefore > tyResizeHandler.tolerance) {
				tyResizeHandler.sizeBefore = docWidth;
				
				clearTimeout(tyResizeHandler.timeout);
				tyResizeHandler.timeout = setTimeout(function(){
					var interval = null;
					interval = setInterval(function() {
						if (tyResizeHandler.isAnimating === false) {
							tyResizeHandler.addAnimations(docWidth, first);
							clearInterval(interval);
						}
					}, 500);
				}, 200);
			}
		},
		
		addAnimations : function(docWidth, first) {
			var animations = [];
			var reverseAnimations = [];
			var first = first || false;
			
			var leftWrap = $('body>.leftWrap');
			var leftPadding = parseInt(leftWrap.css('paddingLeft'));
			
			if (leftPadding > 0 && docWidth < ( tyResizeHandler.DOCUMENT_WIDTH_THRESHOLD + tyResizeHandler.DOCUMENT_CONDITIONAL_LEFT_PADDING ) ) {
				animations.push({
					"uniquekey" : 'downsize:leftWrap',
					"element" : leftWrap,
					"parameters" : function(options) {
						this.css('paddingLeft', '0px');
						if ($.isFunction(options.complete)) {
							options.complete.call();
						}
					},
					"options" : {
						// no special options
					},
					"callback" : null,
					"additionals" : {
						"direction" : -1
					}
				});
				
				leftPadding = 0;
			}
			
			if (leftPadding == 0 && docWidth >= ( tyResizeHandler.DOCUMENT_WIDTH_THRESHOLD + tyResizeHandler.DOCUMENT_CONDITIONAL_LEFT_PADDING ) ) {
				animations.push({
					"uniquekey" : 'enlarge:leftWrap',
					"element" : leftWrap,
					"parameters" : function(options) {
						this.css('paddingLeft', tyResizeHandler.DOCUMENT_CONDITIONAL_LEFT_PADDING+'px');
						if ($.isFunction(options.complete)) {
							options.complete.call();
						}
					},
					"options" : {
						// no special options
					},
					"callback" : null,
					"additionals" : {
						"direction" : 1
					}
				});
				
				leftPadding = tyResizeHandler.DOCUMENT_CONDITIONAL_LEFT_PADDING;
			}
			
			$('#tySidebar2 .tyVerticalResizableContainer').each(function(){
				var element = $(this);
				if (docWidth >= tyResizeHandler.DOCUMENT_WIDTH_THRESHOLD + leftPadding) {
					if (element.is(':hidden')) {
						animations.push({
							"uniquekey" : 'enlarge:' + element.attr('id'),
							"element" : element,
							"parameters" : element.slideDown,
							"options" : {
								// no special options
							},
							"callback" : null,
							"additionals" : {
								"direction" : 1
							}
						});
					}
				}
				
				if (docWidth < tyResizeHandler.DOCUMENT_WIDTH_THRESHOLD + leftPadding) {
					if (first || element.is(':visible')) {
						reverseAnimations.push({
							"uniquekey" : 'downsize:' + element.attr('id'),
							"element" : element,
							"parameters" : element.slideUp,
							"options" : {
								// no special options
							},
							"callback" : null,
							"additionals" : {
								"direction" : -1
							}
						});
					}
				}
			});
			
			reverseAnimations.reverse();
			
			$(animations).each(function(idx, anim) {
				tyResizeHandler.addAnimation( anim.uniquekey, anim.element, anim.parameters, anim.options, anim.callback, anim.additionals );
			});
			$(reverseAnimations).each(function(idx, anim) {
				tyResizeHandler.addAnimation( anim.uniquekey, anim.element, anim.parameters, anim.options, anim.callback, anim.additionals );
			});
			
			tyResizeHandler.animate();
		},
		
		getUniqueID : function() {
			return ++this.lastUniqueID;
		}
	};
	
	// Callbacks anbinden
	function bindResizeHandlers() {
		if ($('#filter-container-region').length) {
			bindWineResizeHandlers();
		} else if ($('#filter-container-sizes').length) {
			bindFashionResizeHandlers();
		}
	}
	
	function bindWineResizeHandlers() {
		tyResizeHandler.bind('filter-container-region', 'onResizeComplete', function(direction) {
			if (direction == -1) {
				if ($('#filter-container-region').length) {
					var boxClone = $('#filter-container-region').clone();
					boxClone.attr('id','filter-container-region-copy');
					boxClone.prependTo($('#tySideBarOverlowArea'));
					boxClone.slideDown('fast');
					$('#tySidebar2').hide();
					$('#fscontentWrap').removeClass('tygrw13').addClass('tygrw10');
					
					if (typeof articleListHandler != "undefined") {
						articleListHandler.addContainer("#filter-container-region-copy", {
							"file" : '/templates/includes/2010/filter/wine/regions.inc.php'
						});
						articleListHandler.addTrigger("#filter-container-region ul.filter-list a[rel]");
					}
				}
			}
		});
		tyResizeHandler.bind('filter-container-vineyard', 'onResizeComplete', function(direction) {
			if (direction == -1) {
				if ($('#filter-container-vineyard').length) {
					$('#tySideBarOverlowArea').html('').show();
					var boxClone = $('#filter-container-vineyard').clone();
					boxClone.attr('id','filter-container-vineyard-copy');
					boxClone.prependTo($('#tySideBarOverlowArea'));
					boxClone.slideDown('fast');
					
					if (typeof articleListHandler != "undefined") {
						articleListHandler.addContainer("#filter-container-vineyard-copy", {
							"file" : '/templates/includes/2010/filter/wine/vineyards.inc.php'
						});
						articleListHandler.addTrigger("#filter-container-vineyard ul.filter-list a[rel]");
					}
				}
			}
		});
		tyResizeHandler.bind('filter-container-region', 'onBeforeResize', function(direction) {
			if (direction == 1) {
				if ($('#filter-container-region').length) {
					$('#tySidebar2').show();
					$('#fscontentWrap').removeClass('tygrw10').addClass('tygrw13');
					$('#filter-container-region-copy').slideUp('fast', function() { 
						$(this).remove();
					});
					
					if (typeof articleListHandler != "undefined") {
						articleListHandler.addContainer("#filter-container-region", {
							"file" : '/templates/includes/2010/filter/wine/regions.inc.php'
						});
					}
				}
			}
		});
		tyResizeHandler.bind('filter-container-vineyard', 'onBeforeResize', function(direction) {
			if (direction == 1) {
				if ($('#filter-container-vineyard').length) {
					$('#filter-container-vineyard-copy').slideUp('fast', function() { 
						$(this).remove();
						$('#tySideBarOverlowArea').hide().html('');
					});
					
					if (typeof articleListHandler != "undefined") {
						articleListHandler.addContainer("#filter-container-vineyard", {
							"file" : '/templates/includes/2010/filter/wine/vineyards.inc.php'
						});
					}
				}
			}
		});
	}
	
	function bindFashionResizeHandlers() {
		tyResizeHandler.bind('filter-container-sizes', 'onResizeComplete', function(direction) {
			if (direction == -1) {
				if ($('#filter-container-sizes').length) {
					var boxClone = $('#filter-container-sizes').clone();
					boxClone.attr('id','filter-container-sizes-copy');
					boxClone.prependTo($('#tySideBarOverlowArea'));
					boxClone.slideDown('fast');
					$('#tySidebar2').hide();
					$('#fscontentWrap').removeClass('tygrw13').addClass('tygrw10');
					
					if (typeof articleListHandler != "undefined") {
						articleListHandler.addContainer("#filter-container-sizes-copy", {
							"file" : '/templates/includes/2010/filter/fashion/sizes.inc.php'
						});
						articleListHandler.addTrigger("#filter-container-sizes ul.filter-list a[rel]");
					}
				}
			}
		});
		tyResizeHandler.bind('filter-container-colors', 'onResizeComplete', function(direction) {
			if (direction == -1) {
				if ($('#filter-container-colors').length) {
					$('#tySideBarOverlowArea').html('').show();
					var boxClone = $('#filter-container-colors').clone();
					boxClone.attr('id','filter-container-colors-copy');
					boxClone.prependTo($('#tySideBarOverlowArea'));
					boxClone.slideDown('fast');
					
					if (typeof articleListHandler != "undefined") {
						articleListHandler.addContainer("#filter-container-colors-copy", {
							"file" : '/templates/includes/2010/filter/fashion/colors.inc.php'
						});
						articleListHandler.addTrigger("#filter-container-colors ul.filter-list a[rel]");
					}
				}
			}
		});
		tyResizeHandler.bind('filter-container-sizes', 'onBeforeResize', function(direction) {
			if (direction == 1) {
				if ($('#filter-container-sizes').length) {
					$('#tySidebar2').show();
					$('#fscontentWrap').removeClass('tygrw10').addClass('tygrw13');
					$('#filter-container-sizes-copy').slideUp('fast', function() { 
						$(this).remove();
					});
					
					if (typeof articleListHandler != "undefined") {
						articleListHandler.addContainer("#filter-container-sizes", {
							"file" : '/templates/includes/2010/filter/fashion/sizes.inc.php'
						});
					}
				}
			}
		});
		tyResizeHandler.bind('filter-container-colors', 'onBeforeResize', function(direction) {
			if (direction == 1) {
				if ($('#filter-container-colors').length) {
					$('#filter-container-colors-copy').slideUp('fast', function() { 
						$(this).remove();
						$('#tySideBarOverlowArea').hide().html('');
					});
					
					if (typeof articleListHandler != "undefined") {
						articleListHandler.addContainer("#filter-container-colors", {
							"file" : '/templates/includes/2010/filter/fashion/colors.inc.php'
						});
					}
				}
			}
		});
	}
	
	tyResizeHandler.events.onReady = function() {
		var leftWrap = $('body>.leftWrap');
		var docWidth = parseInt($('#windowSize').width());
		
		if (docWidth < ( tyResizeHandler.DOCUMENT_WIDTH_THRESHOLD + tyResizeHandler.DOCUMENT_CONDITIONAL_LEFT_PADDING ) ) {
			leftWrap.css('paddingLeft', '0px');
		}
		bindResizeHandlers();
	}
	tyResizeHandler.init();
	
	/*var menuColors = {
		"mnbg0" : '#15afa2',
		"mnbg1" : '#89c300',
		"mnbg2" : '#bf8e25',
		"mnbg3" : '#641727',
		"mnbg4" : '#824095',
		"mnbg5" : '#af156f'
	};
	
	/**
	 * effects for navigation
	 * /
	function initNavigation() {
		var timeout = null;
		
		$('ul.tyMainNav > li').hover(
			navigationItemHover, navigationItemOut
		);
		
		$('ul.tyMainNav a.mnL2act').hover( hideDownLevel2_helper, function() { clearTimeout(navL2Timeout); } );
	}
	
	var navL2Timeout = navigationItemTimeout = null;
	
	function navigationItemHover() {
		var element = $(this);
		clearTimeout(navigationItemTimeout);
		if (!element.hasClass('mnliact')) {
			navigationItemTimeout = setTimeout(function() {
				var bgContainer = element.children('.mnliL1bgc-a');
				var currClass = element.attr('class');
				var currBGClass = currClass.substr(currClass.lastIndexOf(' ')+1);
				var link = element.find('.mnliL1c > a');
				bgContainer.fadeIn(300);
				link.animate({"backgroundColor" : menuColors[ currBGClass ]}, {
					"duration" : 300,
					"easing" : "swing"
				});
			}, 500);
		}
	}
	
	function navigationItemOut() {
		clearTimeout(navigationItemTimeout);
		if (!$(this).hasClass('mnliact')) {
			var bgContainer = $(this).children('.mnliL1bgc-a');
			var link = $(this).find('.mnliL1c > a');
			bgContainer.fadeOut(300);
			link.animate({"backgroundColor" : '#1f1f1f'}, {
				"duration" : 300,
				"easing" : "swing"
			});
		}
	}
	
	function hideDownLevel2_helper(ev) {
		var domLink = this;
		navL2Timeout = setTimeout(function() {
			hideDownLevel2.call(domLink, ev);
		}, 300);
	}
	
	function hideDownLevel2(ev) {
		var link = $(this);
		var subLevelList = link.next('ul');
		
		link.unbind('mouseenter');
		
		link.animate(
			{
				"lineHeight" : '0px',
				"height" : '0px'
			},{
				"duration" : 200,
				"easing" : 'swing',
				"complete" : function() {
					slideUpLevel2Full(link);
				}
			}
		);
		if (subLevelList.length) {
			subLevelList.animate(
				{
					"bottom" : '0px'
				},{
					"duration" : 200,
					"easing" : 'swing'
				}
			);
		}
	}
	
	function slideUpLevel2Full(link) {
		var linkParentList = link.parents('ul.mnulL1');
		var subLevelList = link.next('ul');
		
		if (subLevelList.length) {
			subLevelList.hide();
		}
		
		linkParentList.hide();
		link.css('lineHeight', '17px').css('height', 'auto');
		link.removeClass('mnL2act');
		linkParentList.children('li').show();
		linkParentList.slideDown(200, function() {
			$(this).animate(
				{
					"paddingTop" : '10px',
					"paddingBottom" : '10px'
				},{
					"duration" : 100,
					"complete" : function() {
						$(this).addClass('mnulL1-full');
						$(this).bind('mouseleave', hideDownLevel2Full);
					}
				}
			);
		});
	}
	
	function hideDownLevel2Full() {
		$(this).unbind('mouseleave');
		
		var linkParentList = $(this);
		var link = linkParentList.find('a.mnL2cur');
		var subLevelList = link.next('ul');
		linkParentList.removeClass('mnulL1-full');
						
		linkParentList.animate(
			{
				"paddingTop" : '0px',
				"paddingBottom" : '0px'
			},{
				"duration" : 100,
				"complete" : function() {
					$(this).slideUp(200, function() {
						$(this).children().hide();
						link.css('lineHeight','0px').css('height','0px');
						link.addClass('mnL2act');
						slideUpLevel2(link);
					});
				}
			}
		);
	}
	
	function slideUpLevel2(link) {
		var subLevelList = link.next('ul');
		var linkParentList = link.parents('ul.mnulL1');
		link.parent().show();
						
		linkParentList.show();
		if (subLevelList.length) {
			subLevelList.show();
		}
		
		link.animate(
			{
				"lineHeight" : '45px',
				"height" : '45px'
			},{
				"duration" : 200,
				"easing" : 'swing',
				"complete" : function() { 
					link.hover( hideDownLevel2_helper, function() { clearTimeout(navL2Timeout); } );
				}
			}
		);
		if (subLevelList.length) {
			subLevelList.animate(
				{
					"bottom" : '45px'
				},{
					"duration" : 200,
					"easing" : 'swing'
				}
			);
		}
	}*/
	
	function expandNavigation() {
	    $('#headGoToBasketBtn').unbind('mouseenter');
	    $('#headGoToBasketBtn').unbind('mouseleave');
	    $('#search-input').attr('readonly','readonly');
	    
		$('#tyHeader>div').fadeTo(1000, 0.5);
		$(this).parents('#isportal').animate({width:'136px'}, 300, function() {
	        var targetWidth = '453px';//$('ul.mainnav-extension-list').css('width');
	        var el = $('.mainnav-extension');
	        $('#navigation>.navigationWrap').addClass('animating');
	        el.css('overflow', 'hidden');
	        el.css('width', '1px');
	        el.show();
	        el.animate({"width": targetWidth}, 700, function() {
	        	$('#navigation>.navigationWrap').removeClass('animating');
	        	$(this).css('overflow', 'visible');
	        });
	        
	        $('#isportal a').click(collapseNavigation);
	        $('#close_switch').click(collapseNavigation);
	    });
	    return false;
	}
	
	function collapseNavigation() {
		var trigger = $(this);
		var link = $('#isportal');
		trigger.unbind('click');
		$('#tyHeader>div').fadeTo(1000, 1, function() {
			$(this).css('opacity', null);
			if (typeof showShortBasket != "undefined") {
				$("#headGoToBasketBtn").hover(showShortBasket, hideShortBasket);
				$('#search-input').removeAttr('readonly');
				$('#navigation>.navigationWrap').removeClass('animating');
			}
		});
		$('#navigation>navigationWrap').addClass('animating');
		$('.mainnav-extension').css('overflow', 'hidden');
		$('.mainnav-extension').animate({width: '1px'}, 700, function() {
			$(this).hide();
			link.animate({width:'180px'}, 300);
		});
		return false;
	}
	
	$(function(){
		var browsercheck = true;
		var mytyObj = {basePath:'/tycon',session:{id:''}};
		if( typeof myty != "undefined" ) {mytyObj = myty;}
		if (jQuery.browser.msie) {if (parseInt(jQuery.browser.version) < 8) {browsercheck = false;}}
	   	$('#isportal .arrow').click(expandNavigation);
		var isHttps = false;
		var domainHtml = "";
		if( window.location.href.indexOf("https") !== -1 ){isHttps = true;}
		//if( window.location.href.indexOf("outlet.sansibar.de") === -1 ){domainHtml += '<img src="' + (isHttps?"https":"http") + '://outlet.sansibar.de/templates/domain_sync_new.php?session=' + mytyObj.session.id + '" width="1" width="1" border="0"/>';}
		if( window.location.href.indexOf("www.sansibar.de") === -1 ){domainHtml += '<img src="' + (isHttps?"https":"http") + '://www.sansibar.de/templates/domain_sync_new.php?session=' + mytyObj.session.id + '" width="1" width="1" border="0"/>';}
		if( window.location.href.indexOf("skgaming.sansibar.de") === -1 ){domainHtml += '<img src="' + (isHttps?"https":"http") + '://skgaming.sansibar.de/templates/domain_sync_new.php?session=' + mytyObj.session.id + '" width="1" width="1" border="0"/>';}
		//domainHtml += '<img src="' + (isHttps?"https":"http") + '://sansibar.de/templates/domain_sync_new.php?session=' + mytyObj.session.id + '" width="1" width="1" border="0"/>';
		$('body').append(domainHtml);
	});
})(jQuery);
