// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

function addEvent(o, e, f) {
    if (o.addEventListener) {
        o.addEventListener(e, f, false);
    } else if (o.attachEvent) {
        o.attachEvent('on' + e, function() {
            var e = window.event, s = false;
  
            e.stopPropagation = function() {
                e.cancelBubble = true;
            }
            e.preventDefault = function() {
                s = true;
            }
   
            f.call(o, e);
        
            if (s)
                return false;
        });
    }
}

/*addEvent(a, 'click', function(eventObject) {
    alert(eventObject.clientX, eventObject.clientY); //geht nun auch im IE ohne window.event

    alert(this.target); //kann hier nun verwendet werden, da auch im IE nun this richtig referenziert wird
});
*/


function add_star_over(star,num) {
	if (star.addEventListener){
		star.addEventListener('mouseover', function(e){
			for (var i = 1;i <= num; i++) {
				if (star_array[i]) {
					star_array[i].firstChild.src = '/images/green_star.gif';
				}
			}
		}, false);
		star.addEventListener('mouseout', function(e){
			for (var i = 1;i <= star_array.length; i++) {
				if (star_array[i]) {
					star_array[i].firstChild.src = '/images/white_star.gif';
				}
			}
		}, false);
	} else if (star.attachEvent){
		star.attachEvent('onmouseout', function(e){
			for (var i = 1;i <= num; i++) {
					star_array[i].firstChild.src = '/images/green_star.gif';
			}
		});
		star.attachEvent('onmouseover', function(e){
			for (var i = 1;i <= star_array.length; i++) {
					star_array[i].firstChild.src = '/images/white_star.gif';
			}
		});
	}

}


function show_flickr_image(el,url) {
	var container = document.createElement("div");
	el.style.position = "absolute";
	el.style.zIndex = 1500;
	container.style.position = "relative";
	container.style.zIndex = 1500;
	container.style.top = (-200) + "px";
	container.style.left= (-15) + "px";
	container.id = "image_container";
	el.onclick = container.onmouseout = function() {
		el.removeChild(container);
		el.style.position = "static";
		el.onclick = function() {
			show_flickr_image(el, url);
		}
	};
	var img = document.createElement("img");
	img.src = url;
	container.appendChild(img);
	el.appendChild(container);
}

function check_image_form() {
	if ($F('rights') != 1) {
		$('image_error_message').innerHTML = 'Du kannst ein Bild nur dann hochladen, wenn die Checkbox angekreuzt ist!<br/>';
		return (false);
	} else {
		$('image_error_message').innerHTML = '';
		return (true);
	}
}


function fill_trash() {
    $('trash').src = "/images/trashfull.gif";
}       

function empty_trash() {
    $('trash').src = "/images/trash.gif";
}

function set_visibility(check) {
	if (!check.checked) {
		$$('div.guestconfirm_text').each( function(elm) {
			elm.style.color='#eee';
		});
		$('event_guestconfirm_1').disable();
		$('event_guestconfirm_0').disable();
	} else {
		$$('div.guestconfirm_text').each( function(elm) {
			elm.style.color='#000';
		});
		$('event_guestconfirm_1').enable();
		$('event_guestconfirm_0').enable();					
	}
}


function add_guest_with_profile_name(profile_name) {
	if (profile_name) {
		if (document.getElementById('guest_login_' + profile_name)) {
			alert("Diesen User hast du schon eingeladen!");
		} else {
			var field = document.createElement('input');
			Element.extend(field);
			field.type = "hidden";
			field.value = profile_name;
			field.name = 'guest_logins[]';
			field.id = 'guest_login_' + profile_name;
	
			var display_field = document.createElement('div');
			display_field.id = 'display_guest_login_' + profile_name;
			display_field.style.cursor = 'pointer';
			display_field.style.lineHeight = '18px';
			display_field.innerHTML = profile_name;

			add_mouse_over(display_field,display_field.innerHTML);

			document.new_event_form.appendChild(field);
			$("event_guestlist").appendChild(display_field);
			if (document.getElementById("user_list_" + profile_name)) {
				$("user_list_" + profile_name).hide();
			}
		}
	}
}


function add_guest_with_email(email) {
	if (email) {
		var clean_email = email.gsub(/[\.\@\-\_]/,'');
		if (document.getElementById('guest_email_' + clean_email)) {
			alert("Diese eMail hast du schon eingeladen!");
		} else {
			var field = document.createElement('input');
			Element.extend(field);
			field.type = "hidden";
			field.value = email;
			field.name = 'guest_emails[]';
			field.id = 'guest_email_' + clean_email;
	
			var display_field = document.createElement('div');
			display_field.id = 'display_guest_email_' + clean_email;
			display_field.style.cursor = 'pointer';
			display_field.style.lineHeight = '18px';
			display_field.innerHTML = email;
	
			add_mouse_over(display_field,display_field.innerHTML);
	
			document.new_event_form.appendChild(field);
			$("event_guestlist").appendChild(display_field);
		}
	}
}				

function add_mouse_over(display_field,val) {

	var profile_name = display_field.id.split('_').slice(3).join('_');
	
	addEvent(display_field, 'click', function(){remove_guest_with_profile_name(this)});
	addEvent(display_field, 'mouseover', function(e) {
		display_field.style.backgroundColor='#e7e7e9';
		display_field.innerHTML  += ' <span alt="l&ouml;schen" style="color:#666">»</span>';
	});
	addEvent(display_field, 'mouseout', function(e) {
		display_field.style.backgroundColor='#dcdcde';
		display_field.innerHTML  = val;
	})
					
	/*if (display_field.addEventListener){
		display_field.addEventListener('click', remove_guest_with_profile_name, false);
		display_field.addEventListener('mouseover', function(e){
			this.style.backgroundColor='#e7e7e9';
			display_field.innerHTML  += ' <span alt="l&ouml;schen" style="color:#666">&gt;&gt;</span>';
		}, false);
		display_field.addEventListener('mouseout', function(e){
			this.style.backgroundColor='#dcdcde';
			display_field.innerHTML  = val;
		}, false);
	} else if (display_field.attachEvent){
		display_field.attachEvent('onclick', remove_guest_with_profile_name);
		display_field.attachEvent('onmouseout', function(e){
			display_field.style.backgroundColor='#dcdcde';
			display_field.innerHTML  = val;
		});
		display_field.attachEvent('onmouseover', function(e){
			display_field.style.backgroundColor='#e7e7e9';
			display_field.innerHTML  += ' <span alt="l&ouml;schen" style="color:#666">&gt;&gt;</span>';
		});
	}*/
}

/*addEvent(a, 'click', function(eventObject) {
    alert(eventObject.clientX, eventObject.clientY); //geht nun auch im IE ohne window.event

    alert(this.target); //kann hier nun verwendet werden, da auch im IE nun this richtig referenziert wird
});
*/

function remove_guest_with_profile_name(e) {
	//alert(e.id);
	document.getElementById('event_guestlist').removeChild(e);
	var profile_name = e.id.split('_').slice(3).join('_');
	if (document.getElementById("guest_login_" + profile_name)) {
		document.getElementById('new_event_form').removeChild(document.getElementById("guest_login_" + profile_name));
	}
	if (document.getElementById("guest_email_" + profile_name)) {
		document.getElementById('new_event_form').removeChild(document.getElementById("guest_email_" + profile_name));
	}					
	if (document.getElementById("user_list_" + profile_name)) {
		$("user_list_" + profile_name).show();
	}
}


function help_text(el,text) {
	var text_container = document.createElement("div");
	//alert(el.offsetTop);

	el.style.position = "absolute";

	text_container.style.position = "relative";
	text_container.style.zIndex = 150;
	text_container.style.top = -20 + 'px';
	text_container.style.left= -20 + "px";
	text_container.style.width = "200px";
	text_container.style.color = '#fff';
	text_container.style.fontWeight = 'normal';
	text_container.style.padding = '5px';
	text_container.style.backgroundColor = '#99CC00';
	text_container.style.border = '1px solid #fff';
	text_container.id = "text_container";
	el.onclick = text_container.onmouseout = function() {
		el.removeChild(text_container);
		el.style.position = "relative";
		el.onclick = function() {
			help_text(el, text);
		}
	};
	text_container.innerHTML = text;
	el.appendChild(text_container);
}

function _help_text(e,text) {
//	alert(text);
	if (!e) e = window.event;
	var text_container = document.createElement("div");
    mousey = e.pageY ? e.pageY : e.clientY;// + body.scrollTop;
    mousex = e.pageX ? e.pageX : e.clientX;// + body.scrollLeft;
	text_container.style.position = "absolute";
	text_container.style.zIndex = 15;
	text_container.style.top = (mousey - 20) + "px";
	text_container.style.left= (mousex - 20) + "px";
	text_container.style.width = "200px";
	text_container.style.color = '#fff';
	text_container.style.padding = '5px';
	text_container.style.backgroundColor = '#87aa02';
	text_container.style.border = '1px solid #fff';
	text_container.id = "text_container";
	text_container.onmouseout = function() {
		document.getElementsByTagName("body")[0].removeChild(text_container);
	};
	text_container.innerHTML = text;
	document.getElementsByTagName("body")[0].appendChild(text_container);
}

function add_answer() {
	var num = $$('#answers input').length;
	var field = document.createElement('input');
	with(field) {
		type = 'text';
		name = 'answer[' + num + '][text]';
		id = 'answer_' + num + '_text';
		style.marginBottom = '4px';
		size = '30';
	};
	$("answers").appendChild(field);
	$("answers").appendChild(document.createElement('br'));
}

function switch_commercial() {
//	if ($F('event_commercial') == 'true') {
		// Ausblenden der privaten Elemente
		$('bring_along_list').hide();
		$('event_form_layer_who').hide();
		$('entryfee_container').show();
//	} else {
//		$('bring_along_list').show();		
//		$('event_form_layer_who').show();
//		$('entryfee_container').hide();
//	}
}

/*var sites = document.getElementsByClassName('event_form_layer');
var cnt = 0;

function fold() {
	if (cnt < sites.length) {
		sites[cnt].style.display='none';
		cnt++;
		sites[cnt].style.display='block';
		if (sites.length == (cnt + 1)) {
			$('event_form_continue').hide();
		}
		$('event_form_back').show();
		
	}
}
function unfold() {
	if (cnt > 0) {
		sites[cnt].style.display='none';
		cnt-=1;
		sites[cnt].style.display='block';
		if (cnt == 0) {
			$('event_form_back').hide();
		}
		$('event_form_continue').show();
	}
}
*/
