﻿/****************************************************************************************************************
Created By  : Kumarchand R. Tripathi
Created On  : 21 Apr 2011
Purpose     : To Open or Close the model popup based on mode of call.
*****************************************************************************************************************/
function OpenCloseModelPopUp(modelPopupID, mode) {
    try {
        var ModelPopup = $find(modelPopupID);
        if (ModelPopup != null) {
            if (mode == 'open') {
                ModelPopup.show();
            }
            else {
                ModelPopup.hide();
            }
        }
    }
    catch (e) {
        alert(e.message);
    }
}

/****************************************************************************************************************
Created By  : Kumarchand R. Tripathi
Created On  : 21 Apr 2011
Purpose     : To Play flow player video in model view.
*****************************************************************************************************************/
function ShowVideoInModelPopup(videoURL, modelPopupID, videoPlayerContainerID, modelViewContainerID, videoPlayerID) {
    try {
        var divAnnouncementVideoPlayer = $('#' + videoPlayerContainerID);
        var divAnnouncementVideo = $('#' + modelViewContainerID);
        //var content = "<a href='" + videoURL + "' style='display:inline-block;width:640px;height:400px;' id='" + videoPlayerID + "'> <img src ='/images/spacer.gif' alt='splash image'/></a>";
        var content = "<a href='" + videoURL + "' style='display:inline-block;width:640px;height:400px;' id='" + videoPlayerID + "'></a>";
        divAnnouncementVideoPlayer.html(content);
        setTimeout(function() { PlayStopFlowPlayerVideo(videoPlayerID, 'play', true, true, null) }, 100);
        OpenCloseModelPopUp(modelPopupID, 'open');
        divAnnouncementVideo.show('slow');
    }
    catch (e) {
        alert(e);
    }
}

function CloseModelPopupVideoPlayer(modelPopupID, videoPlayerContainerID, modelViewContainerID, videoPlayerID) {
    try {
        PlayStopFlowPlayerVideo(videoPlayerID, 'stop', false, false, null);
        //$f(videoPlayerID).unload();
        OpenCloseModelPopUp(modelPopupID, 'close');
        var playerContainer = $('#' + modelViewContainerID);
        var player = $('#' + videoPlayerContainerID);
        if (playerContainer != null && player != null) {
            playerContainer.hide('slow');
            player.html('');
        }
    } catch (e) {
        alert(e);
    }
}

function PlayStopFlowPlayerVideo(videoPlayerID, mode, autoPlayStatus, scrubberDisplay, beforeResumeCallback) {
    try {
        if (mode == 'play') {
            //$f(videoPlayerID, '/images/flowplayer/flowplayer.controls-3.2.5.swf');
            $f(videoPlayerID, { src: '/images/flowplayer/flowplayer-3.2.7.swf', wmode: 'transparent' },
                   {
                       onLoad: function() {	// provide a Player event listener
                           this.unmute();
                           this.setVolume(80);
                       },
                       /*
                       onResume: function() {
                       try {
                       if (beforeResumeCallback && typeof (beforeResumeCallback) === "function") {
                       beforeResumeCallback();
                       this.pause();
                       //PlayStopFlowPlayerVideo(videoPlayerID, mode, autoPlayStatus, scrubberDisplay, beforeResumeCallback);
                       }
                       } catch (e) {
                       alert(e);
                       }
                       },*/
                       onBeforeResume: function() {
                           try {
                               if (beforeResumeCallback && typeof (beforeResumeCallback) === "function") {
                                   this.unload();
                                   // this.pause();
                                   setTimeout(function() { PlayStopFlowPlayerVideo(videoPlayerID, mode, autoPlayStatus, scrubberDisplay, beforeResumeCallback); }, 100);
                                   beforeResumeCallback();
                                   return false;
                               }
                               return true;
                           } catch (e) {
                               alert(e);
                           }
                       },
                       clip: {
                           autoPlay: autoPlayStatus,
                           autoBuffering: true
                       },

                       plugins: {
                       //Added by Chaitanya this viral pulgin
                           viral:
                            {
                                // load the viral videos plugin
                                url: '/images/flowplayer/flowplayer.viralvideos-3.2.5.swf',



                                // define a video title used in sharing
                                share:
			                        {
			                            description: 'Extreme surfers riding big waves',
			                            // disable livespaces (it's from Microsoft)
			                            livespaces: false
			                        },
                                embed:false,
                                email: false



                            },
                           controls: {
                               autoHide: false,
                               stop: true,
                               time: true,
                               fullscreen: true,
                               scrubber: scrubberDisplay,
                               tooltips: {
                                   buttons: true,
                                   stop: 'stop',
                                   pause: 'pause',
                                   play: 'play',
                                   fullscreen: 'fullscreen'
                               }
                           }
                       }
                   });
        }
        else {
            $f(videoPlayerID).unload();
            $f(videoPlayerID).close();
        }
    }
    catch (e) {
        alert(e.message);
    }
}


/*
Added By    : Kumarchand R. Tripathi
Added On    : 25 Apr 2011 
Purpose     : To resolve wmode assignment issue with flow player
Wrote by jose.nobile@gmail.com
Free to use for any purpose
Tested at IE 7, IE 8, FF 3.5.5, Chrome 3, Safari 4, Opera 10
Tested with Object[classid and codebase] < embed >, object[classid and codebase], embed, object < embed > -> Vimeo/Youtube Videos
Please, reporte me any error / issue
*/

function fix_flash(containerID) {
    
    var container = containerID == null ? document : document.getElementById(containerID);
    // loop through every embed tag on the site
    var embeds = container.getElementsByTagName('embed');
    for (i = 0; i < embeds.length; i++) {
        embed = embeds[i];
        var new_embed;
        // everything but Firefox & Konqueror
        if (embed.outerHTML) {
            var html = embed.outerHTML;
            // replace an existing wmode parameter
            if (html.match(/wmode\s*=\s*('|")[a-zA-Z]+('|")/i))
                new_embed = html.replace(/wmode\s*=\s*('|")window('|")/i, "wmode='transparent'");
            // add a new wmode parameter
            else
                new_embed = html.replace(/<embed\s/i, "<embed wmode='transparent' ");
            // replace the old embed object with the fixed version
            embed.insertAdjacentHTML('beforeBegin', new_embed);
            embed.parentNode.removeChild(embed);
        } else {
            // cloneNode is buggy in some versions of Safari & Opera, but works fine in FF
            new_embed = embed.cloneNode(true);
            if (!new_embed.getAttribute('wmode') || new_embed.getAttribute('wmode').toLowerCase() == 'window')
                new_embed.setAttribute('wmode', 'transparent');
            embed.parentNode.replaceChild(new_embed, embed);
        }
    }
    // loop through every object tag on the site
    var objects = container.getElementsByTagName('object');
    for (i = 0; i < objects.length; i++) {
        object = objects[i];
        var new_object;
        // object is an IE specific tag so we can use outerHTML here
        if (object.outerHTML) {
            var html = object.outerHTML;
            // replace an existing wmode parameter
            if (html.match(/<param\s+name\s*=\s*('|")wmode('|")\s+value\s*=\s*('|")[a-zA-Z]+('|")\s*\/?\>/i))
                new_object = html.replace(/<param\s+name\s*=\s*('|")wmode('|")\s+value\s*=\s*('|")window('|")\s*\/?\>/i, "<param name='wmode' value='transparent' />");
            // add a new wmode parameter
            else
                new_object = html.replace(/<\/object\>/i, "<param name='wmode' value='transparent' />\n</object>");
            // loop through each of the param tags
            var children = object.childNodes;
            for (j = 0; j < children.length; j++) {
                if (children[j].getAttribute('name').match(/flashvars/i)) {
                    new_object = new_object.replace(/<param\s+name\s*=\s*('|")flashvars('|")\s+value\s*=\s*('|")[^'"]*('|")\s*\/?\>/i, "<param name='flashvars' value='" + children[j].getAttribute('value') + "' />");
                }
            }
            // replace the old embed object with the fixed versiony
            object.insertAdjacentHTML('beforeBegin', new_object);
            object.parentNode.removeChild(object);
        }
    }
}

/*****************************************************************************************************************
Created By  : Kumarchand R. Tripathi
Created On  : 20 May 2011
Purpose     : Used to set the players wmode settings to transparent mode so that html over those videos should be shown properly
******************************************************************************************************************/
function setPlayersWmodeSettings(containerID) {
    //set wmode for the embed tags
    $('#' + containerID + ' embed').each(
    function() {
        try {
            $(this).attr('wmode', 'transparent');
        } catch (e) {
            alert(e);
        }
    }
    );

    //set wmode setting for the object tags

    //get all flash objects
    var flashObjects = $('#' + containerID + ' object');
    //loop over the objects
    flashObjects.each(function() {
        //check params with wmode parameter name
        var params = flashObjects.find('param[name=wmode]')
        //if param exists then set its mode as transparent
        params.each(function() {
            $(this).attr('value', 'transparent');
        });
        //if params does not exists then add the param in objects html
        if (params.length == 0) {
            flashObjects.html(flashObjects.html() + '<param name="wmode" value="transparent"/>');
            alert(flashObjects.html());
        }
    });
}

/*****************************************************************************************************************
Created By  : Kumarchand R. Tripathi
Created On  : 23 May 2011
Purpose     : Stop the buffering of players when players gets loaded and its first clip shown.
******************************************************************************************************************/
//var bufferingStopped = false;
var playerBuffers = new Array();
function stopBuffer(selector) {
    try {
        $(selector).each(function() {
            var playerprocessed = false;
            for (var i = 0; i < playerBuffers.length; i++) {
                if (playerBuffers[i] == $(this).attr('id')) {
                    playerprocessed = true;
                    break;
                }
            }
            if (!playerprocessed) {
                $f($(this).attr('id')).stopBuffering();
                $f($(this).attr('id')).setVolume(100);
                playerBuffers[playerBuffers.length] = $(this).attr('id');
            }
        });
    } catch (e) {
        alert(e);
    }
}
