/**
 * ausgelagerter javascript code vom Rahmentemplate
 * User: steglich
 */




/**
 * just in case a4p is not defined
 */
if(typeof window.a4p !== 'object') {
	window.a4p = {};
}




(function(window, a4p, undefined){

	/**
	 * if there are Links with a class fancyboxBild and an image inside and a reference to a big
	 * image, then the function enables opening the bigger image in a fancybox
	 *
	 * if there are tags with a class fancyboxGalerie with links inside, which have images inside
	 * and which have a reference to the big images and are combined via the rel attribute,
	 * then the function enables opening the bigger images in a fancybox slide show
	 */
	a4p.enableFancyboxImage = function() {

		var formatTitleSingleImage = function(title) {
			return getTitle(title, '');
		};

		var fancyOptions = {
				'titlePosition': 'inside',
				'showCloseButton': false,
				'titleFormat': formatTitleSingleImage,
				'overlayOpacity': 0.7,
				'overlayColor': '#000',
				'autoScale' : false,
				'onComplete' : function() {
					$('.fancyboxPrintIcon').click(function() {
						a4p.printFancybox($('#fancybox-content').html());
					});
				}
			},
			$fancyboxGalerie = $('.fancyboxGalerie'),
			$fancyboxBild = $('.fancyboxBild'),
			heights = {};

		a4p.printFancybox = function(content) {
			var win = window.open();
			self.focus();
			win.document.open();
			win.document.write('<'+'html'+'><'+'head'+'><'+'style'+'>');
			win.document.write('body, td { font-family: Verdana; font-size: 10pt;}');
			win.document.write('<'+'/'+'style'+'><'+'/'+'head'+'><'+'body'+'>');
			win.document.write(content);
			win.document.write('<'+'/'+'body'+'><'+'/'+'html'+'>');
			win.document.close();
			win.print();
			win.close();			
		};

		var getTitle = function(title, add){
			return '<div id="fancybox-title-inside">' + 
				'<span><a href="javascript:;" onclick="$.fancybox.close();">Schlie&szlig;en ' +
				'<img src="/sys4p/fancybox/closelabel.png" /></a></span>' +
				'<span><a class="fancyboxPrintIcon" href="#">Drucken ' +
				'<img src="/sys4p/fancybox/icon-drucker.png" /></a></span>' +
				'<b>' + (title && title.length ? title : '&nbsp;' ) + '</b>' + add + '</div>';
		};

		var formatTitleGallery = function(title, currentArray, currentIndex) {
			return getTitle(title, ' Bild ' + (currentIndex + 1) + ' von ' + currentArray.length);
		};

		var wrapFancybox = function($this){
			var h = heights,
				id = $this.attr('src'),
				thisHeight = $this.height();
			h[id] = (h[id] !== undefined) ? h[id] : -1;
			if (thisHeight == h[id]) {
				var wc = 'lupeWrapper',
					browserVer = $.browser.version.slice(0,1)*1;
				$this.wrap('<span class="'+wc+'" />');
				if ($.browser.msie && (browserVer === 7 || browserVer === 8)) {
					$this.parent('.'+wc).height(thisHeight+3);
				}
				$this.after('<img src="/sys4p/lupe-bilder.png" alt="Lupe" class="lupe" />');
			} else {
				if (thisHeight > 0) {
					h[id] = thisHeight;
				}
				window.setTimeout(function(){ wrapFancybox($this); }, 500);
			}
		};

		$fancyboxBild.parent('a').fancybox(fancyOptions);

		$fancyboxBild.filter('img').each(function(){
			wrapFancybox($(this));
		});

		fancyOptions.titleFormat = formatTitleGallery;

		$fancyboxGalerie.wrap('<div class="fancyboxWrapper" />');
		$fancyboxGalerie
			.each(function(){
				var $this = $(this);
				$this.css('width', $this.find('a').length * 160);
			})
			.find('a')
				.attr('rel', 'grp1')
				.fancybox(fancyOptions);
	};




	/**
	 * if there is a Link with class fancyWeb4pVideoLink with the reference to the Web4P Video
	 * then this function enables opening the video in a fancybox
	 */
	a4p.enableWeb4pVideoLink = function() {

		$('.fancyWeb4pVideoLink').fancybox({
			'width' : 794,
			'height' : 660,
			'margin' : 0,
			'padding' : 0,
				'overlayColor': '#000',
			'overlayOpacity': 0.7,
			'scrolling': 'no',
			'showNavArrows': false,
			'titleShow': false,
			'type': 'iframe'
		});
	};





	/**
	 * if there are Randboxen onthe page, then they will be enabled with rancomized displaying
	 * limited by the variables randBoxPrio1 to randBoxPrio3
	 */
	a4p.enableRandboxControlling = function() {

		function outputRandBox(prio, box) {
			var imageHtmlElement, img;
			if (box.text !== undefined) {
				imageHtmlElement = box.text;
			} else {
				img = '<img src="' + box.image + '" alt="' + box.title + '" />';
				if (box.link !== '') {
					imageHtmlElement =
							'<a href="' + box.link + '" title="' + box.title + '" target= "' + box.target + '">'
									+ img + '</a>';
				} else {
					imageHtmlElement = img;
				}
			}
			$('<div class="randBoxPrio' + prio + '" />')
					.prependTo('.contentSideBoxes')
					.append(imageHtmlElement);
		}

		function outputRandBoxen(boxen, max, prio) {
			var prioCount = boxen.length,
					out = [],
					thisBox;
			max = (boxen.length < max)
					? boxen.length
					: max;
			do {
				thisBox = boxen[Math.floor(Math.random() * prioCount)];
				if ($.inArray(thisBox, out) === -1) {
					out.push(thisBox);
					outputRandBox(prio, thisBox);
				}
			} while (out.length < max);
			return out;
		}

		if (typeof randBoxPrio3 === 'object' && randBoxPrio3.length > 0) {
			maxOutputRandBoxPrio3 = (maxOutputRandBoxPrio3 !== undefined)
				? maxOutputRandBoxPrio3
				: 1;
			var outputPrio3 = outputRandBoxen(randBoxPrio3, maxOutputRandBoxPrio3, 3);
		}
		if (typeof randBoxPrio2 === 'object' && randBoxPrio2.length > 0) {
			maxOutputRandBoxPrio2 = (maxOutputRandBoxPrio2 !== undefined)
			? maxOutputRandBoxPrio2
			: 1;
			var outputPrio2 = outputRandBoxen(randBoxPrio2, maxOutputRandBoxPrio2, 2);
		}
		if (typeof randBoxPrio1 === 'object' && randBoxPrio1.length > 0) {
			var thisRandBoxPrio1 = randBoxPrio1[Math.floor(Math.random() * randBoxPrio1.length)];
			outputRandBox(1, thisRandBoxPrio1);
		}

		/* Randboxen define height, if content is smaller than Randboxen */
		$('#content').css('minHeight', $('.contentSideBoxes').height() + 'px');
	};




	/**
	 * if there are links with a data-et-method attribute with javascript-code inside
	 * this javascript-code will be executed
	 */
	a4p.enableEtrackerEventLinks = function() {

		var code = window.magicLinkCallCode,
			links = code.split("<BR>"),
			zeile,
			$selector,
			i = 0;
		for (; i<links.length; i++){
			zeile = links[i].split(";");
			$selector = $('a[href$="' + zeile[0] +'"]');
			$selector.data('et-method',zeile[1]);
			$selector.live('click', function(){
				var eventFkt = $(this).data('et-method');
				var f = new Function(eventFkt);
				f.call(window);
			});
		}
	};




	/**
	 * enables the asynchronous etracking dependent on loades cid (with timeout)
	 */
	a4p.enableEtrackerAsync = function() {

		$(document).bind('cidChange', function(){
			var et = document.createElement('script');
			et.src = 'http'+('https:'==document.location.protocol?'s':'')
				+'://code.etracker.com/a.js?et=EEgcrK';
			var head = document.getElementsByTagName('head')[0];
			head.insertBefore(et, head.firstChild);
		});
		var etTonrTempCounter = 0,
			etTonrTempHandler = function(){
				if(typeof et_tonr_temp !== 'undefined' || etTonrTempCounter > 10){
					$(document).trigger('cidChange');
				} else {
					etTonrTempCounter++;
					window.setTimeout(etTonrTempHandler, 200);
				}
			},
			etScriptLoadedHandler = function() {
				var $el = $('[href*="www.etracker.de/app"]');
				if($el.length > 0) {
					$el.css({position: 'absolute', top: 0});
				} else {
					window.setTimeout(etScriptLoadedHandler, 200);
				}
			};

		et_params = function() {

			et_areas        = (typeof et_areas_temp    !== 'undefined') ? et_areas_temp     : '';
			et_ilevel       = (typeof et_ilevel_temp   !== 'undefined') ? et_ilevel_temp    : 0;
			et_lpage        = (typeof et_lpage_temp    !== 'undefined') ? et_lpage_temp     : '';
			et_pagename     = (typeof et_pagename_temp !== 'undefined') ? et_pagename_temp  : undefined;
			et_sub          = (typeof et_sub_temp      !== 'undefined') ? et_sub_temp       : '';
			et_tag          = (typeof et_tag_temp      !== 'undefined') ? et_tag_temp       : '';
			et_target       = (typeof et_target_temp   !== 'undefined') ? et_target_temp    : '';
			et_trig         = (typeof et_trig_temp     !== 'undefined') ? et_trig_temp      : '';
			et_tsale        = (typeof et_tsale_temp    !== 'undefined') ? et_tsale_temp     : 0;
			et_tval         = (typeof et_tval_temp     !== 'undefined') ? et_tval_temp      : '';
			et_url          = (typeof et_url_temp      !== 'undefined') ? et_url_temp       : '';

			et_se           = 1;

			if(et_tval !== '') {
				et_tonr = (typeof et_tonr_temp !== 'undefined') ? et_tonr_temp : '';
			} else {
				et_tonr = '';
			}
		};
		
		etTonrTempHandler();
		// etracker-image ausblenden / nach oben schieben
		etScriptLoadedHandler();
	};
}(this, this.a4p));
	


(function(a4p, events, undefined) {

	var jqueryPub = function() {
		events.pub(events.channels.jqueryLoaded, a4p.jqueryVersion);
	};

	events.sub(
		events.channels.jqueryLoaded,
		function() {

			if(a4p.randboxControlling === true) {
				a4p.enableRandboxControlling();
			}

			if(a4p.etrackerEventLinks === true) {
				a4p.enableEtrackerEventLinks();
			}

			if(a4p.etrackerAsync === true) {
				a4p.enableEtrackerAsync();
			}

			$.extend({
				getUrlVars: function(){
								var vars = [], hash;
								var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
								for(var i = 0; i < hashes.length; i++) {
									hash = hashes[i].split('=');
									vars.push(hash[0]);
									vars[hash[0]] = hash[1];
								}
								return vars;
							},
				getUrlVar: function(name){
							   return $.getUrlVars()[name];
						   }
			});

			yepnope([
				{
					test 		: a4p.jqueryUi, 
					yep 		: [
									'/sys4p/jquery-ui-1.8.16/js/jquery-ui-1.8.16.custom.min.js',
									'/sys4p/jquery-ui-1.8.16/css/custom-theme/jquery-ui-1.8.16.custom.css'
									],
					callback	: function() {
						events.pub(events.channels.jqueryUILoaded);
					}
				},
				{
					test		: a4p.fancyboxImage || a4p.web4pVideo, 
					yep			: [
									'/sys4p/fancybox/jquery.fancybox-1.3.4.pack.js',
									'/sys4p/fancybox/jquery.fancybox-1.3.4.css'
									],
					callback	: function() {
						if(a4p.fancyboxImage) {
							a4p.enableFancyboxImage();
						} 
						if(a4p.web4pVideo) {
							a4p.enableWeb4pVideoLink();
						}
						events.pub(events.channels.fancyboxLoaded);
					}
				},
				{
					test		: a4p.jqueryValidate,
					yep			: '/sys4p/jquery.validate.min.js',
					callback	: function() {
						events.pub(events.channels.jqueryValidateLoaded);
					}
				},
				{
					test		: a4p.jquerySticky,
					yep			: [
									'/sys4p/jquery-contained-sticky-scroll.js',
									'/sys4p/jquery-easing-1.3.js'
									],
					callback	: function() {
						events.pub(events.channels.jqueryStickyLoaded);
					}
				},
				{
					test		: a4p.jqueryEasing,
					yep			: '/sys4p/jquery-easing-1.3.js',
					callback	: function() {
						events.pub(events.channels.jqueryEasingLoaded);
					}
				}
			]);
			
		}
	);


	/**
	 * loads the framework, which is needed for the activated function in a4p
	 */
	yepnope([
		{
			test		: a4p.referrer,
			yep			: '/flash/swfobject.js',
			callback	: function() {
				load_info();
			}
		},
		{
			test		: (a4p.jqueryVersion === '1.7.1'),
			yep			: '/sys4p/jquery-1.7.1.min.js',
			callback	: jqueryPub
		},
		{
			test		: (a4p.jqueryVersion === '1.6.4'),
			yep			: '/sys4p/jquery-1.6.4.min.js',
			callback	: jqueryPub
		}
	]);

}(a4p, a4p.events));

