// cookie functions http://www.quirksmode.org/js/cookies.html
jQuery.fn.createCookie = function(name,value,days) {
    if (days)
    {
        var date = new Date();
        date.setTime(date.getTime()+(days*24*60*60*1000));
        var expires = "; expires="+date.toGMTString();
    }
    else var expires = "";
    document.cookie = name+"="+value+expires+"; path=/";
    
    //alert('Click-createCookie: '+name);
}

jQuery.fn.readCookie = function(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++)
    {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) {
                //alert('Click-readCookie: '+c.substring(nameEQ.length,c.length));
                return c.substring(nameEQ.length,c.length);
        }
    }

    return null;
}

jQuery.fn.eraseCookie = function(name) {
    $(this).createCookie(name,"",-1);
    //alert('Click-eraseCookie');
}

/**
 * Schriftvergroesseerung in Stufen
 * - hier sind es 3
 * - gebunden an 'li class="AAA"'
 * @see CookieName: VTstyle
 */
var SchriftVT = {
    s_id:0,                     //internal Brain 4 status
    s: [13, 15, 17 ],   //here the 3 Fontsizes in px - or better in em?  (initially: 13,14,15)
    spc: [120, 140, 160 ],   //here the 3 Fontsizes in % - or better in em?  (initially: 120,130,140)
    elems: [],
    defaults : [],
    
    /* 4the future oso */
    init: function() {
      var self = this;
      $('.noFontResize').find('div, span, h1, h2, h3, h4, h5, ul, li, a, p').addClass('noFontResize');
      //$('#body div, #body span, #body h1, #body h2, #body h3, #body h4, #body h5, #body ul, #body li, #body a, #body p').each(function() {
      $('div, span, h1, h2, h3, h4, h5, ul, li, a, p').each(function() {
        var fs = $(this).css('font-size');
        var exclude = $(this).hasClass('noFontResize');
        if(fs != '' &&  !exclude) {
          self.elems.push($(this));
          self.defaults.push(fs);
        }
      });
      var c = $(this).readCookie('VTstyle');
      c = (c != '' && parseInt(c)<3 ? parseInt(c) : 0);
      self.s_id = c;
      self.set();
    },
    next: function() {
      var self = this;
      self.s_id = (self.s_id<2 ? self.s_id+1 : 0);
      self.set();
      $(this).createCookie('VTstyle', self.s_id, 365);
    },
    set: function() {
      var self = this;
      jQuery.each(self.elems, function(i) {
        var fs = $(this).css('font-size');
        
        if(fs != '') {
          var fsVal = parseFloat(fs);
          var fsUnit = fs.slice(-2);

          if(fsUnit == 'px') {
            if(self.s_id == 0) {
              fs = self.defaults[i];
              //$(this).css('font-size', ''+fs);
            } else {
              fs = fsVal + self.s_id;
              fs = fs+'px';
              //$(this).css('font-size', ''+fs+'px');
            }
            $(this).animate({fontSize: fs});
          }
        }
      });
    },
    get: function() {
      var c = $(this).readCookie('VTstyle');
      //0 is valid!!
      c = (c != '' && parseInt(c)<3 ? parseInt(c) : 0);
      this.s_id = c; //dort machen wir weiter
    }
}

jQuery(document).ready(function(){
  $('#featureNav').show();
  // Change font-size (fixed steps)
  SchriftVT.init();
  jQuery('li.AAA a').click(function() {
    SchriftVT.next();
  });
  
  // Opening external links in new window and adding event tracking
  jQuery("a[href*='http://']:not([href*='norta.com'])").addClass("new-window");
  jQuery('a.new-window').each(function() {
    jQuery(this).attr("title", "Visit "+jQuery(this).attr("href")+" (Opens in a new window)");
      jQuery(this).attr("target", "_blank");
    jQuery(this).attr("onclick", "pageTracker._trackEvent('Outlinks', 'Click', '"+jQuery(this).attr("href")+"');");
  });
  
  jQuery('input[type="text"]').addClass("idleField");  
  jQuery('input[type="text"]').focus(function() {  
  jQuery(this).removeClass("idleField").addClass("focusField");  
    if (this.value == this.defaultValue){  
      this.value = '';  
    }  
    if(this.value != this.defaultValue){this.select();}  
  });  
  jQuery('input[type="text"]').blur(function() {
    if ($.trim(this.value) == ''){
      jQuery(this).removeClass("focusField").addClass("idleField");
      this.value = (this.defaultValue ? this.defaultValue : '');
    } else {jQuery(this).removeClass("focusField").addClass("completedField");}
  });
  
  $(".video").click(function() {
      $.fancybox({
          'padding'		: 0,
          'autoScale'		: false,
          'transitionIn'	: 'elastic',
          'transitionOut'	: 'elastic',
          'title'			: this.title,
          'width'			: 601,
          'height'		: 338,
          'href'			: this.href.replace(new RegExp("([0-9])","i"),'moogaloop.swf?clip_id=$1'),
          'type'			: 'swf'
        });

      return false;
    });
    
    $(".videoYt").click(function() {
                     $.fancybox({
                      'padding'             : 0,
                      'autoScale'   : false,
                      'transitionIn'        : 'elastic',
                      'transitionOut'       : 'elastic',
                      'title'               : this.title,
                      'width'               : 601,
                      'height'              : 338,
                      'href'                : this.href.replace(new RegExp("watch\\?v=", "i"), 'v/'),
                      'type'                : 'swf',    // <--add a comma here
                      'swf'                 : {'allowfullscreen':'true'} // <-- flashvars here
                      });
                     return false;

                });
    
});

function vimeoLoadingThumb(id){    
    var url = "http://vimeo.com/api/v2/video/" + id + ".json?callback=showThumb";

    var id_img = "#vimeo-" + id;
    $(id_img).before('<script type="text/javascript" src="'+ url +'"></script>');
}


function showThumb(data){
    var id_img = "#vimeo-" + data[0].id;
    $(id_img).attr('src',data[0].thumbnail_medium);
}
