/*=============================
	LOADING GRAPHIC
=============================*/
var loading_graphic = new Image();
loading_graphic.src = '/images/loading.gif';

function displayLoadingGraphic( button ) {
	
	var loading_block = document.createElement('div');
	loading_block.className = 'loading';
	loading_block.appendChild( loading_graphic );
	button.parentNode.appendChild( loading_block );
	button.style.display = 'none';
	button.style.visibility = 'hidden';
}

function sleep(milliseconds) {
  var start = new Date().getTime();
  for (var i = 0; i < 1e7; i++) {
    if ((new Date().getTime() - start) > milliseconds){
      break;
    }
  }
}

/*============================================
	DISPLAY PAGE HIGHLIGHT HANDLERS.
============================================*/
var page_highlights_handlers = new Array();

// REGISTER YOUR HANDLER HERE.
function registerPageHighlightsHandler( handler )
{
	page_highlights_handlers;

	page_highlights_handlers[page_highlights_handlers.length] = handler;
}
// CALL EACH HANDLER
function displayPageHighlights( page_id )
{
	page_highlights_handlers;

	var c;
	for ( c = 0; c < page_highlights_handlers.length; c++ )
	{
		page_highlights_handlers[c].displayPageHighlights( page_id );
	}

	return true;
}

/*====================================================================
	UPDATE PADDING, MARGIN, AND BORDER SETTINGS DYNAMICALLY
====================================================================*/
function set_background_color( module, module_instance_id, hexcolor )
{
	document.getElementById( module + module_instance_id ).style.backgroundColor = hexcolor;
}
function set_border_left_color( module, module_instance_id, hexcolor )
{
	document.getElementById( module + module_instance_id ).style.borderLeftColor = hexcolor;
}
function set_border_left_width( module, module_instance_id, border_width )
{
	document.getElementById( module + module_instance_id ).style.borderLeftWidth = border_width + 'px';
}
function set_border_left_style( module, module_instance_id, style )
{
	document.getElementById( module + module_instance_id ).style.borderLeftStyle = style;
}
function set_border_right_color( module, module_instance_id, hexcolor )
{
	document.getElementById( module + module_instance_id ).style.borderRightColor = hexcolor;
}
function set_border_right_width( module, module_instance_id, border_width )
{
	document.getElementById( module + module_instance_id ).style.borderRightWidth = border_width + 'px';
}
function set_border_right_style( module, module_instance_id, style )
{
	document.getElementById( module + module_instance_id ).style.borderRightStyle = style;
}
function set_border_top_color( module, module_instance_id, hexcolor )
{
	document.getElementById( module + module_instance_id ).style.borderTopColor = hexcolor;
}
function set_border_top_width( module, module_instance_id, border_width )
{
	document.getElementById( module + module_instance_id ).style.borderTopWidth = border_width + 'px';
}
function set_border_top_style( module, module_instance_id, style )
{
	document.getElementById( module + module_instance_id ).style.borderTopStyle = style;
}
function set_border_bottom_color( module, module_instance_id, hexcolor )
{
	document.getElementById( module + module_instance_id ).style.borderBottomColor = hexcolor;
}
function set_border_bottom_width( module, module_instance_id, border_width )
{
	document.getElementById( module + module_instance_id ).style.borderBottomWidth = border_width + 'px';
}
function set_border_bottom_style( module, module_instance_id, style )
{
	document.getElementById( module + module_instance_id ).style.borderBottomStyle = style;
}


function set_padding_left( module, module_instance_id, padding_width )
{
	document.getElementById( module + module_instance_id ).style.paddingLeft = padding_width + 'px';
}
function set_padding_right( module, module_instance_id, padding_width )
{
	document.getElementById( module + module_instance_id ).style.paddingRight = padding_width + 'px';
}
function set_padding_top( module, module_instance_id, padding_width )
{
	document.getElementById( module + module_instance_id ).style.paddingTop = padding_width + 'px';
}
function set_padding_bottom( module, module_instance_id, padding_width )
{
	document.getElementById( module + module_instance_id ).style.paddingBottom = padding_width + 'px';
}

function set_margin_left( module, module_instance_id, margin_width )
{
	document.getElementById( module + module_instance_id ).style.marginLeft = margin_width + 'px';
}
function set_margin_right( module, module_instance_id, margin_width )
{
	document.getElementById( module + module_instance_id ).style.marginRight = margin_width + 'px';
}
function set_margin_top( module, module_instance_id, margin_width )
{
	document.getElementById( module + module_instance_id ).style.marginTop = margin_width + 'px';
}
function set_margin_bottom( module, module_instance_id, margin_width )
{
	document.getElementById( module + module_instance_id ).style.marginBottom = margin_width + 'px';
}
function commentLimiter(limit)
{
	$('.highlightedArea1 ').hide();
	for(var cnt = 0;cnt < limit; cnt++) {
		$('.highlightedArea1 ').eq(cnt).show();
	}
	$('.highlightedArea1 ').eq(limit - 1).after('<a  class="showComments" href="#" onclick="showComments(' + limit + ')">Show More Comments</a>');
	$('.hideComments').remove();
}
function showComments(limit)
{
	$('.showComments').remove();
	$('.highlightedArea1 ').show();
	$('.highlightedArea1 ').eq(limit - 1).after('<a class="hideComments" href="#" onclick="commentLimiter(' + limit + ')">Hide Comments</a>');
}
function searchBarController(mod_inst_id)
{
	if($('#search' + mod_inst_id).val() == '')
	{
		$('#search' + mod_inst_id).val('search for locations, wildlife or keywords');
	}
	$('#search' + mod_inst_id).click(function() {

		if($('#search' + mod_inst_id).val() == 'search for locations, wildlife or keywords')
		{
			$('#search' + mod_inst_id).val('');
		}
	});
}
function isAvailable()
{
	if($('select option:selected').attr('title') == 'event_full')
	{
		alert("sorry this event is not available");
		return false;
	}
	if($('select option:selected').attr('value') == '')
	{
		alert("please select an option");
		return false;
	}
	return true;
}

function acceptTerms()
{
	if($('.accept:checked').length == 0)
	{
		return false;
	}
	return true;
}
/*================================
	PREVIEW MODE TOGGLE
=================================*/
var preview = false;
$(document).ready(function(){

	$('#previewMode').click(function() {
		preview;
		if(preview == false)
		{
			$('div.cmsEditingBox').hide(100);
			preview = true;
		}
		else
		{
			$('div.cmsEditingBox').show(100);
			preview = false;
		}
	});
});

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 remoteFunctionCall(ajax_error,ajax_success,arguments,methodName,module_instance_id)
{
	var parameterContainer = new Array();
	for(var i = 0; i < arguments.length; i++)
	{
		parameterContainer[i] = arguments[i];
	}
	var url = '/remoteService.php?callback=?';

	$.ajax({
		url: url,
		async: true,
		data:({method:methodName,methodParameters:parameterContainer,moduleInstanceId:module_instance_id}),
		type: 'GET',
		dataType: 'json',
		timeout: 10000,
		success: ajax_success,
		error: ajax_error
	});
	return true;
}
