/*
 * File   : $Source: /srv/cvs/LISTERINE-CMS-CH/public_html/js/default.js,v $
 * Date   : $Date: 2011/05/20 13:02:51 $
 * Version: $Revision: 1.9 $
 * Author : Carsten Wilhelm (wilhelm@bplusd.de)
 * Author : Carsten Wogatzky (wogatzky@bplusd.de)
 * Author : Thomas Weckert (weckert@bplusd.de)
 */

/*
 * Ersetzt ein DIV durch ein Flash, das den gleichen Text (ohne HTML-Tags)
 * anzeigt.
 * Die �bergebene H�he sollte auf eine einzeilige Headline gemessen werden,
 * da das JavaScript die H�he mal der Anzahl der gefundenen Zeilen
 * multipliziert (um ein "Zucken" auf der Website zu vermindern).
 */
function replaceHeadline(flash,id,width,height,color,bgcolor,fontface,size,link,arrow,padding) {
	var div = document.getElementById(id);
	if (div) {
		// Text der Headline suchen
		var lines = 1;
		var text = div.innerText ? div.innerText : div.textContent;
		if (text) {
			// Whitespaces am Anfang und Ende entfernen
			text = text.replace(/^\s+/, "");
			text = text.replace(/\s+$/, "");
			// &-Zeichen ersetzen
			text = text.replace(/&/g, "%26");
			// Newlines vereinfachen
			text = text.replace(/[\r\n]+/g, "\n");
			// Anzahl Zeilen z�hlen
			lines = text.split("\n").length;
		}

		// Link der Headline suchen
		var link;
		var atags = getElementsByTagName(div, "a");
		if (atags && atags.length > 0) {
			link = atags[0].href;
		}

		// Flash einsetzen
		var so = new SWFObject(flash, id, width, lines * height, "8", "#fff");
		so.addVariable("text", text);
		so.addVariable("colorhex", color);
		so.addVariable("fontsize", size);
		so.addVariable("fontface", fontface);
		so.addVariable("padding_left", padding);
		so.addVariable("bgcolor", bgcolor);
		so.addVariable("link", link);
		so.addVariable("arrow", arrow);
		so.addParam("wmode", "transparent");
		so.write(id);

		// Gefundened DIV zur�ckliefern
		return div;
	}
}

/*
 * Liefert alle Tags eines bestimmten Namens unterhalb des
 * �bergebenen Root-Knotens
 */
function getElementsByTagName(root, tagname) {
	// Neue Liste f�r zu bearbeitende Knoten
	var todo = new Array(root);
	// Neue Liste f�r gefundene Tags
	var found = new Array();
	// Tagnamen auf Gro�buchstaben bringen
	tagname = tagname.toUpperCase();

	// Liste abarbeiten
	while(todo.length > 0) {
		var tag = todo.pop();
		if (tag.nodeName.toUpperCase() == tagname) {
			found.push(tag);
		}
		if (tag.childNodes) {
			for(var i=tag.childNodes.length-1 ; i>=0 ; --i) {
				todo.push(tag.childNodes[i]);
			}
		}
	}

	return found;
}



/*
 * Skript zum �ndern der Schriftgr��en im Dokument
 *
 * Size1:   10 / 11 / 12 / 15 / 16 / 28 px
 * Size2:   12 / 13 / 14 / 18 / 20 / 32 px
 * Size3:   14 / 16 / 18 / 20 / 22 / 40 px
 */
var fontSize = 1;
var fontMatrix = {
	1: {
		"10px": "12px",
		"11px": "13px",
		"12px": "14px",
		"13px": "15px",
		"14px": "16px",
		"15px": "18px",
		"16px": "20px",
		"28px": "32px"
	},
	2: {
		"12px": "14px",
		"13px": "16px",
		"14px": "18px",
		"15px": "19px",
		"16px": "20px",
		"18px": "22px",
		"20px": "24px",
		"32px": "40px"
	},
	3: {
		"14px": "10px",
		"16px": "11px",
		"18px": "12px",
		"19px": "13px",
		"20px": "14px",
		"22px": "15px",
		"24px": "16px",
		"40px": "28px"
	}
};
function applyTextSize() {
	var matrix = fontMatrix[fontSize];

	var theRules;
	if (document.styleSheets.length > 0) {
		if (document.styleSheets[0].cssRules) {
			theRules = document.styleSheets[0].cssRules;
		} else if (document.styleSheets[0].rules) {
			theRules = document.styleSheets[0].rules;
		}
		for(var i=0 ; i<theRules.length ; ++i) {
			if (theRules[i].style.fontSize) {
				var mySize = theRules[i].style.fontSize;
				var newSize = matrix[mySize];
				if (newSize) {
					theRules[i].style.fontSize = newSize;
				} else {
					// alertet immer
					//alert("Testing: Schriftart unbekannt - "+mySize+" ("+fontSize+")");
				}
			}
		}
		$('*[style]').each(function(i,e){
			var mySize = e.style.fontSize;
			if (mySize) {
				var newSize = matrix[mySize];
				if (newSize) {
					e.style.fontSize = newSize;
				} else {
					// alertet immer
					//alert("Testing: Schriftart unbekannt - "+mySize+" ("+fontSize+")");
				}
			}
		});
	}
}
function resizeText() {
	// Anwenden
	applyTextSize();
	// Schriftgröße erhöhen
	fontSize = (fontSize == 3) ? 1 : fontSize+1;
}



// Prüft das eingegebene Geburtsjahr
function checkGebJahr(id,cname) {


	var tooyoung = $.cookie(cname);
	if (tooyoung == 1) {
		alert("Bitte beachten Sie, dass das Mindestalter 18 Jahre beträgt.");
		return false;
	}

	var now = new Date();
	var val = $("#"+id).val();

	if (val != null && val != '') {

		if (val >= now.getFullYear() - 17) {
			$.cookie(cname, '1', { path: '/' });

			alert("Bitte beachten Sie, dass das Mindestalter 18 Jahre beträgt.");
			return false;
		}

		return true;
	} else {
		return true;
	}

	return false;
}

// Video-Layer öffnen
function openVideoLayer() {

	var layer = $('#videolayer');
	if(layer.length > 0) {
		layer.modal({
			overlayCss: {backgroundColor:'#000'},
			containerCss: {width:'470px'},
			closeHTML: '<img style="position:absolute; top:5px; right:5px; cursor:pointer" src="/img/close.png"/>'
		});

		/*
		var style ="height: 100%; outline: 0px none; width: 100%;";
		$('.simplemodal-wrap').attr('style', style);
		*/
	}
}

// Beim Aufruf einer Seite ein Video im Layer aufrufen
$(document).ready(function() {
	if (document.location.hash == '#video') {
		openVideoLayer();
	}
});

