/**
// Copyright (C) 2009 - 2011 Mike Feng Jinglong
//
// This program is a commercial software; you cannot redistribute it
// and/or modify it without the explicit consent from the author.
//
// All title and copyright in and to the Product is owned by Mike Feng
// and is protected by international copyright laws and treaty provisions.
// Mike Feng retains all ownership rights in the Product.
// The copyright notice must be reproduced and included on any copy
// that is expressly permitted under this Agreement.
**/

window.addEvent("domready", function() {
	Paginate();
	Sexy = new SexyAlertBox({
		OverlayStyles: {
			"opacity": 0.1
		}
	});
	$$(".hasTip").each(function(element,index) {  
		var title = element.getProperty("title");
		if (title == null) return;
		var content = title.split("::");  
		element.store("tip:title", content[0]);  
		element.store("tip:text", content[1]);
	});  
	var JTooltips = new Tips($$(".hasTip"));
});


function resetField(el, e, defaultvalue) {
	if (e == "focus" && el.value == defaultvalue) {
		el.value = "";
		el.removeClass("blurfield");
	}
	else if (e == "blur" && el.value == "") {
		el.value = defaultvalue;
		el.addClass("blurfield");
	}
}

function checkFieldCount(el, maxcount, count_el) {
	var content = el.value;
	var difference = maxcount - content.length;
	if (difference < 0) {
		el.value = content.substring(0, maxcount);
		difference = '0';
	}
	count_el.innerHTML = difference;
}

function cat1Select(ask_question) {
	$('cats1-hidden').value = $('cats1-select').value;
	$('cats2-hidden').value = '';
	$('cats3-hidden').value = '';
	$('cats2-select').getChildren('option').each(function(el) {
		if (el.value == 0)
			el.setProperty("selected", "selected");
		else
			el.destroy();
	});
	$('cats3-select').getChildren('option').each(function(el) {
		if (el.value == 0)
			el.setProperty("selected", "selected");
		else
			el.destroy();
	});
	if (ask_question) {
		var parent = $('cat-'+$('cats1-select').value).getProperty('parent');
		if (parseInt(parent) != 1)
			return false;
	}
	if ($('cats1-select').value == 0)
		return false;

	var url = saurlraw+"&view=ask&task=getsubcategories&catid="+$('cats1-select').value;
	new Request.JSON({
		'url': url,
		'method': 'get',
		onRequest: function() {
			
		},
		onSuccess: function(responseJson, responseText) {
			responseJson.each(function(el) {
				var category = new Element('option');
				category.setProperty('id', 'cat-'+el.id);
				category.setProperty('value', el.id);
				if (el.parent == '1') {
					category.setProperty('parent', '1');
					category.setProperty('html', el.name+' &gt;');
				}
				else
					category.setProperty('html', el.name);
				category.inject($('cats2-select'));
			});
		},
		onFailure: function(){
			Sexy.error("<strong>"+SA_AJAX_ERROR+"</strong>");
			(function() { $('BoxAlertBtnOk').focus(); }).delay(800);
		}
	}).send();
}

function cat2Select(ask_question) {
	$('cats2-hidden').value = $('cats2-select').value;
	$('cats3-hidden').value = '';
	$('cats3-select').getChildren('option').each(function(el) {
		if (el.value == 0)
			el.setProperty("selected", "selected");
		else
			el.destroy();
	});
	if (ask_question) {
		var parent = $('cat-'+$('cats2-select').value).getProperty('parent');
		if (parseInt(parent) != 1)
			return false;
	}
	if ($('cats2-select').value == 0)
		return false;

	var url = saurlraw+"&view=ask&task=getsubcategories&catid="+$('cats2-select').value;
	new Request.JSON({
		'url': url,
		'method': 'get',
		onRequest: function() {
			
		},
		onSuccess: function(responseJson, responseText) {
			responseJson.each(function(el) {
				var category = new Element('option');
				category.setProperty('id', 'cat-'+el.id);
				category.setProperty('value', el.id);
				if (el.parent == '1') {
					category.setProperty('parent', '1');
					category.setProperty('html', el.name+' &gt;');
				}
				else
					category.setProperty('html', el.name);
				category.inject($('cats3-select'));
			});
		},
		onFailure: function(){
			Sexy.error("<strong>"+SA_AJAX_ERROR+"</strong>");
			(function() { $('BoxAlertBtnOk').focus(); }).delay(800);
		}
	}).send();
}

var Paginate = (function(){
	var PaginateSlider = new Class({
		
		Extends: Slider,
		
		initialize: function(element, knob, options){
			this.parent(element, knob, options);
			
			this.drag.addEvents({
				beforeStart: (function(){
					this.isDropping = true;
				}).bind(this),
				complete: (function(){
					this.isDropping = false;
				}).bind(this)
			});
		},
		
		clickedElement: function(event){
			if(this.isDropping) return;
			
			this.parent(event);
		}
		
	});

	return function(){
		$$('div.paginate').each(function(el){
			if(el.retrieve('paginate')) return;
			
			el.store('paginate', true);
			var link = el.getElement('a.link'),
				per = el.getElement('span.per').get('text').toInt(),
				page = el.getElement('span.current').get('text').toInt()-1,
				type = el.getElement('span.type').get('text'),
				init = true;
			
			new PaginateSlider(el.getElement('div.slider'), el.getElement('div.knob'), {
				wheel: false,
				offset: 10,
				steps: link.get('text').toInt()-1,
				onChange: function(step){
					el.getElement('span.current').set('text', step+1);
				},
				onComplete: function(step){
					var previous = el.getElement('span.previous').get('text').toInt();
					var current = el.getElement('span.current').get('text').toInt();
					if (init)
						init = false;
					else {
						switch (type) {
							case 'category':
								changeCategoryPage(type, current);
								break;
							case 'dashboard-answers':
								changeDashboardAnswers(type, current);
								break;
							case 'dashboard-questions':
								changeDashboardQuestions(type, current);
								break;
							case 'dashboard-starred':
								changeDashboardStarred(type, current);
								break;
							case 'dashboard-watchlist':
								changeDashboardWatchlist(type, current);
								break;
						}
					}
					el.getElement('span.previous').set('text', current);
				}
			}).set(page);
		});
	};
})();

function changeCategoryPage(type, page) {
	var output = $('questions-list-output');
	var url = saurlraw+"&view=category&catid="+$('catid').value+"&task="+$('task').value+"&page="+page;
	var myReq = new Request({
		'url': url,
		'method': 'get',
		onRequest: function(){
			$('ajax-loading-'+type).setStyle('visibility','visible');
			output.fade('out');
		},
		onSuccess: function(responseText) {
			output.fade('hide');
			$('ajax-loading-'+type).setStyle('visibility','hidden');
			output.innerHTML = responseText;
			output.fade('in');
		},
		onFailure: function(){
			Sexy.error("<strong>"+SG_AJAX_ERROR+"</strong>");
			(function() { $('BoxAlertBtnOk').focus(); }).delay(800);
		}
	}).send();
}

function setStar(star_elem, qid, starred, qntools) {
	var url = saurlraw+"&view=question&task=setstar&qid="+qid;
	new Request.JSON({
		'url': url,
		'method': 'get',
		onRequest: function() {
			if (star_elem.hasClass('star')) {
				star_elem.setStyle('background-position', '0px -396px');
			}
			else if (star_elem.hasClass('starred') == true) {
				star_elem.setStyle('background-position', '0px -296px');
			}
		},
		onSuccess: function(JSON, responseText) {
			if (JSON.error == 1) {
				Sexy.error("<strong>"+JSON.message+"</strong>");
				(function() { $('BoxAlertBtnOk').focus(); }).delay(800);
				if (starred) {
					star_elem.setStyle('background-position', '');
					star_elem.removeClass('star');
					star_elem.addClass('starred');
				}
				else {
					star_elem.setStyle('background-position', '');
					star_elem.removeClass('starred');
					star_elem.addClass('star');
				}
			}
			else {
				(function() {
					if (qntools) {
						if (parseInt(JSON.count) == 0)
							$$('#interesting div.star-count').setProperty('html', '');
						else
							$$('#interesting div.star-count').setProperty('html', JSON.count);
						$('interesting-output').innerHTML = JSON.output;
					}
					else {
						if (parseInt(JSON.count) == 0)
							star_elem.getPrevious("div").setProperty('html', '');
						else
							star_elem.getPrevious("div").setProperty('html', JSON.count);
					}
					if (JSON.action == 'delete') {
						star_elem.setStyle('background-position', '');
						star_elem.removeClass('starred');
						star_elem.addClass('star');
					}
					else if (JSON.action == 'insert') {
						star_elem.setStyle('background-position', '');
						star_elem.removeClass('star');
						star_elem.addClass('starred');
					}
				}).delay(800);
			}
		},
		onFailure: function(){
			Sexy.error("<strong>"+SA_AJAX_ERROR+"</strong>");
			(function() { $('BoxAlertBtnOk').focus(); }).delay(800);
		}
	}).send();
}

function extendExpiration(qid) {
	var url = saurlraw+"&view=question&task=extend_expiration&qid="+qid;
	var myReq = new Request({
		'url': url,
		'method': 'get',
		onRequest: function(){

		},
		onSuccess: function(responseText) {
			$('extend-expiration').empty();
			$('extend-expiration').addClass('after-extend');
			Sexy.info("<strong>"+responseText+"</strong>");
			(function() { $('BoxAlertBtnOk').focus(); }).delay(800);
		},
		onFailure: function(){
			Sexy.error("<strong>"+SG_AJAX_ERROR+"</strong>");
			(function() { $('BoxAlertBtnOk').focus(); }).delay(800);
		}
	}).send();
}

function setQuestionNotify(qid) {
	var url = saurlraw+"&view=question&task=set_question_notify&qid="+qid;
	var myReq = new Request({
		'url': url,
		'method': 'get',
		onRequest: function(){
			$('tools-notify').getFirst('a').setStyle('cursor', 'progress');
		},
		onSuccess: function(responseText) {
			$('tools-notify').getFirst('a').setStyle('cursor', 'pointer');
			$('tools-notify').getFirst('a').innerHTML = responseText;
		},
		onFailure: function(){
			Sexy.error("<strong>"+SG_AJAX_ERROR+"</strong>");
			(function() { $('BoxAlertBtnOk').focus(); }).delay(800);
		}
	}).send();
}

function setWatchlist(qid) {
	var url = saurlraw+"&view=question&task=set_watchlist&qid="+qid;
	var myReq = new Request({
		'url': url,
		'method': 'get',
		onRequest: function(){
			$('tools-watchlist').getFirst('a').setStyle('cursor', 'progress');
		},
		onSuccess: function(responseText) {
			$('tools-watchlist').getFirst('a').setStyle('cursor', 'pointer');
			$('tools-watchlist').getFirst('a').innerHTML = responseText;
		},
		onFailure: function(){
			Sexy.error("<strong>"+SG_AJAX_ERROR+"</strong>");
			(function() { $('BoxAlertBtnOk').focus(); }).delay(800);
		}
	}).send();
}

function submitComment(qid) {
	var url = saurlraw+"&view=question&task=submitcomment";
	var myReq = new Request.JSON({
		'url': url,
		'method': 'post',
		'data': {
			'comment': $('commentbox').value,
			'qid': qid
		},
		onRequest: function(){
			$('comments-area').setStyle('cursor', 'progress');
		},
		onSuccess: function(JSON, responseText) {
			$('comments-area').setStyle('cursor', 'pointer');
			if (JSON.error == 1) {
				if ($("comment-error"))
					$("comment-error").destroy();
				var content_item = new Element('div', {
					'id': 'comment-error',
					'html': JSON.content
				});
				content_item.inject($('comments-list'), 'bottom');
				content_item.highlight('#CC0000');
			}
			else {
				if ($("comment-error"))
					$("comment-error").destroy();
				if ($("no-comments"))
					$("no-comments").destroy();
				var content_item = new Element('div', {
					'id': 'comment-'+JSON.cmtid,
					'class': 'comment-item',
					'html': JSON.content
				});
				content_item.inject($('comments-list'), 'top');
				$('comment').getFirst('span').innerHTML = JSON.count;
				content_item.highlight();
			}
			$('commentbox').value = '';
		},
		onFailure: function(){
			Sexy.error("<strong>"+SG_AJAX_ERROR+"</strong>");
			(function() { $('BoxAlertBtnOk').focus(); }).delay(800);
		}
	}).send();
}

function thumbThis(direction, aid) {
	$('thumb-response-'+aid).setProperty('html', '');
	var url = saurlraw+"&view=thumb&direction="+direction+"&aid="+aid;
	var myReq = new Request.JSON({
		'url': url,
		'method': 'get',
		onRequest: function(){
			$('thumb-'+direction+'-'+aid).setStyle('cursor', 'progress');
		},
		onSuccess: function(JSON, responseText) {
			$('thumb-'+direction+'-'+aid).setStyle('cursor', 'pointer');
			var thumbup = $('thumb-up-'+aid);
			var thumbup_span = thumbup.getFirst('span.hasTip');
			var thumbdown = $('thumb-down-'+aid);
			var thumbdown_span = thumbdown.getFirst('span.hasTip');
			if (JSON.error == 0) {
				thumbup.setProperty('onclick', '');
				thumbdown.setProperty('onclick', '');
				thumbup.setProperty('class', JSON.thumbup_class);
				thumbdown.setProperty('class', JSON.thumbdown_class);
				thumbup_span.setProperty('html', JSON.up);
				thumbdown_span.setProperty('html', JSON.down);

				thumbup_span.store("tip:title", '');  
				thumbup_span.store("tip:text", JSON.uptip);
				var JTooltips = new Tips(thumbup_span);

				thumbdown_span.store("tip:title", '');  
				thumbdown_span.store("tip:text", JSON.downtip);
				var JTooltips = new Tips(thumbdown_span);
			}
			$('thumb-response-'+aid).setProperty('html', JSON.message);
		},
		onFailure: function(){
			Sexy.error("<strong>"+SG_AJAX_ERROR+"</strong>");
			(function() { $('BoxAlertBtnOk').focus(); }).delay(800);
		}
	}).send();
}

function editAboutMe() {
	if ($('aboutme_edit').getStyle('display') == 'none') {
		$('aboutme').setStyle('display', 'none');
		$('aboutme_edit').setStyle('display', 'block');
	}
	else {
		$('aboutme').setStyle('display', 'inline');
		$('aboutme_edit').setStyle('display', 'none');
	}
}

function saveAboutMe() {
	var url = saurlraw+"&view=dashboard&task=editaboutme&user="+$('dashboard-uid').value;
	var myReq = new Request.JSON({
		'url': url,
		'method': 'post',
		'data': {
			'aboutme_text': $('aboutme_text').value
		},
		onSuccess: function(JSON, responseText) {
			$('aboutme').innerHTML = JSON.aboutme;
			$('aboutme_text').value = JSON.aboutme_text;
			$('aboutme').setStyle('display', 'inline');
			$('aboutme_edit').setStyle('display', 'none');
		},
		onFailure: function(){
			Sexy.error("<strong>"+SG_AJAX_ERROR+"</strong>");
		}
	}).send();
}
