/**
 * @author stefan.schult
 * @id $Id: functions.js 315 2010-10-25 11:08:01Z stefan.schult $
 */
function refreshBrowser() {
	if (typeof refreshIE7 == 'function') {
		refreshIE7();
	}
}

var ie = (function(){
    var undef, v = 3, div = document.createElement('div');

    while (
        div.innerHTML = '<!--[if gt IE '+(++v)+']><i></i><![endif]-->',
        div.getElementsByTagName('i')[0]
    ){}

    return v> 4 ? v : undef;
}());

(function(){
  // if firefox 3.5+, hide content till load (or 3 seconds) to prevent FOUT
  var d = document, e = d.documentElement, s = d.createElement('style');
  if (e.style.MozTransform === ''){ // gecko 1.9.1 inference
    s.textContent = 'body{visibility:hidden}';
    e.firstChild.appendChild(s);
    function f(){ s.parentNode && s.parentNode.removeChild(s); }
    addEventListener('load',f,false);
    setTimeout(f,3000);
  }
})();

/**
 * console.debug
 */
(function() {
	if (typeof console == 'undefined') {
		window['console'] = {};
	}
	;
	if (typeof console.debug == 'undefined') {
		window['console'].debug = function() {
			if (typeof console.log == 'undefined') {
			}
			else console.log(arguments);
		};
	}
	window.fieldsetInitState = {};
}());

/**
 * global inits
 */
$(function(){
	window.isSubpage = ($('div#subPage').length) ? true : false;

	$('table tr:nth-child(odd)').addClass('odd');
	$('table tr:nth-child(even)').addClass('even');

	$('body>div:first')
		.removeClass('no-js')
		.addClass('js');
	$('div.imagesLinkListMod li:nth-child(4n+1)')
		.addClass('firstInRow');
	$('div.richTextMod > :first-child')
		.addClass('firstChild');
});


$(function(){
	$('div.galleryMod').ppGallery({prefix:'gallery'});
	$('div.tabs').ppTabs();
	$('.contMod:first').wrap($('<div class="firstContMod" />'));
});


/**
 * youtubePlayerMod
 */
$(function(){
	var ytPlayerMod = $('div.youtubePlayerMod');
	function getVideoId(url){
//		url = $('p.player a:first', this).attr('href');
		url = encodeURI(url);
		var urlParamsStr = url.split('?')[1];
		var urlParams = urlParamsStr.split('&');
		var v = '';
		$.each(urlParams, function(i, o){
			var pa = o.split('=');
			if(pa[0] == 'v') {
				v =  pa[1];
				return false;
			}
		});
		return v;
	}
	ytPlayerMod.each(function(i){
		var $this=this;
		var player = null;
		var pid = 'ytPlayer-' +i;
		var vid = getVideoId($('p.player a', this).attr('href'));
		if(window.isSubpage){
			var dim = {
				width: 420,
				height: 268
			};
		}
		else {
			var dim = {
				width: 514,
				height: 320
			};
		}
		$('p.player', this).replaceWith('<div class="player" />');

		$('div.player', this).tubeplayer({
			width: dim.width, // the width of the player
			height:dim.height, // the height of the player
			playerID: pid,
			initialVideo: vid,
			onPlayerEnded: function(){
				$('.youtubePlayerLayer', $this).removeClass('hide').width(dim.width-2).height(dim.height-2);
				$('.player object', $this).attr("style", "position:absolute;left:-4000px;");
			}
		});

		//Attach the headline
		$('.youtubePlayerLayer', $this).wrapInner($('<div class="innerYoutubePlayerLayer" />'));
//		var headline = $('.iContMod h3:lt(0)', $this);
//		$('.innerYoutubePlayerLayer', $this).prepend($('<p class="layerHl"></p>').text(headline.text()));
//		headline.addClass("inv");
		//Attach the event listener
		$('.playback a', $this).click(function(){
			$('.player object', $this).attr("style","");
			$('.player', $this).tubeplayer("play");
			$('.youtubePlayerLayer', $this).addClass('hide');
			return false;
		});

	});
});

// print page
$(function(){
	$('div#navMetaBottom ul').append($('<li><a href="javascript:window.print();">Seite drucken</a></li>'));
});


$(function(){
	$('div#navMain ul:first>li:has(ul)')
		.append('<div class="flyout-pointer" />')
		.hover(
			function(){
				$(this).addClass('onHover');
			},
			function(){
				$(this).removeClass('onHover');
			}
		)
		.focusin(function(){
			$(this).addClass('onHover');
		})
		.focusout(function(){
			$(this).removeClass('onHover');
		})
});

/**
 * Form
 */
$(function(){
	function checkPreFilledContent(field){
		return field.value == $(field).data('preFilled') || field.value == '';
	}
	$('input.preFilled').each(function(){
		var l = $(this).siblings('label');
		$(this).data('preFilled', $.trim(l.text()));
		$(this).toggleClass('isPreFilled', checkPreFilledContent(this));
	}).focus(function(){
		var check = checkPreFilledContent(this);
		if(check) {
			this.value = '';
			$(this).removeClass('isPreFilled');
		}
	}).blur(function(){
		var check = checkPreFilledContent(this);
		if(check) {
			this.value = $(this).data('preFilled');
			$(this).addClass('isPreFilled');
		}
	}).trigger('blur');

});

// projectMap
$(function(){
	var projects = isSubpage ? $('.projects').clone(true) : $('.projects').remove();
	$('.country', projects).each(function(){
		var selector = 'div.' + this.className.split(' ')[1];
		var country = $(this).find('h3').remove().text();
		$(this).find('li').each(function(){
			var liSelector = selector + ' li.' + this.className + ' div.iProject';
			var project = $(this).find('a').text();
			var imgSrc = $(this).find('img').attr('src');
			var link = $(this).find('a').attr('href');
			var linkTitle = $(this).find('a').attr('title');
			$(this).empty();
			$(this).append($('<div class="iProject"><img src="' + imgSrc + '" alt="" /><strong>' + country + '</strong><span>' + project + '</span><a href="' + link + '">' + linkTitle + '</a></div><div class="pointer" />'));
			$(this).click(function(){
				location.href = link;
			});
		});
	}).hover(function(){
			var css = "div#content div.projects div." + this.className.replace(" ", ".");
			$(css).addClass('onHover');
		}, function(){
			var css = "div#content div.projects div." + this.className.replace(" ", ".");
			$(css).removeClass('onHover');
		});
	projects.attr('id', 'map');
	if(projects.length) {
		projects.removeClass('projects');
		$('body>div:first').addClass('hasMap');
		$('div#colsContent').before(projects);
		$('.projectsTeaser').remove();

	}

	if(window.isSubpage) {
		$('.projects h2').remove();
		$('.projects .country').click(function(){
			location.href = $('a', this).attr('href');
		}).hover(function(){
			var css = "#map ." + this.className.replace(" ", ".");
			$(css).addClass('onHover');
		}, function(){
			var css = "#map ." + this.className.replace(" ", ".");
			$(css).removeClass('onHover');
		})
	}
});

$(function(){
	var donateSchool = $('div.donateSchool');
	var href = $('a[href]', donateSchool).attr('href');
	donateSchool.click(function(){
		location.href = href;		
	});
});

$(function(){
	$('.forms p span.error').parents('p').addClass('hasError');
});

/*All loaded show Body*/
$(function(){
	document.body.className = "";
});

// Helper for Gallery
$(function(){
	$('div.galleryMod').ppGallery("reloadCss");
});

// Helper for NavSub-Headline
$(function(){
	var navSub = $('div#navSub');
	var t3 = $('h3', navSub).html();
	$('h3', navSub).replaceWith($('<div class="headOuter"><div class="headBefore"></div><h3>' + t3 + '</h3><div class="headAfter"></div></div>'));
	var w0 = $('.headOuter', navSub).outerWidth();
	var w1 = Math.round( $('.headBefore', navSub).outerWidth() * 100 / w0 );
	var w2 = Math.round( $('h3', navSub).outerWidth() * 100 / w0 );
	$('h3>*', navSub).css('paddingRight', '0');
	var w3 = 100-w1-w2;

	$('.headBefore', navSub).css('width', w1 + '%');
	$('h3', navSub).css('width', w2 + '%');
	$('.headAfter', navSub).css('width', w3 + '%');
});

//Helper for Ambassador
$(function(){
	$(document).ready(function() {
		//build lists of four items
		var orig_ul = $('#homePage .tabs div.imageTickerMod ul');
		var lis = orig_ul.find('li:lt(4)').remove();
		var transform_list = function (lis) {
			var i = 0;
			var new_list = "";
			$(lis).each(function() {
				if (i % 4 == 0) {
					new_list = $('<div class="ambassador-box"></div>');
				}
				var transformed_element = $('<p></p>').html($(this).html());
				$(orig_ul).parent().append(new_list.append(transformed_element));
				i++;
			});
		};
		transform_list(lis);
		transform_list($(orig_ul).find('li'));
		$(orig_ul).remove();
		//attach the gallery
		$('#homePage .tabs div.imageTickerMod')
			.ppGallery({
				prefix:'imgTicker',
				items: '.iContMod>div'
			}).ppGallery("reloadCss");
	});
});

// Forms

$(function(){
	var groups = {
		P: "addressData-private",
		F: "addressData-company",
		S: "addressData-school",
		O: "addressData-organisation"
	};
	function addresstypeChanged(which) {
		// this == fieldset.addressData
		$('p.addressData', this)
			.removeClass('inv')
			.data('addressdata-show', true);
		$('p.addressData:not(p.'+which+')', this)
			.addClass('inv')
			.data('addressdata-show', false);
	}
	$('fieldset.addressData').each(function(){
		var $t = $(this);
		var fields = $(':input:not(type=[hidden])', $t.parents('form')).ppFormsSkipHiddenFields();
		var fn = $.proxy(addresstypeChanged, $t);
		var addresstype = $("select#form-onlinedonation-addresstype", this);
		if(addresstype.length) {
			fn(groups[addresstype.val()]);
			addresstype.change(function(){
				fn(groups[this.value]);
			})
		}
	});
});

// Facebook - LikeItButton
$(function(){
	$('div#facebookLikeIt').fbLike(encodeURIComponent(location.href), {
		width: 300
	});
});

$(function() {
	if($('.tx-indexedsearch-searchbox-sword').length) {
		$('.tx-indexedsearch-searchbox-sword').val($('.tx-indexedsearch-searchbox-sword').val());
	}
});

$(function() {
	refreshBrowser();
});


// i-gelb Erweiterung

$(document).ready(function() {
  	var cnt = $('.image-caption .bodytext').html();
  	/*console.log(cnt);*/
});




