/*
 * JavaScript Functions for iZ-smart.net
 * http://www.iZ-smart.net
 *
 * Copyright 2010, Christian Boenning <hexa@iZ-smart.net>
 *
*/

$(function () {
  // footerstats
  $.ajax({
    type: "GET",
    url: "/api/www/stats.php",
    success: function (data) {
      if (data) {
        $("#currentusers").html(data);
        $("#footerstats").tipsy({
          gravity: 'n',
          fade: true,
          delayIn: 250,
          delayOut: 500,
          maxWidth: 350
        });
      } else {
        $("#currentusers").html("Ajax-Error");
      }
    }
  });

  // externe links
  link2self = window.location.hostname;
  $("a[href^=http]").not("a[href*='" + link2self + "']").attr('target', '_blank').addClass('external');

  // Fancybox 
  $.fn.getTitle = function () {
    var arr = $("a.fancybox");
    $.each(arr, function () {
      var title = $(this).children("img").attr("title");
      $(this).attr('title', title);
    });
  };

  var thumbnails = 'a:has(img)[href$=".gif"],a:has(img)[href$=".jpg"],a:has(img)[href$=".jpeg"],a:has(img)[href$=".png"]';
  $("a[href*='.jpg']").addClass("fancybox").attr("rel", "fancybox");
  $("a[href*='.png']").addClass("fancybox").attr("rel", "fancybox");
  $(thumbnails).addClass("fancybox").attr("rel", "fancybox").getTitle();
  $("a.fancybox").fancybox({
    'padding': 10,
    'margin': 20,
    'width': 950,
    'height': 640,
    'autoScale': true,
    'autoDimensions': false,
    'centerOnScroll': true,
    'transitionIn': 'elastic',
    'transitionOut': 'elastic',
    'speedIn': 500,
    'speedOut': 500,
    'changeSpeed': 300,
    'showNavArrows': false,
    'easingIn': "easeOutBack",
    'easingOut': "easeInBack",
    'easingChange': "easeInOutQuart"
  });

  // IFrames-Fancybox
  var FancyContainers = "#mail2opers,#mail2helpdesk,#feedback,#flood";
  $(FancyContainers).fancybox({
    'width': '75%',
    'height': '90%',
    'autoScale': false,
    'easingIn': "easeOutBack",
    'easingOut': "easeInBack",
    'easingChange': "easeInOutQuart",
    'type': 'iframe'
  });

  // mark thumbs for linking
  $("a > img").addClass("thumb");

  // tipsy
  $('a[title]').tipsy({
    delayIn: 250,
    delayOut: 500,
    fade: true,
    fallback: '',
    gravity: $.fn.tipsy.autoWE,
    html: false,
    opacity: 0.8,
    title: 'title',
    maxWidth: 350
  });

  // site version (title of copyright)
  $('#version').tipsy({
    gravity: 'ne',
    fade: true,
    delayIn: 250,
    delayOut: 500,
    maxWidth: 350
  });
});

function openWebchat() {
  window.open('http://www.iZ-smart.net/webchat.html', 'Webchat', 'width=1100,height=700,scrollbars=no');
}

function shareFacebook() {
  window.open('http://www.facebook.com/sharer.php?u=' + encodeURIComponent(location.href) + '&t=' + encodeURIComponent(document.title), 'sharer', 'toolbar=0,status=0,width=626,height=436');
}

function shareTwitter() {
  window.open('http://twitter.com/home?status=Lese gerade bei @izsmart: ' + encodeURIComponent(location.href), 'Twitter', 'toolbar=0,status=0,width=800,height=600,scrollbars=yes');
}

function NetzwerkStatus() {
  window.open('http://wwwapi.iz-smart.net/status/', 'Status', 'width=810, height=700, scrollbars=no');
}

// Debug, Debug, Debug
window.onerror = function (desc, page, line, chr) {
  if (this.console) {
    console.log('JavaScript Error - Desc: ' + desc + ' - Page: ' + page + ' - Line: ' + line);
  }
};