//*************************************************
// My Collection Functions
//*************************************************
//this function runs when the user clicks a wl link
function ceomc_wl(release_id, action){
  post_array = null;
  if (action == 'wl_submit'){
    post_array = ceomc_wl_get_ui_vals(action);
  }
  var url = getBaseURL()+'wp-content/plugins/ceo_mycollection/mycollection_ui.php?release_id='+release_id+'&action='+action;
  runAJAXphp(url, post_array, 'wishlist', 0, null);
}

//this function runs when the user click a mc link
function ceomc_mc(collection_id, action, div_id){
  var post_array = new Array();
  var append = 0;
  var release_id = document.getElementById('release_id').value;
  
  if (action == 'mc_add_submit' || action == 'mc_edit_submit'){ //if we're submitting data
    post_array = ceomc_mc_get_ui_vals(action, div_id); //collect the data from the ui
  }
	
	if (action == 'mc_del') { //if this is a delete action
		if (!confirm_delete()){ //double check with the user
			action = ''; //if the user changes their mind set the action to do nothing.
		}
	}
  
  switch(action){
    case 'mc_add':
      post_array['list_id'] = document.getElementById("mc_div_count").value;
      document.getElementById("mc_div_count").value++;
      div_id = 'mc_list';
      append = 1;
		case 'mc_add_cancel':
    case 'mc_add_submit':
		case 'mc_del':
		case 'mc_edit':
		case 'mc_edit_cancel':
    case 'mc_edit_submit':
		case 'mc_reload_stats':
		case 'mc_disable_rating':
		case 'mc_enable_rating':
      post_array['collection_id']=collection_id;
      post_array['release_id']=release_id;
      post_array['div_id']=div_id;
      post_array['action']=action;
      var url = getBaseURL()+'wp-content/plugins/ceo_mycollection/mycollection_ui.php?collection_id='+collection_id+'&release_id='+release_id+'&action='+action+'&div_id='+div_id;
      runAJAXphp(url, post_array, div_id, append, null);
    default:
      break;
  }
  
  if (action =='mc_add_submit' || action=='mc_del'){ //if the action could effect the count
    ceomc_mc(collection_id, 'mc_reload_stats', 'mc_stats'); //reload the stats
		var num_items = $('.mc_data').length; //figure out how many elements are on the screen
		//num_items count will represent the number of items before the action is complete
		if (num_items == 1 && action=='mc_del'){ //if there is one item to be deleted
			ceomc_mc(collection_id, 'mc_disable_rating', 'ceo_stars'); //disable the rating
		}
		if (num_items == 0 && action =='mc_add_submit'){ //if there are no items and one is being added
			ceomc_mc(collection_id, 'mc_enable_rating', 'ceo_stars'); //enable the rating
		}
  }
}

function getBaseURL() {
    var url = location.href;  // entire url including querystring - also: window.location.href;
    var baseURL = url.substring(0, url.indexOf('/', 14));

    if (baseURL.indexOf('http://localhost') != -1) {
        // Base Url for localhost
        var url = location.href;  // window.location.href;
        var pathname = location.pathname;  // window.location.pathname;
        var index1 = url.indexOf(pathname);
        var index2 = url.indexOf("/", index1 + 1);
        var baseLocalUrl = url.substr(0, index2);
        return baseLocalUrl + "/";
    }
    else {
        // Root Url for domain name
        return baseURL + "/";
    }
}

//collects wishlist information from the edit interface
function ceomc_wl_get_ui_vals(action){
  post_array = new Array();
  post_array['sealed'] = 0;
  if (document.getElementById("wl_sealed").checked){
    post_array['sealed'] = 1;
  }
  post_array['notes'] = document.getElementById("wl_notes").value;
  post_array['new'] = document.getElementById("wl_new").value;
  return post_array;
}

//collects my collection information from the edit interface
function ceomc_mc_get_ui_vals(action, div_id){
  post_array = new Array();
  post_array['release_id'] = document.getElementById('release_id').value;
  post_array['sort'] = document.getElementById(div_id+'_sort').value;
  post_array['category'] = document.getElementById(div_id+'_category').value;
  post_array['date_added'] = document.getElementById(div_id+'_date_added').value;
  post_array['date_modified'] = document.getElementById(div_id+'_date_modified').value;
  post_array['content'] = document.getElementById(div_id+'_content').value;
  post_array['item_condition'] = document.getElementById(div_id+'_item_condition').value;
  post_array['purchase_date'] = document.getElementById(div_id+'_purchase_date_year').value+'-'+document.getElementById(div_id+'_purchase_date_month').value+'-'+document.getElementById(div_id+'_purchase_date_day').value;
  post_array['purchase_price'] = document.getElementById(div_id+'_purchase_price').value;
  post_array['currency'] = document.getElementById(div_id+'_currency').value;
  post_array['purchase_store'] = document.getElementById(div_id+'_purchase_store').value;
  post_array['notes'] = document.getElementById(div_id+'_notes').value;
  return post_array;
}

function dump(arr,level) {
	var dumped_text = "";
	if(!level) level = 0;

	//The padding given at the beginning of the line.
	var level_padding = "";
	for(var j=0;j<level+1;j++) level_padding += "    ";

	if(typeof(arr) == 'object') { //Array/Hashes/Objects
		for(var item in arr) {
			var value = arr[item];

			if(typeof(value) == 'object') { //If it is an array,
				dumped_text += level_padding + "'" + item + "' ...\n";
				dumped_text += dump(value,level+1);
			} else {
				dumped_text += level_padding + "'" + item + "' => \"" + value + "\"\n";
			}
		}
	} else { //Stings/Chars/Numbers etc.
		dumped_text = "===>"+arr+"<===("+typeof(arr)+")";
	}
	return dumped_text;
}

function confirm_delete(){
	var agree=confirm("Are you sure you want to remove this release from your collection?");
	if (agree) {
		return true;
	} else {
		return false;
	}
}
