﻿var J = jQuery.noConflict();
var LiveOnPage = {};
var extraParams = '';
LiveOnPage.init = function(domId, location) {
    this.obj = J("#" + domId);
    this.location = location;
}

LiveOnPage.setLocation = function(x, y) {

    var viewH = J(window).height();
    var scrollH = J(window).scrollTop();
    var viewW = J(window).width();
    var scrollW = J(window).scrollLeft();

    var t = 0;
    var l = 0;
    var isCustom = true;
    switch (this.location) {

        case "topleft":
            t = scrollH;
            l = scrollW;
            this.obj.css('top', t + "px").css('left', l + "px");
            isCustom = false;
            break;
        case "topcenter":
            t = scrollH;
            l = scrollW + (viewW / 2) - (this.obj.width() / 2);
            this.obj.css('top', t + "px").css('left', l + "px");
            isCustom = false;
            break;
        case "topright":
            t = scrollH;
            l = viewW + scrollW - this.obj.width();
            this.obj.css('top', t + "px").css('left', l + "px");
            isCustom = false;
            break;

        case "centerleft":
            t = scrollH + (viewH / 2) - (this.obj.height() / 2);
            this.obj.css('top', t + "px").css('left', l + "px");
            isCustom = false;
            l = scrollW;
            break;
        case "centerpage":
            t = scrollH + (viewH / 2) - (this.obj.height() / 2);
            l = scrollW + (viewW / 2) - (this.obj.width() / 2);
            this.obj.css('top', t + "px").css('left', l + "px");
            isCustom = false;
            break;
        case "centerright":
            t = scrollH + (viewH / 2) - (this.obj.height() / 2);
            l = viewW + scrollW - this.obj.width();
            this.obj.css('top', t + "px").css('left', l + "px");
            isCustom = false;
            break;

        case "bottomleft":
            t = viewH + scrollH - this.obj.height();
            l = scrollW;
            this.obj.css('top', t + "px").css('left', l + "px");
            isCustom = false;
            break;
        case "bottomright":
            t = viewH + scrollH - this.obj.height();
            l = viewW + scrollW - this.obj.width();
            this.obj.css('top', t + "px").css('left', l + "px");
            isCustom = false;
            break;
        case "bottomcenter":
            t = viewH + scrollH - this.obj.height();
            l = scrollW + (viewW / 2) - (this.obj.width() / 2);
            this.obj.css('top', t + "px").css('left', l + "px");
            isCustom = false;
            break;

    }

    if (isCustom) {

        this.obj.css('top', y + "px").css('left', x + "px");
    }




};

LiveOnPage.Unload = function() {
    if (LiveOnPage.Clips[LiveOnPage.ReloadClipIndex].showPlayAgain) {
        this.obj.html('<A href="#" onclick="LiveOnPage.ReloadLastClip();return false;"><img src="http://motiliti.net/content/images/playagain.jpg" /></A>');
        LiveOnPage.setLocation(0,0);
    }
    else {
        this.obj.html('');

    }
};

LiveOnPage.exitLOPPanel = function(id) {
    setTimeout('LiveOnPage.Unload()', 5);
};

LiveOnPage.OpenWindow = function(url) {
    window.open(url + LiveOnPage.GetQueryString(url), 'liveOnPage', '');
};

LiveOnPage.RedirectQueue = function(url) {
    setTimeout('LiveOnPage.OpenWindow("' + url + '")', 5);
};

LiveOnPage.RedirectQueueNoPop = function(url) {
    setTimeout('LiveOnPage.Redirect("' + url + '")', 5);
};

LiveOnPage.Redirect = function(url) {
    window.location = url + LiveOnPage.GetQueryString(url);
};
LiveOnPage.GetQueryString = function (url) {
    if (url.indexOf('?') == -1) {
        return '?' + extraParams;
    }
    return '&' + extraParams;

}

LiveOnPage.LoadClip = function(pageName) {
    var selectedClip = -1;

    for (var i = 0; i < LiveOnPage.Clips.length; i++) {
        if (LiveOnPage.Clips[i].pageName.toString().toLowerCase() == pageName.toString().toLowerCase()) {
            selectedClip = i;
            break;
        }
    }    
    
    if (selectedClip != -1) {
            LiveOnPage.ReloadClipIndex = selectedClip;
            setTimeout(LiveOnPage.Runner, LiveOnPage.Clips[selectedClip].delaySeconds*1000);
    }



};

LiveOnPage.Runner = function() {
    LiveOnPage.LoadClipByIndex(LiveOnPage.ReloadClipIndex);
}


LiveOnPage.ReloadLastClip = function() {

J("#LOPContainer").html('');
LiveOnPage.LoadClipByIndex(LiveOnPage.ReloadClipIndex);
};


LiveOnPage.LoadClipByIndex = function(selectedClip) {


    LiveOnPage.ReloadClipIndex = selectedClip;
    LiveOnPage.SetClip(LiveOnPage.Clips[selectedClip]);
    LiveOnPage.init("LOPContainer", LiveOnPage.Clips[selectedClip].location);
    LiveOnPage.setLocation(LiveOnPage.Clips[selectedClip].x, LiveOnPage.Clips[selectedClip].y);


    if (LiveOnPage.Clips[selectedClip].isScrolling) {

        J(window).scroll(function() { LiveOnPage.setLocation(0, 0); });
        J(window).resize(function() { LiveOnPage.setLocation(0, 0); });
    }
    else {
        J(window).unbind("scroll");
        J(window).unbind("resize");
    }
    LiveOnPage.setLocation(LiveOnPage.Clips[selectedClip].x, LiveOnPage.Clips[selectedClip].y);
};


//LiveOnPage.SetClip = function(width, height, presenterUrl, videoName, pageName, playerMode, removalOption, followUrl, clickUrl, logoUrl, clickImage, baseUrl, motilitiControls, showCloseButton)
LiveOnPage.SetClip = function(clip)
{
    J("#LOPContainer").html('<OBJECT id="LOPPlayer"  '
    + ' classid="' + flashParameters.classid + '" '
    + ' codebase="' + flashParameters.codebase + '"'
    + ' WIDTH="' + clip.width + '"'
    + ' HEIGHT="' + clip.height + '"' 
    + ' ALIGN="' + flashParameters.ALIGN + '">'
    + ' <PARAM NAME=movie VALUE="' + clip.presenterUrl + '">'
    + ' <PARAM NAME=quality VALUE=' + flashParameters.quality + '>'
    + ' <PARAM NAME=wmode VALUE=' + flashParameters.wmode + '>'
    + ' <PARAM NAME=allowScriptAccess VALUE=' + flashParameters.allowScriptAccess + '>'
    + ' <PARAM NAME=FlashVars VALUE="' + generateFlashVars(clip) + '">'
    + ' <EMBED '
    + ' src="' + clip.presenterUrl + '"'
    + ' allowScriptAccess=' + flashParameters.allowScriptAccess + ' '
    + ' quality="' + flashParameters.quality + '"'
    + ' WIDTH="' + clip.width + '"' 
    + ' HEIGHT="' + clip.height + '"'
    + ' ALIGN="' + flashParameters.ALIGN + '"'
    + ' TYPE="' + flashParameters.TYPE + '"'
    + ' wmode="' + flashParameters.wmode + '"' 
    + ' PLUGINSPAGE="' + flashParameters.PLUGINSPAGE + '"'
    + ' FLASHVARS="' + generateFlashVars(clip) + '">'
    + '</EMBED></OBJECT>');


};
function generateFlashVars(clip)
{

return 'motilitiControls=' + clip.motilitiControls
        + '&baseUrl=' + clip.baseUrl + '&clickImage=' + clip.clickImage + '&logoUrl=' + clip.logoUrl + '&link=' + clip.clickUrl
        + '&video=' + clip.videoName + '&id=' + clip.pageName + '&playerMode=' + clip.playerMode + '&removalOption=' + clip.removalOption
        + '&followURL=' + clip.followUrl + '&showCloseButton=' + clip.showCloseButton + "&openingPreview=" + clip.openingPreview
        + '&movieWidth=' + clip.width + '&movieHeight=' + clip.height + '&playAgainPreview=' + clip.playAgainPreview
        + '&logoImage=' + clip.logoImage + '&popUpLinks=' + clip.popUpLinks + '&' + extraParams;
}

LiveOnPage.WipeQueue = function(time) {

    LiveOnPage.Wiper = setTimeout('LiveOnPage.Unload()', time * 1000);
};

LiveOnPage.CancelWipeQueue = function() {
    clearTimeout(LiveOnPage.Wiper);
};
