/*
 * Changebox - jQuery plugin
 *
 * zmeni checkboxy a radiobutton na klikatelne objekty.
 *
 * autor: Gastorm
 */

(function($) {

	function ParamRewrite(node) {
		this.node = node;
		this.init();
	};
	/**
	 * klasicky ceckbox
	 */
	function clickOnCheckbox(param, content){
		var inputId = $(param).attr('id').substr(2);
		if($(param).hasClass('checked')){
			$("#" + inputId).attr('checked', false);
			$(param).removeClass('checked');
		}else{
			$("#" + inputId).attr('checked', true);
			$(param).addClass('checked');
		}
	}

	/**
	 * ceckbox se zobrazuje jako input text
	 */
	function clickOnCheckboxText(param, content){
		var inputId = $(param).attr('id').substr(2) + "_count";
		if($(param).hasClass('checked')){
			$("#" + inputId).attr('value', '');
			$(param).removeClass('checked');
		}else{
			$("#" + inputId).attr('value', 1);
			$(param).addClass('checked');
		}
	}
	
	function clickOnRadio(param, content){
		$(".rewrite_content_button",content).each(function(){
			var inputId = $(this).attr('id').substr(2);
			$("#" + inputId).attr('checked', false);
			$(this).removeClass('checked');
		});
		var inputId = $(param).attr('id').substr(2);
		$("#" + inputId).attr('checked', true);
		$(param).addClass('checked');
	}

	$.extend(ParamRewrite.prototype, {
		init: function() {
			var thisObj = this;
			$("*",thisObj.node).hide();
			var rewriteContent = $("<div>",{'class' : 'rewrite_content'});
			var paramName = $(".param_name", thisObj.node).text();
			var isRadio = thisObj.node.hasClass('radio');
			rewriteContent.append($("<div>",{'class' : 'rewrite_content_name', 'text' : paramName}));
			$("tr.sude, tr.liche",thisObj.node).each(function(){
				var input =	$("input:first", $(this));
				var title = $("label:first", $(this));
				var images = $("img:first", $(this));
				
				var new_elem = $("<div>",{'class' : 'rewrite_content_button', 'id' : "r_" +$(input).attr('id')});
				if($(images).length==1){
					new_elem.addClass("img");
					new_elem.append($("<img>",{'title' : $(title).text(), 'src' : $(images).attr('src')}));
				}else{
					new_elem.append($("<span>",{'text' : $(title).text()}));
				}
				if(isRadio){
					if($(input).attr('checked')){
						new_elem.addClass('checked');
					}
					new_elem.click(function() {
						clickOnRadio(this,rewriteContent);
					});
				}else if($(input).attr('type')=="hidden"){
					if($("#" + $(input).attr('id') + "_count").attr('value')>0){
						new_elem.addClass('checked');
					}
					new_elem.click(function() {
						clickOnCheckboxText(this,rewriteContent);
					});
				}else{
					if($(input).attr('checked')){
						new_elem.addClass('checked');
					}
					new_elem.click(function() {
						clickOnCheckbox(this,rewriteContent);
					});
				}
				rewriteContent.append(new_elem);
			});
			$(this.node).append(rewriteContent);
		}
	});

	$.fn.paramRewrite = function() {
		return this.each(function(){
			new ParamRewrite($(this));
		});
	}
})(jQuery);
