$(document).ready(function() {
	$.ajaxSetup({
		scriptCharset: "UTF-8" ,
		contentType: "application/x-www-form-urlencoded; charset=UTF-8",
		type: "POST",
	});
	init();
});
function openPopup2(params){
	var options = {
		autoOpen: (params.auto=='yes'?false:true),
		modal: true,
		width: (params.auto == 'yes'?'auto':(parseInt($(window).width())-100)),
		height: (params.auto == 'yes'?'auto':(parseInt($(window).height())-50)),
		close: function() {
			if(typeof(params.closeCallback) =='string' && params.closeCallback!='' && params.closeCallback!='undefined'){
				eval(closeCallback);
			}else if(typeof(params.closeCallback) =='function' && params.closeCallback!='' && params.closeCallback!='undefined'){
				params.closeCallback();
			}
			closePopup(this);
		},
		buttons: { }
	};
	options.title = params.title;
	options.link = params.link;
	if(typeof params.obj == 'object'){
		options.title = params.obj.title;
		options.link = params.obj.href;
	}
	if(params.saveCallback != 'undefined' && params.saveCallback != ''){
		options.buttons.Запиши = function(){
			if(typeof(params.saveCallback) =='string'){
				eval(params.saveCallback);
			}else if(typeof(params.saveCallback) =='function'){
				params.saveCallback();
			}
		}
	}
	options.buttons.Затвори = function() {
		if(typeof(params.closeCallback) =='string' && params.closeCallback!='' && params.closeCallback!='undefined'){
			eval(params.closeCallback);
		}else if(typeof(params.closeCallback) =='function' && params.closeCallback!='' && params.closeCallback!='undefined'){
			params.closeCallback();
		}
		closePopup(this);
	}
	
	$('<div rel="popup"></div>')
	.load(options.link, function(){
			if( params.auto=='yes'){
				$(this).dialog('open');
			}
			init(this);
		})
	.dialog(options);
	return false;
}

function openPopup(obj, str, auto, closeCallback){
	var link = '';
	var title = '';
	var width = (auto == 'yes'?'auto':(parseInt($(window).width())-100));
	var height = (auto == 'yes'?'auto':(parseInt($(window).height())-50));
	if(typeof obj == 'object'){
		link = obj.href;
		title = obj.title;
	}else if(typeof obj == 'string'){
		link = obj;
		title = str;
	}
	$('<div rel="popup"></div>')
	.load(link, function(){
			if(auto == 'yes'){
				$(this).dialog('open');
			}
			init(this);
		})
	.dialog({
		autoOpen: (auto=='yes'?false:true),
		title: title,
		width: width,
		height: height,
		modal: true,
		stack: true,
		close: function() {
			if(typeof(closeCallback) =='string' && closeCallback!='' && closeCallback!='undefined'){
				eval(closeCallback);
			}else if(typeof(closeCallback) =='function' && closeCallback!='' && closeCallback!='undefined'){
				closeCallback();
			}
			closePopup(this);
		},
		buttons: {
			'Затвори': function() {
				if(typeof(closeCallback) =='string' && closeCallback!='' && closeCallback!='undefined'){
					eval(closeCallback);
				}else if(typeof(closeCallback) =='function' && closeCallback!='' && closeCallback!='undefined'){
					closeCallback();
				}
				closePopup(this);
			}
		}
	});
	return false;
}

function initForms(_dom){
	$(_dom).find('form').each(function(){
		if($(this).attr('rel')=='ajax'){
			var target;
			if(this.target){
				target = this.target;
			}else{
				target = null;
			}
			$(this).ajaxForm({
	            target: target,
	            contentType: "application/x-www-form-urlencoded; charset=UTF-8",
				scriptCharset: "UTF-8" ,
	            type: $(this).attr('method'),
	            beforeSubmit: function(formArray, jqForm){
	            	formArray.push(
	            		{	name:'fuckIE', value:(new Date().valueOf()) },
						{	name: 'ajax',  value: '1'}
					);
					// custom case in the notes
					if (typeof($('#submitArray').val()) != 'undefined' ){
						var arr = Array();
						arr = $('#submitArray').val().split('&');
						for(i=0;i<arr.length;i++){
							var strArr = arr[i].split('=');
							formArray.push(
								{name:strArr[0], value:strArr[1]},
								{name:'label_'+strArr[1], value:$('#tabOrderId-'+strArr[1]).children().attr('innerHTML')}
							);
						}
					}
	            },
	            success: function(){
	            	init(target);
	            }
	        });
        }
	});
}

function init(_dom){
	_dom = _dom != undefined ? _dom : 'body';
	initForms(_dom);
	initButtons(_dom);
}

function initButtons(_dom){
	$(".button, .icon", _dom).each(function(){
		if($(this).hasClass('button')){
			if( $(this).hasClass('add') ){
				$(this).button({ icons: { primary: 'ui-icon-circle-plus'} });
			}else if( $(this).hasClass('edit') ){
				$(this).button({ icons: { primary: 'ui-icon-pencil'} });
			}else if( $(this).hasClass('save') ){
				$(this).button({ icons: { primary: 'ui-icon-disk'} });
			}else if( $(this).hasClass('del') ){
				$(this).button({ icons: { primary: 'ui-icon-circle-close'} });
			}else if( $(this).hasClass('note') ){
				$(this).button({ icons: { primary: 'ui-icon-note'} });
			}else if( $(this).hasClass('mail') ){
				$(this).button({ icons: { primary: 'ui-icon-mail-closed'} });
			}else{
				$(this).button();
			}
		}
		if($(this).hasClass('icon')){
			if( $(this).hasClass('add') ){
				$(this).button({ icons: { primary: 'ui-icon-circle-plus'}, text: false });
			}else if( $(this).hasClass('edit') ){
				$(this).button({ icons: { primary: 'ui-icon-pencil'}, text: false });
			}else if( $(this).hasClass('del') ){
				$(this).button({ icons: { primary: 'ui-icon-circle-close'}, text: false });
			}else if( $(this).hasClass('contact') ){
				$(this).button({ icons: { primary: 'ui-icon-contact'}, text: false });
			}else if( $(this).hasClass('note') ){
				$(this).button({ icons: { primary: 'ui-icon-note'}, text: false });
			}else if( $(this).hasClass('calendar') ){
				$(this).button({ icons: { primary: 'ui-icon-calendar'}, text: false });
			}else if( $(this).hasClass('home') ){
				$(this).button({ icons: { primary: 'ui-icon-home'}, text: false });
			}else if( $(this).hasClass('flag') ){
				$(this).button({ icons: { primary: 'ui-icon-flag'}, text: false });
			}else if( $(this).hasClass('alert') ){
				$(this).button({ icons: { primary: 'ui-icon-alert'}, text: false });
			}else if( $(this).hasClass('save') ){
				$(this).button({ icons: { primary: 'ui-icon-disk'}, text: false });
			}else{
				$(this).button({text: false});
			}
			
			
		}
	});
}


function ask(question, trueCallback, falseCallback){
	$('<div rel="confirm"></div>')
	.attr('innerHTML',question)
	.dialog({
		resizable: false,
		modal: true,
		buttons: {
			OK: function() {
				if(typeof(trueCallback) =='string' && trueCallback!='' && trueCallback!='undefined'){
					eval(trueCallback);
				}else if(typeof(trueCallback) =='function' && trueCallback!='' && trueCallback!='undefined'){
					trueCallback();
				}
				closePopup(this);
			},
			Cancel: function() {
				if(typeof(falseCallback) =='string' && falseCallback!='' && falseCallback!='undefined'){
					eval(falseCallback);
				}else if(typeof(falseCallback) =='function' && falseCallback!='' && falseCallback!='undefined'){
					falseCallback();
				}
				closePopup(this);
			}
		}
	});
	return false;
}

function closePopup(obj){
	if(typeof(obj) == 'Object'){
		$(obj).dialog('destroy');
		$(obj).remove();
	}else{
		var popup = getTopPopup();
		$(popup).dialog('destroy');
		$(popup).remove();
	}
}

function getTopPopup(){
	var index = 0;
	var popup;
	var popupContainer;
	$('.ui-dialog').each(function(){
		var tmp_index = $(this).css('z-index');
		if(parseInt(tmp_index)>parseInt(index)){
			index = tmp_index;
			popup = this;
		}
	});
	$('.ui-dialog-content', popup).each(function(){
		popupContainer = this;
	});
	return popupContainer;
}
function openSimplePopup(obj, str, auto){
	var link = '';
	var title = '';
	var width = (auto == 'yes'?'auto':(parseInt($(window).width())-100));
	var height = (auto == 'yes'?'auto':(parseInt($(window).height())-50));
	if(typeof obj == 'object'){
		link = obj.href;
		title = obj.title;
	}else if(typeof obj == 'string'){
		link = obj;
		title = str;
	}
	$('<div rel="popup"></div>')
	.load(link, function(){
			if(auto == 'yes'){
				$(this).dialog('open');
			}
			init(this);
		})
	.dialog({
		autoOpen: (auto=='yes'?false:true),
		title: title,
		width: width,
		height: height,
		modal: true,
		stack: true,
		close: function() {
			closePopup(this);
		}
	});
	return false;
}

function UILoad(element, url, querystring, callback){
	$.ajax({
		url:url,
		contentType: "application/x-www-form-urlencoded; charset=UTF-8",
		scriptCharset: "UTF-8" ,
		data: querystring,
		type: "POST",
		dataType: 'html',
		success : function (html){
			$(element).html(html);
			if (typeof(callback) == 'string') {
				try{ eval(callback); } catch(e) { }
			} else if (typeof(callback) == 'function') {
				callback();
			}
		}
	});
}
