var curvyCornersVerbose = false;
function insertAtCaret(obj, text) {
        if(document.selection) {
                obj.focus();
                var orig = obj.value.replace(/\r\n/g, "\n");
                var range = document.selection.createRange();

                if(range.parentElement() != obj) {
                        return false;
                }

                range.text = text;

                var actual = tmp = obj.value.replace(/\r\n/g, "\n");

                for(var diff = 0; diff < orig.length; diff++) {
                        if(orig.charAt(diff) != actual.charAt(diff)) break;
                }

                for(var index = 0, start = 0;
                        tmp.match(text)
                                && (tmp = tmp.replace(text, ""))
                                && index <= diff;
                        index = start + text.length
                ) {
                        start = actual.indexOf(text, index);
                }
        } else if(obj.selectionStart) {
                var start = obj.selectionStart;
                var end   = obj.selectionEnd;

                obj.value = obj.value.substr(0, start)
                        + text
                        + obj.value.substr(end, obj.value.length);
        }

        if(start != null) {
                setCaretTo(obj, start + text.length);
        } else {
                obj.value += text;
        }
}

function setCaretTo(obj, pos) {
        if(obj.createTextRange) {
                var range = obj.createTextRange();
                range.move('character', pos);
                range.select();
        } else if(obj.selectionStart) {
                obj.focus();
                obj.setSelectionRange(pos, pos);
        }
}
function addTextileLink(elementID) {
  a = prompt("Hva er adressen?", "http://")
  if (a == null || a == '') { return; }
  n = prompt("Hva skal navnet på lenken være?")
  if (n == null || n == '') { return; }
  insertAtCaret($(elementID), String.concat('"',n,'":',a))
}

$(function() {
  // Datepicker
  $('.datepicker').datepicker({
    changeMonth: true,
    changeYear: true,
    yearRange: '-100:+0'
  });

  // WYSIWYG editor
  $('.wweditor').wysiwyg({ css : { fontFamily: 'verdana, arial, helvetica, sans-serif', fontSize : '12px'} });

  // Facebox
  $('a[rel*=facebox]').facebox();
  $(document).bind('reveal.facebox', function() {
    $("#facebox .content :input:visible:first").focus()
  });

  // inline form field labels

  $('input[title]').each(function() {
	    if($(this).val() === '') {
	        $(this).addClass("inline_label");
		    $(this).val($(this).attr('title'));
      }
		$(this).focus(function() {
			if($(this).val() === $(this).attr('title')) {
				$(this).val('').removeClass('inline_label');
			}
		}); 
		$(this).blur(function() {
			if($(this).val() === '') {
				$(this).val($(this).attr('title')).addClass('inline_label');
			}
		});       
  });

  // Focus hack
  var is_focus = $('.focus:first');
  if (is_focus.size() > 0) {
    is_focus.focus();
  }
  else {
    $(":input:not(.nofocus):visible:first").focus();
  }

  // Shopping cart
  $('.cart_action').live('click', function() {
    $.get(this.href, null, null, 'script');
    return false;
  });
    
    
  
        
  /* Rounded corners on public navigation
  if ($.fn.corner) {
    $('.rounded').corner();
    $('.rounded_bottom_left').corner("rounded bl");
    $('.rounded_bottom_right').corner("rounded br");
  }*/
});

