function rawurlencode (str) {
    // http://kevin.vanzonneveld.net
    // +   original by: Brett Zamir (http://brett-zamir.me)
    // +      input by: travc
    // +      input by: Brett Zamir (http://brett-zamir.me)
    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +      input by: Michael Grier
    // +   bugfixed by: Brett Zamir (http://brett-zamir.me)
    // +      input by: Ratheous
    // +      reimplemented by: Brett Zamir (http://brett-zamir.me)
    // %          note 1: This reflects PHP 5.3/6.0+ behavior
    // *     example 1: rawurlencode('Kevin van Zonneveld!');
    // *     returns 1: 'Kevin%20van%20Zonneveld%21'
    // *     example 2: rawurlencode('http://kevin.vanzonneveld.net/');
    // *     returns 2: 'http%3A%2F%2Fkevin.vanzonneveld.net%2F'
    // *     example 3: rawurlencode('http://www.google.nl/search?q=php.js&ie=utf-8&oe=utf-8&aq=t&rls=com.ubuntu:en-US:unofficial&client=firefox-a');
    // *     returns 3: 'http%3A%2F%2Fwww.google.nl%2Fsearch%3Fq%3Dphp.js%26ie%3Dutf-8%26oe%3Dutf-8%26aq%3Dt%26rls%3Dcom.ubuntu%3Aen-US%3Aunofficial%26client%3Dfirefox-a'
 
    var hexStr = function (dec) {
        return '%' + dec.toString(16).toUpperCase();
    };
 
    var ret = '',
            unreserved = /[\w.~-]/; // A-Za-z0-9_.~-
    str = (str+'').toString();
 
    for (var i = 0, dl = str.length; i < dl; i++) {
        var ch = str.charAt(i);
        if (unreserved.test(ch)) {
            ret += ch;
        }
        else {
            var code = str.charCodeAt(i);
            // Reserved assumed to be in UTF-8, as in PHP
            if (code < 128) { // 1 byte
                ret += hexStr(code);
            }
            else if (code >= 128 && code < 2048) { // 2 bytes
                ret += hexStr((code >> 6) | 0xC0);
                ret += hexStr((code & 0x3F) | 0x80);
            }
            else if (code >= 2048 && code < 65536) { // 3 bytes
                ret += hexStr((code >> 12) | 0xE0);
                ret += hexStr(((code >> 6) & 0x3F) | 0x80);
                ret += hexStr((code & 0x3F) | 0x80);
            }
            else if (code >= 65536) { // 4 bytes
                ret += hexStr((code >> 18) | 0xF0);
                ret += hexStr(((code >> 12) & 0x3F) | 0x80);
                ret += hexStr(((code >> 6) & 0x3F) | 0x80);
                ret += hexStr((code & 0x3F) | 0x80);
            }
        }
    }
    return ret;
}

/**
 * function remaining_characters()
 *
 * check element's value against defined limit, cut everything what is above
 * that limit and display number of characters remaining
 *
 * @param string element (source element Id)
 * @param int limin (length limit)
 * @param string display_element (element to display remaining strings count) 
 */
function remaining_characters(element, limit, display_element) {

	/**
	 * both source and and remaining characters element must exist
	 */
	if (document.getElementById(element) && document.getElementById(display_element)) {

		/**
		 * get content length
		 */
		var length = document.getElementById(element).value.length;

		/**
		 * if content length is higher than defined limit, cut it to defined
		 * length
		 */
		if (limit - length < 0) {
			document.getElementById(element).value = document.getElementById(element).value.substring(0, limit);
		}

		/**
		 * display remaining length
		 */
		document.getElementById(display_element).innerHTML = limit - length;
	}
}


/**
 * function add_image()
 *
 * add another fild for image upload up to 10 of them
 *
 * no params needed ;-)
 */
function add_image(max_images) {
	for (var i = 0; i < max_images; i++) {
		if (!document.getElementById("image" + i)) {
			var image_field = document.createElement("input");
			image_field.type = "file";
			image_field.name = "image[]";
			image_field.id = "image" + i;
			image_field.className = "file";
			image_field.size = 53;
			document.getElementById("image_container").appendChild(image_field);
			break;
		}
	}
}

function url_remove(url, what) {
	var index = url.indexOf(what);
	if (index != -1) {
		var pos = index + 1;
		while (pos < url.length && url.substr(pos, 1) != "/") pos++;
		
		if (pos != url.length) return url.substr(0, index) + url.substr(pos + 1);
		else return url.substr(0, index);
	}
	return url;
}

var pf_man_flag = false;

function product_filter_all_manufacturer() {
	switch (document.getElementById("manufacutrers_rest").style.display) {
		case "block":
			document.getElementById("manufacutrers_rest").style.display = "none";
			document.getElementById("all_manufacturer").innerHTML = "Více &gt;&gt;";
			break;

		case "none":
			document.getElementById("manufacutrers_rest").style.display = "block";
			document.getElementById("all_manufacturer").innerHTML = "&lt;&lt; Méně";
			break;
	}
	document.getElementById("all_manufacturer").blur();

/*
	pf_man_flag = !pf_man_flag;
	
	var pos, d = document.getElementsByTagName("input");
	for (pos = 0; pos < d.length; pos++) {
		if (d[pos].type == "checkbox" && d[pos].name == "manufacturer[]") d[pos].checked = pf_man_flag;
	}
	
	var href = document.getElementById("all_manufacturer");
	if (href != null) href.innerHTML = pf_man_flag ? "Méně &lt;&lt;" : "Více &gt;&gt;";
*/
}

function product_filter_set_manufacturer(value) {
	var founded = false, pos = 0, d = document.getElementsByTagName("input");
	
	while (!founded && pos < d.length) {
		if (d[pos].type == "checkbox" && d[pos].name == "manufacturer[]" && d[pos].value == value) founded = true;
		else pos++;
	}
	
	if (founded) d[pos].checked = !d[pos].checked;
}

/**
 * function product_filter()
 *
 * get filtering conditions, create path and redirect user to that path
 *
 * no params needed ;-)
 */
function product_filter(to_rem) {
	
	var url = "http://zbozi." + document.getElementById("domain").value;
	if (document.getElementById("path").value != "") url += "/" + document.getElementById("path").value;
	//if (document.getElementById("order_min_price") != null && document.getElementById("order_min_price").checked == true) url += "/seradit-od-nejlevnejsiho";
	//if (document.getElementById("order_max_price") != null && document.getElementById("order_max_price").checked == true) url += "/seradit-od-nejdrazsiho";
	
	var obox = document.getElementById("order_box");
	if (obox != null) {
		switch (obox.options[obox.selectedIndex].value) {
			case "order_min_price" :
				url += "/seradit-od-nejlevnejsiho";
				break;
				
			case "order_max_price" :
				url += "/seradit-od-nejdrazsiho";
				break;
				
			case "order_favourite" :
				url += "/seradit-podle-oblibenosti";
				break;
		}
	}

	if (to_rem != "min_price" && document.getElementById("min_price") != null && document.getElementById("min_price").value != "" && document.getElementById("min_price").value != document.getElementById("product_min_price").value) url += "/cena-od-" + document.getElementById("min_price").value;
	if (to_rem != "max_price" && document.getElementById("max_price") != null && document.getElementById("max_price").value != "" && document.getElementById("max_price").value != document.getElementById("product_max_price").value) url += "/cena-do-" + document.getElementById("max_price").value;

	if (to_rem != "product_name" && document.getElementById("product_name") != null && document.getElementById("product_name").value != "") url += "/nazev-" + rawurlencode(document.getElementById("product_name").value);
	
	var pref = null, f, d = document.getElementsByTagName("input"), man = "";
	
	if (to_rem != null && to_rem.substr(0, 12) == "manufacturer") pref = to_rem.substr(13);
	
	for (f = 0; f < d.length; f++) {
		if ((pref == null || pref != d[f].value) && d[f].type == "checkbox" && d[f].name == "manufacturer[]" && d[f].checked) {
			if (man != "") man += "|";
			man += rawurlencode(d[f].value);
		}
	}
	
	if (man != "") url += "/vyrobce-" + man;
	window.parent.window.location.href = url;
}

function other_filter_toggle() {
	var ca = document.getElementById("other_filter");
	var cb = document.getElementById("other_filter_href");
	
	if (ca.style.display == "block") {
		ca.style.display = "none";
		cb.innerHTML = "Zobrazit další filtry &gt;&gt;";
	} else {
		ca.style.display = "block";
		cb.innerHTML = "&lt;&lt; Skrýt další filtry";
	}
}

function sales_filter(to_rem) {
	
	var url = "http://bazar." + document.getElementById("domain").value;
	if (document.getElementById("path").value != "") url += "/" + document.getElementById("path").value;
	
	var obox = document.getElementById("order_box");
	if (obox != null) {
		switch (obox.options[obox.selectedIndex].value) {
			case "order_new" :
				url += "/seradit-od-nejnovejsich";
				break;
				
			case "order_old" :
				url += "/seradit-od-nejstarsich";
				break;
				
			case "order_min_price" :
				url += "/seradit-od-nejnizsi-ceny";
				break;
				
			case "order_max_price" :
				url += "/seradit-od-nejvyssi-ceny";
				break;
		}
	}
	
	if (to_rem != "min_price" && document.getElementById("min_price") != null && document.getElementById("min_price").value != "") url += "/cena-od-" + document.getElementById("min_price").value;
	if (to_rem != "max_price" && document.getElementById("max_price") != null && document.getElementById("max_price").value != "") url += "/cena-do-" + document.getElementById("max_price").value;
	
	var o_type = document.getElementById("type");
	if (to_rem != "type" && o_type != null && o_type.options[o_type.selectedIndex].value != "") url += "/kde-vyhledavat-" + o_type.options[o_type.selectedIndex].value;
	
	var o_status = document.getElementById("status");
	if (to_rem != "status" && o_status != null && o_status.options[o_status.selectedIndex].value != "") url += "/stav-zbozi-" + o_status.options[o_status.selectedIndex].value;
	
	var o_location = document.getElementById("location");
	if (to_rem != "location" && o_location != null && o_location.options[o_location.selectedIndex].value != "") url += "/lokalita-" + o_location.options[o_location.selectedIndex].value;
	
	if (to_rem != "query_text" && document.getElementById("query_text") != null && document.getElementById("query_text").value != "") url += "/nazev-" + rawurlencode(document.getElementById("query_text").value);
	if (to_rem != "query_number" && document.getElementById("query_number") != null && document.getElementById("query_number").value != "") url += "/cislo-" + rawurlencode(document.getElementById("query_number").value);
	if (to_rem != "query_user" && document.getElementById("query_user") != null && document.getElementById("query_user").value != "") url += "/jmeno-" + rawurlencode(document.getElementById("query_user").value);
	
	window.parent.window.location.href = url;
	
}

/**
 * function ig()
 *
 * initialize object, set following properties (only customizable ones are listed):
 *  .step_visible - ammount of visible images
 *  .step_length - how many total pixels to scrol within one step
 *  .move_length - how many pixels to move in one cycle
 */
function ig() {

	this.left = 0;
	this.next_left = 0;
	this.direction = null;
	this.moving = false;
	this.selected = null;
	this.ii = 0;

	this.step_visible = 3;
	this.step_length = 83;
	this.move_length = 20;
	this.next_move = 0;

	if (document.getElementById("igc")) {
		this.igc = document.getElementById("igc");
		this.step_max = this.igc.childNodes.length;
		this.igc.style.width = this.step_max * this.step_length + "px";
	}

	/**
	 * function ig_scroll()
	 *
	 * check if scroll can be done. if true, call move function
	 *
	 * @param string direction "scrolling direction (left|right)"
	 */
	this.ig_scroll = function(direction) {

		/**
		 * continue only if object is not moving right now
		 */
		if (this.moving == false) {
			this.direction = direction;

			/**
			 * get next target left position and move length
			 */
			this.next_left = this.direction == "left" ? this.left + this.step_length : this.left - this.step_length;
			this.next_move = this.direction == "left" ? -1 * this.move_length : this.move_length;

			/**
			 * check if we really can scroll
			 */
			if (this.next_left >= (this.step_max - this.step_visible) * -1 * this.step_length && this.next_left <= 0) {
				this.moving = true;
				this.ig_move();
			}
		}
	}

	/**
	 * function ig_move()
	 *
	 * move element until this.next_left limit reached
	 *
	 * no params needed ;-)
	 */
	this.ig_move = function() {

		/**
		 * set new left position
		 */
		this.left -= this.next_move;
		this.igc.style.left = this.left + "px";

		/**
		 * if we've reached next limit, stop scrolling and adjust position by that limit.
		 * else repeat itself within 10 milliseconds
		 */
		if ((this.direction == "right" && this.left > this.next_left) || (this.direction == "left" && this.left < this.next_left)) {
			setTimeout("this.ig_move();", 10);
		} else {
			this.left = this.next_left;
			this.igc.style.left = this.left + "px";
			this.moving = false;
		}
	}


	/**
	 * function ig_select()
	 *
	 * select one image, replace first image with selected
	 *
	 * @param int ifo
	 * @param string ifi
	 * @param int ii	 	 
	 */
	this.ig_select = function(ifo, ifi, ii) {
		if (document.getElementById("ig_main_image")) {

			/**
			 * hide main image, set action when image is loaded
			 */
			document.getElementById("ig_main_image").style.visibility = "hidden";
			document.getElementById("ig_loader").style.visibility = "visible";

			document.getElementById("ig_main_image").onload = function() {
				document.getElementById("ig_main_image").style.visibility = "visible";
				document.getElementById("ig_loader").style.visibility = "hidden";
			}

			/**
			 * display new image, set selected image
			 */
			document.getElementById("ig_main_image").src = "/tn.php?ifo=" + ifo + "&ifi=" + ifi + "&ity=2";
			document.getElementById("ig_main_image_link").href = "/images/ads/" + ifo + "/" + ifi;
			this.selected = ifi;
			this.ii = ii;
		}
	}


	/**
	 * function ig_lightbox()
	 *
	 * open lightbox by simulating "click" event on selected image
	 *
	 * no params needed ;-)
	 */
	this.ig_lightbox = function() {
		if (document.getElementById("ig_image" + this.ii)) {
			if (document.createEvent) {
				var evt = document.createEvent("MouseEvents");
				evt.initEvent("click", true, false);
				document.getElementById("ig_image" + this.ii).dispatchEvent(evt);
			} else if (document.createEventObject) {
				document.getElementById("ig_image" + this.ii).fireEvent("onclick");
			}
		}
	}
}


/**
 * init image gallery object on window load
 */
window.onload = ig;


/**
 * function open_lightbox()
 *
 * open lightbox by simulating "click" event on first image in gallery
 *
 * no params needed ;-)
 */
function open_lightbox() {
	if (document.getElementById("ig_image1")) {
		if (document.createEvent) {
			var evt = document.createEvent("MouseEvents");
			evt.initEvent("click", true, false);
			document.getElementById("ig_image1").dispatchEvent(evt);
		} else if (document.createEventObject) {
			document.getElementById("ig_image1").fireEvent("onclick");
		}
	}
}


/**
 * function rating_display()
 *
 * change image depending on giver rating
 *
 * @param int rating "rating"
 */
function rating_display(rating) {
	document.getElementById("custom_rating").src = "/images/layout/main/rating_" + rating + ".jpg";
}


/**
 * function rating_add_display()
 *
 * change image depending on giver rating and user action
 *
 * @param int rating "rating"
 * @param string action "action performed (over|out|click)"
 * @param object element "element which performed action"
 */
var current_rating = 0;
function rating_add_display(rating, action, element) {
	switch (action) {
		case "over":
			document.getElementById("custom_rating").src = "/images/layout/main/rating_" + rating + "_grey.jpg";
			break;

		case "out":
			document.getElementById("custom_rating").src = "/images/layout/main/rating_" + current_rating + "_grey.jpg";
			break;

		case "click":
			current_rating = rating;
			document.getElementById("custom_rating").src = "/images/layout/main/rating_" + rating + "_grey.jpg";
			document.getElementById("rating_" + current_rating).checked = true;
			if (element) element.blur();
			break;
	}
}


/**
 * function toggle_description()
 *
 * toggle short / full description
 *
 * no params needed ;-)
 */
function toggle_description() {
	switch(document.getElementById("description_full").style.display) {
		case "":
			document.getElementById("description_short").style.display = "";
			document.getElementById("description_full").style.display = "none";
			break;

		default:
			document.getElementById("description_short").style.display = "none";
			document.getElementById("description_full").style.display = "";
			break;
	}
}


/**
 * function add_smile()
 *
 * add smile symbol into text
 *
 * @param string smile "smile symbol"
 */
function add_smile(symbol) {
	var e = document.getElementById("text");
	symbol = " " + symbol + " ";

	/**
	 * section for Gecko based browsers
	 */
	if (e.selectionStart || e.selectionStart == 0) {
		var replaced = false;
		var begin = e.selectionStart;
		var end = e.selectionEnd;

		if (end - begin) replaced = true;
		var scroll_top = e.scrollTop;
		var txt = (e.value).substring(begin, end);

		subst = txt + symbol;

		e.value = e.value.substring(0, begin) + subst + e.value.substring(end, e.value.length);
		e.focus();

		if (replaced) {
			var pos = begin + (txt.length + symbol.length);
			e.selectionStart = pos;
			e.selectionEnd = pos;
		} else {
//			e.selectionStart = begin;
			e.selectionEnd = begin + symbol.length;
		}

		e.scrollTop = scroll_top;

	/**
	 * non-Gecko browsers
	 */
	} else {
		var sel = document.selection.createRange().text;
		e.focus();
		document.selection.createRange().text = sel + symbol;
	}

	/**
	 * duplicate selection if supported
	 */
	if (e.createTextRange) e.caretPos = document.selection.createRange().duplicate();
}


/**
 * function sa_switch()
 *
 * show / hide advanced search form depending on current status, change link title and text
 *
 * @no params needed ;-)
 */
function sa_switch() {
	switch (document.getElementById("form_wrapper").style.display) {
		case "block":
			document.getElementById("form_wrapper").style.display = "none";
			document.getElementById("sa_link").title = "Zobrazit vyhledávací formulář";
			document.getElementById("sa_link").innerHTML = "Zobrazit vyhledávací formulář";
			break;

		case "none":
			document.getElementById("form_wrapper").style.display = "block";
			document.getElementById("sa_link").title = "Schovat vyhledávací formulář";
			document.getElementById("sa_link").innerHTML = "Schovat vyhledávací formulář";
			break;
	}

	document.getElementById("sa_link").blur();
}