function parent_location(str)
{
	str += '';
	if (str.indexOf('#')>-1) str = str.substring(0,str.indexOf('#'));
	return str;
	parent.location = str;
}
function MytinyMCEinit()
{
    tinyMCE.init({
				 /*
        mode : "textareas",
        theme : "advanced",
		theme_advanced_buttons3_add : "ibrowser",
        editor_selector : "mceEditor",
		force_br_newlines: true,
		force_p_newlines: false,
		forced_root_block: ''
		*/
			mode : "textareas",
			theme : "advanced",
			//plugins: "paste,inlinepopups,searchreplace,contextmenu",
plugins : "spellchecker,style,table,advhr,advlink,iespell,inlinepopups,searchreplace,print,contextmenu,paste",
theme_advanced_buttons1 : "bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,|,fontsizeselect",
theme_advanced_buttons2 : "pastetext,pasteword,|,search,|,bullist,numlist,|,forecolor,backcolor,help,code",
theme_advanced_buttons3 : "advhr,removeformat,|,sub,sup,|,charmap,|,link,unlink,cleanup,|,print,spellchecker",
theme_advanced_toolbar_location : "bottom",
theme_advanced_toolbar_align : "left",
theme_advanced_statusbar_location : "bottom",
theme_advanced_resizing : true,
			editor_selector : "mceEditor",
			force_br_newlines: true,
			force_p_newlines: false,
			forced_root_block: '',
			content_css: tpl_url+'css/style_bebeloo_tinymce.css',
		convert_urls : true,
		relative_urls : false,
		document_base_url : lang_url,
		remove_script_host : false

    });
}
function myattachdatepicker()
{
$(document).ready(function() {
	$('input.datepicker_fire').datepicker({dateFormat: 'yy-mm-dd', yearRange: '1930:2010' });
});
}
myattachdatepicker();
var loader_image = new Image();
$(document).ready(function(){
	loader_image.src = tpl_url+'images/loader.gif';
});
function ajax_json(url,data,loader_div)
{
	var loader_div_backup = '';
	if (typeof loader_div == "undefined")
		loader_div ='abcdefghijklmnopqrstuv';
	else
		loader_div_backup = $('#'+loader_div).html();
	$('#'+loader_div).html(loader_image);
	$.ajax({
		type: "POST",
		url: url,
		data: data,
		async: true,
		dataType: "json",
		success: function(data){
			$('#'+loader_div).html(loader_div_backup);
			process(data);
		},
		error: function(obj,err,e){
			$('#'+loader_div).html(loader_div_backup);
			alert('An error occured during request. Please try again.');
		}
	});
}
function process(data)
{
	if (data.error)
	{
		alert(data.error_msg);
		$('#edit_submit').attr('disabled','');
		$('#edit_cancel').attr('disabled','');
	}else if (typeof data.action != "undefined")
	{
		eval(data.action);
	}
}

function cms_user_action_submit(action,form_id,extra)
{
	var extra_data = '';
	if (typeof extra != "undefined") extra_data = '&'+extra;
	ajax_json(lang_url+"ajax/"+action+"/save",$('#'+form_id).serialize()+extra_data);	
}
function edit_cancel(cancel_all)
{
	if (typeof cancel_all == "undefined") cancel_all = true;
	if (cancel_all)
	{
		$('.editlink').css('visibility','');
		$('.menueditlink').css('visibility','');
		destroy_photo_sort();
		destroy_video_sort();
		enable_main_sort();
	}
	$('#edit_submit').attr('disabled','');
	$('#edit_cancel').attr('disabled','');
}
function edit_clear()
{
	$('#main_edit').html('');
	$("*[id^='page_edit_']").html('');
	$("*[id^='paragraph_edit_']").html('');
}
function cms_action(action,id,page_id,prod_id,loader_div)
{
	if (action.indexOf('delete')>-1)
		if (!confirm('Are you sure?')) return false;
	if (action.indexOf('edit')>-1)
	{
		$('.editlink').css('visibility','hidden');
		$('.menueditlink').css('visibility','hidden');
		disable_main_sort();
	}
	var cmd = '';
	if (typeof page_id != "undefined") cmd += '&page_id='+page_id;
	if (typeof prod_id != "undefined") cmd += '&prod_id='+prod_id;
	ajax_json(base_url+"admin/ajax/"+action+"/",'id='+id+cmd,loader_div);
}
function cms_action_submit(action,form_id,extra)
{
	$('textarea.mceEditor').each(function(e)
	{
		var obj =  tinyMCE.get($(this).attr('id'));
		$(this).val(obj.getContent());
	});
	var extra_data = '';
	if (typeof extra != "undefined") extra_data = '&'+extra;
	if (action=='product_edit')
	{
		if (!multiple_has_any('pages'))
		{
			alert('Please select at least a page.');
			return false;
		}
	}
	$('#edit_submit').attr('disabled','disabled');
	$('#edit_cancel').attr('disabled','disabled');
	ajax_json(base_url+"admin/ajax/"+action+"/save",$('#'+form_id).serialize()+extra_data);
	
}
function cms_action_cancel(what,forcemain)
{
	var id = $('#'+what+'_id').val();
	if (typeof forcemain == "undefined") forcemain = false;
	if (forcemain) id =0;
	if (id==0)
	{
		$('#edit_main').hide(800);
		$('#edit_main_hide').show(800);
	}else
	{
		$('#'+what+'_edit_'+id).hide(800);
		$('#'+what+'_'+id).show(800);
	}
	edit_cancel();
}
function cms_sort(what,data,extra)
{
	if (typeof extra != "undefined") 
	{
		data += (data.length>0?'&':'')+extra;
	}
	ajax_json(base_url+"admin/ajax/"+what,data);
}

function show_tab(id)
{
	$("*[id^='tabid_']").hide();
	$("#tabid_"+id).show();
	$("*[id^='tabtitle_']").removeClass('tabselected');
	$("*[id^='tabtitle_']").addClass('tabunselected');
	$("#tabtitle_"+id).removeClass('tabunselected');
	$("#tabtitle_"+id).addClass('tabselected');
	$("#tabtitle_"+id).children().blur();
}
function over_tab(id)
{
	$("#tabtitle_"+id).removeClass('tabunselected');
	$("#tabtitle_"+id).addClass('tabselected');
	$("#tabtitle_"+id).children().blur();
}
function notover_tab(id)
{
	$("#tabtitle_"+id).removeClass('tabselected');
	$("#tabtitle_"+id).addClass('tabunselected');
	$("#tabtitle_"+id).children().blur();
}
function show_more_photos(id)
{
	$("#linkmore_"+id).hide(800);
	$("#morephotos_"+id).show(800);
}
function show_div(divname)
{
	$("#div"+divname).toggle(800);
}
/* PARAGRAPH REORDER FUNCTIONS START */
function random_nr(min,max)
{
	if (min<max) return min+Math.floor(Math.random()*(max-min+1));
	if(min>max) return random_nr(max,min);
	if(min=max) return min;
}
function dec2hex_char(nr)
{
	if (nr<10) return nr.toString();
	else if (nr==10) return 'a';
	else if (nr==11) return 'b';
	else if (nr==12) return 'c';
	else if (nr==13) return 'd';
	else if (nr==14) return 'e';
	else if (nr==15) return 'f';
	else return 'f';
}
var paragraphs = [];
var paragraphs2 = [];
var reorder_cols = 20;
var reorder_rows = 10;
var reorder_resize = [];
var reorder_cell_width = 0;
var reorder_cell_height = 0;
function reorder_clear_cell(x,y,check)
{
	if (typeof check == "undefined") check = true;
	$('#col_'+x+'_'+y).html('');
	$('#col_'+x+'_'+y).css('background-color','#e9b96e');
	if (check) reorder_check_graf();
}
function init_paragraph_reorder()
{
	reorder_cell_width = $('#col_1_1').width();
	reorder_cell_height = $('#col_1_1').height();
	var tmp_paragraphs = [];
	var tmp_paragraphs2 = [];
	var tmp_cnt = 0;
	$("*[id^='reorder_paragraph_']").each(function(){
		tmp_paragraphs[$(this).attr('id')] = [];
		tmp_paragraphs[$(this).attr('id')][0] = $(this);
		tmp_paragraphs[$(this).attr('id')][1] = $(this).html();
		tmp_paragraphs[$(this).attr('id')][2] = $(this).attr('id').substring(18);
		tmp_paragraphs[$(this).attr('id')][3] = ++tmp_cnt;
		tmp_paragraphs2[tmp_paragraphs2.length] = tmp_paragraphs[$(this).attr('id')];
		$(this).html(tmp_cnt+' - '+$(this).html());
		$(this).css('background-color','#'
			+dec2hex_char(random_nr(5,15))+dec2hex_char(random_nr(5,15))
			+dec2hex_char(random_nr(5,15))+dec2hex_char(random_nr(5,15))
			+dec2hex_char(random_nr(5,15))+dec2hex_char(random_nr(5,15))
		);
	});
	paragraphs2 = tmp_paragraphs;
	paragraphs = tmp_paragraphs2;
	var i=0; var j =0; var k =0;
	for (i=1;i<=reorder_rows;i++)
		for (j=1;j<=reorder_cols;j++)
			for (k=0;k<paragraphs.length;k++)
				if ($('#col_'+i+'_'+j).html()==paragraphs[k][2].toString())
				{
					$('#col_'+i+'_'+j).html(paragraphs[k][3].toString());
					$('#col_'+i+'_'+j).css('background-color',paragraphs[k][0].css('background-color'));
				}
	
	
    $(".reorder_drag").draggable({helper: 'clone'});
	$(".reorder_drop").droppable({
		accept: ".reorder_drag",
		tolerance: 'pointer',
		delay: 0,
		cursor: 'move',
		//activeClass: 'reorder_dragging',
		hoverClass: 'reorder_over',
		drop: function(ev, ui) {
			show_loading('#reorder_graph');
			setTimeout("$('#"+$(this).attr('id')+"').html('"+paragraphs2[$(ui.draggable).attr('id')][3].toString()+"');"+
			"reorder_update_graf($('#"+$(this).attr('id')+"'),paragraphs2['"+$(ui.draggable).attr('id')+"']);"+
			"hide_loading();"
			,10);
			//$(this).html(paragraphs2[$(ui.draggable).attr('id')][3].toString());
			//reorder_update_graf($(this),paragraphs2[$(ui.draggable).attr('id')]);
		}
	});
	$('.reorder_drop').click(function(e){
		var tmp = $(this).attr('id');
		tmp = tmp.substring(tmp.indexOf('_')+1);
		row_id = tmp.substring(0,tmp.indexOf('_'));
		col_id = tmp.substring(tmp.indexOf('_')+1);
		show_loading('#reorder_graph');
		setTimeout("reorder_clear_cell('"+row_id+"','"+col_id+"',true);"+
		"reorder_check_graf();"+
		"reorder_show_resize();"+
		"hide_loading();",10);
	});
	//apply graf resize drop!
	var offset1 = $('#col_1_1').offset();
	var offset2 = $('#col_'+reorder_rows+'_'+reorder_cols).offset();
	var last_top = 0; var last_left = 0;
	for (i=1;i<=reorder_rows;i++)
	{
		var offset = $('#col_'+i+'_1').offset();
		var $el = $('#resize_drop_NS').clone();
		$($el).attr('id','resize_drop_NS_'+(i-1));
		$($el).css('width',offset2.left - offset1.left + reorder_cell_width + 2);
		$($el).css('top',offset.top);
		$($el).css('left',offset1.left);
		$el.appendTo($('#resize_attach'));
		if (i==reorder_rows)
		{//add an extra drop target at the end.
			var $el = $('#resize_drop_NS').clone();
			$($el).attr('id','resize_drop_NS_'+i);
			$($el).css('width',offset2.left - offset1.left + reorder_cell_width + 2);
			$($el).css('top',offset.top+(offset.top - last_top));
			$($el).css('left',offset1.left);
			$el.appendTo($('#resize_attach'));
		}
		last_top = offset.top;
	}
	for (j=1;j<=reorder_cols;j++)
	{
		var offset = $('#col_1_'+j).offset();
		var $el = $('#resize_drop_EW').clone();
		$($el).attr('id','resize_drop_EW_'+(j-1));
		$($el).css('height',offset2.top - offset1.top + reorder_cell_height + 2);
		$($el).css('top',offset1.top);
		$($el).css('left',offset.left);
		$el.appendTo($('#resize_attach'));
		if (j==reorder_cols)
		{//add an extra drop target at the end.
			var $el = $('#resize_drop_EW').clone();
			$($el).attr('id','resize_drop_EW_'+j);
			$($el).css('height',offset2.top - offset1.top + reorder_cell_height + 2);
			$($el).css('top',offset1.top);
			$($el).css('left',offset.left+(offset.left - last_left));
			$el.appendTo($('#resize_attach'));
		}
		last_left = offset.left;

	}
	$('#resize_drop_NS').remove();
	$('#resize_drop_EW').remove();
	$(".resize_drop_NS").droppable({
		accept: ".resizeN,.resizeS",
		tolerance: 'pointer',
		delay: 0,
		cursor: 'move',
		//activeClass: 'reorder_dragging',
		hoverClass: 'resize_over',
		drop: function(ev, ui) {
			show_loading('#reorder_graph');
			setTimeout("reorder_do_resize('"+$(ui.draggable).attr('id')+"','"+$(ui.element).attr('id')+"');hide_loading();",10);
		}
	});
		$(".resize_drop_EW").droppable({
		accept: ".resizeE,.resizeW",
		tolerance: 'pointer',
		delay: 0,
		cursor: 'move',
		//activeClass: 'reorder_dragging',
		hoverClass: 'resize_over',
		drop: function(ev, ui) {
			show_loading('#reorder_graph');
			setTimeout("reorder_do_resize('"+$(ui.draggable).attr('id')+"','"+$(ui.element).attr('id')+"');hide_loading();",10);
		}
	});
	reorder_check_graf();//for limits!
	reorder_show_resize();//apply resizeable margins!
	
}
function reorder_do_resize(what,where)
{
	var dir = 1; var i=0; var j=0;
	var tmp = what.substring(6).substring(0,1);
	if (tmp == 'N' || tmp == 'S') dir = 1; else dir = 2;
	var par_id = what.substring(8);
	var drop_id = where.substring(15);
	if(dir == 1)
	{
		if (paragraphs[par_id][6]>drop_id)
			reorder_do_resize_cell(parseInt(drop_id)+1,paragraphs[par_id][7],par_id);
		else if (paragraphs[par_id][8]<drop_id)
			reorder_do_resize_cell(drop_id,paragraphs[par_id][7],par_id);
		else if (paragraphs[par_id][6]<=drop_id && paragraphs[par_id][8] > drop_id)
		{
			if (tmp=='N')
				for (var i=paragraphs[par_id][7];i<=paragraphs[par_id][9];i++)
					for (var j=paragraphs[par_id][6];j<=drop_id;j++)
						reorder_clear_cell(j,i,false);//clear cell are indicii corecti?
			else
				for (var i=paragraphs[par_id][7];i<=paragraphs[par_id][9];i++)
					for (var j=parseInt(drop_id)+1;j<=paragraphs[par_id][8];j++)
						reorder_clear_cell(j,i,false);//clear cell are indicii corecti?
		}
	}else
	{
		if (paragraphs[par_id][7]>drop_id)
			reorder_do_resize_cell(paragraphs[par_id][6],parseInt(drop_id)+1,par_id);
		else if (paragraphs[par_id][9]<drop_id)
			reorder_do_resize_cell(paragraphs[par_id][6],drop_id,par_id);
		else if (paragraphs[par_id][7]<=drop_id && paragraphs[par_id][9] > drop_id)
		{
			if (tmp=='W')
				for (var i=paragraphs[par_id][6];i<=paragraphs[par_id][8];i++)
					for (var j=paragraphs[par_id][7];j<=drop_id;j++)
						reorder_clear_cell(i,j,false);//clear cell are indicii corecti?
			else
				for (var i=paragraphs[par_id][6];i<=paragraphs[par_id][8];i++)
					for (var j=parseInt(drop_id)+1;j<=paragraphs[par_id][9];j++)
						reorder_clear_cell(i,j,false);//clear cell are indicii corecti?
		}
	
	}
	reorder_check_graf();
	reorder_show_resize();
}
function reorder_do_resize_cell(x,y,par_id)
{
	$('#col_'+x+'_'+y).html(paragraphs[par_id][3].toString());
	reorder_update_graf($('#col_'+x+'_'+y),paragraphs[par_id]);	
}
function reorder_clear_par(id)
{
	show_loading('#reorder_graph');
	setTimeout("reorder_clear_par_("+id+");hide_loading();",10);
}
function reorder_clear_par_(id)
{
	var par_id = paragraphs2['reorder_paragraph_'+id][3];
	var i=0; var j=0;
	for (i=1;i<=reorder_rows;i++)
		for (j=1;j<=reorder_cols;j++)
			if ($('#col_'+i+'_'+j).html()==par_id)
			{
				reorder_clear_cell(i,j,false);
			}
	reorder_check_graf();
	reorder_show_resize();
}
function reorder_show_resize()
{
	var i = 0; var k = 0;
	for (i = 0; i < reorder_resize.length; i++)
	{
		//reorder_resize[i].remove();
		//alert('1');
	}
	while ($('.resizeS').length>0) $('.resizeS').remove();
	while ($('.resizeN').length>0) $('.resizeN').remove();
	while ($('.resizeE').length>0) $('.resizeE').remove();
	while ($('.resizeW').length>0) $('.resizeW').remove();
	for (k = 0; k < paragraphs.length; k++)
	if ($('#col_'+paragraphs[k][6]+'_'+paragraphs[k][7]).html()==paragraphs[k][3])
	{
		//TOP RESIZE
		var $div = document.createElement("div");
		$($div).attr('class','resizeN');
		$($div).attr('id','resizeN_'+k);
		var offset1 = $('#col_'+paragraphs[k][6]+'_'+paragraphs[k][7]).offset();
		var offset2 = $('#col_'+paragraphs[k][6]+'_'+paragraphs[k][9]).offset();
		$($div).css('top',offset1.top);
		$($div).css('left',offset1.left);
		$($div).css('width',offset2.left - offset1.left + reorder_cell_width + 2);
		$div = $('#resize_attach').append($div);
		reorder_resize[reorder_resize.length] = $div;
		//BOTTOM RESIZE
		var $div = document.createElement("div");
		$($div).attr('class','resizeS');
		$($div).attr('id','resizeS_'+k);
		var offset1 = $('#col_'+paragraphs[k][8]+'_'+paragraphs[k][7]).offset();
		var offset2 = $('#col_'+paragraphs[k][8]+'_'+paragraphs[k][9]).offset();
		$($div).css('top',offset1.top + reorder_cell_height);
		$($div).css('left',offset1.left);
		$($div).css('width',offset2.left - offset1.left + reorder_cell_width + 2);
		$div = $('#resize_attach').append($div);
		reorder_resize[reorder_resize.length] = $div;
		//RIGHT RESIZE
		var $div = document.createElement("div");
		$($div).attr('class','resizeE');
		$($div).attr('id','resizeE_'+k);
		var offset1 = $('#col_'+paragraphs[k][6]+'_'+paragraphs[k][9]).offset();
		var offset2 = $('#col_'+paragraphs[k][8]+'_'+paragraphs[k][9]).offset();
		$($div).css('top',offset1.top);
		$($div).css('left',offset1.left +reorder_cell_width );
		$($div).css('height',offset2.top - offset1.top + reorder_cell_height + 2);
		$div = $('#resize_attach').append($div);
		reorder_resize[reorder_resize.length] = $div;
		//LEFT RESIZE
		var $div = document.createElement("div");
		$($div).attr('class','resizeW');
		$($div).attr('id','resizeW_'+k);
		var offset1 = $('#col_'+paragraphs[k][6]+'_'+paragraphs[k][7]).offset();
		var offset2 = $('#col_'+paragraphs[k][8]+'_'+paragraphs[k][7]).offset();
		$($div).css('top',offset1.top);
		$($div).css('left',offset1.left  );
		$($div).css('height',offset2.top - offset1.top + reorder_cell_height + 2);
		$div = $('#resize_attach').append($div);
		reorder_resize[reorder_resize.length] = $div;

	}
	$(".resizeN").draggable("destroy");
	$(".resizeS").draggable("destroy");
	$(".resizeE").draggable("destroy");
	$(".resizeW").draggable("destroy");
	$(".resizeN").draggable({helper: 'clone',axis:'y'});
	$(".resizeS").draggable({helper: 'clone',axis:'y'});
	$(".resizeE").draggable({helper: 'clone',axis:'x'});
	$(".resizeW").draggable({helper: 'clone',axis:'x'});
}
function reorder_update_graf(cell,par)
{
	var i = 0; var j = 0; var k = 0;
	min_x = reorder_rows; min_y = reorder_cols;
	max_x = 1; max_y = 1;
	par_id = par[3].toString();
	for (i=1;i<=reorder_rows;i++)
		for (j=1;j<=reorder_cols;j++)
			if ($('#col_'+i+'_'+j).html()==par_id)
			{
//				alert('matched ('+par_id+') on ['+i+','+j+']');
				min_x = Math.min(min_x,i);
				min_y = Math.min(min_y,j);
				max_x = Math.max(max_x,i);
				max_y = Math.max(max_y,j);
			}
//	alert('['+min_x+','+min_y+'],['+max_x+','+max_y+']');
	for (i=min_x;i<=max_x;i++)
		for (j=min_y;j<=max_y;j++)
		{
			$('#col_'+i+'_'+j).html(par[3].toString());
			$('#col_'+i+'_'+j).css('background-color',par[0].css('background-color'));
		}
	reorder_check_graf();
	reorder_show_resize();
}
function reorder_check_graf()
{
	//init!
	var i=0; var j =0; var k=0;
	for (i=0;i<paragraphs.length;i++)
	{
		paragraphs[i][6] = reorder_rows; paragraphs[i][7] = reorder_cols;
		paragraphs[i][8] = 1; paragraphs[i][9] = 1;
	}
	//calc limits
	for (i=1;i<=reorder_rows;i++)
		for (j=1;j<=reorder_cols;j++)
			for (k=0;k<paragraphs.length;k++)
				if ($('#col_'+i+'_'+j).html()==paragraphs[k][3].toString())
				{
					paragraphs[k][6] = Math.min(paragraphs[k][6],i);
					paragraphs[k][7] = Math.min(paragraphs[k][7],j);
					paragraphs[k][8] = Math.max(paragraphs[k][8],i);
					paragraphs[k][9] = Math.max(paragraphs[k][9],j);
				}
	//test
	for (i=1;i<=reorder_rows;i++)
		for (j=1;j<=reorder_cols;j++)
			for (k=0;k<paragraphs.length;k++)
			{
				if (paragraphs[k][6]<=i && paragraphs[k][8]>=i && paragraphs[k][7]<=j && paragraphs[k][9]>=j)
					//alert('hmmmm.... ['+paragraphs[k][6]+','+paragraphs[k][7]+'],['+paragraphs[k][8]+','+paragraphs[k][9]+'] ... '+paragraphs[k][3].toString()+' - '+$('#col_'+i+'_'+j).html()+'i,j='+i+','+j);
				if (paragraphs[k][6]<=i && paragraphs[k][8]>=i && paragraphs[k][7]<=j && paragraphs[k][9]>=j && $('#col_'+i+'_'+j).html() != paragraphs[k][3].toString())
				{//bad combination!!!
		//			alert('bad combination @['+i+','+j+']');
					nr_right = remove_cell(paragraphs[k],i,j,1,1,false);
					nr_left = remove_cell(paragraphs[k],i,j,-1,1,false);
					nr_top = remove_cell(paragraphs[k],i,j,1,0,false);
					nr_bottom = remove_cell(paragraphs[k],i,j,-1,0,false);
//console.log('bad combination @['+i+','+j+'] :: r/l/t/b: '+nr_right+'/'+nr_left+'/'+nr_top+'/'+nr_bottom);
					if (nr_right>0 && nr_right<=nr_left && nr_right<=nr_bottom && nr_right<=nr_top)
						remove_cell(paragraphs[k],i,j,1,1,true);
					else if (nr_bottom>0 && nr_bottom<=nr_top && nr_bottom<=nr_left && nr_bottom<=nr_right)
						remove_cell(paragraphs[k],i,j,-1,0,true);
					else if (nr_left>0 && nr_left<=nr_right && nr_left<=nr_top && nr_left<=nr_bottom)
						remove_cell(paragraphs[k],i,j,-1,1,true);
					else if (nr_top>0 && nr_top<=nr_bottom && nr_top<=nr_left && nr_top<=nr_right)
						remove_cell(paragraphs[k],i,j,1,0,true);
					else {alert('Error: Coudn\'t find a way to fix paragraphs combination!'); return; }
					//RECURSE TO FIX OTHERS....
					reorder_check_graf();
					//fixed.. now exit this cicle! now!
					return;
				}
			}
}
function remove_cell(par,x,y,dir1,dir2,exec)//dir1: 1-dreapta/jos -1: stanga/sus,dir2: 1-x,0-y
{
//console.log('remove cell: '+par+'-,-'+x+'-,-'+y+'-,-'+dir1+'-,-'+dir2+'-,-'+exec);
	var start_x = 0;
	var start_y = 0;
	var end_x = 0;
	var end_y = 0;
	var cnt = 0;
	var i=0; var j =0; var k=0;
	if (dir1 == 1)
	{
		start_x = par[6];
		end_x = par[8];
		if(dir2 == 1) {start_y = y; end_y = par[9]; }
		else {start_y = par[7]; end_y = y; }
		//{start_x = x; end_y = y; start_y = par[8]; end_y = par[9];}
		//else {end_x = x; start_y = y; start_x = par[6]; end_x = [7];}
	}else
	{
		start_y = par[7];
		end_y = par[9];
		if(dir2 == 1) {start_x = x; end_x = par[8]; }
		else {start_x = par[6]; end_x = x; }
	}
//console.log('removing interval ['+start_x+','+end_x+'],['+start_y+','+end_y+']');
	for (i=start_x;i<=end_x;i++)
		for (j=start_y;j<=end_y;j++)
			if($('#col_'+i+'_'+j).html()==par[3].toString())
		{
//console.log('clear: '+i+','+j);
			if(exec)reorder_clear_cell(i,j,false);
			else cnt++;
		}
//console.log('return: '+cnt);
	return cnt;
}
function reorder_submit(action,form_id)
{
	//var data = $('#'+form_id).serialize();
	var data = "";
	var k =0;
	for (k=0;k<paragraphs.length;k++)
	{
		data+='&par_'+paragraphs[k][2]+'='+paragraphs[k][3];
	}
	$("div[id^='col_']").each(function(){
		var tmp = parseInt($(this).html());
		if (tmp!=NaN && tmp >0)
			data += '&' + $(this).attr('id') + '=' + tmp;
	});
	cms_action_submit(action,form_id,data.substring(1));
	//ajax_json(base_url+"admin/ajax/"+action+"/save",data);
}
function show_loading(obj)
{
	//console.log('hide in '+obj);
	var offset = $(obj).offset();
	var top = parseInt(offset.top);
	var left = parseInt(offset.left);
	var width = parseInt($(obj).width());
	var height = parseInt($(obj).height());
	$('#page_loading').css('top',top);
	$('#page_loading').css('left',left);
	$('#page_loading').css('width',width);
	$('#page_loading').css('height',height);
	$('#page_loading').css('opacity',0.5);
	$('#page_loading').show();
	//33 is width of loading img
	left = Math.max(left + Math.floor(width/2) - 33,left);
	top+=150;
	//console.log('left: '+left+', width: '+width+ ' top: '+top);
	$('#page_loading_img').css('top',top);
	$('#page_loading_img').css('left',left);
	$('#page_loading_img').show();
//	$('#page_loading_img').css('width',$('#mainrightcontent').css('width'));
//	$('#page_loading_img').css('height',$('#mainrightcontent').css('height'));
	

}
function hide_loading()
{
	$('#page_loading').hide();
	$('#page_loading_img').hide();
}
/* PARAGRAPH REORDER FUNCTIONS END */
function init_main_sort()
{
	$("*[id^='tabid_']").each(function(e){
		var what = 'sort/paragraph';
		var extra = '';
		if ($(this).attr('id')!='tabid_0') {what = 'sort/product'; extra = 'page_id='+$('#hidden_page_id').val();}
		if ($(this).children().length != 0)
		$(this).sortable(
		{ 
			axis:'y',
			//containment: $("#edit_main_hide"),
			handle:'.move_handle',
			update: function(e,ui)  { cms_sort(what,$(this).sortable('serialize'),extra); }
		 });
	});
	$(".menu_to_sort").sortable(
	{ 
		axis:'y',
		handle:'.move_handle',
		containment: $(".menu_sort_containment"),
		update: function(e,ui)  { cms_sort('sort/page',$(this).sortable('serialize')); }
	 });	
	$(".menu_to_sort2").sortable(
	{ 
		axis:'y',
		handle:'.move_handle2',
		containment: $(".menu_sort_containment2"),
		update: function(e,ui)  { cms_sort('sort/page',$(this).sortable('serialize')); }
	 });	
}
function disable_main_sort()
{
	$("*[id^='tabid_']").each(function(e){
		if ($(this).children().length != 0)
			$(this).sortable('destroy');
	});
	$(".menu_to_sort").sortable('destroy');	
	$(".menu_to_sort2").sortable('destroy');	
}
function enable_main_sort()
{
	init_main_sort();
}
var photo_sort_enabled = false;
function init_photo_sort(what)
{
	if ($('#photos_div').html().indexOf('photo_sort_') == -1 ) return false;
	if (photo_sort_enabled)
	{
		$('#photos_div').sortable('refresh');
	}
	else
	{
		$('#photos_div').sortable(
		{ 
			axis:'y',
			handle:'.photo_move_handle',
			containment: $('#photos_div_containment'),
			update: function(e,ui)  { cms_sort('sort/'+what+'_photo',$(this).sortable('serialize')); }
		 });
	}
	photo_sort_enabled = true;
}
function destroy_photo_sort()
{
	if (photo_sort_enabled==true)
	{
		$('#photos_div').sortable('destroy');
		photo_sort_enabled = false;
	}
}
var video_sort_enabled = false;
function init_video_sort(what)
{
	if (what!='paragraph') return;
	if ($('#videos_div').html().indexOf('video_sort_') == -1 ) return false;
	if (video_sort_enabled)
	{
		$('#videos_div').sortable('refresh');
	}
	else
	{
		$('#videos_div').sortable(
		{ 
			axis:'y',
			handle:'.video_move_handle',
			containment: $('#videos_div_containment'),
			update: function(e,ui)  { cms_sort('sort/'+what+'_video',$(this).sortable('serialize')); }
		 });
	}
	video_sort_enabled = true;
}
function destroy_video_sort()
{
	if (video_sort_enabled==true)
	{
		$('#videos_div').sortable('destroy');
		video_sort_enabled = false;
	}
}
function init_header_photos(change)
{
	if (change && $("[id^='page_header_photo_']").length>1)
	{
		var current_id = '';
		var first_id = '';
		var new_id = '';
		var current2_id = '';
		$("[id^='page_header_photo_']").each(function(e){
			if ($(this).css('display')=='none') 
			{
				if (current_id!='') {new_id = $(this).attr('id'); current_id='';}
				if (first_id=='') {first_id=$(this).attr('id');}
			}
			else
			{
				current_id = $(this).attr('id');
				current2_id = current_id;
			}
		});
		if (new_id=='' && first_id!='') new_id = first_id;
		if (new_id!='' && new_id!=current2_id)
		{
			$('#'+current2_id).fadeOut(1000);
			$('#'+new_id).fadeIn(1000);
		}
	}
	setTimeout("init_header_photos(true);",5000);
}
function init_edit(what,id)
{
	if ($("[id^='video_sort_']").length>0)
	{
		$('#video_settings1').show();
		$('#video_settings2').show();
		if ($("[id^='video_sort_']").length>1)
			$('#video_settings3').show();
		else
			$('#video_settings3').hide();
	}else
	{
		$('#video_settings1').hide();
		$('#video_settings2').hide();
		$('#video_settings3').hide();
	}
}
function product_add_pdf()
{
	if ($('#product_id').val()==0)
	{
		if (confirm('You must save the product before you can add pdf. Do you want to save it now?'))
		{
			cms_action_submit('product_edit','EditProduct','add_pdf=true');
			return true;
		}else return false;
	}
	tb_show('',base_url+'admin/product_add_pdf/'+$('#product_id').val()+'?TB_iframe=true&KeepThis=true&height=100&width=300&modal=true','');	
}

function add_photos(what)
{
	if ($('#'+what+'_id').val()==0)
	{
		if (confirm('You must save the '+what+' before you can add photos. Do you want to save it now?'))
		{
			if (what=='paragraph')
				cms_action_submit('paragraph_edit','EditParagraph','add_photos=true');
			else if (what=='product')
				cms_action_submit('product_edit','EditProduct','add_photos=true');
			else if (what=='page')
				cms_action_submit('page_edit','EditPage','add_photos=true');
			return true;
		}else return false;
	}
	tb_show('',base_url+'admin/'+what+'_add_photos/'+$('#'+what+'_id').val()+'?TB_iframe=true&KeepThis=true&height=220&width=460&modal=true','');	
}
var photo_fields = 3;
function add_more_photos()
{
	if (photo_fields==20)
	{
		alert('You can only add 20 photos at one time.');
		return;
	}
	photo_fields ++;
	$('#photo_row_'+photo_fields).show();
	if (photo_fields==20)
	{
		$('#add_more_photos_div').hide();	
	}
}
function add_videos(what)
{
	if ($('#'+what+'_id').val()==0)
	{
		if (confirm('You must save the '+what+' before you can add videos. Do you want to save it now?'))
		{
			if (what=='paragraph')
				cms_action_submit('paragraph_edit','EditParagraph','add_videos=true');
			else if (what=='product')
				cms_action_submit('product_edit','EditProduct','add_videos=true');
			else if (what=='page')
				cms_action_submit('page_edit','EditPage','add_videos=true');
			return true;
		}else return false;
	}
	tb_show('',base_url+'admin/'+what+'_add_videos/'+$('#'+what+'_id').val()+'?TB_iframe=true&KeepThis=true&height=220&width=860&modal=true','');	
}
var video_fields = 3;
function add_more_videos()
{
	if (video_fields==20)
	{
		alert('You can only add 20 videos at one time.');
		return;
	}
	video_fields ++;
	$('#video_row_'+video_fields).show();
	if (video_fields==20)
	{
		$('#add_more_videos_div').hide();	
	}
}
function multiple_has_any(id)
{
	var obj = document.getElementById(id);
	var i = 0;
	for (i=0;i<obj.options.length;i++)
		if (obj.options[i].selected) return true;
	return false;
}
function only_one_selected(id1,id2)
{
	var obj1 = document.getElementById(id1);
	var obj2 = document.getElementById(id2);
	var i = 0;
	var ob1_sel = false;
	for (i=0;i<obj1.options.length;i++)
		if (obj1.options[i].selected && obj1.options[i].value>0) ob1_sel = true;
	if (ob1_sel==true)
	{
		obj2.options[0].selected = true;
	}
}
// ADMIN FUNCTIONS

function delete_promote(promote_id)
{
	if (confirm("Are you sure do you want to delete this row?"))
	{
		$('#trpr'+promote_id)
			.hide()
			.load(base_url + 'ajax/delete-promote/promote/' + promote_id)

	}
}
function show_edit_promote(promote_id)
{
		$('#treditpr'+promote_id)
			.show();
		$('#tdeditpr'+promote_id)
			.load(base_url + 'ajax/show-edit-promote/promote/' + promote_id);
}
function hide_edit_promote(promote_id)
{
		$('#treditpr'+promote_id)
			.hide();
		$('#tdeditpr'+promote_id)
			.html("&nbsp;");
}
function edit_promote(promote_id) {
	cms_user_action_submit('edit-promote','edit-promote','promote_id='+promote_id);
}
function change_promote_status(promote_id) {
		$('#statuspr'+promote_id)
			.load(base_url + 'ajax/change-promote-status/promote/' + promote_id);
}

function delete_customers(customers_id)
{
	if (confirm("Are you sure do you want to delete this row?"))
	{
		$('#trpr'+customers_id)
			.hide()
			.load(base_url + 'ajax/delete-customers/customers/' + customers_id)

	}
}
function show_edit_customers(customers_id)
{
		$('#treditpr'+customers_id)
			.show();
		$('#tdeditpr'+customers_id)
			.load(base_url + 'ajax/show-edit-customers/customers/' + customers_id);
}
function hide_edit_customers(customers_id)
{
		$('#treditpr'+customers_id)
			.hide();
		$('#tdeditpr'+customers_id)
			.html("&nbsp;");
}
function edit_customers(customers_id) {
	cms_user_action_submit('edit-customers','edit-customers','customers_id='+customers_id);
}
function change_customers_status(customers_id) {
		$('#statuspr'+customers_id)
			.load(base_url + 'ajax/change-customers-status/customers/' + customers_id);
}


function delete_opponents(opponents_id)
{
	if (confirm("Are you sure do you want to delete this row?"))
	{
		$('#trpr'+opponents_id)
			.hide()
			.load(base_url + 'ajax/delete-opponents/opponents/' + opponents_id)

	}
}
function show_edit_opponents(opponents_id)
{
		$('#treditpr'+opponents_id)
			.show();
		$('#tdeditpr'+opponents_id)
			.load(base_url + 'ajax/show-edit-opponents/opponents/' + opponents_id);
}
function hide_edit_opponents(opponents_id)
{
		$('#treditpr'+opponents_id)
			.hide();
		$('#tdeditpr'+opponents_id)
			.html("&nbsp;");
}
function edit_opponents(opponents_id) {
	cms_user_action_submit('edit-opponents','edit-opponents','opponents_id='+opponents_id);
}
function change_opponents_status(opponents_id) {
		$('#statuspr'+opponents_id)
			.load(base_url + 'ajax/change-opponents-status/opponents/' + opponents_id);
}

function show_subcats(cat_id)
{
	$('#catslist'+cat_id).html('<img src="' + tpl_url + '/images/loader.gif" alt="" />');
	setTimeout("$('#catslist"+cat_id+"').load('"+base_url + "ajax/show-subcategories/category/"+ cat_id+"')",2000);
}
function show_edit_cat(cat_id)
{
		$('#trcatedit'+cat_id+'').show();
		for( var lang in site_languages ) {
			$('#catname'+cat_id+'_'+site_languages[lang]).val(''+$('#scatname'+cat_id+'_'+site_languages[lang]+'').html());
		}
		$('#catid'+cat_id).val(''+cat_id);
}
function edit_cat(cat_id) {
		for( var lang in site_languages ) {
			if ($('#catname'+cat_id+'_'+site_languages[lang]).val().length < 3 ) {
				alert('Type at least 3 characters!');
				return ;
			} 
		}
	
	cms_user_action_submit('edit-cat','edit_cat'+cat_id,'cat_id='+cat_id);
}

function add_cat(cat_id) {
		cms_user_action_submit('add-cat','add_cat'+cat_id,'cat_id='+cat_id);
}
function show_edit_subcat(cat_id, subcat_id)
{
		$('#trsucatedit'+cat_id+'').show();
		for( var lang in site_languages ) {
			$('#subcatname'+cat_id+'_'+site_languages[lang]).val(''+$('#tdsubcat'+subcat_id+'_'+site_languages[lang]+'').html());
		}
		$('#subcatid'+cat_id).val(''+subcat_id);
}
function edit_subcat(cat_id) {
	for( var lang in site_languages ) {
		if ($('#subcatname'+cat_id+'_'+site_languages[lang]).val().length < 3 ) {
			alert('Type at least 3 characters!');
			return;
		} 
	}
	cms_user_action_submit('edit-subcat','edit_subcat'+cat_id,'cat_id='+cat_id);
}

function add_subcat(cat_id) {
	for( var lang in site_languages ) {
		if ($('#newsubcatname'+cat_id+'_'+site_languages[lang]).val().length < 3 ) {
			alert('Type at least 3 characters!');
			return;
		} 
	}
	cms_user_action_submit('add-subcat','add_subcat'+cat_id,'cat_id='+cat_id);
}
function delete_subcategory(cat_id)
{
	if (confirm("Are you sure do you want to delete this category?"))
	{
		$('#trsubcat'+cat_id)
			.hide()
			.load(base_url + 'ajax/delete-subcategory/cat/' + cat_id)
	}
}
function delete_category(cat_id)
{
	if (confirm("Are you sure do you want to delete this category?"))
	{
		$('#maincat'+cat_id)
			.hide()
			.load(base_url + 'ajax/delete-category/cat/' + cat_id)
	}
}
function show_subopts(opt_id)
{
	$('#optslist'+opt_id).html('<img src="' + tpl_url + '/images/loader.gif" alt="" />');
	setTimeout("$('#optslist"+opt_id+"').load('"+base_url + "ajax/show-suboptions/option/"+ opt_id+"')",2000);
}
function add_subopt(opt_id) {
	for( var lang in site_languages ) {
		if ($('#newsubopttitle'+opt_id+'_'+site_languages[lang]).val().length < 3 ) {
			alert('Type at least 3 characters!');
			return;
		} 
	}
	cms_user_action_submit('add-subopt','add_subopt'+opt_id,'opt_id='+opt_id);
}
function show_edit_subopt(opt_id, subopt_id)
{
		$('#trsuboptedit'+opt_id+'').show();
		for( var lang in site_languages ) {
			$('#subopttitle'+opt_id+'_'+site_languages[lang]).val(''+$('#tdsubopt'+subopt_id+'_'+site_languages[lang]+'').html());
		}
		$('#suboptid'+opt_id).val(''+subopt_id);
}
function edit_subopt(opt_id) {
	for( var lang in site_languages ) {
		if ($('#subopttitle'+opt_id+'_'+site_languages[lang]).val().length < 3 ) {
			alert('Type at least 3 characters!');
			return;
		} 
	}
	cms_user_action_submit('edit-subopt','edit_subopt'+opt_id,'opt_id='+opt_id);
}

function delete_suboption(opt_id)
{
	if (confirm("Are you sure do you want to delete this suboption?"))
	{
		$('#trsubopt'+opt_id)
			.hide()
			.load(base_url + 'ajax/delete-suboption/opt/' + opt_id)
	}
}
function delete_option(opt_id)
{
	if (confirm("Are you sure do you want to delete this option?"))
	{
		$('#mainopt'+opt_id)
			.hide()
			.load(base_url + 'ajax/delete-option/opt/' + opt_id)
	}
}
function change_option_status(catopt_id,optstatus)
{
	$('#optstatus'+catopt_id)
			.html('<img src="' + tpl_url + '/images/loader.gif" alt="" />')
			.load(base_url + 'ajax/catopts-change-status/status/' + catopt_id + '/'+optstatus);
}

function checkform(what)
{
	if (what == 'add_cat')
	{
		for( var lang in site_languages ) {
			if ($('#catname_'+site_languages[lang]).val().length < 3 ) {
				alert('Type at least 3 characters!');
				return false;
			} 
		}
		return true;
	}
	return true;
}

function catopts_change_isrequired(catopt_id, issreq)
{
	$('#isreq'+catopt_id)
			.html('<img src="' + tpl_url + '/images/loader.gif" alt="" />')
			.load(base_url + 'ajax/catopts-change-isrequired/catopts/' + catopt_id + '/' + issreq);
}
function admin_show_edit_settings(settings_var)
{
	setTimeout("tb_show('',base_url+'popup/admin-show-edit-settings/var/"+settings_var+"?TB_iframe=true&KeepThis=true&height=350&width=500&modal=true','');",500);
}
function action_admin_edit_settings(settings_var) {
	cms_user_action_submit('admin-edit-settings','admin-edit-settings','var='+settings_var);
}
//shoty
function category_propose_add(id,name)
{
	if ($('#proposed_catid').val()!=0)
	{
		$('#propose_'+$('#proposed_catid').val()).show();
	}
	$('#propose_'+id).hide();
	$('input[id^="catname_"]').val(name);
	$('#proposed_catid').val(id);
	$('#propose_'+id).hide();
	$('#poposed_cat_parent').show();
}
function category_propose_delete(id)
{
	if (id==0) 
	{
		$('#proposed_categories').html('<img src="' + tpl_url + '/images/loader.gif" alt="" />');
		$('#proposed_categories').load(base_url+'admin/ajax/delete_proposed_category/html/');
	}
	else
		cms_action('delete_proposed_category',id,0,0,'proposed_categories')
}
function update_newsletter_hf()
{
	cms_action_submit('newsletter_select_template','newsletteradd','');
}
