function add_content(content_id, url) {
	window.__content_id = content_id;
	$.get(url, function(content){
		content_id = window.__content_id;
		$(content).insertBefore(content_id);
	});
}

function change_content_type() {
	var type = $('#do_page_type select').val();
	var count = 0;
	var fields = new Array();
	fields[count++] = 'type';
	switch(type) {
		default:
			$('#do_page_type select').val('content');
		case 'content':
			count = 0;
			break;
		case 'section_header':
			fields[count++] = 'title';
			fields[count++] = 'status';
			fields[count++] = 'parent_id';
			fields[count++] = 'order';
			break;
	}

	if(count != 0) {
		$('.form_item').hide();
		for(var i = 0; i < count; i++) {
			$('#do_page_' + fields[i]).show();
		}
	} else {
		$('.form_item').show();
	}
}

function change_form_autorespond() {
	var value = $('#do_custom_form_autorespond input:checked').val();
	var count = 0;
	var fields = new Array();
	fields[count++] = 'message';
	fields[count++] = 'email_field';
	fields[count++] = 'notify_bad_email';

	if(value == 0) {
		for(var i = 0; i < count; i++) {
			$('#do_custom_form_' + fields[i]).hide();
		}
	} else {
		for(var i = 0; i < count; i++) {
			$('#do_custom_form_' + fields[i]).show();
		}
	}
}

function change_form_payment() {
	var value = $('#do_custom_form_payment input:checked').val();
	var count = 0;
	var fields = new Array();
	fields[count++] = 'payment_processor';

	if(value == 0) {
		for(var i = 0; i < count; i++) {
			$('#do_custom_form_' + fields[i]).hide();
		}
	} else {
		for(var i = 0; i < count; i++) {
			$('#do_custom_form_' + fields[i]).show();
		}
	}
}

function toggle_navigation(anchor) {
	var original_id = anchor.id;
	anchor.id = 'toggle_navigation';
	
	if($('#toggle_navigation').hasClass('expanded')) {
		$('#toggle_navigation').removeClass('expanded');
		$('#toggle_navigation').siblings('ul').hide();
		$('#toggle_navigation').siblings('a').removeClass('expanded')
	} else {
		$('#toggle_navigation').addClass('expanded');
		$('#toggle_navigation').siblings('ul').show();
		$('#toggle_navigation').siblings('a').addClass('expanded')
	}

	anchor.id = original_id;
	return false;
}

function preload(arrayOfImages) {
    $(arrayOfImages).each(function(){
        $('<img/>')[0].src = this;
    });
}

function reset_session_variable(variable) {
	$.getJSON(base_url + 'reset_session_variable.html?variable=' + urlencode(variable), function(j){});
}

/* Usage:

preload([
    'img/imageName.jpg',
    'img/anotherOne.jpg',
    'img/blahblahblah.jpg'
]);
*/

function var_dump(data,addwhitespace,safety,level) {
    var rtrn = '';
    var dt,it,spaces = '';
    if(!level) {level = 1;}
    for(var i=0; i<level; i++) {
       spaces += '   ';
    }//end for i<level
    if(typeof(data) != 'object') {
       dt = data;
       if(typeof(data) == 'string') {
          if(addwhitespace == 'html') {
             dt = dt.replace(/&/g,'&amp;');
             dt = dt.replace(/>/g,'&gt;');
             dt = dt.replace(/</g,'&lt;');
          }//end if addwhitespace == html
          dt = dt.replace(/\"/g,'\"');
          dt = '"' + dt + '"';
       }//end if typeof == string
       if(typeof(data) == 'function' && addwhitespace) {
          dt = new String(dt).replace(/\n/g,"\n"+spaces);
          if(addwhitespace == 'html') {
             dt = dt.replace(/&/g,'&amp;');
             dt = dt.replace(/>/g,'&gt;');
             dt = dt.replace(/</g,'&lt;');
          }//end if addwhitespace == html
       }//end if typeof == function
       if(typeof(data) == 'undefined') {
          dt = 'undefined';
       }//end if typeof == undefined
       if(addwhitespace == 'html') {
          if(typeof(dt) != 'string') {
             dt = new String(dt);
          }//end typeof != string
          dt = dt.replace(/ /g,"&nbsp;").replace(/\n/g,"<br>");
       }//end if addwhitespace == html
       return dt;
    }//end if typeof != object && != array
    for (var x in data) {
       if(safety && (level > safety)) {
          dt = '*RECURSION*';
       } else {
          try {
             dt = var_dump(data[x],addwhitespace,safety,level+1);
          } catch (e) {continue;}
       }//end if-else level > safety
       it = var_dump(x,addwhitespace,safety,level+1);
       rtrn += it + ':' + dt + ',';
       if(addwhitespace) {
          rtrn += '\n'+spaces;
       }//end if addwhitespace
    }//end for...in
    if(addwhitespace) {
       rtrn = '{\n' + spaces + rtrn.substr(0,rtrn.length-(2+(level*3))) + '\n' + spaces.substr(0,spaces.length-3) + '}';
    } else {
       rtrn = '{' + rtrn.substr(0,rtrn.length-1) + '}';
    }//end if-else addwhitespace
    if(addwhitespace == 'html') {
       rtrn = rtrn.replace(/ /g,"&nbsp;").replace(/\n/g,"<br>");
    }//end if addwhitespace == html
    return rtrn;
 }