//jQuery.noConflict();

function popup(id, frame){
   url=base+"/eintrag/"+id+"?popup=1";

	// achtung - url nur in ankündigungsphase gültig
	//url="/handbuch-bk/karte/forumview/"+id+"?popup=1";
	//url="http://wieweiterwohnen.de/07/aktion/"+id+"?popup=1";
	
	//$.fn.colorbox({href:url, open:true, innerWidth:700});
   window.open(url, frame,'width=830,height=830,toolbar=no,menubar=no,status=yes,resizable=yes,scrollbars=yes,left=60,top=60').
      focus();
    //return false;

}

function popup_doku(id, frame){
	
   url=base+"/karte/viewdoku/"+id+"?popup=1";
	//$.fn.colorbox({href:url, open:true, innerWidth:700});
	window.open(url, frame,'width=830,height=830,toolbar=no,menubar=no,status=yes,resizable=yes,scrollbars=yes,left=60,top=60').
	    focus(); 
}

/**/
jQuery.fn.toggleDefault = function(focusClass) {
	this.each(function() {
		$(this).each(function(){
		  // init
		  if($(this).val() == "") { $(this).val($(this).attr("default"));}
		  if($(this).val() == $(this).attr("default")) { $(this).addClass("default"); }
		}).focus(function(){
			// clear value if current value is the default
			if($(this).val() == $(this).attr("default")) { 
			  $(this).val("");
			  $(this).removeClass("default");
			}
		}).blur(function() {
			// restore to the default value if current value is empty
			if($(this).val() == "") { 
			  $(this).val($(this).attr("default")); 
			  $(this).addClass("default"); 
			}

		});
	});
}


/*
 * TextLimit - jQuery plugin for counting and limiting characters for input and textarea fields
 * 
 * pass '-1' as speed if you don't want slow char deletion effect. (don't just put 0)
 * Example: jQuery("Textarea").textlimit('span.counter',256)
 *
 * $Version: 2007.10.24 +r1
 * Copyright (c) 2007 Yair Even-Or
 * vsync.design@gmail.com
 */
jQuery.fn.textlimit=function(counter_el, thelimit, speed) {
	var charDelSpeed = speed || 15;
	var toggleCharDel = speed != -1;
	var toggleTrim = true;
	
	that = this[0];
	updateCounter();
	
	function updateCounter(){
		jQuery(counter_el).text(thelimit - that.value.length);
	};
	
	this.keypress (function(e){ if( this.value.length >= thelimit && e.charCode != '0' ) e.preventDefault() })
	.keyup (function(e){
		updateCounter();
		if( this.value.length >= thelimit && toggleTrim ){
			if(toggleCharDel){
				// first, trim the text a bit so the char trimming won't take forever
				that.value = that.value.substr(0,thelimit+100);
				var init = setInterval
					( 
						function(){ 
							if( that.value.length <= thelimit){ init = clearInterval(init); updateCounter() }
							else{ that.value = that.value.substring(0,that.value.length-1); jQuery(counter_el).text('trimming...  '+(thelimit - that.value.length)); };
						} ,charDelSpeed 
					);
			}
			else this.value = that.value.substr(0,thelimit);
		}
	});
	
};

/**
 *
 * Copyright (c) 2007 Tom Deater (http://www.tomdeater.com)
 * Licensed under the MIT License:
 * http://www.opensource.org/licenses/mit-license.php
 *
 */

(function($) {
       /**
        * attaches a character counter to each textarea element in the jQuery object
        * usage: $("#myTextArea").charCounter(max, settings);
        */

       $.fn.charCounter = function(max, settings) {
               max = max || 100;
               settings = $.extend({
                       container: "<span>",
                       classname: "charcounter",
                       format: "(%1 characters remaining)",
                       pulse: true
               }, settings);
               var p;

               function count(el, container) {
                       el = $(el);
                       if (el.val().length > max) {
                        //  NICHT abschneiden!
                       //    el.val(el.val().substring(0, max));
                           if (settings.pulse && !p) {
                               pulse(container, true);
                           };
                       };
                       container.html(settings.format.replace(/%1/, (max - el.val().length)));
               };

               function pulse(el, again) {
                       if (p) {
                               window.clearTimeout(p);
                               p = null;
                       };
                       el.animate({ opacity: 0.1 }, 100, function() {
                               $(this).animate({ opacity: 1.0 }, 100);
                       });
                       if (again) {
                               p = window.setTimeout(function() { pulse(el) }, 200);
                       };
               };

               return this.each(function() {
                       var container = (!settings.container.match(/^<.+>$/))
                               ? $(settings.container)
                               : $(settings.container)
                                       .insertBefore(this)
                                       .addClass(settings.classname);
                       $(this)
                               .bind("keydown", function() { count(this, container); })
                               .bind("keypress", function() { count(this, container); })
                               .bind("keyup", function() { count(this, container); })
                               .bind("focus", function() { count(this, container); })
                               .bind("mouseover", function() { count(this, container); })
                               .bind("mouseout", function() { count(this, container); })
                               .bind("paste", function() {
                                       var me = this;
                                       setTimeout(function() { count(me, container); }, 10);
                               });
                       if (this.addEventListener) {
                               this.addEventListener('input', function() { count(this, container); }, false);
                       };
                       count(this, container);
               });
       };

})(jQuery);

$(document).ready(function(){
	$("#switch_allestaedte").mouseover(function () {
	      $("#startpagecontent").css("background-image","url(gfx/karte_schlagwoerter_allestaedte_mitmachen.jpg)");
	});
	$("#switch_hauptstaedte").mouseover(function () {
	      $("#startpagecontent").css("background-image","url(gfx/karte_schlagwoerter_hauptstaedte_mitmachen.jpg)");
	});
	$("#switch_ohnestaedte").mouseover(function () {
	      $("#startpagecontent").css("background-image","url(gfx/karte_schlagwoerter_ohnestaedte_mitmachen.jpg)");
	});
	
	$("#postit").draggable();

	

  	$("#keywordinput").toggleDefault();
//  $("#profile").textlimit("#profilecount", 350);
  //$("#profile").charCounter(300, {container: "#profilecount", format: "noch %1 Zeichen"});
  //$("#profile2").charCounter(800, {container: "#profile2count", format: "noch %1 Zeichen"});

	
	$('.date-pick')
		.datePicker(
			{
				//createButton:true,
				startDate:'01/02/2010',
				endDate:'31/12/2010',
				displayClose:true,
				closeOnSelect:false,
				selectMultiple:true,
				inline:true
			}
		)
		.bind(
			'click',
			function()
			{
				$(this).dpDisplay();
				this.blur();
				return false;
			}
		)
		.bind(
			'dateSelected',
			function(e, selectedDate, $td, state)
			{
				//console.log($(this).selectedDates);
				var seldates = $(this).dpGetSelected();
				var val_seldates = $(this).dpGetSelected().join(";");
				
				$("#selecteddates").val( val_seldates );
				//$("input[name$='finder[selecteddates]']").val(val_seldates);
				//console.log($(this).dpGetSelected());
				//$("#filterform").submit();
				
				//alert($("#selecteddates").val());
			}
		)
		.bind(
			'dpClosed',
			function(e, selectedDates)
			{
				//console.log('You closed the date picker and the ' // wrap
				//	+ 'currently selected dates are:');
				//console.log(selectedDates);
				
			}
		);
		
	


	$("#filterform td.cat label").bind(
        'click',
	   function(e)
  		{
			if($(this).hasClass("active")) {
				
				//falls vorher bereits ausgewählt, deaktivieren
				$('#no_category').attr('checked', 'checked');
				$(this).removeClass("active");
				$("#filterform").submit();
				return false;
				
			} else {
				
				$("#filterform td.cat label").removeClass('active');
				$(this).addClass("active");
		
			}
			

  		}
  	);

	// elemente mit class 'submitter' schicken bei änderung des wertes das formular ab
	$("#filterform .submitter, #ageselect").bind(
		'change',
		function(e){
			$("#filterform").submit();
			return false;
		}
	);
		
				
 });



/* This utility function resolves the string movieName to a Flash object reference based on browser type. */
function getElementbyName(elementname) {
	alert("getElementbyName("+elementname+")");
  	if (navigator.appName.indexOf("Microsoft") != -1) {
  	return window[elementname]
  	}
  	else {
  		return document[elementname]
  	}
 }





