// Project.js
var status_classes = ['pl_status', 'pl_accepted', 'pl_declined', 'pl_scheduled', 'pl_done'];

function Panel1() {
	var big = ($('#post').attr('class') == 'proposePanelBig') ? true : false;
	if(big)	$('#post').css('background', 'url(/images/panels.gif) no-repeat center -580px');
	else 	$('#post').css('background', 'url(/images/panels.gif) no-repeat center -427px');
	$('#type').val(1);
	
}
function Panel2() {
	var big = ($('#post').attr('class') == 'proposePanelBig') ? true : false;
	if(big)	$('#post').css('background', 'url(/images/panels.gif) no-repeat center -1007px');
	else 	$('#post').css('background', 'url(/images/panels.gif) no-repeat center -854px');
	$('#type').val(2);
}
function Panel3() {
	var big = ($('#post').attr('class') == 'proposePanelBig') ? true : false;
	if(big)	$('#post').css('background', 'url(/images/panels.gif) no-repeat center -153px');
	else 	$('#post').css('background', 'url(/images/panels.gif) no-repeat center 0px');
	$('#type').val(3);
}
function Panel4() {
	var big = ($('#post').attr('class') == 'proposePanelBig') ? true : false;
	if(big)	$('#post').css('background', 'url(/images/panels.gif) no-repeat center -1434px');
	else 	$('#post').css('background', 'url(/images/panels.gif) no-repeat center -1281px');
	$('#type').val(4);
}

function Search(project, search) {
	$.ajaxSetup({scriptCharset: "utf-8", contentType: "application/json; charset=utf-8"});
	$.getJSON(
		'/ajax.php',
		{
			'project': project,
			'search': search
		},
		function(obj) {
			$('#search_result').html(obj.search_result_html);
			if(search) {
				SearchShow(obj.search_result_count, obj.search_result_from);
				
				$('#searchResultPosts').text('');
				for(r in obj.search_result) {
					$('#searchResultPosts').append('\
						<div class="'+obj.search_result[r]['type_class']+'">\
							<div class="numVoteBlock">\
								<span class="nvbNum">'+obj.search_result[r]['votes']+'</span>\
								<span class="nvbvotes">votes</span>\
							</div>\
							<div class="srBdecription">\
								<a href="'+obj.search_result[r]['url']+'">\
									'+obj.search_result[r]['title']+'\
								</a>\
							</div>\
							<a href="javascript:void(0)" onClick="Vote('+obj.search_result[r]['post_id']+')" class="srB'+(!obj.search_result[r]['voted'] ? 'add' : 'remove')+'voteBTN"></a>\
							<a href="javascript:void(0)" onClick="$(\'#srTextBlock'+obj.search_result[r]['post_id']+'\').toggle()" class="srCSBTN"></a>\
						</div>\
						<div class="srPBdescr" id="srTextBlock'+obj.search_result[r]['post_id']+'">\
							<div class="srText">\
								'+obj.search_result[r]['text']+'\
								<div class="VotesPostTool">\
									<a href="'+obj.search_result[r]['url']+'" class="VotesPostComments"><span>'+obj.search_result[r]['comments']+'</span> comments</a>\
									'+obj.search_result[r]['author']+'\
									<span class="VotesPostDate">'+obj.search_result[r]['date']+'</span>\
								</div>\
							</div>\
							<div class="clear"></div>\
						</div>\
					');
				}
			}
		}
	);
	
	if(!search) {
		SearchHide();
	}
}
function SearchShow(search_result_count, search_result_from) {
	$('#post').attr('class', 'proposePanelBig');
	$('#post_description').show();
	$('#post_right').show();
	var type = $('#type').val();
	if(type == 1) Panel1();
	if(type == 2) Panel2();
	if(type == 3) Panel3();
	if(type == 4) Panel4();
	$('#searchResultCount').text(search_result_count);
	$('#searchResultFrom').text(search_result_from);
	$('.searchResult').show();
}
function SearchHide() {
	$('#post').attr('class', 'proposePanel');
	$('#post_description').hide();
	$('#post_right').hide();
	var type = $('#type').val();
	if(type == 1) Panel1();
	if(type == 2) Panel2();
	if(type == 3) Panel3();
	if(type == 4) Panel4();
	$('.searchResult').hide();
}

function Vote(post_id, type, negative_votes) {
	$.getJSON(
		'/ajax.php',
		{
			'vote': post_id,
			'type': type
		},
		function(obj) {
			$('#vote'+post_id).text('');
			if(negative_votes) {
				if(type == -1) {
					$('#vote'+post_id).append('<a href="javascript:void(0)" class="voteAdd" onClick="Vote('+post_id+', 0, '+negative_votes+')"></a>');
					$('#vote'+post_id).append('<a href="javascript:void(0)" class="voteRemoveDis"></a>');
				}
				if(type ==  0) {
					$('#vote'+post_id).append('<a href="javascript:void(0)" class="voteAdd" onClick="Vote('+post_id+', 1, '+negative_votes+')"></a>');
					$('#vote'+post_id).append('<a href="javascript:void(0)" class="voteRemove" onClick="Vote('+post_id+', -1, '+negative_votes+')"></a>');
				}
				if(type ==  1) {
					$('#vote'+post_id).append('<a href="javascript:void(0)" class="voteAddDis"></a>');
					$('#vote'+post_id).append('<a href="javascript:void(0)" class="voteRemove" onClick="Vote('+post_id+', 0, '+negative_votes+')"></a>');
				}
			} else {
				if(type == 1)
					$('#vote'+post_id).append('<a href="javascript:void(0)" class="removevote" onClick="Vote('+post_id+', 0, '+negative_votes+')"></a>');
				if(type == 0)
					$('#vote'+post_id).append('<a href="javascript:void(0)" class="addvote" onClick="Vote('+post_id+', 1, '+negative_votes+')"></a>');
			}
			$('#post'+post_id+'_votes').text(obj.post_votes);
			
			
			// Sidebar
			if(obj.voted_posts) {
				$('#voted_posts').html(obj.voted_posts);
				$('#voted_posts_container').show();
			} else
				$('#voted_posts_container').hide();
		}
	);
}

function RemoveBookmark(post_id) {
	$.get(
		'/ajax.php',
		{
			'bookmark': post_id
		},
		function(data) {
			if(data == 0)
				$('#bookmark'+post_id).hide();
		}
	);
}

function DeletePost(post_id) {
	var reason = prompt('Are you sure?\nPlease enter reason:');
	
	if(reason) {
		$.get(
			'/ajax.php',
			{
				'delete_post': 1,
				'post': post_id,
				'reason': reason
			},
			function(data) {
				if(data == "1")
					$('#post'+post_id).animate({height: "hide"})
			}
		);
	}
}

function DeleteComment(comment_id, post_id) {
	var reason = prompt('Are you sure?\nPlease enter reason:');
	
	if(reason) {
		$.getJSON(
			'/ajax.php',
			{
				'delete_comment': 1,
				'post': post_id,
				'comment': comment_id,
				'reason': reason
			},
			function(obj) {
				if(obj)
					$('#comment'+comment_id).animate({height: "hide"})
				
				$('#post'+post_id+'_comments').html('<span>'+obj.comments+'</span> comments');
			}
		);
	}
}

function ShowResponse(post_id) {
	$('#response'+post_id).hide();
	$('#response_show'+post_id).hide();
	$('#response_block'+post_id).show();
	$('#response_form'+post_id).show();
}

function HideResponse(post_id) {
	$('#response'+post_id).show();
	$('#response_show'+post_id).show();
	$('#response_block'+post_id).hide();
}

function ShowSetStatus(post_id) {
	$('#set_status'+post_id).toggle();
}
function SetStatus(post_id, status) {
	$.get(
		'/ajax.php',
		{
			'post_id': post_id,
			'status': status
		},
		function(data) {
			if(data == "1") {
				$('#status_s'+post_id).attr('class', status_classes[status]+'_s');
				ShowSetStatus(post_id);
			}
		}
	);
}

function ShowTextForm(post_id) {
	$('#title'+post_id).hide();
	$('#text'+post_id).hide();
	$('#title_form'+post_id).show();
	$('#text_form'+post_id).show();
}

function HideTextForm(post_id) {
	$('#title_form'+post_id).hide();
	$('#text_form'+post_id).hide();
	$('#title'+post_id).show();
	$('#text'+post_id).show();
}

function ShowCommentForm(comment_id) {
	$('#comment_text'+comment_id).hide();
	$('#comment_form'+comment_id).show();
}
function HideCommentForm
(comment_id) {
	$('#comment_text'+comment_id).show();
	$('#comment_form'+comment_id).hide();
}

function ShowFade(form, form_submit) {
	$('#qr_form_name').val(form);
	$('#qr_form_submit_name').val(form_submit);
	$('#ql_form_name').val(form);
	$('#ql_form_submit_name').val(form_submit);
	
	$('#fade').css('display', 'block');
	$('#fade_signin').fadeIn('slow');
	
	return false;
}
function HideFade() {
	$('#fade_signin').hide();
	$('#fade_login').hide();
	$('#fade').css('display', 'none');
	
	return false;
}
function ShowQuickLogin() {
	$('#fade_signin').fadeOut('slow');
	$('#fade_login').fadeIn('slow');
}
function ShowQuickSignin() {
	$('#fade_login').fadeOut('slow');
	$('#fade_signin').fadeIn('slow');
}



function FormQRError(html) {
	$('#form_qr_error').html(html).show();
}
function FormQLError(html) {
	$('#form_ql_error').html(html).show();
}
function FromErrorColor(el) {
	$('#'+el).css('background', '#ffeeee');
}
function FormQCheckSignIn() {
	form = $('#qr_form_name').val();
	form_submit = $('#qr_form_submit_name').val();
	var error = '';
	var flag = false;
	
	form_password = $('#form_qr_password').val();
	form_password2 = $('#form_qr_password2').val();
	form_email = $('#form_qr_email').val();
	
	form_beta_tester = $('#form_beta_tester').attr('checked');
	form_agree = $('#form_agree').attr('checked');
	
	if(!form_password || form_password == 'password') {
		error += '<li>"password" field required.</li>';
		FromErrorColor('form_qr_password');
		flag = true;
	}
	if(!form_password2 || form_password2 == 'confirm password') {
		error += '<li>"confirm password" field required.</li>';
		FromErrorColor('form_qr_password2');
		flag = true;
	}
	if(!form_email || form_email == 'email') {
		error += '<li>"email" field required.</li>';
		FromErrorColor('form_qr_email');
		flag = true;
	}
	
	if(form_password != form_password2) {
		error += '<li>"password" and "confirm password" fields incorrect.</li>';
		FromErrorColor('form_qr_password');
		FromErrorColor('form_qr_password2');
		flag = true;
	}
	if(!form_email.match(/([0-9a-z\.-_]{1,16})@([0-9a-z\.-_]{1,16})\.([a-z]{2,4})/)) {
		error += '<li>"email" field incorrect.</li>';
		FromErrorColor('form_qr_email');
		flag = true;
	}
	if(form_beta_tester && !form_agree) {
		error += '<li>You must agree to NDA conditions.</li>';
		flag = true;
	}
	
	$.get(
		'/ajax.php',
		{
			'password': form_password,
			'password2': form_password2,
			'email': form_email,
			'beta_tester': form_beta_tester
		},
		function(data) {
			if(data) {
				error += data;
				FromErrorColor('form_qr_password');
				FromErrorColor('form_qr_password2');
				FromErrorColor('form_qr_email');
				flag = true;
			}
			
			
			
			// return
			if(flag) {
				FormQRError(error);
				return false;
			}
			
			HideFade();
			$('#'+form_submit).removeAttr('onClick');
			$('#'+form).removeAttr('onSubmit');
			$('#'+form).submit();
		}
	);
}
function FormQCheckLogIn() {
	form = $('#ql_form_name').val();
	form_submit = $('#ql_form_submit_name').val();
	var error = '';
	var flag = false;
	
	form_email = $('#form_ql_email').val();
	form_password = $('#form_ql_password').val();
	form_remember = $('#form_ql_remember').val();
	
	if(!form_email || form_email == 'E-mail') {
		error += '<li>"E-mail" field required.</li>';
		FromErrorColor('form_ql_email');
		flag = true;
	}
	if(!form_email.match(/([0-9a-z\.-_]{1,16})@([0-9a-z\.-_]{1,16})\.([a-z]{2,4})/)) {
		error += '<li>"E-mail" field incorrect.</li>';
		FromErrorColor('form_ql_email');
		flag = true;
	}
	if(!form_password || form_password == 'password') {
		error += '<li>"password" field required.</li>';
		FromErrorColor('form_ql_password');
		flag = true;
	}
	
	$.get(
		'/ajax.php',
		{
			'email': form_email,
			'password': form_password,
			'remember': form_remember
		},
		function(data) {
			if(data) {
				error += data;
				FromErrorColor('form_ql_email');
				FromErrorColor('form_ql_password');
				flag = true;
			}
			
			
			
			// return
			if(flag) {
				FormQLError(error);
				return false;
			}
			
			HideFade();
			$('#'+form_submit).removeAttr('onClick');
			$('#'+form).removeAttr('onSubmit');
			$('#'+form).submit();
		}
	);
}

function ToggleNDA() {
	$('#nda').toggle();
}

function ShowAddPoll(post_id) {
	$('#add_poll'+post_id).show();
}
function AddAnswer(post_id) {
	$('#answers'+post_id).append('<input type="text" name="answers[]" class="PostVotesVField" /><br />');
}

function UpdateAddProjectFields() {
	$('#qr_title').val($('#form_title').val());
	$('#qr_description').val($('#form_description').val());
	$('#qr_tags').val($('#form_tags').val());
	$('#qr_site').val($('#form_site').val());
	
	$('#ql_title').val($('#form_title').val());
	$('#ql_description').val($('#form_description').val());
	$('#ql_tags').val($('#form_tags').val());
	$('#ql_site').val($('#form_site').val());
}

function SetFeel(feel) {
	$('#feel').val(feel);
	
	if(feel == 1)
		$('#feel1').attr('class', 'AddCBFeelsGreen_hover');
	else
		$('#feel1').attr('class', 'AddCBFeelsGreen');

	if(feel == 2)
		$('#feel2').attr('class', 'AddCBFeelsYellow_hover');
	else
		$('#feel2').attr('class', 'AddCBFeelsYellow');

	if(feel == 3)
		$('#feel3').attr('class', 'AddCBFeelsBlue_hover');
	else
		$('#feel3').attr('class', 'AddCBFeelsBlue');

	if(feel == 4)
		$('#feel4').attr('class', 'AddCBFeelsRed_hover');
	else
		$('#feel4').attr('class', 'AddCBFeelsRed');

		
	switch(feel) {
		case 1: $('#feelingText').text('Happy!'); break;
		case 2: $('#feelingText').text('Kidding!'); break;
		case 3: $('#feelingText').text('Indifferent!'); break;
		case 4: $('#feelingText').text('Sad!'); break;
		default: $('#feelingText').text('');
	}
}

function SearchProjects(search) {
	$.ajaxSetup({scriptCharset: "utf-8", contentType: "application/json; charset=utf-8"});
	$.getJSON(
		'/ajax.php',
		{
			'search': search
		},
		function(obj) {
			$('#projects').text('');
			
			for(r in obj.search_result) {
				$('#projects').append('\
					<div class="projectp_block">\
						<div class="projectp_logo"><img src="/images/projects/'+obj.search_result[r]['logo']+'" width="90" /></div>\
							<div class="projectp_desc">\
								<div class="projectp_dTitle"><a href="/'+obj.search_result[r]['url']+'/">'+obj.search_result[r]['name']+'</a></div>\
								<div class="projectp_dCntnt">'+obj.search_result[r]['description']+'</div>\
								<div class="projectp_dtools">\
									<div class="projectp_dtoolsbl">\
										<a href="http://'+obj.search_result[r]['site']+'" target="_blank" class="projectp_offsite">official website</a>\
										<div class="projectp_offperson">official establishment <a href="/user/'+obj.search_result[r]['user_id']+'/">'+obj.search_result[r]['login']+'</a></div>\
									</div>\
									<div class="projectp_dtoolsbr">\
										<div class="projectp_ideas">'+obj.search_result[r]['posts']+' ideas</div>\
										<div class="projectp_comments">'+obj.search_result[r]['comments']+' comments</div>\
									</div>\
									<div class="clear"></div>\
								</div>\
							</div>\
						<div class="clear"></div>\
					</div>\
				');
			}
		}
	);
}

function ShowAddScreenshot(post_id) {
	$('#add_screenshot'+post_id).show();
}

function Important(post_id) {
	$.get(
		'/ajax.php',
		{
			'important': post_id
		},
		function(data) {
			if(data == 0) {
				$('#important'+post_id).text('stick');
				$('#important'+post_id).attr('class', 'StickPost');
			} else {
				$('#important'+post_id).text('unstick');
				$('#important'+post_id).attr('class', 'StickPost2');
			}
		}
	);
}

function BookmarkPost(post_id) {
	$.get(
		'/ajax.php',
		{
			'bookmark': post_id
		},
		function(data) {
			if(data == 1) {
				$('#bookmark'+post_id).text('remove bookmark');
				$('#bookmark'+post_id).attr('class', 'EditBookmark2');
			} else if(data == 0) {
				$('#bookmark'+post_id).text('add bookmark');
				$('#bookmark'+post_id).attr('class', 'EditBookmark');
			}
		}
	);
}

function Quote(comment_id) {
	login = $('#comment_login'+comment_id).text();
	text = $('#comment_text'+comment_id).text();
	$('#comment_text').replaceSelection('[quote "'+login+'"]'+text+'[/quote]');
}

function ToggleAllWithText() {
	if($('#toggleWithText').attr('checked')) {
		$('#response').attr('checked', true);
		$('#text').attr('checked', true);
		$('#title').attr('checked', true);
		$('#comments').attr('checked', true);
	} else {
		$('#response').attr('checked', false);
		$('#text').attr('checked', false);
		$('#title').attr('checked', false);
		$('#comments').attr('checked', false);
	}
}

function ToggleAllStatus() {
	if($('#toggleStatus').attr('checked')) {
		$('#done').attr('checked', true);
		$('#accepted').attr('checked', true);
		$('#declined').attr('checked', true);
		$('#sheduled').attr('checked', true);
		$('#none').attr('checked', true);
	} else {
		$('#done').attr('checked', false);
		$('#accepted').attr('checked', false);
		$('#declined').attr('checked', false);
		$('#sheduled').attr('checked', false);
		$('#none').attr('checked', false);
	}
}

function bbUrlShow() {
	$('#addURLtoComment').attr('class', 'addURLtoComment_hover');
	$('#addIMGtoComment').attr('class', 'addIMGtoComment');
	
	$('#bbImg').hide();
	
	$('#bbUrlText').val('');
	$('#bbUrlUrl').val('');
	$('#bbUrl').show();
	$('#bbUrlText').focus();
}
function bbUrl() {
	$('#bbUrl').hide();
	$('#comment_text').replaceSelection('[url "'+$('#bbUrlUrl').val()+'"]'+$('#bbUrlText').val()+'[/url]');
	$('#comment_text').focus();
}

function bbImgShow() {
	$('#addURLtoComment').attr('class', 'addURLtoComment');
	$('#addIMGtoComment').attr('class', 'addIMGtoComment_hover');
	
	$('#bbUrl').hide();
	
	$('#bbImgUrl').val('');
	$('#bbUpload').attr('disabled', false);
	$('#bbImg').show();
	$('#bbImgUrl').focus();
}
function bbImg() {
	$('#bbImg').hide();
	$('#comment_text').replaceSelection('[img "'+$('#bbImgUrl').val()+'"]\n');
	$('#comment_text').focus();
}

function SetPostType(post_id, type) {
	$.get(
		'/ajax.php',
		{
			'post': post_id,
			'type': type
		},
		function(data) {
			var c = 'BlockLove';
			switch(type) {
				case 1: c = 'BlockLove'; break;
				case 2: c = 'BlockProblem'; break;
				case 3: c = 'BlockIdea'; break;
				case 4: c = 'BlockQuestion'; break;
			}
			$('#post'+post_id).attr('class', c);
		}
	);
}

function SetPrivate(post_id) {
	$.get(
		'/ajax.php',
		{
			'private': post_id
		},
		function(data) {
			if(data == 1) {
				$('#private'+post_id).text('private post');
				$('#private'+post_id+'_popup').text('to public');
			}
			if(data == 0) {
				$('#private'+post_id).text('public post');
				$('#private'+post_id+'_popup').text('to private');
			}
		}
	);
}

function MergeSearch(project, search, except, section) {
	$.getJSON(
		'/ajax.php',
		{
			'project': project,
			'search': search,
			'except': except
		},
		function(obj) {
			switch(section) {
				case 1:
					$('#mp_filter_result_block').html('');
					if(search) {
						for(r in obj.search_result) {
							$('#mp_filter_result_block').append('\
								<label><input type="radio" name="post" value="'+obj.search_result[r]['post_id']+'" />'+obj.search_result[r]['title']+'</label>\
							');
						}
					}
					break;
				case 2:
					$('#mp_filter_result_block').html('');
					if(search) {
						for(r in obj.search_result) {
							$('#mp_filter_result_block').append('\
								<label class="mp_topictobemerge" id="postBottom'+obj.search_result[r]['post_id']+'"><input type="checkbox" value="'+obj.search_result[r]['post_id']+'" onChange="MergeTop('+obj.search_result[r]['post_id']+', \''+obj.search_result[r]['title']+'\')" />'+obj.search_result[r]['title']+'</label>\
							');
						}
					}
					break;
			}
		}
	);
}

$('body').click(function(e) {
	var c = $(e.target).attr('class');

	if(
		c != 'SSMCbButton' &&
		c != 'SSMCbAccepted' &&
		c != 'SSMCbDeclined' &&
		c != 'SSMCbScheduled' &&
		c != 'SSMCbDone' &&
		c != 'SSMCbRemove' &&
		c != 'StatusSMenuSep' &&
		c != 'StatusSMenuCb'
		
	) {
		$('.StatusSMenu').hide();
	}
	
	if(
		c != 'changeVoteStatus' &&
		c != 'typeMenuSep' &&
		c != 'typeMenuCb'
	) {
		$('.typeMenu').hide();
	}
	
	if(
		c != 'LBML_LoginProfile' &&
		c != 'profMenuSep' &&
		c != 'profMenuCb'
	) {
		$('.profMenu').hide();
	}
});
