/* IE PNG fix multiple filters */
(function ($) {
    if (!$) return;
    $.fn.extend({
        fixPNG: function(sizingMethod, forceBG) {
                if (!($.browser.msie)) return this;
                var emptyimg = "/clear.gif"; //Path to empty 1x1px GIF goes here
                sizingMethod = sizingMethod || "scale"; //sizingMethod, defaults to scale (matches image dimensions)
                this.each(function() {
                        var isImg = (forceBG) ? false : jQuery.nodeName(this, "img"),
                                imgname = (isImg) ? this.src : this.currentStyle.backgroundImage,
                                src = (isImg) ? imgname : imgname.substring(5,imgname.length-2);
                        this.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "', sizingMethod='" + sizingMethod + "')";
                        if (isImg) this.src = emptyimg;
                        else this.style.backgroundImage = "url(" + emptyimg + ")";
                });
                return this;
        }
    });
})(jQuery);

var bg = '#bg';		// id of background div
var wr = '#wrap';		// id of wrapper div
var hpBodyClass = 'body_4'; // body class of home page

jQuery(function($) {
   initLongsunday();
});

function initLongsunday() {
   	replaceNavi();
   	initResizing( bg, wr );
	initVisualSlideshow();
	initLightboxes();
	initVisitedHighlighting();
	// Intro deactivated!!!
	//initHiddenContent(hpBodyClass);
	initFacebookLinks();
	openFacebookLinks();
}

function initFacebookLinks() {
	if($('#wrap').hasClass('overlay')) {
		$fbLink = 'http://www.facebook.com/plugins/like.php?locale=de_DE&send=false&layout=button_count&width=450&show_faces=false&action=like&colorscheme=light&font=tahoma&height=21&href=';
		// $fbLink = $fbLink + encodeURIComponent(location.href);
		$fbLink = $fbLink + encodeURIComponent(location.href + '&parent=' + parent.location.href);
		$('#facebook iframe').attr('src', $fbLink);
	}
}

function openFacebookLinks() {
	if(getUrlVars()['parent'] && getUrlVars()['parent'].length > -1) {
		var isPage = getUrlVars()['parent'].match(/pointer/);
		var searchSign = (isPage && isPage.length > 0) ? "&" : "?";
		location.href = decodeURIComponent(getUrlVars()['parent']) + searchSign + 'ttnews=' + getUrlVars()['tx_ttnews[tt_news]'];
	}
	if(getUrlVars()['ttnews']) {
		var newsid = getUrlVars()['ttnews'];
		$('div.news-list-item[rel='+newsid+'] a').trigger('click');
	}
}

var hcShown = false;
function initHiddenContent(cl) {
	if($('body').hasClass(cl)) {
		$('#content').append('<div id="skipintro"><a href="#"></a></div>');
		$('#skipintro a').click(function(e) { e.preventDefault(); showHiddenContent();});
		window.setTimeout( 'showHiddenContent()', 8000 );
	}
}

function showHiddenContent() {
	if(hcShown) return; // call function only once!
	hcShown = true;
	$('#skipintro').fadeOut();
	$('#contentBody > *').fadeOut( 600, function() {
		$('#contentHidden > *').prependTo('#contentBody').fadeIn( function() {
			replaceNewsLatest();
			replaceNewsMore();
		});
	});
}

function initResizing(bg, wr) {
	repositionElement(bg);
	repositionElement(wr);
	$(window).resize( function() {
		repositionElement(bg);
		repositionElement(wr);
	});
}

function repositionElement(c) {
	var wh = $(window).height();
	var ch = $(c).height();
	if(wh > ch) {
		$(c).css('top', Math.ceil((wh-ch)/2));
	}
}

function initVisualSlideshow() {
	$('#visualBody').cycle({
		timeout: 15000
		,cleartype: true
		,cleartypeNoBg: true
	});
}

function initVisitedHighlighting() {
	$('.csc-menu-1 a').click( function(evt) {
		evt.preventDefault();
		$.cookie('ls-'+$('.csc-menu-1 a').index($(this)), 1);
		$(this).addClass('visited');
	});
	$('.csc-menu-1 a').each(function(i) {
		if($.cookie('ls-'+i) == 1) {
			$(this).addClass('visited');
		}
	});
}

function initLightboxes() {
	// append single news id for impressionen page to information menu
	$('.csc-menu-1 a:last').attr('href', $('.csc-menu-1 a:last').attr('href') + '?tx_ttnews[tt_news]=73');
	
	$('.ls-news .news-list-item a, .ls-news .news-latest-item a, .ls-projects a, .csc-menu-1 a, .news-list-browse a').addClass('iframe').fancybox({
			// 'transitionIn'	:	'elastic'
			// 			,'transitionOut': 'elastic'
			// 			,'easingIn'		:  'swing'
			// ,'speedIn'		:	300
			// 			,'speedOut'		:	200 
			'overlayShow'	:	true
			,'overlayColor':  '#000000'
			,'overlayOpacity':0.5
			,'titleShow'   :  false
			,'margin'		: 	0
			,'width'			:  1100
			,'height'		:  733
			,'showCloseButton':false
			,'scrolling'	:  'no'
			,'autoScale'	:  false
			// ,'centerOnScroll':  true
			,'onStart'		:  function() { $.fancybox.showActivity(); }
		});
	$('.close').live('click', function(evt) {
		evt.preventDefault();
		parent.$.fancybox.close();
	});
	if($('.news-single-img').parents('.impressionen').length>0) {
		$('#img-pager').addClass('impressionen');
	}
	$('.news-single-img').cycle({
	   fx:     'fade'
      ,speed:  500
      ,timeout: 0
      ,pager:  '#img-pager'
		,next: '#next'
		,slideExpr: 'div.news-img'
		,containerResize: 1
		,width: 680
		,height:parseInt($('.news-images').height())
		,after: function() {}
		,before: function() {
				// $(this).css({'margin-top':$(this).parent().height()-$(this).height()});
				// 				if( $(this).find('img').height() > $(this).find('img').width() || $(this).find('img').width() < $('#contentBody .news-images').width() ) {
				// 					$(this).find('img').addClass('repositioned');
				// 				}
				positionImg(this);
			}
	});
	// only one image? then reposition image anyway
	if($('.news-img').length == 1) {
		positionImg($('.news-img'));
	}
}

function positionImg(el) {
	var margTop = $(el).parent().height()-$(el).height();
	margTop = (margTop < 40) ? 40 : margTop;
	if( $(el).find('img').height() < $(el).find('img').width() ) {
		margTop = (margTop > 120) ? 120 : margTop;	
	}
	$(el).css({'margin-top':margTop+'px'});
	//$(el).css({'margin-top':$(el).parent().height()-$(el).height()});
	if( $(el).find('img').height() > $(el).find('img').width() || $(el).find('img').width() < $('#contentBody .news-images').width() ) {
		$(el).find('img').addClass('repositioned');
	}
}

function getUrlVars() {
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }
    return vars;
}



