function tabs_init(tabs_id)
{
	$('#tabs_'+tabs_id+' div.single').hide();
	$('#tabs_'+tabs_id+' div.'+tabs_id+'-content').hide();
	$('#tabs_'+tabs_id+' div.header').show();
	
	$(function(){
		if($.cookie('tabs_active_'+tabs_id) != null)
		{
			tab_activate_init($.cookie('tabs_active_'+tabs_id), tabs_id);
		}
	});
}

function tab_activate_init(tab_id, tabs_id)
{
	$('#tabs_'+tabs_id+' .'+tabs_id+'-content').hide();
	$('#tab_'+tab_id+'_content').show();
	$('#tabs_'+tabs_id+' div.header-'+tabs_id+'>ul>li.tabSelected').removeClass('tabSelected');
	$('#tab_'+tabs_id+'_'+tab_id).addClass('tabSelected');
}

function tab_activate(tab_id, tabs_id)
{
	tab_activate_init(tab_id, tabs_id);
	
	$.cookie('tabs_active_'+tabs_id, tab_id);
}

/**
 * Sluzi na zmenu URL aby bola pouzitelna pre naloadovanie stranky ajaxom do fancyboxu
 */
function fancy_box_url(template_name, obj)
{
	var str;
	
	str = obj.attr("href").replace(/menuitem=.*?($|&)/, "");
	str += "&ajax=true&class=page_template&method=call_page&page_name=" + template_name + "&show_template=true";
	obj.attr("href", str);
}

/**
 * Funkcia prida fancy box k objektom add_to naplni ho sablonou template_name a bude mat rozmery width x height
 */
function add_fancy_box(add_to, template_name, width, height, hide_close_button)
{
	add_to.each(function(i)
	{
		fancy_box_url(template_name, $(this));
	});
	
	show_button = true;
	
	if(hide_close_button === true)
	{
		show_button = false;
	}
	add_to.fancybox(
	{
		'frameWidth'             : width,
		'frameHeight'            : height, 
		'overlayOpacity'         : 0, 
		'hideOnOverlayClick'     : true, 
		'hideOnContentClick'     : false,
		'easingIn'               : 'easeOutBack',
		'easingOut'              : 'easeInBack',
		'titleShow'				 : false,
		'showCloseButton'		 : show_button
	});
}

/**
 * Funkcia ziska obsah celeho Formulara a vytvori z jeho obsahu a GETu jeden post
 *
 * @param object - objekt nejakeho prvku vo formulari najlepsie submitu
 * @param 2 druhy parameter je volitelny, ked ma hodnotu true tak sa automaticky pocita s tym, 
 *        ze objekt je submit a automaticky sa znovu objavi a skryje loading obrazok
 *
 * @return string hodnota komplet post a get obsiahnuta pri odoslani formularu v ktorom sa dany objekt nachadza
 */
function get_form_content_to_get(object)
{
	var form_id = "";
	var get_value;
	var tmp;
	var ret_string = "";
	var x;
	
	if(arguments.length == 2 && arguments[1] == true)
	{
		var id;
		object.show();
		id = object.attr("id");
		id = id.substring(5);
		$("#submit_" + id + "_loading").hide();
	}
	
	if(object.get(0).nodeName != "A")
	{
		ret_string += object.attr("name") + ': "' + object.attr("value") + '",';
	}
	
	form_id = get_id_of_specific_parent(object, "FORM");
	
	tmp = window.location.toString();
	tmp = tmp.split("?");
	tmp = tmp[1];
	tmp = tmp.split("&");
	
	for(x = 0; x < tmp.length; x++)
	{
		if(tmp[x].length == 0)
		{
			continue;
		}
		
		get_value = tmp[x].split("=");
		
		if(get_value[0] == "menuitem" || get_value[0] == "page")
		{
			continue;
		}
		
		ret_string += get_value[0] + ': "' + get_value[1] + '",';
	}
	
	$("#" + form_id + " :input").each(function()
	{
		if(this.type != "submit")
		{
			if(this.type == "checkbox" && this.checked == true || (this.type != "checkbox" && this.type != "radio"))
			{
				ret_string += '"' + this.name + '": "' + this.value + '",';
			}
			else if(this.type == "radio" && this.checked == true)
			{
				ret_string += '"' + this.name + '": "' + this.value + '",';
			}
		}
	});
	
	ret_string = ret_string.substring(0, ret_string.length - 1);
	return ret_string;
}

/**
 * Funkcia ziska ID specifikovaneho HTML tagu pre dany objekt
 */
function get_id_of_specific_parent(object, parent_tag_name)
{
	var return_val = true;
	
	while(object.parent().get(0).nodeName != parent_tag_name)
	{
		object = object.parent();
	}
	
	return object.parent().get(0).id;
}

/**
 * Ziska hodnotu z getu z <a> 
 */
function get_value_from_link(a_object, get_parameter_name)
{
	var href = a_object.attr("href");
	var tmp = "";
	var x = 0;
	var result = "";
	
	href = href.split("?");
	href = href[1];
	href = href.split("&");
	
	for(x = 0; x < href.length; x++)
	{
		if(href[x].length == 0)
		{
			continue;
		}
		
		tmp = href[x].split("=");
		
		if(tmp[0] == get_parameter_name)
		{
			result = tmp[1];
		}
	}
	
	return result;
}

/**
 * Sluzi na registraciu onclik pre mazanie alebo hocijake spracovanie ajaxom bez reloadu - vola sa iba page bez sablony
 */
function process_by_ajax(object)
{
	
}

function toggle_baskets()
{
	$('.unused_baskets_table table tr:not(.head,.highlight)').toggle();
}

function validate(id, object, page)
{
	value = object.value;
	ajax_path = "index.php";
	classname = "page_template";
	methodname = "call_page";
	
	$.post(ajax_path, { ajax: "true", class_name: classname, method: methodname, page_name: page, show_template: false, ajax_validation: "true", ajax_validation_input_id: id, ajax_validation_value: value}, function(data)
	{
		$("div#error_" + id).remove();
		
		if(data.length > 2)
		{
			$(data).insertAfter(object);
		}
	}, "text");
}

function input_help_value(id, value)
{
	var help_text = value+"     ";
	var help_color = "#CCCCCC";
	var old_color = $("#"+id).css("color");

	if($("#"+id).val() == "")
	{	
		$("#"+id).val(help_text);
		$("#"+id).css("color",help_color);
	}
	
	$("#"+id).focus(function () 
	{
		if($(this).val() == help_text)
		{	
			$(this).val("");
			$(this).css("color",old_color);
		}
	});

	$("#"+id).blur(function () 
	{
		if($(this).val() == "")
		{	
			$(this).val(help_text);
			$(this).css("color",help_color);
		}
	});

	$(function() {
		$('form').submit(function () 
		{
			if($("#"+id).val() == help_text)
			{	
				($("#"+id).val(""));
			}
		});
	});

	
}

/**
 * Pocitani bodu v productlistu
 */
var product_list_point_count = new Array();

var product_list_point_change_for_bs_count = new Array();

function point_count(fid, count, available)
{	
	var sum = $('#form_count_'+fid).val() * count;
	//console.log(product_list_point_count);
	
	if(isNaN(sum))
	{
		sum = 0;
	}
	product_list_point_count['form_count_'+fid] = sum;
	
	if($('#form_tmpSB_'+fid).attr('checked') == true)
	{
		product_list_point_change_for_bs_count[fid] = sum;
	}
	else
	{
		product_list_point_change_for_bs_count[fid] = 0;
	}
	
	actualize_point_count(available);
}

var nadlimitni_upper = 100;

var nadlimitni_lower = 80;

function actualize_point_count(available)
{	
	var total_points = 0;
	for ( i in product_list_point_count ) {
	    total_points += product_list_point_count[i];
	}
	$("#product_detail_point_sum").html(total_points);
	
	var total_points_wanted = 0;
	for ( i in product_list_point_change_for_bs_count ) {
	    total_points_wanted += product_list_point_change_for_bs_count[i];
	}
	$("#product_detail_point_wanted").html(total_points_wanted);
	
	var html_available = available - total_points_wanted;
	if (html_available < 0)
	{
		html_available = '<span class="sb_alert">'+html_available+'</span>';
	}
	$("#product_detail_point_available").html(html_available);
	
	var nadlimitni_left = nadlimitni_upper - total_points;
	var notification = $.cookie("points_notify");
	//console.log(notification);
	//console.log(nadlimitni_lower);
	if(total_points == 0)
	{
		$.cookie("points_notify", null, { path: '/', expires: date });
	}
	
	if(total_points >= nadlimitni_lower && total_points < nadlimitni_upper)
	{
		if(notification == null || notification > total_points)
		{
			var date = new Date();
			// platnost cookie
	        date.setTime(date.getTime() + (30*60*1000));
	
			$('<div></div>').html('<strong>K dosažení hranice '+nadlimitni_upper+' bodů, nad kterou platí sleva za nadlimitní objednávku Vám ještě stačí přiobjednat produkty pouze za '+nadlimitni_left+' bodů.</strong><br/><br/>'+
									'Sleva 5 - 10% (dle Vaší minulé pozice) se počítá z bodů přesahujících tuto hranici. Body se sčítají za celý měsíc.<br/><br/>'+
									'Více o nadlimitní objednávce najdete <a href="http://ekancelar.klas.cz/?menuitem=rewards_ra_overlimit" target="_blank">ZDE</a> ').dialog({
				// je to prasecina, ale bohuzel nemame translator v javascriptu...
				title : 'Upozornění',
				modal : true,
				width : 500,
				buttons : {'OK' : function(){$.cookie("points_notify", total_points, { path: '/', expires: date }); $(this).dialog('close');}}
			}).dialog('open');
		}
	}
}

/**
 * vrati aktualny GET ako nazov=hodnota&nazov1=hodnota1
 */
function get_current_get_values()
{
	var tmp = window.location.toString();
	tmp = tmp.split("?");
	
	if(tmp.length == 1 || tmp[1].length == 0)
	{
		return new Array();
	}
	
	tmp = tmp[1];
	return tmp;
}

function toggle_content(header, content, h3, show_on_init) {
	if(show_on_init == true)
	{
		h3.addClass('minus');
		content.show();
	}
	else
	{
		h3.addClass('plus');
		content.hide();
	}
	
	header.click(function(){
		content.toggle(200);
		if (h3.hasClass('minus')) {
			h3.removeClass('minus');
			h3.addClass('plus');
		} else if (h3.hasClass('plus')) {
			h3.removeClass('plus');
			h3.addClass('minus');
		}
	});
}

/**
 * var_dump pre ajaxove volania
 */
function var_dump(returned_value_from_ajax_call)
{
	var div = $("#this_is_ajax_debug_div");
	div.children().remove();
	$(returned_value_from_ajax_call).appendTo(div);
}

function copyNumbersTo(object, id)
{
	var value = $(object).val();
	var new_value = "";
	var re1 = /(\d+)/g;
	var result = value.match(re1);
	if(result != null)
	{
		for ( var i in result )
		{
			if ( parseInt(i) == i )
			{
				new_value = new_value + result[i];
			}
		} 
	}
	
	$('#'+id).val(new_value);
}

/**
 * Funkcia vola translator a z lokalizacnej konstanty vrati jej prelozenu verziu
 */
function translate(loc_const, where_to_append, class_name)
{
	$.post(ajax_path, { ajax: "true", class_name: "ajax_utilities", method: "translate", constant: loc_const}, function(data)
	{
		$(where_to_append).children().remove();
		$("<div class='" + class_name + "'>" + data + "</div>").appendTo(where_to_append);
	}, "text");
}

function externalLinks() {  
	if (!document.getElementsByTagName) return;  
	var anchors = document.getElementsByTagName("a");
	for (var i=0; i<anchors.length; i++) {  
		var anchor = anchors[i]; 
		if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external")  
			anchor.target = "_blank";  
	}  
}   

/**
 * funkce pro ajaxove odeslani informaci v pripade chyby
 * @TODO - parametr s uzivatelskym komentarem, clenskym cislem...
 */
function helpMe(text, dump, resolution, user, to_redmine, blank_url)
{
	$.post('helpmehandler.php', 
		{ 
			'helpmeplease' : 'thx',
			'html' : '<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\"><html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"cs\" lang=\"cs\">'+$('html').html()+'</html>',
			'text' : text,
			'dump' : dump,
			'window' : resolution,
			'user' : user,
			'to_redmine' : to_redmine
		},
		function(data)
		{
			if(to_redmine == true)
			{
				window.location.replace(blank_url+"?page=admin_issues&id=" + parseInt(data));
			}
		}
	);
}