
/*************************************************** Externe Links vorbereiten */

function externalLinks() 
	{	
	if (!document.getElementsByTagName) return; 
 	var anchors = document.getElementsByTagName("a"); 
 	for (var i=0; i<anchors.length; i++)
		{ 
   		var anchor = anchors[i]; 
   		if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "blank")anchor.target = "_blank"; 
		} 
	} 
	
/*************************************************** IE6 */

function handleIE6()
	{
	if($.browser.msie && parseInt($.browser.version,0)<7)
		{
		$(":text").addClass("ie6textinput");
		$("textarea").addClass("ie6textinput");
		$("input[type='password']").addClass("ie6textinput");
		}
	}

/*************************************************** killCSS */

function css(parent,element,css)
	{
	if($(parent).length)
		{
		$(parent).find(element).css(css)
		}
	}

/*************************************************** Tabs left */

var tabs, togglers, hash, bg;

function changeTab(idx)
	{
	// tabs.hide();
	//togglers.removeClass('active');
	var tab = $(tabs[idx]);
	var toggler = $(togglers[idx]);
	if(tab.length)
		{
		tab.toggle(500);
		toggler.toggleClass('active').blur();
		}
	}
	
function tabNav()
	{
	if($('#links .tabs').length)
		{
		togglers = $('#links').find('.toggler');
		tabs = $('#links').find('.tab .contents');
		togglers.each(function(idx)
			{
			$(this).click(function(event)
				{
				event.preventDefault();
				changeTab(idx);
				});
			});
		tabs.not('.nohide').hide();
		}
	}

/*************************************************** Orderform */

var slide, pos,toggle=0;

function showForm()
	{
	slide.toggle(500,function(){
		if(toggle==0)
			{
			pos = parseInt(slide.position().top)+'px';
			toggle=1;
			}
		else if(toggle==1)
			{
			pos = '200px';
			toggle=0;
			}
		$('html').animate({scrollTop:pos}, 'slow'); 
	});
	this.blur();
	}
	
function orderForm()
	{
	if($('#mitte .slide').length)
		{
		var button = ' <div class="order"><a onclick="showForm()" class="orderbtn"> Produkt bestellen</a></div>';
		slide = $('#mitte .slide');
		var buttonloc = $('#mitte .desc p:first');
		buttonloc.append(button);
		slide.not('.nohide').hide();
		}
	}

function spendeForm()
	{
	if($('#mitte .slidespende').length)
		{
		var button = ' <div class="order"><a class="orderbtn"> Zum Spendenformular</a></div>';
		slide = $('#mitte .slidespende');
		$('#mitte .box h1:contains(SPENDENFORMULAR)').replaceWith(slide);
		slide.prev("p").append(button);
		button = $('#mitte .box').find('.order');
		slide.not('.nohide').hide();
		button.click(function(event){
			event.preventDefault();
			slide.toggle(500,function(){
				if(toggle==0)
					{
					pos = parseInt(slide.position().top)+'px';
					toggle=1;
					}
				else if(toggle==1)
					{
					pos = '200px';
					toggle=0;
					}
				$('html').animate({scrollTop:pos}, 'slow'); 
			});
			this.blur();
			});
		}
	}

/*************************************************** set & get Cookies */

function setCookie(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 getCookie(name) 
	{
	var name_eq = 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(name_eq) == 0) return c.substring(name_eq.length,c.length);
		}
	return null;
	}

/*************************************************** change font size */

var currentSize=90, maxSize=100, minSize=80;

function changeSize(fontSize)
	{
	$('body').css({'font-size':fontSize+'%'});
	}

function fontSize()
	{
	if($('#fontsize').length)
		{
		var anchors = $('#fontsize').find('a');
		anchors.each(function(idx){
			$(this).click(function(event)
				{
				event.preventDefault();
				if(idx==0)
					currentSize = currentSize-10;
				if(idx==1)
					currentSize = currentSize+10;
				if(currentSize>maxSize)
					currentSize = maxSize;
				if(currentSize<minSize)
					currentSize = minSize;
				changeSize(currentSize);
				setCookie('font_size',currentSize,90);
				$(this).blur();
				});
			});
 		}
	}


function handleCookies()
	{
	var cookie_fontSize = parseInt(getCookie("font_size"));
	if(cookie_fontSize && cookie_fontSize != null)
		{
		changeSize(cookie_fontSize);
		currentSize = cookie_fontSize;
		}
	}

/*************************************************** handle galleries */

var curImg = {};
var gallery;

function handleGalleries()
	{
	if($('.gallery').length)
		{
		$('.gallery').each(function(idx){ 
			curImg[idx] = 0;
			$
			});
		}
	}

function prevImg(galleryId)
	{
	gallery = $('#gallery'+galleryId);
	
	if(curImg[galleryId]==0) curImg[galleryId] = imagesCount[galleryId]-1;
	else curImg[galleryId] = curImg[galleryId]-1;
	
	gallery.find('img').attr('src',images[galleryId][curImg[galleryId]].src);
	}

function nextImg(galleryId)
	{
	gallery = $('#gallery'+galleryId);
	
	if(curImg[galleryId]==imagesCount[galleryId]-1) curImg[galleryId] = 0;
	else curImg[galleryId] = curImg[galleryId]+1;
	
	gallery.find('img').attr('src',images[galleryId][curImg[galleryId]].src);
	}

/*************************************************** facebook share */

function facebookLinks()
{
	if($('.facebook').length)
	{
	$('.facebook').click(function()
		{
			u=location.href;
			t=document.title;
			window.open('http://www.facebook.com/sharer.php?u='+encodeURIComponent(u)+'&t='+encodeURIComponent(t),'sharer','toolbar=0,status=0,width=626,height=436');
			return false
		})
	}
}

/*************************************************** Funktionsaufrufe */

$(document).ready(function(){
	externalLinks();
	tabNav();
	fontSize();
	handleCookies();
	handleGalleries();
	css('#mitte .more','.product:nth-child(3n)',{'margin-right':0});
	facebookLinks();
	handleIE6();
});

$(window).load(function()
{
	orderForm();
	spendeForm();
});
