/**
  * ein paar funktionen, die ausgefuehrt werden muessen, wenn jq geladen wurde
  */

$(document).ready(function(){
  /* DROPSHADOW
  var opt = {
      left: 2,
      top: 2,
      blur: 2,
      opacity: .3,
      color: "black",
      swap: false
  };
  */
//  $('.shadow').dropShadow(opt);
  $("a[href^=#]").each(function(){
    $(this).click(function(){
      var $href = $(this).attr("href");
      $href = $href.split("#");
      window.location.hash = "#" + $href[1];
      return false;
    });
  });
	/* WETTER-DATEN-SLIDER */
	if(typeof $(".vticker").jCarouselLite == 'function') {
		$(".vticker").jCarouselLite(
		{
			vertical: true,
			visible: 3,
			auto: 1000,
			speed: 1000
		});
	}
	/* PNG-FIX */
	if (typeof $(document).pngFix == 'function')
	{
		$(document).pngFix(	{ blankgif:'../../assets/common/z.gif' } );
	}
	/* CM-BILDERGALERIE */
//	$('.galerie_v2 ul').css('display','none');
	if (typeof $('.galerie_v2 a').lightBox == 'function')
	{
		$('.galerie_v2 a').lightBox({fixedNavigation:true});
	}
	/* TOOLTIPS */
	if (typeof $('label').tooltip == 'function')
	{
		$('label').tooltip({extraClass: "tt_superz"});
	}
	/* UPLOAD */
	if (typeof $("#swfupload-control").swfupload == 'function')	{
		handle_upload();
	}
	// accordion gis
	if(typeof $("#map_sidebar").accordion == 'function') 	{
		$("#map_sidebar").accordion(
			{
				header: 'h3.tab',
				autoHeight: false
			}
		);
	}

});


/*fuerwebcam*/
function updateCams(target)
{
  $(target).each(function(){
//    var img_src = $(this).attr('src'); // einfach das gleiche bild nehmen, wenn eh imme nur ein dateiname
//    var the_child = $(this).children('img:first');
    var the_child = $(this).find('img');
    var the_elem = $(this);

    var mysrc = the_child.attr('src');

    var wcd = '';
    var w = the_child.css('width');
    if (w == '510px')
    {
      wcd = '&wcd=1';
    }

    var folder = $.url.setUrl(mysrc).attr("directory");
    folder = folder.replace(/\/fellbach.de\//,'');
    var ts = new Date().getTime();
    if (0)
    {
//      $(this).load("../../content_scripts/ajax_webcam_camonpage.php?wcdir="+folder+wcd,'',function(data){});
    }
    else
    {
      $.get("../../content_scripts/ajax_webcam_camonpage.php?wcdir="+folder+wcd,'',function(data){
        $('<img />')
        .attr('src', data)
        .attr('class', 'webcam')
        .load(function(test){
            $(the_elem).empty().append( $(this) );
            // Your other custom code
        });
      });
    // Insert preloaded image after it finishes loading
    }
  });
}

function toggleCB(mclass)
{
  // erste passende cb holen und an der den akulellen status ermitteln, ob also alle checked oder nicht checked
  var the_checkbox = $(document).find(mclass);
  var status_new = !the_checkbox.attr('checked');
    $(mclass).attr('checked',status_new);

  // wenn tbaum dann auch noch alle marker togglen
  if (mclass=='.tbaum')
  {
    $('input' + mclass).each(function()
    {
      var type = $(this).val();
      var zustand = $(this).attr("checked");
      toggleMarkersByType(type,zustand);
    });
  }
}

function check(id)
{
  $('#' + id).attr('checked','checked');
}

function handle_upload()
{
  $("#htmlupload-wrapper").css("display","none");
  $("#swfupload-wrapper").css("display","block");
  var idu = $("#id_upload").val();
  $("#swfupload-control").swfupload({
  upload_url: "http://web.werbeagentur-aufwind.com/fellbach.de/content_scripts/fb_bg_ajax_upload.php?id_upload="+idu,
  file_size_limit : "200240",
  file_types : "*.jpg;*.zip", // *.tiff;*.tif;*.png;
  file_types_description : "All Files",
  file_upload_limit : "0",
  flash_url : "http://web.werbeagentur-aufwind.com/fellbach.de/assets/flash/swfupload.swf",
  button_image_url : "http://web.werbeagentur-aufwind.com/fellbach.de/assets/common/btn_datei_auswaehlen.gif",
  button_width : 177,
  button_height : 20,
  button_placeholder : $("#button")[0],
  debug: false,
  custom_settings : {something : "here"}
  })
  .bind("swfuploadLoaded", function(event){
    $("#swfupload_status").fadeIn("slow").html("Upload ist bereit");
  })
  .bind("fileQueued", function(event, file){
    $("#swfupload_status").fadeIn("slow").html("Neue Datei in Warteschlange: "+file.name+"");
  // start the upload since it iss queued
  $(this).swfupload("startUpload");
  })
  .bind("fileQueueError", function(event, file, errorCode, message){
  })
  .bind("fileDialogStart", function(event){
  })
  .bind("fileDialogComplete", function(event, numFilesSelected, numFilesQueued){
  })
  .bind("uploadStart", function(event, file){
  })
  .bind("uploadProgress", function(event, file, bytesLoaded){
    var prozent = parseInt(bytesLoaded / file.size * 100);
  $("#swfupload_status").fadeIn("slow").html("Fortschritt: "+prozent+" %");
  })
  .bind("uploadSuccess", function(event, file, serverData){
  //  $("#swfupload_status").fadeIn("slow").html("Datei wurde hochgeladen: "+file.name+"");
  //  $("#swfupload_status").fadeIn("slow").html(serverData);
    var arr_data = eval(serverData);
    $("#swfupload_status").fadeIn("slow").html(arr_data.msg);
    if (arr_data.status>0)
    {
      var anzahl = parseInt($("#swfupload_anzahl").val());
      anzahl = parseInt(arr_data.status) + parseInt(anzahl);
      $("#swfupload_anzahl").val(anzahl);
      var endung = "en";
      if (anzahl == 1)
        endung = "";
      $("#swfupload_status_anzahl").fadeIn("slow").html(anzahl + " Datei" + endung + " hochgeladen");
    }
  })
  .bind("uploadComplete", function(event, file){
    $(this).swfupload("startUpload");
  })
  .bind("uploadError", function(event, file, errorCode, message){
  });
}

function animate_aw_balken()
{
  $(".aw_balken").each(function(){
    // width zwischenspeichern, 0 setzen und hinanimieren
    var $width = $(this).css('width');
    $(this).css('width','0px');
    $(this).animate({
        width: $width
      }, 1500 );
  });
}