/*
Script: mng.js
  Mango Script
  
*/

var MNG = {
  'version': 'dev-0.2.1'
};


// Iframe urls
var Iframe = new Class({
  
  Implements: [Options],
  
  options: {
    default_section: 'inicio',
    url: ''
  },
  
  initialize: function(url, options) {
    this.setOptions(options);
    
    this.options.url = url;
    this.options.default_section = options.default_section || this.options.default_section;
  },
  
  src: function() {
    // Check alias
    if (Mango.getURLParameters(this.options.url, 'alias') != "") {
      var s = this.options.url.split('alias=')[1];
      
      // Check http:// of alias
      if (s.indexOf('http://', 0)) {
        return './'+this.options.url.split('alias=')[1];
      } else {
        return s;
      }
      
    } else {
      Mango.setTracker('/'+this.options.default_section);
      
      return 'sections/index.html?load='+this.options.default_section;
    }
  }
  
});

//
var iframe = new Iframe(location.href, {default_section: 'inicio'});


var Mango = new function() {
  this.getURLParameters = function(url, value) {
    var regexS = "[\\?&]"+value+"=([^&#]*)";
    var regex = new RegExp(regexS);
    
    var results = regex.exec(url);
    return (results == null) ? "" : unescape(results[1]);
  };
  
  this.tellMeAlias = function(value) {
    return (value == "") ? "" : value.split('?')[0];
  };
  
  this.goto = function(url, id, tracker, menu, click_center, sub_section) {
    // alert(url+" id: "+id+" tracker: "+tracker+" menu: "+menu+" click_center: "+click_center+" sub_section: "+sub_section);
    
    // click_center to know if click in the middle
    var cc = ((click_center == 'true') ? '&click_center=true' : '');
    
    //
    var ss = ((sub_section !== undefined && sub_section !== null && sub_section != 'null' && sub_section != 'undefined') ? '&sub_section=' + sub_section : '');
    
    document.id('mng').src = url + '&load=' + id + cc + ss;
    
    this.disableOption(menu);
    
    this.setTracker(tracker);
  };
  
  
  this.setTracker = function(tracker) {
    pageTracker._trackPageview(tracker);
  };
  
  
  // Disable options
  this.disableOption = function(menu) {
    var turn = (menu == 'flNavigation') ? 'flNavigationBottom' : 'flNavigation';
    
    Browser.Engine.trident ? window[turn].hiddenOption() : document[turn].hiddenOption();
  };
  
  this.visibleOption = function(section, menu) {
    // Browser.Engine.trident ? window[menu].setVisibleOption(section) : document[menu].setVisibleOption(section);
  };
  
  
  
  this.domain = function() {
    // mango.com or hominiemerito.com
    if (window.location.hostname.replace(/^www\./,'').charAt(0) == 'm') {
      return 'mng'
    } else {
      return 'he';
    };
  };
  
  this.getAlias = function(value){
    return (value == "") ? "" : value.split('?')[0];
  };
  
};


// flashfix
var onload = function() {
  objects = document.getElementsByTagName("object");
  
  for (var i = 0; i < objects.length; i++) {
    objects[i].outerHTML = objects[i].outerHTML;
  }
};


// kko

// Duration days of Mango season 2010
function $seasonDurationFromNow(current_date) {
  // var endSeason = '2010/08/15'
  return ((8 - current_date.getMonth()) * 30 + 15);
};


// Trackers
function $getTrackerFromId(id) {
  switch(id) {
    case '':
      return '';
    case '':
      return '';
      
    case '':
    case '':
      return '';
      
    default:
      return '';
      
  };
};
