
/*  ----------------------------------------------
    core.js
    author: Regi E. (rellis@pushhere.com, Lead Dev)
    
    req/location
      scripts/mootools.js
      scripts/core.js
    ----------------------------------------------  */
    
    
/*  Class: Core
    -----------------------------------------------  */
    if(typeof Core != 'undefined') throw ('Core is already defined.');
    var Core = {

        init: function() {
            this.name = 'Core Library';
            this.version = '0.0.1';

            this.scriptable();

            this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
            this.version = this.searchVersion(navigator.userAgent)
              || this.searchVersion(navigator.appVersion)
              || "an unknown version";
            this.OS = this.searchString(this.dataOS) || "an unknown OS";
            
            $$('html').addClass(this.browser + ' ' + this.version + ' ' + this.OS);
        },

        scriptable: function(other) {
         if ( other === false
            || !document.createTextNode
            || !Array.prototype.push
            || !Object.hasOwnProperty
            || !document.createElement
            || !document.getElementsByTagName
            ) {
                return false;
            }
            $$('html').addClass('scriptable');
        },

        searchString: function (data) {
          for (var i=0;i<data.length;i++)  {
            var dataString = data[i].string;
            var dataProp = data[i].prop;
            this.versionSearchString = data[i].versionSearch || data[i].identity;
            if (dataString) {
              if (dataString.indexOf(data[i].subString) != -1)
                return data[i].identity;
            }
            else if (dataProp)
              return data[i].identity;
          }
        },

        searchVersion: function (dataString) {
          var index = dataString.indexOf(this.versionSearchString);
          if (index == -1) return;
          return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
        },
        dataBrowser: [
          {
            string: navigator.userAgent,
            subString: "Chrome",
            identity: "Chrome"
          },
          {
            string: navigator.vendor,
            subString: "Apple",
            identity: "Safari",
            versionSearch: "Version"
          },
          {
            prop: window.opera,
            identity: "Opera"
          },
          {
            string: navigator.userAgent,
            subString: "Firefox",
            identity: "Firefox"
          },
          {
            string: navigator.userAgent,
            subString: "MSIE",
            identity: "Explorer",
            versionSearch: "MSIE"
          },
          {
            string: navigator.userAgent,
            subString: "Gecko",
            identity: "Mozilla",
            versionSearch: "rv"
          },
        ],
        dataOS : [
          {
            string: navigator.platform,
            subString: "Win",
            identity: "Windows"
          },
          {
            string: navigator.platform,
            subString: "Mac",
            identity: "Mac"
          },
          {
            string: navigator.userAgent,
            subString: "iPhone",
            identity: "iPhone/iPod"
            },
          {
            string: navigator.platform,
            subString: "Linux",
            identity: "Linux"
          }
        ]
    };


/*  Class: String Truncation
    -----------------------------------------------  */
    String.implement({
      truncate: function(length, truncation) {
        length = $pick(length, 30);
        truncation = $pick(truncation, '...');
        return this.length > length ?
          this.slice(0, length - truncation.length) + truncation : String(this);
      }
    });


/*  Class: InsertVisual
    -----------------------------------------------  */
    function insertVisual(parent, location, element, className) {
      var parent = $$(parent);
      var visual = new Element(element,{
        'class' : className
      });
      parent.grab(visual, location);
    }
    

/*  =Function: printPage
    ----------------------------------------------  */
    function printPage(btn) {
      $$(btn).addEvent('click', function(event) {
          window.print();
          event.stop();
      });
    };
    

/*  =Class: clockSwf
    ----------------------------------------------  */    
    var clockSwf = new Swiff('/static/images/flash/antiquorum_clock.swf', {
        id: 'clock',
        width: '149',
        height: '149',
        style: 'visibility: visible',
        container: $('adContent'),
            params:  {
                bgcolor: '#000000',
                allowScriptAccess: 'always',
                quality: 'high',
                wMode: 'transparent',
                scale: 'noscale',
                menu: 'false',
                swLiveConnect: 'false'
            }
    });
    
    
/*  =Function: inputWaterMark
    ----------------------------------------------  */    
    function inputWaterMark(elements) {
      var values = [];
      var inputs = [];
      
      function addWaterMark(el){
       try {
           values.push(el.value);
           el.addEvent('focus',function(){
               if (el.value === values[inputs.indexOf(el)]){el.value = ''};
           });
           el.addEvent('blur',function(){
               if(this.value === ''){el.value = values[inputs.indexOf(el)]};
           });
       } catch(e) {console.log('addWaterMark: ', e)}
      };
       inputs = $$(elements);
       inputs.each(addWaterMark);
    };
    

/*  Class: Module
    -----------------------------------------------  */
    var InfoModule = new Class({
      Implements: [Options],
      options: {
        duration: 600,
        transition: 'back:out',
        alias: null,
        ajaxRequest: false // inline or request
      },
      init: function(module, btn, options) {
        this.btn = $$(btn); 
        this.setOptions(options);
        this.handleLink();
        (this.options.ajaxRequest) ? this.createRequest() : this.module = $(module);
        this.module.setStyle('position', 'absolute');
      },
      createRequest: function() {
        var requestContent = new Element('div', {
          'id': $pick(this.options.alias, 'requested-content'),
          'class': 'request-content'
        });
        requestContent.setStyle('display', 'none');
        requestContent.inject($('site'));
        var requestModule = new Request.HTML({
            method: 'get',
            url: this.btn.get('href').toString(),
            onSuccess: function(html) {
              this.module.adopt(html);
              $('id_link').set('value', document.location);
            }
        });
        requestModule.send();
        this.module = requestContent;
      },
      createCloseBtn: function() {
        var closeBtn = new Element('a', {
          'class': 'close-btn',
          'text': 'close button',
          'events': {
              'click': function(){
                  this.hide();
                  closeBtn.getParent().dissolve();
              }
          }
        });
        closeBtn.inject(this.module);
      },
      handleLink: function() {
        this.btn.addEvent('click', function(event) {
          this.module.reveal({
            duration: 250
          });
          this.createCloseBtn();
          this.intialPosition();
          event.preventDefault();
        }.bind(this));        
      },
      intialPosition: function() {
        this.module.setStyles({
            left: (window.getScrollLeft() + (window.getWidth() - this.module.getWidth()) / 2) + 'px',
            top: (window.getScrollTop() + (window.getHeight() - this.module.getHeight()) / 6) + 'px'
        });
        this.scrollPosition();
        window.addEvents({
          'scroll': function() {
            this.scrollPosition();
          }.bind(this),
          'resize': function() {
            this.scrollPosition();
          }.bind(this)
        });
      },
      scrollPosition: function() {
        this.module.set('morph', {
            duration: this.options.duration,
            transition: this.options.transition
        });
        this.module.morph({
            left: (window.getScrollLeft() + (window.getWidth() - this.module.getWidth()) / 2) + 'px',
            top: (window.getScrollTop() + (window.getHeight() - this.module.getHeight()) / 2) + 'px'
        });
      }
    });

    var explorer = new InfoModule();
    var friend = new InfoModule();
   
    
/*  =Function: IE 6 Redirect (Bad form!)
    ----------------------------------------------  */
    function redirection() {
      if (Browser.Engine.trident4 || Browser.Engine.trident54) {
        window.location = "/explorer/"
      };
    };

   
/*  =Mobile
    ----------------------------------------------  */   
    function mobile() {
      window.location = 'http://beta.antiquorum.com/mobile/';
    }


/*  Ready
    -----------------------------------------------  */
    window.addEvent('domready', function(){        
        Core.init();
        redirection();
        ($chk(Browser.Platform.ipod)) ? mobile() : false;
        printPage('.print a');
        ($chk($('advertisement'))) ? clockSwf.replaces($('adContent')) : false;
        inputWaterMark('form input');
    });
