/* GRUEN Berlin javascript */

var gruencolors  = {
		'#281809' : '#ffffff',
		'#5c2612' : '#ffffff',
		'#421115' : '#ffffff',
		'#37072c' : '#ffffff',
		'#122b46' : '#ffffff',
		'#143746' : '#ffffff',
		'#474e10' : '#ffffff',
		'#333333' : '#ffffff',
		'#422b16' : '#ffffff',
		'#c0461a' : '#ffffff',
		'#960f28' : '#ffffff',
		'#5e0b4e' : '#ffffff',
		'#346695' : '#ffffff',
		'#006c81' : '#ffffff',
		'#909600' : '#000000',
		'#4d4d4d' : '#ffffff',
		'#7d4528' : '#ffffff',
		'#e17602' : '#ffffff',
		'#be281e' : '#ffffff',
		'#834777' : '#ffffff',
		'#5a87ad' : '#000000',
		'#2994a9' : '#ffffff',
		'#cfc129' : '#000000',
		'#808080' : '#000000',
		'#be9050' : '#000000',
		'#f49d00' : '#000000',
		'#d27370' : '#000000',
		'#bba3ca' : '#000000',
		'#8daec1' : '#000000',
		'#90c8d7' : '#000000',
		'#e8dd87' : '#000000',
		'#b3b3b3' : '#000000',
		'#ffffff' : '#000000',
		'#000000' : '#ffffff'
	
};


var words = {
	deutsch : {
		kontrast : {
			off : 'Normale Version',
			on : 'Hoher Kontrast'	
		}	
	},
	englisch : {
		kontrast : {
			off : 'Normal Version',
			on: 'High Contrast'
		}	
	}	
}

var loaded = false;



if(!(typeof console).match(/object/i))
{
	console = { log : function(){ /* leer ...*/ } };
}

dlhooks = [];

teaserinfo = {
	
		currentOpen : null,
		paddingTop : 10,
		paddingBottom : 11
		
	};

$(document).ready(function()
{
	if($("#fix-fp-image img").length > 0)
	{
		$("#fix-fp-image img").attr({
			'alt' : $("#fix-fp-image").attr('title')
			});
	}

	if($("#fix-logo-container img").length > 0)
	{
		$("#fix-logo-container img").attr({
			'alt':'Logo',
			'title':'Logo'
		});
	}
	
	if(readCookie('contrast')){
		
		if($('.tx-gruensetup-pi4').length > 0)
		{
			for(var i in teasercolors)
			{
				teasercolors[i] = '#000000'
			}
		}
		
		toggleContrast();
	}
	
	if($(".tx-indexedsearch").length > 0)
	{
		searchFixes();	
	}

	$("a").each(function(i,item){
        //if(!item.href.match(/\(/i))   item.href += '&no_cache=1';
	});

	$('#navigation-sub ul li').each(function() {
		$(this).mouseover(function() {
			openMenue($(this));
		});
	});

	$('#navigation-sub ul li').each(function() {
		$(this).mouseout(function() {
			closeMenue($(this));
		});
	});
	
	
	teaserinfo.totalheight = 520;
	teaserinfo.itemheight = 0;
	teaserinfo.itemheights = {};
	teaserinfo.count = $(".teaser").length;
	
	$(".teaser").each(function(i,item){


		if(!$(item).hasClass('number2'))
		{
			hideAllButHeadLines(item);
		}
		else
		{
				var _id = $(item).attr('id').split("-")[1];

				$(item).css('backgroundColor',teasercolors[_id]);
				$(item).find("*").css('color',gruencolors[teasercolors[_id]]);
				$(item).find("a").css('background-image', 'url(/fileadmin/templates/images/layout/icons/'+gruencolors[teasercolors[_id]].split('#').join('')+'.gif)');
		}
		
	//	$(item).find("h5").css('marginTop','10px');
	/*	
		$(item).css({
			paddingTop : '0px',
			paddingBottom: '0px'
		});
		*/
		teaserinfo.itemheight += $(item).height();
	
		
		
		 var h = $(item).height();
		h = h < 280?280:h;
		teaserinfo.itemheights[ $(item).attr('id').split("-")[1] ] = h;
		
		
		$(item).css(
			{
				visibility:"visible",
				overflow : 'hidden'
			});
			

		
	});
	
	if($(".tx-gruensetup-pi4").length > 0)
	{
		try{
			expandTeaser($(".number2").attr('id').split("-")[1]);
			$(".tx-gruensetup-pi4").css("height",teaserinfo.totalheight);
		}
		catch(Excp)
		{
			//WAS?
		}
	}	

	
	
	$(".teaser").hover(
		function()
		{

				var _id = $(this).attr('id').split("-")[1];

				$(this).css('backgroundColor',teasercolors[_id]);
				$(this).find("*").css('color',gruencolors[teasercolors[_id]]);
				$(this).find("a").css('background-image', 'url(fileadmin/templates/images/layout/icons/'+gruencolors[teasercolors[_id]].split('#').join('')+'.gif)');

		},
		
		function()
		{

				var _id = $(this).attr('id').split("-")[1];
				if(teaserinfo.currentOpen != _id)
				{
					$(this).css('backgroundColor','');
					$(this).find("*").css('color','#000000');
				}
			
		}
	).click(function(){
		//window.location = $($(this).find("a")[0]).attr('href');
		
		expandTeaser($(this).attr('id').split("-")[1])
		
	});

	$(dlhooks).each(function(i,fn){ 
		fn(); 
	});
	
	
	loaded = true;
});

function searchFixes()
{
	
	if($(".browsebox").length == 0)
	{
		$(".tx-indexedsearch-browsebox").css("display","none");
		return;	
	}
	
	$($(".tx-indexedsearch-browsebox")[1]).css({"display":"block"}).prepend('<p style="float:left;text-transform:uppercase;margin-top:0">'+(ENGLISH?'More results':'Weitere Ergebnisse')+'</p>').find("a").each(function(i,item){
		if($(item).text().match(/previous|next/i)){
			$(item).parent().remove();	
		}
	});
	var auszeichnungsfarbe = $('body#content-text-bild div#content div#content-layer div#content-area div div a').css('color')
	$('.browsebox .tx-indexedsearch-browselist-currentPage a').css('background-color', auszeichnungsfarbe);
	$('#content-area .tx-indexedsearch .browsebox a').css({'background-image':'none !important', 'padding-left':'0'});
	$('.browsebox a').mouseover(function() {$(this).css('background-color', auszeichnungsfarbe)});
	$('.browsebox a').mouseout(function() {$(this).css('background-color', '#ffffff')});
	$('.browsebox .tx-indexedsearch-browselist-currentPage a').mouseout(function() {$(this).css('background-color', auszeichnungsfarbe)});
}

function expandTeaser($teaserID)
{
	if(!teaserinfo.currentOpen)
	{
		//ist der erste!
		
		var fullHeight = teaserinfo.itemheights[$teaserID] +2; // +2 wg. Versatz mit Content
		var closedHeight = Math.floor((520 - (teaserinfo.count * (teaserinfo.paddingBottom + teaserinfo.paddingTop))  - fullHeight) / (teaserinfo.count -  1)) ;



	
		$("#teaser-"+$teaserID).css({
					height : fullHeight 
				});	
		setImageInActiveTeaser($("#teaser-"+$teaserID));
		for(var i in teaserinfo.itemheights)
		{
			if(i != $teaserID)
			{
				$("#teaser-"+i).css({
					height : closedHeight 
				});	
			}
			else
			{
				
			}
		}
		
		
		teaserinfo.currentOpen = $teaserID;
		return;
	}
	
	if($teaserID == teaserinfo.currentOpen)return;
	teaserinfo.currentOpen = $teaserID;

	var fullHeight = teaserinfo.itemheights[$teaserID] +2; // +2 wg. Versatz mit Content 
	var closedHeight = Math.floor((520 - (teaserinfo.count * (teaserinfo.paddingBottom + teaserinfo.paddingTop))  - fullHeight) / (teaserinfo.count -  1)) ;
	
	var total = (closedHeight * (teaserinfo.count -1)) +  fullHeight;
	
	if(total < 415)
	{
		fullHeight += 415 - total; // 14.07.2009 20:26:55 MT
	}
	
	for(var i in teaserinfo.itemheights)
	{
		if(i != $teaserID)
		{
			
			hideAllButHeadLines($("#teaser-"+i));
			$("#teaser-"+i).find("*").css({
				'color':'#000'
			});
			
			$("#teaser-"+i).css({
				'backgroundColor':'#fff'
				}).animate({
				height : closedHeight 

			},500);	
		}
		else
		{
			showAllElems($("#teaser-"+i));
			$("#teaser-"+i).animate({
				height : fullHeight 

			},500, function() { setImageInActiveTeaser(this) });
		}
	}	

	

}

function setImageInActiveTeaser(teaser) {
	var teaserHeight = $(teaser).height();
	var teaserChildren = $(teaser).children('*:not(img)');
	var teaserImage		 = $(teaser).children('img');
	textHeight = 0;
	teaserChildren.each(function() {
		textHeight += $(this).outerHeight(true);
	});

	var imageMargin = teaserHeight - textHeight - teaserImage.height() + 4;
	teaserImage.animate({ marginTop: imageMargin }, 400); //css('margin-top', imageMargin);
}

function hideAllButHeadLines(item)
{
	$(item).find("*").each(function(i,item){
		if(!(item.tagName).match(/5|6/i))
		{
			$(item).css('visibility','hidden');	
		}
	});	
}

function showAllElems(item)
{
	$(item).find('*').css('visibility','visible');	
}


var contrast = false;

function toggleContrast()
{
	if(contrast)
	{
		$('body').removeClass('kontrast');
		$("link[href='/fileadmin/templates/css/styles-contrast.css']").remove();
		/*
		if (jQuery.browser.msie || jQuery.browser.safari) {
			$("head").append('<link href="/fileadmin/templates/css/styles.css" media="screen" type="text/css" rel="stylesheet">');
		}
		*/
		eraseCookie('contrast')
		
		$("a[href*=Contrast]").text(words[ENGLISH?'englisch':'deutsch'].kontrast.on);
		contrast=false;
		
		if(loaded && $('.tx-gruensetup-pi4').length > 0)
		{
			
			for(var i in otc)
			{
				teasercolors[i] = otc[i];	
			}

			
				$('.tx-gruensetup-pi4 .teaser').each(function(i,item)
				{
					if($(item).attr('id').split("-")[1] == teaserinfo.currentOpen)
					{
						$(item).css('background',teasercolors[$(item).attr('id').split("-")[1]]).find('*').css('color',
						gruencolors[teasercolors[$(item).attr('id').split("-")[1]]]
						).find('a').css('background-image', 'url(/fileadmin/templates/images/layout/icons/'+gruencolors[teasercolors[$(item).attr('id').split("-")[1]]].split('#').join('')+'.gif)');
					}
				});

		}
		
	}
	else
	{
		$('body').addClass('kontrast');
		$("head").append('<link href="/fileadmin/templates/css/styles-contrast.css" media="screen" type="text/css" rel="stylesheet">');	
		createCookie('contrast',true);
		$("a[href*=Contrast]").text(words[ENGLISH?'englisch':'deutsch'].kontrast.off);
		contrast = true;
		
		if(loaded && $('.tx-gruensetup-pi4').length > 0)
		{
			if(teasercolors)
			{
	
				for(var i in teasercolors)
				{
					teasercolors[i] = '#000000';
				}
				
				$('.tx-gruensetup-pi4 .teaser').each(function(i,item)
				{
					if($(item).attr('id').split("-")[1] == teaserinfo.currentOpen)
					{
						$(item).css('background','#000').find('*').css('color','#fff').find('a').css('background-image', 'url(/fileadmin/templates/images/layout/icons/ffffff.gif)');
					}
				});
				
			}
			
		}
		
	}
}



function openMenue(navElement) {
	if (navElement.children('ul').length > 0) {
		var ul = navElement.children('ul')[0];

		var tmpMenuOffset = $(ul).offset().left;
		var tmpMenuWidth  = $(ul).width();
		var offsetMenue = tmpMenuOffset + tmpMenuWidth;
		
		var offsetContent = 0;
		var tmpContentOffset = $('#content-layer').offset().left;
		var tmpContentWidth  = $('#content-layer').width();
		offsetContent = tmpContentOffset + tmpContentWidth;
		
		if (offsetMenue > offsetContent) {
			var left				= parseInt($(ul).css('left'));
			var paddingLeft	= parseInt($(ul).css('padding-left'));
			var newLeft = left + (2 * paddingLeft);
			
			$(ul).css('padding-right', paddingLeft + 'px');
			$(ul).css('left', '-' + newLeft + 'px');
		}
		
		$(ul).css('visibility', 'visible');
	}
}

function closeMenue(navElement) {
	if (navElement.children('ul').length > 0) {
		var ul = navElement.children('ul')[0];
		$(ul).css('visibility', 'hidden');
	}
}


var IMGTITLES = {};

function showGal($galid)
{
	if($("#imgdump").length == 0)
	{
		$(document.body).append('<div id="imgdump"></div>');
	}
	
	
	IMGTITLES = {};
	
	$("#imgdump").empty();
	
	var content = galleries[$galid];
	for(var i = 0,o; o = content[i]; i++)
	{
		$("#imgdump").append('<a class="gal2item" href="/uploads/tx_gruengal/'+o.img+'" rel="shadowbox[gal-'+$galid+']" title="'+gallerytitles[$galid] +'"></a>');	
		IMGTITLES[window.location.protocol+'//'+window.location.hostname+'/uploads/tx_gruengal/'+o.img] = o.title;
	}

	Shadowbox.gallery = [];
	Shadowbox.cache = [];
	
	Shadowbox.setup("a.gal2item",{
        displayNav : true,
        slideshowDelay : 4,
        gallery : 'shadowbox[gal-'+$galid+']',
        autoPlay : true
    });
	
	Shadowbox.open($("a.gal2item")[0]);
	
	$("#sb-nav-pause").css("display","none");
	$("#sb-nav-play").css("display","block");
	
	//setTimeout('pauseShadowBoxAfterOpen()',2000);
	
}


function showVid($galid)
{
	if($("#imgdump").length == 0)
	{
		$(document.body).append('<div id="imgdump"></div>');
	}
	
	$("#imgdump").empty();
	
	var content = vids[$galid];
	

	$("#imgdump").append('<a class="gal2item" href="'+content.vid+'" rel="shadowbox[gal-'+$galid+'];height='+content.h+';width='+content.w+';backcolor:#ff0000" title="'+vidtitles[$galid] +'"></a>');	


	Shadowbox.gallery = [];
	Shadowbox.cache = [];
	

	
	Shadowbox.setup("a.gal2item",{
        displayNav : false,
        player : 'flv',
        gallery : 'shadowbox[gal-'+$galid+']'
      
    });
    
    /*Shadowbox.options.flashVars['backcolor'] = '#e17602';*/
    Shadowbox.options.flashVars['skin'] = '/fileadmin/templates/javascript/shadowbox/players/skin/nacht/nacht.swf';
    
/*
	Shadowbox.options.flashVars['height'] = 500;
	Shadowbox.options.flashVars['width'] = 600;
	Shadowbox.options.flashVars['autostart'] = false;
*/
	Shadowbox.open($("a.gal2item")[0]);
	
	$("#sb-nav-pause").css("display","none");
	$("#sb-nav-play").css("display","block");
	$("#sb-nav-close").css("display","block");
	
	
}

function pauseShadowBoxAfterOpen(){
	Shadowbox.pause();	

}


function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}
