/*
 * CPQuickTimeView.j
 * AppKit
 *
 * Created by Philippe Laval.
 * Copyright 2009, www.nice-panorama.com .
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

@import <AppKit/CPDOMWindowBridge.j>
@import <AppKit/CPView.j>

@import "CPQuickTimeMovie.j"


@implementation CPQuickTimeView : CPView
{
    CPQuickTimeMovie    _quickTimeMovie;
    
    DOMElement      _DOMEmbedElement;
    DOMElement      _DOMMParamElement;
    DOMElement      _DOMObjectElement;
}

- (id)initWithFrame:(CGRect)aFrame
{
    self = [super initWithFrame:aFrame];
    
    if (self)
    {
		// Create the "object" node
		
        _DOMObjectElement = document.createElement("object");
		_DOMObjectElement.setAttribute("classid", "clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B%");
		_DOMObjectElement.setAttribute("codebase", "http://www.apple.com/qtactivex/qtplugin.cab");
        // _DOMObjectElement.classid = "clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B%";
        // _DOMObjectElement.codebase = "http://www.apple.com/qtactivex/qtplugin.cab";
        _DOMObjectElement.width = "100%";
        _DOMObjectElement.height = "100%";
        _DOMObjectElement.style.top = "0px";
        _DOMObjectElement.style.left = "0px";
        
		// Create the "param" nodes inside the "object" node
		
        _DOMParamElement = document.createElement("param");
        _DOMParamElement.name = "src";
		// _DOMParamElement.value will be set in setQuickTimeMovie:
        _DOMObjectElement.appendChild(_DOMParamElement);

        var param = document.createElement("param");
        param.name = "cache";
        param.value = "true";
        _DOMObjectElement.appendChild(param);

        param = document.createElement("param");
        param.name = "autoplay";
        param.value = "true";
        _DOMObjectElement.appendChild(param);

        param = document.createElement("param");
        param.name = "controller";
        param.value = "false";
        _DOMObjectElement.appendChild(param);

		// Create the "embed" nodes inside the "object" node
        _DOMEmbedElement = document.createElement("embed");

		// _DOMEmbedElement.src will be set in setQuickTimeMovie:
		// _DOMEmbedElement.setAttribute("type", "video/quicktime");
        _DOMEmbedElement.type = "video/quicktime";
        _DOMEmbedElement.setAttribute("controller", "false");
        _DOMEmbedElement.setAttribute("autoplay", "true");
        _DOMEmbedElement.setAttribute("cache", "true");
        _DOMEmbedElement.setAttribute("pluginspace", "http://www.apple.com/quicktime/download/index.html");
        _DOMEmbedElement.width = "100%";
        _DOMEmbedElement.height = "100%";

        // IE requires this thing to be in the _DOMElement and not the _DOMObjectElement.
        _DOMObjectElement.appendChild(_DOMEmbedElement);
                
        _DOMElement.appendChild(_DOMObjectElement);
    }
    
    return self;
}

- (void)setQuickTimeMovie:(CPQuickTimeMovie)aQuickTimeMovie
{
    if (_quickTimeMovie == aQuickTimeMovie)
        return;
        
    _quickTimeMovie = aQuickTimeMovie;
    
    _DOMParamElement.value = aQuickTimeMovie._fileName;
    
    if (_DOMEmbedElement)
       _DOMEmbedElement.src = aQuickTimeMovie._fileName;
}

- (CPQuickTimeMovie)quickTimeMovie
{
    return _quickTimeMovie;
}

- (void)mouseDragged:(CPEvent)anEvent
{
    [[CPDOMWindowBridge sharedDOMWindowBridge] _propagateCurrentDOMEvent:YES];
}

- (void)mouseDown:(CPEvent)anEvent
{
    [[CPDOMWindowBridge sharedDOMWindowBridge] _propagateCurrentDOMEvent:YES];
}

- (void)mouseUp:(CPEvent)anEvent
{
    [[CPDOMWindowBridge sharedDOMWindowBridge] _propagateCurrentDOMEvent:YES];
}

@end
