﻿/// <reference path="~/TopletsResources/js/jquery-vsdoc.js" />

/**
* Scripts for the use in VideoZone
*/
var VideoZone = {
    init: function(catId) {
        VideoBox.get(1, catId);
        VideoTabs.Playlist.refresh();
        VideoTabs.History.refresh();
    }
}

/**
* Scripts for the use of video player
*/
var VideoPlayer = {
    Src: null,
    Ads: {
        Preroll: null,
        Postroll: null
    },
    Players: {
        currentPlayer: {},
        VideoZonePlayer: {
            pendingRequest: new Function()
        }
    }, // video player flash object
    play: function(url) {
        if (!!this.Players.VideoZonePlayer.flash) {
            if (VideoPlayer.Ads.Preroll) this.Players.VideoZonePlayer.flash.updateConfig("prerollPath", VideoPlayer.Ads.Preroll + new Date().getTime());
            if (VideoPlayer.Ads.Postroll) this.Players.VideoZonePlayer.flash.updateConfig("postrollPath", VideoPlayer.Ads.Postroll + new Date().getTime());
            this.Players.VideoZonePlayer.flash.updateConfig("videoPathHighQ", url);
            this.Players.VideoZonePlayer.flash.commandPlayer("reset", null, null);
        } else {
            var _this = this;
            this.Players.VideoZonePlayer.pendingRequest = function() { _this.play(url); }
        }
    }
}

/**
* Called by video player for event handling
* s - Omniture object
*/
function handlePlayerEvent(eventType, param) {
    var metadata = VideoPlayer.Players.currentPlayer.metaData;
    switch (eventType) {
        case "Play":
            try { s.Media.play(metadata.uid + "-" + metadata.title, param); } catch (e) { if (console) console.log(e); }
            break;
        case "Stop":
            try { s.Media.stop(metadata.uid + "-" + metadata.title, param); } catch (e) { if (console) console.log(e); }
            break;
        case "Pause":
            try { s.Media.stop(metadata.uid + "-" + metadata.title, param); } catch (e) { if (console) console.log(e); }
            break;
        case "StartingMainVideo":
            VideoTabs.History.add(metadata.uid);
            try {
                s.Media.open(metadata.uid + "-" + metadata.title, param, "VideoZone Player");
                s.Media.play(metadata.uid + "-" + metadata.title, 0);
            } catch (e) {
                if (console) console.log(e);
            }
            break;
        case "FinishedMainVideo":
            try {
                s.Media.stop(metadata.uid + "-" + metadata.title, param);
                s.Media.close(metadata.uid + "-" + metadata.title);
            } catch (e) {
                if (console) console.log(e);
            }
            if (VideoTabs.Playlist.isPlaying) VideoTabs.Playlist.next();
            break;
    }
}

/*****************************************
* Binding Events
******************************************/
// play video item event
/*
jQuery('a.playUID').live('click.selectVideo', function(event) {
    if (event.button === 0) {
        event.preventDefault();
        VideoItemInfo.update(jQuery(this).attr('href').match(/(\d+)[^\/]*$/)[1]);
    }
});
*/
// playlist button
jQuery('a.playlist-button')
    .live('update', function() {
        var $this = jQuery(this);
        var uid = $this.closest('.videoItem').attr('uid');
        if (VideoTabs.Playlist.isInPlaylist(uid)) {
            $this.find('span').html("Remove from Playlist");
        } else {
            $this.find('span').html("Add to Playlist");
        }
    }).live('click', function(event) {
        event.preventDefault();
        var $this = jQuery(this);
        var uid = $this.closest('.videoItem').attr('uid');
        if (VideoTabs.Playlist.isInPlaylist(uid)) {
            VideoTabs.Playlist.remove(uid);
        } else {
            VideoTabs.Playlist.add(uid);
        }
    });

// toolbox buttons
jQuery(".js-help a, .js-video_info_input")
    .live("click", function(event) {
        $(this).parent().find("input").focus().select();
    });
