// tyclipso.net JavaScript Document
var wrap4cols = 1230;
var innerwrap4cols = 1000;
var wrap3cols = 980;
var innerwrap3cols = 750;

$(document).ready( function(){
	var browsercheck = true;
	
	/** Update h1, h2 Elements for sifr usage **/
	$('#content-wrapper h1:not(h1[class])').each(function() { $(this).addClass("fco01") } );
	$('#content-wrapper h2:not(h2[class])').each(function() { $(this).addClass("fco01") } );
	
	/** Switch Search input default text **/
	$('input#search').focus(function() { if (this.value == this.defaultValue){ this.value = ''; } if(this.value != this.defaultValue){ this.select(); } });
   	$('input#search').blur(function() { if ($.trim(this.value) == ''){ this.value = (this.defaultValue ? this.defaultValue : ''); } });
	$('input#blogsearch').focus(function() { if (this.value == this.defaultValue){ this.value = ''; } if(this.value != this.defaultValue){ this.select(); } });
   	$('input#blogsearch').blur(function() { if ($.trim(this.value) == ''){ this.value = (this.defaultValue ? this.defaultValue : ''); } });

   	if (jQuery.browser.msie) {
   		if (parseInt(jQuery.browser.version) < 8) {
   			browsercheck = false;
   		}
   	}
   	
	/*if (browsercheck == true) {
		$('#tyMainContent abbr[title]').qtip( {
		  position: { corner: {  tooltip: 'topMiddle', target: 'bottomMiddle' } },
		  style: { 
			width: { min: 80, max: 250 } , padding: 5, background: '#1f1f1f', color: '#FFFFFF', textAlign: 'left', border: { width: 7, radius: 5, color: '#1f1f1f' },
			tip: { corner: 'topMiddle' },
			name: 'dark' 
		  }
		} );
		$('#tySidebar abbr[title]').qtip( {
		  position: { corner: {  tooltip: 'topLeft', target: 'bottomRight' } },
		  style: { 
			width: { min: 80, max: 250 } , padding: 5, background: '#1f1f1f', color: '#FFFFFF', textAlign: 'left', border: { width: 7, radius: 5, color: '#1f1f1f' },
			tip: { corner: 'topLeft' },
			name: 'dark' 
		  }
		} );
	}*/
	
	$('abbr').tooltip({ 
		id: 'tyToolTip', 
		fade: 300, showURL: false, fixPNG: true, opacity: 1, top: 15, left: 0 
	});
});

var tyResizeHandler = {
	MIN_DOCUMENT_WIDTH : 800,
	GRIDSIZE : 80,
	
	tolerance : 20,
	sizeBefore : 0,
	timeout : null,
	
	events : {
		'onBeforeResize' : [],
		'onResizeComplete' : []
	},
	animations : {},
	animationIDs : [],
	defaultDuration : 300,
	defaultEasing : 'swing',
	animationDelay : 200,
	
	lastUniqueID : 0,
	
	init : function() {
		tyResizeHandler.sizeBefore = $('body').width();
		
		$(window).resize( tyResizeHandler.resize );
		$(document).ready( tyResizeHandler.resize );
	},
	
	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;
		
		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);
			}
		}
	},
	
	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() {
		var docWidth = $('body').width();
		
		if (tyResizeHandler.sizeBefore - docWidth > tyResizeHandler.tolerance || docWidth - tyResizeHandler.sizeBefore > tyResizeHandler.tolerance) {
			tyResizeHandler.sizeBefore = docWidth;
			
			clearTimeout(tyResizeHandler.timeout);
			tyResizeHandler.timeout = setTimeout(function(){
				var columnsShowable = (docWidth - (docWidth % tyResizeHandler.GRIDSIZE)) / tyResizeHandler.GRIDSIZE; // 11 Columns
				var downsizeAnimations = [];
				
				$('.rightbox:hidden').each(function(){
					var element = $(this);
					tyResizeHandler.addAnimation(
						'enlarge:rightboxes_' + tyResizeHandler.getUniqueID(),
						element,
						element.slideDown, {
							// no special options
						},
						null, {
							"direction" : 1
						}
					);
				});
				
				$('.tyResizableContainerWrap').each(function(){
					var element = $(this);
					
					if (columnsShowable >= 15 && element.hasClass('tygrw13')) {
						// 1. Spalte einblenden
						var elementParent = element.parent();
						var targetWidth = element.find('.maxsize').width();
						
						if (elementParent.width() < targetWidth) {
							tyResizeHandler.addAnimation(
								'enlarge:' + element.parent().attr('id'),
								elementParent, {
									"width" : targetWidth+'px'
								}, {
									// no special options
								}, null, {
									"direction" : 1
								}
							);
						}
					}
					
					if (columnsShowable >= 17 && element.hasClass('tygrw15')) {
						// 2. Spalte einblenden
						var elementParent = element.parent();
						var targetWidth = element.find('.maxsize').width();
						
						if (elementParent.width() < targetWidth) {
							tyResizeHandler.addAnimation(
								'enlarge:' + element.parent().attr('id'),
								elementParent, {
									"width" : targetWidth+'px'
								}, {
									// no special options
								}, null, {
									"direction" : 1
								}
							);
						}
					}
					
					if (columnsShowable < 17 && element.hasClass('tygrw15')) {
						// 2. Spalte ausblenden
						var elementParent = element.parent();
						var targetWidth = element.find('.minsize').width();
						
						if (elementParent.width() > targetWidth) {
							downsizeAnimations.push({
								"uniquekey" : 'downsize:' + element.parent().attr('id'),
								"element" : elementParent,
								"parameters" : {
									"width" : targetWidth+'px'
								},
								"options" : {
									// no special options
								},
								"callback" : null,
								"additionals" : {
									"direction" : -1
								}
							});
						}
					}
				
					if (columnsShowable < 15 && element.hasClass('tygrw13')) {
						// 1. Spalte ausblenden
						var elementParent = element.parent();
						var targetWidth = element.find('.minsize').width();
						
						if (elementParent.width() > targetWidth) {
							downsizeAnimations.push({
								"uniquekey" : 'downsize:' + element.parent().attr('id'),
								"element" : elementParent,
								"parameters" : {
									"width" : targetWidth+'px'
								},
								"options" : {
									// no special options
								}, 
								"callback" : null,
								"additionals" : {
									"direction" : -1
								}
							});
						}
					}
					
				});
				
				$('.tyVerticalResizableContainer').each(function(){
					var element = $(this);
					
					if (columnsShowable >= 17 && element.hasClass('tygrw02')) {
						if (element.is(':hidden')) {
							tyResizeHandler.addAnimation(
								'enlarge:' + element.attr('id'),
								element,
								element.slideDown, {
									// no special options
								},
								null, {
									"direction" : 1
								}
							);
						}
					}
					
					if (columnsShowable < 17 && element.hasClass('tygrw02')) {
						if (element.is(':visible')) {
							downsizeAnimations.push({
								"uniquekey" : 'downsize:' + element.attr('id'),
								"element" : element,
								"parameters" : element.slideUp,
								"options" : {
									// no special options
								},
								"callback" : null,
								"additionals" : {
									"direction" : -1
								}
							});
						}
					}
				});
				
				downsizeAnimations.reverse();
				
				$(downsizeAnimations).each(function(idx, anim) {
					tyResizeHandler.addAnimation( anim.uniquekey, anim.element, anim.parameters, anim.options, anim.callback, anim.additionals );
				});
				
				tyResizeHandler.animate();
			}, 200);
		}
	},
	
	getUniqueID : function() {
		return ++this.lastUniqueID;
	}
};

tyResizeHandler.init();

if (typeof tyIsBlog != 'undefined') {
	var menuColors = {
		"mnbg0" : '#15afa2',
		"mnbg1" : '#89c300',
		"mnbg2" : '#bf8e25',
		"mnbg3" : '#641727',
		"mnbg4" : '#824095',
		"mnbg5" : '#af156f'
	};
} else {
	var menuColors = {
		"mnbg0" : '#15afa2',
		"mnbg1" : '#641727',
		"mnbg2" : '#bf8e25',
		"mnbg3" : '#89c300',
		"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'
			}
		);
	}
}

tyResizeHandler.bind('tyTwitterBox', 'onResizeComplete', function(direction) {
	if (direction == -1) {
		var twitterBoxClone = $('#tyTwitterBox').clone();
		twitterBoxClone.attr('id','tyTwitterBoxCopy');
		twitterBoxClone.appendTo($('#tySidebar'));
		twitterBoxClone.slideDown('fast');
	}
});
tyResizeHandler.bind('tyTwitterBox', 'onBeforeResize', function(direction) {
	if (direction == 1) {
		$('#tyTwitterBoxCopy').slideUp('fast', function() { $(this).remove(); });
	}
});

jQuery(function() {
	initNavigation();
	
	tyResizeHandler.bind('leftOffsetContainer', 'onBeforeResize', function(direction) {
		if (direction == 1) {
			$('#leftOffsetContainer').find('.newsblog').removeClass('tygrw06');
		} else if (direction == -1) {
			$('#leftOffsetContainer').find('.newsblog').removeClass('tygrw09');
		}
	});
	tyResizeHandler.bind('leftOffsetContainer', 'onResizeComplete', function(direction) {
		if (direction == 1) {
			$('#leftOffsetContainer').find('.newsblog').addClass('tygrw09');
		} else if (direction == -1) {
			$('#leftOffsetContainer').find('.newsblog').addClass('tygrw06');
		}
	});
});
