// ----------------------------------------------------------------------------
// DOM READY
// ----------------------------------------------------------------------------

$(document).ready(function() {

	// REMOVE EMPTY ELEMENTS
	$('#tpl_menu ul:empty, #tpl_submenu ul:empty').parent().remove();

	// FIX MENU2 DIVIDER
	$('#tpl_submenu a:last').css('border-right-width', '0px');

	// BIND EVENTS FOR CLICKABLE ITEMS
	$('.clickable').bind('click', function() {
		var url = $(this).find('a[href]:last').attr('href');
		var target = $(this).find('a[href]:last').attr('target');
		if(url) {
			if($('base[href]').length > 0) { // IE BUG
				url = url.replace($('base[href]').attr('href'), '');
				url = $('base[href]').attr('href') + url;
			}
			if($.data(document, 'notclickable') == false) {
				if(target == '_blank') {
					window.open(url);
				} else {
					window.location.href = url;
				}
			}
		}
	});

	// SET VARIABLE FOR NOT CLICKABLE ITEMS
	$('.clickable .notclickable').bind('mouseenter', function() {
		$.data(document, 'notclickable', true);
	}).bind('mouseleave', function() {
		$.data(document, 'notclickable', false);
	});
	$.data(document, 'notclickable', false);

	// BIND EVENTS FOR HOVERABLE ITEMS
	$('.hoverable').bind('mouseenter', function() {
		$(this).addClass('hover');
	}).bind('mouseleave', function() {
		$(this).removeClass('hover');
	});

	// BIND EVENTS FOR FORMS
	$('.formline, .formarea, .formselect').bind('focus blur', function() {
		$(this).toggleClass('focus');
	});
	$('.formbut').bind('mouseenter mouseleave', function() {
		$(this).toggleClass('hover');
	});


	// ------------------------------------------------------------------------
	// LOAD EXTERNAL SCRIPTS / PLUGINS
	// ------------------------------------------------------------------------

	$.getScript(jquery_coreurl+'/include/jquery/easing.latest.js', function() {
	$.getScript(jquery_coreurl+'/include/jquery/cycle.latest.all.js', function() {

		// NEWS ANIMATION
		$('.tpl_newsitem:first .content .image img').each(function() {
			//console.log('Laden: ' + $(this).attr('src'));
			//$(this).bind('load', function(event) {
				//console.log('Geladen: ' + $(this).attr('src'));
				$('.tpl_newsitem .content').show().height(0);
				$('.tpl_newsitem').bind('mouseenter', function() {
					$('.tpl_newsitem').removeClass('hover').find('.content').stop(true, false).animate({
						height: 0
					}, 1000, 'easeOutExpo');
					$(this).addClass('hover').find('.content').stop(true, false).animate({
						height: 320
					}, 1000, 'easeOutExpo');
				});
				$('.tpl_newsitem:first').addClass('hover').find('.content').stop(true, false).animate({
					height: 320
				}, 0, 'easeInOutExpo');
				
				$('.tpl_newsitem_wrapper').bind('mouseenter', function() {
					window.clearInterval(window.newsintervalid);
				}).bind('mouseleave', function() {
					//$('.tpl_newsitem').eq(0).trigger('mouseenter');
					window.newsintervalid = window.setInterval("$('.tpl_newsitem_wrapper').trigger('newsitemnext')", 6000);
				}).bind('newsitemnext', function() {
					var index = $('.tpl_newsitem.hover').index('.tpl_newsitem');
					//alert(index);
					index = index + 1;
					if(index == $('.tpl_newsitem').length) {
						index = 0;
					}
					$('.tpl_newsitem').removeClass('hover').find('.content').stop(true, false).animate({
						height: 0
					}, 1000, 'easeOutExpo');
					$('.tpl_newsitem').eq(index).addClass('hover').find('.content').stop(true, false).animate({
						height: 320
					}, 1000, 'easeOutExpo');
				}).trigger('mouseenter');
				window.newsintervalid = window.setInterval("$('.tpl_newsitem_wrapper').trigger('newsitemnext')", 6000);
			//}).attr('src', $(this).attr('src')+'?r='+(Math.floor(Math.random()*1000000)));
		});

		// PROJECT STATUS ANIMATION
		$('.tpl_statusitem_menu div.name a').bind('click', function(event) {
			event.preventDefault();
			var index = $(this).index('.tpl_statusitem_menu a.forindex');
			$('.tpl_statusitem_menu div.name a').removeClass('active');
			$(this).addClass('active');
			$('.tpl_statusitem_content .content').hide().eq(index).show();
			Cufon.refresh();
		}).eq(0).trigger('click');

		// CYCLE ANIMATION
		$('.diashowpictures').cycle();

	});
	});


	// ------------------------------------------------------------------------
	// ONLOAD / LOAD COMPLETE
	// ------------------------------------------------------------------------

	$(window).load(function() {

		// FOOTER LOGO EVENT
		$('#tpl_footer_logoos img').each(function() {
			var source = $(this).attr('src');
			source = source.replace('.png', '_bg.png');
			$(this).parent().css({
				backgroundImage: 'url('+source+')'
			}).bind('mouseenter', function() {
				if($.browser.msie && $.browser.version < 9) {
					$(this).find('img').stop(true, false).fadeTo(0, 0);
				} else {
					$(this).find('img').stop(true, false).fadeTo(350, 0);
				}
			}).bind('mouseleave', function() {
				if($.browser.msie && $.browser.version < 9) {
					$(this).find('img').stop(true, false).fadeTo(0, 1);
				} else {
					$(this).find('img').stop(true, false).fadeTo(350, 1);
				}
			});
		});

	});

});
