/**
 * @author Gurkan Oluc
 * @namespace eo
 *
 * Main Class and Common functions
 *
 */
/**
 * Temporary translate function
 * */
function __(string)
{
	return string;
}

function _log(log)
{
    if(typeof console != "undefined")
        console.log(log);
}


/**
 * Main class
 */
var eo = {};

/**
 * Common functions
 */
eo.common = {
	ajaxModalBox : null,
	waitingModalBox : null,
	baseUrl : '',
	currentUrl : '',
	shouldLoginModalEnabled : false,
    mouseTop: 0
};

/**
 * Modalbox function
 * @param title		Title of modalbox
 * @param message	Content of modalbox
 * @param type		Content type of modalbox
 * @param timeOut	timeOut for closing modal box
 * @param url		Url to redirect user after modal box hide
 * @param options	Modalbox options
 */
eo.common.modal = function(title, message, type, timeOut, url, options)
{
	if(typeof type == 'undefined')
		type = 'normal';

	if(typeof title != 'undefined' && title.length == 0 &&  type == 'error')
		title = eo.config.errorMessageTitle;

	options = $.extend({
		title: title,
		closeText: __('Kapat'),
		draggable: false,
		modal: true
	}, options || {});

	if(type == 'error')
	{
		options.closeable = false;
	}
	
	var html = '<div class="'+ type +'">'+ message +'</div>';

	if(type == 'waiting')
		html += '<div class="loading"><img src="/images/loading.gif" /></div>';

	var modal = new Boxy(html, options);
	
	if(timeOut)
	{
		setTimeout(function() {
			modal.hide(function() {
				if(url)
					document.location.href = eo.config.baseUrl + url;
			})
		}, timeOut * 1000);
	}
    
    
	return modal;
};


eo.common.ajaxModal = function(options)
{
	eo.common.get(options.url, options.data || {}, function(res) {
		if(res.result == 1)
		{
			if(typeof options.successCallback != 'undefined')
				options.successCallback.call(this, res);
			else
				eo.common.ajaxModalBox = eo.common.modal(options.title, res.content);
		}
		else
		{
			if(typeof options.errorCallback != 'undefined')
				options.errorCallback.call(this, res);
			else
				eo.common.ajaxModalBox = eo.common.modal('', res.message, 'error', 10);
		}
	});
};

/**
 * jQuery get ajax request
 *
 * @param url Url to request
 * @param data data to send withr request
 * @param successCallback success callback function
 */
eo.common.get = function(url, data, successCallback, options)
{
	var options = options || {};
	var showLoading = typeof options.showLoading == 'undefined' ? true : options.showLoading;
	showLoading && this._hideAllModalsShowWaitingModal();
	$.get(eo.config.baseUrl + url, data || {}, function(res) {
		showLoading && eo.common._hideWaitingModal();

		if(eo.common._checkUserLogin(res))
		{
			var callback = successCallback || eo.common._standartSuccessCallback
			callback.call(this, res);
		}

	}, 'json');
};

eo.common._checkUserLogin = function(r) {
	if(r.result == -1)
	{
		console.log('result -1');
		if(!eo.common.shouldLoginModalEnabled)
		{
			console.log('shouldLoginModalEnabled');
			eo.common.modal(__('Giriş Yapınız'), r.message, 'error');
			eo.common.shouldLoginModalEnabled = true;
		}
		return false;
	}
	return true;
};


/**
 * jQuery post ajax request
 *
 * @param url Url to request
 * @param data data to send withr request
 * @param successCallback success callback function
 */
eo.common.post = function(url, data, successCallback)
{
	this._hideAllModalsShowWaitingModal();
	$.post(eo.config.baseUrl + url, data || {}, function(res) {
		eo.common._hideWaitingModal();
		var callback = successCallback || eo.common._standartSuccessCallback
		callback.call(this, res);
	}, 'json');
};

/**
 * Asks to user
 * @param message	string		message of question
 * @param chocies	array		choices of question
 * @param callback	function	callback function
 */
eo.common.ask = function(message, choices, callback)
{
	return Boxy.ask(message, choices, callback);
}

/**
 * Standard ajax callback
 */
eo.common._standartSuccessCallback = function(res) {
	if(res.result)
		eo.common.modal(res.title, res.message, 'success', 3);
	else
		eo.common.modal('', res.message, 'error', 3)
};

/**
 * Displays waiting modal box
 */
eo.common._hideAllModalsShowWaitingModal = function() {
	this.ajaxModalBox && this.ajaxModalBox.hide();

	if(!this.waitingModalBox)
	{
		this.waitingModalBox = eo.common.modal('', __('Lütfen bekleyiniz'), 'waiting');
	}
	else
	{
		if(!this.waitingModalBox.isVisible()) {
			this.waitingModalBox.show();
		};
	}
};

/**
 * Hides waiting modal box
 */
eo.common._hideWaitingModal = function() {
	if(this.waitingModalBox)
		this.waitingModalBox.hide();
};


/**
 * Reloads page
 */
eo.common.reload = function()
{
    // We are not using reload method, it's causing problems at Firefox.
    document.location.href = eo.config.baseUrl + eo.config.currentUrl;
    
};

eo.common.redirect = function(url) {
    document.location.href = eo.config.baseUrl + url;
}


/**
 * Logging function
 */
eo.common.log = function(str) {
    if(typeof console != 'undefined')
        console.log(str);
}





