/*jslint browser: true */
/*extern Drupal, $, jQuery, google */

function popUpPlayer(url) {
  window.open(url, 'player', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=400,height=230,left=50,top=50');
  return;
}

function popUpMusicBox(URL, path) {
  var day = new Date();
  var id = day.getTime();
  eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=988,height=610,left = 50,top = 50');");
  return;
}

function popUpAudio(URL) {
  window.open(URL, 'audioPlayer', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=850,height=430,left = 50,top = 50');
  return false;
}

function popUpVideo(URL) {
	window.open(URL, 'videoPlayer', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=988,height=610,left = 50,top = 50');
	return;
}

Drupal.behaviors.sonyScalableText = function (context) {
  $('.scalable-text:not(.sonyScalableText-processed)', context)
    .addClass('sonyScalableText-processed')
    .scalableText();
};

(function($) {
  $.fn.scalableText = function () {
    return this.each(
      function() {
        var o = $.scalableText.defaults;
        var h1 = $(this).height();

        $(this).height('auto');

        var fits = ($(this).height() <= h1);

        if (o.paddingFirst) {
          var padLeft = parseInt($(this).find('li a').css('padding-left'));
          var padRight = parseInt($(this).find('li a').css('padding-right'));
          
          while (!fits && (padLeft > o.paddingMin) && (padRight > o.paddingMin)) {
            $(this).find('li a').css('padding-left', padLeft--).css('padding-right', padRight--);
            fits = ($(this).height() <= h1);
          }
        }

        var i = parseInt($(this).css('font-size'));
        while (!fits && (i > 0)) {
          $(this).css('font-size', i-- + 'px');
          fits = ($(this).height() <= h1);
        }

        $(this)
          .css('font-size', i + 'px')
          .height(h1)
          .css('visibility', 'visible');
      }
    );
  };

  $.scalableText = {
    defaults: {
      paddingFirst: true,
      paddingMin: 5
    }
  };

})(jQuery);

$.fn.inlineLabel = function () {
  return this.each(function() {
    var $label = $('label[for=' + $(this).attr('id') + ']');
    var $input = $(this);

    var positionLabel = function() {
      $label
        .css($.extend({ position: 'absolute', zIndex: 100 }, $input.position()))
        .height($input.height())
        .width($input.width());
    };
    
    var showLabel = function() {
      positionLabel();
      $label.show();
    };
    
    var hideLabel = function() {
      $label.hide();
    };

    $label.addClass('inline-label-processed');
    $label.html($label.html().replace(':', ''));

    // set up nice form labels
    $(this).focus(function() {
      hideLabel();
    }).blur(function() {
      $(this).val() || showLabel();
    });

    // initialize label state
    hideLabel();
    $(this).val() || showLabel();
  });
};

Drupal.behaviors.inlineLabels = function() {
  $('input.inline-label, textarea.inline-label')
    .not('.inline-labels-processed')
    .addClass('inline-labels-processed')
    .each(function() {
      $(this).inlineLabel();
    });
};

