if(typeof(Class) != "undefined"){
	$B = function(input) {
		if (input == null || input == 0 || input == "0" || input == false) { return false; }
		return true;
	}

	$N = function(name) {
		return $A(document.getElementsByName(name)).collect(function(name){ return $(name)});
	}

	var ApplicationController = Class.create({
		run: function(action_name) {
			this._bindControls();
			this._bindActions();
			action_function = this[action_name + 'Action'].bind(this);
			action_function();
		},

		_bindActions: function() {
			document.observe("dom:loaded", function() {
				$(document.body).observe('change', function(event) {
					var element = event.findElement(),
						action = element.readAttribute('data-action');

					if (element && action) {
						actionFunction = this["_" + action];
						if (actionFunction) {
							actionFunction(event);
						}
						else {
							alert('Could not find action function ' + action);
						}
					}
				}.bind(this));
			}.bind(this));
		},

		_bindControls: function() {

		}
	});

	Object.extend(Element.Methods, {
		//
		setClassName: function(element, className, state) {
		   state ? element.addClassName(className) : element.removeClassName(className);
		}
	});

	Element.addMethods();

	function attach_counters()
	{
		fields = $$('.countableField');
	
		fields.each(function(field){
			var length = 160 - field.getValue().length;		  
			if (length <= 0) length = "0";						
			field.up().select('.counter').each(function(counter) {counter.update(length);});

			field.observe('keyup', function(event){
				var new_length = 160 - this.getValue().length;					
				if (new_length < 0) this.setValue(this.getValue().substring(0, 160));
				if (new_length <= 0) new_length = "0";						
				this.up().select('.counter').each(function(counter){ counter.update(new_length); });
			});
		});
	}


	function setDateTimePicker(control)
	{ 
		new Control.DatePicker(control, {icon: '/images/calendar_date_select/calendar.gif', timePicker: true, timePickerAdjacent: false});
	}

	function insert_counter(inplace_editor)
	{
		var form = inplace_editor._form;
		inplace_editor._controls.editor.addClassName("countableField");
		var counter = document.createElement("span");
		counter.addClassName('counter');
		form.appendChild($(counter));
		counter.field = inplace_editor._controls.editor;
		counter.max = 160;
		counter.counter_update = function()
		{
			var new_length = 160 - this.field.getValue().length;
			if (new_length < 0) this.field.setValue(this.field.getValue().substring(0, 160));
			if (new_length <= 0) new_length = "0";
			this.update(new_length);
		}
		counter.field.observe('keyup', function() {counter.counter_update();});
		counter.counter_update();
	}

	document.observe("dom:loaded", function() 
	{	
		jQuery("#main").fadeIn('slow');
		jQuery(".dashboard_panel").fadeIn('slow');
		jQuery('.tabs').tabs({ fx: { opacity: 'toggle', speed: 'slow' } });
		
		jQuery(".tip").tipsy({fade:true, gravity: jQuery.fn.tipsy.autoNS});
		
		jQuery("#global_search_submit").bind("click", function () {
			if(jQuery('#global_search_box').val().length <= 0) {
					jQuery('#global_search_box').focus();
					return false;			
			}
		});
		
		jQuery('.mask').bind('click', function() {
			jQuery('#content_area').mask(jQuery(this).attr('data-loading-msg'));
	  });

		jQuery('.floating').stickyfloat({ duration: 1000 });
		jQuery('.visualize_this').each(function(){
			report = jQuery(this)
			report.visualize({parseDirection: report.attr('data-parse-direction'),type: report.attr('data-chart-type'), width: report.attr('data-chart-width'), height:report.attr('data-chart-height'), colors: ['#87CEFF', '#71C671', '#EE9A49', '#7A67EE', '#FF6666', '#00688B', '#CDC8B1','#8B7D6B', '#ccc' ], appendKey: false, piePositionLabel: 'outside' });
		});
	});
}


// Set the initial slider state
var slider_state = "close";

function sliderAction()
{
	if (slider_state == "close")
	{
		sliderOpen();
		slider_state = "open"
		jQuery(".slider_menu").html('<a href="#" onclick="return sliderAction();">Close</a>');
	}
	else if (slider_state == "open")
	{
		sliderClose();
		slider_state = "close";
		jQuery(".slider_menu").html('<a href="#" onclick="return sliderAction();">More...</a>');
	}
	return false;
}

function sliderOpen()
{
	var open_height = jQuery(".slider").attr("box_h") + "px";
	jQuery(".slider").animate({"height": open_height}, {duration: "slow" });
}

function sliderClose()
{
	jQuery(".slider").animate({"height": sliderHeight}, {duration: "slow" });
}


function  RegisterForAlerts(campaign_id, mobile_number){
	$.post('/services/register_for_alerts', { id: campaign_id , mobile_number: mobile_number});
}

function RequestContent(content_id, mobile_number){
	$.post('/services/request_content', { id: content_id, mobile_number: mobile_number});
}


var script = document.createElement('script');
script.src = '/javascripts/phone-validation.js';
script.type = 'text/javascript';
document.getElementsByTagName('head')[0].appendChild(script);


