function VyplnFormAction(skript)
{
	var FormElement = document.getElementById("PosliEmailForm");

	var NahodneCislo = Math.round((Math.random() * 20000)) * 8513;

	if (FormElement != undefined){
		FormElement.action = baseUrl + 'tools/'+skript+'?AuthKey=' + NahodneCislo;
	}

}

//-----------------------------------------------------------------------------------

function initializeInputItem( defaultText, elementId ) {
	var value = document.getElementById(elementId).value;
	if (value == defaultText)
	document.getElementById(elementId).value = "";
	else if (value == "")
	document.getElementById(elementId).value = defaultText;
	return false;
}

//-----------------------------------------------------------------------------------

function otevriPanel( divId )
{
	div = document.getElementById(divId);

	if (div.style.display=='block') {
		$('#'+divId).hide('slow');
	} else {
		$('#'+divId).show('fast');
	}
}
//-----------------------------------------------------------------------------------

function otevriPanelFotoCheckBox( divId )
{
	div = document.getElementById(divId);
	fotocheckbox = document.getElementById('fotocheckbox');

	if (fotocheckbox.checked==false) {
		$('#'+divId).hide('slow');
	} else {
		$('#'+divId).show('fast');
	}
}

//-----------------------------------------------------------------------------------

function VyplnNapisNamFormAction(skript)
{
	var FormElement = document.getElementById("PosliEmailForm");

	var NahodneCislo = Math.round((Math.random() * 20000)) * 8513;

	jmeno = document.getElementById("name").value;
	email = document.getElementById("email").value;
	phone = document.getElementById("phone").value;
	mess = document.getElementById("message").value;

	if (jmeno == '') {
		alert('Vyplňte prosím jméno.');
		return false;
	}
	if (email == '') {
		alert('Vyplňte prosím email.');
		return false;
	}
	if (phone == '') {
		alert('Vyplňte prosím telefon.');
		return false;
	}
	if (mess == '') {
		alert('Vyplňte prosím zprávu.');
		return false;
	}
	

	if (FormElement != undefined){
		FormElement.action = BASE__URL + 'tools/'+skript+'?AuthKey=' + NahodneCislo;
	}

}
//-----------------------------------------------------------------------------------
function ohlaschybu(str){
	Ext.MessageBox.show({
		title: 'Informace',
		msg: str,
		buttons: Ext.MessageBox.OK,
		icon: Ext.MessageBox.INFO
	});
	return false;
}
//-----------------------------------------------------------------------------------
function setBorder(id) {
	document.getElementById(id).selected = true;
	closeBorders();

						var imgWidth = getImageWidth('image-c');;
								var imgHeight = getImageHeight('image-c');
//								alert(imgWidth + ' x ' + imgHeight);
								
								height = '400px';
								width = '266px';
								ram = 'h';
								if (imgWidth > imgHeight) {
									height = '266px';
									width = '400px';
									ram = 'w';
								}
	
								borderize(ram);
}
//-----------------------------------------------------------------------------------
function showBorders() {
	showPageOnClick('ramceky');
	document.getElementById('ramceky').style.display = '';
}
//-----------------------------------------------------------------------------------
function closeBorders() {
	document.getElementById('ramceky').style.display = 'none';
}
//-----------------------------------------------------------------------------------
function rotatePic() {

	var request;
	var rot = document.form1.rotateSelect.value;

	if (rot == 0 || rot == '0') {
		return false;
	}

	var file = document.form1.file.value;
	var key = document.form1.key.value;

	try {
		request = new XMLHttpRequest();
	} catch(e) {

		try {
			request = ActiveXObject("Msxml2.XMLHTTP");
		} catch(e) {

			try {
				request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				alert("Error!");
				return false;
			}
		}
	}

	request.onreadystatechange = function(){
		if(request.readyState == 4){
			var r = request.responseText;
			document.getElementById('loading').style.display = 'none';
			var hiddenRotate = document.form1.rotate.value;
			document.form1.rotate.value = (parseInt(hiddenRotate) + parseInt(document.form1.rotateSelect.value)) % 360;
			rotation[imageId] = parseInt(document.form1.rotate.value);
			document.form1.rotateSelect.selectedIndex = 0;
			document.form1.border.selectedIndex = 0;
			document.form1.colority.selectedIndex = 0;
			document.getElementById('border-1').style.display = 'none';

			var file = document.form1.file.value;
			var key = document.form1.key.value;
			var a = "http://babyphoto.themis.redcat.cz/image.php?file=" + file + "&key=" + key + "&size=";

			var c = a + "800x600";
			var g = a + "800x600g";
			var s = a + "800x600s";

			var imgC = document.getElementById('image-c');
			var imgG = document.getElementById('image-g');
			var imgS = document.getElementById('image-s');

			imgC.style.width = '';
			imgC.style.height = '';

			imgG.style.width = '';
			imgG.style.height = '';

			imgS.style.width = '';
			imgS.style.height = '';

			var now = new Date();

			imgC.src = c + '&' + now.getTime();
			imgG.src = g + '&' + now.getTime();
			imgS.src = s + '&' + now.getTime();

			imgC = document.getElementById('image-c');

			var height = '400px';
			var width = '266px';
			if (r == 'w') {
				height = '266px';
				width = '400px';
			}
			//alert(imgC.clientWidth+'x'+imgC.clientHeight);
			imgC.style.width = width;
			imgC.style.height = height;
			
			imgG.style.width = width;
			imgG.style.height = height;
			
			imgS.style.width = width;
			imgS.style.height = height;

			document.getElementById('image-color').style.display = '';
			document.getElementById('image-greyscale').style.display = 'none';
			document.getElementById('image-sepia').style.display = 'none';

			// refresh thumb
			var thumb = document.getElementById('thumb-' + document.form1.imageId.value);
			thumb.src = a + '100x75&' + now.getTime();

			if (r == 'w') {
				$('#i-'+key).addClass('wide');
			} else {
				$('#i-'+key).removeClass('wide');
			}
		}
	}

	// selected
	var rotate = document.form1.rotateSelect.value;
	var imageId = document.form1.imageId.value;

	var queryString = "?file=" + file + "&key=" + key + "&rotate=" + rotate + "&imageId=" + imageId;

	document.getElementById('loading').style.display = '';
	request.open("GET", "tools/rotate.php" + queryString, true);
	request.send(null);
}
//-----------------------------------------------------------------------------------
function rotateFotoPenazenka() {

	var request;
	var rot = document.form1.rotateSelect.value;

	if (rot == 0 || rot == '0') {
		return false;
	}

	var file = document.form1.file.value;
	var key = document.form1.key.value;

	try {
		request = new XMLHttpRequest();
	} catch(e) {

		try {
			request = ActiveXObject("Msxml2.XMLHTTP");
		} catch(e) {

			try {
				request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				alert("Error!");
				return false;
			}
		}
	}

	request.onreadystatechange = function(){
		if(request.readyState == 4){
			var r = request.responseText;
			document.getElementById('loading').style.display = 'none';
			var hiddenRotate = document.form1.rotate.value;
			document.form1.rotate.value = (parseInt(hiddenRotate) + parseInt(document.form1.rotateSelect.value)) % 360;
			rotation[imageId] = parseInt(document.form1.rotate.value);
			document.form1.rotateSelect.selectedIndex = 0;
			document.form1.colority.selectedIndex = 0;
//			document.form1.border.selectedIndex = 0;
//			document.getElementById('border-1').style.display = 'none';

			document.getElementById('image-color').style.display = '';
			document.getElementById('image-greyscale').style.display = 'none';
			document.getElementById('image-sepia').style.display = 'none';
			
			var file = document.form1.file.value;
			var key = document.form1.key.value;
			var a = "http://babyphoto.themis.redcat.cz/image.php?file=" + file + "&key=" + key + "&size=";

			var c = a + "800x600";
			var g = a + "800x600g";
			var s = a + "800x600s";

			var imgC = document.getElementById('image-c');
			var imgG = document.getElementById('image-g');
			var imgS = document.getElementById('image-s');

			imgC.style.width = '';
			imgC.style.height = '';

			imgG.style.width = '';
			imgG.style.height = '';

			imgS.style.width = '';
			imgS.style.height = '';

			var now = new Date();

			imgC.src = c + '&' + now.getTime();
			imgG.src = g + '&' + now.getTime();
			imgS.src = s + '&' + now.getTime();

			imgC = document.getElementById('image-c');

			var height = '400px';
			var width = '266px';
			var ram = 'h';
//			alert(imgC.clientWidth + ' x ' + imgC.clientHeight);
			if (r == 'w') {
				height = '266px';
				width = '400px';
				ram = 'w';
			}
//			alert(imgC.clientWidth + ' x ' + imgC.clientHeight);
			imgC.style.width = width;
			imgC.style.height = height;
			
			imgG.style.width = width;
			imgG.style.height = height;
			
			imgS.style.width = width;
			imgS.style.height = height;

			// refresh thumb
			var thumb = document.getElementById('thumb-' + document.form1.imageId.value);
			thumb.src = a + '100x75&' + now.getTime();
			
			if (r == 'w') {
				$('#i-'+key).addClass('wide');
			} else {
				$('#i-'+key).removeClass('wide');
			}
			
			borderize(ram);
//			window.location.reload();
		}
	}

	// selected
	var rotate = document.form1.rotateSelect.value;
	var imageId = document.form1.imageId.value;

	var queryString = "?file=" + file + "&key=" + key + "&rotate=" + rotate + "&imageId=" + imageId;

	document.getElementById('loading').style.display = '';
	request.open("GET", "tools/rotate.php" + queryString, true);
	request.send(null);
}
//-----------------------------------------------------------------------------------
function basename (path, suffix) {
	// Returns the filename component of the path
	//
	// version: 1008.1718
	// discuss at: http://phpjs.org/functions/basename    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
	// +   improved by: Ash Searle (http://hexmen.com/blog/)
	// +   improved by: Lincoln Ramsay
	// +   improved by: djmix
	// *     example 1: basename('/www/site/home.htm', '.htm');    // *     returns 1: 'home'
	// *     example 2: basename('ecra.php?p=1');
	// *     returns 2: 'ecra.php?p=1'
	var b = path.replace(/^.*[\/\\]/g, '');
	if (typeof(suffix) == 'string' && b.substr(b.length-suffix.length) == suffix) {
		b = b.substr(0, b.length-suffix.length);
	}

	return b;
}
//-----------------------------------------------------------------------------------
function showPageOnClick(divId) {

	var WindowHeight = 0;
	var WindowWidth = 0;
 
	// the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight
	if (typeof window.innerWidth != 'undefined')
	{
		WindowWidth = window.innerWidth,
		WindowHeight = window.innerHeight
	}
 
	// IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)
 
	else if (typeof document.documentElement != 'undefined'
		   && typeof document.documentElement.clientWidth !=
		   'undefined' && document.documentElement.clientWidth != 0 )
	{
	       WindowWidth = document.documentElement.clientWidth,
	       WindowHeight = document.documentElement.clientHeight
	}
 
	// older versions of IE
 
	else
	{
	       WindowWidth = document.getElementsByTagName('body')[0].clientWidth,
	       WindowHeight = document.getElementsByTagName('body')[0].clientHeight
	}
 
	// get the middle height using below formula
	var middleHeight = (WindowHeight - jQuery('#'+divId).height()) / 2;
 
	// get the middle width using below formula
	var middleWidth = (WindowWidth - jQuery('#'+divId).width()) / 2;
 
	// get the scrolled value from top
	var scrollTop = $(window).scrollTop();
	middleHeight = middleHeight + scrollTop;
	/*
	if (jQuery.browser.msie && parseInt(jQuery.browser.version) < 8) {
		jQuery('#'+divId).css( { "left": + middleWidth + "px", "top": + middleHeight + "px", "z-index": "1000", "position": "absolute", "margin": "-100px 0 0 -720px" } );
		return true;
	}*/
	
	zIndex = "20";
	if (divId == 'ramceky') zIndex = "25";
 
	// Now set the css for provided div Id
	jQuery('#'+divId).css( { "left": + middleWidth + "px", "top": + middleHeight + "px", "z-index": + zIndex, "position": "absolute" } );
 
	// Above code will show the div area in the middle of the screen
	// Below code is useful to show rest screen in black color to hide rest area. 
	/*var fullHeight = Math.max($(document).height(), $(window).height(), document.documentElement.clientHeight);
 
	var overlayDiv = document.createElement('div');
	var divIdName = 'overlay';
    	overlayDiv.setAttribute('id', divIdName);
	overlayDiv.setAttribute('style', 'height: ' + fullHeight + 'px; width: ' + WindowWidth + 'px; left:0px; top:0px; position:absolute; background-color:#000000; opacity:0.8;');
	overlayDiv.innerHTML = '';
	var mainDiv = document.getElementById('body');
	mainDiv.appendChild(overlayDiv);*/
}
//-----------------------------------------------------------------------------------
function generateFbLike(img, key, color) {
	
	var s = img + ':' + key + ':' + color;
//	var hash = encode(s, "##q2#");
	var hash = base64_encode(s);
//	alert(hash);
	url = HOST + BASE__URL + 'fb/' + hash ;
//	alert(url);
	url = encodeURIComponent(url);
//	alert(url);
	$("#fblike").html('<div class="fb-box"><iframe src="http://www.facebook.com/plugins/like.php?href='+url+'&amp;layout=button_count" scrolling="no" frameborder="0" style="height: 62px; width: 300px; margin-bottom: 2px;" allowTransparency="true"></iframe></div>');
}
//-----------------------------------------------------------------------------------
function encode(string, k) {
    var key = sha1(k);
    var strLen = strlen(string);
    var keyLen = strlen(key);
//    alert (key+ " " + strLen + " " + keyLen);
    var i; 
    var j = 0;
    var hash = "";
    for (i = 0; i < strLen; i++) {
        var ordStr = ord(substr(string, i, 1));
//        alert(ordStr);
        if (j == keyLen) { 
        	j = 0; 
        }
        var ordKey = ord(substr(key, j, 1));
//        alert(ordKey);
        j++;
        hash = hash + strrev(base_convert(dechex(ordStr + ordKey), 16, 36));
    }
    return hash;
}

//-----------------------------------------------------------------------------------
function sha1 (str) {
    // Calculate the sha1 hash of a string  
    // 
    // version: 1102.614
    // discuss at: http://phpjs.org/functions/sha1
    // +   original by: Webtoolkit.info (http://www.webtoolkit.info/)
    // + namespaced by: Michael White (http://getsprink.com)
    // +      input by: Brett Zamir (http://brett-zamir.me)
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // -    depends on: utf8_encode
    // *     example 1: sha1('Kevin van Zonneveld');
    // *     returns 1: '54916d2e62f65b3afa6e192e6a601cdbe5cb5897'
    var rotate_left = function (n, s) {
        var t4 = (n << s) | (n >>> (32 - s));
        return t4;
    };
 
/*var lsb_hex = function (val) { // Not in use; needed?
        var str="";
        var i;
        var vh;
        var vl;
 
        for ( i=0; i<=6; i+=2 ) {
            vh = (val>>>(i*4+4))&0x0f;
            vl = (val>>>(i*4))&0x0f;
            str += vh.toString(16) + vl.toString(16);
        }
        return str;
    };*/
 
    var cvt_hex = function (val) {
        var str = "";
        var i;
        var v;
 
        for (i = 7; i >= 0; i--) {
            v = (val >>> (i * 4)) & 0x0f;
            str += v.toString(16);
        }
        return str;
    };
 
    var blockstart;
    var i, j;
    var W = new Array(80);
    var H0 = 0x67452301;
    var H1 = 0xEFCDAB89;
    var H2 = 0x98BADCFE;
    var H3 = 0x10325476;
    var H4 = 0xC3D2E1F0;
    var A, B, C, D, E;
    var temp;
 
    str = this.utf8_encode(str);
    var str_len = str.length;
 
    var word_array = [];
    for (i = 0; i < str_len - 3; i += 4) {
        j = str.charCodeAt(i) << 24 | str.charCodeAt(i + 1) << 16 | str.charCodeAt(i + 2) << 8 | str.charCodeAt(i + 3);
        word_array.push(j);
    }
 
    switch (str_len % 4) {
    case 0:
        i = 0x080000000;
        break;
    case 1:
        i = str.charCodeAt(str_len - 1) << 24 | 0x0800000;
        break;
    case 2:
        i = str.charCodeAt(str_len - 2) << 24 | str.charCodeAt(str_len - 1) << 16 | 0x08000;
        break;
    case 3:
        i = str.charCodeAt(str_len - 3) << 24 | str.charCodeAt(str_len - 2) << 16 | str.charCodeAt(str_len - 1) << 8 | 0x80;
        break;
    }
 
    word_array.push(i);
 
    while ((word_array.length % 16) != 14) {
        word_array.push(0);
    }
 
    word_array.push(str_len >>> 29);
    word_array.push((str_len << 3) & 0x0ffffffff);
 
    for (blockstart = 0; blockstart < word_array.length; blockstart += 16) {
        for (i = 0; i < 16; i++) {
            W[i] = word_array[blockstart + i];
        }
        for (i = 16; i <= 79; i++) {
            W[i] = rotate_left(W[i - 3] ^ W[i - 8] ^ W[i - 14] ^ W[i - 16], 1);
        }
 
 
        A = H0;
        B = H1;
        C = H2;
        D = H3;
        E = H4;
 
        for (i = 0; i <= 19; i++) {
            temp = (rotate_left(A, 5) + ((B & C) | (~B & D)) + E + W[i] + 0x5A827999) & 0x0ffffffff;
            E = D;
            D = C;
            C = rotate_left(B, 30);
            B = A;
            A = temp;
        }
 
        for (i = 20; i <= 39; i++) {
            temp = (rotate_left(A, 5) + (B ^ C ^ D) + E + W[i] + 0x6ED9EBA1) & 0x0ffffffff;
            E = D;
            D = C;
            C = rotate_left(B, 30);
            B = A;
            A = temp;
        }
 
        for (i = 40; i <= 59; i++) {
            temp = (rotate_left(A, 5) + ((B & C) | (B & D) | (C & D)) + E + W[i] + 0x8F1BBCDC) & 0x0ffffffff;
            E = D;
            D = C;
            C = rotate_left(B, 30);
            B = A;
            A = temp;
        }
 
        for (i = 60; i <= 79; i++) {
            temp = (rotate_left(A, 5) + (B ^ C ^ D) + E + W[i] + 0xCA62C1D6) & 0x0ffffffff;
            E = D;
            D = C;
            C = rotate_left(B, 30);
            B = A;
            A = temp;
        }
 
        H0 = (H0 + A) & 0x0ffffffff;
        H1 = (H1 + B) & 0x0ffffffff;
        H2 = (H2 + C) & 0x0ffffffff;
        H3 = (H3 + D) & 0x0ffffffff;
        H4 = (H4 + E) & 0x0ffffffff;
    }
 
    temp = cvt_hex(H0) + cvt_hex(H1) + cvt_hex(H2) + cvt_hex(H3) + cvt_hex(H4);
    return temp.toLowerCase();
}
//-----------------------------------------------------------------------------------
function strlen (string) {
    // Get string length  
    // 
    // version: 1102.614
    // discuss at: http://phpjs.org/functions/strlen
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Sakimori
    // +      input by: Kirk Strobeck
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   bugfixed by: Onno Marsman
    // +    revised by: Brett Zamir (http://brett-zamir.me)
    // %        note 1: May look like overkill, but in order to be truly faithful to handling all Unicode
    // %        note 1: characters and to this function in PHP which does not count the number of bytes
    // %        note 1: but counts the number of characters, something like this is really necessary.
    // *     example 1: strlen('Kevin van Zonneveld');
    // *     returns 1: 19
    // *     example 2: strlen('A\ud87e\udc04Z');
    // *     returns 2: 3
    var str = string + '';
    var i = 0,
        chr = '',
        lgth = 0;
 
    if (!this.php_js || !this.php_js.ini || !this.php_js.ini['unicode.semantics'] || this.php_js.ini['unicode.semantics'].local_value.toLowerCase() !== 'on') {
        return string.length;
    }
 
    var getWholeChar = function (str, i) {
        var code = str.charCodeAt(i);
        var next = '',
            prev = '';
        if (0xD800 <= code && code <= 0xDBFF) { // High surrogate (could change last hex to 0xDB7F to treat high private surrogates as single characters)
            if (str.length <= (i + 1)) {
                throw 'High surrogate without following low surrogate';
            }
            next = str.charCodeAt(i + 1);
            if (0xDC00 > next || next > 0xDFFF) {
                throw 'High surrogate without following low surrogate';
            }
            return str.charAt(i) + str.charAt(i + 1);
        } else if (0xDC00 <= code && code <= 0xDFFF) { // Low surrogate
            if (i === 0) {
                throw 'Low surrogate without preceding high surrogate';
            }
            prev = str.charCodeAt(i - 1);
            if (0xD800 > prev || prev > 0xDBFF) { //(could change last hex to 0xDB7F to treat high private surrogates as single characters)
                throw 'Low surrogate without preceding high surrogate';
            }
            return false; // We can pass over low surrogates now as the second component in a pair which we have already processed
        }
        return str.charAt(i);
    };
 
    for (i = 0, lgth = 0; i < str.length; i++) {
        if ((chr = getWholeChar(str, i)) === false) {
            continue;
        } // Adapt this line at the top of any loop, passing in the whole string and the current iteration and returning a variable to represent the individual character; purpose is to treat the first part of a surrogate pair as the whole character and then ignore the second part
        lgth++;
    }
    return lgth;
}

function substr (str, start, len) {
    // Returns part of a string  
    // 
    // version: 909.322
    // discuss at: http://phpjs.org/functions/substr
    // +     original by: Martijn Wieringa
    // +     bugfixed by: T.Wild
    // +      tweaked by: Onno Marsman
    // +      revised by: Theriault
    // +      improved by: Brett Zamir (http://brett-zamir.me)
    // %    note 1: Handles rare Unicode characters if 'unicode.semantics' ini (PHP6) is set to 'on'
    // *       example 1: substr('abcdef', 0, -1);
    // *       returns 1: 'abcde'
    // *       example 2: substr(2, 0, -6);
    // *       returns 2: false
    // *       example 3: ini_set('unicode.semantics',  'on');
    // *       example 3: substr('a\uD801\uDC00', 0, -1);
    // *       returns 3: 'a'
    // *       example 4: ini_set('unicode.semantics',  'on');
    // *       example 4: substr('a\uD801\uDC00', 0, 2);
    // *       returns 4: 'a\uD801\uDC00'
    // *       example 5: ini_set('unicode.semantics',  'on');
    // *       example 5: substr('a\uD801\uDC00', -1, 1);
    // *       returns 5: '\uD801\uDC00'
    // *       example 6: ini_set('unicode.semantics',  'on');
    // *       example 6: substr('a\uD801\uDC00z\uD801\uDC00', -3, 2);
    // *       returns 6: '\uD801\uDC00z'
    // *       example 7: ini_set('unicode.semantics',  'on');
    // *       example 7: substr('a\uD801\uDC00z\uD801\uDC00', -3, -1)
    // *       returns 7: '\uD801\uDC00z'
    // Add: (?) Use unicode.runtime_encoding (e.g., with string wrapped in "binary" or "Binary" class) to
    // allow access of binary (see file_get_contents()) by: charCodeAt(x) & 0xFF (see https://developer.mozilla.org/En/Using_XMLHttpRequest ) or require conversion first?
    var i = 0,
        allBMP = true,
        es = 0,
        el = 0,
        se = 0,
        ret = '';
    str += '';
    var end = str.length;
 
    // BEGIN REDUNDANT
    this.php_js = this.php_js || {};
    this.php_js.ini = this.php_js.ini || {};
    // END REDUNDANT
    switch ((this.php_js.ini['unicode.semantics'] && this.php_js.ini['unicode.semantics'].local_value.toLowerCase())) {
    case 'on':
        // Full-blown Unicode including non-Basic-Multilingual-Plane characters
        // strlen()
        for (i = 0; i < str.length; i++) {
            if (/[\uD800-\uDBFF]/.test(str.charAt(i)) && /[\uDC00-\uDFFF]/.test(str.charAt(i + 1))) {
                allBMP = false;
                break;
            }
        }
 
        if (!allBMP) {
            if (start < 0) {
                for (i = end - 1, es = (start += end); i >= es; i--) {
                    if (/[\uDC00-\uDFFF]/.test(str.charAt(i)) && /[\uD800-\uDBFF]/.test(str.charAt(i - 1))) {
                        start--;
                        es--;
                    }
                }
            } else {
                var surrogatePairs = /[\uD800-\uDBFF][\uDC00-\uDFFF]/g;
                while ((surrogatePairs.exec(str)) != null) {
                    var li = surrogatePairs.lastIndex;
                    if (li - 2 < start) {
                        start++;
                    } else {
                        break;
                    }
                }
            }
 
            if (start >= end || start < 0) {
                return false;
            }
            if (len < 0) {
                for (i = end - 1, el = (end += len); i >= el; i--) {
                    if (/[\uDC00-\uDFFF]/.test(str.charAt(i)) && /[\uD800-\uDBFF]/.test(str.charAt(i - 1))) {
                        end--;
                        el--;
                    }
                }
                if (start > end) {
                    return false;
                }
                return str.slice(start, end);
            } else {
                se = start + len;
                for (i = start; i < se; i++) {
                    ret += str.charAt(i);
                    if (/[\uD800-\uDBFF]/.test(str.charAt(i)) && /[\uDC00-\uDFFF]/.test(str.charAt(i + 1))) {
                        se++; // Go one further, since one of the "characters" is part of a surrogate pair
                    }
                }
                return ret;
            }
            break;
        }
        // Fall-through
    case 'off':
        // assumes there are no non-BMP characters;
        //    if there may be such characters, then it is best to turn it on (critical in true XHTML/XML)
    default:
        if (start < 0) {
            start += end;
        }
        end = typeof len === 'undefined' ? end : (len < 0 ? len + end : len + start);
        // PHP returns false if start does not fall within the string.
        // PHP returns false if the calculated end comes before the calculated start.
        // PHP returns an empty string if start and end are the same.
        // Otherwise, PHP returns the portion of the string from start to end.
        return start >= str.length || start < 0 || start > end ? !1 : str.slice(start, end);
    }
    return undefined; // Please Netbeans
}

function ord (string) {
    // Returns the codepoint value of a character  
    // 
    // version: 1102.614
    // discuss at: http://phpjs.org/functions/ord
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   bugfixed by: Onno Marsman
    // +   improved by: Brett Zamir (http://brett-zamir.me)
    // +   input by: incidence
    // *     example 1: ord('K');
    // *     returns 1: 75
    // *     example 2: ord('\uD800\uDC00'); // surrogate pair to create a single Unicode character
    // *     returns 2: 65536
    var str = string + '',
        code = str.charCodeAt(0);
    if (0xD800 <= code && code <= 0xDBFF) { // High surrogate (could change last hex to 0xDB7F to treat high private surrogates as single characters)
        var hi = code;
        if (str.length === 1) {
            return code; // This is just a high surrogate with no following low surrogate, so we return its value;
            // we could also throw an error as it is not a complete character, but someone may want to know
        }
        var low = str.charCodeAt(1);
        return ((hi - 0xD800) * 0x400) + (low - 0xDC00) + 0x10000;
    }
    if (0xDC00 <= code && code <= 0xDFFF) { // Low surrogate
        return code; // This is just a low surrogate with no preceding high surrogate, so we return its value;
        // we could also throw an error as it is not a complete character, but someone may want to know
    }
    return code;
}

function strrev (string) {
    // Reverse a string  
    // 
    // version: 1102.614
    // discuss at: http://phpjs.org/functions/strrev
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   bugfixed by: Onno Marsman
    // +   reimplemented by: Brett Zamir (http://brett-zamir.me)
    // *     example 1: strrev('Kevin van Zonneveld');
    // *     returns 1: 'dlevennoZ nav niveK'
    // *     example 2: strrev('a\u0301haB') === 'Baha\u0301'; // combining
    // *     returns 2: true
    // *     example 3: strrev('A\uD87E\uDC04Z') === 'Z\uD87E\uDC04A'; // surrogates
    // *     returns 2: true
    string = string + '';
 
    // Performance will be enhanced with the next two lines of code commented
    //      out if you don't care about combining characters
    // Keep Unicode combining characters together with the character preceding
    //      them and which they are modifying (as in PHP 6)
    // See http://unicode.org/reports/tr44/#Property_Table (Me+Mn)
    // We also add the low surrogate range at the beginning here so it will be
    //      maintained with its preceding high surrogate
    var grapheme_extend = /(.)([\uDC00-\uDFFF\u0300-\u036F\u0483-\u0489\u0591-\u05BD\u05BF\u05C1\u05C2\u05C4\u05C5\u05C7\u0610-\u061A\u064B-\u065E\u0670\u06D6-\u06DC\u06DE-\u06E4\u06E7\u06E8\u06EA-\u06ED\u0711\u0730-\u074A\u07A6-\u07B0\u07EB-\u07F3\u0901-\u0903\u093C\u093E-\u094D\u0951-\u0954\u0962\u0963\u0981-\u0983\u09BC\u09BE-\u09C4\u09C7\u09C8\u09CB-\u09CD\u09D7\u09E2\u09E3\u0A01-\u0A03\u0A3C\u0A3E-\u0A42\u0A47\u0A48\u0A4B-\u0A4D\u0A51\u0A70\u0A71\u0A75\u0A81-\u0A83\u0ABC\u0ABE-\u0AC5\u0AC7-\u0AC9\u0ACB-\u0ACD\u0AE2\u0AE3\u0B01-\u0B03\u0B3C\u0B3E-\u0B44\u0B47\u0B48\u0B4B-\u0B4D\u0B56\u0B57\u0B62\u0B63\u0B82\u0BBE-\u0BC2\u0BC6-\u0BC8\u0BCA-\u0BCD\u0BD7\u0C01-\u0C03\u0C3E-\u0C44\u0C46-\u0C48\u0C4A-\u0C4D\u0C55\u0C56\u0C62\u0C63\u0C82\u0C83\u0CBC\u0CBE-\u0CC4\u0CC6-\u0CC8\u0CCA-\u0CCD\u0CD5\u0CD6\u0CE2\u0CE3\u0D02\u0D03\u0D3E-\u0D44\u0D46-\u0D48\u0D4A-\u0D4D\u0D57\u0D62\u0D63\u0D82\u0D83\u0DCA\u0DCF-\u0DD4\u0DD6\u0DD8-\u0DDF\u0DF2\u0DF3\u0E31\u0E34-\u0E3A\u0E47-\u0E4E\u0EB1\u0EB4-\u0EB9\u0EBB\u0EBC\u0EC8-\u0ECD\u0F18\u0F19\u0F35\u0F37\u0F39\u0F3E\u0F3F\u0F71-\u0F84\u0F86\u0F87\u0F90-\u0F97\u0F99-\u0FBC\u0FC6\u102B-\u103E\u1056-\u1059\u105E-\u1060\u1062-\u1064\u1067-\u106D\u1071-\u1074\u1082-\u108D\u108F\u135F\u1712-\u1714\u1732-\u1734\u1752\u1753\u1772\u1773\u17B6-\u17D3\u17DD\u180B-\u180D\u18A9\u1920-\u192B\u1930-\u193B\u19B0-\u19C0\u19C8\u19C9\u1A17-\u1A1B\u1B00-\u1B04\u1B34-\u1B44\u1B6B-\u1B73\u1B80-\u1B82\u1BA1-\u1BAA\u1C24-\u1C37\u1DC0-\u1DE6\u1DFE\u1DFF\u20D0-\u20F0\u2DE0-\u2DFF\u302A-\u302F\u3099\u309A\uA66F-\uA672\uA67C\uA67D\uA802\uA806\uA80B\uA823-\uA827\uA880\uA881\uA8B4-\uA8C4\uA926-\uA92D\uA947-\uA953\uAA29-\uAA36\uAA43\uAA4C\uAA4D\uFB1E\uFE00-\uFE0F\uFE20-\uFE26]+)/g;
    string = string.replace(grapheme_extend, '$2$1'); // Temporarily reverse
    return string.split('').reverse().join('');
}

function base_convert (number, frombase, tobase) {
    // Converts a number in a string from any base <= 36 to any base <= 36  
    // 
    // version: 1102.614
    // discuss at: http://phpjs.org/functions/base_convert
    // +   original by: Philippe Baumann
    // +   improved by: Rafał Kukawski (http://blog.kukawski.pl)
    // *     example 1: base_convert('A37334', 16, 2);
    // *     returns 1: '101000110111001100110100'
    return parseInt(number + '', frombase | 0).toString(tobase | 0);
}

function dechex (number) {
    // Returns a string containing a hexadecimal representation of the given number  
    // 
    // version: 1102.614
    // discuss at: http://phpjs.org/functions/dechex
    // +   original by: Philippe Baumann
    // +   bugfixed by: Onno Marsman
    // +   improved by: http://stackoverflow.com/questions/57803/how-to-convert-decimal-to-hex-in-javascript
    // +   input by: pilus
    // *     example 1: dechex(10);
    // *     returns 1: 'a'
    // *     example 2: dechex(47);
    // *     returns 2: '2f'
    // *     example 3: dechex(-1415723993);
    // *     returns 3: 'ab9dc427'
    if (number < 0) {
        number = 0xFFFFFFFF + number + 1;
    }
    return parseInt(number, 10).toString(16);
}

function utf8_encode (argString) {
    // Encodes an ISO-8859-1 string to UTF-8  
    // 
    // version: 1102.614
    // discuss at: http://phpjs.org/functions/utf8_encode
    // +   original by: Webtoolkit.info (http://www.webtoolkit.info/)
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: sowberry
    // +    tweaked by: Jack
    // +   bugfixed by: Onno Marsman
    // +   improved by: Yves Sucaet
    // +   bugfixed by: Onno Marsman
    // +   bugfixed by: Ulrich
    // *     example 1: utf8_encode('Kevin van Zonneveld');
    // *     returns 1: 'Kevin van Zonneveld'
    var string = (argString + ''); // .replace(/\r\n/g, "\n").replace(/\r/g, "\n");
    var utftext = "",
        start, end, stringl = 0;
 
    start = end = 0;
    stringl = string.length;
    for (var n = 0; n < stringl; n++) {
        var c1 = string.charCodeAt(n);
        var enc = null;
 
        if (c1 < 128) {
            end++;
        } else if (c1 > 127 && c1 < 2048) {
            enc = String.fromCharCode((c1 >> 6) | 192) + String.fromCharCode((c1 & 63) | 128);
        } else {
            enc = String.fromCharCode((c1 >> 12) | 224) + String.fromCharCode(((c1 >> 6) & 63) | 128) + String.fromCharCode((c1 & 63) | 128);
        }
        if (enc !== null) {
            if (end > start) {
                utftext += string.slice(start, end);
            }
            utftext += enc;
            start = end = n + 1;
        }
    }
 
    if (end > start) {
        utftext += string.slice(start, stringl);
    }
 
    return utftext;
}

function base64_decode (data) {
    // Decodes string using MIME base64 algorithm  
    // 
    // version: 1102.614
    // discuss at: http://phpjs.org/functions/base64_decode
    // +   original by: Tyler Akins (http://rumkin.com)
    // +   improved by: Thunder.m
    // +      input by: Aman Gupta
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   bugfixed by: Onno Marsman
    // +   bugfixed by: Pellentesque Malesuada
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +      input by: Brett Zamir (http://brett-zamir.me)
    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // -    depends on: utf8_decode
    // *     example 1: base64_decode('S2V2aW4gdmFuIFpvbm5ldmVsZA==');
    // *     returns 1: 'Kevin van Zonneveld'
    // mozilla has this native
    // - but breaks in 2.0.0.12!
    //if (typeof this.window['btoa'] == 'function') {
    //    return btoa(data);
    //}
    var b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
    var o1, o2, o3, h1, h2, h3, h4, bits, i = 0,
        ac = 0,
        dec = "",
        tmp_arr = [];
 
    if (!data) {
        return data;
    }
 
    data += '';
 
    do { // unpack four hexets into three octets using index points in b64
        h1 = b64.indexOf(data.charAt(i++));
        h2 = b64.indexOf(data.charAt(i++));
        h3 = b64.indexOf(data.charAt(i++));
        h4 = b64.indexOf(data.charAt(i++));
 
        bits = h1 << 18 | h2 << 12 | h3 << 6 | h4;
 
        o1 = bits >> 16 & 0xff;
        o2 = bits >> 8 & 0xff;
        o3 = bits & 0xff;
 
        if (h3 == 64) {
            tmp_arr[ac++] = String.fromCharCode(o1);
        } else if (h4 == 64) {
            tmp_arr[ac++] = String.fromCharCode(o1, o2);
        } else {
            tmp_arr[ac++] = String.fromCharCode(o1, o2, o3);
        }
    } while (i < data.length);
 
    dec = tmp_arr.join('');
    dec = this.utf8_decode(dec);
 
    return dec;
}

function base64_encode (data) {
    // Encodes string using MIME base64 algorithm  
    // 
    // version: 1102.614
    // discuss at: http://phpjs.org/functions/base64_encode
    // +   original by: Tyler Akins (http://rumkin.com)
    // +   improved by: Bayron Guevara
    // +   improved by: Thunder.m
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   bugfixed by: Pellentesque Malesuada
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // -    depends on: utf8_encode
    // *     example 1: base64_encode('Kevin van Zonneveld');
    // *     returns 1: 'S2V2aW4gdmFuIFpvbm5ldmVsZA=='
    // mozilla has this native
    // - but breaks in 2.0.0.12!
    //if (typeof this.window['atob'] == 'function') {
    //    return atob(data);
    //}
    var b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
    var o1, o2, o3, h1, h2, h3, h4, bits, i = 0,
        ac = 0,
        enc = "",
        tmp_arr = [];
 
    if (!data) {
        return data;
    }
 
    data = this.utf8_encode(data + '');
 
    do { // pack three octets into four hexets
        o1 = data.charCodeAt(i++);
        o2 = data.charCodeAt(i++);
        o3 = data.charCodeAt(i++);
 
        bits = o1 << 16 | o2 << 8 | o3;
 
        h1 = bits >> 18 & 0x3f;
        h2 = bits >> 12 & 0x3f;
        h3 = bits >> 6 & 0x3f;
        h4 = bits & 0x3f;
 
        // use hexets to index into b64, and append result to encoded string
        tmp_arr[ac++] = b64.charAt(h1) + b64.charAt(h2) + b64.charAt(h3) + b64.charAt(h4);
    } while (i < data.length);
 
    enc = tmp_arr.join('');
 
    switch (data.length % 3) {
    case 1:
        enc = enc.slice(0, -2) + '==';
        break;
    case 2:
        enc = enc.slice(0, -1) + '=';
        break;
    }
 
    return enc;
}

// Zavrie detail fotografie
function closeDetail() {
	
	document.getElementById('photo-detail').style.display = 'none';
	document.getElementById('image-c').src = "";
	document.getElementById('image-g').src = "";
	document.getElementById('image-s').src = "";
}

// Validacia formu
function validate(form1) {
	if (form1.pieces.value == '' || form1.pieces.value < 1) {
		alert("Zadejte, prosím, počet kusů.");
		return false; 
	}
	if (form1.size.value == 0) {
		alert("Vyberte, prosím, rozměr fotografie.");
		return false; 
	}

	return true;
}

// zaskrtne obraz, ak sa zmenil pocet kusov a je to cele cislo > 0
function checkImage(i) {

	var pieces = $("#pieces-"+i).val();

	if (!pieces.toString().search(/^[0-9]+$/) == 0 || pieces < 1 || pieces == '') {
		alert("Zadejte, prosím, počet kusů.");
		$("#pieces-"+i).val('0');
		$("#image-"+i).attr('checked', false);
	} else {
//		alert('a1');
		$("#image-"+i).attr('checked', true);
	}
}

function checkPlatno(i) {
	
	var pieces = $("#pieces-platno-"+i).val();
//	alert(pieces);

	if (!pieces.toString().search(/^[0-9]+$/) == 0 || pieces < 1 || pieces == '') {
		alert("Zadejte, prosím, počet kusů.");
		$("#pieces-platno-"+i).val('0');
		$("#image-platno-"+i).attr('checked', false);
	} else {
//		alert('a1');
		$("#image-platno-"+i).attr('checked', true);
	}
}

function checkFoto(i) {
	
	var pieces = $("#pieces-foto-"+i).val();

	if (!pieces.toString().search(/^[0-9]+$/) == 0 || pieces < 1 || pieces == '') {
		alert("Zadejte, prosím, počet kusů.");
		$("#pieces-foto-"+i).val('0');
		$("#image-foto-"+i).attr('checked', false);
	} else {
//		alert('a1');
		$("#image-foto-"+i).attr('checked', true);
	}
}
//-----------------------------------------------------------------------------------
function rotatePlagat() {

	var request;
	var rot = document.form1.rotateSelect.value;

	if (rot == 0 || rot == '0') {
		return false;
	}

	var file = document.form1.file.value;
	var key = document.form1.key.value;

	try {
		request = new XMLHttpRequest();
	} catch(e) {

		try {
			request = ActiveXObject("Msxml2.XMLHTTP");
		} catch(e) {

			try {
				request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				alert("Error!");
				return false;
			}
		}
	}

	request.onreadystatechange = function(){
		if(request.readyState == 4){
			var r = request.responseText;
			document.getElementById('loading').style.display = 'none';
			var hiddenRotate = document.form1.rotate.value;
			document.form1.rotate.value = (parseInt(hiddenRotate) + parseInt(document.form1.rotateSelect.value)) % 360;
			rotation[imageId] = parseInt(document.form1.rotate.value);
			document.form1.rotateSelect.selectedIndex = 0;
//			document.form1.border.selectedIndex = 0;
			document.form1.colority.selectedIndex = 0;
			document.getElementById('border-1').style.display = 'none';

			var file = document.form1.file.value;
			var key = document.form1.key.value;
			var a = "http://babyphoto.themis.redcat.cz/image.php?file=" + file + "&key=" + key + "&size=";

			var c = a + "800x600";
			var g = a + "800x600g";
			var s = a + "800x600s";

			var imgC = document.getElementById('image-c');
			var imgG = document.getElementById('image-g');
			var imgS = document.getElementById('image-s');

			imgC.style.width = '';
			imgC.style.height = '';

			imgG.style.width = '';
			imgG.style.height = '';

			imgS.style.width = '';
			imgS.style.height = '';

			var now = new Date();

			imgC.src = c + '&' + now.getTime();
			imgG.src = g + '&' + now.getTime();
			imgS.src = s + '&' + now.getTime();

			imgC = document.getElementById('image-c');

			var height = '400px';
			var width = '266px';
			var ram = 'h';
//			alert(imgC.clientWidth + ' x ' + imgC.clientHeight);
			if (r == 'w') {
				height = '266px';
				width = '400px';
				ram = 'w';
			}
			//alert(imgC.clientWidth+'x'+imgC.clientHeight);
			imgC.style.width = width;
			imgC.style.height = height;
			
			imgG.style.width = width;
			imgG.style.height = height;
			
			imgS.style.width = width;
			imgS.style.height = height;

			document.getElementById('image-color').style.display = '';
			document.getElementById('image-greyscale').style.display = 'none';
			document.getElementById('image-sepia').style.display = 'none';

			// --- aplikacia ramceka
//			var t = setTimeout("borderize()", 250);
//			borderize();
//			alert('b');
			// refresh thumb
			borderize(ram);
			
			var thumb = document.getElementById('thumb-' + document.form1.imageId.value);
			thumb.src = a + '100x75&' + now.getTime();
			
			if (r == 'w') {
				$('#i-'+key).addClass('wide');
			} else {
				$('#i-'+key).removeClass('wide');
			}
		}
	}

	// selected
	var rotate = document.form1.rotateSelect.value;
	var imageId = document.form1.imageId.value;

	var queryString = "?file=" + file + "&key=" + key + "&rotate=" + rotate + "&imageId=" + imageId;

	document.getElementById('loading').style.display = '';
	request.open("GET", "tools/rotate.php" + queryString, true);
	request.send(null);
}
//-----------------------------------------------------------------------------------
function rotateSimple() {

	var request;
	var rot = document.form1.rotateSelect.value;

	if (rot == 0 || rot == '0') {
		return false;
	}

	var file = document.getElementById('file').value;
	var key = document.getElementById('key').value;

	try {
		request = new XMLHttpRequest();
	} catch(e) {

		try {
			request = ActiveXObject("Msxml2.XMLHTTP");
		} catch(e) {

			try {
				request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				alert("Error!");
				return false;
			}
		}
	}

	request.onreadystatechange = function(){
		if(request.readyState == 4){
			var r = request.responseText;
			document.getElementById('loading').style.display = 'none';
			var hiddenRotate = document.getElementById('rotate').value;
			document.form1.rotate.value = (parseInt(hiddenRotate) + parseInt(document.getElementById('rotateSelect').value)) % 360;
			rotation[imageId] = parseInt(document.getElementById('rotate').value);
			
			var file = document.getElementById('file').value;
			var key = document.getElementById('key').value;
			var a = "http://babyphoto.themis.redcat.cz/image.php?file=" + file + "&key=" + key + "&size=";

			var c = a + "800x600";
			var g = a + "800x600g";
			var s = a + "800x600s";

			var imgC = document.getElementById('image-c');

			imgC.style.width = '';
			imgC.style.height = '';

			var now = new Date();

			imgC.src = c + '&' + now.getTime();

			imgC = document.getElementById('image-c');

			var height = '400px';
			var width = '266px';
			if (imgC.clientWidth > imgC.clientHeight) {
				height = '266px';
				width = '400px';
			}
			
			document.getElementById('image-color').style.display = '';
			document.getElementById('rotateSelect').selectedIndex = 0;
			// refresh thumb
			var thumb = document.getElementById('thumb-' + document.getElementById('imageId').value);
			thumb.src = a + '100x75&' + now.getTime();
			
			if (r == 'w') {
				$('#i-'+key).addClass('wide');
			} else {
				$('#i-'+key).removeClass('wide');
			}
		}
	}

	// selected
	var rotate = document.getElementById('rotateSelect').value;
	var imageId = document.getElementById('imageId').value;

	var queryString = "?file=" + file + "&key=" + key + "&rotate=" + rotate + "&imageId=" + imageId;

	document.getElementById('loading').style.display = '';
	request.open("GET", "tools/rotate.php" + queryString, true);
	request.send(null);
}
//-----------------------------------------------------------------------------------
function rotateKlicenka() {

	var request;
	var rot = document.form1.rotateSelect.value;

	if (rot == 0 || rot == '0') {
		return false;
	}

	var file = document.form1.file.value;
	var key = document.form1.key.value;

	try {
		request = new XMLHttpRequest();
	} catch(e) {

		try {
			request = ActiveXObject("Msxml2.XMLHTTP");
		} catch(e) {

			try {
				request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				alert("Error!");
				return false;
			}
		}
	}

	request.onreadystatechange = function(){
		if(request.readyState == 4){
			var r = request.responseText;
			document.getElementById('loading').style.display = 'none';
			var hiddenRotate = document.form1.rotate.value;
			document.form1.rotate.value = (parseInt(hiddenRotate) + parseInt(document.form1.rotateSelect.value)) % 360;
			rotation[imageId] = parseInt(document.form1.rotate.value);
			document.form1.rotateSelect.selectedIndex = 0;
			document.form1.colority.selectedIndex = 0;
//			document.form1.border.selectedIndex = 0;
//			document.getElementById('border-1').style.display = 'none';

			document.getElementById('image-color').style.display = '';
			document.getElementById('image-greyscale').style.display = 'none';
			document.getElementById('image-sepia').style.display = 'none';
			
			var file = document.form1.file.value;
			var key = document.form1.key.value;
			var a = "http://babyphoto.themis.redcat.cz/image.php?file=" + file + "&key=" + key + "&size=";

			var c = a + "800x600";
			var g = a + "800x600g";
			var s = a + "800x600s";

			var imgC = document.getElementById('image-c');
			var imgG = document.getElementById('image-g');
			var imgS = document.getElementById('image-s');

			imgC.style.width = '';
			imgC.style.height = '';

			imgG.style.width = '';
			imgG.style.height = '';

			imgS.style.width = '';
			imgS.style.height = '';

			var now = new Date();

			imgC.src = c + '&' + now.getTime();
			imgG.src = g + '&' + now.getTime();
			imgS.src = s + '&' + now.getTime();

			imgC = document.getElementById('image-c');

			var height = '400px';
			var width = '266px';
			var ram = 'h';
//			alert(imgC.clientWidth + ' x ' + imgC.clientHeight);
			if (r == 'w') {
				height = '266px';
				width = '400px';
				ram = 'w';
			}
//			alert(imgC.clientWidth + ' x ' + imgC.clientHeight);
			imgC.style.width = width;
			imgC.style.height = height;
			
			imgG.style.width = width;
			imgG.style.height = height;
			
			imgS.style.width = width;
			imgS.style.height = height;

			// refresh thumb
			var thumb = document.getElementById('thumb-' + document.form1.imageId.value);
			thumb.src = a + '100x75&' + now.getTime();
			
			if (r == 'w') {
				$('#i-'+key).addClass('wide');
			} else {
				$('#i-'+key).removeClass('wide');
			}
			
//			borderize(ram);
//			window.location.reload();
		}
	}

	// selected
	var rotate = document.form1.rotateSelect.value;
	var imageId = document.form1.imageId.value;

	var queryString = "?file=" + file + "&key=" + key + "&rotate=" + rotate + "&imageId=" + imageId;

	document.getElementById('loading').style.display = '';
	request.open("GET", "tools/rotate.php" + queryString, true);
	request.send(null);
}
//-----------------------------------------------------------------------------------
function rotateBlahoprani() {

	var request;
	var rot = document.form1.rotateSelect.value;

	if (rot == 0 || rot == '0') {
		return false;
	}

	var file = document.form1.file.value;
	var key = document.form1.key.value;

	try {
		request = new XMLHttpRequest();
	} catch(e) {

		try {
			request = ActiveXObject("Msxml2.XMLHTTP");
		} catch(e) {

			try {
				request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				alert("Error!");
				return false;
			}
		}
	}

	request.onreadystatechange = function(){
		if(request.readyState == 4){
			var r = request.responseText;
			document.getElementById('loading').style.display = 'none';
			var hiddenRotate = document.form1.rotate.value;
			document.form1.rotate.value = (parseInt(hiddenRotate) + parseInt(document.form1.rotateSelect.value)) % 360;
			rotation[imageId] = parseInt(document.form1.rotate.value);
			document.form1.rotateSelect.selectedIndex = 0;
			document.form1.colority.selectedIndex = 0;
//			document.form1.border.selectedIndex = 0;
//			document.getElementById('border-1').style.display = 'none';

			document.getElementById('image-color').style.display = '';
			document.getElementById('image-greyscale').style.display = 'none';
			document.getElementById('image-sepia').style.display = 'none';
			
			var file = document.form1.file.value;
			var key = document.form1.key.value;
			var a = "http://babyphoto.themis.redcat.cz/image.php?file=" + file + "&key=" + key + "&size=";

			var c = a + "800x600";
			var g = a + "800x600g";
			var s = a + "800x600s";

			var imgC = document.getElementById('image-c');
			var imgG = document.getElementById('image-g');
			var imgS = document.getElementById('image-s');

			imgC.style.width = '';
			imgC.style.height = '';

			imgG.style.width = '';
			imgG.style.height = '';

			imgS.style.width = '';
			imgS.style.height = '';

			var now = new Date();

			imgC.src = c + '&' + now.getTime();
			imgG.src = g + '&' + now.getTime();
			imgS.src = s + '&' + now.getTime();

			imgC = document.getElementById('image-c');

			var height = '400px';
			var width = '266px';
			var ram = 'h';
//			alert(imgC.clientWidth + ' x ' + imgC.clientHeight);
			if (r == 'w') {
				height = '266px';
				width = '400px';
				ram = 'w';
			}
//			alert(imgC.clientWidth + ' x ' + imgC.clientHeight);
			imgC.style.width = width;
			imgC.style.height = height;
			
			imgG.style.width = width;
			imgG.style.height = height;
			
			imgS.style.width = width;
			imgS.style.height = height;

			// refresh thumb
			var thumb = document.getElementById('thumb-' + document.form1.imageId.value);
			thumb.src = a + '100x75&' + now.getTime();
			
			if (r == 'w') {
				$('#i-'+key).addClass('wide');
			} else {
				$('#i-'+key).removeClass('wide');
			}
			changeValPrice();
		}
	}

	// selected
	var rotate = document.form1.rotateSelect.value;
	var imageId = document.form1.imageId.value;

	var queryString = "?file=" + file + "&key=" + key + "&rotate=" + rotate + "&imageId=" + imageId;

	document.getElementById('loading').style.display = '';
	request.open("GET", "tools/rotate.php" + queryString, true);
	request.send(null);
}
//-----------------------------------------------------------------------------------
// --- zisti vysku a sirku obrazku
function getImageWidth(imgsrc) {
		
	var t = new Image();
	t.src = $('#'+imgsrc).attr('src');
//	alert('w: ' + t.width);
	return t.width;
}
//-----------------------------------------------------------------------------------
function getImageHeight(imgsrc) {

	var t = new Image();
	t.src = $('#'+imgsrc).attr('src');;
//	alert('h:' + t.height);
	return t.height;
}
//-----------------------------------------------------------------------------------
function limitText(limitField, limitCount, limitNum) {
	if (limitField.value.length > limitNum) {
		limitField.value = limitField.value.substring(0, limitNum);
	} else {
		limitCount.value = limitNum - limitField.value.length;
	}
}
//-----------------------------------------------------------------------------------
function saveRamcek() {
	
	var $imageId = $('#imageId').val();
	var $selected = $('#border').val();
	$('#check-'+$imageId).val($selected);
	$('#check-'+$imageId).attr('checked', true);
	if ($selected == 0) {
		$('#span-'+$imageId).html('');
	} else {
		$('#span-'+$imageId).html('Rámeček '+$selected);
	}
	closeDetail();
}
//-----------------------------------------------------------------------------------
function deleteFromKlicenka(item, id) {
	$.ajax({
		type: 'POST',
		url: BASE__URL+"tools/delete_from_klicenka.php",
		data: 'itemId='+item,
		success: function(data){
			$('#thumb0'+id).removeAttr('style');
			$('#thumb0'+id).html('');
			if ($('#thumb01').html() == '' || $('#thumb02').html() == '') {
				$('#klicenkaButton').hide();
			}
		}
	});
}
//-----------------------------------------------------------------------------------
function addKlicenkaToCart() {
	$.ajax({
		type: 'POST',
		url: BASE__URL+"tools/add_klicenka_to_cart.php",
		success: function(data){
			$('#count').html(data);
			alert('Úspešně vloženo.');
			$('#thumb01').html('');
			$('#thumb02').html('');
			$('#thumb01').removeAttr('style');
			$('#thumb02').removeAttr('style');
			$('#klicenkaButton').hide();
		}
	});
}
//-----------------------------------------------------------------------------------
function in_array(val, arr) { 
	for (i = 0; i < arr.length; i++) {
		if (val == arr[i]) return true; 
	}
	return false; 
}
//-----------------------------------------------------------------------------------
function checkFotokniha(id) {
	
	var $pole = parseInt($('#pole-'+id).val());
	
	var $count = 0;
	$('.foto-image').each(function() {
		var elemId = $(this).attr('id').replace('image-','');
		if ($(this).is(':checked')) {
			if (parseInt($('#pole-'+elemId).val()) > 0) {
				$count = $count + parseInt($('#pole-'+elemId).val());
			}
		}
	});
	
	if ($count <= (24-$pole)) {
		if ($pole > 0) {
			switch ($pole) {
				case 1:
				case 2:
				case 4:
					$('#image-'+id).attr('checked', true);
					break;
				default:
					alert('Pole môže být jenom 1, 2, 4.');
					$('#pole-'+id).val('0');
					$('#image-'+id).attr('checked', false);
					break
			}
		} else {
			$('#pole-'+id).val('0');
			$('#image-'+id).attr('checked', false);
		}
	} else {
		alert('Maximální počet polí je 24.');
		$('#pole-'+id).val('0');
		$('#image-'+id).attr('checked', false);
	}
}
//-----------------------------------------------------------------------------------
function getFotoknihaCount() {
	
	var $count = 0;
//	$('.foto-image').each(function() {
//		var elemId = $(this).attr('id').replace('image-','');
//		if ($(this).is(':checked')) {
//			if (parseInt($('#pole-'+elemId).val()) > 0) {
//				$count = $count + parseInt($('#pole-'+elemId).val());
//			}
//		}
//	});
	$('.foto-pole').each(function() {
		$count = $count + parseInt($(this).val());
	});
	return $count;
}
//-----------------------------------------------------------------------------------
function countFotokniha(id) {
	
	var $pole = parseInt($('#pole-'+id).val());

	var $count = getFotoknihaCount();
	
	if ($count > 24) {
		alert('Maximální počet polí je 24.');
		$('#pole-'+id).val('0');
	}
	
	var $count = getFotoknihaCount();
	
	var $remain = 24 - $count;
	
	if ($pole > 0) {
		$('#poradi-'+id).attr('disabled', false);
	} else {
		$('#poradi-'+id).val('0')
		$('#poradi-'+id).attr('disabled', true);
	}

	$('#counterFotoKniha').val($remain);
}
//-----------------------------------------------------------------------------------
function validateFotokniha() {
	
	var $control = true;
	
	$('.foto-pole').each(function() {
		if (parseInt($(this).val()) > 0) {
			var $id = $(this).attr('id').replace('pole-','');
			if (parseInt($('#poradi-'+$id).val()) < 1) {
				alert('Nevybrali jste pořadí fotografie ve fotoknize');
				$('#pole-'+$id).focus();
				$control = false;
				return;
			}
		}
	});
	return $control;
}
//-----------------------------------------------------------------------------------
//-----------------------------------------------------------------------------------
//-----------------------------------------------------------------------------------
//-----------------------------------------------------------------------------------
