var url=window.location.href;
if (/#!/i.test(url)) {
	var splitty=url.split("#!");
	window.location.replace(splitty[1]);
}
function createUploader(container,extensions,multiple,callback){
	$('#'+container).html('<div id="upload-'+container+'-progbar"></div><div id="upload-'+container+'-info" align="center"></div><div id="upload-'+container+'-btn"></div>');      
	var uploader = new qq.FileUploader({
    	element: document.getElementById('upload-'+container+'-btn'),
        action: '/includes/ajax/upload-handler.php',
        debug: false,
		allowedExtensions: extensions,
		multiple: multiple,
		onComplete: function(id, fileName, responseJSON) {
			eval(callback+'('+responseJSON.upload_id+');');
			$('#upload-'+container+'-info').slideUp('medium');
			if(multiple) {
				$('#upload-'+container+'-progbar').slideUp('medium');
				$('#upload-'+container+'-btn').slideDown('medium');
			}
		},
		onProgress: function(id, fileName, loaded, total) {
			var percent=(loaded/total)*100;
			$('#upload-'+container+'-progress').progressbar("option","value",percent);
		},
		onSubmit: function(id, fileName){
			$('#upload-'+container+'-btn').slideUp('medium');
			$('#upload-'+container+'-progbar').html('<div id="upload-'+container+'-progress"></div>').slideDown('medium');
			$('#upload-'+container+'-progress').progressbar({
				value: 0
			});
			$('#upload-'+container+'-info').html('<strong>Uploading '+fileName+'</strong>').slideDown('medium');
		}
	});
}
var npldsvdsvds='';
var global_game_id=0;
function loady() {
    $('a').unbind('click',beginloadpage);
	$('a').bind('click',beginloadpage);
}
$(window).bind('hashchange', function(event) {
  	loady();
	var url=window.location.href;
	if (/#!/i.test(url)) {
		var splitty=url.split("#!");
		loadpage(splitty[1]);
	}
});
function beginloadpage(event) {
	if(1==0) {
	var url=this.href;
	if(/developers.newhaze.com/i.test(url) && url!='http://developers.newhaze.com/logout.php') {
		if(/insights.php/i.test(url) || this.target=='_blank') {
		}else{
			event.preventDefault();
			if (/#/i.test(url)) {
			}else{
				loadpage(url);
			}
		}
	}
	}
}
function loadpage(url) {
	if(npldsvdsvds.length==0) {
		npldsvdsvds='1';
		checklogin();
		if (/\?/i.test(url)) {
			var sendurl=url+'&shrink';
		}else{
			var sendurl=url+'?shrink';
		}
		$('#vfdvjdfvkvkjzvhdvkbds').html('');
		$('#vfdvjdfvkvkjzvhdvkbds').load(sendurl,function(response) {
			npldsvdsvds='';
			loady();
			if($("#vfdvjdfvkvkjzvhdvkbds title").html()==null) {
				document.title = "NewHaze Developers";
			}else{
				document.title = $("#vfdvjdfvkvkjzvhdvkbds title").html();
			}
    	});
		changelocation(url);
		window.scrollTo(0, 0);
	}
}
function changelocation(url) {
	window.location = '#!'+(url.replace("http://developers.newhaze.com", ""));
}
function follow(id) {
	$.post("includes/ajax/profile.php",{follow:id},function(data) {
		display_success_msg("You are now following this developer.");
		return true;
	});
}
function unfollow(id) {
	$.post("includes/ajax/profile.php",{unfollow:id},function(data) {
		display_success_msg("You have stopped following this developer.");
		return true;
	});
}
function dialog_set_loading_html(id) {
	$("#"+id).html('<h2>Loading...</h2>');
}
function gamePopup(gameId,name,description,category) {
	var cats=['Unknown', 'Action', 'Adventure', 'Board Game', 'Casino', 'Driving', 'Fighting', 'Puzzles', 'Shooting', 'Sports', 'Arcade', 'Not Games', 'Other'];
	$("#game_pop").html('<table width="420" cellpadding="0" cellspacing="5" border="0"><tr valign="top"><td width="100"><img src="http://connect.newhaze.com/images/games/'+gameId+'/100x100" width="100" height="100" class="noborder" /><a href="/game.php?id='+gameId+'" class="jq-button">View Profile</a><br /><a href="javascript:;" class="jq-button" onclick="openAddGame('+gameId+',\''+name+'\');";">Add to Site</a></td><td><em>'+description+'</em><br /><b>Category: </b>'+cats[category]+'</td></tr></table>').dialog("option","title",name).dialog('open');
	$(".jq-button").button();
}
function openAddGame(gameId,name) {
	$.post('/includes/ajax/games.php', {ecats:gameId}, function(data) {
		data=eval(data);
		selectgamecategory(0, data, function(cid) {
			$.post("/includes/ajax/games.php",{addg2c:cid,id:gameId},function(data){
				display_success_msg('This game has been added to your site successfully!');
			});
		});
	});
}
function checklogin() {
	if(loggedin==1) {
		$.get('/includes/ajax/checksession.php', function(data) {
			if(data==0) {
				loggedin=0;
				location.reload(true);
			}
		});
	}
}
function display_success_msg(message) {
	$('#fb-zone-main').slideUp('fast',function(){$('#fb-zone-main').html('<div class="ui-state-highlight ui-corner-all" style="margin-top: 20px; padding: 0 .7em;"><p><strong>'+message+'</strong></p></div>').slideDown('medium',function(){$('#fb-zone-main').delay(5000).slideUp("medium");});});
}
function display_error_msg(message) {
	$('#fb-zone-main').slideUp('fast',function(){$('#fb-zone-main').html('<div class="ui-state-error ui-corner-all" style="margin-top: 20px; padding: 0 .7em;"><p><strong>'+message+'</strong></p></div>').slideDown('medium',function(){$('#fb-zone-main').delay(5000).slideUp("medium");});});
}
function closeDialog(name) {
	$('#'+name).dialog('close');
}
function htmlInElement(name,html) {
	$('#'+name).html(html);
}
function submit_comment(node_id, comment) {
	$.post("/includes/ajax/profile.php", { comment: comment, node_id: node_id }, function(data) {
		display_success_msg("Your comment has been sent successfully!");
		return true;
	});
}
function remove_comment(cid) {
	$.post("/includes/ajax/profile.php", { id: cid, remove_comment: true }, function(data) {
		display_success_msg("Your comment has been removed successfully!");
		return true;
	});
}
function post_comment(node_id,level) {
	if(loggedin==1) {
		var message=$('#node_comment_'+node_id).val();
		if(message!='') {
			$.post("/includes/ajax/profile.php", { comment: message, node_id: node_id }, function(data) {
				display_success_msg("Your comment has been added successfully!");
				$('#node_comment_'+node_id).val('');
				var picsize=32;
				if(level==1) {
					picsize=50;
				}
				if(level==0) {
					picsize=75;
				}
				var date_id=random_id_generator();
				message=message.replace(/(((http|https|ftp):\/\/([A-Z0-9][A-Z0-9_-]*(?:.[A-Z0-9][A-Z0-9_-]*)+):?(d+)?\/?))(?!([^<]+)?>)/gi, '<a href="$1" target="_blank">$1</a>');
				str="<tr valign=\"top\" id=\"dev_comment_"+data+"\"><td width=\""+picsize+"\"><a href=\"/profile.php?id="+loggedin_id+"\"><img src=\"http://connect.newhaze.com/uploads/"+loggedin_picture+"\" width=\""+picsize+"\" height=\""+picsize+"\" class=\"noborder\" /></a></td><td style=\"border-bottom:1px dotted #CCCCCC;\"><strong><a href=\"/profile.php?id="+loggedin_id+"\">"+loggedin_username+"</a></strong> "+message+"<div style=\"font-size:11px;color:#666666;\">"+wd_code(get_current_timestamp())+" · <a id=\"show_comments_link_c_"+data+"\" href=\"javascript:;\" title=\"Show all comments on this post\" onclick=\"togglecommentdisplay('"+data+"');\">Comment</a> · <a href=\"javascript:;\" onclick=\"hide_comment("+data+",'"+node_id+"');\">Delete</a></div><div id=\"comments_"+data+"\" style=\"display:none;\"></div></td></tr>";
				if (level==1) {
					$('#comment_form_'+node_id).after(str);
				}else{
					$('#comment_form_'+node_id).before(str);
				}
				changecommentslink(node_id,'u');
			});
		}
	}
}
function random_id_generator() {
	chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
	pass = "";
	for(x=0;x<20;x++) {
		i = Math.floor(Math.random() * 62);
	   	pass += chars.charAt(i);
	}
	return pass;
}
function hide_comment(id,par_node) {
	remove_comment(id);
	$('#dev_comment_'+id).remove();
	changecommentslink(par_node,'d');
}
function togglecommentdisplay(node_id) {
	if($('#comments_'+node_id).is(':hidden')) {
		$('#comments_'+node_id).slideDown('medium');
	}else{
		$('#comments_'+node_id).slideUp('medium');
	}
}
function changecommentslink(id, dir) {
	var val=$('#show_comments_link_'+id).html();
	var num=0;
	var output=val;
	if(val=='Comment') {
		num=0;
	}else{
		val=val.split(" ");
		num=parseInt(val[0]);
	}
	if(dir=='u') {
		num=num+1;
	}else{
		num=num-1;
	}
	if(num==0) {
		output='Comment';
	}else if(num==1) {
		output='1 comment';
	}else{
		output=num+' comments';
	}
	$('#show_comments_link_'+id).html(output);
}
function get_current_timestamp() {
	var timestamp = new Date().getTime();
	return timestamp/1000;
}
function wd_code(timestamp) {
	var id=random_id_generator();
	var old=new Date(timestamp*1000);
	var info=watch_date(timestamp*1000,id);
	return '<span id="'+id+'" title="'+old.toString()+'">'+info+'</span>';
}
function pluralize(num, singular, plural) {
	if(num==1) {
		return singular;
	}else{
		return plural;
	}
}
function watch_date(timestamp,id) {
	var old=new Date(timestamp);
	var now=new Date();
	var output="";
	var refreshin=0;
	var diff=Math.round((now-old)/1000);
	if(diff<60) {
		output=diff+pluralize(diff,' second',' seconds');
		refreshin=1;
	}else if(diff<3600) {
		var num=Math.round((diff/60));
		output=num+pluralize(num,' minute',' minutes');
		refreshin=60;
	}else if(diff<86400) {
		var num=Math.round((diff/3600));
		output=num+pluralize(num,' hour',' hours');
		refreshin=3600;
	}else{
		var num=Math.round((diff/86400));
		output=num+pluralize(num,' day',' days');
		refreshin=86400;
	}
	$('#'+id).html(output+' ago');
	setTimeout("watch_date("+timestamp+",'"+id+"');",(refreshin*1000));
	return output+' ago';
}
function nh_date(timestamp) {
	if(timestamp>0) {
		var now=new Date();
		var output="";
		var old=new Date(timestamp*1000);
		var diff=Math.round((now-old)/1000);
		if(diff<60) {
			if(diff==1) {
				output="1 second ago";
			}else{
				output=diff+" seconds ago";
			}
		}else if(diff<3600) {
			if(Math.round((diff/60))==1) {
				output="1 minute ago";
			}else{
				output=Math.round((diff/60))+" minutes ago";
			}
		}else if(diff<86400) {
			if(Math.round((diff/3600))==1) {
				output="1 hour ago";
			}else{
				output=Math.round((diff/3600))+" hours ago";
			}
		}else{
			if(Math.round((diff/86400))==1) {
				output="1 day ago";
			}else{
				output=Math.round((diff/86400))+" days ago";
			}
		}
		return output;
	}
}
$(document).ready(function(){
	$(".jq-button").button();
	loady();
	$('#game_pop').dialog({
		autoOpen: false,
		width: 450,
		position: 'center',
		resizable: false
	});
});
