// browser sniffer
var Browser = function(){
  this.uA = navigator.userAgent.toLowerCase();
  this.aN = navigator.appName.toLowerCase();
  this.iE = this.aN.indexOf('microsoft') != -1 ? 1 : 0;
  this.mac =  this.uA.indexOf('mac') != -1 ? 1 : 0;
  this.win = this.uA.indexOf('windows') != -1 ? 1 : 0;
  this.safari =  this.uA.indexOf('webkit') != -1 ? 1 : 0;
  this.opera =  this.uA.indexOf('opera') != -1 ? 1 : 0;   
  this.operaMini =  this.uA.indexOf('mini') != -1 ? 1 : 0;  
  this.mozilla = this.aN.indexOf('netscape') != -1 && !this.safari ? 1 : 0;
  this.winMozilla = this.mozilla && this.win ? 1 : 0;
  this.winIE = this.iE && this.win && !this.opera ? 1 : 0;
  this.winIE6Down = this.winIE && parseInt(this.uA.split('msie ')[1].substring(0,1)) <= 6 ? 1: 0;
  this.macIE = this.iE && this.mac ? 1 : 0;
};
var browser = new Browser();
	
//incl. .js
var jsPath = "/js";
document.write('<scr'+'ipt type="text/javascript" src="' + jsPath + '/jquery.tabs/jquery.tabs.min.js"><\/scr'+'ipt>');
document.write('<scr'+'ipt type="text/javascript" src="' + jsPath + '/jquery.shuffle.js"><\/scr'+'ipt>');

// addStyle
var addStyle = function(selector,properties){
  if (document.styleSheets) {
    var s = document.getElementsByTagName('STYLE');
    if (s.length == 0){
      var sheet = document.createElement('style');
      sheet.setAttribute('type','text/css');
      document.getElementsByTagName('HEAD')[0].appendChild(sheet);}
    if (browser.winIE){
      var lastSheet = document.styleSheets[document.styleSheets.length - 1];
      lastSheet.addRule(selector, properties);}
    else {var lastSheet = s[0];
      lastSheet.appendChild(document.createTextNode(selector + ' { ' + properties + ' }'));}
  	}
};
addStyle('.drempel','display:none;');
addStyle('.noscript','display: block !important;');
addStyle('.visible_onready', 'visibility: hidden;');

/**
 * init jQuery.tabs
 * used for right-column paginator (should really be solved as RPC-driven Doctrine-paginator)
 */
$.do_paginator_tabs = function(container, prev, next){
	// prev-next are be resolved within multiple classed tabs-containers
	$(container).each(function(r){
		var t = this;
		$(t).tabs({
			// TODO eventually (..?) implement RPC-loading of tabs-paging
//			remote: true,
			fxFade: true
		});
		// activate previous-next behaviour
		if($(this).hasClass('rotator')){
			// content rotator prev-next
			$.media_rotator('.tabs-container', '.prev', '.next');
		}else{
			// straight paginator prev-next
			$(t).find(prev).click(function(evt){
				var a = $(t).activeTab();
				$(t).triggerTab(a - 1);
				evt.preventDefault();
			});

			$(t).find(next).click(function(evt){
				var a = $(t).activeTab();
				$(t).triggerTab(a + 1);
				evt.preventDefault();
			});
		}

	});
}

/*
 * init media-rotators
 */
$.media_rotator = function(container, prev, next){
	// prev-next are used to rotate the content within the tabbed content
	$(container).each(function(r){
		var t = this;
		$(t).find('.content-container').shuffle();
		var items = $(t).find('.content-item');
		if(items.length>0){
			var title_el = $(t).find('.content-nav .title'); // find title rotator
			var active = $(t).active_item = 0;
			$(items).each(function(index, el){
				if(index != active) {
					/* fodeOut() fails on tab-containers that are hidden ...
					$(el).fadeOut(0);
					*/
					// ! set the expected inline css hard instead!
					$(el).css('display', 'none');
				}
			});
			title_el.html( $(items[active]).find('.title').html() );
			
			$(t).find(prev).click(function(evt){
				var curr_active = active;
				active = (items.length+active-1)%(items.length);
				$(items[curr_active]).fadeOut('normal', function(){ 
					title_el.html( $(items[active]).find('.title').html() );
					$(items[active]).fadeIn('normal');
				});
				evt.preventDefault();
			});
			
			$(t).find(next).click(function(evt){
				var curr_active = active;
				active = (active+1)%(items.length);
				$(items[curr_active]).fadeOut('normal', function(){ 
					title_el.html( $(items[active]).find('.title').html() );
					$(items[active]).fadeIn('normal');
				});
				evt.preventDefault();
			});
		}
	});
}

//DOM loader
function init() {
//	console.log('init');
	if (arguments.callee.done) return;
	arguments.callee.done = true;

//	alert('DOM loaded');
	// write the stacked swfObjects
	var swfs = window.writePageSwfObjects;
	if(swfs && swfs.length>0){
		for(var i=0; i<swfs.length; i++){
			if (confirm('Loading swf from stack should not be used: disable window.writePageSwfObjects in the appropriate view. Do it now anyway?')){
			swfs[i]();
			}
		}
	}
	$.do_paginator_tabs('.JQtabs', '.prev', '.next');
//	console.log('do visible onready'); // still not appropriate TODO re-evaluate DOM-loading
	$('.visible_onready').css({'visibility':'visible'});
	$('input.required').closest('li').addClass('required');
	$('input.required').closest('ul.radio_list').addClass('required');
	$('ul.form-event-list li:first').addClass('required');
};
if (document.addEventListener) {
	document.addEventListener("DOMContentLoaded", init, null);
}
if (/WebKit/i.test(navigator.userAgent)) {  
var _timer = setInterval(function() {        
	if (/loaded|complete/.test(document.readyState)) {            
		clearInterval(_timer); init();}}, 10);
};
// for Internet Explorer (using conditional comments)
/*@cc_on @*/
/*@if (@_win32)document.write("<script id=__ie_onload defer src=javascript:void(0)><\/script>");
var script = document.getElementById("__ie_onload");
script.onreadystatechange = function() {    
	if (this.readyState == "complete") {        
		init();  
	}
};
/*@end @*/
// neither is necessary ... yet
//jQuery.onready = init;
//window.onload = init;