/*SVN Add-ins

$Rev:: 2969       $: Revision of last commit
$Author:: cgl     $: Author of last commit
$Date:: 2012-02-08 1#$: Date of last commit

Authors: JDE, SPI
Copyright (c) A&B One Digital GmbH
*/
/* + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + */
/*
    behaviour.js
    13.01.2011  JDE
*/
/* + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + */

/* + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + */

;(function($) {

/* + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + */
/**
* browser detection

var agt = navigator.userAgent.toLowerCase();
var ff  = (agt.indexOf("firefox") != -1);
var op = (agt.indexOf("opera") != -1);
var mac_sf = ((agt.indexOf('applewebkit') != -1) || (navigator.vendor == "Apple Computer, Inc."));
var mac_ff  = ( (agt.indexOf("firefox") != -1) && (agt.indexOf("macintosh") != -1) );
*/

var agt = navigator.userAgent.toLowerCase();
var is_ie6, is_ie7, is_ie8 = false;
var ie  = (agt.indexOf("msie") != -1);
var ff  = (agt.indexOf("firefox") != -1);
var op = (agt.indexOf("opera") != -1);

var i = 0;
if(ie){
	var IEcomp = null;
	if (document.documentMode) {
		IEcomp = document.documentMode;
	}else {
		IEcomp = 5;
		if (document.compatMode) {
			IEcomp = 6;
			if( agt.indexOf("msie 7.")!= -1) IEcomp = 7;
		}
	}
	if(null != IEcomp){
		is_ie6 = (agt.indexOf("msie 6.")!= -1); /* ie6 */
		is_ie7 = (agt.indexOf("msie 7.")!= -1); /* ie7 */
		is_ie8 = (IEcomp==8); /* ie8 */
		is_ie8k7 = (!is_ie7) && (IEcomp == 7); /* ie8 im 7er Kompatibilitaetsmodus */
	}
}


var check = function(){ alert("JS + jQuery eingebunden"); }


/* + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + */
/**
* insert print link 
*/
var write_printlink = function(){
	var linktxt = 'Seite drucken'; var linktitle = ' Den Inhalt dieser Seite ausdrucken ';
	$('p.pagefooter').append('<span class="divider"> | </span><span class="bgimg"><a class="htmlprint" title="'+ linktitle +'" href="javascript:window.print()">'+ linktxt +'</a></span>');
}


/* + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + */
/**
* executes functions when the page loads.
* @param func_name name of function
*/
var addLoadEvent = function(func_name){
	var lastonload = window.onload;

	if (typeof window.onload != 'function') {
		window.onload = func_name;
	} else {
		window.onload = function() {
			lastonload(); func_name();
		}
	}
}

/* + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + */
/* topteasercontent behaviour */
if ('undefined' == typeof(tOptions)) { tOptions = new Object(); }
tOptions = {
	'cnum' : 0,
	'ncount' : 0,
	'maxTeaser':12,
	'defaulthead' : 'aktuelle Nachricht',
	'imagesize': 'width="645" height="343"',
	'fid': 'teaserfocus',
	'blindimg': '/fileadmin/layout/images/structure/blind.gif',
	'icon_browser_default_src': '/fileadmin/layout/images/structure/icon_round.gif',
	'icon_browser_active_src': '/fileadmin/layout/images/structure/icon_round_active.gif',
	'icon_browser_arrow_prev': '/fileadmin/layout/images/structure/icon_prev.gif',
	'icon_browser_arrow_next': '/fileadmin/layout/images/structure/icon_next.gif',
	'timerIsOn' : false
};/* tOptions */
//	'imageclass': 'image',
//	'teasersize': 'width="238"',
//	'teaserclass': 'teaser',

var cimgs = new Array(); 
var tteaser = (".start .main .eb1 .box:not(.browse)");
//var tteaser = (".start #main .area1 #content .box #teasercontent");

var counter = 0;

/* load events for topteasercontent behaviour */
var initiate_topteasercontent = function() {
	if(document.getElementById('jsbrowser') && $('div.wrapper').hasClass('start')) {
		try{
			if(topteaser && topteaser.count) {
				//if(parseInt(topteaser.count) > tOptions.maxTeaser) counter = tOptions.maxTeaser; else counter = parseInt(topteaser.count);
				counter = parseInt(topteaser.count);
				if(topteaser.count > 1){
					write_focuslink(tOptions.fid);
					tOptions.ncount = topteaser.count-1;
					$.extend(true, tOptions, topteaser);
					$(tteaser).attr('aria-live', 'polite');
					build_sliderlist();
					//next_teaserlink();
					//prev_teaserlink();
				}else if(topteaser.count == 1) {
					$.extend(true, tOptions, topteaser);
				}
			}
		}catch(err){
			//alert(err);
		}
	}
}; /* initiate_topteasercontent */

var build_sliderlist = function(){
	var html = '';
	if(typeof($('#teasercontent .items') != undefined)){
		for( var i = 0; i < topteaser.count; i++ ){
			html += '<div class="item">';
			html += '<div class="teaserimg"><img width="615" height="343" alt="'+topteaser[i].img_alt+'" src="'+topteaser[i].img_src+'" /></div>';
			html += '<div class="inner">'+topteaser[i].tlink;
			html += topteaser[i].ttext;
			html += '</div>';
			html += '</div>';
		}
		$('#teasercontent .items').append($(html));		
	}
}

/* ersetzt die Pfeile mit dem Mini Teaser Browser*/
var showMiniTeaserBrowser = function(){	

	var browseContainer = $('.start .main .eb1 .box.browse');
	var html = '<div>';
	var count = 0;
	if('undefined' != typeof(topteaser)&& topteaser != null && topteaser.count)
		count = parseInt(topteaser.count);
	if( browseContainer.length > 0 ){
		browseContainer.html('');

		if(count > 1){
			if(tOptions.cnum == 0)
				var prevTeaser = counter;
			else
				var prevTeaser = tOptions.cnum;

			if(tOptions.cnum == counter-1)
				var nextTeaser = 1;
			else
				var nextTeaser = tOptions.cnum+2;

			html += '<h2 class="out">Bl&auml;tterfunktion</h2>';
			html += '<a href="#" class="prev noprint"><span class="noborder"><img height="31" width="31" src="'+tOptions.icon_browser_arrow_prev+'" alt="vorheriges Thema" /></span>';
			html += '<span class="out"> '+(prevTeaser)+' anzeigen</span>';
			html += '</a>';
			html += '<span class="out"> - </span>';
			for( var i = 0; i < counter; i++ ){
				var page = i + 1;
				if(tOptions.cnum == i)
					html += '<strong class="current"><span class="out">aktuelles Thema: </span>'+ page +' / '+ counter +'</strong>';
			}
			html += '<span class="out"> - </span>';
			html += '<a href="#" class="next noprint"><span class="noborder"><img height="31" width="31" src="'+tOptions.icon_browser_arrow_next+'" alt="n&auml;chstes Thema" /></span>';
			html += '<span class="out"> '+(nextTeaser)+' anzeigen</span>';
			html += '</a>';
		}
		html += '</div><div class="clearer"><hr class="structure" /></div>';
	}
	browseContainer.html(html);
	//registerOnClickEventsTeaserLinks();
}

var updateBrowser = function(){
	var count = 0;
	if('undefined' != typeof(topteaser)&& topteaser != null && topteaser.count){
		count = parseInt(topteaser.count);	
		if(tOptions.cnum == 0)
			var prevTeaser = counter;
		else
			var prevTeaser = tOptions.cnum;

		if(tOptions.cnum == counter-1)
			var nextTeaser = 1;
		else
			var nextTeaser = tOptions.cnum+2;
		
		$('.browse strong.current').html('<span class="out">aktuelles Thema:</span>'+(tOptions.cnum+1)+' / '+parseInt(topteaser.count));
		$('a.next span.out').html(nextTeaser+' anzeigen');
		$('a.prev span.out').html(prevTeaser+' anzeigen');
	}
}

var startjQuerySlider = function(){
	$("#teasercontent").scrollable({
		next: 'a.next',
		prev: 'a.prev',
		circular: true,
		onSeek: function(){
			tOptions.cnum = this.getIndex();			
			updateBrowser();
		}
	});
}

var t;
var firstTimeout;

/* click Event auf Pfeile und Puenktchen der Teaser Mininavigation */
var registerOnClickEventsTeaserLinks = function(){
	var prev = $('a.prev');
	var next = $('a.next');
	
	prev.click(function(e){
		if(firstTimeout != null) clearTimeout(firstTimeout);
		if(t != null && 'undefined' != typeof(t)){
			clearTimeout(t);
			tOptions.timerIsOn = false;
		}
		$('#teasercontent').fadeOut(1000, function() {
			loadPrevTeaser();
			$('#teasercontent').fadeIn(1000);
		});
		set_newfocus(tOptions.fid);
		return false;
	});

	next.click(function(e){
		if(firstTimeout != null) clearTimeout(firstTimeout);
		if(t != null && 'undefined' != typeof(t)){
			clearTimeout(t);
			tOptions.timerIsOn = false;
		}		
		$('#teasercontent').fadeOut(1000, function() {
			loadNextTeaser();
			$('#teasercontent').fadeIn(1000);
		});
		set_newfocus(tOptions.fid);
		return false;
	});
}

/**
 * click auf kleinen linken Pfeil in der Teaser-Navigation
 * */
var loadPrevTeaser = function(){
	if( tOptions.cnum == 0 ){
		loadTeaser(counter-1);
	} else { 
		loadTeaser(tOptions.cnum - 1);
	}
	showMiniTeaserBrowser();
}

/**
 * click auf kleinen rechten Pfeil in der Teaser-Navigation
 * */
var loadNextTeaser = function(){
	if(tOptions.cnum == counter-1){
		loadTeaser(0);
	} else {
		loadTeaser(tOptions.cnum + 1);
	}
	showMiniTeaserBrowser();
}

var startSlider = function(){	
	if('undefined' != typeof(topteaser) && topteaser != null && (topteaser.count > 1)){
		if(!tOptions.timerIsOn){
			tOptions.timerIsOn = true;
			firstTimeout = setTimeout(timeOut, 5000);
		}
	}
}

var timeOut = function(){
	if(firstTimeout != null) clearTimeout(firstTimeout);
	$('#teasercontent').fadeOut(1000, function() {
		loadNextTeaser();
		$('#teasercontent').fadeIn(1000);
	});
	t = setTimeout(timeOut,10000);
}

/* load next teaser content */
var loadTeaser = function(num){
	tOptions.cnum = num;
	switch_teaserimg(num);
	switch_teasertext(num);
}; /* loadTeaser */

/* write next teaser img */
var switch_teaserimg = function(key){
	if(tOptions[key] && tOptions[key].img_src && tOptions[key].img_alt && tOptions.imagesize /*&& tOptions[key].ttype && tOptions.teasersize && tOptions.teaserclass && tOptions.imageclass*/ ){
		var sizeattr = tOptions.imagesize;
		$('#teasercontent .teaserimg img:not(.bimg)').replaceWith('<img src="'+tOptions[key].img_src+'" alt="'+tOptions[key].img_alt+'" '+sizeattr+' />');
	}
}; /* switch_teaserimg */


/* write next teaser headline and text */
var switch_teasertext = function(key){
	$('#teasercontent .inner').empty();
	if(tOptions[key]){
	if(tOptions[key].tlink){
		$('#teasercontent .inner').append(tOptions[key].tlink);
		//$('#teasercontent .inner > h3, #teasercontent .inner > h4, #teasercontent .inner > h5, #teasercontent .inner > h6, #teasercontent .inner > p,#teasercontent .inner > ul,#teasercontent .inner > ol').remove();
	} else {
		$('#teasercontent .inner').append('<h2 class="out">'+tOptions.defaulthead+'</h2>');
	}
	if(tOptions[key].ttext)
		$('#teasercontent .inner h2').after(tOptions[key].ttext);
	}
}; /* switch_teaserimg */

/* set focus to teaserheadline */
var set_newfocus = function(fid){
	var focusobj = document.getElementById(fid);
	if(focusobj) focusobj.focus();
}; /* set_newFocus */

/* set focus to teaserheadline */
var write_focuslink = function(fid){
	$(tteaser).prepend('<a href="#content" class="'+fid+'" name="'+fid+'" id="'+fid+'">zur Nachricht</a>');
}; /* write_focuslink */


/* Backlink auf der Detailseite von tt_products. Wichtig, wenn der Benutzer ueber die Produktsuche auf eine Detailseite kommt. */
/*
var init_tt_products_backlink = function(){
	var backlink = $('.tt-products .backlink p a');
	var backPID = get_params('tt_products[backPID]');
	
	if(typeof(backlink) != 'undefined' && typeof(backPID) != 'undefined'){
		backlink.click(function(){			
			history.back();
			return false;
		});
	}	
}

var get_params = function(value){
	var HTTP_GET_VARS = new Array();
	var strGET = document.location.search.substr(1,document.location.search.length);
	if(strGET!=''){
		gArr = strGET.split('&');
	    for(i=0; i<gArr.length; ++i){
	    	var v = '';
	    	var vArr = gArr[i].split('=');
	        if(vArr.length>1){
	        	v = vArr[1];
	        }
	        if(unescape(vArr[0]) == value) 
	        	return v;
	        else 
	        	return  'undefined';
	    }   
	}	
}*/	

/* + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + */
/* loaded events */
addLoadEvent(write_printlink);
addLoadEvent(initiate_topteasercontent);
addLoadEvent(showMiniTeaserBrowser);
//addLoadEvent(startSlider);
addLoadEvent(startjQuerySlider);
addLoadEvent(init_tt_products_backlink);

})(jQuery);


