//commom.jquery.js
$(document).ready(function() {
  var btnGroup = $("button");
  $(btnGroup).click(function() {
    btnGroup.attr("disabled",true);
    var href=$(this).attr('href'),role=$(this).attr('role');
//$.log('data');
    if( href ) { self.location = href; return true; }
    var fields = $(":input",'#'+role).serializeArray();
        fields[fields.length] = { name: 'active',   value: this.id };
        fields[fields.length] = { name: 'selected', value: role };
       //$.log(fields);
    $.ajaxSetup({url:"ajax/ajax-request.php",type:"POST",dataType:"json",timeout:5000});
    $.ajax_host(fields);
    btnGroup.attr("disabled",false);
    return false;
  });
$.ajax_host = function(fields) {
  $.ajax({ data: fields })
   .complete(function(data) {
     switch( data.statusText ) {
       case 'timeout':  //status = 0, statusText = timeout
         $.dialog_panel('ajax error','系統忙碌中，請稍後再試...');
         break;
       case 'parsererror': // status = 200 statusText = parsererror responseText = 訊息
         //$.dialog_panel(data.responseText.dialog);
         $.log(data.responseText);
         break;
       case 'success':  // status = 200 statusText = parsererror responseText = 訊息
         //$.dialog_panel(data.responseText);
         if( data.responseText !== 'null' ) {
         	 responseText = $.parseJSON(data.responseText);
             switch( responseText.status ) {
             //case 301: window.location = responseText.href; break;
             case 301:  window.location = responseText.href; break;
             default:
               if( responseText.dialog ) { $.dialog_panel(responseText.dialog); }
               break;
           }
         } else {
         	 $.dialog_panel('system error','responseText is null...');
         }
       break;
     }
  });
};

$("#announce-content").accordion({
  header: 'div#ui-accordion-entry',
  icons: { header: "ui-icon-circle-arrow-e", headerSelected: "ui-icon-circle-arrow-s"},
  collapsible: true, // 點擊開關(指定選項是否可點擊關閉 )
 	active:     false, // 開啟指定內容
 	autoHeight: false, // 保持所有選項高度一致
  alwaysOpen: false, // 始終開啟()
  //navigation: true ,
  animated:   false
});

$.dialog_panel = function(p) {
	if( p.status ) {
  	$('.status-bar').html(p.message);
  } else {
	  p = $.extend({  title: '提示訊息',
                    modal: true,
                   height: 'auto',
                 minWidth: 650,
                resizable: false,
                  buttons: [{ text: "關閉", click: function() { $(this).dialog("close"); } }],
                    close: function() { $(this).html(""); }
        }, p || {});
    $('#dialog').dialog("destroy");
    $('#dialog').html(p.message);
    $('#dialog').dialog(p);
  }
};
// http://api.jquery.com/jQuery.parseJSON/
var utf8_decode = function (utftext) {
  var string = '';
  var i = 0;
  var c = c1 = c2 = 0;
  while (i < utftext.length) {
    c = utftext.charCodeAt(i);
    if (c < 128) {
      string += String.fromCharCode(c);
      i++;
    } else if ((c > 191) && (c < 224)) {
      c2 = utftext.charCodeAt(i + 1);
      string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
      i += 2;
    } else {
      c2 = utftext.charCodeAt(i + 1);
      c3 = utftext.charCodeAt(i + 2);
      string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
      i += 3;
    }
  }
  return string;
};
jQuery.fn.reset = function() {
	this.each(function(){
		if($(this).is('form')) {
			var button = jQuery(jQuery('<input type="reset" />'));
			button.hide();
			$(this).append(button);
			button.click().remove();
		} else if($(this).parent('form').size()) {
			var button = jQuery(jQuery('<input type="reset" />'));
			button.hide();
			$(this).parent('form').append(button);
			button.click().remove();
		} else if($(this).find('form').size()) {
			$(this).find('form').each(function(){
				var button = jQuery(jQuery('<input type="reset" />'));
				button.hide();
				$(this).append(button);
				button.click().remove();
			});
		}
	})
	return this;
};


});


