/*The resizeType can be changed by parentSize or cropSize*/

function resizeImg (img, resizeType) {	
	var w,h;
	var parentWidth = $(img).parent().parent().width();
	var parentHeight = $(img).parent().parent().height();
	if (parentHeight<$(img).height() || parentWidth<$(img).width()){
		var imgWidth = $(img).attr("width");
		var imgHeight = $(img).attr("height");
		xRatio = parentWidth / imgWidth;
		yRatio = parentHeight / imgHeight;
		
		if (resizeType != "parentSize") {
			if ( xRatio < yRatio ) {
				w = Math.ceil(imgWidth * yRatio);
				h = parentHeight;
				imgMarginLeft = (parentWidth - w)/2;
				$(img).css("margin-left", imgMarginLeft);
			}
			else {
				w = parentWidth
				h = Math.ceil(imgHeight * xRatio);
				imgMarginTop = (parentHeight - h)/2+"px";
				$(img).css("margin-top", imgMarginTop);
			}			
		}
		else {
			if ( xRatio > yRatio ) {
				w = Math.ceil(imgWidth * yRatio);
				h = parentHeight;
			}
			else {
				w = parentWidth
				h = Math.ceil(imgHeight * xRatio);
			}
		}
	}
		$(img).width(w);
		$(img).height(h);
};


jQuery.fn.placeScaledImage = function (_url, max_width, max_height, _function, _params) {

    var _el = this;
    if (typeof (max_width) == 'undefined' || Number(max_width) <= 0) max_width = 950;
    if (typeof (max_height) == 'undefined' || Number(max_height) <= 0) max_height = 550;
   
    var _im = $("<img>");

    _im.bind("load", function () {

        var _width = this.width
        var _height = this.height

        var ratio = _height / max_height;
        if ((_width / max_width) > ratio) ratio = _width / max_width;

        if (ratio < 1) ratio = 1;

        var new_width = parseInt(_width / ratio);
        var new_height = parseInt(_height / ratio);

        $(_el).attr('src', _url);
        $(_el).width(new_width);
        $(_el).height(new_height);

        var str_params = "";

        if (typeof (_params) != 'undefined') str_params = _params.join(",")
        if (typeof (_function) != 'undefined') eval(_function + '(' + str_params + ');');

    });

    _im.attr('src', _url);

}



jQuery.fn.placeScaledProductPicture = function (_url, _max_width, _max_height, _loader_url, _resize) {

    var _el = this;
    var loader_url = "";
    var max_width = 570;
    var max_height = 412;
    var resize = false;
    if (typeof (_max_width) != 'undefined' && Number(_max_width) > 0) max_width = _max_width;
    if (typeof (_max_height) != 'undefined' && Number(_max_height) > 0) max_height = _max_height;
    if (typeof (_loader_url) != 'undefined') loader_url = _loader_url;
    if (typeof (_resize) != 'undefined') resize = _resize;

    var resultX = 0;
    var resultY = 0;
    var resultWidth = max_width;
    var resultHeight = max_height;

    if (loader_url != "") $(_el).removeAttr("style").addClass('loader').attr('src', loader_url).removeClass('hidden-picture');
    //alert(loader_url);
    var _im = $("<img>");

    _im.bind("load", function () {

        var _width = this.width
        var _height = this.height
        if (_width < max_width && _height < max_height) {
            resultWidth = _width;
            resultHeight = _height;
        } else {

            var ratio = _height / max_height;
            if (resize) {
                if ((_width / max_width) < ratio) ratio = _width / max_width;
            } else {
                if ((_width / max_width) < ratio) ratio = _width / max_width;
                if (ratio < 1) ratio = 1;
            }
            var resultWidth = parseInt(_width / ratio);
            var resultHeight = parseInt(_height / ratio);
            resultX = (max_width - resultWidth) / 2;
            resultY = (max_height - resultHeight) / 2;
            if (resultX > 0) resultX = 0;
            if (resultY > 0) resultY = 0;
        }

        $(_el).css({
            width: resultWidth + "px",
            height: resultHeight + "px",
            marginLeft: resultX + "px",
            marginTop: resultY + "px"
        }).removeClass("loader").attr('src', _url);

    });

    _im.attr('src', _url);

}



