function printStackTrace() {
  var callstack = [];
  var isCallstackPopulated = false;
  try {
    i.dont.exist+=0; //doesn't exist- that's the point
  } catch(e) {
    if (e.stack) { //Firefox
      var lines = e.stack.split('\n');
      for (var i=0, len=lines.length; i<len; i++) {
        if (lines[i].match(/^\s*[A-Za-z0-9\-_\$]+\(/)) {
          callstack.push(lines[i]);
        }
      }
      //Remove call to printStackTrace()
      callstack.shift();
      isCallstackPopulated = true;
    }
    else if (window.opera && e.message) { //Opera
      var lines = e.message.split('\n');
      for (var i=0, len=lines.length; i<len; i++) {
        if (lines[i].match(/^\s*[A-Za-z0-9\-_\$]+\(/)) {
          var entry = lines[i];
          //Append next line also since it has the file info
          if (lines[i+1]) {
            entry += " at " + lines[i+1];
            i++;
          }
          callstack.push(entry);
        }
      }
      //Remove call to printStackTrace()
      callstack.shift();
      isCallstackPopulated = true;
    }
  }
  if (!isCallstackPopulated) { //IE and Safari
    var currentFunction = arguments.callee.caller;
    while (currentFunction) {
      var fn = currentFunction.toString();
      var fname = fn || 'anonymous';
      callstack.push(fname);
      currentFunction = currentFunction.caller;
    }
  }
  output(callstack);
}

function output(arr) {
  //Optput however you want
  alert(arr.join('\n\n'));
}


function showDetailedImage(imagePath){
	$("#overlay").fadeIn(500);
	$(".overlayContent").html('<img src="' + imagePath + '" alt = "'+js_image+'" />');
}

//jcrop instance
var jcrop = null;
function setImage(file, width, height, coordinates)
{		
	if (jcrop !== null) {
		jcrop.destroy();
		$('#image').unbind('load');
	} else {
		$('#imageupload').hide();
		$('#showcase').hide();
		$('#imagecrop').show('slow');
		$('.description').hide();
		$('body').removeClass('indexPage');
	}
	
	
	path = originalThumbPath + file + extension;
	
	$('#image').attr('src', path);

	$('#image').load(function() {
	    $(this).removeAttr("width").removeAttr("height");
	    		
		w = this.width;
		h = this.height;

		if (coordinates.length != 4){
			coordinates = [ 20, 30, w-10, h-60 ]
		}
		
		//se preveri, ce slika ni v 4/3 razmerju
		var minCropWidthPic		= Math.round(w / (width / minCropWidth));
		var minCropHeightPic	= Math.round(h / (height / minCropHeight));
		
		var options = {
				        onSelect:		saveCoords,
				        onChange:		saveCoords,
				        bgColor:		'black',
				        bgOpacity:		.4,
				        setSelect:		coordinates,
				        minSize:		[minCropWidthPic, minCropHeightPic]
				      };
		
		jcrop = $.Jcrop('#image', options);
		
	});
	
}

function saveCoords(c)
{
	x1=c.x;
	y1=c.y;
	x2=c.x2;
	y2=c.y2;
	getSimplePoints();
	
}

function getSimplePoints(){
	ps=x1+"_"+y1+","+x2+"_"+y2;	
	$('#points').val(ps);
}

function submitCrop()
{
	$('#ajaxCropLoader').show();
	$('#imagecrop').submit();
}

$(document).ready(function() {  

	new AjaxUpload('uploadOther', {
					action: '/ajax/upload/',
					autoSubmit: true,
					responseType: 'json',
					onChange: function(file, extension){
						$('#ajaxCropLoader').show().find('.loading').html(js_uploading+'<br/>'+js_new_image);
					},
					onComplete: function(file, response) {							
						if (!response.error) {
							var imagePath = response.image;
							var imageWidth = response.imageWidth;
							var imageHeight = response.imageHeight;

							setImage(imagePath, imageWidth, imageHeight, []);
							
							$('#ajaxCropLoader').hide().find('.loading').html(js_cropping+'<br/>'+js_image_dot);
						}
						
					}
	});

}); 
