// include css
var e = document.createElement("link");
e.setAttribute("rel","stylesheet");
e.setAttribute("href","/jq_popwin.css");
try {
        document.getElementsByTagName('head')[0].appendChild(e);
} catch(z) {
        document.body.appendChild(e);
}

function load_pop(ptype,ptitle,pcontent,poptions) {

	var e = document.createElement('div');
	e.setAttribute("id","popwin");
	e.setAttribute("title",ptitle);
	document.body.appendChild(e);

	var dp_w = 300;
	var dp_h = 'auto';

	if (poptions.indexOf("p_width") != -1) {
		parts = poptions.split("p_width:");
		p_bits = parts[1].split(" ");
		p_w = p_bits[0];
		dp_w = p_w;
	}
	if (poptions.indexOf("p_height") != -1) {
		parts = poptions.split("p_height:");
		p_bits = parts[1].split(" ");
		p_h = p_bits[0];
		dp_h = p_h;
	}

	var e = document.createElement('div');
	e.setAttribute("id","popwidth");
	document.body.appendChild(e);
	$('#popwidth').css({ "display":"none"});
	$('#popwidth').html(dp_w);

	$('#popwin').dialog({ 
		modal: true, 
		position: 'top',
		draggable: false,
		height: dp_h,
		width: dp_w
	});

	if (poptions.indexOf('fadein') != -1) {
		$('.ui-dialog').css({ "display":"none" });
	}

	if (ptype == 'text') {
		$('#popwin').html(pcontent);
		do_load_pop(ptype,ptitle,pcontent,poptions);
	}

	if (ptype == 'ajax') {

		if (poptions.indexOf('transition') != -1) {
			var e = document.createElement('div');
			e.setAttribute("id","tmp_popwin");
			document.body.appendChild(e);
			$('#tmp_popwin').css({'display':'none','float':'left','width':'auto'});
			$('#popwin').css({
				'background-image':'url(http://libs.zaftr.com/gfx/al-orange.gif)',
				'background-position':'center center',
				'background-repeat':'no-repeat'
			});
			$.post(pcontent,  ''  ,function(aResult) {
				$('#tmp_popwin').html(aResult);
				do_load_pop(ptype,ptitle,pcontent,poptions);
			});

		} else {
			$.post(pcontent,  ''  ,function(aResult) {
				$('#popwin').html(aResult);
				do_load_pop(ptype,ptitle,pcontent,poptions);
			});
		}
	}

	if (ptype == 'image') {

		if (poptions.indexOf('transition') != -1) {

			pop_loading(ptitle);

			var e = document.createElement('img');
			e.setAttribute("id","tmp_popwin");
			e.setAttribute("src",pcontent);
			document.body.appendChild(e);
			$('#tmp_popwin').css({'display':'none','border':'0','padding':'0','margin':'0'});
			$('#popwin').css({
				'background-image':'url(http://libs.zaftr.com/gfx/al-orange.gif)',
				'background-position':'center center',
				'background-repeat':'no-repeat'
			});
			$('#tmp_popwin').bind('load',function() {
				do_load_pop(ptype,ptitle,pcontent,poptions);
			});

		} else {
			$('#popwin').html('<img src='+pcontent+' border=0>');
			do_load_pop(ptype,ptitle,pcontent,poptions);
		}
	}

	$('#popwintype').html(ptype);

}

function pop_loading(ptitle) {

	do_load_pop('text',ptitle,'<img src=http://libs.zaftr.com/gfx/al-orange.gif>','');

}

function do_load_pop(ptype,ptitle,pcontent,poptions) {

	$('#popwin').bind('dialogclose', function(event, ui) {
		$('#popwin').dialog('destroy');
		var p = document.getElementById('popwin');
		document.body.removeChild(p);
	});

	if (poptions.indexOf('info') != -1) {
		$('.ui-dialog-titlebar').css({
			"background-image": 'url(http://libs.zaftr.com/gfx/icons/i_info.gif)',
			"background-position": '3px center',
			"background-repeat": 'no-repeat',
			"padding-left": '22px'
		});
	}
	if (poptions.indexOf('warning') != -1) {
		$('.ui-dialog-titlebar').css({
			"background-image": 'url(http://libs.zaftr.com/gfx/icons/i_warning.gif)',
			"background-position": '3px center',
			"background-repeat": 'no-repeat',
			"padding-left": '22px'
		});
	}
	if (poptions.indexOf('alert') != -1) {
		$('.ui-dialog-titlebar').css({
			"background-image": 'url(http://libs.zaftr.com/gfx/icons/i_alert.gif)',
			"background-position": '3px center',
			"background-repeat": 'no-repeat',
			"padding-left": '22px'
		});
	}
	if (poptions.indexOf('error') != -1) {
		$('.ui-dialog-titlebar').css({
			"background-image": 'url(http://libs.zaftr.com/gfx/icons/i_error.gif)',
			"background-position": '3px center',
			"background-repeat": 'no-repeat',
			"padding-left": '22px'
		});
	}
	if (poptions.indexOf('hidden') != -1) {
		$('.ui-dialog-titlebar-close').css({
			"display":"none"
		});
	} else {
		$('.ui-dialog-titlebar-close').css({ 
			"right"	: "15px"
		});
		$('.ui-dialog-titlebar-close').text();
		$('.ui-dialog-titlebar-close').html('<img src=http://libs.zaftr.com/gfx/pu_close.gif border=0>');
		if (poptions.indexOf('fadeout') != -1) {
			$('.ui-dialog-titlebar-close').unbind('click');
			$('.ui-dialog-titlebar-close').removeAttr('href');
			$('.ui-dialog-titlebar-close').bind('click',function() {
				$('#popwin').fadeOut('3000');
				$('.ui-dialog').fadeOut('3000',function() {
					close_pop();
				});
				
			});
		}
	}

	if (poptions.indexOf('fadein') != -1) {
		$('#popwin').fadeIn('3000');
		$('.ui-dialog').fadeIn('3000');
	}

	if (poptions.indexOf('scroll') != -1) {
		$('#popwin').fadeIn('3000');
		$('.ui-dialog-content').css({
			"overflow":"auto"
		});
	}

	if (poptions.indexOf('transition') != -1) {
		var t_w = $('#tmp_popwin').width();
		if (t_w > 980) {
			t_w = 980;
		}
		var t_h = $('#tmp_popwin').height();

		if (poptions.indexOf("pw_width") != -1) {
			parts = poptions.split("pw_width:");
			p_bits = parts[1].split(" ");
			p_h = p_bits[0];
			t_w = p_h*1;
		}
		if (poptions.indexOf("pw_height") != -1) {
			parts = poptions.split("pw_height:");
			p_bits = parts[1].split(" ");
			p_h = p_bits[0];
			t_h = p_h*1;
		}


		var e_w = 0;
		if (ptype == 'ajax' || ptype == 'image') {
			e_w = 10;
		}
		$('.ui-dialog').animate({
			'left':(($(window).width()/2) - (t_w/2)) - 25 +'px',
			'width':t_w+20+e_w+'px',
			'height':t_h+50+'px'
		});
		$('#popwin').animate({
			'width':t_w+e_w+'px',
			'height':t_h+'px'
		},
		function() {
			$('#popwin').css({
				'background-image':''
			});
			if (ptype == 'image') {
				var tphtml = $('#tmp_popwin').attr('src');
				$('#popwin').html('<img src='+tphtml+'>');
			} else {
				var thehtml = $('#tmp_popwin').html();
				$('#popwin').html(thehtml);
			}
			remove_tmp_popwin();
		});


	} else {
		reposition_pop();
	}

	$(window).bind('resize',function() {
		reposition_pop();
	});

}

function remove_tmp_popwin() {

	var w = document.getElementById('tmp_popwin');
	document.body.removeChild(w);

}

function reposition_pop() {

	var pw = $('body').width();
	var iw = $('.ui-dialog').width();
	var popw = $('#popwidth').html()*1;
	var t_w = $('.ui-dialog-content').width();
	var e_w = 0;
	var ptype = $('#popwintype').html();
	if (ptype == 'ajax' || ptype == 'image') {
		e_w = 10;
	}

	// hack for the width identifying on non-IE browser (oh, the irony)
	// and pop window width > half browser width
	h_br = navigator.userAgent;
//	if (h_br.indexOf("MSIE") == -1 && iw > pw/2) {
//		iw = iw/2;
//	}

	var nw = (pw/2) - (iw/2);

	$('.ui-dialog').css({ "left": nw+"px"});
//	$('.ui-dialog').css({ "width": popw+"px"});

//	$('#popwin').css({
//		'width':t_w+'px'
//	});

}

function close_pop() {

	$('#popwin').dialog('destroy');
	var p = document.getElementById('popwin');
	document.body.removeChild(p);
	var p = document.getElementById('popwidth');
	document.body.removeChild(p);
	//$(window).unbind('resize');


}

