// JavaScript Document
var base_href = document.getElementsByTagName('base')[0].href;

// DOM Ready
$(document).ready(function(){
	$('.boxgrid.slideright').hover(function(){
		$(".cover", this).stop().animate({left:'325px'},{queue:false,duration:300});
	}, function() {
		$(".cover", this).stop().animate({left:'0px'},{queue:false,duration:300});
	}); 
	
	// Tooltips
	$(".tip[title]").tooltip({ position: 'top left', offset: [4, 140], tip: '#tooltip' });
	
	$('div.owner-avatar').click(function(){
		$('#avatar-file').attr("style","z-index: 100;");
		$('div.edit-avatar').animate({"marginTop":"0"},300);
	});
	
	
	// Owner edit links
	$('.owner-edit').click(function(){
		inlineEdit($(this).attr('id'),$(this).attr('target'),$(this).attr('value'),'start');
	});
	
	$('.edit-input').blur(function(){
		var id = $('#picture-id').val();
		var type = $('#content-type').val();
		var edit = $(this).attr('alt');
		var val = $(this).val();
		var rel = $(this).attr('rel');
		var target = $(this).attr('target');
		$('#'+target+' a').html('<img src="images/loader_wheel.gif" style="vertical-align: middle;" alt="" border="0" />');
		$.getJSON('ajax.php?call=inlineEdit&type='+escape(type)+'&edit='+escape(edit)+'&id='+escape(id)+'&val='+escape(val)+'',
		function(data){
			if(data.status == 'ok'){
				setTimeout(function(){
					if(edit == 'title'){
						changeIndivTitle(data.value);
					}else if(edit == 'desc'){
						changeIndivDesc(data.value);
					}else if(edit == 'tags'){
						changeIndivTags(data.value,data.tags);
					}
					$('#'+target+' a').html('save');
					inlineEdit(rel,target);
				},200);
			}else{
				alert(data.message);
			}
		});
	});
	
	// Safari tweaks
	if(navigator.userAgent.toLowerCase().match(/safari/)){
		$('#query').attr('style','height:25px;');
		$('#searchbox').attr('style','margin-top:-5px;');
	}
});

// Anchor Nav
var url_anchor = document.location.toString();
if(url_anchor.match('#')){
  var anchor_file = url_anchor.split('#')[1];
}else{
	var anchor_file = '';
}

function clearText(thefield) {
  if(thefield.defaultValue==thefield.value) { thefield.value = "" }
}

function replaceText(thefield) {
  if(thefield.value=="") { thefield.value = thefield.defaultValue }
}

function preloadImages() {
  var d=document; if(d.images){ if(!d.p) d.p=new Array();
    var i,j=d.p.length,a=preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.p[j]=new Image; d.p[j++].src=a[i];}}
}

function fsSearch(q,type){
	if(!q) var q = $('input#query').val();
	if(!type) var type = $('#type').val();
	if(type == 'web'){
		document.location = 'http://fullspeed.com/web-search/user.php?target=search&query='+escape(q)+'&action=search';
	}else{
		document.location = '/search/'+type+'/'+q;
	}
}

function logreg(){
	tb_show('Create A Profile / Log In','login.php?blah=1&width=801&height=356','1');
}


function usercomment(){
	tb_show('Add Comments for Tracks','tracksaddcomments.php?blah=1&width=435&height=200','1');
}
function usertypecomment(){
	tb_show('Add Comments for Tracks','trackstypecomments.php?blah=1&width=435&height=200','1');
}

function messages(to){
	if(!global_user_id){
		logreg();
	}else{
		if(to == ''){
			tb_show('Messages','messages.php?blah=1&width=801&height=356','1');
		}else{
			tb_show('Messages','messages.php?blah=1&width=801&height=356&compose='+to+'','1');
		}
	}
}

function test(){
	tb_show('Messages','modules/test.php?blah=1&width=801&height=356','1');
}


$.fn.clearForm = function() {
  return this.each(function() {
 var type = this.type, tag = this.tagName.toLowerCase();
 if (tag == 'form')
   return $(':input',this).clearForm();
 if (type == 'text' || type == 'password' || tag == 'textarea')
   this.value = '';
 else if (type == 'checkbox' || type == 'radio')
   this.checked = false;
 else if (tag == 'select')
   this.selectedIndex = -1;
  });
};


function changeTab(target){
	$(target).each(function(){
		$(this).removeClass('selected');
	});
}

function swapContent(target,show){
	$(target).each(function(){
		$(this).hide();
	});
	$(show).show();
}

function switchTabs(tabelem,conttohide,newcont){
	changeTab(tabelem);
	swapContent(conttohide,newcont);
}

function changeBg(type){
	switch(type){
		case "moto" : $('#wrapper').attr('style','background: #020202 url("images/bg_main_moto.jpg") top center no-repeat;'); break;
		case "auto" : $('#wrapper').attr('style','background: #020202 url("images/bg_main_m3.jpg") top center no-repeat;'); break;
		case "porsche" : $('#wrapper').attr('style','background: #020202 url("images/bg_main_porsche.jpg") top center no-repeat;'); break;
		case "nascar" : $('#wrapper').attr('style','background: #020202 url("images/bg_main_nascar.jpg") top center no-repeat;'); break;
		case "truck" : $('#wrapper').attr('style','background: #020202 url("images/bg_main_truck.jpg") top center no-repeat;'); break;
		case "girls" : $('#wrapper').attr('style','background: #020202 url("images/bg_main_girls.jpg") top center no-repeat;'); break;
		default : $('#wrapper').attr('style','background: #020202 url("images/bg_main.jpg") top center no-repeat;'); break;
	}
}

function youtubeThumb(url,size){
  if(url === null){ return ""; }
  size = (size === null) ? "big" : size;
  var vid;
  var results;
  results = url.match("[\\?&]v=([^&#]*)");
  vid = ( results === null ) ? url : results[1];
  if(size == "small"){
    return "http://img.youtube.com/vi/"+vid+"/2.jpg";
  }else {
    return "http://img.youtube.com/vi/"+vid+"/0.jpg";
  }
}

function featuredPlayer(type,src,target,img){
	if(type == 'video'){
		flashembed(target,"flash/player.swf",{
			flashvars: '&provider=video&file='+src+'&image='+img
		});
	}else if(type == 'youtube'){
		var img = youtubeThumb(src,'big');
		flashembed(target,"flash/player.swf",{
			flashvars: '&provider=youtube&file='+src+'&image='+img
		});
	}
}

function bigPlayer(type,src,target,img){
	if(type == 'video'){
		flashembed(target,"flash/player.swf",{
			flashvars: '&autostart=true&provider=video&file='+src+'&image='+img
		});
	}else if(type == 'youtube'){
		var img = youtubeThumb(src,'big');
		flashembed(target,"flash/player.swf",{
			flashvars: '&autostart=true&provider=youtube&file=http://www.youtube.com/watch?v='+src+'&image='+img
		});
	}
}

function blogPlayer(type,src,target,img){
	if(type == 'video'){
		flashembed(target,"flash/player.swf",{
			flashvars: '&provider=video&file='+src+'&image='+img
		});
	}else if(type == 'youtube'){
		var img = youtubeThumb(src,'big');
		flashembed(target,"flash/player.swf",{
			flashvars: '&provider=youtube&file='+src+'&image='+img
		});
	}
}

function loader(status){
	if(status == 'start'){
		$(".loader").html('<img src="images/loader_wheel.gif" alt="" />');
		$(".hide_onsubmit").hide();
		$(".show_onsubmit").show();
	}else{
		$(".loader").html('');
		$(".hide_onsubmit").show();
		$(".show_onsubmit").hide();
	}
}

function loginUser(post){
	loader('start');
	$.post('ajax.php',post,function(data){
		if(data.status == 'ok'){
			setTimeout("loader('stop')",4000);
			window.location = "user/"+data.username;
		}else{
			loader('stop');
			alert(data.message);
		}
	},"json");
}

function trackcomments(post){
	loader('start');
	$.post('submitcommentTracks.php',post,function(data){
		if(data.status == 'ok'){
			setTimeout("loader('stop')",4000);
			window.location = "user/"+data.username;
		}else{
			loader('stop');
			alert(data.message);
		}
	},"json");
}


function photoLoadMask(stop){
	if(stop){
		$('div.img-stream-body').attr("style","opacity: 1;");
	}else{
		$('div.img-stream-body').attr("style","opacity: 0.5;");
	}
}

function changeIndivTitle(content){
	$('#img-stream-title').html(content);
	$('#img-stream-title-input').val(content);
	$('#edit-photo-title').val(content);
}

function changeIndivDesc(content){
	$('#img-stream-desc').html(content);
	$('#img-stream-desc-input').val(content);
	$('#edit-photo-desc').val(content);
}

function changeIndivTags(html,array){
	if(html){
		$('#img-stream-tags').html(html);
		if(array){
			var tags = '';
			$.each(array,function(i,item){
				tags += item+', ';
			});
			$('#img-stream-tags-input').val(tags);
			$('#edit-photo-tags').val(tags);
		}
	}else{
		$('#img-stream-tags').html('');
		$('#img-stream-tags-input').val('');
		$('#edit-photo-tags').val('');
	}
}

function loadPhoto(id){
	photoLoadMask();
	$.getJSON('ajax.php?call=loadPhoto&id='+escape(id)+'',
	function(data){
		//photoLoadMask(true);
		if(data.status == 'ok'){
			changeIndivTitle(data.title);
			changeIndivDesc(data.desc);
			$('img#img-stream-img').attr('src',data.src);
			$('img#img-stream-img').load(function(){ photoLoadMask(true) });
			$('#comment-body').html(data.comments);
			$('#picture-id').val(data.id);
			$('#like-box').html(data.likes);
			$('#picture-likes').html(data.likes_num);
			$('#picture-comments').html(data.comments_num);
			$('#picture-views').html(data.views_num);
			$('#share-text').val(data.share_link);
			$('#img-stream-album').html('<a href="/user/'+data.owner_username+'/photos/#album_'+data.album_id+'">'+data.album_name+'</a>');
			
			if(data.comments_num){
				$('#comment-num').html(data.comments_num);
			}else{
				$('#comment-num').html('0');
			}
			if(data.tags){
				changeIndivTags(data.tags,data.tags_raw);
			}else{
				changeIndivTags(false,false);
			}
			document.location = url_anchor.split('#')[0]+'#'+data.slug;
		}else{
			alert(data.message);
		}
	});
}

function loadGallery(id){
	$.getJSON('ajax.php?call=loadGallery&id='+escape(id)+'',
	function(data){
		if(data.status == 'ok'){
			var photo_body = '';
			$.each(data.photo,function(i,item){
				photo_body += '<li class="boxgrid slideright pointer" onclick="window.location=\'/user/'+item.owners_username+'/photos/'+item.slug+'\';"><img src="'+item.src+'" height="100" width="100" border="0" /><p><strong>'+item.title+'</strong></p></li>';
			});
			$("#gallery-photos").html(photo_body);
			$("#gallery-title").html(data.title);
			document.location = url_anchor.split('#')[0]+'#'+data.album_slug;
		}else{
			alert(data.message);
		}
	});
}

function registerProfile(post,form_id){
	$('form#'+form_id+' span.loading').html('<img src="images/loading_gray.gif" alt="" />');
	jQuery.post('ajax.php',post,function(data){
		$('form#'+form_id+' span.loading').html('');
		if(data.status == 'ok'){
			$("#register_content").hide();
			$("#login_content").hide();
			$("#users_email").html(data.email);
			$("#registered").show();
		}else{
			alert(data.message);
		}
	},"json");
}

function editProfile(post,form_id){
	$('form#'+form_id+' span.loading').html('<img src="images/loader_wheel_gray.gif" alt="" style="vertical-align: middle; margin-right: 10px;" /> Updating profile...');
	$('form#'+form_id+' span.loading').show();
	jQuery.post('ajax.php',post,function(data){
		$('form#'+form_id+' span.loading').html('');
		if(data.status == 'ok'){
			if(data.welcome){
				$("#edit-profile").slideUp(300);
				alert('Your profile has been successfully finished!');
			}else{
				$('form#'+form_id+' span.loading').html('Profile updated!');
				$('form#'+form_id+' span.loading').delay(1000).fadeOut(300);
				$('#edit-profile').delay(2000).slideToggle(300);
				$('#profile-user-firstname').html(data.request.firstname);
				$('#profile-user-bio').html(data.request.bio);
				$('#profile-user-website').html(data.request.website);
			}
		}else{
			alert(data.message);
		}
	},"json");
}

function makeComment(post,form_id){
	$('form#'+form_id+' span.loading').html('<img src="images/loading_gray.gif" alt="" /> Adding...');
	jQuery.post('ajax.php',post,function(data){
		$('form#'+form_id+' span.loading').html('');
		if(!data.vars.content_target) data.vars.content_target = 'comment-body';
		if(!data.vars.timeout) data.vars.timeout = 200;
		if(data.status == 'ok'){
			$('form#'+form_id).clearForm();
			$('.no-comment').hide();
			if(data.vars.method == 'prepend'){
				//alert('prepend');
				$('#'+data.vars.content_target).prepend(data.comment);
			}else if(data.vars.method == 'prepend-slide'){
				//alert('prepend-slide');
				$('#'+data.vars.content_target).prepend('<div style="display: none" class="newest-comment">'+data.comment+"</div>");
				setTimeout('$(".newest-comment").fadeIn(300)',data.vars.timeout);
			}else if(data.vars.method == 'replace'){
				//alert('replace');
				$('#'+data.vars.content_target).html(data.comment);
//deleteComment();
			}else{
				//alert('else');
				$('#'+data.vars.content_target).append(data.comment);
			}
		}else{
			alert(data.message);
		}
	},"json");
}

function updateTextarea(){
	if(navigator.userAgent.match(/firefox/gi)){
		var bodytext = $('textarea.wysiwyg').htmlarea('toHtmlString');
		if($('textarea.wysiwyg').html(bodytext)){
			return true;
		}else{
			return false;
		}
	}else{
		return true;
	}
}

function updateBlog(){
	var post = $('form#blog_form').serialize();
	var form_id = 'blog_form';
	$('form#'+form_id+' span.loading').show();
	$('form#'+form_id+' span.loading').html('<img src="images/loader_wheel.gif" style="vertical-align: middle;margin-right: 10px; margin-left: 10px;" alt="" /> Saving...');
	$.post('ajax.php',post,function(data){
		$('form#'+form_id+' span.loading').html('');
		if(data.status == 'ok'){
			$('form#'+form_id+' span.loading').html(' Saved!');
			$('form#'+form_id+' span#auto-updated').html(data.updated_time);
			if(data.blog_id) $('#blog_id').val(data.blog_id);
			setTimeout('$(\'form#'+form_id+' span.loading\').fadeOut(300)',1500);
			if(data.published){
				//alert($('#basic-editor').val());
				if(confirm("Your blog has been updated, would you like to view it?")) window.location = data.url;
			}else{
				alert("Your blog has been saved. Please note that your blog will not be published until you click the \'Publish\' button.");
			}
		}else if(data.status == 'publish'){
			$('#save-blog').show();
			$('#publish-blog').remove();
			if(data.blog_id) $('#blog_id').val(data.blog_id);
			if(confirm("Your blog has been updated, would you like to view it?")) window.location = data.url;
		}else{
			alert(data.message);
		}
	},"json");
}

function deleteComment(id,type){
	if(confirm("Are you sure you want to delete this comment?")){
		$.getJSON('ajax.php?call=deleteComment&id='+escape(id)+'&type='+escape(type)+'',
		function(data){
			if(data.status == 'ok'){
				if(type == 'profile'){
					$('#comment_'+id).fadeOut(300);
				}else{
					$('#comment_'+id).slideUp(300);
				}
			}else{
				alert(data.message);
			}
		});
	}else{
		return false;
	}
}

function like(id,type){
	$.getJSON('ajax.php?call=like&id='+escape(id)+'&type='+escape(type)+'',
	function(data){
		if(data.status == 'ok'){
			if(data.action == 'resetLikes'){
				$('ul#like_dislike_'+id).removeClass('like');
				$('ul#like_dislike_'+id).removeClass('dislike');
			}else{
				$('ul#like_dislike_'+id).addClass('like');
				$('ul#like_dislike_'+id).removeClass('dislike');
			}
		}else if(data.status == 'no_user'){
			logreg();
			alert(data.message);
		}else{
			alert(data.message);
		}
	});
}

function dislike(id,type){
	$.getJSON('ajax.php?call=dislike&id='+escape(id)+'&type='+escape(type)+'',
	function(data){
		if(data.status == 'ok'){
			if(data.action == 'resetLikes'){
				$('ul#like_dislike_'+id).removeClass('like');
				$('ul#like_dislike_'+id).removeClass('dislike');
			}else{
				$('ul#like_dislike_'+id).removeClass('like');
				$('ul#like_dislike_'+id).addClass('dislike');
			}
		}else if(data.status == 'no_user'){
			logreg();
			alert(data.message);
		}else{
			alert(data.message);
		}
	});
}

function showAddTag(resetLink){
	if(resetLink){
		setTimeout('$("#tag-add").fadeOut(300)',1000);
	}else{
		$("#tag-add").fadeIn(300);
	}
}

function addTag(){
	var text = $('#tags-text').val();
	if(!text) return false;
	var current = $('#blog_tags').val();
	$('#tags').append('<a class=\'tag-link\'>'+text+'</a>');
	$('#tags_none').hide();
	$('#blog_tags').val(current+text+', ');
	$('#tags-text').val('');
}

function addToBody(){
	var string = $('textarea.wysiwyg').htmlarea('toHtmlString');
	$('#new-body').html(string);
}

function deleteBlog(id){
	if(confirm("Are you sure you want to delete this blog post?\nThis action is undoable!")){
		$.getJSON('ajax.php?call=deleteBlog&id='+escape(id)+'',
		function(data){
			if(data.status == 'ok'){
				window.location='/user/'+global_user_handle;
			}else{
				alert(data.message);
			}
		});
	}
}

function uploadSelectGallery(val){
	if(val == 'new'){
		$('#create_gallery').show();
		$('#create_gallery select').addClass('required');
		$('#create_gallery input').addClass('required');
	}else{
		$('#create_gallery_input').val('');
		$('#create_gallery').hide();
		$('#create_gallery select').removeClass('required');
		$('#create_gallery input').removeClass('required');
	}
}

function sendMessage(post,form_id){
	$('form#'+form_id+' span.loading').html('<img src="images/loading_gray.gif" alt="" /> Sending...');
	jQuery.post('ajax.php',post,function(data){
		$('form#'+form_id+' span.loading').html('');
		if(data.status == 'ok'){
			$('form#'+form_id).clearForm();
			$('form#'+form_id+' span.loading').show();
			$('form#'+form_id+' span.loading').html(' Message Sent!');
			setTimeout("$('form#"+form_id+" span.loading').fadeOut(300)",2000);
		}else if(data.status == 'no_user'){
			logreg();
		}else{
			alert(data.message);
		}
	},"json");
}

function composeMessage(user,id,subject){
	$('#message-content').hide();
	$('#message-welcome').hide();
	$('#compose-message').show();
	if(user){
		$('select#recipient-select').append('<option value="'+id+'" selected="selected">'+user+'</option>');
		$('input#recipient-subject').val('RE: '+subject);
		$('input#to').val(id);
	}
}

function getMessage(id){
	$('#compose-message').hide();
	$('#message-welcome').hide();
	$('#message-content').html('<div class="view-message" style="line-height: 230px; text-align: center; color: #aaa;"><img src="images/loader_wheel.gif" style="vertical-align: middle; margin-right: 10px;" alt="" /> Getting message...</div>');
	$('#message-content').show();
	$.getJSON('ajax.php?call=getMessage&id='+escape(id)+'',
	function(data){
		$('#message-content').html('');
		$('#list_message_'+id).removeClass('unread');
		if(data.status == 'ok'){
			var content = '<div class="view-message"><h3>'+data.subject+'</h3>';
			content += '<p class="gray">From <a href="users/'+data.author+'/">'+data.author+'</a> '+data.time+'  / <a href="#" onclick="deleteMessage(\''+id+'\'); return false;">delete</a> / <a href="#" onclick="composeMessage(\''+data.author+'\',\''+data.author_id+'\',\''+data.subject+'\'); return false;">reply</a></p>';
			content += '<p>'+data.content+'</p>';
			content += '</div>';
			$('#message-content').html(content);
		}else{
			alert(data.message);
		}
	});
}

function getMessageFolder(type){
	$.getJSON('ajax.php?call=getMessageFolder&type='+escape(type)+'',
	function(data){
		if(data.status == 'ok'){
			$('#message_list').html(data.content);
		}else{
			alert(data.message);
		}
	});
}

function deleteMessage(id){
	if(confirm('Are you sure you want to delete this message?')){
		$.getJSON('ajax.php?call=deleteMessage&id='+escape(id)+'',
		function(data){
			if(data.status == 'ok'){
				var msg_num = parseInt($('#messages-num').html());
				msg_num--;
				$('#messages-num').html(msg_num);
				$('#list_message_'+id).fadeOut(300);
			}else{
				alert(data.message);
			}
		});
	}else{
		return false;
	}
}

function checkCountry(id){
	if(id !== 'United States'){
		$("#zip").removeClass('required');
		$("#zip-label").html('enter your <strong>postal code</strong>');
	}else{
		$("#zip").addClass('required');
		$("#zip-label").html('enter your <strong>zip code</strong>');
	}
}

function checkValue(id,target,types){
	if(!types) types = new Array('jpg');
	
	var ext = $('#'+id).val().split('.').pop().toLowerCase();
	if($.inArray(ext,types) == -1){
		if(target){
			$('#'+target).attr("style","background-color: #dc0000;");
		}else{
			$('#'+id).attr("style","background-color: #dc0000;");
		}
		$('#'+id).val('');
		alert("Only "+types.join(", ")+" files are allowed!");
	}else{
		if(target){
			var newval = $('#'+id).val().substring(0,25)+'...';
			$('.selected-item').html(newval);
			$('#'+target).attr("style","background-color: #1aba00;");
		}else{
			$('#'+id).attr("style","background-color: #1aba00;");
		}
	}
}

function checkValue_(id,target){
	val = $('#'+id).val();
	
	if(val !== ''){
		pattern = /jpg/gi;
		if(val.match(pattern)){
			if(target){
				$('#'+target).attr("style","background-color: #1aba00;");
			}else{
				$('#'+id).attr("style","background-color: #1aba00;");
			}
		}else{
			if(target){
				$('#'+target).attr("style","background-color: #dc0000;");
			}else{
				$('#'+id).attr("style","background-color: #dc0000;");
			}
			$('#'+id).val('');
			alert("Only 'jpg' files are allowed!");
		}
	}
}

function inlineEdit(hide,show,formid,state){
	if(state){
		$('#'+show).show();
		$('#'+hide).hide();
		$('#'+formid).focus();
	}else{
		$('#'+hide).show();
		$('#'+show).hide();
	}
}

function editPhoto(post,form_id){
	var id = $('#picture-id').val();
	post += '&id='+id;
	$('form#'+form_id+' span.loading').html('<img src="images/loader_wheel.gif" alt="" />');
	$.post('ajax.php',post,function(data){
		$('form#'+form_id+' span.loading').html('');
		if(data.status == 'ok'){
			$('form#'+form_id+' span.loading').html('<span id="edit-photo-status" class="success-inline">Saved!</span>');
			setTimeout("$('#edit-photo-status').fadeOut(300)",2000);
			setTimeout("$('#edit-photo').slideToggle(300)",2500);
		}else{
			alert(data.message);
		}
	},"json");
}

function deletePhoto(id,title){
	if(!id) id = $('#picture-id').val();
	if(!title) title = $('#img-stream-title-input').val();
	if(confirm("Are you sure you want to delete:\n'"+title+"'?\nThis action is un-doable!")){
		$.getJSON('ajax.php?call=deletePhoto&id='+escape(id)+'',
		function(data){
			if(data.status == 'ok'){
				alert("Your photo has been successfully deleted!");
			}else{
				alert(data.message);
			}
		});
	}else{
		return false;
	}
}

function editVideo(post,form_id){
	$('form#'+form_id+' span.loading').html('<img src="images/loader_wheel.gif" alt="" />');
	$.post('ajax.php',post,function(data){
		$('form#'+form_id+' span.loading').html('');
		if(data.status == 'ok'){
			$('form#'+form_id+' span.loading').html('<span id="edit-video-status" class="success-inline">Saved!</span>');
			setTimeout("$('#edit-video-status').fadeOut(300)",2000);
			setTimeout("$('#edit-video').slideToggle(300)",2500);
		}else{
			alert(data.message);
		}
	},"json");
}

function deleteVideo(id,title){
	if(confirm("Are you sure you want to delete:\n'"+title+"'?\nThis action is un-doable!")){
		$.getJSON('ajax.php?call=deleteVideo&id='+escape(id)+'',
		function(data){
			if(data.status == 'ok'){
				alert("Your video has been successfully deleted!");
			}else{
				alert(data.message);
			}
		});
	}else{
		return false;
	}
}

function addFriend(id,prmpt){
	if(confirm("Send a friend request to this person?")){
		$('#request-friend-'+id).html('Sending...');
		$.getJSON('ajax.php?call=addFriend&id='+escape(id)+'',
		function(data){
			if(data.status == 'ok'){
				if(prmpt) alert("Friend request sent!");
				$('#request-friend-'+id).html('Friend request sent!');
			}else if(data.status == 'no_user'){
				logreg();
				alert('You must login or register before adding friends!');
			}else{
				alert(data.message);
			}
		});
	}else{
		return false;
	}
}

function friendAction(action,id){
	if(action == 'remove' && !confirm('Are you sure you want to delete this person from your friends?')) return false;
	
	$.getJSON('ajax.php?call=friendAction&action='+escape(action)+'&id='+escape(id)+'',
	function(data){
		if(data.status == 'ok'){
			if(action == 'remove'){
				$('#request-friend-'+id).html(data.message);
			}else{
				$('.friend-request-accept').html(data.message);
				$("#list_message_"+data.message_id).fadeOut(300);
			}
		}else{
			alert(data.message);
		}
	});
}

var marker_all = new Array();
var marker_all_comments = new Array();
var marker_all_photos = new Array();

function addMarker(id,lat,lon,html,item_id,comments,photos){
	if(item_id) marker_all[''+item_id+''] = html;
	if(comments) marker_all_comments[''+item_id+''] = comments;
	if(photos) marker_all_photos[''+item_id+''] = photos;
	
	$('#'+id).jmap('AddMarker',{
		'pointLatLng':[lat, lon],
		'pointHTML': html
	},function(marker,element,options){
		GEvent.addListener(marker,"click", function(){
			$('#'+id).jmap("MoveTo", {'mapCenter': [lat,lon], 'mapZoom':14});
			
			var tabs = [];
			tabs.push(new GInfoWindowTab('Track Info', html));
			tabs.push(new GInfoWindowTab('Comments', comments));
			tabs.push(new GInfoWindowTab('Photos', photos));
			marker.openInfoWindowTabsHtml(tabs, {maxWidth: '450'});
			//marker.openInfoWindowHtml(html, {maxWidth: '450'});
		});
	});
}

function findTracks(){
	var type = $('#track-type').val();
	var state = $('#track-state').val();
	
	$.getJSON('ajax.php?call=findTracks&type='+escape(type)+'&state='+escape(state)+'',
	function(data){
		if(data.status == 'ok'){
			var opt = '<option value="">&nbsp;</option>';
			$.each(data.markers,function(i,item){
				var html = '<strong style="font-size: 20px;">'+item.tracks_name+'</strong><br /><span class="gray">'+item.tracks_street+'<br />'+item.tracks_city+', '+item.tracks_state+'</span><br /><em>'+item.tracks_phone+'<br /><a href="'+item.tracks_url+'" target="_blank">'+item.tracks_url+'</a><br /><a href="#" onclick="getWeather(\''+item.tracks_zip+'\',\''+item.tracks_name+'\',\'track-finder\'); return false;" class="weather-link">Show weather forecast</a><span id="weather_box"></span><br /><a href="#" onclick="searchNearTrack(); return false;" class="search-near-here">Search Near This Track</a><br /><br />'+item.desc+'<br /><br /></em>';
				
				addMarker('map',item.tracks_lat,item.tracks_lng,html,item.tracks_id,item.comments,item.photos);
				
				opt += '<option value="'+item.tracks_lat+';'+item.tracks_lng+';'+item.tracks_id+'">'+item.tracks_state+' - '+item.tracks_city+' - '+item.tracks_name+'</option>';
			});
			$('#track-results').html(opt);
			$('#result-list').show();
		}else{
			alert(data.message);
		}
	});
}

function loadTrack(id){
	$('#map').animate({'height': '200px'},300,function(){
		$(this).jmap('CheckResize');
	});
	
	$.getJSON('ajax.php?call=loadTrack&id='+escape(id)+'',
	function(data){
		if(data.status == 'ok'){
			$('#track-details').delay(300).show();
			$.scrollTo('#track-details',300);
			var html = '<p>'+data.desc+'</p><p><strong>Price:</strong><br />'+data.price+'</p><p><strong>'+data.address+'</strong></p><p>'+data.phone+'<br /><a href="'+data.website+'">'+data.website+'</a><br />'+data.hours+'</p>';
			$('#track-desc').html(html);
			$('#track-name').html(data.name);
		}else{
			alert(data.message);
		}
	});
}

function getWeather(zip,name,area){
	if(area == "track-finder"){
		tb_show('Weather','tracks_weather.php?blah=1&zip='+escape(zip)+'&track_name='+escape(name)+'&width=600&height=450','1');
	}
}

function filterSearchState(state){
	window.location = '/search/users/state:'+state;
}

function forgotPassword(){
	if(confirm("Forgot your password? Click 'OK' to have you a new activation code sent to your email address.")){
		var email = prompt('Please enter your email address:');
		if(email){
			$.getJSON('ajax.php?call=sendPassword&email='+escape(email)+'',
			function(data){
				if(data.status == 'ok'){
					alert("Your new activation code has been successfully sent to "+email);
				}else{
					alert(data.message);
				}
			});
		}else{
			return false;
		}
	}else{
		return false;
	}
}

function changePassword(auth){
	if(auth){
		var pass = prompt('Please enter your new password:');
		var pass_conf = prompt('Please enter your new password again:');
		if(pass && pass_conf){
			$.getJSON('ajax.php?call=changePassword&auth='+escape(auth)+'&pass_curr='+escape(pass_curr)+'&pass='+escape(pass)+'&pass_conf='+escape(pass_conf)+'',
			function(data){
				if(data.status == 'ok'){
					alert("Your password has been successfully changed!");
				}else{
					alert(data.message);
				}
			});
		}else{
			return false;
		}
	}else{
		if(confirm("Are you sure you want to change your password?")){
			var pass_curr = prompt('Please enter your current password:');
			var pass = prompt('Please enter your new password:');
			var pass_conf = prompt('Please enter your new password again:');
			if(pass_curr && pass && pass_conf){
				$.getJSON('ajax.php?call=changePassword&pass_curr='+escape(pass_curr)+'&pass='+escape(pass)+'&pass_conf='+escape(pass_conf)+'',
				function(data){
					if(data.status == 'ok'){
						alert("Your password has been successfully changed!");
					}else{
						alert(data.message);
					}
				});
			}else{
				return false;
			}
		}else{
			return false;
		}
	}
}

function addTrackComment(id){
	var content = $('#comment-track').val();
	
	$.getJSON('ajax.php?call=addTrackComment&id='+escape(id)+'&content='+escape(content)+'',
	function(data){
		if(data.status == 'ok'){
			$('#track-comments-'+id).prepend(data.content);
			$('#comment-track').val('');
		}else if(data.status == 'no_user'){
			logreg();
		}else{
			alert(data.message);
		}
	});
}

function searchNearTrack(){
	$('#search-near-track').remove();
	$('div.gels-form').prepend('<div id="search-near-track" style="line-height: 20px;background: #fff; padding: 0 5px; margin-bottom: 10px;">Enter a keyword to search the map: <span class="gray">(eg. pizza)</span></div>');
	$.scrollTo('input.gels-input',300);
	$('input.gels-input').delay(300).focus();
}

function quickLinks(state){
	if(state == 'hide'){
		setTimeout(function(){
			$('#quick-links-list').hide();
		},200);
	}else{
		$('#quick-links-list').show();
	}
}
