var isloggedin;
var press_data_all = new Array();
var did_alert = false;

var myrules = {

	'#form_newtag' : function(element) {
		element.onsubmit = function(){
			// let behaviour know that
			// we are about to submit the form.
			ajax_isloading(1);
			$(this).request({
				onComplete: function(){
//					alert("saved");
					$('body_newtag').hide();
					$('button_addtags').show();
					movieid = $F($('movieid'));
					
					ajax_read_tags();
					ajax_isloading(-1);

				}
			});
			return false;
		}
	},

	'#loading_wait' : function(element){
		element.oninit = function(){

			if (window.innerHeight && window.scrollMaxY) {	
				xScroll = document.body.scrollWidth;
				yScroll = window.innerHeight + window.scrollMaxY;
			} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
				xScroll = document.body.scrollWidth;
				yScroll = document.body.scrollHeight;
			} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
				xScroll = document.body.offsetWidth;
				yScroll = document.body.offsetHeight;
			}

			var windowWidth, windowHeight;
			if (self.innerHeight) {	// all except Explorer
				windowWidth = self.innerWidth;
				windowHeight = self.innerHeight;
			} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
				windowWidth = document.documentElement.clientWidth;
				windowHeight = document.documentElement.clientHeight;
			} else if (document.body) { // other Explorers
				windowWidth = document.body.clientWidth;
				windowHeight = document.body.clientHeight;
			}

			$(this).style.height = xScroll + "px";
			$(this).style.top = 0;

			$('loading_image').style.top = ((windowHeight - 139) / 2) + "px";

			$(this).hide();
		}
	},

	'#add_rezension_link' : function(element){
		element.onclick = function(){
			$('rezension_form').show();
			$('add_rezension_link').hide();
			return false;
		}
	},
	'.form_rezension' : function(element) {
		element.onsubmit = function(){
			ajax_isloading(1);
			$(this).request({
				onComplete: function(){
					$('rezension_form').hide();
					$('add_rezension_link').show();
					ajax_isloading(-1);
				}
			});
			return false;
		}
	},

	'.form_password' : function(element) {
		element.onsubmit = function(){
			ajax_isloading(1);
			$(this).request({
				onComplete: function(){
					ajax_isloading(-1);

					document.getElementsByClassName('s_personal_data').each(function(element){
						$(element).up(0).toggleClassName('invis');
						$(element).up(0).classNames().grep(/s_/).each(function(s){
							if ($(s)) {
								$(s).toggle();
							};
						});
					});

					$(this).reset();
				}
			});
			return false;
		}
	},
	'.form_personal_data' : function(element) {
		element.onsubmit = function(){
			ajax_isloading(1);
			$(this).request({
				onComplete: function(){
					ajax_isloading(-1);

					document.getElementsByClassName('s_personal_data').each(function(element){
						$(element).up(0).toggleClassName('invis');
						$(element).up(0).classNames().grep(/s_/).each(function(s){
							if ($(s)) {
								$(s).toggle();
							};
						});
					});
				}
			});
			return false;
		}
	},

	'#myphe' :  function(element){
		element.oninit = function(){
			new Ajax.Request('/ajax/get_username.php', {
				method:'get',
				onSuccess: function(transport){
					var json = transport.responseText.evalJSON(true);

					if (json) {

						new Insertion.Bottom($('myphe'), json.name + "'s Paramount");
						new Insertion.Bottom($('myphe'), "&nbsp;/" + "/&nbsp;<a href='/myphe/logout'>Logout</a>");
	
						if ($('page_filminfo')) {
							ajax_read_page_filminfo();
						}
					}
				}
			});
		}
	},

	'#link_clubcinema' : function(element){
		element.onclick = function(){
			new Ajax.Request('/ajax/set_link_bsystem.php', {
				method:'get',
				parameters: {system: 'clubcinema'},
				onComplete: function(transport){
// fixme: reagieren!
					window.location.reload();
				}
			});
			return false;
		}
	},

	'#link_tvstars' : function(element){
		element.onclick = function(){
			new Ajax.Request('/ajax/set_link_bsystem.php', {
				method:'get',
				parameters: {system: 'tvstars'},
				onComplete: function(transport){
// fixme: reagieren!
					window.location.reload();
				}
			});
			return false;
		}
	},

	'.removelang a' : function(element) {
		element.onclick = function() {
			ajax_isloading(1);
			$(this).up(0).classNames().grep(/l_/).each(function(s){
				new Ajax.Request('/ajax/set_userlang.php', {
					method:'post',
					parameters: {remove: true, lang: s},
					onComplete: function(transport){
						window.setTimeout("ajax_read_languages()", 500);
						ajax_isloading(-1);
					}
				});
				});
			return false;
		}
	},

	'.removesub a' : function(element) {
		element.onclick = function() {
			$(this).up(0).classNames().grep(/l_/).each(function(s){
				ajax_isloading(1);
				new Ajax.Request('/ajax/set_usersubtitle.php', {
					method:'post',
					parameters: {remove: true, lang: s},
					onComplete: function(transport){
						window.setTimeout("ajax_read_subtitles()", 500);
						ajax_isloading(-1);
					}
				});
				});
			return false;
		}
	},


	'.removegenre a' : function(element) {
		element.onclick = function() {
			$(this).up(0).classNames().grep(/l_/).each(function(s){
				ajax_isloading(1);
				new Ajax.Request('/ajax/set_usergenre.php', {
					method:'post',
					parameters: {remove: true, lang: s},
					onComplete: function(transport){
						window.setTimeout("ajax_read_genres()", 500);
						ajax_isloading(-1);
					}
				});
				});
			return false;
		}
	},

	'#form_newlang' : function(element) {
		element.onsubmit = function(){
			// let behaviour know that
			// we are about to submit the form.
			$(this).request({
				onComplete: window.setTimeout("ajax_read_languages()", 500)
			});
			return false;
		}
	},
	
	'#form_newgenre' : function(element) {
		element.onsubmit = function(){

			$(this).request({
				onComplete: window.setTimeout("ajax_read_genres()", 500)
			});
			return false;
		}
	},
	'#form_newgenre .iselect': function(element){
		element.onchange = function(){

			$('form_newgenre').request({
				onComplete: window.setTimeout("ajax_read_genres()", 500)
			});
			return false;
		}
	},
	'#form_newsubtitle' : function(element) {
		element.onsubmit = function(){

			$(this).request({
				onComplete: window.setTimeout("ajax_read_subtitles()", 500)
			});
			return false;
		}
	},
	'#form_newsubtitle .iselect': function(element){
		element.onchange = function(){

			$('form_newsubtitle').request({
				onComplete: window.setTimeout("ajax_read_subtitles()", 500)
			});
			return false;
		}
	},
	'#form_newlang .iselect': function(element){
		element.onchange = function(){
			// let behaviour know that
			// we are about to submit the form.
			$('form_newlang').request({
				onComplete: window.setTimeout("ajax_read_languages()", 500)
			});
			return false;
		}
	},

	'.extlink' : function(element){
		element.onclick = function() {
			window.open($(this).href, '_blank');
			return false;
		}
	},

	'#flash_marquee' : function(element){
		element.oninit = function(){
			var so = new SWFObject("/elements/marquee.swf", "marquee_flash_file", "879", "110", "7", "#0035ad");
			so.write("flash_marquee");
		}
	},

	'#flash_trailer' : function(element){
		element.oninit = function(){
			movieid = $F($('movieid'));
			var so = new SWFObject("/trailer/" + movieid, "trailer_flash_file", "330", "300", "7", "#a1beea");
			so.write("flash_trailer");
			
			$(this).show();
		}
	},

	'#flash_gallery' : function(element){
		element.oninit = function(){
			movieid = $F($('movieid'));
			var so = new SWFObject("/elements/galleryplayer.swf?film=" + movieid, "gallery_flash_file", "330", "300", "6", "#a1beea");
			so.write("flash_gallery");

			if (!$('flash_trailer')) {
				$(this).show();
			}
		}
	},

	'#flash_wallpaper' : function(element){
		element.oninit = function(){
			movieid = $F($('movieid'));
			var so = new SWFObject("/elements/wallpaperplayer.swf?film=" + movieid, "wallpaper_flash_file", "330", "300", "6", "#a1beea");
			so.write("flash_wallpaper");

			if (!$('flash_trailer') && !$('flash_gallery')) {
				$(this).show();
			}
		}
	},

	'#flash_gsp' : function(element){
		element.oninit = function(){
			$(this).classNames().grep(/^gsp_.+$/).each(function(s){
				classname = s;
			});

			$(this).classNames().grep(/^x\d+$/).each(function(s){
				valx = s.replace(/x/,'');
			});
			$(this).classNames().grep(/^y\d+$/).each(function(s){
				valy = s.replace(/y/,'');
			});

			var so = new SWFObject("/elements/content_flash/" + classname + ".swf", "flash_gsp", valx, valy, "6", "#ffffff");
			so.write("flash_gsp");
		}
	},


	'.bt_dl a' : function(element) {
		element.onclick = function() {
			var classname;

			$(this).up(0).classNames().grep(/^type_.*$/).each(function(s){
				classname = s;
			});

			if (classname == 'type_trailer') {
				if ($('flash_gallery')) {
					$('flash_gallery').hide();
				}
				if ($('flash_wallpaper')) {
					$('flash_wallpaper').hide();
				}
				if ($('flash_trailer')) {
					$('flash_trailer').show();
				}
			}
			if (classname == 'type_galerie') {
				if ($('flash_gallery')) {
					$('flash_gallery').show();
				}
				if ($('flash_wallpaper')) {
					$('flash_wallpaper').hide();
				}
				if ($('flash_trailer')) {
					$('flash_trailer').hide();
				}
			}
			if (classname == 'type_wallpaper') {
				if ($('flash_gallery')) {
					$('flash_gallery').hide();
				}
				if ($('flash_wallpaper')) {
					$('flash_wallpaper').show();
				}
				if ($('flash_trailer')) {
					$('flash_trailer').hide();
				}
			}
			if (classname == 'type_presse') {
				if ($('flash_gallery')) {
					$('flash_gallery').hide();
				}
				if ($('flash_wallpaper')) {
					$('flash_wallpaper').hide();
				}
				if ($('flash_trailer')) {
					$('flash_trailer').hide();
				}
				if ($('presse_downloads')) {
					$('presse_downloads').show();
				}
			}
			if (classname == 'type_website') {
			}

			return false;
		}
	},

	'.posthide' : function(element) {
		element.oninit = function(){
			$(this).hide();
		}
	},

	'#page_filminfo' : function(element){
		element.oninit = function(){
/*
	Init all web 2.0-items for product-info here!
*/

			$$('input.recipient').each(function(s){
				Element.observe(s, 'keypress', function(eEvent){
					if (eEvent.keyCode == Event.KEY_ESC) {
						$(s).clear();
						$$('.notify_form').invoke('hide');
					}
				});
			});

			$$('input.rezension').each(function(s){
				Element.observe(s, 'keypress', function(eEvent){
					if (eEvent.keyCode == Event.KEY_ESC) {
						$(s).clear();
						$('rezension_form').hide();
						$('add_rezension_link').show();
					}
				});
			});


			
			movieid = $F('movieid');
			new Ajax.Request('/ajax/get_notifyallowed.php', {
				method:'post',
				parameters: {movie: movieid},
				requestHeaders: {Accept: 'application/json'},
				onSuccess: function(transport){
					var json = transport.responseText.evalJSON(true);

					if (json.data) {
						json.data.each(function(v) {
							if ($('container').getElementsByClassName('notify_' + v.id)) {
								$$('.button_sms.notify_' + v.id).each(function(s) {
									s.show();
									s.addClassName('notify_a_' + v.id);
								});
							}
	
						});
						
						Behaviour.apply();
					}
				}
			});


			
		}
	},
	
	'.button_sms a' : function(element){
		element.onclick = function() {
			$(this).up(0).classNames().grep(/notify_/).each(function(s){
//				$$('.notify_form').invoke('hide');
				$$('.notify_form.' + s).invoke('show');
			});
			return false;
		}
	},
	
	'.form_notify' : function(element){
		element.onsubmit = function(){
			$(this).request({
				onComplete: function(){
					$$('.notify_form').invoke('hide');
				}
			});
			return false;
		}
	},

	'#page_myphe' : function(element){
/*
	Called upon load of users web.20-page
*/
		element.oninit = function(){
			ajax_read_tags();
			ajax_read_languages();
			ajax_read_subtitles();
			ajax_read_genres();
		}
	},
	
	'.hideview a' : function(element){
		element.onclick = function(){
			$(this).up(0).toggleClassName('invis');
			$(this).up(0).classNames().grep(/s_/).each(function(s){
				if ($(s)) {
					$(s).toggle();
				}
				});
		}
	},

	'#button_addtags a' : function(element){
		element.onclick = function(){
			$('body_newtag').show();
			$('button_addtags').hide();
			$('newtag').activate();
			return false;
		}
	},
	'#button_addlangs a' : function(element){
		element.onclick = function(){
			$('body_newlang').show();
			$('button_newlang').hide();
			$('newlang').activate();
			return false;
		}
	},
	'#button_addsubtitles a' : function(element){
		element.onclick = function(){
			$('body_newsubtitle').show();
			$('button_newsubtitle').hide();
			$('newsubtitle').activate();
			return false;
		}
	},
	'#button_addgenres a' : function(element){
		element.onclick = function(){
			$('body_newgenre').show();
			$('button_newgenre').hide();
			$('newgenre').activate();
			return false;
		}
	},

	'#body_rezensionen' : function(element) {
		element.oninit = function(){
			$('body_rezensionen').hide();
		}
	},
	'#body_castcrew' : function(element) {
		element.oninit = function(){
			$('body_castcrew').hide();
		}
	},

	'#select_medium' : function(element){
		element.oninit = function(){
			hddvd = $('cont_technical_hddvd');
			bluray = $('cont_technical_bluray');
			dvd = $('cont_technical_dvd');
			umd = $('cont_technical_umd');
			video = $('cont_technical_video');

			if (hddvd) {
				hddvd.hide();
			}
			if (bluray) {
				bluray.hide();
			}
			if (dvd) {
				dvd.hide();
			}
			if (umd) {
				umd.hide();
			}
			if (video) {
				video.hide();
			}

			if (dvd) {
				$('cont_technical_dvd').show();
				$('select_medium').className = "selector dvd";
			}
			else if (hddvd) {
				$('cont_technical_hddvd').show();
				$('select_medium').className = "selector hddvd";
			}
			else if (bluray) {
				$('cont_technical_bluray').show();
				$('select_medium').className = "selector bluray";
			}
			else if (umd) {
				$('cont_technical_umd').show();
				$('select_medium').className = "selector umd";
			}
			else if (video) {
				$('cont_technical_video').show();
				$('select_medium').className = "selector video";
			}

		}
	},

/*
	Funktionen der Registerkarten
		auf der Seite Produktinfos
*/
	'#register_story a' : function(element){
		element.onclick = function(){
			$('select_info').className='selector story';
			$('body_story').show();
			$('body_castcrew').hide();
			$('body_rezensionen').hide();
			return false;
		}
	},
	'#register_castcrew a' : function(element){
		element.onclick = function(){
			$('select_info').className='selector castcrew';
			$('body_castcrew').show();
			$('body_story').hide();
			$('body_rezensionen').hide();
			return false;
		}
	},
	'#register_rezensionen a' : function(element){
		element.onclick = function(){
			$('select_info').className='selector rezensionen';
			$('body_rezensionen').show();
			$('body_castcrew').hide();
			$('body_story').hide();
			return false;
		}
	},

	'#register_dvd a' : function(element){
		element.onclick = function(){
		
			$('cont_technical_hddvd', 'cont_technical_bluray', 'cont_technical_dvd', 'cont_technical_umd', 'cont_technical_video').each(function(v) {
				if (v) {
					v.hide()
				}
				});

			$('cont_technical_dvd').show();
			$('select_medium').className = "selector dvd";

			return false;
		}
	},
	'#register_bluray a' : function(element){
		element.onclick = function(){
			$('cont_technical_hddvd', 'cont_technical_bluray', 'cont_technical_dvd', 'cont_technical_umd', 'cont_technical_video').each(function(v) {
				if (v) {
					v.hide()
				}
				});

			$('cont_technical_bluray').show();
			$('select_medium').className = "selector bluray";

			return false;
		}
	},
	'#register_hddvd a' : function(element){
		element.onclick = function(){
			$('cont_technical_hddvd', 'cont_technical_bluray', 'cont_technical_dvd', 'cont_technical_umd', 'cont_technical_video').each(function(v) {
				if (v) {
					v.hide()
				}
				});

			$('cont_technical_hddvd').show();
			$('select_medium').className = "selector hddvd";

			return false;
		}
	},
	'#register_video a' : function(element){
		element.onclick = function(){
			$('cont_technical_hddvd', 'cont_technical_bluray', 'cont_technical_dvd', 'cont_technical_umd', 'cont_technical_video').each(function(v) {
				if (v) {
					v.hide()
				}
				});

			$('cont_technical_video').show();
			$('select_medium').className = "selector video";
			return false;
		}
	},
	'#register_umd a' : function(element){
		element.onclick = function(){
			$('cont_technical_hddvd', 'cont_technical_bluray', 'cont_technical_dvd', 'cont_technical_umd', 'cont_technical_video').each(function(v) {
				if (v) {
					v.hide()
				}
				});

			$('cont_technical_umd').show();
			$('select_medium').className = "selector umd";
			return false;
		}
	},

	'div.button_dvdsammlung a' : function(element){
		element.onclick = function(){
			var id = $(this).up(0).classNames().find(function(s) { return s.startsWith('_');});
			var remove = $(this).up(0).hasClassName('set');
			new Ajax.Request('/ajax/set_own.php', {
				method:'post',
				parameters: {produkt: id, remove: remove}
				});
			$(this).up(0).toggleClassName('set');
			return false;
		}
	},
	'div.button_wunschliste a' : function(element){
		element.onclick = function(){
			var id = $(this).up(0).classNames().find(function(s) { return s.startsWith('_');});
			var remove = $(this).up(0).hasClassName('set');
			new Ajax.Request('/ajax/set_wish.php', {
				method:'post',
				parameters: {produkt: id, remove: remove}
				});
			$(this).up(0).toggleClassName('set');
			return false;
		}
	},

/*
	Filmbewertungen
	Sterne
*/
	
	'#bewertung_star1' : function(element){
		element.onclick = function(){
			var stars = 1;
			ajax_set_stars(stars);

			movieid = $F($('movieid'));
			new Ajax.Request('/ajax/set_rating.php',
				{
				method:'post',
				parameters: {movie: movieid, rating: stars}
				});
			return false;
		}
	},
	'#bewertung_star2' : function(element){
		element.onclick = function(){
			var stars = 2;

			ajax_set_stars(stars);

			movieid = $F($('movieid'));
			new Ajax.Request('/ajax/set_rating.php',
				{
				method:'post',
				parameters: {movie: movieid, rating: stars}
				});
			return false;
		}
	},
	'#bewertung_star3' : function(element){
		element.onclick = function(){
			var stars = 3;

			ajax_set_stars(stars);

			movieid = $F($('movieid'));
			new Ajax.Request('/ajax/set_rating.php',
				{
				method:'post',
				parameters: {movie: movieid, rating: stars}
				});
			return false;
		}
	},
	'#bewertung_star4' : function(element){
		element.onclick = function(){
			var stars = 4;

			ajax_set_stars(stars);

			movieid = $F($('movieid'));
			new Ajax.Request('/ajax/set_rating.php',
				{
				method:'post',
				parameters: {movie: movieid, rating: stars}
				});
			return false;
		}
	},
	'#bewertung_star5' : function(element){
		element.onclick = function(){
			var stars = 5;

			ajax_set_stars(stars);

			movieid = $F($('movieid'));
			new Ajax.Request('/ajax/set_rating.php',
				{
				method:'post',
				parameters: {movie: movieid, rating: stars}
				});
			return false;
		}
	},

	'#accept_rules' : function(element){
		element.onclick = function(){
			if ($F('accept_rules') && $F('accept_privacy') && $F('accept_register')) {
				$('register_submit').enable();
			}
			else {
				$('register_submit').disable();
			}
		}
	},
	'#accept_privacy' : function(element){
		element.onclick = function(){
			if ($F('accept_rules') && $F('accept_privacy') && $F('accept_register')) {
				$('register_submit').enable();
			}
			else {
				$('register_submit').disable();
			}
		}
	},
	'#accept_register' : function(element){
		element.onclick = function(){
			if ($F('accept_rules') && $F('accept_privacy') && $F('accept_register')) {
				$('register_submit').enable();
			}
			else {
				$('register_submit').disable();
			}
		}
	},

	'#newtag' : function(element){
		new Ajax.JSONAutocompleter( 'newtag', 'ac', '/ajax/get_tags.php' );
	},

	'#bt_showalllangs' : function (element) {
		element.onclick = function() {
			$$('.sprachen').each(function(s) {
				s.immediateDescendants().each(function(ss) {
					ss.classNames().grep(/sprache_\d+/, function(n) { ss.removeClassName(n); });
				});
			});
			$$('.untertitel').each(function(s) {
				s.immediateDescendants().each(function(ss) {
					ss.classNames().grep(/untertitel_\d+/, function(n) { ss.removeClassName(n); });
				});
			});
			$(this).remove();
		}
	},
	
/* * * * * * * *
 * Presse      *
 * * * * * * * */

	'.show_press' : function (element) {
		var name = element.getAttribute('id').substr( 18 );
		$('press_data_'+name).hide();
		press_data_all.push( element );
		element.onclick = function() {
			var name = element.getAttribute('id').substr( 18 );
			var elem = $('press_data_'+name);
			if (elem.visible())
			{
				elem.immediateDescendants().each( function( e ) { 
					e.firstChild.style.backgroundImage = '';
				} );
				elem.hide();
			}
			else
			{
				elem.show();
				elem.immediateDescendants().each( function( e ) {
					var style = e.firstChild.getAttribute( 'href' );
					e.firstChild.style.backgroundImage = 'url('+style+'?thumb=1&show=1)';
				} );
			}
		}
	},

	'#presse_weiteres_filter' : function (element) {
		element.onkeyup = function()
		{
			var suche = this.value.toLowerCase();
			$$('ul.presse_downloadlist').each( function( s ) { if(s.visible()) s.hide(); } );
			press_data_all.each(
				function( elem )
				{
					if (!elem)
						return;
					if (!elem.getAttribute)
						return;
	
					if (-1 < elem.getAttribute('title').toLowerCase().indexOf( suche ))
						$(elem).show();
					else
						$(elem).hide();
				}
			);
		}
	},

	'.to_200907_spongebob' : function (element) {
		element.onclick = function () {
			document.location.href = "/suche?q=spongebob&type=titel";
		}
	}
};

Behaviour.register(myrules);
