/*CVS Add-ins*/
/*
$Revision: 1.31 $
$RCSfile: teaserbehaviour.js,v $
author: JDE
*/

/* + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + */
/*
    teaserbehaviour.js (AJ)
    21.11.2008  JDE
*/
/* + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + */

/* + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + */
/*Copyright (c) A&B FACE2NET GmbH*/

 var c_arr = new Array();
 var sliding = true;
 var current_nr = 0;

$(document).ready(function(){ 
	$(".jquerybox #boxnavigation li a")
		.each(function(i){
			c_arr.push( this.href.substr(this.href.indexOf("/start/")) ); //channel path for firstteaser
			if ("current_channel" == String($(this).parent("li").attr("id"))) current_nr = i;
		});
	$('#footer .copy').after('<div id="aj_service"></div>'); //container fuer serviceinhalte
	read_ctd();
	preload_firstImages();
	if(sliding) set_slider();
});

function read_ctd(){
	if(typeof teaserdata !="undefined"){
		try{	
			ctd = $.toJSON(teaserdata);
			if( has_validnum($.secureEvalJSON(ctd).teaser_pos) ){
				write_prevTeaser($.secureEvalJSON(ctd).channel_path, $.secureEvalJSON(ctd).teaser_pos, $.secureEvalJSON(ctd).teaser_path);
				write_nextTeaser($.secureEvalJSON(ctd).channel_path, $.secureEvalJSON(ctd).teaser_pos, $.secureEvalJSON(ctd).teaser_path);
				write_firstTeaser();
				prepare_focus();
				write_service();
				if($("*").index( $('#contact')[0] != -1 )) {
					send_message();
				}
			}//if
						
		} catch(err){
		  //dev comment
			alert("Es ist ein Fehler aufgetreten:"+err.toString());
		}//catch
		
	}//if
}//read_ctd()


/* + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + */
/* functions for dhtml / loading */
/* + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + */

/* content */

/* ausgelagert an Server
function write_turnPage( ctp, ctc ){

	var ctp = String(parseInt(ctp)+1); //position in teaserarray starts with 0 not 1
	$("#content .turnPage .page")
		.empty()
		.append('<span><span class="out">Seite</span>'+ctp+'/'+ctc+'</span>');	
}*/

function write_prevTeaser( cc_p, ctp, ct_p ){
	$("#content .turnPage .rew a")
		.attr("href","#thema")
		.unbind( "click" )
		.click(function(){ 
			if(sliding) stop_sliding();
			$("#content").fadeTo("slow", 0 ,function(){
				$("#content").load(generate_prevTeaser_path(cc_p, ctp, ct_p),
					function() { 
						read_ctd();
						$("#content").fadeTo("slow", 1); 
						preload_turnPageImages();
					}
				);
			});
		});
}

function write_nextTeaser( cc_p, ctp, ct_p ){
	$("#content .turnPage .ff a")
		.attr("href","#thema")
		.unbind( "click" )
		.click(function(){ 
			if(sliding) stop_sliding();
			$("#content").fadeTo("slow", 0 ,function(){
				$("#content").load(generate_nextTeaser_path(cc_p, ctp, ct_p),
					function() { 
						read_ctd();
						$("#content").fadeTo("slow", 1); 
						preload_turnPageImages();
					}
				);
			});
		});
}

function write_firstTeaser(){
	var i = 0;
	$(".jquerybox #boxnavigation li a")
		.attr("href","#thema")
		.each(function(i){
  		this.rel = i;
		})
		.unbind( "click" )
		.click(function(){ 
			var firstpath = c_arr[parseInt(this.rel)];
			reset_channels();
			if(sliding) stop_sliding();
			$(this).parent("li").attr("id", "current_channel"); 
			$("#content").fadeTo("slow", 0 ,function(){
				$("#content").load( generate_firstTeaser_path(firstpath),
					function() { 
						read_ctd(); 
						$("#content").fadeTo("slow", 1);
						preload_turnPageImages();
						return false;
					}//function
				);//load
				
			});//fadeTo

		});//click
}

function reset_channels(){
 var defaultsrc = String($(".jquerybox #boxnavigation ul li#current_channel img.entries").attr("src")).replace(/eintrag_h/,"eintrag_d");
  if(defaultsrc != "undefined"){ $(".jquerybox #boxnavigation ul li#current_channel img.entries").attr("src",defaultsrc); }
 $("#boxnavigation li").removeClass("hover");
 $("#boxnavigation li").removeAttr("id");
  
}


/* Footer */

function write_service(){
	$("#service .footerlinks li[@id^='aj_'] a")
		.attr("href","#service")
		.unbind( "click" )
		.click(function(){
			if(sliding) stop_sliding();
			var currentservice = "".concat($(this).parent().attr("class"));
			if(currentservice==""){
				var servicepath = $(this).parent().attr("id");
				$("#aj_service").fadeTo("slow", 0.1 ,function(){
					$("#aj_service").load(generate_service_path( servicepath ),
					function() { 
						$("#aj_service").fadeTo("normal", 1);
						close_service();
						read_ctd();
					}//function
				); //load
			});//fadeTo
			$("#service")[0].focus();
			$("#service .footerlinks li").removeClass("active");
			$(this).parent().addClass("active"); 
		}//if
		});
}

function close_service(){
	$("#aj_service .display a").click(function(){
		$("#aj_service").fadeTo("slow", 0 ,function(){
			$("#aj_service").empty();
			$("#aj_service").fadeTo("normal", 1);
			$("#service .footerlinks li").removeClass("active");
			$("#searchheader")[0].focus();
		});//fadeTo	
	});//click
}



function send_message(){
	$("form#formcontact").submit(function(){
		//alert($("#empfaenger").val()+":"+$("#empfaenger")[0].selectedIndex);
		var select_nr = $("#empfaenger")[0].selectedIndex;
		if ($('#consent').is(':checked')) var datasafe = $("#consent").val();
		else var datasafe = "nein";
		if ($('#nl').is(':checked')) var nlaccept = $("#nl").val();
		else var nlaccept = "0";

		$.post("/generate/kontakt", { empfaenger: $("#empfaenger").val(), empfaenger_nr: select_nr, tTitle: $("#tTitle").val(), tMessage: $("#tMessage").val(), eMail: $("#eMail").val(), sName: $("#sName").val(), fName: $("#fName").val(), uStreet: $("#uStreet").val(), uCity: $("#uCity").val(), phone: $("#phone").val(), orga: $("#orga").val(), consent: datasafe, nl: nlaccept }, 
			function(data){
				build_statusmessage(data);
			}
		);
		return false;
	});
}//send_message()


function build_statusmessage(data){
	if(data.match("var error = {}")){
		write_statusmessage("<strong>Vielen Dank.</strong><br> Ihre Nachricht wurde versandt.");
	}else if(data.match("var error = {")){
		var errors = data.replace(/var error = {/, "{");
		errors = errors.replace(/\"/g, '"');			
		var e_messages = new Array($.secureEvalJSON(errors).empfaenger, $.secureEvalJSON(errors).tTitle, $.secureEvalJSON(errors).sName, $.secureEvalJSON(errors).fName, $.secureEvalJSON(errors).tMessage, $.secureEvalJSON(errors).eMail );
		var e_labels = new Array("empfaenger", "tTitle", "sName", "fName", "tMessage", "eMail");                 	
		write_errormessage("<strong>Bitte korrigieren Sie Ihre Eingaben!</strong>", e_messages);
	}//if
}//build_statusmessage(data)

function write_statusmessage(msg){
	$("#contact #footercontent")
		.empty()
		.prepend('<p class="status">'+msg+'</p>');
}

function write_errormessage(msg, e_messages){
	//alert("Fehler:"+msg);
	for(var i_e=0; i_e <= e_messages.length; i_e++){
		if(e_messages[i_e] != undefined) {
			msg += "<br>"+e_messages[i_e];
		}//if
	}//for
	$("#contact #footercontent p").remove(".error");
	$("#contact #footercontent")
		.prepend('<p class="error">'+msg+'<br></p>');
}


function fallbackTeaserNavigation(){
	return false;
}

/* + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + */
/* functions for slider */
/* + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + */

function set_slider(){
	var slider = window.setTimeout("slide_teaser()", 15000);
}

function stop_sliding(){
 sliding = false;
}

function slide_teaser(){
	if(sliding){
		try{
			//alert("slider ausgefuehrt:"+c_arr[current_nr]+":"+current_nr);
			if (c_arr.length-1 > current_nr ) { current_nr++; }
			else { current_nr = 0; }
			reset_channels();
			var firstpath = c_arr[current_nr];
			var next_nr = current_nr+1;
			mark_currentChannel(next_nr);
			$("#content").fadeTo("slow", 0 ,function(){
				$("#content").load( generate_firstTeaser_path(firstpath),
					function() { 
						
						$("#content").fadeTo("slow", 1);
						read_ctd(); 
						return false;
					}//function
				);//load
				
			});//fadeTo
	
				
		} catch(err){
		}//catch
	set_slider();
	}//else
}//slide_teaser

/* + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + */
/* functions for pathgeneration */
/* + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + */

/**
* generate path for next teaser
*/
function generate_nextTeaser_path( cc_p, ctp, ct_p ){
	return "/generate/next"+cc_p+"?teaser_path="+ct_p+"&teaser_pos="+ctp;
}

/**
* generate path for previous teaser
*/
function generate_prevTeaser_path( cc_p, ctp, ct_p ){
	return "/generate/prev"+cc_p+"?teaser_path="+ct_p+"&teaser_pos="+ctp;
}

/**
* generate path for first channel teaser
*/
function generate_firstTeaser_path( c_p ){
	return "/generate/first"+c_p;
}

/**
* generate path for service path
*/
function generate_service_path( s_p ){
	return s_p.replace("aj_","/start/portal/")+".html";
}




/* + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + */
/* function for preloading */
/* + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + */
function preload_firstImages(){
	if(typeof firstTeasers !="undefined"){
		var fimgs = new Array(); 
		var cimgs = new Array(); 
		try{	
			var elem = 0;
			jQuery.each(firstTeasers, function(attr, val) {
				fimgs[elem]=val;
				elem++
			});//each

			for (var cii=0; cii < fimgs.length; cii++){
				cimgs[cii] = new Image();
				cimgs[cii].src = fimgs[cii];
			}//for

		} catch(err){
		  //dev comment
		}//catch
	}//if


}

function preload_turnPageImages(){
	if(typeof teaserdata !="undefined"){
		var timgs = new Array(); 
		var ctimgs = new Array(); 
		try{	
			var elem = 0;
			jQuery.each(teaserdata.preloadable, function(attr, val) {
			if(not_null( val )){
				timgs[elem]=val;
				elem++;
				//alert(val);
				}//if
			});//each

			for (var cii=0; cii < timgs.length; cii++){
				ctimgs[cii] = new Image();
				ctimgs[cii].src = timgs[cii];
			}//for

		} catch(err){
		//devcomment
		}//catch
	}
}


/* + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + */
/* little helpers */
/* + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + */
function has_num( str ){
	checkNum = parseInt( str );
	return (isNaN( checkNum ) ? true : false );
}

function has_validnum( str ){
	checkNum = parseInt(str);
	if(isNaN( checkNum )) return false;
	else return ( checkNum <= 9 ? true: false );
}

function not_null( val ){
	return (val != null ? true : false);
}

function prepare_focus(){
	$('#content').prepend('<div id="thema"></div>');
}


function mark_currentChannel(current_index){
	$('.jquerybox #boxnavigation li:nth-child('+current_index+')').attr("id", "current_channel");
	var activesrc = String($(".jquerybox #boxnavigation ul li#current_channel img.entries").attr("src")).replace(/eintrag_d/,"eintrag_h");
	if(activesrc != "undefined"){ $(".jquerybox #boxnavigation ul li#current_channel img.entries").attr("src",activesrc); }
}