/**
 *	Bildwurf Kinowerbung Uebersicht
 *
 *	Javascript
 *
 */

var supported = (document.getElementById || document.all);


if (supported) {
	var winwidth	= getWindowWidth();
	var winheight	= getWindowHeight();
	
	if (winwidth < 990) {
		winwidth	= 990;
	}
	
	var rest = Math.round((winwidth - 540 - 20)/2);
	var links = 540 + rest;
	// a - oben
	// var xa_max = links - 100;
	var xa_max = rest - 100;
	var xa = Math.round(Math.random() * xa_max);
	var ya = 65 + Math.round(Math.random() * 7);
	// b - rechts
	var xb_max = rest - 100;
	var xb = links + Math.round(Math.random() * xb_max);
	var yb = Math.round (Math.random() * (winheight - 100));
	// 
	var a = Math.round(Math.random() * 6);
	if (a <= 3) {
		// oben
		var x1 = xa;
		var y1 = ya;
		// rechts
		var x2 = xb;
		var y2 = yb;
	} else {
		// rechts
		var x1 = xb;
		var y1 = yb;
		// oben
		var x2 = xa;
		var y2 = ya;
	}
	addStyle("#minimotion { position:absolute; top:" + y1 + "px; left:" + x1 + "px; z-index:10; }",-1);
	addStyle("#ambassador { position:absolute; top:" + y2 + "px; left:" + x2 + "px; z-index:10; }",-1);
}

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - //

function getWindowHeight() {
	var windowHeight = 0;
	if (typeof(window.innerHeight) == 'number') {
		windowHeight = window.innerHeight;
	} else {
		if (document.documentElement && document.documentElement.clientHeight) {
			windowHeight = document.documentElement.clientHeight;
		} else {
			if (document.body && document.body.clientHeight) {
				windowHeight = document.body.clientHeight;
			}
		}
	}
	return windowHeight;
}

function getWindowWidth() {
	var windowWidth = 0;
	if (typeof(window.innerWidth) == 'number') {
		windowWidth = window.innerWidth;
	} else {
		if (document.documentElement && document.documentElement.clientWidth) {
			windowWidth = document.documentElement.clientWidth;
		} else {
			if (document.body && document.body.clientWidth) {
				windowWidth = document.body.clientWidth;
			}
		}
	}
	return windowWidth;
}

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - //

/* 'Stylesheet/Regeln hinzufuegen' (c) cybaer@binon.net - http://Coding.binon.net/AddStyle */
/* Lizenz CC <http://creativecommons.org/licenses/by-nc-sa/2.5/> */
function addStyle(rules,target) {
 var styleObj=null, styleSheetObj=null, i, j, p, selector, singleSelector, text;
 if(document.createElement && document.getElementsByTagName) {
  if(typeof(target)=="number") {
   if(target<=-1) { target=document.getElementsByTagName("style").length+Math.ceil(target); }
   target=Math.max(0,Math.min(document.getElementsByTagName("style").length-1,Math.floor(target)));
  }
  if(typeof(target)=="undefined" || typeof(target)=="string" || !document.getElementsByTagName("style")[target]) {
   if(document.createStyleSheet) {
    styleSheetObj=document.createStyleSheet();
    styleObj=styleSheetObj.owningElement || styleSheetObj.ownerNode;
   } else {
    styleObj=document.createElement("style");
    document.getElementsByTagName("head")[0].appendChild(styleObj);
   }
   styleObj.setAttribute("type","text/css");
   if(target) { styleObj.setAttribute("media",target); }
  } else if(typeof(target)=="number") {
   styleObj=document.getElementsByTagName("style")[target];
   styleSheetObj=styleObj.sheet || styleObj.styleSheet;
  }
  if(styleObj && rules) {
   /*@cc_on
   @if(@_jscript)
    rule=rules.replace(/\s+/g," ").replace(/\/\*.+?\*\//g,"").split("}");
    for(i=0;i<rule.length;i++) {
     p=rule[i].indexOf("{");
     selector=rule[i].substring(0,p).replace(/^\s+|\s+$/g,"");
     text=rule[i].substring(p+1);
     if(selector) {
      if(selector.indexOf(",")) {
       singleSelector=selector.split(",");
      } else {
       singleSelector=new Array(selector);
      }
      for(j=0;j<singleSelector.length;j++) { styleSheetObj.addRule(singleSelector[j].replace(/^\s+|\s+$/g,""),text.replace(/^\s+|\s+$/g,"")); }
     }
    }
   @else @*/
    if(styleObj.firstChild) { styleObj.firstChild.nodeValue=styleObj.firstChild.nodeValue.replace("<!--",""); }
    if(styleObj.lastChild) { styleObj.lastChild.nodeValue=styleObj.lastChild.nodeValue.replace("-->",""); }
    styleObj.appendChild(document.createTextNode(rules+"\n"));
   /*@end @*/
  }
 }
 return styleObj;
}

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - //
// grazie peterradelfinger[dotcom]com

var currentMaxZIndex = 10 + 1;	// anzahl elemente+1
function bringInFront(element) {
	//alert(element.style.zIndex);
	if (element.style.zIndex != currentMaxZIndex) {
		currentMaxZIndex = parseInt(currentMaxZIndex) + 1;
		element.style.zIndex = currentMaxZIndex;
	}
	
}

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - //

window.addEvent('domready', function() {
	// hover
	$$('a').each(function(el, i){
		if (el.hasClass('schwarz')) {
			el.setStyle('color', '#000');
			if (el.rel!='') {
				el.addEvent('mouseenter', function() {
					$(el.rel).setStyle('color', '#000');
				});
				el.addEvent('mouseleave', function() {
					$(el.rel).setStyle('color', '');
				});
			}
		}
	});
	if ($('email')) {
		var txt = $('email');
		$('email').addEvents({
			'focus': function() {
				if (txt.value.contains('E-Mail') || txt.value.contains('???')) {
					txt.value = '';
					txt.setStyle('color', '');
				}
			},
			'blur': function() {
				if (txt.value=='') {
					txt.value = 'E-Mail';
					txt.setStyle('color', '');
				}
			}
		});
	}
});

function Newsletter() {
	var txt = $('email');
	if (txt.value=='' || txt.value=='E-Mail' || txt.value=='bitte E-Mail eintragen') {
		txt.value = 'bitte E-Mail eintragen';
		txt.setStyle('color', '#FF0000');
	} else {
		var emailExp = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;
		var val = txt.value;
		if (val.match(emailExp)) {
			document.Newsletter.submit();
			txt.setStyle('color', '');
		} else {
			txt.value	= (txt.value.contains('???'))
						? txt.value
						: txt.value + ' ???';
			txt.setStyle('color', '#FF0000');
		}
	}
}

