var DPopup =
{
  'bgColor': '#fff',
  'bgOpacity': 0.7,
  'closeCallBack':function(){},
	'_this':this,

  'Init': function(){
	  $('body').append('<div class="popup-bg"></div>');
    $(window).resize(function () { DPopup.resizePopupBg(); });
    $('.popup').resize(function () { centerPopup($(this)); });
  },
  'resizePopupBg': function(){
    $('.popup-bg').css('width', $(document).width());
    $('.popup-bg').css('height', $(document).height());	  
  },
  'centerPopup': function(popup){
    popup.css('left', ( $(document).width() / 2 - popup.outerWidth() / 2 ) + 'px');

    if( ( $(window).height() - popup.outerHeight() ) < 0 )
    {
      popup.css('top', ( (10 + $(window).scrollTop()) + 'px'));
    }
    else
    {
      popup.css('top', ( ( ( $(window).height() - popup.outerHeight() ) / 2 ) + $(window).scrollTop()) + 'px');
    }
  },
  'Create': function(id, options){
    this.Remove(id);
    var options = jQuery.extend({
        width: '600px',
        addClass: '',
	      hideTime: 0,
        CloseButton: true,
        closeCallBack: function(result){}
      }, options);

    $html = '    <div class="popup" id="'+id+'">' +
	          '     <div class="popup-page"></div> '+
	          '     <div class="actions"></div> '+
            '    </div>';
    $('body').append($html);

    $('#'+id).css({
      width: options.width
    });
    $('#'+id).addClass(options.addClass);
    if(options.CloseButton)
    {
	    var a = $('<a class="popup-close"  href="javascript:void(0)"></a>');
	    a.click(function(){
		    DPopup.Close(id, options.closeCallBack);
	    });

      $('#'+id+'').append(a);
    }

	  if(options.hideTime > 0)
	  {
		  setTimeout(function(){
			  DPopup.Close(id);
		  },options.hideTime);
	  }
  },
  'CreateConfirm': function(id, options){
    this.Remove(id);
    var options = jQuery.extend({
        width: '500px',
        addClass: '',
        CloseButton: true,
        title: 'Kita Confirm',
        description: 'Confirm description',
        bYesText: 'yes',
        bNoText: 'no',
        feedback: function(result){}
      }, options);
    this.Create(id, {
      width: options.width,
      addClass: options.addClass,
      CloseButton: options.CloseButton
    });
    var popup = $('#'+id);

    var butNo = $('<a class="button b1" href="#">'+options.bNoText+'<div class="rounded-button-left"></div><div class="rounded-button-right"></div></a>').click(function(){ DPopup.Close(id);});
    var butYes = $('<a class="button b2" href="#" style="margin-left: 5px;">'+options.bYesText+'<div class="rounded-button-left"></div><div class="rounded-button-right"></div></a>').click(function(){ DPopup.Close(id); options.feedback(true) });

    var html = 	$('<div><h2>'+options.title+'</h2>'+'<p>'+options.description+'</p>'+'<div class="actions" style="margin-top: 5px;"></div></div>');

    $('.popup-page', popup).append(html);
    $('.actions', html).append(butNo).append(butYes);
  },

  'CreateAlert': function(id, options){
    this.Remove(id);
    var options = jQuery.extend({
        width: '500px',
        addClass: '',
        CloseButton: true,
        title: 'Kita Alert',
        description: 'Alert description',
        bOkText: 'Ok',
	      hideTime: 0,
        feedback: function(){}
      }, options);
    this.Create(id, {
      width: options.width,
      addClass: options.addClass,
      CloseButton: options.CloseButton,
	    hideTime: options.hideTime
    });
    var popup = $('#'+id);

    //var bOkText = $('<button class="pleft-button"><div class="right"><div>'+options.bOkText+'</div></div></button>').click(function(){ DPopup.Close(id); options.feedback() });
		var bOkText = $('<a class="b1" style="margin-left: 40%;" href="#">'+options.bOkText+'</a>').click(function(){ DPopup.Close(id); options.feedback() });
    var html = 	$('<div><h2>'+options.title+'</h2>'+'<p>'+options.description+'</p>'+'<div class="actions" style="text-align: center;"></div></div>');

    $('.popup-page', popup).append(html);
    $('.actions', html).append(bOkText);
  },

  'Open': function(id, params){
    var popup = $('#' + id);
    this.resizePopupBg();
	  if(params != undefined && params.no_bg == true)
	  {

	  }else
	  {
		  $('.popup-bg').show();
	  }
		if(params != undefined && params.position != undefined)
		{
			if(params.position == 'center')
			{
				this.centerPopup(popup);
			}
			if(params.position == 'relative')
			{
				if(params.obj != undefined)
				{
					params.obj.append(popup);
				}
				popup.css(params.css);
			}
		}else
		{
			this.centerPopup(popup);
		}
    popup.show();
    if( ( $(window).height() - popup.height() ) < 0 )
    {
      this.resizePopupBg();
    }
    $('#swf-content').hide();
  },
  'Text': function(id, text){
    var popup = $('#' + id);
    $('.popup-page', popup).empty();
    $('.popup-page', popup).append(text);
  },
  'Close': function(id, callback){
    $('.popup-bg').hide();
    $('#' + id).hide();
    $('#swf-content').show();
	  if(callback != undefined)
	  {
		  callback();
	  }
  },
  'Remove': function(id)
  {
    $('#' + id).remove();
  },
	ShowMsg: function(msg, type)
	{
		$('.popup .msg').removeClass('error').removeClass('notice').empty();
		$('.popup .msg').append(msg);
		if(type =='err')
		{
			$('.popup .msg').addClass('error');
		}
		if(type =='nc')
		{
			$('.popup .msg').addClass('notice');
		}
	},
	HideMsg: function()
	{
			$('.popup .msg').removeClass('error').removeClass('notice').empty();
	}

};

$(document).ready(function(){
	DPopup.Init();
});
