/* menu */
var menu = {
	admin:	['pods', 'modules', 'users', 'settings','earings'],
	earings:['ebay','paypal','amazon','cafepress'],
	profile:['info', 'settings', 'payment','earings']
}

/*
*  AJAX IFRAME METHOD (AIM)
*  http://www.webtoolkit.info/
*/
AIM = {
	frame : function(c) {
		var n = 'f' + Math.floor(Math.random() * 99999);
		var d = window.document.createElement('DIV');
		d.innerHTML = '<iframe style="display:none" src="about:blank" id="'+n+'" name="'+n+'" onload="AIM.loaded(\''+n+'\')"></iframe>';
		window.document.body.appendChild(d);
		var i = window.document.getElementById(n);
		if (c && typeof(c.onComplete) == 'function') {
			i.onComplete = c.onComplete;
		}
		return n;
	},
	form:function( form, name ){
		form.setAttribute('target', name);
	},
	submit : function(f, c) {
		AIM.form(f, AIM.frame(c));
		if (c && typeof(c.onStart) == 'function') {
			return c.onStart();
		} else {
			return true;
		}
	},

	loaded : function( id )
	{
		var i = window.document.getElementById(id);
		if (i.contentDocument) {
			var d = i.contentDocument;
		} else if (i.contentWindow) {
			var d = i.contentWindow.document;
		} else {
			var d = window.frames[id].document;
		}
		if (d.location.href == "about:blank") {
			return;
		}

		if (typeof(i.onComplete) == 'function') {
			i.onComplete(d.body.innerHTML);
		}
	}

}

/*
*
* DASH_HELPER
*
*/

function display( id ) {
	window.document.getElementById(id).style.display = 'block';
}
function show( id ) {
	window.document.getElementById(id).style.display = 'block';
}

function hide( id )
{
	window.document.getElementById(id).style.display = 'none';
	//$( id ).hide(); / prototype
}

function display_dash_el(id) {
	window.document.getElementById( id ).style.display = 'block';
}

function hide_dash_el(id) {
	window.document.getElementById(id).style.display = 'none';
}

function oe( module_id )
{
	openEdit( 'editform' + module_id, module_id );
	if( colorpicker == true )
	{
		var inner = window.document.getElementById( "col_pic_num_" + module_id ).innerHTML;
		if( inner == '1' ){
			setTimeout("colorpicker_create1('"+window.document.getElementById("col_pic_col1_"+module_id).innerHTML+"', " + module_id +")",1000);
		}
		if( inner == '2' ){
			setTimeout("colorpicker_create2('"+window.document.getElementById("col_pic_col1_"+module_id).innerHTML+"','"+window.document.getElementById("col_pic_col2_"+module_id).innerHTML + "', " + module_id+")",1000);
		}
	}
	return false;
}

function colorpicker_create1(color, module_id) {
//window.confirm('color create_' + module_id);
	cp1 = new Refresh.Web.ColorPicker('cp1_' + module_id, {startHex: color, startMode:'s'});
}

function colorpicker_create2(color1, color2, module_id) {
	cp1 = new Refresh.Web.ColorPicker('cp1_' + module_id, {startHex: color1, startMode:'s'});
	cp2 = new Refresh.Web.ColorPicker('cp2_' + module_id, {startHex: color2, startMode:'s'});
}

function openEdit( div_id, module_id ) {
	new Ajax.Updater( div_id, conf.base_url + 'dashboard/showModuleForm/' + module_id + '/admin', { evalScripts:true } );
}

function ajax_get_modules( cat_id ) {
	new Ajax.Updater('div_modules', conf.base_url + 'dashboard/getModules/'+cat_id, {evalScripts:true} );
}

/* function sendFormData(module_id) {
	new Ajax.Updater('div_'+module_id, '/podshare/dashboard/saveModule/'+module_id, {
		parameters: $('form_'+module_id).serialize(true)
	});
	window.document.getElementById('editform'+module_id).innerHTML = '';
}
 */
function sendFormData1(module_id) {
	if(colorpicker==true) {
		var inner = window.document.getElementById("col_pic_num_"+module_id).innerHTML;
		if(inner=='1') {
			cp1 = null;
			var testNode = window.document.getElementById('colorpicker_absolute_cp1_'+module_id+'_map');
			window.document.body.removeChild(testNode);
			var testNode = window.document.getElementById('colorpicker_absolute_cp1_'+module_id+'_slider');
			window.document.body.removeChild(testNode);
		}
		if(inner=='2') {
			cp1 = null;
			cp2 = null;
			var testNode = window.document.getElementById('colorpicker_absolute_cp1_map');
			window.document.body.removeChild(testNode);
			var testNode = window.document.getElementById('colorpicker_absolute_cp2_map');
			window.document.body.removeChild(testNode);
			var testNode = window.document.getElementById('colorpicker_absolute_cp1_slider');
			window.document.body.removeChild(testNode);
			var testNode = window.document.getElementById('colorpicker_absolute_cp2_slider');
			window.document.body.removeChild(testNode);
		}
	}
	return true;
}

function cancelForm(module_id) {
	window.document.getElementById('editform'+module_id).innerHTML = '';
		if(colorpicker==true) {
		inner = window.document.getElementById("col_pic_num_"+module_id).innerHTML;
		if(inner=='1') {
			cp1 = null;
			var testNode = window.document.getElementById('colorpicker_absolute_cp1_'+module_id+'_map');
			window.document.body.removeChild(testNode);
			var testNode = window.document.getElementById('colorpicker_absolute_cp1_'+module_id+'_slider');
			window.document.body.removeChild(testNode);
		}
		if(inner=='2') {
			cp1 = null;
			cp2 = null;
			var testNode = window.document.getElementById('colorpicker_absolute_cp1_map');
			window.document.body.removeChild(testNode);
			var testNode = window.document.getElementById('colorpicker_absolute_cp2_map');
			window.document.body.removeChild(testNode);
			var testNode = window.document.getElementById('colorpicker_absolute_cp1_slider');
			window.document.body.removeChild(testNode);
			var testNode = window.document.getElementById('colorpicker_absolute_cp2_slider');
			window.document.body.removeChild(testNode);
		}
	}
}

function ajax_edit_pod_Callback(response) {
	Element.hide('pod_edit');
	//window.location.reload(true);
	window.document.getElementById('ajax_pod_details').innerHTML = response;
}

function updateList(container) {
	var ajax = new Ajax.Request( conf.base_url + 'dashboard/reorderModules', { method: 'post', parameters: Sortable.serialize(container.id) });
}

function updateModuleList(container) {
	var ajax = new Ajax.Request( conf.base_url + 'profile/defaultModules', { method: 'post', parameters: Sortable.serialize(container.id) });
}

function ajax_load_suspended()
{
	new Ajax.Updater('suspended_div', conf.base_url + 'profile/loadSuspended', { evalScripts:true } );
	Element.hide('choose_suspended');
	Element.show('suspended_div');
}

function saveFormData( module_id )
{
	new Ajax.Updater('ajax', conf.base_url + 'dashboard/saveFormData/'+module_id, {
		parameters: $( 'form_'+module_id ).serialize( true )
	});
}
function ajax_startCallback(module_id) {
	// make something useful before submit (onStart)
	window.document.getElementById('module_id').innerHTML = module_id;
	return true;
}

function ajax_completeCallback(response) {

	// make something useful after (onComplete)
	var module_id = window.document.getElementById('module_id').innerHTML;
	window.document.getElementById('div_'+module_id).innerHTML = response;
	window.document.getElementById('editform'+module_id).innerHTML = '';
	if(colorpicker==true) {
		window.document.getElementById("styles_"+module_id).innerHTML = window.document.getElementById("colorpicker_styles_"+module_id).innerHTML;
	}
	//alert(response);
}
function ajax_completeCallbackAdmin(response) {
	// make something useful after (onComplete)
	var module_id = window.document.getElementById('module_id').innerHTML;
	window.document.getElementById('div_adm_'+module_id).innerHTML = response;
	window.document.getElementById('editform'+module_id).innerHTML = '';
	if( response == 'colorbox' || response == 'text_list_votex'/*colorpicker==true*/) {
		//window.document.getElementById("styles_"+module_id).innerHTML = window.document.getElementById("colorpicker_styles_"+module_id).innerHTML;
		window.location.reload(true);
	}
	//alert(response);
}

function doModuleAjax(div_id, base_path, plug_name, func_name, pod_plug_id, modul_params1, modul_params2)
{
	var target = div_id;
	var url = base_path + 'dashboard/call/'+ plug_name + '/' + func_name + '/' + pod_plug_id + '/' + modul_params1 + '/' + modul_params2;
	new Ajax.Updater( target, url );
}

function doModuleAjaxInsert(div_id, base_path, plug_name, func_name, modul_params, pod_plug_id, last_item_index)
{
	var url = base_path + 'dashboard/call/'+ plug_name + '/' + func_name + '/' + modul_params + '/' + pod_plug_id + '/' + last_item_index;
	//alert( div_id + ':' + url );
	new Ajax.Updater(div_id, url, { insertion:Insertion.Bottom } );
}

function AddTagElementNewPod(divTagsID)
{
  var newDivTag = window.document.createElement('p');
  var num_tag_field=parseInt(window.document.getElementById('div_num_last_tag').innerHTML, 10)+1;
  newDivTag.id = 'tags' + num_tag_field;
	my_div_tag = window.document.getElementById(divTagsID);
  newDivTag.innerHTML = langarr['addtagelementnewpod_tword'] + num_tag_field + ': <input type="text" size="20" maxlength="255" name="tags' + num_tag_field + '" value="" />';
  my_div_tag.appendChild(newDivTag);
  return num_tag_field;
}

function RemoveTagElementNewPod(divTagsID){
	var num_tag_field=parseInt(window.document.getElementById('div_num_last_tag').innerHTML, 10);
	if( num_tag_field > 0 ) {
		//window.document.getElementById('new_pod_tags_add').removeChild(window.document.getElementById('new_pod_tags_add').lastChild);
		window.document.getElementById('new_pod_tags_add').removeChild( window.document.getElementById('tags'+num_tag_field) );
		num_tag_field--;
		window.document.getElementById('div_num_last_tag').innerHTML=num_tag_field;
	}
}

function AddTagEditElementPod(base_url, divTagsID, num_last_tag){
  var newDivTag = window.document.createElement('div');
  var num_tag_field =parseInt(window.document.getElementById('num_tag_edit_div').innerHTML, 10);
  newDivTag.id = 'tag_num_' + num_tag_field;
	my_div_tag = window.document.getElementById(divTagsID);
	newDivTag.innerHTML = '<span><input type="text" size="20" maxlength="255" name="tags' + num_tag_field + '" id="tags' + num_tag_field + '" value="" /></span>' + ' <img src="' + base_url + 'images/design/delete_g.gif" onclick="window.document.getElementById(' + "'tags" + num_tag_field + "').value=''; window.document.getElementById('tag_num_" + num_tag_field + "').style.display='none';" + '"/><br />';
  my_div_tag.appendChild(newDivTag);
  num_tag_field++;
  window.document.getElementById('num_tag_edit_div').innerHTML = num_tag_field;
}

function EditTagsAjax(base_url, pod_id) {
	new Ajax.Updater('edit_tags_ajax', base_url + 'dashboard/edittags/'+ pod_id);
}

function validate_search () {
	var pattern=/[^ \+\"a-zA-Z0-9\@_\.\-]/i;
	var textvalue = window.document.getElementById('search').search_word.value;
	if( ! textvalue )
	{
		window.alert( langarr['val_search_insterm'] );
		return false;
	}
	if( pattern.test( textvalue ) ) {
		window.alert( langarr['val_search_chkspell'] );
		return false;
	}
	return true;
}

function saveRatingNew (base_url, rating, pod_id) {
	var url = conf.base_url + 'show/rate/'+ rating + '/' + pod_id;
	new Ajax.Updater('rate_pod_stars', url );
}


function ValidateEditTags(base_url, pod_id) {
	var tag_field_num = 0;
	var tags_arr = new Array();
	var tag_field_name = 'tags' + tag_field_num;
	var pattern_blank = /[^a-zA-Z0-9]+/i;
	while ( window.document.getElementById(tag_field_name) ) {
		var tag = window.document.getElementById(tag_field_name).value;
		if ( tag && !pattern_blank.test(tag) ) {
			tags_arr.push(tag);
		}
		tag_field_num ++ ;
		tag_field_name = 'tags' + tag_field_num;		
	}
	var tags_url = tags_arr.join('/');
	new Ajax.Updater( 'edit_tags_ajax', base_url + 'dashboard/savetags/' + pod_id + '/' + tags_url );
}

function CloneModule ( base_url, pod_plug_id )
{
	var url = base_url + 'dashboard/CloneModule/' + pod_plug_id;
	new Ajax.Updater( 'clone_div_aj_' + pod_plug_id, url );
}

function CloneModuleSave ( base_url, pod_plug_id, clone_method, clone_in_pod, method_r ) {
	var thisform = window.document.getElementById( 'clone_form_ajax_' + pod_plug_id );
	if ( method_r == 'continue' ) {
			new Ajax.Updater( 'clone_div_aj_' + pod_plug_id, base_url + 'dashboard/CloneModuleSave/' + pod_plug_id + '/' + clone_in_pod + '/' + clone_method + '/' + method_r );
	} else {
		window.location = base_url + 'dashboard/CloneModuleSave/' + pod_plug_id + '/' + clone_in_pod + '/' + clone_method + '/' + method_r;
	}
}

function CancelAjaxDiv(aj_div) {
	window.document.getElementById( aj_div ).innerHTML = '';
}

function ReportPodAj ( base_url ) {
	new Ajax.Updater( 'report_pod_aj', base_url + 'show/ReportPodForm/' );
	return false;
}

function ReportPodSend(base_url, r_pod_coded_url) {
	var thisform = window.document.getElementById( 'report_form' );
	var r_type = thisform.report_type.value;
	var where_is = thisform.report_where.value;
	var pattern=/[a-zA-Z]/i;
	if( !where_is || !pattern.test(where_is) ){window.alert(langarr['reportpodsend_where']); return false;}
	var r_message = thisform.report_message.value;
	if( !r_message || !pattern.test(r_message) ){window.alert(langarr['reportpodsend_msg']); return false;}
	hide_dash_el('report_pod_hidden');
	var rep_uri = base_url + 'show/ReportPodSave/' + r_pod_coded_url;
	new Ajax.Updater( 'report_pod_aj', rep_uri, {
		parameters: { report_type: r_type, report_where: where_is, report_message: r_message }
	});
	return true;
}

function RemoveFromReported(base_url, pod_c_url) {
	if (window.confirm(langarr['removefromreported_rpod'] + pod_c_url + langarr['removefromreported_fromrl']) ){
		window.document.getElementById('show_reported_pods').removeChild( window.document.getElementById('show_reported_pods_' + pod_c_url) );
		if ( window.document.getElementById('admin_suspendPod_url').value == pod_c_url ) {
			window.document.getElementById('admin_suspendPod_url').value = '';
		}
		new Ajax.Updater( 'reported_p_users_aj', base_url + 'profile/RemoveFromReported/' + pod_c_url );
	} else {
	 return false;
	}
}

function getReportUsers(base_url, pod_c_url) {
	if(pod_c_url){
		new Ajax.Updater( 'reported_p_users_aj', base_url + 'profile/getReportUsers/' + pod_c_url );
	}
}

function MarkPodAsAdult(base_url, pod_c_url) {
	new Ajax.Updater( 'reported_p_users_aj', base_url + 'profile/MarkPodAsAdult/' + pod_c_url );
}

function DeleteModule(base_url, module_del_id, step_flag ){
	if ( window.confirm(langarr['deletemodule_remove']) ) {
		new Ajax.Updater( 'del_modules_msg_aj', base_url + 'profile/DeleteModule/' + module_del_id + '/' + step_flag );
		if(step_flag == 2){
			window.document.getElementById('module_del').removeChild( window.document.getElementById('del_mod_' + module_del_id) );
		}
	} else {
		return false;
	}
}

function SuspendModule(base_url, module_del_id)
{
		new Ajax.Updater( 'del_modules_msg_aj', base_url + 'profile/SuspendModule/' + module_del_id);
	  suspModEl = window.document.createElement("option") ;
	  suspModEl.value = module_del_id;
	  suspModEl.id = 'show_suspended_mods_' + module_del_id;
	  suspModEl.text = window.document.getElementById('hidden_curr_delmod_name').innerHTML;
		window.document.getElementById('show_suspended_mods').appendChild( suspModEl );
}

function UnsuspendModule(base_url, module_unsusp_id)
{
		new Ajax.Updater( 'unsusp_mod_err_msg_aj', base_url + 'profile/UnsuspendModule/' + module_unsusp_id);
		window.document.getElementById('show_suspended_mods').removeChild( window.document.getElementById('show_suspended_mods_' + module_unsusp_id) );
}

function getModuleConfig(base_url, module_class)
{
	new Ajax.Updater( 'edit_config_module_aj', base_url + 'profile/getModuleConfig/' + module_class );
}

function saveModuleConfig(base_url, module_class)
{
	var aj_url = base_url + 'profile/saveModuleConfig/' + module_class;
	new Ajax.Updater( 'edit_config_module_aj', aj_url, {
		parameters: $('edit_config_p_modules').serialize(true)
	});
}
/* load an charity edit form */
var Charity = {
	loadCharityEditForm:function( id )
	{
		var url		=	conf.base_url + 'profile/ajax_getCharity/' + id;
		var target	=	"edit_charity_hold";
		render.render( target, lang.loading );
		new Ajax.Updater( target, url);
	}
}
/*
*
* FORM
*
*/

function checkForOpenForms() {
  var moduleListForm = $('pod_content');
  var moduleList = moduleListForm.getElementsByTagName('form');
  var found = false;
  var result = true;
  var models = Array();
  for (var i=0; i<moduleList.length; i++) {
    var thediv = moduleList[i];	
	if (thediv.id.indexOf('form_') == 0) {
		found = true;
		models.push(thediv.id.substr(5));
		//alert(thediv.id.substr(5));
	}
  }
  //alert(confirm);
  if (found) {
    if (confirm) {
		var message = langarr['checkforopenforms_unsaved'];
		result = confirm(message);
	}

    if (result) {
		for (var i=0; i<models.length; i++) {
			var model_id = models[i];	
			//alert(model_id);
			saveFormData(model_id);
		}
    }
  }

  return result;
}

function saveAllOpenForms() {
  var moduleListForm = $('pod_content');
  var moduleList = moduleListForm.getElementsByTagName('form');
  var found = false;
  var models = Array();
  for (var i=0; i<moduleList.length; i++) {
    var thediv = moduleList[i];	
	if (thediv.id.indexOf('form_') == 0) {
		found = true;
		models.push(thediv.id.substr(5));
		//alert(thediv.id.substr(5));
	}
  }

  if (found) {
	for (var i=0; i<models.length; i++) {
		var model_id = models[i];	
		saveFormData(model_id);
		//alert("saving "+model_id);
		cancelForm(eval(model_id));
	}
  }
}

/*
*
* PLUGINS
*
*/

function useAjaxPlugin(pods_plugins_id, url) {
	new Ajax.Updater('div_' + pods_plugins_id, url, {parameters: $('plugform_' + pods_plugins_id).serialize(true)});
}

function ajTextVotexVote(div_id, url) {
	var curr_el_votes = window.document.getElementById(div_id).innerHTML;
	new Ajax.Updater(div_id, url);
}

function loadPage(div, url) {
	new Ajax.Updater(div, url);
}
function ajaxForm(div, url, formid) {
	new Ajax.Updater(div, url, {parameters: $(formid).serialize(true)});
}

function openLink( url ) {
	if( url != '' ) {
		window.location = url;
	}
}

function getStyle(el, styleProp) {
	var x = window.document.getElementById(el);
	if (x.currentStyle)
		var y = x.currentStyle[styleProp];
	else if (window.getComputedStyle)
		var y = window.document.defaultView.getComputedStyle(x,null).getPropertyValue(styleProp);
	return y;
}
function ch(x) {
	if (getStyle(x,'display') == 'block') {
		window.document.getElementById(x).style.display = 'none';
	} else {
		window.document.getElementById(x).style.display = 'block';
	}
}

function multi_ch() {
	var plugin_data = plugins_data;
	for( var i = 0; i < arguments.length; i++ ) {
		window.document.getElementById(arguments[i]).style.display = (i != 0) ? 'none' : 'block';
	}
}

function multi_check() {
	for( var i = 0; i < arguments.length; i++ ) {
		window.document.getElementById(arguments[i]).style.display = ( i != 0 ) ? 'none' : 'block';
		if ( i != 0 ) {
			window.document.getElementById(arguments[i]+'_tabr').style.display = 'block';
			window.document.getElementById(arguments[i]+'_tabw').style.display = 'none';
		} else {
			window.document.getElementById(arguments[i]+'_tabr').style.display = 'none';
			window.document.getElementById(arguments[i]+'_tabw').style.display = 'block';
		}
	}
}
var __check = function( where, key )
{
	for( var i in where )
	{
		if ( window.document.getElementById( where[ i ] ) != null  )
		{
			var item = where[ i ];
			window.document.getElementById( item ).style.display = ( item != key ) ? 'none' : 'block';
			if ( item  !=  key ) {
				window.document.getElementById( item+'_tabr').style.display = 'block';
				window.document.getElementById( item+'_tabw').style.display = 'none';
			} else {
				window.document.getElementById( item+'_tabr').style.display = 'none';
				window.document.getElementById( item+'_tabw').style.display = 'block';
			}
		}
	}
}



function voteCh(pods_plugins_id, pod_id, base_url) {
	if ( check(pods_plugins_id,'voted') == 1 ) {
		loadPage('plugins_db', base_url+'dashboard/getJS/'+pod_id);
		window.document.getElementById('div_' + pods_plugins_id + '_vote').style.display = 'none';
		window.document.getElementById('div_' + pods_plugins_id + '_result').style.display = 'block';
	}
}



function AddElement(elementID, element, descr, custom){
	my_element = window.document.getElementById(elementID);
	var lastNum = my_element.childNodes.length;
	var newElement = window.document.createElement(element);
	sum = lastNum + 1;
	if (!custom) {
		newElement.innerHTML = descr + ' <input type="text" name="parameter_' + sum + '" id="parameter_' + sum + '" />';
	} else {
		newElement.innerHTML = descr;
	}
	my_element.appendChild(newElement);
}

function RemoveElement(elementID){
	var element = window.document.getElementById(elementID);
	element.removeChild(element.lastChild);
}

function enable_handle(element) {
	Sortable.create(element,{handle:'handle'});
}



function get() {
	var plugin_data = plugins_data;
	for( var i = 0; i < arguments.length; i++ ) {
		if (plugin_data) {
			plugin_data = plugin_data[arguments[i]];
		}
	}
	if (plugin_data) {
		window.document.write(plugin_data);
	}
}
function check() {
	var plugin_data = plugins_data;
	for( var i = 0; i < arguments.length; i++ ) {
		if (plugin_data) {
			plugin_data = plugin_data[arguments[i]];
		}
	}
	if (plugin_data) {
		return plugin_data;
	}
}

function newVars(pod_id, base_url) {
	loadPage('plugins_db', base_url+'dashboard/getJS/'+pod_id);
}

/*
 
Correctly handle PNG transparency in Win IE 5.5 & 6.
http://homepage.ntlworld.com/bobosola. Updated 18-Jan-2006.

Use in <HEAD> with DEFER keyword wrapped in conditional comments:
<!--[if lt IE 7]>
<script defer type="text/javascript" src="pngfix.js"></script>
<![endif]-->

var arVersion = navigator.appVersion.split("MSIE");
var version = parseFloat( arVersion[1] );
if ( (version >= 5.5) && (window.document.body.filters) ) 
{
	for(var i=0; i<window.document.images.length; i++)
	{
		var img = window.document.images[i]
		var imgName = img.src.toUpperCase()
		if (imgName.substring(imgName.length-3, imgName.length) == "PNG")
		{
			var imgID = (img.id) ? "id='" + img.id + "' " : ""
			var imgClass = (img.className) ? "class='" + img.className + "' " : ""
			var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' "
         var imgStyle = "display:inline-block;" + img.style.cssText 
         if (img.align == "left") imgStyle = "float:left;" + imgStyle
         if (img.align == "right") imgStyle = "float:right;" + imgStyle
         if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle
         var strNewHTML = "<span " + imgID + imgClass + imgTitle
         + " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";"
         + "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
         + "(src=\'" + img.src + "\', sizingMethod='scale');\"></span>" 
         img.outerHTML = strNewHTML
         i = i-1
      }
   }
}
*/
/* timeout */
var wait = {
	delay:function()
	{
		
	}
}
var Render = function()
{
	this.render = function( where, what ){
		$( where ).innerHTML = what;
	}
}
render = new Render();
/* 
* admin / eBay 
*/
var Ebay = {
	current_page:0,
	pagination_targets:[],
	init:function( page, pagination_targets, list_target )
	{
		Ebay.current_page = page;
		Ebay.pagination_targets = pagination_targets;
		Ebay.list_target = list_target;
		$( list_target ).show();
		for( var i = 0; i < Ebay.pagination_targets.length; i++)
		{
			$( Ebay.pagination_targets[i] ).show();
		};
	},
	load_pagination:function()
	{
		var url = conf.base_url + 'profile/admin/get_earings_pagination/' + Ebay.current_page;
		var success = function( data )
		{
			if( data.status == 200 )
			{
				for( var i in Ebay.pagination_targets )
				{
					render.render( Ebay.pagination_targets[i], data.responseText );
				}
			}
		}
		new Ajax.Request( url, { onComplete: success } );
	},
	load_earings:function( page )
	{
		Ebay.current_page = page;
		render.render( Ebay.list_target, lang.loading );
		var url = conf.base_url + 'profile/admin/get_earings_list/' + Ebay.current_page;
		var success = function( data )
		{
			if( data.status == 200 )
			{
				render.render( Ebay.list_target, data.responseText );
				Ebay.load_pagination();
			}else{
				render.broadcast( data.status );
			}
		}
		new Ajax.Request( url, { onComplete: success } );
	},
	clearEaringList:function()
	{
		for( var i = 0; i < Ebay.pagination_targets.length; i++)
		{
			$( Ebay.pagination_targets[i] ).hide();
		};
		$( Ebay.list_target ).hide();
	}
}
/* admin / Amazon */
var Amazon = {
	current_page:0,
	pagination_targets:[],
	init:function( page, pagination_targets, list_target )
	{
		Amazon.current_page = page;
		Amazon.pagination_targets = pagination_targets;
		Amazon.list_target = list_target;
		/* fix */
		$( list_target ).show();
		for( var i = 0; i < Amazon.pagination_targets.length; i++)
		{
			$( Amazon.pagination_targets[i] ).show();
		};
	},
	load_pagination:function()
	{
		var url = conf.base_url + 'profile/admin/get_amazon_earings_pagination/' + Amazon.current_page;
		var success = function( data )
		{
			if( data.status == 200 )
			{
				for( var i in Amazon.pagination_targets )
				{
					render.render( Amazon.pagination_targets[i], data.responseText );
				}
			}
		}
		new Ajax.Request( url, { onComplete: success } );
	},
	load_earings:function( page )
	{
		Amazon.current_page = page;
		render.render( Amazon.list_target, lang.loading );
		var url = conf.base_url + 'profile/admin/get_amazon_earings_list/' + Amazon.current_page;
		var success = function( data )
		{
			if( data.status == 200 )
			{
				render.render( Amazon.list_target, data.responseText );
				Amazon.load_pagination();
			}else{
				render.broadcast( data.status );
			}
		}
		new Ajax.Request( url, { onComplete: success } );
	},
	clearEaringList:function()
	{
		var len = Amazon.pagination_targets.length;
		for( var i = 0; i < len; i++)
		{
			$( Amazon.pagination_targets[i] ).hide();
		};
		$( Amazon.list_target ).hide();
	},
	addNewSuffix:function( suffix )
	{
		/* validation must be here */
		if( suffix == '' ) return false;
		
		var state = confirm( lang.are_you_sure );
		if( !state ) return false;
		var suffix = suffix;
		var url = conf.base_url + 'profile/add_new_amazon_suffix/' + suffix;
		var success = function( data )
		{
			if( data.status == 200 )
			{
				var reply = data.responseText;
				switch( reply )
				{
					case 'exist':
						$('amazon_suffix').value = '';
						$('amazon_err').innerHTML = "Track id already exist, please try another one";
					break;
					case 'yes':
						$('amazon_suffix').value = '';
						$('amazon_err').innerHTML = "Amazon track id sequnce request, please check your mail after 24 hours form confirmation.";
					break;
					default:
				};
			}else{
				render.broadcast( data.status );
			}
		}
		new Ajax.Request( url, { onComplete: success } );
	}
}
/* admin / Paypal */
var Paypal = {
	current_page:0,
	pagination_targets:[],
	init:function( page, pagination_targets, list_target )
	{
		Paypal.current_page = page;
		Paypal.pagination_targets = pagination_targets;
		Paypal.list_target = list_target;
		$( list_target ).show();
		for( var i = 0; i < Paypal.pagination_targets.length; i++)
		{
			$( Paypal.pagination_targets[i] ).show();
		};
	},
	load_pagination:function()
	{
		var url = conf.base_url + 'profile/admin/get_amazon_earings_pagination/' + Paypal.current_page;
		var success = function( data )
		{
			if( data.status == 200 )
			{
				for( var i in Paypal.pagination_targets )
				{
					render.render( Paypal.pagination_targets[i], data.responseText );
				}
			}
		}
		new Ajax.Request( url, { onComplete: success } );
	},
	load_earings:function( page )
	{
		Paypal.current_page = page;
		render.render( Paypal.list_target, lang.loading );
		var url = conf.base_url + 'profile/admin/get_amazon_earings_list/' + Paypal.current_page;
		var success = function( data )
		{
			if( data.status == 200 )
			{
				render.render( Paypal.list_target, data.responseText );
				Paypal.load_pagination();
			}else{
				render.broadcast( data.status );
			}
		}
		new Ajax.Request( url, { onComplete: success } );
	},
	clearEaringList:function()
	{
		var len = Paypal.pagination_targets.length;
		for( var i = 0; i < len; i++)
		{
			$( Paypal.pagination_targets[i] ).hide();
		};
		$( Paypal.list_target ).hide();
	},
	getBalance:function( target )
	{
		var url = conf.base_url + 'profile/getbalance';
		var success = function( data )
		{
			if( data.status == 200 )
			{
				render.render( target, data.responseText );
			}
		}
		new Ajax.Request( url, { onComplete: success } );
		render.render( target, lang.loading );
	}
}
/* Cafepress  */
var Cafepress = function(){
	this.elements = ['advanced','simple','shop'];
	this.show = function( what, id )
	{
		for( i=0; i< this.elements.length; i++ )
		{
			if( this.elements[ i ] == what )
			{
				Element.show( this.elements[ i ] + id );
			}else{
				Element.hide( this.elements[ i ] + id );
			}
		}
	}
	this.order = false;
	this.current_page = 0;
	this.pagination_targets = [];
	this.init = function( page, pagination_targets, list_target )
	{
		this.current_page = page;
		this.pagination_targets = pagination_targets;
		this.list_target = list_target;
		/* fix */
		$( list_target ).show();
		for( var i = 0; i < this.pagination_targets.length; i++)
		{
			$( this.pagination_targets[i] ).show();
		};
	};
	this.load_pagination = function()
	{
		var url = conf.base_url + 'profile/admin/get_cafepress_earings_pagination/' + this.current_page;
		var success = function( data )
		{
			if( data.status == 200 )
			{
				for( var i in this.pagination_targets )
				{
					render.render( this.pagination_targets[i], data.responseText );
				}
			}
		}
		new Ajax.Request( url, { onComplete: success } );
	};
	this.load_earings = function( page )
	{
		this.current_page = page;
		render.render( this.list_target, lang.loading );
		var url = conf.base_url + 'profile/admin/get_cafepress_earings_list/' + this.current_page;
		var success = function( data )
		{
			if( data.status == 200 )
			{
				render.render( Cafepress.list_target, data.responseText );
				this.load_pagination();
			}else{
				render.broadcast( data.status );
			}
		}
		new Ajax.Request( url, { onComplete: success } );
	};
	this.clearEaringList = function()
	{
		var len = this.pagination_targets.length;
		for( var i = 0; i < len; i++)
		{
			$( this.pagination_targets[i] ).hide();
		};
		$( this.list_target ).hide();
	};
	this.productSort = function( id )
	{
		var button = 'me_pick_reorder_button_' + id;
		if( this.order )
		{
			Sortable.destroy('me_add_edit_' + id);
			this.order = false;
			render.render( button, 'Start Reorder');
			
		}else{
			Sortable.create('me_add_edit_' + id);
			this.order = true;
			render.render( button, 'Stop Reorder');
		}
		return false;
	};
	this.verifyTextarea = function( id )
	{
		var url = conf.base_url;
		var last_item_index = window.document.getElementById('last_item_num_me_li_'+ id).innerHTML;
		var last_item_index = parseInt(last_item_index, 10);
		var pattern=/[^ a-zA-Z0-9\n\r]/i;
		var textvalue = window.document.getElementById('cafepress_txta_'+id).value;
		if( ! textvalue )
		{
			alert('The form is empty');
			return false
		}
		if( pattern.test( textvalue ) )
		{
			window.alert('Please check your spelling');
			return false;
		}
		window.document.getElementById('cafepress_txta_'+id).value='';
		textvalue += ' ';
		var ptrnm=/([a-zA-Z0-9]+)[ \n\r]/g;
		var item_id = "";
		var item_id_arr;
		while ( ( item_id_arr = ptrnm.exec( textvalue ) ) != null )
		{
		  item_id += item_id_arr[1]+'_';
		  last_item_index++;
		}
		if( !item_id )
		{
			alert('Please enter a valid productId');
			return false;
		}
		doModuleAjaxInsert('me_add_edit_'+id, url, 'cafepress', 'addCafepressProductsTxt', item_id, id, last_item_index);
		render.render( 'last_item_num_me_li_'+id, last_item_index );
		return true;
	};
	this.HelpAddItem = function(pod_plug_id, pod_id, plug_name, func_name)
	{
		var width = 550;
		var height = 420;
		var _left = ( ( screen.width-width ) >> 1 );
		var _top = ( ( screen.height-height ) >> 1 );
		var url = conf.base_url + 'dashboard/call/'+ plug_name + '/' + func_name + '/' + pod_plug_id + '/' + pod_id;
		myWin = window.open(url,'find_items_help_' + pod_plug_id,'status=yes,scrollbars=yes,resizable=yes,width='+width+',height='+height+',left='+_left+',top='+_top);
		if ( myWin.opener == null) myWin.opener = window;
		myWin.opener.name = 'opener';
		myWin.focus();
	};
	this.runProductSearch = function( pod_plugins_id, store_id, last_index )
	{
		doModuleAjaxInsert('bulkListShop'+pod_plugins_id, conf.base_url, 'cafepress', 'addStoreProductsTxt', store_id, pod_plugins_id, last_index );
		render.render( 'last_item_num_me_li_'+pod_plugins_id, last_index);
		return false;
	}
}
var Cafepress = new Cafepress();
var checkProducts = function ( id )
{
	alert( id );
}
/*
* validation regex.js and validation/check.js
* must be loaded for user side input data validation
*/