$(function() {
/*	$("a[rel*=lightbox]").lightBox();
	$("a[rel*=facebox]").each(function(){
		var href = $(this).attr("href");
		if(!href.match(/^#/))
			this.href += href.match(/\?/) ? "&popup=1" : "?popup=1";
		$(this).facebox();
	});
*/	$(".pngfix").ifixpng();
	$(".pngfix_str").ifixpng();
	$("a[rel*=hover] img")
		.each(function() { new Image().src = this.src.replace(/(\.[a-z]+)$/, "_s$1"); })
		.mouseover(function() { this.src = this.src.replace(/(\.[a-z]+)$/, "_s$1"); })
		.mouseout(function() { this.src = this.src.replace(/_s(\.[a-z]+)$/, "$1"); });
	$(".tabcontrol > ul").tabs();
	$("input.datepicker").datepicker({
		dateFormat: "mm/dd/yy",
		showOn: "both",
		buttonImage: 'images/design_js/calendar-btn.png', 
		buttonImageOnly: true, 
		yearRange: "-90:+10" 
	}).attr("readonly", "readonly");
	$(".ui-datepicker-trigger").css({
		verticalAlign: 'middle',
		position: 'relative',
		left: '-20px'
	});
	$('form[name=fullsearch]').submit(function(){
		var q = $('input[name=query]').val();
		if(q == '')
			return false;
		if($(':radio[name=search]:checked').val() == 'web') {
			this.action = 'search/user.php';
		} else {
			window.location = $(this).attr('action') + '?q=' + escape(q);
			return false;
		}
		return true;
	});
});

$.fn.image_refresh = function() {
	return this.each(function(){
		var url = this.src.replace(/\?.*$/, '');
		this.src = url + '?' + Math.round(Math.random()*100000);
    });
};

function dlg_ajax(html, dlg, onsubmit) {
	if(html == '') {
		dlg.dialog('close');
		return;
	}
	
	dlg.html(html);
	$('.ui-dialog-close', dlg).click(function() {
		dlg.dialog('close');
		return false;
	});
	$('input.datepicker', dlg).datepicker({
		dateFormat: "mm/dd/yy",
		showOn: "both",
		buttonImage: 'images/design_js/calendar-btn.png', 
		buttonImageOnly: true, 
		yearRange: "-90:0",
		changeYear: true,
		changeMonth: true
	}).attr("readonly", "readonly");
	$('#ui-datepicker-div').addClass('promoteZ');
	$('form', dlg).ajaxForm({
		success: function(html) {
			dlg_ajax(html, dlg);
			if($.isFunction(onsubmit))
				onsubmit(html);
		}
	});
	$('.hspacer', dlg).height(400);
	var h = 392 + $(dlg).height() - $(dlg)[0].scrollHeight;
	$('.hspacer', dlg).height(h > 0 ? h : 10);
}

function check_username(username, element) {
	if(!username)
		return false;
	$.getJSON('dialog/register.php', 'action=usercheck&users_username='+escape(username), function(data) {
		if(data == 1)
			alert('Congratulations, this display name is available!');
		else
			alert('This display name is unavailable.');
	});
	return false;
}

function a_register() {
	window.scrollTo(0,0);
	var dlg = $('<div style="width:auto;height:100%;text-align:center;"><div style="margin:50px;"><img src="images/design_js/lightbox-ico-loading.gif" border="0"></div></div>');
	dlg.dialog({
		title: 'Registration',
		width: 700,
		height: 600 + (jQuery.support.boxModel ? 0 : 160),
		resizable: false,
		close: function(e, ui) {
			$(this).dialog('destroy');
			$(this).remove();
		},
		closeOnEscape: false
	}).css('padding', '0px');
	$.get('dialog/register.php', '', function(html) {
		dlg_ajax(html, dlg, function(res){
			if(res == '')
				window.location = 'profile.php?pro=main_menu&first_login=1';
		});
	})
	return false;
}

function a_login(href) {
	window.scrollTo(0,0);
	var dlg = $('<div style="width:auto;height:100%;text-align:center;"><div style="margin:50px;"><img src="images/design_js/lightbox-ico-loading.gif" border="0"></div></div>');
	dlg.dialog({
		title: 'Login',
		width: 700,
		height: 500 + (jQuery.support.boxModel ? 0 : 160),
		resizable: false,
		close: function(e, ui) {
			$(this).dialog('destroy');
			$(this).remove();
		},
		closeOnEscape: false
	}).css('padding', '0px');
	$.get('dialog/login.php', '', function(html) {
		dlg_ajax(html, dlg, function(res){
			if(res == '')
				window.location.reload();
		});
	})
	return false;
}

function a_sendmessage(user_id) {
	window.scrollTo(0,0);
	var dlg = $('<div style="width:auto;height:100%;text-align:center;"><div style="margin:50px;"><img src="images/design_js/lightbox-ico-loading.gif" border="0"></div></div>');
	dlg.dialog({
		title: 'Compose a Message',
		width: 700,
		height: 500 + (jQuery.support.boxModel ? 0 : 160),
		resizable: false,
		close: function(e, ui) {
			$(this).dialog('destroy');
			$(this).remove();
		},
		closeOnEscape: false
	}).css('padding', '0px');
	$.get('dialog/send_message.php', 'uid='+user_id, function(html) {
		dlg_ajax(html, dlg);
	})
	return false;
}

function a_sendinvite(user_id) {
	window.scrollTo(0,0);
	var dlg = $('<div style="width:auto;height:100%;text-align:center;"><div style="margin:50px;"><img src="images/design_js/lightbox-ico-loading.gif" border="0"></div></div>');
	dlg.dialog({
		title: 'Add Friend',
		width: 700,
		height: 500 + (jQuery.support.boxModel ? 0 : 160),
		resizable: false,
		close: function(e, ui) {
			$(this).dialog('destroy');
			$(this).remove();
		},
		closeOnEscape: false
	}).css('padding', '0px');
	$.get('dialog/send_invite.php', 'uid='+user_id, function(html) {
		dlg_ajax(html, dlg);
	})
	return false;
}

function a_page(page,width,height) {
	if(!width) width = 700;
	if(!height) height = 500;
	window.scrollTo(0,0);
	var dlg = $('<div style="width:auto;height:100%;text-align:center;"><div style="margin:50px;"><img src="images/design_js/lightbox-ico-loading.gif" border="0"></div></div>');
	dlg.dialog({
		title: 'FullSpeed',
		width: width,
		height: height + (jQuery.support.boxModel ? 0 : 160),
		resizable: false,
		close: function(e, ui) {
			$(this).dialog('destroy');
			$(this).remove();
		},
		closeOnEscape: false
	}).css('padding', '0px');
	$.get('dialog/page.php', 'page='+escape(page), function(data) {
		dlg.dialog('option', 'title', data.title);
		dlg_ajax(data.body, dlg);
	}, 'json')
	return false;
}

function a_reportabuse(url) {
	window.scrollTo(0,0);
	var dlg = $('<div style="width:auto;height:100%;text-align:center;"><div style="margin:50px;"><img src="images/design_js/lightbox-ico-loading.gif" border="0"></div></div>');
	dlg.dialog({
		title: 'Report Abuse',
		width: 700,
		height: 500 + (jQuery.support.boxModel ? 0 : 160),
		resizable: false,
		close: function(e, ui) {
			$(this).dialog('destroy');
			$(this).remove();
		},
		closeOnEscape: false
	}).css('padding', '0px');
	$.get('dialog/abuse.php', 'url='+escape(url), function(html) {
		dlg_ajax(html, dlg);
	})
	return false;
}

function a_forgot_password() {
	window.scrollTo(0,0);
	var dlg = $('<div style="width:auto;height:100%;text-align:center;"><div style="margin:50px;"><img src="images/design_js/lightbox-ico-loading.gif" border="0"></div></div>');
	dlg.dialog({
		title: 'Forgot Your Password?',
		width: 700,
		height: 500 + (jQuery.support.boxModel ? 0 : 160),
		resizable: false,
		close: function(e, ui) {
			$(this).dialog('destroy');
			$(this).remove();
		},
		closeOnEscape: false
	}).css('padding', '0px');
	$.get('dialog/forgot_password.php', '', function(html) {
		dlg_ajax(html, dlg);
	})
	return false;
}

function mail_count_update(count) {
	if(count) {
		$('.mail_unseen_count').html(count);
		return false;
	}
	$.get('inbox.php', 'action=unseen_count', function(html){
		$('.mail_unseen_count').html(html);
	});
	return false;
}

function share_facebook() {
	var u = location.href;
	var t = document.title;
	window.open('http://www.facebook.com/sharer.php?u='+encodeURIComponent(u)+'&t='+encodeURIComponent(t),'sharer','toolbar=0,status=0,width=626,height=436');
	return false;
}

function share_myspace() {
	var u = location.href;
	var t = document.title;
	
	window.open('http://www.myspace.com/index.cfm?fuseaction=postto&u='+encodeURIComponent(u)+'&t='+encodeURIComponent(t),'sharer','toolbar=0,status=0,scrollbars=auto,width=800,height=600');
	return false;
}

function share_twitter() {
	var u = location.href;
	var t = document.title;
	window.open('http://twitter.com/home?status='+encodeURIComponent(t+': '+u)+'&source=fullspeed.com','sharer','toolbar=0,status=0,scrollbars=auto,width=800,height=600');
	return false;
}

function share_email() {
	var u = location.href;
	var t = document.title;
	location.href = 'mailto:?subject='+escape(t)+'&body='+escape('I wanted to share this url with you: '+u);
	return false;
}

function cms_writecomment(type, id, reply_to, onsubmit) {
	window.scrollTo(0,0);
	var dlg = $('<div style="width:auto;height:100%;text-align:center;"><div style="margin:50px;"><img src="images/design_js/lightbox-ico-loading.gif" border="0"></div></div>');
	dlg.dialog({
		title: 'Write Comment',
		width: 700,
		height: 300 + (jQuery.support.boxModel ? 0 : 160),
		resizable: false,
		close: function(e, ui) {
			$(this).dialog('destroy');
			$(this).remove();
		},
		closeOnEscape: false
	}).css('padding', '0px');
	$.get('dialog/cms_comment.php', 'type='+escape(type)+'&id='+id+'&reply_to='+reply_to, function(html) {
		dlg_ajax(html, dlg, function(res){
			if(res == '') {
				if(onsubmit && onsubmit != '') {
					eval(onsubmit);
				} else {
					window.setTimeout(function(){
						location.reload();
					}, 500);
				}
			}
		});
	})
	return false;
}

function cms_show_password(field, visible, field2) {
	var name = $(field)[0].name;
	var pass = $(field).val();
	var parent = $(field).parent();
	if(visible)
		parent.html(parent.html().replace('type="password"', 'type="text"'));
	else
		parent.html(parent.html().replace('type="text"', 'type="password"'))
	$('input[name='+name+']', parent).val(pass);
	
	if(field2) cms_show_password(field2, visible);
	
	return false;
}