jQuery(window).bind('resize', function(){
	var h = jQuery(window).height();
	if(h>520)
	{
		jQuery("div.pop_big").css({height: h - 200});
		jQuery("div.pop_cont_metro").css({height: h - 255});
		jQuery("div.rez_lst").css({height: h - 490});
		var h_head = jQuery("div.popup_items_tbl_h").height();
		var h_head = jQuery("div.form_scroll_h").height();
		if ((jQuery("div.errors:visible").length > 0)||(jQuery("div.messages:visible").length > 0))
		{
			var h_errors = jQuery("div.errors").height();
			var h_oks = jQuery("div.messages").height();
			jQuery("div.selection_tbl").css({height: h - 388 - h_errors - h_head});
			jQuery("div.popup_items_tbl").css({height: h- 460 - h_errors - h_head});
			jQuery("div.popup_items_tbl").css({height: h- 500 - h_oks - h_head});
			jQuery("div.form_scroll").css({height: h - 280 - h_errors - h_head});
			jQuery("div.form_scroll").css({height: h - 280 - h_oks - h_head});
		}
		else
		{
			jQuery("div.selection_tbl").css({height: h - 371 - h_head});
			jQuery("div.popup_items_tbl").css({height: h- 440 - h_head});
			jQuery("div.form_scroll").css({height: h - 200 - h_head});
		}
		jQuery("div.pop_big .tbl_grid_list").css({height: h - 220});
	}
	else
	{
		jQuery("div.pop_big").css({height: "320px"});
		jQuery("div.pop_cont_metro").css({height: "265px"});
		jQuery("div.rez_lst").css({height: "30px"});
		var h_head = jQuery("div.popup_items_tbl_h").height();
		var h_head = jQuery("div.form_scroll_h").height();
		if ((jQuery("div.errors:visible").length > 0)||(jQuery("div.messages:visible").length > 0))
		{
			var h_errors = jQuery("div.errors").height();
			var h_oks = jQuery("div.messages").height();
			jQuery("div.selection_tbl").css({height: 133 - h_errors - h_head});
			jQuery("div.popup_items_tbl").css({height: h- 450 - h_errors - h_head});
			jQuery("div.popup_items_tbl").css({height: h- 620 - h_oks - h_head});
			jQuery("div.form_scroll").css({height: h - 200 - h_errors - h_head});
			jQuery("div.form_scroll").css({height: h - 200 - h_oks - h_head});
			//alert(h);
		}
		else
		{
			jQuery("div.selection_tbl").css({height: 150 - h_head});
			jQuery("div.popup_items_tbl").css({height: h- 380 - h_head});
			jQuery("div.form_scroll").css({height: h - 150 - h_head});
		}
		jQuery("div.pop_big .tbl_grid_list").css({height:"300px"});
	}
});
		  						   
(function($){
	$.fn.dialog = function (params){
		$(this).each(function(){
			
			if(typeof params == 'string'){
				$('div.pop_cont_in', this).empty().append(params);
			}else{
				
				
				var object = $(this)
					.appendTo('body')
					.addClass('pop_big');
				
				if(typeof(params) == 'object'){
					if(params.hidden){
						object.hide();
					}
				}
				
				/* conent of div */
				var content = $('<div>')
					.addClass('pop_cont')
					.append(
						$('<div class="pop_cont_in" >')
							.append($(object).children())
					)
					.appendTo(object);
				/* head */
				$('<div>').addClass("shw_l png_scale").prependTo(object);
				$('<div>').addClass("shw_r png_scale").prependTo(object);
				var head = $('<div>').addClass("pop_head").prependTo(object);
					$('<div>').addClass("pop_head_r png_for_ie").appendTo(head);
					$('<div>').addClass("pop_head_l png_for_ie").appendTo(head);
					var head_title_cont = $('<div>').addClass("pop_head_bg png_scale").appendTo(head);
						var title = $('<span>').appendTo(head_title_cont);
						$('<div class="exit_cross">').appendTo(head_title_cont).click(function(){
							$(object).trigger('close');
						});
				
				/* bottom */
				var bottom = $('<div>').addClass("pop_btm").appendTo(object);
					$('<div>').addClass("pop_btm_r png_for_ie").appendTo(bottom);
					$('<div>').addClass("pop_btm_l png_for_ie").appendTo(bottom);
					var footer_cont = $('<div>').addClass("pop_btm_bg png_scale").appendTo(bottom);			
				
				$(object).bind('close',function(){$(this).stop(true, true).fadeOut(100);});
				$(object).bind('open',function(){$(this).stop(true, true).fadeIn(300);});
				
				/* ajax load */
				var ajaxloader = $('<img>')
					.attr({src:'/images/ajax-loader_popup.gif','class':'ajax_loader'})
					.css({
						'float':'right',
						'position':'relative',
						'right':20,
						'top':10
					})
					.hide()
					.prependTo(head_title_cont)
				
				$(object).bind('sendAjax',function(){
					ajaxloader.show();
				});
				$(object).bind('loadAjax',function(){
					ajaxloader.hide();
				});
				
				if(typeof(params) == 'object'){
					if(params.title){
						title.text(params.title);
					}
					if(params.modal === true){
						var modal_popup = $('<div class="popup_modal">').css({"opacity":"0.4"}).hide().appendTo('body');
						$(object).bind('close', function(){
							modal_popup.hide();
						});
						$(object).bind('open', function(){
							modal_popup.show();
						});
						if(params.hidden){
							modal_popup.hide();
						}else{
							modal_popup.show();
						}
					}
				}
				
			}
		});
		
		$(window).trigger('resize');
		return $(this);

	};
})(jQuery)

