$j(function(){

	$j('.btn').hover(
		function(){ 
			$j(this).addClass("ui-state-hover"); 
		},
		function(){ 
			$j(this).removeClass("ui-state-hover"); 
		}
	);
	
	if($j('.timeBody').get(0)){
		var height = document.documentElement.offsetHeight - 155;
		$j('.timeBody').get(0).style.height = height+'px';
	}
	
});

function getPosition(el){
    var left = 0;
	var top = 0;
	var node = el;
	while(node.nodeName != 'BODY' && node.nodeName != 'HTML'){
		left += node.offsetLeft;
		top += node.offsetTop;
		node = node.offsetParent;
	}
	return {'top':top, 'left':left};
};

var Locale = {
	get: function(str){
		return str;
	}
};

// Variable to keep all callback functions which needed to call when dashboard is loaded. This functionality needed for plugins.
var eventRenderCallBacks = [];

function getDaysCount(date){
	
}

function createDatePickerConnection(first, second){
	$j('#'+first).change(function(){
		var firstDate = $j('#'+first).datepicker('getDate');
		var secondDate = $j('#'+second).datepicker('getDate');
		if(secondDate && firstDate.getTime() > secondDate.getTime()){
	   		$j('#'+second).val(this.value);
	   	}
	});
	
	$j('#'+second).change(function(){
		var firstDate = $j('#'+first).datepicker('getDate');
		var secondDate = $j('#'+second).datepicker('getDate');
		if(firstDate && firstDate.getTime() > secondDate.getTime()){
	   		$j('#'+first).val(this.value);
	   	}
	});
}

var lightBoxInited = false;
function attachLightBox(className){
	if(typeof Lightbox != 'undefined'){
		if(!lightBoxInited){
			Lightbox.initialize();
			lightBoxInited = true;
		}
		
		$j('.'+className+':not(.lightbox-processed)').addClass('lightbox-processed').click(function(e) {
			if (Lightbox.disableCloseClick) {
			  $j('#lightbox').unbind('click');
			  $j('#lightbox').click(function() { Lightbox.end('forceClose'); } );
			}
			Lightbox.start(this, false, false, false, false);
			if (e.preventDefault) { e.preventDefault(); }
			return false;
	    });
	}
}

function handlePageHelpHint () {
	var hint = $j("<span class='hint-text ui-widget ui-widget-content ui-corner-all' />")
			     .html($j('.page_help').html());
			     
	$j("<span class='ui-state-highlight ui-corner-all hint-container' />")
	  .append("<span class='ui-icon ui-icon-info' />")
	  .append(hint)
	  .appendTo($j('.page_header'));
	
	//$j('.page_help').css('display', 'none');		
}

if (typeof(LANG) == 'undefined') {
	LANG = {};
}

var Locale = {
	get: function() {
		var str = arguments[0];
		if(LANG[str]) {
			str = LANG[str];
		}

		if(arguments.length > 1) {
			arguments[0] = str;
			str = sprintf.apply(null, arguments);
		}

		return str;
	}
};

function translateArray(strings) {
	for(var i = strings.length - 1; i > -1; i--) {
		if (typeof(strings[i]) == 'function') {
			continue;
		}
		strings[i] = Locale.get(strings[i]);
	}

	return strings;
}

function translateDialogButtons(buttonsData) {
	var result = {};
	for(var key in buttonsData) {
		if(typeof key != 'function') {
			result[Locale.get(key)] = buttonsData[key];
		}
	}

	return result;
}

Date.make = function (mysqlDate) {
	var data = mysqlDate.split('-');
	return new Date(data[0], parseInt(data[1], 10) - 1, parseInt(data[2], 10));
}

Date.prototype.format = function () {
	if (typeof(calendarFormat) == 'undefined') {
		var calendarFormat = 'd.mm.yy';
	}
	return calendarFormat.replace('d', fill0(this.getDate())).replace('mm', fill0(this.getMonth() + 1)).replace('yy', this.getFullYear());
}

function fill0(val) {
	val = parseInt(val, 10);
	if (val < 10) {
		return '0' + val;
	}
	return val;
}

function cancelEvent(el, company, id, code) {
	if(window.confirm(Locale.get('Are you sure you want to cancel this event?'))) {
		$j.getJSON('/user/cancel-event/company/' + company + '/event_id/' + id + '/code/' + code,
			function(result) {
				if(result == 1) {
					el.className = 'state';
					el.innerHTML = Locale.get('Inactive');
				}
			});
	}
}

function getTimeData(hours, minutes) {
	if (typeof(TIMEFORMAT) != 'undefined' && TIMEFORMAT == '12hr') {
		var pref = 'AM';
		if (hours >= 12) {
			if (hours > 12) {
				hours = hours - 12;
			}
			pref = 'PM';
		}
		if (hours == 0) {
			hours = 12;
		}
		return {
			'hour': fill0(hours),
			'minutes': minutes,
			'pref': pref
		};
	}
	return {
		'hour': fill0(hours),
		'minutes': minutes
	}
}

function getTimeDataByTime(time) {
	time = time.toUpperCase();
	var addHour = 0;
	if (time.search('PM') != -1) {
		addHour = 12;
	}
	time = time.replace('PM', '').replace('AM', '').trim();
	time = time.split(':');
	
	return getTimeData(parseInt(time[0], 10) + addHour, parseInt(time[1]));
}

function formatTime(value) {
	var timeData = getTimeDataByTime(value);
	return timeData.hour + ':' + fill0(timeData.minutes) + (timeData.pref ? ' ' + timeData.pref : '');
}
