/* PGBELIZE.COM GLOBAL JAVASCRIPT */

var show_markers = false;
var editing = false;


/**
 * MAP FUNCTIONS
 */

function pagerBuild(index, DOMelement)
{
	var src = String(DOMelement);
	return '<a href="#"><img src="'+src+'" alt="'+index+'" /></a>';
}

function save_lm(id)
{
    //validate form
    var errors = '';
    
    var name   = $('#lm_name').val();
    var marker = (id > 0) ? '#editmarker_lm' : '#marker_lm';
    
    var offset = $(marker).position();
    var top    = Math.round(offset.top);
    var left   = Math.round(offset.left);
    
    if(name == 'undefined' || name.length < 1) { errors += 'You must enter a landmark name.\n'; }
   	
   	if(errors.length > 0)
   	{
   		alert(errors);
   		return;
   	}
   	
   	$.ajax(
	{
		type:     'POST',
		url:      'actions_ajax.php',
		dataType: 'json',
		data:     
		{ 
			'from_form'	: 1,
			'action'	: 'save_landmark',
			'id'		: id,
			'name' 		: name,
			'x' 		: left,
			'y'			: top	
		},
		success:  function(data)
		{
			if(data.errors.length > 0) { alert(data.errors); }

			//alert('Landmark created successfully!');
			$('#landmarks').html('<div style="text-align:center; padding:70px;"><img src="images/ajax-loader.gif" alt="Loading.." /></div>');
			$('#admin_tabs').tabs('select', 2);
			
			$.post
			(
				'inc.admin_listlocs.php',
				{ 'from_form':1 },
				function(data)
				{
					$('#landmarks').html(data);
				}
			);
			
			$('#addform_lm').hide();
            $('#addform_biz').hide();
            $('#add_choose').show();
			$('#lm_name').val('');
			$('#marker_lm').css({'top':'70px', 'left':'70px'});
			$('#add_select').val('');
		},
		error:    function(obj, type, e)
        {
        	alert(obj);
        	alert(type);
        	alert(e);
        }
	});
}

function save_biz(id)
{
	var marker = (id > 0) ? '#editmarker_biz' : '#marker_biz';
	
	var prefix = (id > 0) ? 'edit' : '';
	
    //validate form
    var errors = '';
    
    var biz_name 		= $('#'+prefix+'biz_name').val();
    var biz_url 		= $('#'+prefix+'biz_url').val();
    var biz_cat 		= $('#'+prefix+'biz_cat').val();
    var biz_address 	= $('#'+prefix+'biz_address').val();
    var biz_phone 		= $('#'+prefix+'biz_tel').val();
    var biz_fax 		= $('#'+prefix+'biz_fax').val();
    var biz_email 		= $('#'+prefix+'biz_email').val();
    var biz_hoursMon 	= $('#'+prefix+'biz_hourMon').val();
    var biz_hoursTues 	= $('#'+prefix+'biz_hourTues').val();
    var biz_hoursWed 	= $('#'+prefix+'biz_hourWed').val();
    var biz_hoursThurs 	= $('#'+prefix+'biz_hourThurs').val();
    var biz_hoursFri 	= $('#'+prefix+'biz_hourFri').val();
    var biz_hoursSat 	= $('#'+prefix+'biz_hourSat').val();
    var biz_hoursSun 	= $('#'+prefix+'biz_hourSun').val();
    var biz_descrip 	= $('#'+prefix+'biz_description').html();
    var biz_services 	= $('#'+prefix+'biz_service').html();
    
    var biz_img1_file = $('#'+prefix+'biz_img1_file').val();
    var biz_img2_file = $('#'+prefix+'biz_img2_file').val();
    var biz_img3_file = $('#'+prefix+'biz_img3_file').val();
    var biz_img4_file = $('#'+prefix+'biz_img4_file').val();
    
    var offset 		  = $(marker).position();
    var top    		  = Math.round(offset.top);
    var left   		  = Math.round(offset.left);
    
    if(biz_name.length < 1) 	{ errors += 'Enter a Business Name.\n'; }
    if(biz_url.length < 1) 		{ errors += 'Enter a Page URL.\n'; }
    if(biz_address.length < 1) 	{ errors += 'Enter an Address.\n'; }

    if(errors.length > 0)
   	{
   		alert(errors);
   		return;
   	}
    
    $.ajax(
	{
		type:     'POST',
		url:      'actions_ajax.php',
		dataType: 'json',
		data:     
		{ 
			'from_form'		: 1,
			'action'		: 'save_biz',
			'id'			: id,
			'biz_name' 		: biz_name,
			'biz_url' 		: biz_url,
			'biz_cat' 		: biz_cat,
			'biz_address' 	: biz_address,
			'biz_phone' 	: biz_phone,
			'biz_fax' 		: biz_fax,
			'biz_email' 	: biz_email,
			'biz_hoursMon' 	: biz_hoursMon,
			'biz_hoursTues' : biz_hoursTues,
			'biz_hoursWed' 	: biz_hoursWed,
			'biz_hoursThurs': biz_hoursThurs,
			'biz_hoursFri' 	: biz_hoursFri,
			'biz_hoursSat' 	: biz_hoursSat,
			'biz_hoursSun' 	: biz_hoursSun,
			'biz_descrip' 	: biz_descrip,
			'biz_services' 	: biz_services,
			'biz_img1_file' : biz_img1_file,
			'biz_img2_file' : biz_img2_file,
			'biz_img3_file' : biz_img3_file,
			'biz_img4_file' : biz_img4_file,
			'biz_mapX'		: left,
			'biz_mapY'		: top
		},
		success:  function(data)
		{
			if(data.errors.length > 0) { alert(data.errors); }
			
			if(data.error.length > 0)
			{
				alert(data.error);
			}
			else
			{
				$('#biz').html('<div style="text-align:center; padding:70px;"><img src="images/ajax-loader.gif" alt="Loading.." /></div>');
				$('#admin_tabs').tabs('select', 1);
				
				$.post
				(
					'inc.admin_listbiz.php',
					{ 'from_form':1 },
					function(data)
					{
						$('#biz').html(data);
					}
				);
				
				$('#addform_lm').hide();
	            $('#addform_biz').hide();
	            $('#add_choose').show();
	            
				$('#addform_biz input[type="hidden"],#addform_biz input[type="text"]').each(function()
				{
					$(this).val('');
				});
				
				$('#addform_biz textarea').each(function()
				{
					$(this).html('');
				});
				
				$('#biz_img1_link').html('');
				$('#biz_img2_link').html('');
				$('#biz_img3_link').html('');
				$('#biz_img4_link').html('');
				
				$('#marker_biz').css({'top':'70px', 'left':'70px'});
				$('#add_select').val('');
			}
			
		},
		error:    function(obj, type, e)
        {
        	alert(obj);
        	alert(type);
        	alert(e);
        }
	});
}

function delete_rec(id, type)
{
	var name = (type == 'biz') ? 'Business' : 'Landmark';

	var choice = confirm('Are you sure you want to delete this '+name+'?');
	
	if(choice)
	{
		$('#'+type+'_'+id).remove();
	
		$.ajax(
		{
			type:     'POST',
			url:      'actions_ajax.php',
			dataType: 'json',
			data:     
			{ 
				'from_form'	: 1,
				'action'	: 'delete_rec',
				'id'		: id,
				'type'		: type
			},
			success:  function(data)
			{
				if(data.errors.length > 0) { alert(data.errors); }
			},
			error:    function(obj, type, e)
	        {
	        	alert(obj);
	        	alert(type);
	        	alert(e);
	        }
		});
	}
}

function toggle_status(id, type)
{
	$.ajax(
	{
		type:     'POST',
		url:      'actions_ajax.php',
		dataType: 'json',
		data:     
		{ 
			'from_form'	: 1,
			'action'	: 'toggle_status',
			'id'		: id,
			'type'		: type
		},
		success:  function(data)
		{
			if(data.errors.length > 0) { alert(data.errors); }
			
			var text;
			
			if(data.status == 1)
			{
				text = (data.type == 'biz') ? 'Business is active. Click to make inactive' : 'Location is active. Click to make inactive';
			
				$('#tog'+data.type+'_'+data.id).attr('title', '');
				$('#tog'+data.type+'_'+data.id).attr('src', '../images/tick.png');
			}
			else
			{
				text = (data.type == 'biz') ? 'Business is inactive. Click to make active' : 'Location is inactive. Click to make active';
			
				$('#tog'+data.type+'_'+data.id).attr('title', '');
				$('#tog'+data.type+'_'+data.id).attr('src', '../images/disabled.png');
			}
			
		},
		error:    function(obj, type, e)
        {
        	alert(obj);
        	alert(type);
        	alert(e);
        }
	});
}

function open_edit(id, type)
{

	var tab = (type == 'biz') ? 'biz' : 'landmarks';
	
	$('#'+tab).html('<div style="text-align:center; padding:70px;"><img src="images/ajax-loader.gif" alt="Loading.." /></div>');

	$.ajax(
	{
		type:     'POST',
		url:      'actions_ajax.php',
		dataType: 'json',
		data:     
		{ 
			'from_form'	: 1,
			'action'	: 'edit_form',
			'id'		: id,
			'type'		: type
		},
		success:  function(data)
		{
			if(data.errors.length > 0) { alert(data.errors); }
			
			$('#'+data.tab).html(data.html);
			
		},
		error:    function(obj, type, e)
        {
        	alert(obj);
        	alert(type);
        	alert(e);
        }
	});
}

function cancel_edit(type)
{
	var tab 	= (type == 'biz') ? 'biz' : 'landmarks';
	var file 	= (type == 'biz') ? 'inc.admin_listbiz.php' : 'inc.admin_listlocs.php';
	
	editing = false;
	
	$('#'+tab).html('<div style="text-align:center; padding:70px;"><img src="images/ajax-loader.gif" alt="Loading.." /></div>');
	
	$.post
	(
		file,
		{ 'from_form':1 },
		function(data)
		{
			$('#'+tab).html(data);
		}
	);
}

function img_upload(field_id)
{
	var prefix = (editing == true) ? 'edit' : '';

	if($('#'+prefix+field_id).val().length < 1 || $('#'+prefix+field_id).val() == 'undefined')
	{
		alert('Please select an file to upload!');
		return;
	}
	
	$('#loading_overlay, #loading').show();

	$.ajaxFileUpload
	(
		{
			url:'../file_upload.php?field_id='+prefix+field_id,
			secureuri: false,
			fileElementId: prefix+field_id,
			dataType: 'json',
			success: function (data, status)
			{
				if(data.success == 1)
				{
					alert('File uploaded successfully!');
					$('#'+data.field+'_file').val(data.file);
					$('#'+data.field+'_link').html('<a href="/bizfiles/'+data.file+'" target="_blank" class="fancybox">'+data.file+'</a>');
					$('#'+data.field+'_link a').fancybox(
					{
						cyclic: true,
						centerOnScroll: true,
						transitionIn: 'elastic',
						transitionOut: 'elastic',
						overlayOpacity: 0.6,
						overlayColor: '#000',
						titlePosition: 'over',
						titleShow: false
					});
				}
				else
				{
					alert(data.error);
				}
				
				$('#loading_overlay, #loading').hide();
				
			},
			error: function (data, status, e)
			{
				alert(e);
				
				$('#loading_overlay, #loading').hide();
			}
		}
	);

	return false;

}

function img_upload2(field_id)
{

	if($('#'+field_id).val().length < 1 || $('#'+field_id).val() == 'undefined')
	{
		alert('Please select an file to upload!');
		return;
	}
	
	$('#loading_overlay, #loading').show();

	$.ajaxFileUpload
	(
		{
			url:'../file_upload.php?field_id='+field_id,
			secureuri: false,
			fileElementId: field_id,
			dataType: 'json',
			success: function (data, status)
			{
				if(data.success == 1)
				{
					alert('File uploaded successfully!');
					$('#'+data.field+'_file').val(data.file);
					$('#'+data.field+'_link').html('<a href="/bizfiles/'+data.file+'" target="_blank" class="fancybox">'+data.file+'</a>');
					$('#'+data.field+'_link a').fancybox(
					{
						cyclic: true,
						centerOnScroll: true,
						transitionIn: 'elastic',
						transitionOut: 'elastic',
						overlayOpacity: 0.6,
						overlayColor: '#000',
						titlePosition: 'over',
						titleShow: false
					});
				}
				else
				{
					alert(data.error);
				}
				
				$('#loading_overlay, #loading').hide();
				
			},
			error: function (data, status, e)
			{
				alert(e);
				
				$('#loading_overlay, #loading').hide();
			}
		}
	);

	return false;

}


function toggle_labels()
{
    show_markers = (show_markers) ? false : true;

    if(show_markers)
    {
        $('.map_label').fadeIn('fast');
    }
    else
    {
        $('.map_label').fadeOut('fast');
    }
}


function display_point(xpos, ypos, name, link, id, type)
{
    if(type == 'biz')
    {
        $('#map_wrap').append('<a class="map_point" id="point_'+id+'" href="'+link+'"><div class="map_marker" style="background-image:url(../images/building.png)"></div><div class="map_label">'+name+'</div></a>');
    
    	$('#point_'+id).css({'top':ypos+'px', 'left':xpos+'px'});

    	$('#point_'+id).show();
    	
    	$('#point_'+id).supersleight({shim: "images/transparent.gif"});
    }
    else
    {
        $('#map_wrap').append('<a class="map_point" id="lm_'+id+'" href="javascript:void(0);"><div class="map_marker" style="background-image:url(../images/star.png)"></div><div class="map_label">'+name+'</div></a>');
    
    	$('#lm_'+id).css({'top':ypos+'px', 'left':xpos+'px'});

   		 $('#lm_'+id).show();
   		 
   		 $('#lm_'+id).supersleight({shim: "images/transparent.gif"});
    }
    
    $('.map_point').supersleight({shim: "images/transparent.gif"});
}

function display_minipoint(xpos, ypos, name)
{
	$('.biz_smallmap').append('<a class="map_point" id="point_biz" href="javascript:void(0);"><div class="map_marker" style="background-image:url(../images/building.png)"></div><div class="map_label">'+name+'</div></a>');
    
	$('#point_biz').css({'top':ypos+'px', 'left':xpos+'px'});

	$('#point_biz').show();
	
	$('#point_biz').supersleight({shim: "images/transparent.gif"});
}

function open_map()
{
	var mapwin = window.open('interactive_map.php', 'full_map', 'width=632, height=889, menubar=no, toolbar=no, status=no, scrollbars=yes');
}

function show_blogform(id)
{
	$('#blog_list').hide();

	if(id > 0)
	{	
		$('#blog_loading').show();
	
		$.ajax(
		{
			type:     'POST',
			url:      'actions_ajax.php',
			dataType: 'json',
			data:     
			{ 
				'from_form'	: 1,
				'action'	: 'get_blog',
				'id'		: id
			},
			success:  function(data)
			{
				if(data.errors.length > 0) { alert(data.errors); }
				
				$('#blog_title').val(data.title);
				$('#blog_author').val(data.author);
				$('#blog_body').html(data.body);
				$('#blog_id').val(data.id);
				
				$('#blog_loading').hide();
				$('#blog_form').show();
				
			},
			error:    function(obj, type, e)
	        {
	        	alert(obj);
	        	alert(type);
	        	alert(e);
	        }
		});
	
	}
	else
	{
		//clear form
		$('#blog_title').val('');
		$('#blog_author').val('');
		$('#blog_body').html('');
		$('#blog_id').val(0);

		$('#blog_form').show();
	}
}

function hide_blogform()
{
	$('#blog_list').show();
	$('#blog_form').hide();
}


function delete_blog(id)
{
	var choice = confirm('Are you sure you want to delete this blog entry?');
	
	if(choice)
	{
		$('#blog_'+id).remove();
		
		if($('.admin_blogrow').length < 1)
		{
			$('#entries_wrap').html('<div id="no_entries" style="padding:20px; text-align:center; font-style:italic;">No Blog Entries</div>');
		}
		
		$.ajax(
		{
			type:     'POST',
			url:      'actions_ajax.php',
			dataType: 'json',
			data:     
			{ 
				'from_form'	: 1,
				'action'	: 'delete_blog',
				'id'		: id
			},
			success:  function(data)
			{
				if(data.errors.length > 0) { alert(data.errors); }
				
			},
			error:    function(obj, type, e)
	        {
	        	alert(obj);
	        	alert(type);
	        	alert(e);
	        }
		});
	}
}

function save_blog()
{
	var id 		= $('#blog_id').val();
	var title 	= $('#blog_title').val();
	var author 	= $('#blog_author').val();
	var body 	= $('#blog_body').html();
	
	if(title.length < 1 || author.length < 1 || body.length < 1)
	{
		alert('Please fill in all the required fields');
		return;
	}
	
	$('#blog_loading').show();
	$('#blog_form').hide();

	$.ajax(
		{
			type:     'POST',
			url:      'actions_ajax.php',
			dataType: 'json',
			data:     
			{ 
				'from_form'	: 1,
				'action'	: 'save_blog',
				'id'		: id,
				'title'		: title,
				'author'	: author,
				'body'		: body
			},
			success:  function(data)
			{
				if(data.errors.length > 0) { alert(data.errors); }
				
				$('#blog_loading').hide();
				$('#blog_list').show();
				
				$('#entries_wrap').html(data.html);
				
				$('.ui_button').button();
			},
			error:    function(obj, type, e)
	        {
	        	alert(obj);
	        	alert(type);
	        	alert(e);
	        }
		});
	
}

function save_links()
{
    var content = $('#links_content').html();
    
    $.ajax(
    {
        type:     'POST',
        url:      'actions_ajax.php',
        dataType: 'json',
        data:    
        {
            'from_form' : 1,
			'action'	: 'update_links',
			'content'	: content
        },
        success:  function(data)
        {
            alert('Links saved!');
        },
        error:    function(obj, type, e)
        {
            alert(obj+'\n\n'+type+'\n\n'+e);
        }
    });
}


$(document).ready(function()
{
    //png fix for IE
   $('body').supersleight({shim: "images/transparent.gif"});

   //ui buttons bind
   $('.ui_button').button();

   //admin page tabs
   $('#admin_tabs').tabs();
   
   //fancybox listeners
	$('a.fancybox').fancybox({
		cyclic: true,
		centerOnScroll: true,
		transitionIn: 'elastic',
		transitionOut: 'elastic',
		overlayOpacity: 0.6,
		overlayColor: '#000',
		titlePosition: 'over',
		titleShow: false
	});

});
