//tovabb

function tovabb_szallitasi() {
    document.getElementById("kosar_form_action").value = "update_tovabb";
    document.getElementById("kosar_form").submit();
    return false;
}

// rename group

function renamegroup() {
	$("#renamegroup_text").css("display", "none");
	$("#renamegroup_form").css("display", "inline");
	return false;
}

function renamegroup_cancel() {
	$("#renamegroup_text").css("display", "inline");
	$("#renamegroup_form").css("display", "none");
	return false;
}

// add group

function addgroup() {
	$("#addgroup").css("display", "block");
	$("#addgroup .text").select();
	return false;
}

function addgroup_cancel() {
	$("#addgroup").css("display", "none");
	return false;
}

//delete images

function deleteimages() {
    document.getElementById("kepmuvelet_action").value = "delete";
    document.getElementById("kepmuvelet").submit();
    return false;
}

// move menu
function showmenu(obj) {
	var obj = document.getElementById(obj);
	if (obj.style.display == "inline") {
		obj.style.display = "none";
	} else {
		obj.style.display = "inline";
	}
	$('body').mouseup(function() {
		$("#imagedropdown").css("display","none");
	});
	return false;
}

function menuclick(obj, action, params) {
	var obj = document.getElementById(obj);
	obj.style.display = "none";
	document.getElementById("kepmuvelet_action").name = "action_"+action;
	document.getElementById("kepmuvelet_params").value = params;
	document.getElementById("kepmuvelet").submit();
	$('body').unbind("mouseup");
	return false;
}

function setcheckboxes(formid, state) {
	$("#"+formid+" input[type=checkbox]").each(function() { 
		this.checked = state; 
	});
	return false;
}

function setnum(size, obj) {
	var obj = $("#"+obj);
	var value = obj.attr("value");
	$("table.cartitem input.num_"+size).val(value)//.animate({ backgroundColor: 'yellow' }, 500).animate({ backgroundColor: 'yellow' }, 1000).animate({ backgroundColor: 'white' }, 500);
	return false;
}

// larger image

function hide_larger() {
	$("#larger").hide();
	document.onscroll = large_onscroll;
	window.onresize = large_onresize;
	return false;
}

function large_onscroll() {
	var scrolly = (document.all)?document.body.scrollTop:window.pageYOffset;
	var winh = (document.all)?document.body.offsetHeight:window.innerHeight;
	var h = $("#larger").height();
	$("#larger").css("top", scrolly+winh/2-h/2);
}

function large_onresize() {
	var scrolly = (document.all)?document.body.scrollTop:window.pageYOffset;
	var winw = (document.all)?document.body.offsetWidth:window.innerWidth;
	var winh = (document.all)?document.body.offsetHeight:window.innerHeight;
	var w = $("#larger").width();
	var h = $("#larger").height();
	$("#larger").css("top", scrolly+winh/2-h/2);	
	$("#larger").css("left", winw/2-w/2);
}

function larger(node, w, h) {
	var href = node.href;
	var scrolly = (document.all)?document.body.scrollTop:window.pageYOffset;
	var winw = (document.all)?document.body.offsetWidth:window.innerWidth;
	var winh = (document.all)?document.body.offsetHeight:window.innerHeight;
	
	var larger = $("#larger");
	
	larger.show();
	larger.html("<a href='#' onclick='return hide_larger();' class='noborder'><img src='"+href+"' width="+w+" height="+h+"/></a>");
	larger.css("top", scrolly+winh/2-h/2);
	larger.css("left", winw/2-w/2);
	
	document.onscroll = large_onscroll;
	window.onresize = large_onresize;
	
	return false;
}

// fillfit

function setfillfit_fill(obj) {
	var childs = obj.children();
	var imgobj = $(childs[1]);
	var borderobj = $(childs[0]);
	
	borderobj.css({ "border": "1px solid red"});
	imgobj.css({ "margin-top": "0px" });
	
	var w = imgobj.width();
	var h = imgobj.height();
	obj.css({"width": w, "height": h, "margin-left": "auto", "margin-right": "auto"});
    
	if (w == 100) { //horizontal pic
        var targeth = (100/15)*10;
        var borderh = Math.round((h-targeth)/2);
        if (borderh < 0) {
            var targetw = (h/10)*15;
            var borderw = Math.round((w-targetw)/2);
            borderobj.css({
                "width": w-borderw*2,
                "height": Math.min(100, h),
                "border-left": borderw+"px solid red",
                "border-right": borderw+"px solid red",
                "border-top": "0px solid red",
                "border-bottom": "0px solid red"
            });
        } else {
            borderobj.css({
                "width": 100,
                "height": h-borderh*2,
                "border-left": "0px solid red",
                "border-right": "0px solid red",
                "border-top": borderh+"px solid red",
                "border-bottom": borderh+"px solid red"
            });
        }
	} else { //vertical pic
		var targetw = (100/15)*10;
		var borderw = Math.round((w-targetw)/2);
		borderobj.css({
			"width": w-borderw*2,
			"height": Math.min(100, h),
			"border-left": borderw+"px solid red",
			"border-right": borderw+"px solid red",
			"border-top": "0px solid red",
			"border-bottom": "0px solid red"
		});
	}
}

function setfillfit_fit(obj) {
	var childs = obj.children();
	var imgobj = $(childs[1]);
	var borderobj = $(childs[0]);
	
	borderobj.css({ "border": "0px solid white" });
	
	var w = imgobj.width();
	var h = imgobj.height();
    
    var targetw = (h/10)*15;
	if (w == 100 && w < targetw) { //horizontal pic
		var borderw = Math.round((targetw-w)/2);
		obj.css({"width": w+borderw*2, "height": h, "margin-left": "auto", "margin-right": "auto", "background-color": "white"});
		borderobj.css({
			"width": w,
			"height": h
		});
	} else { //vertical pic
        if (w == 100)
            var targeth = (100/15)*10
        else
            var targeth = (w/10)*15;
		var borderh = Math.round((targeth-h)/2);
		obj.css({"width": w, "height": h+borderh*2, "margin-left": "auto", "margin-right": "auto"});
		imgobj.css("margin-top", borderh);
		if (borderh>0) {
			borderobj.css({
				"width": w,
				"height": h,
				"border-top": borderh+"px solid white",
				"border-bottom": borderh+"px solid white"
			});
		} else {
			borderh = Math.abs(borderh);
			borderobj.css({
				"width": w,
				"height": h,
				"border-top": "0px solid white",
				"border-bottom": "0px solid white"
			});
			imgobj.css("margin-top", "0");
			
			obj.css({"height": h, "width": w+borderh*2});
		}
	}
}

function setfillfit_fitcrop(obj) {
	var childs = obj.children();
	var imgobj = $(childs[1]);
	var borderobj = $(childs[0]);
	
	borderobj.css({ "border": "0px solid white" });
	
	var w = imgobj.width();
	var h = imgobj.height();
	
	obj.css({"width": w, "height": h, "margin-left": "auto", "margin-right": "auto"});
	borderobj.css({ "width": w, "height": h, "border-top": "0px solid white", "border-bottom": "0px solid white", "border-left": "0px solid white", "border-right": "0px solid white"});
	imgobj.css({ "margin-top": "0px" });
}


function setfillfit(obj, type) {
	if (type == "fill") setfillfit_fill(obj);
	if (type == "fit") setfillfit_fit(obj);
	if (type == "fitcrop") setfillfit_fitcrop(obj);
}



function setchecked(checked) {
	if (checked) {
		$("#szamlazasi .text, #szamlazasi select").attr("disabled", "disabled").css("background-color", "#EEEEEE");
	} else {
		$("#szamlazasi .text, #szamlazasi select").removeAttr("disabled").css("background-color", "#FFFFFF");
	}
}

function show_fitcrop_help() {
	ddrivetip('Laborunk fix szélességű fényérzékeny fotópapírral dolgozik, ebből kifolyólag sajnos 9x13cm képméretek esetében "Fit levágva" típusú előhívás helyett "Fit" eljárást alkalmazunk.');
}

function everypic_progress(name, func, percent) {
	var start_num = Math.round(everypic_array_length*(percent/100));
	var end_num = start_num+Math.round(everypic_array_length*(percentstep/100));
	//alert(percent+":"+start_num+"->"+end_num);
	for (var i=start_num;i<=end_num;i++) {
		if (i <= lastobjnum) continue;
		var obj = everypic_array[i];
		if (obj)
			func(obj);
		lastobjnum = i;
	}
	percent += percentstep;
	percentdiv.html("Betöltés..."+percent+"%");
	if (percent < 100) {
		everytimer = setTimeout(function () {everypic_progress(name, func, percent)}, 50);
	} else {
		percentdiv.html("");
		percentdiv.css("background-color","#FFFFFF");
	}
}

everytimer = 1;
function everypic(name, func) {
	percentdiv.html("Betöltés..."+name);
	percentdiv.css("background-color", "#FF0000");
	clearTimeout(everytimer);
	lastobjnum=-1;
	if (everypic_array_length < 50) {
		percentstep = 50
	} else if (everypic_array_length < 100) {
		percentstep = 20
	}
	everytimer = setTimeout(function () {everypic_progress(name, func, 0)}, 100);
}

function setallmode(myobj) { //set all image to this elohivasi mod
	var myeobj = $(myobj);
	myselectval = myeobj.parent().children("select").val();
	everypic("setfillfit2", function(obj) { //setfillfit preview
		var obj = $(obj);
		var select = $(obj.siblings()[2]);
		select.val(myselectval);
		setfillfit(obj, select.val());
	});
	return false
}

percentstep = 1;
lastobjnum = -1;
everypic_progress_array = new Array();
fillfithelp = 'A <b>&quot;fill&quot;</b> eljárással előhívott képek teljesen kitöltik a kiválasztott papírméretet, így előfordulhat, hogy az eredeti kép és az előhívott kép eltérő oldalarányai miatt az eredeti kép egyes részei nem kerülnek rá az előhívott fényképre. Ilyenkor vagy az előhívott kép tetejéről és aljáról vagy pedig a jobb és bal oldaláról hiányzik egy kis részlet, ami az eredeti képen még rajta volt (a levágott részt pirossal vonalak jelzik). A <b>&quot;fit&quot;</b> eljárással előhívott képek esetében az eredeti kép teljes egészében rákerül a fotópapírra, de az esetlegesen eltérő oldalarányok miatt a fotópapíron maradhat egy fehér sáv, amit a <b>&quot;fit levágva&quot;</b> eljárással el is távolíthatunk.';
alkalmazmindre = 'Eljárás alkalmazása mindre.';


// Az oldal betöltését követően ez a kód fut le és állítja be a kis képeket fill-re
$(document).ready(function(){
	percentdiv = $("#percent");
	everypic_array = $("table.cartitem .picthumb");
	everypic_array_length = everypic_array.length;
	if (pagetype == "cart") { //if on cart page
		var opener = "window.open('fillfit.html','image','toolbar=0,width=400,height=500,location=0,directories=0,menuBar=0,scrollbars=1,resizable=0')";
		var after = "<a href=\"javascript:null("+opener+");\" class='icon' onmouseover=\"ddrivetip(fillfithelp)\" onmouseout='hideddrivetip()'><img align='bottom' src='/media/img/question_mark.gif'/></a>";
		var setter = "setallmode(this);";
		after += "<a href=\"#\" class='icon' onmouseup='return "+setter+"' onmouseover=\"ddrivetip(alkalmazmindre)\" onmouseout='hideddrivetip()'><img align='bottom' src='/media/img/icon_updown.gif'/></a>";
		everypic("setfillfit", function(obj) { //setfillfit preview
			var obj = $(obj);
			//obj.css("border", "1px solid #999999");
			var select = $(obj.siblings()[2]);
			$(select).after(after);
			setfillfit(obj, select.val());
			select.bind("change keyup", function () {
				
				var value = this.value;
				
				if (value == "fill")
					setfillfit(obj, "fill");
				else if (value == "fit")
					setfillfit(obj, "fit");
				else
					setfillfit(obj, "fitcrop");
			});
		});
	};
    if (pagetype == "osszegzes") {
        $("table.cartitem .picthumb").each(function() {
			var obj = $(this);
			obj.css("border", "1px solid #999999");
			var select = $(obj.siblings()[2]);
            setfillfit(obj, select.text());
        });
    };
	if (pagetype == "szallitasi") {
		setchecked($("#bill_check").is(":checked"));
		$("#bill_check").bind("change click", function () {
			setchecked(this.checked);
		});
	}
	if (pagetype == "cart" || pagetype == "list") {
		hide_larger();
	}
});

