/*	JavaScript Document

	1.) Console Functions (2006-05-11, uldo)
		Revision (2006-11-06, uldo)
			- remove Fade/Appear Effect to fix Rendering Bug in FF Mac;

	2.) Voting Functions for AJAX Video Voting (2006-11-02, uldo)
		Revision (2006-11-06, uldo)
			- E-Mail Validation added

	3.) E-Mail Validation (2006-11-06, uldo)

	4.) OnLoad Functions (2006-05-11, uldo)

*/

// 1.) Console Functions

  var MediaID;
  var MediaType;
  var statusConsole = 0;
  var PageID;
  var Offset;


  var console = { 
	openConsole: function (MediaID,MediaType) {
		if (statusConsole == 0) {
			new Effect.SlideDown('c-console', {duration:0.6,
				afterFinish: function() {
					new Effect.ScrollTo('c-console', []),
					
					// Load Main Video
					new Ajax.Updater('contentConsole','/content/'+MediaType+'player.php?id='+MediaID+'&version='+actualVersion, { 
					  asynchronous: true, 
					  onSuccess: function() { 
						//new Effect.Appear('contentConsole', { duration: 0.6 }),

						// Load Channel Current
						new Ajax.Updater('AJAX-Console-Current','/content/channel_current.php?id='+MediaID, { 
						  asynchronous: true, 
						  onSuccess: function() { 
							new Effect.Appear('AJAX-Console-Current', { duration: 0.6 })
						  } 
						}),
						// Load Channel Other
						new Ajax.Updater('AJAX-Console-Other','/content/channel_other.php?id='+MediaID, { 
						  asynchronous: true, 
						  onSuccess: function() { 
							new Effect.Appear('AJAX-Console-Other', { duration: 0.6 })
						  } 
						}) 
						
					  } 
					}) 
				}
			})
		} else {
			new Effect.ScrollTo('c-console', []),
//			new Effect.Fade('contentConsole', { duration:0.6,
//				afterFinish: function(){
					
					new Ajax.Updater('contentConsole','/content/'+MediaType+'player.php?id='+MediaID+'&version='+actualVersion, { 
					  asynchronous: true, 
					  onSuccess: function() { 
							//new Effect.Appear('contentConsole', { duration: 0.6 }),
							// Load Channel Current
							new Ajax.Updater('AJAX-Console-Current','/content/channel_current.php?id='+MediaID, { asynchronous: true }),
							// Load Channel Other
							new Ajax.Updater('AJAX-Console-Other','/content/channel_other.php?id='+MediaID, { asynchronous: true }) 
					  } 
					})
					
//				}
//			})
		}
		statusConsole = 1;
	},
	closeConsole: function() { 
		statusConsole = 0;
//		new Effect.Fade('contentConsole', { duration:0.6,
//			afterFinish: function() {
				document.getElementById('contentConsole').innerHTML = "",
				new Effect.SlideUp('c-console', {duration:0.6 })
//			}
//		})
	},
	
	browseOther: function(PageID,Offset) {
		new Ajax.Updater('AJAX-Console-Other','/content/channel_other.php?id='+PageID+'&offset='+Offset, { asynchronous: true	})
	},

	browseCurrent: function(PageID,Offset) {
		new Ajax.Updater('AJAX-Console-Current','/content/channel_current.php?id='+PageID+'&offset='+Offset, { asynchronous: true	})
	}

}

// 2.) Voting Functions for AJAX Video Voting, 2006-11-02, uldo

  var VoteID;
  var VoteMail;

  var voting = { 
	startup: function() {
	},
	question: function(MediaID) {
		new Ajax.Updater('FLVcontent','/content/voting.php?id='+MediaID, { asynchronous: true })
	},
	setvote: function(VoteID) {
		document.getElementById('vote'+VoteID).blur();
		document.getElementById("vote").value = VoteID;
		for (i=0; i<6; i++) {
			Element.removeClassName('vote'+i, 'checked');
			Element.addClassName('vote'+i, 'unchecked');
		}
		Element.addClassName('vote'+VoteID, 'checked');
	},
	submitvote: function(MediaID) {
		VoteMail = document.getElementById("email").value;
		VoteID = document.getElementById("vote").value;

		emailOK = emailCheck(VoteMail);

		if (emailOK) {
			new Ajax.Updater('FLVcontent','/content/voting.php?id='+MediaID+'&vote='+VoteID+'&email='+VoteMail, { asynchronous: true,
				onSuccess: function() {
				}
			})
		}
	},
    showcontent: function(MediaID) {
		new Ajax.Updater('FLVcontent','/content/voting.php?video='+MediaID, { asynchronous: true })
	}
  }

// 3.) E-Mail Validation

function emailCheck (emailStr) {
	var checkTLD=1;
	var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;
	var emailPat=/^(.+)@(.+)$/;
	var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]";
	var validChars="\[^\\s" + specialChars + "\]";
	var quotedUser="(\"[^\"]*\")";
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
	var atom=validChars + '+';
	var word="(" + atom + "|" + quotedUser + ")";
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");
	var matchArray=emailStr.match(emailPat);
	
	// E-Mail nicht korrekt
	if (matchArray==null) {
		alert("Die E-Mail Adresse scheint nicht korrekt zu sein!");
		return false;
	}
	var user=matchArray[1];
	var domain=matchArray[2];
	
	// User ungueltige Zeichen
	for (i=0; i<user.length; i++) {
		if (user.charCodeAt(i)>127) {
			alert("Die E-Mail Adresse scheint nicht korrekt zu sein!");
			return false;
		}
	}
	// Domain ungueltige Zeichen
	for (i=0; i<domain.length; i++) {
		if (domain.charCodeAt(i)>127) {
			alert("Die Domain beinhaltet ungueltige Zeichen.");
			return false;
		}
	}
	// User inkorrekt
	if (user.match(userPat)==null) {
		alert("Die E-Mail Adresse scheint nicht korrekt zu sein!");
		return false;
	}
	
	var IPArray=domain.match(ipDomainPat);
	if (IPArray!=null) {

		// IP inkorrekt
		for (var i=1;i<=4;i++) {
			if (IPArray[i]>255) {
				alert("Ziel IP Adresse ist nicht korrekt!");
				return false;
			}
		}
		return true;
	}
	
	var atomPat=new RegExp("^" + atom + "$");
	var domArr=domain.split(".");
	var len=domArr.length;
	
	// Domain inkorrekt
	for (i=0;i<len;i++) {
		if (domArr[i].search(atomPat)==-1) {
			alert("Die angegebene Domain scheint nicht korrekt zu sein.");
			return false;
		}
	}
	
	// Top Level Domain inkorrekt
	if (checkTLD && domArr[domArr.length-1].length!=2 && 
		domArr[domArr.length-1].search(knownDomsPat)==-1) {
		alert("Die E-Mail Adresse hat eine ungueltige Top Level Domain!");
		return false;
	}
	
	// Host inkorrekt
	if (len<2) {
		alert("Die E-Mail Adresse scheint nicht korrekt zu sein!");
		return false;
	}
	return true;
}

// 4.) OnLoad Functions

	// Load Event function

	function addLoadEvent(func) {	
		var oldonload = window.onload;
		if (typeof window.onload != 'function'){
			window.onload = function() { func; };
		} else {
			window.onload = function(){
			oldonload();
			func();
			}
		}
	}
	

function FLVsmall() {
    FLVresize(330,330);
}

function FLVbig() {
    FLVresize(540,490);
}

function FLVresize(w,h) {
    myel = document.getElementById("FLVObj");
    myel.style.width = w + 'px';
    myel.style.height = h + 'px';
    myconsole = document.getElementById("Console");
    myconsole.style.height = (h+55) + 'px';
    myinner = document.getElementById("innerConsole");
    myinner.style.height = (h+30) + 'px';
    mycontent = document.getElementById("contentConsole");
    mycontent.style.height = (h+20) + 'px';
}

//  window.onload = console.startup

addLoadEvent(console.startup);

