/**
 * jQuery.html
 *
 * Creates a jQuery-wrapped DOM tree.
 *
 * Example:
 *
 * $.html('a', {
 *     href: 'google.com',
 *     html: 'Google is a ',
 *     cn: [['strong', { html: 'well-known' }]],
 *     afterHtml: 'search engine.'
 * });
 *
 * Returns the equivalent of:
 * $("<p>Google is a <strong>well-known</strong> search engine.</p>");
 *
 * $.html('p', {}, true);
 *
 * Returns "<p></p>"
 *
 * @param {String} tagName The tag's name, such as "p" or "br". Standard
 * empty tag names will be detected and built as such. There is no way
 * to create a non-standard empty tag.
 * @param {Object} spec The DOM spec object. This may include any
 * desired HTML attributes. "cls" may be used as a substitute for
 * "class" which is a reserved word and an invalid property unless
 * quoted.
 *
 * The following properties will not be created as attributes, instead
 * behaving as described:
 * - afterHtml: Additional innerHTML content to include after any child
 *   elements.
 * - children: An array of arrays, with the first index in each
 *   specifying the tag, and the second the DOM spec object. For
 *   instance:
 *   [['b', { html: 'BoldText' }],
 *    ['i', { html: 'ItalicText' }]]
 * - cn: Alias of "children"
 * - html: The new element's innerHTML. This will come before any child
 *   elements.
 * @param {Boolean} asString true to return a string representation of
 * the tree rather than a jQuery object (this will be faster).
 *
 * @return {jQuery/String} The jQuery DOM tree, or string
 * representation.
 */
(function($) {
    // Empty element lookup table. Lower-case only!
    var empties = {
        area: true,
        base: true,
        basefont: true,
        br: true,
        col: true,
        frame: true,
        hr: true,
        img: true,
        input: true,
        isindex: true,
        link: true,
        meta: true,
        param: true
    };

    // Spec properties that are not HTML attributes. Lower-case only!
    var nonAttrs = {
        afterhtml: true,
        cn: true,
        children: true,
        html: true
    };

    var makeTag = function(tagName, spec) {
        spec = spec || {};
        // Opening tag
        var tag = '<' + tagName,
            // Alias "cn" to "children"
            children = spec.children || spec.cn;

        for (var a in spec) {
            // Each attribute not in nonAttrs
            if (spec.hasOwnProperty(a) && !(a.toLowerCase() in nonAttrs)) {
                // Build the HTML attribute
                tag += ' ' +
                    // Rewrite 'cls' as 'class'
                    (a == 'cls' ? 'class' : a) +
                '="' + spec[a] + '"';
            }
        }

        if (tag.toLowerCase() in empties) {
            // Close the empty tag
            tag += ' />';
        } else {
            // Close the opening tag
            tag += '>';
            // Add the first part of the innerHTML
            if (spec.html) {
                tag += spec.html;
            }
            if (children) {
                // Recursively add child nodes
                for (var i = 0, len = children.length; i < len; i++) {
                    tag += makeTag(children[i][0], children[i][1]);
                }
            }
            // Add the second part of the innerHTML
            if (spec.afterHtml) {
                tag += spec.afterHtml;
            }
            // Add the closing tag
            tag += '</' + tagName + '>';
        }

        return tag;
    };

    $.html = function(tag, spec, asString) {
        var dom = makeTag(tag, spec);
        // If we don't just want the HTML string,
        if (asString !== true) {
            // Create the elements, wrapped as a jQuery object
            dom = $(dom);
        }
        return dom;
    };
})(jQuery);


// define PNC if it does not exist
if(!window.PNC) {
  window.PNC = {};
}

PNC.Video = {
  open: function(element) {
    // remove current video if another is selected
    $('#overlay, #flash_container').remove();
    var link = false;
    var params = {};

    if(typeof element === 'string') {
      params.media_path = element;
    } else {
      params.media_path = element[0].href;
      $link.addClass('remote-processing'); // used to not make multiple calls
      link = true;
    }
		$('#ccFlash').hide();

      var overlay = $('<div id="overlay">');

      $('body').prepend(overlay);
      $('body').prepend(
        $.html('div', {
          id: 'flash_container',
          cn: [['div', {
            id: 'flash_player',
            cn: [['div', {
              id: 'player',
	          cn: [['object', {
	            type: 'application/x-shockwave-flash',
	            width: '100%',
	            height: '100%',
	            id: 'flash-swf',
	            data: '/media/swf/videoplayer.swf',
	            cn: [
	              ['param', { name: 'movie', value: '/media/swf/videoplayer.swf' }],
	              ['param', { name: 'quality', value: 'high' }],
	              ['param', { name: 'flashvars', value: 'vidUrl=' + params.media_path }],
	              ['param', { name: 'wmode', value: 'transparent' }],
	              ['param', { name: 'allowfullscreen', value: 'true' }]
	            ]
	          }],
	          ['a', { href: 'javascript:;', 'class': 'close_player', html: 'Close' }]]
            }]]
          }]]
        }, true)
      );

      $('#overlay').fadeTo('slow', 0.7, function(){
        $('#flash_player').append($('flash_container'));

        if(link)
          $link.removeClass('remote-processing');
      });
  },
  close: function()
  {
    $('#flash_container').remove();
    $('#overlay').fadeTo('slow',0,function(){ $(this).remove() });
    $('.remote-processing').removeClass('remote-processing');
		$('#ccFlash').show();
  }
};

$(function(){
  // define supported extensions
  var selectors = ['mov','flv','mp4','mpg','f4v'];

  for(var i in selectors)
    selectors[i] = 'a[href$=".'+selectors[i]+'"]';

  $(selectors.join(',')).click(function(){
    $link = $(this);
    if(!$link.hasClass('remote-processing'))
      PNC.Video.open($link);

    return false;
  });

  $('.close_player, #overlay').live('click',function(){
    PNC.Video.close();
    return false;
  });

  // remove video on esc
  $(document).keyup(function(event){
    if(event.keyCode == 27)
      PNC.Video.close();
  });
});




