/*
 * CbControlbar script
 *
 * Author: Johannes Wüller
 * Created On: 17.06.2010
 *
 * This script requires jQuery (version 1.4.2+ is proven to work).
 *
 * see lib/framework/class.cb_controlbar.php for further information
 */
jQuery(function () {
   var wrap = $('.__cb-controlbar-wrap'),
      Cookie,
      cookieNames,
      switchToContent,
      switchToLogin,
      adjustHeight;

   cookieNames = {
      visible: '__CB_CONTROLBAR_VISIBILITY_STATUS',
      authed:  '__CB_CONTROLBAR_AUTHORIZATION_STATUS'
   };

   /*
    * utilities
    */
   Cookie = (function () {
      var _set = function (name, value, days) {
         var date = new Date(),
            expires = '';

         if (days) {
            date.setTime(Math.round(date.getTime() + days * 24 * 60 * 60 * 1000));
            expires = '; expires='+date.toGMTString();
         }

         document.cookie = name+'='+value+expires+'; path=/';
      };

      return {
         set: _set,

         get: function (name) {
            var nameEQ = name + '=',
               ca = document.cookie.split(';'),
               i,
               c;

            for (i = 0; i < ca.length; i++) {
               c = ca[i];

               while (c.charAt(0) == ' ') {
                  c = c.substring(1, c.length);
               }

               if (c.indexOf(nameEQ) === 0) {
                  return c.substring(nameEQ.length, c.length);
               }
            }

            return null;
         },

         remove: function (name) {
            _set(name, '', -1);
         }
      };
   }());

   switchToContent = function () {
      wrap.find('.__cb-controlbar-login').hide().find('.__cb-controlbar-user, .__cb-controlbar-pass').val('');
      wrap.find('.__cb-controlbar-content').show();
   };

   switchToLogin = function () {
      wrap.find('.__cb-controlbar-login').show();
      wrap.find('.__cb-controlbar-content').hide();
   };

   adjustHeight = function (duration, targetHeight) {
      duration = duration || 0;
      targetHeight = targetHeight >= 0 ? targetHeight : wrap.find('.__cb-controlbar-content-wrap').outerHeight();

      wrap.stop().animate({
         height: targetHeight+'px'
      }, duration);
   };

   /*
    * bindings
    */
   $('.__cb-controlbar-open-button').click(function () {
      Cookie.set(cookieNames.visible, 'true');
      adjustHeight(500);
   });

   wrap.find('.__cb-controlbar-close-button').click(function () {
      Cookie.remove(cookieNames.visible);
      Cookie.remove(cookieNames.authed);
      adjustHeight(500, 0);
      switchToLogin();
   });

   wrap.find('.__cb-controlbar-pass').keypress(function (e) {
      if (e.which == 13) {
         var project = wrap.find('.__cb-controlbar-project').val();

         $.getJSON('http://new.heimat.de/culturebase/cb_mdb/cms/login.php?proj='+project+'&callback=?', {
            proj:    project,
            todo:    'login',
            account: wrap.find('.__cb-controlbar-user').val(),
            kenn:    wrap.find('.__cb-controlbar-pass').val()
         }, function (data) {
            if (data.status == 'success') {
               Cookie.set(cookieNames.authed, 'true');
               switchToContent();
               adjustHeight(250);
            } else if (data.status == 'failure') {
               alert('Your login attempt failed. Please check username and password, then try again.');
            } else {
               alert('An unknown error occurred. Please try again.');
            }
         });
      }
   });

   /*
    * initialization
    */

   // style fix
   wrap.find('.__cb-controlbar-background').css('opacity', 0.75);

   if (Cookie.get(cookieNames.authed) == 'true' || wrap.find('.__cb-controlbar-login').length === 0) {
      switchToContent();
   }

   if (Cookie.get(cookieNames.visible) == 'true') {
      adjustHeight(0);
   }
});

