function sendform (action)
{
    document.frm_contact.formtype.value = action;
    document.frm_contact.submit();
}


var compulsoryFieldsIn=new Array();
compulsoryFieldsIn[0]="stdIn3";
function checkFieldsIn ()
{
var checkOK=true;
for (var i=0; i<compulsoryFieldsIn.length; i++)
{
var cfObj=document.getElementById(compulsoryFieldsIn[i]);
if (cfObj!=null)
{
if (cfObj.type.toLowerCase()=="text")
{
if (cfObj.value.match(/^\s*$/)) checkOK=false;
}
else if (cfObj.type.toLowerCase()=="radio" || cfObj.type.toLowerCase()=="checkbox")
{
var tmpObj=document.getElementsByName(cfObj.name);
var tmpCheck=false;
for (var j=0; j<tmpObj.length; j++)
{
if (tmpObj[j].checked==true)
{
tmpCheck=true;
break;
}
}
checkOK=tmpCheck;
}
else if (cfObj.type.toLowerCase().indexOf("select")>=0)
{
if (compulsoryFieldsIn[i]=="stdIn12" && cfObj.selectedIndex==0) checkOK=false;
}
}
if (!checkOK) break;
}
if (checkOK) document.getElementById('subscribe').submit();
else window.alert("Bitte füllen Sie alle mit einem * markierten Felder aus.");
}


//COOKIES
function getCookie(name) {
	var cookiename = 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(cookiename) == 0) return c.substring(cookiename.length,c.length);
	}
	return null;
}
function setCookie(name,value,$days,$p){
	if(!$p) $p="/";
	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="+$p;
}
function eraseCookie(n) {
	createCookie(n,"",-1);
}




// MAIN NAVIGATION
function updateNavi(replaceNaviActive){
	$('.tabs a').each(function(){
		if ($(this).hasClass('active')) Cufon.replace($(this), {color: $(this).css('color')});
		else {
			naviShadow = $(this).css("text-shadow");
			Cufon.replace($(this), {color: $(this).css('color'), textShadow: naviShadow});
		}
	});
}
function resetNaviElement() {
	if ($(this).hasClass('active')) return;
	Cufon.replace($(this), {color:'#ffffff', textShadow: naviShadow});
}



function buildRightNavi() {
	
	function showRightTab(id) {
		setCookie('lastTabElement', id.toString());
		
		$target = rightContents[id];
		$('#bottom_right .content').remove();
		$('#bottom_right').append($target);
		
		$rightNavi.find('a').removeClass('active before after');
		$rightNavi.find('a:lt('+id+')').addClass('before');
		$rightNavi.find('a:gt('+id+')').addClass('after');
		$rightNavi.find('a:eq('+id+')').addClass('active');
		
		$('.gallery .gallery_image a').lightBox({overlayBgColor: '#000', overlayOpacity: 0.9});
		$('#lightbox_show_all').click(function(e){e.preventDefault();$('.gallery .gallery_image a:first').trigger('click');});
		//$('a[href$=".jpg"]').lightBox({overlayBgColor: '#000', overlayOpacity: 0.9});
		updateNavi();
		updateContentSize();
	}
	
	var $item;
	var rightContents = new Array();
	var $rightNavi = $('<div class="tabs"></div>');
	var $rightTabs = $('<ul></ul>');
	$rightNavi.append($rightTabs);
	
	$('#bottom_right h3:first').before($rightNavi);
	$('#bottom_right').append('<div class="clear"></div>');
	
	$('#bottom_right').children('h3').each(function(){
		rightContents.push($(this).next());
		$item = $('<li style="width:115px"><a style="cursor:pointer;width:100%;padding-left:0;padding-right:0;text-align:center;">'+$(this).html()+'</a></li>');
		$item.attr('contentID', rightContents.length - 1);
		$rightTabs.append($item);
		
		$item.click(function(){
			showRightTab($(this).attr('contentID'));
		});
		
		$(this).remove();
	});
	
	var lastTab = getCookie('lastTabElement');
	if (lastTab) showRightTab(lastTab.toString());
	else showRightTab(0);
}

function updateTopNavi(timeCalled){
	if (timeCalled == null) timeCalled = 1;
	
	$('#navi a').css({'padding-left':'0px', 'padding-right': '0px', 'width':'auto'});
	var naviWidth = ($('#navi a:last').position().left + $('#navi a:last').width());
	var maxWidth = ($('#navi').width());
	var padding = Math.floor((maxWidth - naviWidth) / $('#navi a').size() * 0.5);
	
	$('#navi a').css({'padding-left':padding+'px', 'padding-right': padding+'px', 'width':'auto'});
	
	setTimeout(function(){
		var difference = maxWidth - ($('#navi a:last').position().left + $('#navi a:last').width() + padding * 2);
		if (difference % 2 == 0) $('#navi a:last').css({'padding-left':(padding+difference*.5)+'px', 'padding-right':(padding+difference*.5)+'px'});
		else  $('#navi a:last').css('padding-right',(padding+difference)+'px');
	}, 500);
	
	if (timeCalled < 3) setTimeout(function(){updateTopNavi(timeCalled+1)}, 150);
	//var size = Math.floor(100 / $('#navi a').size());
	//$('#navi a').css({'padding-left':'3px', 'padding-right': '3px', 'width': size + "%"});
	
	//var size = Math.floor(100 / $('#subnavi a').size());
	//$('#subnavi a').css({'padding-left':'0', 'padding-right': '0', 'width': size + "%", 'text-align': 'center'});
}

function updateContentSize(timeCalled){
	if (timeCalled == null) timeCalled = 1;
	
	$content = $('#bottom_left');
	$facebook = $('#facebook-status');
	
	if ($facebook.size() == 0) return;
	
	$content.css('height', 'auto');
	
	var minHeight = ($facebook.position().top + $facebook.height() - $('#bottom_right .tabs a.active').height() - 44);
	if ($content.height() < minHeight) $content.css('height', minHeight + 'px');
	
	if (timeCalled < 300) setTimeout(function(){updateContentSize(timeCalled+1);}, 150);
}

function updateTiscoverSize(){
	//$('#tiscover_events').ready(function(){
		//alert($("#tiscover_events").contents().find("body").height());
	//$("#tiscover_events").height($("#tiscover_events").find("body").height());

	//});
}


function setShadowBackgroundHeight() {
	$('#shadowWrap, #mainContainer').css('height', ($(window).height() >= $('#shadowWrap').height() ? $(window).height() + 'px' : 'auto'));
	$('#shadowWrap, #mainContainer').css('height', ($(window).height() >= $('#shadowWrap').height() ? $(window).height() + 'px' : 'auto'));
}



var naviShadow;

function ready()
{
	if(typeof initialize == 'function') initialize();
	if(typeof GUnload == 'function')    GUnload();
	
	buildRightNavi();
	Cufon.replace('h1, h2, h3, .cufon');
	$('.tabs a').mouseover(updateNavi).mouseout(resetNaviElement);
	updateNavi(true);
	updateTopNavi();
	updateContentSize();
	updateTiscoverSize();
	
	if ($('#subnavi a.active').size() == 0)
	{
		$('#subnavi a:not(:first)').removeClass('before after active').addClass('after');
		$('#subnavi a:first').removeClass('before after active').css('background-image', 'none');
	}
	
	var headerExtension = '';
	
	try {
		if (document.cookie.indexOf('styletype=fruehling') >= 0 ) headerExtension = '-fruehling';
	} catch(e){}
	
	if (headerExtension == '-fruehling')	{
			// BACKGROUND STRETCHER
			
			var backgroundCount = 9;
			var backgroundID = Math.floor(Math.random() * backgroundCount);
			var backgroundPath = 'images/backgrounds/' + (backgroundID < 10 ? '0' + backgroundID : backgroundID) + '.jpg';
			backgroundPath = 'images/backgrounds/bg-green.gif';
			
			//$('body').css('background', '#ffffff');
			$('#mainContainer').wrap('<div id="shadowWrap"/>');
			$('#shadowWrap').css({'z-index': '9999', 'position': 'relative', 'width':'1179px', 'margin': '0 auto', 'background':'url(images/background-shadow-fruehling.png) repeat-y'});
			$('#footer2').css({'padding-bottom': '20px', 'margin': '0'});
			//$('<div style="height:10px">&nbsp;</div>').insertAfter('#footer2');
			window.setInterval(setShadowBackgroundHeight, 500);
			$(window).resize(setShadowBackgroundHeight);
			setShadowBackgroundHeight();
			
			headerExtension = '-fruehling';
			
			//var bgStretch = $('<div id="bgstretcher" style="width:100%; height:100%; z-index:0; background:#75CC6D; position:fixed !important; top:0; left:0;"></div>');
			//$('body').append(bgStretch);
			
			//$(document).bgStretcher({images: [backgroundPath], imageWidth: 400, imageHeight: 400});
			//$(document).bgStretcher({images: [backgroundPath], imageWidth: 1024, imageHeight: 768});
			//$('<div id="bgstretcher-raster">&nbsp;<div>').prependTo('#bgstretcher');
		}
	
	$('#facebook-status iframe').css('height', '88px');
	
	$('#domain').css({
		'width': '100%',
		'height': '116px',
		'position': 'absolute',
		'top': '0px',
		'left': '0px',
		'background': "url('images/header-" + language + headerExtension + ".gif')"
	});
	$('#domain').html('');
	
	$('a[href=#ppmedien_office]').attr('href', 'mailto:office@pp-medien.at');
}

$(document).ready(ready);
