﻿$(function() {
  // Common site object
  var YN = window.YN = {};

  YN.chromeless = function (url, options) {
    var defaults = {
        name: null,
        width: 600,
        height: 400,
        x: null,
        y: null
    };
    var _options = $.extend({}, defaults, options);

    var link = window.open(
        url,
        name,
        'toolbar=0' +
        ',location=0' +
        ',directories=0' +
        ',status=0' +
        ',menubar=0' +
        ',scrollbars=yes' +
        ',resizable=yes' +
        (_options.y ? ',top=('+ _options.y +')' : '') +
        (_options.x ? ',left=('+ _options.x +')' : '') +
        (_options.x ? ',screenX=('+ _options.x +')' : '') +
        (_options.y ? ',screenY=('+ _options.y +')' : '') +
        ',width='+ _options.width +
        ',height='+ _options.height);
    link.focus();
  };
  
  // Open Purina legal windows
  YN.legal_window = function(url) {
    YN.chromeless(url, { name: 'legal', width: 560, height: 450 });
  }

  // Flash loading utility
  YN.Flash = {};

  // Default SWFObject settings
  YN.Flash.defaults = {
    movie: '',
    id: '',
    width: 0,
    height: 0,
    minVer: '8.0.0',
    flashVars: {},
    params: {
      quality: 'high',
      allowscriptaccess: 'sameDomain',
      wmode: 'transparent',
      base: ''
    },
    attributes: {},
    htmlCallback: $.noop
  };

  // Method to load a Flash movie
  YN.Flash.load = function(options) {
    var flashVars = $.extend({}, YN.Flash.defaults.flashVars, options.flashVars),
      params = $.extend({}, YN.Flash.defaults.params, options.params),
      attributes = $.extend({}, YN.Flash.defaults.attributes, options.attributes);
    
    options = $.extend({}, YN.Flash.defaults, options);
    options.flashVars = flashVars;
    options.params = params;
    options.attributes = attributes;
    
    swfobject.embedSWF(options.movie, options.id, options.width, options.height, options.minVer, null, options.flashVars, options.params, options.attributes);

    if (!swfobject.hasFlashPlayerVersion(options.minVer)) {
      // No Flash plugin or old version. Show alternate content.
      $("#" + options.id).show();
      options.htmlCallback();
    }
  };

  // Method to load the logo Flash
  YN.Flash.loadLogo = function(isHome) {
    var movie = '/Lib/Media/' + (isHome ? "headerHome.swf" : "headerSub.swf");
    YN.Flash.load({ movie: movie, id: 'flashLogo', width: 368, height: 120 });
  };
  
  // Social media sharing helper
  YN.Sharing = {};
  
  YN.Sharing.facebookLink = function (url, title) {
    YN.chromeless('http://www.facebook.com/sharer.php?t=' + encodeURIComponent(title) + '&u=' + encodeURIComponent(url), { name: 'share' });
  };
  
  YN.Sharing.twitter = function (url, message) {
    YN.chromeless('http://twitter.com/share?text=' + encodeURIComponent(message) + '&url=' + encodeURIComponent(url), { name: 'share' });
  };

  YN.init = function(options) {
    options = $.extend({ baseUrl: '', isHome: false }, options);
    
    YN.Flash.defaults.params.base = options.baseUrl;
    
    // Set up legal window popups
    $('a.legal').click(function(e) {
      // Cancel default click and bubble
      e.preventDefault();
      e.stopPropagation();
      // Open popup
      YN.legal_window($(this).attr('href'));
    });
    
    // Reset the nav text to visible (hid it in CSS initially to
    // prevent "flicker" of link text before images load)
    $('#primaryNav a, #secondaryNav a').css({ textIndent: 0 });

    // Apply a fix for links labeled as "long links", links that would
    // span more than one line. This places a character at the end of
    // the link in place of the background image.
    $('html').filter('.ie6, .ie7').find('a.longlink[:not(.inline)]').each(function () {
        var bgImg = $(this).css('background-image').replace(/(?:url\(['"]?)([^'"\)]*)(?:['"]?\))/, '$1');
        $(this).removeClass('longlink').addClass('longlink-applied').append('<span><img alt="" src="' + bgImg + '" /></span>');
    });
    
    YN.Flash.loadLogo(options.isHome);

    // Add 'first' class to sidebar callouts
    $('.sidebarList .tout:last').addClass('last');
    
    // Remove anchor links from page title in all versions of IE
    // This snippet has no affect on other browsers
    function cleanPageTitle() {
        var title = document.title;
        var re = /#[a-z-_][a-z0-9-_]+$/i;
        while (re.exec(title)) {
            title = title.replace(re, '');
        }
        document.title = title;
    }
    setTimeout(cleanPageTitle, 10);
  };
});
