function ajax_json(url,data)
{	
	$.ajax({
		type: "POST",
		url: url,
		data: data,
		async: true,
		dataType: "json",
		success: function(data){
			process(data);
		},
		error: function(obj,err,e){
			alert('An error occured during request. Please try again.');
			if (url.indexOf('save')!= -1)
				edit_cancel(true);
			else
				edit_cancel(false);
		}
	});
}
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 edit_cancel(cancel_all)
{
	if (typeof cancel_all == "undefined") cancel_all = true;
	if (cancel_all)
	{
		$('.editlink').css('visibility','');
		$('.menueditlink').css('visibility','');
		destroy_photo_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)
{
	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 = '';
	var type = '';
	if (typeof page_id != "undefined") 
	{
		cmd += '&page_id='+page_id;
		if(page_id == accessories_parent) type += '&type=1';
		if(page_id == products_parent) type += '&type=0';
		if(page_id == shops_parent) type += '&type=2';
	}
	if (typeof prod_id != "undefined") cmd += '&prod_id='+prod_id;
	ajax_json(base_url+"admin/ajax/"+action+"/",'id='+id+cmd+type);
}
function cms_user_action_submit(action,form_id)
{
	ajax_json(base_url+"ajax/"+action+"/save",$('#'+form_id).serialize());	
}
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)
{
	var id = $('#'+what+'_id').val();
	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 show_menu(id)
{
	$("*[id^='menu_']").removeClass('menuselected');
	$("*[id^='menu_']").addClass('menuunselected');
	$("#menu_"+id).removeClass('menuunselected');
	$("#menu_"+id).addClass('menuselected');
	$("#menu_"+id).children().blur();
}
function clear_menu()
{
	$("*[id^='menu_']").removeClass('menuselected');
	$("*[id^='menu_']").addClass('menuunselected');

}
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_subcat(id)
{
	$("#submenuid_"+id).toggle(800);
}
function show_div(divname)
{
	$("#div"+divname).toggle(800);
}
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); }
		 });
	});
	$("*[id^='menu_to_sort']").sortable(
	{ 
		axis:'y',
		handle:'.move_handle',
		containment: $("#menu_sort_containment"),
		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');
	});
	$("*[id^='menu_to_sort']").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;
	}
}
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 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, type)
{	
	extra = '';
	if (typeof type != "undefined") extra = '/'+type;
		else extra = '/0';
	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');
			else if (what=='collection')
				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()+extra+'?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 update_newsletter_hf()
{
	cms_action_submit('newsletter_select_template','newsletteradd','');
}
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;
	}
}
function MytinyMCEinit()
{
    tinyMCE.init({
        mode : "textareas",
		plugins : "ibrowser",
        theme : "advanced",
		theme_advanced_buttons3_add : "ibrowser",
        editor_selector : "mceEditor",
		force_br_newlines: true,
		force_p_newlines: false,
		forced_root_block: ''
    });
}
function fire_thickbox(obj)
{
	var t = obj.title || obj.name || null;
	var a = obj.href || obj.alt;
	var g = obj.rel || false;
	tb_show(t,a,g);
	obj.blur();
	return false;
}
function admin_show_edit_settings(settings_var)
{
	setTimeout("tb_show('',base_url+'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_action_submit('admin-edit-settings','admin-edit-settings','var='+settings_var);
}
