$(document).ready(function(){

    Date.format = 'dd.mm.yyyy';

    if($("#adminnavi").length==0) {
		$("a.partialLoad").live('click', function() {
	       var href = $(this).attr('href');
	       var container = $(this).parents(".partial");
	       container.find(".block").fadeOut("fast", function() {
	           container.load(href, function() {
	               container.find(".block").hide().fadeIn("fast");
	               displayMessages();
	           });
	       });
       
	       return false;
	   });

	   $("form.partialLoad").live('submit', function() {
	       var form = $(this);
	       var href = form.attr('action');
	       var container = form.parents(".partial");


	       container.find(".block").fadeOut("fast", function() {
	           $.post(href, form.serialize(), function(result) {
	               container.html(result).find(".block").hide().fadeIn("fast");
	               displayMessages();
	           });
	       });
       
	       return false;
	   });
	}

    function displayMessages() {
        $(".messages").hide();
        $(".messages ul li").each(function () {
            var cclassName = '';
            if($(this).hasClass('error') || $(this).hasClass('warning')) {
                cclassName = 'achtungFail';
            }
            if($(this).hasClass('success')) {
                cclassName = 'achtungSuccess';
            }
            
            $.achtung({message: $(this).text(), className: cclassName});
        });
    }
    displayMessages();

    $('a.confirm').live('click', function(e) {
		e.preventDefault();
        var msg = $(this).attr('rel');
        var href = $(this).attr('href');
        jqDialog.confirm(msg,
            function() {top.location.href = href},
            function() {return false;}
        );
        return false;
    });

    $('.tags').autocomplete('/request/tags', {
		multiple: true,
		multipleSeparator: ", ",
		scroll: true,
		scrollHeight: 300
	});
	
	$(".sendform").click(function() {
	    var form = $(this).closest("form");
	    form.hide();
	    form.submit();
	});
	
	$(".trform .sendform").click(function(e) {
	    e.preventDefault();

	    var form = $(this).closest(".trform");
	    var newform = $('<form></form>');
	    
	    var real = $(":file",form);
	    var cloned = real.clone();
	    real.hide();
	    cloned.insertAfter(real);
	    
	    newform.attr("method",$(":input[name=method]",form).val())
	           .attr("action",$(":input[name=action]",form).val())
	           .attr("enctype",$(":input[name=enctype]",form).val())
               .attr("id",'tempform')
	           .append($(":input[type!=file]",form).clone())
	           .append(real);

		$("select",newform).each(function() {
			var val = $("select[name="+$(this).attr('name')+"] option:selected",form).val();
			$(this).find('option[value='+val+']').attr('selected','selected');
		});
        $('body').append(newform);
        $('#tempform').submit();
		
	});

    $(".forum tr .go").click(function() {
       var loc = $(this).find("a").attr("href");
       top.location.href = loc;
    });

    $('.quote').click(function() {
       var toQuote = $(this).parent().parent().find('.text').html();
       toQuote = toQuote.replace(/<br>/g, '');
       $("#writecomment").prepend(blockquoteConvert2('<div><blockquote>' + jQuery.trim(toQuote) + '</blockquote></div>')).focus();
    });

    function blockquoteConvert2(html) {
        var html = $(html);
        while($('blockquote',html).length>0) {
            $('blockquote',html).each(function() {
                if($('blockquote',$(this)).length==0) {
                    var newcontent = '>> ' + $(this).text().replace(/\n/g,'\n>> ');
                    $(this).after(newcontent).remove();
                }
            });
        }
        
        return html.html();
    }

    $('.userlink').tooltip({
        track: true,
        delay: 0,
        showURL: false,
        bodyHandler: function() {
            var content = '';
            $.ajax({
                url: "/request/shortprofile/id/" + $(this).attr('id'),
                success: function(data){
                    content = data;
                },
                async:false
            });
            return content;

        },
        fade: 100
    });
    if($('#cropbox').length>0) {
    $('#cropbox').Jcrop({
					setSelect: [ 100, 100, 0, 0 ],
                    aspectRatio: 1,
                    onSelect: updateCoords
	});
    }

    /*$('.wysiwyg').tinymce({
        // Location of TinyMCE script
        script_url : '/media/javascript/library/tiny_mce/tiny_mce.js',

        // General options
        theme : "advanced",
        plugins : "safari,advhr,advimage,advlink,contextmenu,xhtmlxtras,inlinepopups",

        // Theme options
        theme_advanced_buttons1 : "source,bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,fontsizeselect,|,bullist,numlist,blockquote,|,undo,redo,|,link,unlink,image,|,forecolor,backcolor",
        theme_advanced_buttons2 : "",
        theme_advanced_buttons3 : "",
        theme_advanced_buttons4 : "",
        theme_advanced_toolbar_location : "top",
        theme_advanced_toolbar_align : "center",
        theme_advanced_statusbar_location : "none",
        theme_advanced_resizing : true,

        // Example content CSS (should be your site CSS)
        content_css : "css/content.css",

        // Replace values for the template plugin
        apply_source_formatting : false
    });*/

    $('.date').datePicker({
        clickInput: true,
        createButton: false,
        startDate:'01/01/2007'
    });
});
function updateCoords(c)
    {
        $('#x').val(c.x);
        $('#y').val(c.y);
        $('#w').val(c.w);
        $('#h').val(c.h);
    }

    function checkCoords()
    {
        if (parseInt($('#w').val())) return true;
        alert('Bitte wähle den Bildausschnitt aus!');
        return false;
    }
function Toggle(id, multi) {

  spanid1 = "ToggleRow_"+id;
  spanid2 = "ToggleImg_"+id;

  if(multi == true) {

	elements = document.getElementsByName(spanid1);
	val = document.getElementsByName(spanid1)[0].style.display;
	for (var i=0;i<elements.length;i++) {
	  if (val == "none") elements[i].style.display = "inline";
	  else elements[i].style.display = "none";
	}
  }
  else {

	  if (/MSIE/.test(navigator.userAgent)){
	  	images = document.getElementById(spanid1).getElementsByTagName('img');
	    anz = images.length
	    for(i=0;i<anz;i++){
	    	elem = images[i];
	        if(typeof elem.onload == "function") elem.onload();
	    }
	  }
	  val = document.getElementById(spanid1).style.display;
	  if (val == "none")
	  {
	    document.getElementById(spanid1).style.display = "block";
	    document.getElementById(spanid2).src = "/media/images/icons/collapse.gif";
	    document.getElementById(spanid2).alt = "hide";
		 document.getElementById(spanid2).title = "hide";
	  }
	  else
	  {
	    document.getElementById(spanid1).style.display = "none";
	    document.getElementById(spanid2).src = "/media/images/icons/expand.gif"
	    document.getElementById(spanid2).alt = "read more";
		 document.getElementById(spanid2).title = "read more";
	  }
  }
}
