// PRELOAD
menu = new Image();
menu.src = "/images/bg3.jpg";
menu = new Image();
menu.src = "/images/menu.png";
balk = new Image();
balk.src = "/images/adresbalk-bg.png";
balk = new Image();
balk.src = "/images/close-hover.png";


// GLOBALS 
var popup;	  
var swifff;
var moviebgfade;
var tabs;
var contact;
var nieuwsbrief;
var theatermagazine;
var mask;


window.addEvent('domready', function () {
	initmenu();								  
	tabs = new Tabs();
	zoek = new Zoek(); 
	
	contact = new Contact();
	nieuwsbrief = new Nieuwsbrief();
	theatermagazine = new Theatermagazine();
	
	moviebgfade = new Fx.Morph($('moviebg'), {duration: '10', transition: Fx.Transitions.Sine.easeInOut, link: 'chain'});
	moviebgfade.set({'opacity': 0});
	
	$('movieclose').addEvent('click', function(){
		moviebgfade.start({opacity: 0}).chain(function() {
			$('moviebg').setStyle('visibility', 'hidden')});
		$('moviewrap').setStyles({'width': '1px', 'height': '1px'});
		sendToNonverBlaster('resetAll')
		mask.hide();
		});
	
	$('movieclose').addEvent('mouseenter', function(){
		this.src = '/images/close-hover.png';
		});
	$('movieclose').addEvent('mouseleave', function(){
		this.src = '/images/close.png';
		});
	
	var myDragInstance = new Drag($('moviebg'), {handle: $('moviehandle')});
	
	mandje = new Mandje($('winkelmandje'));
	popup = new PopUp($('popup'), $('popupbody'), $('popuphandle'), $('popupclose'));
	
	mask = new Mask.Fx(document.body, {id:"mask"} );
	
	$('mask').addEvent('click', function(){
		moviebgfade.start({opacity: 0}).chain(function() {
			$('moviebg').setStyle('visibility', 'hidden')});
		$('moviewrap').setStyles({'width': '1px', 'height': '1px'});
		tabs.closeall();
		try {sendToNonverBlaster('resetAll');}
		catch(err) {};
		popup.hide();
		mask.hide();		
		});
	(function(){$('kaartje').set('html', "<img src='/images/plattegrondCuijk.gif' alt='route' width='500' height='367' />");}).delay(3000);
});


function initmenu() {
	$$('#menu li').addEvent('mouseenter', function() {
		this.getElement('.sub').setStyle('visibility', 'visible');
		this.morph({'margin-top': '0px'});
		});
	$$('#menu li').addEvent('mouseleave', function() {
		this.getElement('.sub').setStyle('visibility', 'hidden');
		this.morph({'margin-top': '7px'});
		});
	};
							 
							 
// ADRESBALKTABS

var Tabs = new Class({
					 
	acttab: null,
	//adresfade: null,
	adresfull: null,

	initialize: function() {
		
		var myFx = new Fx.Scroll(window).set (0,0);

		this.adresfade = new Fx.Morph($('adresbalk'), {link: 'cancel'});
		
		window.addEvent('scroll',function() {
			var position = window.getScroll();
			if (this.adresfull && (position.y > 300)) {
				this.adresfade.start({'opacity':0});
				this.adresfull = false;
				}
			if (!this.adresfull && (position.y <= 300)) { 
				this.adresfade.start({'opacity':1});
				this.adresfull = true;
				}
			}.bind(this));
		
		$$('.sluiten').addEvent('click', function(e) {
			this.closeall();
			}.bind(this));
		$$('.sluiten').addEvent('mouseenter', function(e) {
			this.setStyle('background-image', 'url(/images/close-hover.png)');
			});
		$$('.sluiten').addEvent('mouseleave', function(e) {
			this.setStyle('background-image', 'url(/images/close.png)');
			});
		//$('adresbalk').pin();
	},
	
	closeall: function() {
		if ($(this.acttab)) {
			//$(this.acttab).morph({'opacity': 0});
			 $(this.acttab).get('tween').start('opacity', 0).chain(function() {
				$(this.acttab).setStyle('visibility', 'hidden');
        		}.bind(this));
			mask.hide();
			}
	},
	
	opentab: function(el) {
		if ($(this.acttab)) $(this.acttab).setStyle('display', 'none');
		this.acttab = el;
		$(el).setStyles({'opacity': 1, 'visibility': 'visible'});
		$(el).setStyle('display', 'block');
		if (!Browser.ie6) mask.show();
	},
	
	openact: function() {
		if ($(this.acttab)) {
			$(this.acttab).setStyles({'display': 'block', 'visibility': 'visible'});
			$(this.acttab).setStyles({'opacity': 1});
			if (!Browser.ie6) mask.show();	
			}
	},
	
	dimact: function() {
		if ($(this.acttab)) $(this.acttab).morph({'opacity': 0.8});
	}
	
});



/* CONTACT */

var Contact = new Class({
						
	initialize: function() {
		new Request.HTML({url: '/include/contact.php' , method: 'get', update: $('contactformulier'), onComplete: function(){this.initform();}.bind(this)}).get();
	},
	
	initform: function() {
		$('contactform').addEvent('submit', function(e) { 
			new Event(e).stop();												 
			if (this.validate()) {
				$('verzenden').fade('out');
				$('contactbevestiging').set('html', 'Uw bericht wordt verzonden...');
				$('contactform').set('send', {
					 url: 'include/contact.php', 
					 method: 'post', 
					 update: $('contactbevestiging'),
					 onComplete: function(response) {
						$('contactbevestiging').set('html', response);
						$('verzenden').fade('in');
						}
					 });
				$('contactform').send();
				}
			}.bind(this));

		$('opmerkingen').addEvent('focus', function(e) {
			e = new Event(e).stop();
			if ($('opmerkingen').value.substr(0,8) == 'Typ hier') $('opmerkingen').value = '';
			});
		
		$$('#contactform input').addEvent('keypress', function(e){
			return !(e.code == 13); 
			});
		
		$$('#contactform input[type=text]').addEvent('focus', function(e){
			this.setStyle('backgroundColor', '#FFF');
			});
		
		$$('#contactform input[type=text]').addEvent('blur', function(e){
			this.setStyle('backgroundColor', '#F8F8F8');
			});
		
		$$('#contactform .bestemmingkeuze').addEvent('click', function(e) {
			this.checkverplicht();
			}.bind(this));
	},
	
	checkverplicht: function() {
		$$('.verplicht').setStyle('visibility' , 'hidden');
		if ($('contactform').actie[0].checked) {
			$$('.kverplicht').setStyle('visibility' , 'visible');
			}
		else {
			$$('.overplicht').setStyle('visibility' , 'visible');
			}
		$$('#contactform input[type=text]').each(function(item, index){
			item.setStyle('backgroundColor', '#F8F8F8');	
			});
	},

	validate: function() {
		var ok = true;
		var bestemming = false;
		var form = $('contactform');
			
		if ($('checkkassa').checked) {
			bestemming = true;
			$$('#contactform .kv').each(function(item, index){
			item.setStyle('backgroundColor', '#F8F8F8');												 
				if (item.value == '') {
					item.setStyle('backgroundColor', '#FCC');
					ok = false;
					}
				});
			}
			
		if ($('checkoverig').checked) {
			bestemming = true;
			$$('#contactform .ov').each(function(item, index){
			item.setStyle('backgroundColor', '#F8F8F8');
				if (item.value == '') {
					item.setStyle('backgroundColor', '#FCC');
					ok = false;
					}
				});
			}
			
		if (!bestemming) {
			alert ('Geef de bestemming van uw bericht aan.');
			return false;
			}
			
		if (!ok) {
			alert ('Vul de verplichte gegevens in.');
			return false;
			}
		return true;
	}
	
});


// NIEUWSBRIEF

var Nieuwsbrief = new Class({

	initialize: function() {
		new Request.HTML({url: '/include/tmailform.php' , method: 'get', update: $('theatermail'), onComplete: function(){this.initform();}.bind(this)}).get();
	},
	
	initform: function() {
		$('adres').addEvent('focus', function(e) { 
			e = new Event(e).stop();
			if ($('adres').value == 'uw e-mailadres') $('adres').value = '';
			});
		
		$('tmailform').addEvent('submit', function(e) {
			new Event(e).stop();												 
			if (this.validate()) {	
				$('tmailform').fade('out');
				$('tmailform').set('send', {
					 url: 'include/tmailform.php', 
					 method: 'post', 
					 update: $('tmailbevestiging'),
					 onComplete: function(response) {
						$('tmailbevestiging').set('html', response);
						}
					 });
				$('tmailform').send();
				}
			}.bind(this));
			
	},
		
	validate: function() {
		var adres = $('adres').value;
		if (adres == '' || adres == 'uw e-mailadres') return false;
		return true;
	}
	
	
});



// THEATERMAGAZINE

var Theatermagazine = new Class({

	initialize: function() {
		new Request.HTML({url: '/include/theatermagazine.php' , method: 'get', update: $('theatermagazine'), onComplete: function(){this.initform();}.bind(this)}).get();
	},
	
	initform: function() {
		
		$('theatermagazineform').addEvent('submit', function(e) {
			new Event(e).stop();												 
			if (this.validate()) {
				$('theatermagazineform').fade('out');
				$('theatermagazineform').set('send', {
					 url: 'include/theatermagazine.php', 
					 method: 'post', 
					 update: $('theatermagazinebevestiging'),
					 onComplete: function(response) {
						$('theatermagazinebevestiging').set('html', response);
						}
					 });
				$('theatermagazineform').send();
				}
			}.bind(this));
		
		$$('#theatermagazineform input').addEvent('keypress', function(e){
			return !(e.code == 13); 
			});
		
		$$('#theatermagazineform input[type=text]').addEvent('focus', function(e){
			this.setStyle('backgroundColor', '#FFF');
			});
		
		$$('#theatermagazineform input[type=text]').addEvent('blur', function(e){
			this.setStyle('backgroundColor', '#F8F8F8');
			});
			
	},
		
	validate: function() { //todo
		return true;
	}
	
});


// ZOEKEN

var Zoek = new Class({
	
	getResult: function(zoekstring) {
		$('zoekload').fade('show');
		new Request.HTML({url: '/include/zoeken3.php' , method: 'get', update: $('zoekresultaat'), onScuccess: tabs.opentab('reszoek'), onComplete: function(){$('zoekload').fade('out');} }).get({'z': zoekstring, 'p': pag});
		},
				
	initialize: function() {
		$('zoekinput').addEvent('keyup', function(event) { 
			if (event.key == 'left' || event.key == 'right') event.stopPropagation();	
			if (event.key == 'up' || event.key == 'down') return;
			if ($('zoekinput').value.length > 1) this.getResult($('zoekinput').value);
			//else $('zoekresultaat').set('html', '');
			else $('zoekresultaat').empty();
			}.bind(this));
		
		$('zoekinput').addEvent('keydown', function(event){  
			if (event.key == 'left' || event.key == 'right') event.stopPropagation();
			if (event.key == 'up' || event.key == 'down') return;
			});
		$('zoekinput').addEvent('focus', function(e) {
			if ($('zoekinput').value == 'zoeken') $('zoekinput').value = '';
			else {this.getResult($('zoekinput').value);};
			$('zoekinput').addClass('zoekfocus');
			}.bind(this)),
		
		$('zoekinput').addEvent('blur', function(e) {
			this.removeClass('zoekfocus');									 
			})
		
		$('zoekload').fade('hide');
		
		}
});


// POPUP

var PopUp = new Class({
					  
	element: null,
	contentwrap: null,
	closebutton: null,
	popupfade: null,
	
	initialize: function(element, contentwrap, handle, closebutton) {
		this.element=element;
		this.contentwrap = contentwrap;
		this.closebutton = closebutton;
		this.popupfade = new Fx.Morph(element, {duration: '200', transition: Fx.Transitions.Sine.easeInOut, link: 'chain'});
		this.popupfade.set({'opacity': 0});
		this.closebutton.addEvent('click', function(){
			this.hide(); 
			}.bind(this));
		this.closebutton.addEvent('mouseenter', function(){
			this.src = '/images/close-hover.png';
			});
		this.closebutton.addEvent('mouseleave', function(){
			this.src = '/images/close.png';
			});
		var myDragInstance = new Drag(element, {handle: handle});
		},
		
	empty: function() {
		this.contentwrap.empty();
		},
		
	fill: function(content) {
		content.injectInside(this.contentwrap);
		},
	
	show: function() {
		this.element.position();
		this.element.setStyle('visibility', 'visible');
		this.popupfade.start({opacity: 1}); 
		if (!Browser.ie6) mask.show();
		},
		
	hide: function() {
		this.popupfade.start({opacity: 0}).chain(function() {
			this.element.setStyle('visibility', 'hidden');
        }.bind(this));
		mask.hide();
		}

});


// MOVIE

var needflash = (Browser.Plugins.Flash.version < 7);

window.addEvent('load', function(){
	swifff = new Swiff('/swf/NonverBlaster.swf', {
		id: 'nonverblaster',
		width: 640,
		height: 420,
		container: $('moviewrap'), 
		params: {
			wMode: 'opaque',
			bgcolor: '#ff3300',
			allowFullScreen: "true",
			allowScriptAccess: "always"
		},
		vars: {
			mediaURL: "http://www.schouwburgcuijkvideo.com/logo.flv",
			allowSmoothing: "true",
			autoPlay: "true",
			scaling: "true",
			buffer: "10",
			showTimecode: "true",
			loop: "false",
			controlColor: "0xFFCC00",
			controlBackColor: "0x000000",
			scaleIfFullScreen: "true",
			showScalingButton: "true",
			defaultVolume: "100",
			crop: "false",
		    onClick: "toggleFullScreen"
		},
		callBacks: {
		}
	});
	(function(){registerForJavaScriptCommunication("nonverblaster")}).delay(100);
});


function speelVideo(bestand, bron, titel) {
	bestand = escape(bestand);
	
	if (needflash || Browser.ie6) alert ('Helaas kunt u de filmpjes op deze website niet bekijken.\nU gebruikt een verouderde browser of heeft Flash niet geinstalleerd.');

	else {
		$('moviebg').setStyle('visibility', 'visible');
		$('moviebg').position();
		moviebgfade.start({opacity: 1});
		$('moviewrap').setStyles({'width': '640px', 'height': '420px'});
		$('moviewrap').position();
		if(bron=='yt') try{(function(){sendToNonverBlaster("switch:"+"http://www.youtube.com/watch?v="+bestand);}).delay(100);} catch(err){}
		if(bron=='sc') try{(function(){sendToNonverBlaster("switch:"+"http://www.schouwburgcuijkvideo.com/"+bestand);}).delay(100);} catch(err){}
		if (!Browser.ie6) mask.show();
		}
}


// BESTELLEN

function bestelform (titel, org, vrstnr, label) {
	var bestellenWrap = new Element('div', {'id': 'bestellen'});
	new Element('div', {'class': 'titel'}).set({'html': titel}).injectInside(bestellenWrap);
	var besteltekst = new Element('div', {'class': 'besteltekst'}).set({'html':"Vul hier het aantal toegangskaarten in dat u wilt bestellen in klik op 'in winkelwagen'." }).injectInside(bestellenWrap);
	var bestelaantal = new Element('input', {'class': 'bestelaantal', 'type': 'text', 'id': 'aantalvrst'}).injectInside(bestellenWrap);
	var bestelknop = new Element('a', {'class': 'knop'}).set({'html': "<img src='/images/inwinkelmandje.png' alt='in winkelmandje' />", href:'#', onfocus:'blur()'}).injectInside(bestellenWrap);
	var rolstoeltekst = new Element('div', {'class': 'rolstoeltekst'}).set({'html': "* Rolstoelplaatsen kunnen alleen telefonisch worden besteld." }).injectInside(bestellenWrap);
	bestelknop.addEvent('click', function(){
		mandje.toevoegenMandje(org, vrstnr, bestelaantal.value, label); 
		$('toevoegenmandje').set({'html': "<a href='#' onclick='javascript: verderwinkelen(); return false;' onfocus='blur()'><img src='/images/verderwinkelen.png' width='130' height='25' alt='verder winkelen' /></a>&nbsp;&nbsp;<a href='#'  onfocus='blur()' onclick='javascript:mandje.bestelUrl(); return false;'><img src='/images/ditbestellen.png' width='130' height='25' alt='dit bestellen' /></a><div id='wachtoplvp'>Momentje, de bestelpagina wordt geladen.</div>"});
		return false;
		});
	bestelaantal.addEvent('keydown', function(event){
		if (event.key == 'enter' ) {										  
			mandje.toevoegenMandje(org, vrstnr, bestelaantal.value, label); 
			$('toevoegenmandje').set({'html': "<a href='#' onclick='javascript: verderwinkelen(); return false;' onfocus='blur()'><img src='/images/verderwinkelen.png' alt='verder winkelen' width='130' height='25' /></a>&nbsp;&nbsp;<a href='#'  onfocus='blur()' onclick='javascript:mandje.bestelUrl(); return false;'><img src='/images/ditbestellen.png' alt='dit bestellen' width='130' height='25' /></a><div id='wachtoplvp'>Momentje, de bestelpagina wordt geladen.</div>"});
			return false;
			}
		});
	$('toevoegenmandje').empty();
	bestellenWrap.injectInside($('toevoegenmandje'));
	toonmandje();
	$('aantalvrst').focus();
}


function toonmandje() {
	new Fx.Scroll(window, {duration: 500, transition: Fx.Transitions.quadIn}).toTop();
	mandje.toonMandje();
	tabs.opentab('reswinkelmandje');
}

function verderwinkelen() {
	tabs.closeall();
}



// WINKELMANDJE

var Mandje = new Class({
					   
	contentwrap: null,
	
	initialize: function(content) {
		this.contentwrap = content;
		},
		
	toevoegenMandje: function (org, vrstnr, aantal, label) {
		if (aantal == '') aantal = 0;
		if ((!isNaN(org)) && (!isNaN(vrstnr)) && (!isNaN(aantal))) {
			switch (vrstnr) {
				case '1811': if(aantal > 1) {aantal = 1; break;}
				case '1552': if(aantal > 4) {alert('Maximaal 4 kaarten.'); aantal = 4; return; break}
				case '1553': if(aantal > 4) {alert('Maximaal 4 kaarten.'); aantal = 4; return; break}
				default: if (aantal > 10) {alert('Maximaal 10 kaarten.'); aantal = 10; return}
			
				}
			new Request.HTML({url: '/include/mandje.php', update: this.contentwrap}).get({'actie':'toevoegen', 'org':org, 'vrstnr':vrstnr, 'aantal':aantal, 'label':label});
			}
		},
	
	toonMandje: function() {
		var response = new Request.HTML({url: '/include/mandje.php', method: 'get', update: this.contentwrap}).get({'actie':'tonen'});
		},
		
	mandjeVerwijderen: function() {
		//new Request.HTML({url: '/include/mandje.php' , method: 'get', update: this.contentwrap}).get({'actie':'mandjeverwijderen'});
		new Request.HTML({url: '/include/mandje.php' , method: 'get'}).get({'actie':'mandjeverwijderen'});
		},
		
	verwijderVrst: function(vrstnr) {
		new Request.HTML({url: '/include/mandje.php', update: this.contentwrap}).get({'actie':'voorstellingverwijderen', 'vrstnr':vrstnr});
		},
		
	bestelUrl: function() {
		$('wachtoplvp').setStyle('visibility', 'visible');
		return new Request({url:'/include/mandje.php', method: 'get', onComplete: function(response) {this.openResponse(response);}.bind(this)}).get({'actie':'bestelurl'});
		},
		
	openResponse: function (response) {
		if ((response == 0) || (response == 1)) alert('Er is een fout opgetreden. '+ response);
		else {
			if (response == -3) alert('Het winkelwagentje is leeg.');
			else {
				this.mandjeVerwijderen();
				//$('loading').setStyle('visibility', 'visible');	
				window.location = response;
				}
			}
		}
});


function popinfo(info) {
	popup.empty();
	new Request.HTML({'url': '/include/'+info, update: $('popupbody'), onComplete: function() {popup.show()} }).get();
}

function goinfo(pagina, onderwerp) {
	if (pagina == pag) markeer(onderwerp);
	else window.location = pagina+'.php#'+onderwerp;
}

function markeer(id) {
	if ($(id)) {
		$(id).morph({'border': '1px solid #F00', 'background-color': '#FEE'});		
		new Fx.Scroll(window, {offset: {'x': 0, 'y': -200}}).toElement($(id));
		}
}


// NIEUWSITEM

function toonNieuws(id) {
	//$('popup').position();
	popup.empty();
	var nr=id.substring(1);
	new Request.HTML({'url': '/include/getNieuws2.php', update: $('popupbody'), evalScripts: true, onComplete: function() {popup.show()} }).get({'wat':nr});
}


// WACHTLIJST

function wachtlijst(nr) {
	new Request.HTML({url: '/include/wachtlijst.php', update: $('wachtlijst'), onComplete: tabs.opentab('reswachtlijst'),  evalScripts:true}).get({'vrst': nr});	
}


Mask.Fx = new Class({

    Extends: Mask,

    options: {
        start: 0,
        end: 0.6,
        fx: {
            property: 'opacity',
            link: 'cancel', 
			duration: 750
        }
    },

    initialize: function(target, options){
        this.parent(target, options);
        this.element.set('tween', this.options.fx);
        this.element.get('tween').set(this.options.fx.property, this.options.start);
        this.element.setStyle('display', 'block');
    },

    showMask: function(){
		this.element.setStyle('visibility', 'visible');
        this.hidden = false;
        this.fireEvent('show');
        this.element.get('tween').start(this.options.end).chain(function() {
            this.fireEvent('showEnd');
        }.bind(this));
    },

    hideMask: function(){
        this.hidden = true;
        this.fireEvent('hide');
        this.element.get('tween').start(this.options.start).chain(function() {
            this.fireEvent('hideEnd');
			this.element.setStyle('visibility', 'hidden');
        }.bind(this));
    }
});

