//framework.js
var logged_in;
var ajax_in_use = 0;
var n_shown = false;

function clearText(field){
    if (field.defaultValue == field.value) field.value = '';
    else if (field.value == '') field.value = field.defaultValue;
}

// called by 'add site' link

function open_private_url(this_url)
{
	if(logged_in == 1) {					// user is not logged in, so display login box
		window.location = this_url;
	
	} else {								// user is logged in, so navigate to required page
		show_login(this_url);
	}
}

function remove_uploaded_image(edit_key, image_key, image_filename)
{
	$.get(
		'_include/upload_get.php',
		{
			edit_key: edit_key ,
			image_key: image_key ,
			image: image_filename
		}, 
		function(ajax_data){
			if(ajax_data.response == 'success') {

				$('#photos_title').html("<b>Photos</b> (" + ajax_data.n_images + ")");
				$('#tn'+image_filename).hide();
				
			} else {
				
			}
		},
		'json'
	);

}

function respond_friend_request(u, edit_key, r)
{
	if(ajax_in_use == 1) return;
	ajax_in_use = 1;
	
	var response_type;
	if(r == 1) response_type = "accepted";
	if(r == 0) response_type = "declined";
	
	$.get(
		'_include/userprofile_rw_jq.php',
		{
			edit_key: edit_key ,
			op: 'rfr' ,
			u: u ,
			r: r 
		}, 

		function(data){
			if(data.response == 'success') {
				
				// show that this has been done
				
				var not_id = data.not_id;
				var is_last = data.is_last;								// need to know if this is the last notification for this user
				hide_notification(not_id, "Friend request "+response_type, is_last);
				
			} else {
				alert("Something went wrong");
			}
			ajax_in_use = 0;
		},
		'json'
	);
	//
}

// hides specificied notification

function hide_notification(not_id, str_message, is_last)
{
	if(str_message != null) {		// show message first

		$('#not_' + not_id).html("<p class='tmtextg' style='padding-top:4px;'>" + str_message + "</p>");

		setTimeout("hide_notification_animate(" + not_id + ", " + is_last + ")", 800);					// now need to pause before hiding the text
		
	} else {
	
		hide_notification_animate(not_id, is_last);
	}
}

function hide_notification_animate(not_id, is_last)
{
	var n_height = $('#not_' + not_id).height();
	$('#not_' + not_id).height(0);
	
	if(is_last == 1) {
		hide_notifications(true);
		
	} else {
		$('#notifications_outer').animate(
			{ 
				height: '-='+n_height
			},
			100, 'swing', function() { $('#not_' + not_id).hide(); }
		);
	}
}

// removes specified notification from db

function remove_notification(not_id, edit_key, u, str_message)
{
	if(ajax_in_use == 1) return;
	ajax_in_use = 1;
	$.get(
		'_include/userprofile_rw_jq.php',
		{
			edit_key: edit_key ,
			op: 'rn' ,
			u: u ,
			n: not_id 
		}, 
		function(data){
			if(data.response == 'success') {
				var is_last = data.is_last;	// need to know if this is the last notification for this user
				
				hide_notification(not_id, str_message, is_last);		// now hide the notification
				
			} else {
				alert("Something went wrong");
			}
			ajax_in_use = 0;
		},
		'json'
	);
	//
}

// show user notifications

function show_notifications()
{
	if(n_shown == true) return;
	n_shown = true;
	
	$('#a_show_n').attr("href", "javascript:hide_notifications(false);");
	
	var n_height = $('#notifications_outer').height();
	$('#notifications_outer').height(0);
	$('#notifications_outer').show();
	$('#notifications_outer').animate(
        { 
			height: '+='+n_height
        },
		100, 'swing', function() { $('#a_show_n').text("Hide notifications"); }
    );		  
}

// hide user notifications

function hide_notifications(remove_link)
{
	if(n_shown == false) return;
	n_shown = false;
	
	$('#a_show_n').attr("href", "javascript:show_notifications();")
	var n_height = $('#notifications_outer').height();
	$('#notifications_outer').animate(
		{ 
			height: '-='+n_height
		},
		100, 'swing', function() {
			$('#notifications_outer').hide();
			$('#notifications_outer').height(n_height);
			$('#a_show_n').text("Show notifications");
			if(remove_link == true) {
				// user has removed all their notifications, so we need to hide the link
			
				$('#a_show_n').hide();
				$('#e_show_n').hide();
			}
		}
    );
}

function get_site_description(site_type)
{
	var site_description_str = "";
	switch(site_type)
	{
		case 0: site_description_str = "Suitable for camper vans only";
			break;
		case 1: site_description_str = "Suitable for tents only";
			break;
		case 2: site_description_str = "Suitable for camper vans and tents";
			break;
		case 3: site_description_str = "Suitable for bivvy bags only";
			break;
		case 4: site_description_str = "Paid campsite accepting vans only";
			break;
		case 5: site_description_str = "Paid campsite accepting tents only";
			break;
		case 6: site_description_str = "Paid campsite accepting vans and tents";
			break;
	}
	return site_description_str;
}

function get_site_icon(site_type)
{
	var site_type_str = "";
	switch(site_type)
	{
		case 0: site_type_str = "camper";
			break;
		case 1: site_type_str = "tent";
			break;
		case 2: site_type_str = "camper_tent";
			break;
		case 3: site_type_str = "bivvy";
			break;
		case 4: site_type_str = "camper_paid";
			break;
		case 5: site_type_str = "tent_paid";
			break;
		case 6: site_type_str = "camper_tent_paid";
			break;
	}
	var img_filename = "graphics/icons/" + site_type_str + ".png";
	return img_filename;
}

// create map icon based on site type

function create_map_icon(site_type)
{
	var custIcon = new GIcon(G_DEFAULT_ICON);
	custIcon.shadowSize = new GSize(0, 0);
	custIcon.iconAnchor = new GPoint(0, 0);

	switch(site_type)
	{
		case 0:
			custIcon.iconSize = new GSize(32, 19);
			custIcon.imageMap = [0,0 , 32,0 , 32,19 , 0,19];
			var this_icon = new GIcon(custIcon, "graphics/icons/camper.png");
			break;
		
		case 1:
			custIcon.iconSize = new GSize(25, 14);
			custIcon.imageMap = [0,0 , 25,0 , 25,14 , 0,14];
			var this_icon = new GIcon(custIcon, "graphics/icons/tent.png");
			break;
		
		case 2:
			custIcon.iconSize = new GSize(40, 24);
			custIcon.imageMap = [0,0 , 40,0 , 40,24 , 0,24];
			var this_icon = new GIcon(custIcon, "graphics/icons/camper_tent.png");
			break;
		
		case 3:
			custIcon.iconSize = new GSize(25, 11);
			custIcon.imageMap = [0,0 , 25,0 , 25,11 , 0,11];
			var this_icon = new GIcon(custIcon, "graphics/icons/bivvy.png");
			break;
		
		case 4:
			custIcon.iconSize = new GSize(32, 22);
			custIcon.imageMap = [0,0 , 32,0 , 32,22 , 0,22];
			var this_icon = new GIcon(custIcon, "graphics/icons/camper_paid.png");
			break;
			
		case 5:
			custIcon.iconSize = new GSize(26, 19);
			custIcon.imageMap = [0,0 , 26,0 , 26,19 , 0,19];
			var this_icon = new GIcon(custIcon, "graphics/icons/tent_paid.png");
			break;
			
		case 6:
			custIcon.iconSize = new GSize(40, 24);
			custIcon.imageMap = [0,0 , 40,0 , 40,24 , 0,24];
			var this_icon = new GIcon(custIcon, "graphics/icons/camper_tent_paid.png");
			break;
		case 99:
			custIcon.iconSize = new GSize(28, 21);
			custIcon.imageMap = [0,0 , 28,0 , 28,21 , 0,21];
			var this_icon = new GIcon(custIcon, "graphics/icons/pov.png");
			break;
	}
	return this_icon;
}

//this will eventually include all common components

$(document).ready(function() {
    
	// load any images that we might need
	
	//preload_popup_images();
	
	set_screen_resolution();
	set_page_disable_dimensions();
		
	// show login if required
	
	if(window.location == "http://www.camperstar.com/login" && logged_in == 0) {
		show_login();
	}
	if(window.location == "http://www.camperstar.com/signup" && logged_in == 0) {
		show_signup();
	}
});

// allows viewing on smaller resolution screens

function set_screen_resolution()
{
	// get screen res
	
	var screen_width = screen.width;
	
	if(screen_width <= 1200) {
	
		$("#map_main").css("margin-left", "0px");
		$("#map_main").css("margin-right", "0px");
		$('#map_left').hide();
		$('#map_right').hide();
		$('#map_inner').width(935);
		
	} else {
	
		$("#map_main").css("margin-left", "130px");
		$("#map_main").css("margin-right", "130px");
		$('#map_left').show();
		$('#map_right').show();
		$('#map_inner').width(1192);
	
	}
}

// ensures page disable div covers entire height of page

function set_page_disable_dimensions()
{
	var height = $(document).height();
	//height = $('#page_wrapper').height();
	
	$('#page_disable').height(height);
	//$('#popup_container').height(height);
	
	if($('#page_disable').css('display') == "block") {
	
		show_intro();
	}
	
	var screen_width = screen.width;
	//$('#page_disable').width(screen_width-17);
	//$('#popup_container').width(screen_width-17);
	//$('#popup_center').width(screen_width / 2);
}
function getDocHeight() {
    //utility function to find dimensions of page
    var D = document;
    return Math.max(
        Math.max(D.body.scrollHeight, D.documentElement.scrollHeight),
        Math.max(D.body.offsetHeight, D.documentElement.offsetHeight),
        Math.max(D.body.clientHeight, D.documentElement.clientHeight)
    );
}



// preload images for popup windows

function preload_popup_images()
{
	var str_images = 'login.png,signup.png,login_login.jpg,login_close.jpg,signup_cancel.jpg,signup_next.jpg';
	var arr_images = str_images.split(',');
	var img_obj = new Image();
	
	for(i = 0; i < arr_images.length; i++)
	{
		var img_filename = 'graphics/common/' + arr_images[i];
		img_obj.src = img_filename;
	}
}

function get_current_subdomain()
{
	var full = window.location.host
	var parts = full.split('.')
	var sub = parts[0]
	return sub;
}
