var $j = jQuery.noConflict();

// VoteUp function
function voteUp(voteType, voteTypeID){
	if(!loggedIn) {guestAlert();return;}
	
	$j.get(ajaxScript+'?module=token',function(token) {
		$j.getJSON(ajaxScript,{
			'module' : voteType.toLowerCase(),
			'action' : 'voteup',
			'id' : voteTypeID,
			'token'	: token
		},function(data) {
			if(data&&!data.error) {
				$j('#'+voteType[0].toLowerCase()+voteTypeID+'_good').text(data.up);
				$j('#'+voteType[0].toLowerCase()+voteTypeID+'_bad').text(data.down);
				alertSuccess('You successfuly gave this submission a Win rating.');
			}
			else if(data.error) {
				alert(data.error);
			}
			else {
				alert('Something went wrong, please try again.');
			}
		});
	});
}

// VoteUp function
function voteDown(voteType, voteTypeID){
	if(!loggedIn) {guestAlert();return;}
	
	$j.get(ajaxScript+'?module=token',function(token) {
		$j.getJSON(ajaxScript,{
			'module' : voteType.toLowerCase(),
			'action' : 'votedown',
			'id' : voteTypeID,
			'token'	: token
		},function(data) {
			if(data&&!data.error) {
				$j('#'+voteType[0].toLowerCase()+voteTypeID+'_good').text(data.up);
				$j('#'+voteType[0].toLowerCase()+voteTypeID+'_bad').text(data.down);
				alertSuccess('You successfuly gave this submission a Fail rating.');
			}
			else if(data.error) {
				alert(data.error);
			}
			else {
				alert('Something went wrong, please try again.');
			}
		});
	});
}

// commentReply Function
function commentReply(commentID){
	form = $j('#creply_form_'+commentID);
	replylist = $j('#creply_list_'+commentID);
	
	if(replylist&&form) {
		if(replylist.css('display') == 'block'&&form.css('display') == 'none') {
			form.css('display','block');
		}
		else if(replylist.css('display') == 'none'&&form.css('display') == 'none') {
			//replylist.css('display','block');
			form.css('display','block');
		}
		else {
			//replylist.css('display','none');
			form.css('display','none');
		}

	}
}

// showReplies Function
function showReplies(commentID){
	form = $j('#creply_form_'+commentID);
	replylist = $j('#creply_list_'+commentID);
	
	if(replylist) {
		if(replylist.css('display') == 'none') {
			//replylist.css('display','block');
		}
		else {
			//replylist.css('display','none');
			if(form) {
				if(form.css('display') == 'block') {
					//form.css('display','none');
				}
			}
		}
	}
}

// submitForm Function
function submitForm(form) {
	if(!loggedIn) {guestAlert();return;}
	var values = $j(form).serialize();
	var commentID = $j(form).children('input[name="comment_id"]').val();
	
	$j.post($j(form).attr('action'),values,function(response) {
		var replycount = $j('#reply_count_'+commentID);
		var count = parseInt($j(replycount).text())
		
		$j(replycount).text(count+1+' Replies');
		$j('#cResponse_'+commentID).empty().html(response);
		
		$j(form).children('textarea[name="comment"]').val('');
		
		loadCommentReplies(commentID);
	});
}

var user_name_lastvalue;

// checkUser Function
function checkUser() {
	field = $j('#user_name');
	
	if(user_name_lastvalue != field.val()) {
		$j.getJSON(ajaxScript,{
			'module' : 'register',
			'action' : 'checkuser',
			'user' : field.val()
		},function(data) {
			if(data.success) {
				$j('#user_name_results').html('<div class="success">'+data.success+'</div>');
			}
			else if(data.error) {
				$j('#user_name_results').html('<div class="error">'+data.error+'</div>');
			}
			else {
				$j('#user_name_results').html('');
			}
		});
	}
	
	user_name_lastvalue = field.val();
}
		
function guestAlert() {
	$j("#guest_alert").dialog('open');
}

function alertError(message) {
	$j('.alert-bottom').empty().html('<div class="alert-error">'+message+'</div>').animate({bottom: 0}, 500);
	setTimeout(function() {
		$j('.alert-bottom').animate({bottom: -50}, 500);
	},15000);
}

function alertSuccess(message) {
	$j('.alert-bottom').empty().html('<div class="alert-success">'+message+'</div>').animate({bottom: 0}, 500);
	setTimeout(function() {
		$j('.alert-bottom').animate({bottom: -50}, 500);
	},15000);
}

$j(document).ready(function() {
	$j('.alert-bottom').click(function() {
		$j(this).animate({bottom: -50}, 500);
	});
	
	// Playin Update Form
	$j('#playin_update').submit(function(e) {
		e.preventDefault();
		
		$j.post($j(this).attr('action'),$j(this).serialize(),
			function(response) {
				if(response.success) {
					$j('.playin-input-box').animate({backgroundColor: '#dcf5d9'},500).animate({backgroundColor: '#ffffff'},500).val('What game are you Playin?');
					alertSuccess(response.success);
				}
				else if(response.error) {
					// $j('.playin-input-box').animate({backgroundColor: '#ffecec'},350).animate({backgroundColor: '#ffffff'},350);
					alertError(response.error);
				}
				else {
					alertError('Something went wrong. Please contact us.');
				}
			},"json"
		);
	});
	
	// Expandable link click options
	$j("a.toggle-expand").each(function(i) {
		var div = $j(this).parent('span').next('.expandable');
		
		// Click event
		$j(this).click(function(e){

			e.preventDefault();

			// If the display is none, change the toggle link class to hide
			if($j(div).css('display')=='none') {
				$j(this).removeClass('toggle-show');
				$j(this).addClass('toggle-hide');
				$j(this).children('strong').text('Minimize');
			}
			// If the display is not none, change the toggle link class to show
			else {
				$j(this).removeClass('toggle-hide');
				$j(this).addClass('toggle-show');
				$j(this).children('strong').text('Expand');
			}
			
			// Slide toggle
			div.slideToggle(400);

		});
	});
	
	
	$j('.modal-popup').each(function(i, a) {
		var dwidth = parseInt($j("#"+$j(this).attr('rel')).attr('width'));
		var dheight = parseInt($j("#"+$j(this).attr('rel')).attr('height'));
		
		$j("#"+$j(this).attr('rel')).dialog({
			bgiframe: true,
			resizable: false,
			draggable: false,
			autoOpen: false,
			width: (dwidth ? dwidth : 500),
			height: (dheight ? dheight : 300),
			modal: true,
			show: 'fold',
			hide: 'fold'
		});
		
		$j(a).click(function(e) {
			e.preventDefault();
			$j("#"+$j(this).attr('rel')).dialog('open');
		})
	});
	
	$j('.modal').each(function(i,e) {
		$j(e).click(function(el) {
			el.preventDefault();
			
			$j.get(ajaxScript+'?module=token',function(token) {
				$j.get($j(e).attr('href')+'&token='+token,function(html) {
					dwidth = parseInt($j(e).attr('width'));
					dheight = parseInt($j(e).attr('height'));
					
					$j('.modal_wrap').empty().html(html);
					$j(".modal_wrap").dialog({
						bgiframe: true,
						resizable: false,
						draggable: false,
						autoOpen: false,
						width: (dwidth ? dwidth : 500),
						height: (dheight ? dheight : 300),
						modal: true,
						show: 'fold',
						hide: 'fold',
						title:	$j(e).attr('title'),
						close: function() {	
							$j(this).dialog('destroy');
						}
					});
					$j('.modal_wrap').dialog('open');
				});
			});
		});
	});
	
	$j('.emailform').each(function(i,form_item) {
		$j(form_item).submit(function(form) {
			form.preventDefault();
			
			var values = $j(this).serialize();
			var articleID = $j(this).children('input[name="article_id"]').val();
			
			$j.post($j(this).attr('action'),values,function(response) {
				$j('#emailform_response_'+articleID).empty().html(response);
			});
		});
	});
	
	$j("#guest_alert").dialog({
		bgiframe: true,
		resizable: false,
		draggable: false,
		autoOpen: false,
		width: 500,
		height: 315,
		modal: true,
		show: 'fold',
		hide: 'fold',
		buttons: {
			'Register Now!': function() {
				window.location = '/register/';
			},
			Close: function() {
				$j(this).dialog('close');
			}
		}
	});
	
	
	// Connect bar - online friends
	$j('.cbar-olfriends').toggle(function(e) {
		e.preventDefault();
		
		var menu = $j('.cbar-olfriends-list');
		
		menu.css({visibility: "visible",display: "none"}).slideDown(250);
	},
	function(e) {
		e.preventDefault();
		
		var menu = $j('.cbar-olfriends-list');
		// menu.css({visibility: "hidden"});
		menu.slideUp(250);
	});
	
	// $('marquee').marquee();
});

function toggleDiv(div) {
	$j(div).toggle();
}

function ajaxLoad(div, url) {
	$j.get(url,function(html) {			
		$j(div).empty().html(html);
	});
}

function ajaxAppend(div, url) {
	$j.get(url,function(html) {			
		$j(div).html($j(div).html()+html);
	});
}

// deleteStatus function
function deleteStatus(statusID){
	$j.get(ajaxScript+'?module=token',function(token) {
		$j.getJSON(ajaxScript,{
			'module' : 'playin',
			'action' : 'delete',
			'id' : statusID,
			'token'	: token
		},function(data) {
			if(data.success) {
				$j('.status-'+statusID).slideUp(250);
			}
			else if(data.error) {
				alert('Unable to delete Playin status. Please contact us.');
			}
			else {
				alert('Something went wrong, please try again.');
			}
		});
	});
}

// editStatus function
function editStatus(statusID){
	$j.get(ajaxScript+'?module=form&form=edit_playin&id='+statusID,function(html) {	
		$j('.modal_wrap').empty().html(html);
		$j(".modal_wrap").dialog({
			bgiframe: true,
			resizable: false,
			draggable: false,
			autoOpen: false,
			width: 500,
			height: 310,
			modal: true,
			show: 'fold',
			hide: 'fold',
			title:	'Edit Playin Status',
			close: function() {	
				$j(this).dialog('destroy');
			}
		});
		$j('.modal_wrap').dialog('open');
	});
}