/* -----------------------------------------------------------------------------
This webapplication is powered by indual
Copyright (c) 2010 indual GmbH (www.indual.ch)                        
-------------------------------------------------------------------------------- */


/* + add status comment form --------------------------------------------------- */
function add_status_comment (status_id) {
  $('add_status_comment_'+status_id).request({
		method: 'post',
    encoding: 'ISO-8859-1',
		onSuccess: function(response) {
			$('comment_surrounder_'+status_id).update(response.responseText);
		}
	});
}

/* + add guestbook comment form ------------------------------------------------- */
function add_guestbook_comment (id) {
  $('add_guestbook_entry_comment_'+id).request({
    method: 'post',
    encoding: 'ISO-8859-1',
    onSuccess: function(response) {
      $('comment_surrounder_'+id).update(response.responseText);
    }
  });
}

/* + show next page of comments ------------------------------------------------ */
function next_comment_page (id, page, mode) {
  url = '/include/php/ajax_handler.php?action=next_comment_page&id='+id+'&page='+page+'&mode='+mode;
  new Ajax.Request(url, {
   method: 'get',
   encoding: 'ISO-8859-1',
   onComplete: function(response) {
     $('comment_surrounder_'+id).update(response.responseText);
   }
  });
}

/* + delete a state ------------------------------------------------------------ */
function delete_state (state_id) {
  url = '/include/php/ajax_handler.php?action=delete_state&id='+state_id;
  new Ajax.Request(url, {
   method: 'get',
   encoding: 'ISO-8859-1',
   onComplete: function(response) {
     $('state_surrounder_'+state_id).style.display = 'none';
   }
  });
}

/* + delete a comment ---------------------------------------------------------- */
function delete_comment (id, comment_id, mode) {
  url = '/include/php/ajax_handler.php?action=delete_comment&id='+id+'&comment_id='+comment_id+'&mode='+mode;
  new Ajax.Request(url, {
   method: 'get',
   encoding: 'ISO-8859-1',
   onComplete: function(response) {
     $('comment_surrounder_'+id).update(response.responseText);
   }
  });
}

/* + toggle status comments with icons ---------------------------------------- */
function toggle_comment_with_icon (status_id) {
  // toggle comment icon
  if ("minilink_kommentare" == $('comment_icon_'+status_id).className) {
    $('comment_icon_'+status_id).className = "minilink_kommentare background_bottom";
  } else {
    $('comment_icon_'+status_id).className = "minilink_kommentare";
  }
  // show comments
  $('comment_surrounder_'+status_id).toggle();
}

/* + toggle all status comments ----------------------------------------------- */
function toggle_all_status_comments () {
  var label_hide = 'Alle Kommentare einklappen';
  var label_show = 'Alle Kommentare ausklappen';
  var current_label = $('toggle_all_status_comments').innerHTML;
  // get all comment div surrounders
  if (current_label == label_hide) {
    $$('div.comment_surrounder').invoke('hide');
    $$('span.comment_icon a').each(function(element) {
      element.className = 'minilink_kommentare';
    });
    $('toggle_all_status_comments').innerHTML = label_show;
  } else {
    $$('div.comment_surrounder').invoke('show');
    $$('span.comment_icon a').each(function(element) {
      element.className = 'minilink_kommentare background_bottom';
    });
    $('toggle_all_status_comments').innerHTML = label_hide; 
  }
}

/* + add a funny smiley to an element ---------------------------------------- */
function add_a_smiley (element, smiley_number) {
  var smiley_text = '';
  switch (smiley_number) {
    case 1:
      smiley_text = ':-)';
      break;
    case 2:
      smiley_text = ':-1';
      break;
    case 3:
      smiley_text = ':-|';
      break;
    case 4:
      smiley_text = ':-(';
      break;
    case 5:
      smiley_text = ':-O';
      break;
    case 6:
      smiley_text = ':-D';
      break;
    default:
      break;
  }
  // Update element with the new smiley
  insert_at_cursor_position(element, '', ' '+smiley_text+' ');
}

/* + add text at the cursor position  -------------------------------------- */
function insert_at_cursor_position (element, aTag, eTag) {
  var input = $(element);
  input.focus();
  /* f¸r Internet Explorer */
  if (typeof document.selection != 'undefined') {
    /* Einf¸gen des Formatierungscodes */
    var range = document.selection.createRange();
    var insText = range.text;
    range.text = aTag + insText + eTag;
    /* Anpassen der Cursorposition */
    range = document.selection.createRange();
    if (insText.length == 0) {
      range.move('character', -eTag.length);
    } else {
      range.moveStart('character', aTag.length + insText.length + eTag.length);      
    }
    range.select();
  }
  /* f¸r neuere auf Gecko basierende Browser */
  else if (typeof input.selectionStart != 'undefined') {
    /* Einf¸gen des Formatierungscodes */
    var start = input.selectionStart;
    var end = input.selectionEnd;
    var insText = input.value.substring(start, end);
    input.value = input.value.substr(0, start) + aTag + insText + eTag + input.value.substr(end);
    /* Anpassen der Cursorposition */
    var pos;
    if (insText.length == 0) {
      pos = start + aTag.length;
    } else {
      pos = start + aTag.length + insText.length + eTag.length;
    }
    input.selectionStart = pos;
    input.selectionEnd = pos;
  }
}

/* + search member ---------------------------------------------------------- */
function search_member (search, mode) {
  url = '/include/php/ajax_handler.php?action=search_member&search='+search+'&mode='+mode;
  new Ajax.Request(url, {
   method: 'get',
   encoding: 'ISO-8859-1',
   onComplete: function(response) {
     $('search_member_results').update(response.responseText);
   }
  });
}

/* + On load events ------------------------------------------------------------ */
function highslide_settings () {
  // 9ba5cce8b740c154f15a824b5091a9ff (indual GmbH)
  // settings
  hs.graphicsDir = '/include/highslide/graphics/';
  hs.outlineType = null;
  hs.wrapperClassName = 'junior-wrapper';
  hs.width = 419;
  //hs.dimmingOpacity = 0.6;
  //hs.dimmingDuration = 0.5;
  hs.transitions = ['expand', 'crossfade'];
  hs.showCredits = false;
  
  hs.Expander.prototype.onAfterClose = function() {
    highslide_src = hs.getSrc(this.a);
	if ((0 < highslide_src.search('anzeigen')) || (0 < highslide_src.search('gaestebuch'))) {
	  window.location.reload();
	}
	return false;
  };
  
  hs.lang = {
   cssDirection:     'ltr',
   loadingText :     'Lade...',
   loadingTitle :    'Klick zum Abbrechen',
   focusTitle :      'Klick um nach vorn zu bringen',
   fullExpandTitle : 'Zur Originalgrösse erweitern',
   fullExpandText :  'Vollbild',
   creditsText :     'Powered by <i>Highslide JS</i>',
   creditsTitle :    'Gehe zur Highslide JS Homepage',
   previousText :    'Voriges',
   previousTitle :   'Voriges (Pfeiltaste links)',
   nextText :        'Nächstes',
   nextTitle :       'Nächstes (Pfeiltaste rechts)',
   moveTitle :       'Verschieben',
   moveText :        'Verschieben',
   closeText :       'Schliessen',
   closeTitle :      'Schliessen (Esc)',
   resizeTitle :     'Grösse wiederherstellen',
   playText :        'Abspielen',
   playTitle :       'Slideshow abspielen (Leertaste)',
   pauseText :       'Pause',
   pauseTitle :      'Pausiere Slideshow (Leertaste)',
   number :          'Bild %1/%2',
   restoreTitle :    'Klick um das Bild zu schliessen, klick und ziehe um zu verschieben. Benutze Pfeiltasten für vor und zurück.',
   expandCursor :    null,
   restoreCursor :   null
  };
}

/* + close popups -------------------------------------------------------------- */
function close_window () {
	if (window.opener && !window.opener.closed) {
		//window.opener.document.location.reload();
	}
	self.close();
}

// + Ratings ------------------------------------------------------------------- */
function animateRating(jokeId, benchmarkValue) {
	// Animate current benchmark line
	var image;
	for (i=1 ; i<=5 ; i++) {
		image = document.getElementById('jb_'+jokeId+'_'+i);
		if (image) {
			if (benchmarkValue >= i) image.src = '/include/images/krone_gelb.png';
			else image.src = '/include/images/krone_weiss.png';
		}
	}
}

/* + update slider xml --------------------------------------------------------- */
function update_slider_xml () {
  url = '/slider.php';
  new Ajax.Request(url, {
   method: 'get',
   onComplete: function(response) {
     $('result_slider_xml_update').update(response.responseText);
   }
  });
}

//+ check for new user messages ------------------------------------------------ */
function check_new_messages () {
	new Ajax.Request('/include/php/ajax_handler.php?action=check_new_messages', {
		onSuccess: function(result) {
			var message_id = parseInt(result.responseText);
			if (0 < message_id) {
				window.open('/JUNIORclub/nachrichten/anzeigen.php?action=popup&message_id='+message_id,'nachrichtAnzeigen'+message_id,'toolbar=no,screenX=100,screenY=100,width=419,height=600,scrollbars=now,resizable=yes');
			}
			/*
			if (null == hs.getExpander()) {
				if (0 < message_id) {
					hs.Expander.prototype.onAfterClose = function() {
					  window.location.reload();
					};
					hs.htmlExpand(null, {
						src: '/JUNIORclub/nachrichten/anzeigen.php?message_id='+message_id,
						objectType: 'iframe',
						dimmingOpacity: 0.6, 
						dimmingDuration: 0.5
					});
				}
			}
			*/
		}
	});
}


//+ check for new junior messages ------------------------------------------------ */
function check_new_junior_messages () {
	new Ajax.Request('/include/php/ajax_handler.php?action=check_new_junior_messages', {
		onSuccess: function(result) {
			var message_id = parseInt(result.responseText);
			if (null == hs.getExpander()) {
				if (0 < message_id) {
					hs.Expander.prototype.onAfterClose = function() {
					  window.location.reload();
					};
					hs.htmlExpand(null, {
						src: '/JUNIORclub/nachrichten/anzeigen_redaktion.php?message_id='+message_id+'&state=p',
						objectType: 'iframe',
						dimmingOpacity: 0.6, 
						dimmingDuration: 0.5
					});
				}
			}
		}
	});
}


function show_club_register_form () {
	var isIE6 = ((navigator.userAgent.indexOf("MSIE 6.") != -1) && (navigator.userAgent.indexOf("Opera") == -1));
	if (isIE6) {
		window.location.href = '/JUNIORclub/registrieren/index.php';
	} else if ($('registrieren')) {
		Effect.BlindDown('registrieren', { duration: 1.0 });
	}
}


function close_club_register_form () {
	if ($('registrieren')) {
		Effect.BlindUp('registrieren', { duration: 1.0 });
	}
}


// + Misc functions ------------------------------------------------------------ */
function layerclose() {
	document.getElementById('lwrot').style.display='none';
	document.getElementById('anderesflash').style.display='block';
	setCookie("layerclose",1,1);
}
function setCookie(c_name,value,expiredays){
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie=c_name+ "=" +escape(value)+
	((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}
function blurField(id, label) {
	field = document.getElementById(id);
	if (field) {
		field.onblur = function() { alert('blur: '+label); if (field.value == '') field.value = label; }
		field.onfocus = function() { alert('focus: '+field.value); if (field.value == label) field.value = '';	}
	}
}
function toggleComments(object_id) {
	if (document.getElementById('comments_of_object_'+object_id).style.display == 'none') {
		// show it!
		document.getElementById('comments_of_object_'+object_id).style.display = 'block';
		document.getElementById('kommentarformular'+object_id).style.display = 'block';
	} else {
		// hide it!
		document.getElementById('comments_of_object_'+object_id).style.display = 'none';
		document.getElementById('kommentarformular'+object_id).style.display = 'none';
	}
}