

var active_img;
var active_element;
var crop_settings = new Array();

function set_active_img(itemid, elementid)
{
	active_img = itemid;
	active_element = elementid;
}

function init_jcrop()
{
	get_crops();
	
	//alert(navigator.platform);
	
	
	var img_arr = jQuery(".cropme");
	var posterformat = jQuery(".cropformats");
	var img_width = jQuery(".cropwhatx");
	var img_height = jQuery(".cropwhaty");
	
	var regex = /A\d \((.*)x(.*)\)/;
	
	for(var i=0;i < posterformat.length; i++)
	{
		if(regex.exec(posterformat[i].value)!= null)
		{
			mystring = posterformat[i].value;
			values = mystring.match(regex);
			mynewstring = values[1] + "x" + values[2];
			posterformat[i].value = mynewstring.replace(/,/g,".");
		}
	}
	
	for(var i = 0;i < img_arr.length;i++)
	{
		if(img_arr[i].id != undefined)
		{
			
			format = posterformat[i].value.split("x");
			ratio = format[0] / format[1];
			ratio_manip=false;
			
			if(img_arr[i].height/img_arr[i].width > 1)
			{
				ratio=Math.pow(ratio,-1);
				ratio_manip=true;
			}
			
			add_to_crop(img_arr[i].id, img_width[i].value, img_height[i].value, ratio);
			
			//os=navigator.platform;
			//if(os=='MacIntel' ||os=='MacPPC')
			

		}
	}
}

function add_to_crop(id, width, height, ratio)
{

	var sel = getRectSizeInContainerRect(ratio, width, height);	
	jcrop_x = sel.x;
	jcrop_y = sel.y;
	
	if(get_browser()=='FF2')
	{
		jcrop_x = getAlternateRectSizeInContainerRect(ratio, width, height, 'x');
		jcrop_y = getAlternateRectSizeInContainerRect(ratio, width, height, 'y');
	}
	
	
	
	aposter = jQuery("#"+id).parent().attr('id');
	aposter = aposter.split("@@@");
	
	for(var i =0;i<crop_settings.length;i++)
	{
		tsplit 		= crop_settings[i].split("@@@");
		posterid 	= tsplit[0];
		tmsplit		= tsplit[1].split("x");
		sett_x		= tmsplit[0];
		sett_y		= tmsplit[1];
		
		//alert(posterid + "=>" + sett_x+"x"+sett_y);
		if(aposter[1]==posterid)
		{
				jcrop_x=parseInt(sett_x);
				jcrop_y=parseInt(sett_y);
				//alert(sett_x + " y: " + sett_y);
		}
	}
	
	if(get_browser()!='FF2')
	{
		jQuery("#"+id).Jcrop({
			setSelect: [jcrop_x, jcrop_y, jcrop_x+sel.width, jcrop_y+sel.height],
			onSelect: updateCropping,
			allowResize: false,
			minSize: [sel.width, sel.height],
			maxSize: [sel.width, sel.height]
	
		});
	}
	else
	{
		sel_width = getAlternateRectSizeInContainerRect(ratio, width, height, 'width');
		sel_height = getAlternateRectSizeInContainerRect(ratio, width, height, 'height');
		jQuery("#"+id).Jcrop({
			setSelect: [jcrop_x, jcrop_y, jcrop_x+sel_width, jcrop_y+sel_height],
			onSelect: updateCropping,
			allowResize: false,
			minSize: [sel_width, sel_height],
			maxSize: [sel_width, sel_height]
	
		});

	}
	
}

function reset_cropping(id)
{

	jQuery.ajax({
		dataType	: 'json',
		async		: false,
		url			: "/aktion/removecrop/"+id
	});
}

function getRectSizeInContainerRect (ratio, ctWidth, ctHeight) 
{
	var ctRatio = ctWidth/ctHeight;
	var newHeight = 0;
	var newWidth = 0;
	
	
	if (ratio < ctRatio) {				
		newHeight = ctHeight;
		newWidth = (newHeight * ratio);
	} else {			
		newWidth = ctWidth;
		newHeight = (newWidth / ratio);				
	}
	//finally we can resize!
	var xShift = (ctWidth - newWidth)/2;
	var yShift = (ctHeight - newHeight)/2;
	
	var res = {};
	res['width'] = newWidth;
	res['height'] = newHeight;
	res['x'] = xShift;
	res['y'] = yShift;
	return res;
}

function getAlternateRectSizeInContainerRect (ratio, ctWidth, ctHeight, what) 
{
	var ctRatio = ctWidth/ctHeight;
	var newHeight = 0;
	var newWidth = 0;
	if (ratio < ctRatio) {				
		newHeight = ctHeight;
		newWidth = (newHeight * ratio);
	} else {			
		newWidth = ctWidth;
		newHeight = (newWidth / ratio);				
	}
	//finally we can resize!
	var xShift = (ctWidth - newWidth)/2;
	var yShift = (ctHeight - newHeight)/2;
	
	if(what=='width')
		return newWidth;
	if(what=='height')
		return newHeight;
	if(what=='x')
		return xShift;
	if(what=='y')
		return yShift;
}

function updateCropping(coords)
{
	w=(jQuery("#"+active_element).attr("width"));
	h=(jQuery("#"+active_element).attr("height"));
	
	myposter = active_img.split("@@@");
	save_cropping(myposter[1], coords.x, coords.y, w, h);
	//alert(coords.x + " x " + coords.y);
	show_info();
	setTimeout("hide_info()", 2000);
}

function show_info()
{
	var layer_x = (jcrop_x + 10) + "px";
	var layer_y = (jcrop_y + 10) + "px";
	jQuery("#myjcrop_info").css({"position":"fixed"});
	jQuery("#myjcrop_info").css({"top":layer_y});
	jQuery("#myjcrop_info").css({"left":layer_x});
	jQuery("#myjcrop_info").css({"display":"block"});
}

function hide_info()
{
	jQuery("#myjcrop_info").css({"display":"none"});
}

function save_cropping(poster_id,x,y,w,h)
{
	//alert("/aktion/crop/"+poster_id+"&x="+x+"&y="+y+"&w="+w+"&h="+h);
	/*
	$.ajax({
		dataType	: 'json',
		type		: "GET",
		url			: "/aktion/crop/"+poster_id+"&x="+x+"&y="+y+"&w="+w+"&h="+h,
		complete	: function (data) {
			//crop_success(data);

		}
	});	
	*/

	jQuery.ajax({
		dataType	: 'json',
		type		: "POST",
		url			: "/aktion/crop/"+poster_id+"/",
		data		: {"x":x, "y":y, "w":w, "h":h},
		complete	: function (data) {
			//crop_success(data);

		}
	});	

}

function crop_success(data)
{
	obj = eval(data);
	//alert(data);
}

function get_crops()
{
	var myarr = new Array();
	var mytemparr = new Array();
	/*
	$.get("/aktion/getoffsets", function(data)
		{
			var obj = eval(data);
			$.each(obj,function(i,val)
				{
					crop_settings.push(i + "@@@" + val.x + "x" + val.y);
					alert(i + "@@@" + val.x + "x" + val.y);
				}
			);
		}
	);
	*/
	jQuery.ajax({
		dataType	: 'json',
		async		: false,
		url			: "/aktion/getoffsets/",
		success		: function(json){
			set_crops(json);
		}
		
	});
}

function set_crops(data)
{
	var obj = eval(data);
	jQuery.each(obj,function(i,val)
		{
			crop_settings.push(i + "@@@" + val.x + "x" + val.y);
		}
	);

}

function get_browser()
{	
	var brsr;
	if(jQuery.browser.version == '1.9.0.15')
		brsr="FF3.0";
	else if(jQuery.browser.version == '1.9.1.5')
		brsr="FF3.5";
	else if(jQuery.browser.version == '1.8.1.20')
		brsr="FF2";
	else if(jQuery.browser.version == '7.0')
		brsr="IE7";
	else if(jQuery.browser.version == '6.0')
		brsr="IE6";
	else
		brsr="UNDETECTED";
	return brsr;
	
}
