/**
 * Copyright (c) www.mkMob.com. All Rights Reserved.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * mkMob Client Dialog Class.
 *
 * @author  Akeno Barrett
 * @version $Id dialog.js v1.0, 20-Mar-2010, 3:59 PM $
 * @package Dialog
 */
 
var Dialog = {
   /**
    * Server URI.
    */
   URI: null,
   
   /**
    * Type of dialog. 0 represents normal dialog, 1 represents large dialog.
    */
   dialogType: 0,
     
   /**
    * Sets the type of dialog to display
    */
   setType: function(type) {
      switch(type) {
         case 0:
            this.dialogType = type;
         break;
         case 1:
            this.dialogType = type;
         break;
         default:
            this.dialogType = 0;
         break;
      }
   }, 
   /**
    * Sets the title of the dialog
    */
   setTitle: function(title) {
      if(this.dialogType == 0) {
         document.getElementById('dialog-title').innerHTML = '<table cellspacing="0" cellpadding="0" width="100%"><tr><td>' + title + '</td><td><span style="float:right;visibility:hidden;" id="dialog-loading"><img src="/imgs/prog.gif" alt="Loading" /></span></td></tr></table>';   
      } else {
         document.getElementById('dialog-large-title').innerHTML = '<table cellspacing="0" cellpadding="0" width="100%"><tr><td>' + title + '</td><td><span style="float:right;visibility:hidden;" id="dialog-loading"><img src="/imgs/prog.gif" alt="Loading" /></span></td></tr></table>';
      }
   },

   /**
    * Sets body of the dialog. That is, the content returned by the server.
    */
   setBody: function(data) {
      if(this.dialogType == 0) {
         document.getElementById('dialog-body').innerHTML = data;
      } else {
         document.getElementById('dialog-large-body').innerHTML = data;
      }   
   },
    
   /**
    * Sets the footer of the dialog
    */
   setFooter: function() {
      if(this.dialogType == 0) {
         document.getElementById('dialog-footer').innerHTML = '<input type="button" value="Close" onclick="Dialog.close();" />';
      } else {
         document.getElementById('dialog-large-footer').innerHTML = '<input type="button" value="Close" onclick="Dialog.close();" />'
      }
   },
   
   /**
    * Sets the execution URL
    */
   setURI: function(url) {
      this.URI = url; 
   },
    
   /**
    * Closes the dialog
    */
   close: function() {
      if(this.dialogType == 0) {
      document.getElementById('dialog-title').innerHTML = '';
      document.getElementById('dialog-footer').innerHTML = '';
      document.getElementById('dialog-body').innerHTML = '';
      document.getElementById('dialog-area').style.display = 'none';
      } else {
         document.getElementById('dialog-large-title').innerHTML = '';
         document.getElementById('dialog-large-footer').innerHTML = '';
         document.getElementById('dialog-large-body').innerHTML = '';
         document.getElementById('dialog-area-large').style.display = 'none';
         this.setType(0);
      }
      if(document.getElementById('chartdiv') && document.getElementById('chartdiv').style.visibility == 'hidden') {
         document.getElementById('chartdiv').style.visibility = 'visible';
      } else if(document.getElementById('overview_chartdiv') && document.getElementById('overview_chartdiv').style.visibility == 'hidden') {
         document.getElementById('overview_chartdiv').style.visibility = 'visible';
      }
   }, 

   /**
    * Fetches dialog data from the server
    */
   getContent: function() {
      var POST = {};
      POST.push = function(param, value) {
         this[''+param+''] = value;
      }
      if(this.URI.indexOf('dialog-info.php') != -1) {
         this.URI = this.URI.split('?');
         POST.push('tid', this.URI['1']);
         this.URI = this.URI['0'];
      } else {
         if(this.URI.indexOf('?') != -1) {
            this.URI = this.URI.split('?');
            var queryString = this.URI['1'];
            this.URI = this.URI['0'];
            if(queryString.indexOf('&') != -1) {
               queryString = queryString.split('&');
               querylen = queryString.length;
               for(var i = 0;i <querylen;i++) {
                  var tmp = queryString[''+i+''].split('=');
                  POST.push(tmp['0'],tmp['1']);
                  tmp = null;
               }
            } else {
               queryString = queryString.split('=');
               POST.push(queryString['0'],queryString['1']);
            }            
         }
      }            
      $.post(this.URI, POST, function(data) {
         if(data.length > 0) {
            Dialog.setBody(data);
         } else {
            Dialog.setBody('Oops! The server did not return a response.');
         }
      });
   },
    
   /**
    * Shows the dialog
    */
   show: function() {
      if(this.dialogType == 0) {
         document.getElementById('dialog-body').innerHTML = '<div class="dialog-initial-content">Loading...</div>';
         document.getElementById('dialog-area').style.display = 'block';
      } else {
         document.getElementById('dialog-large-body').innerHTML = '<div class="dialog-initial-content">Loading...</div>';
         document.getElementById('dialog-area-large').style.display = 'block';
      }      
      this.getContent();
   },
   
   /**
    * Shows a local dialog. That is, a dialog which doesn't fetch data from the server
    */
   showLocal: function(data) {
      this.setBody(data);
      if(this.dialogType == 0) {
         document.getElementById('dialog-area').style.display = 'block';
      } else {
         document.getElementById('dialog-area-large').style.display = 'block';
      }   
   }, 
   
   /**
    * Display an error in the dialog
    */
   displayError: function(error) {
      if(!errorBox) var errorBox = document.getElementById('dialog-error-box');
      errorBox.innerHTML = error;
      errorBox.style.display = 'block';
   },
   
   /**
    * Displays a success message
    */
   displaySuccess: function(msg) {
      if(!successBox) var successBox = document.getElementById('dialog-success-box');
      successBox.innerHTML = msg;
      successBox.style.display = 'block';
   }   
}
