//Param Init 
       var moVideoParam = new Array();
        moVideoParam['appFeedAlias'] = "widget";
        moVideoParam['apiKey'] = "movideo10";
        //moVideoParam['mediaId'] = ""; 
        moVideoParam['token'] = ""; 
        moVideoParam['videoPageURL'] = "http://readysteadycook.ten.com.au/video.htm";
        moVideoParam['feedSearchTag'] = "['show:type=Full Episode']";
        //Default playlist
        moVideoParam['playlist'] = "38738";
 ////////////////////////////////////////////////////////////////////////////////////////////////////////////
//This function extract the video Tag information for movideo tag search
    function extractTag(tag, tagString){
          tag = tag.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
          var regexS = "[\\?&]"+tag+"=([^&#]*)";
          var regex = new RegExp(regexS);
          var results = regex.exec(tagString);
          if( results == null )
            return "";
          else
            return results[1];
    }
    function extractMovideoParam(urlParam, prefix){
        channel = getChannelFromRoo(urlParam);
        moVideoParam['playlistId'] = getPlaylistID("kit", channel);
        tagString = "clip:code="+getVideoTagFromRoo(urlParam, prefix);
        getMovideoIDByTag(tagString);
    }    
    //Return the Channel from Roo as Tag
    function getChannelFromRoo(clipID){
        //Determine if the clipID is part of URL string
        var rooChannel = "";
        if(clipID.indexOf("channel") > -1){
            rooChannel = extractTag("channel", clipID);
        } else if (clipID.indexOf("vxChannel") > -1){
            rooChannel = extractTag("vxChannel", clipID);
        } else{}
        return rooChannel;
    }
    //Return the Video ID from Roo as Tag
    function getVideoTagFromRoo(clipID, prefix){
        //Determine if the clipID is part of URL string
        var clipIDtemp;
        if(clipID.indexOf("vxClipId") > -1){
            clipIDtemp = extractTag("vxClipId", clipID);
        } else if (clipID.indexOf("clipid") > -1){
            clipIDtemp = extractTag("clipid", clipID);
        } else {
            clipIDtemp = clipID
        }
        startpos = clipIDtemp.indexOf(prefix);
        return clipIDtemp.substring(startpos);
    }
    //Return the true if clip is roo content given prefix
    function rooVideo(clipID, prefix){
        if (clipID.indexOf(prefix) > -1){
            return true;
        } else {
            return false;
        }
    }
    //Return the true if clip is roo content given prefix
    function rooVideoURL(clipID, prefix){
        if ((clipID.indexOf("clipid") > -1) && (clipID.indexOf("channel") > -1) && (clipID.indexOf(prefix) > -1)){
            return true;
        }else if ((clipID.indexOf("ClipId") > -1) && (clipID.indexOf("vxChannel") > -1) && (clipID.indexOf(prefix) > -1)){
            return true;
        } else {
            return false;
        }
    }
    //Check if there is any parameter pass on the url
    function queryString(){
        fullURL = window.location.href;
        if(fullURL.indexOf('?')){
            return true;
        } else {
            return false;
        }
    }
    function urlTrim(rooUrl){
            rooUrl = rooUrl.replace(/\&amp;/g,'&');
            rooUrl = rooUrl.replace('%20' ,' ');
        return rooUrl;
    }
 ////////////////////////////////////////////////////////////////////////////////////////////////////////////
    //Appending the text to HTML
    //Taking in the DIV to draw object and the VideoAttributes
    function appendMedia(obj,videoAttributes)
    {
        videoDescription = truncateText(videoAttributes['description'], 82);
        videourl = moVideoParam['videoPageURL']+"?movideo_p="+videoAttributes['playlist']+"&movideo_m="+videoAttributes['id'];
        //videourl = moVideoParam['videoPageURL']+"?movideo_m="+videoAttributes['id'];
        var e = $('<div class="item"><div style="height: 92px;" class="preview-image"><a href="' + videourl + '" title="' + videoAttributes['title'] + '"><img width="103" height="58" src="' + videoAttributes['image'] + '" alt="image preview '+ videoAttributes['id'] +'"/>'+
                '</a></div><h4 style="width: 185px;"><a href="' + videourl + '" title="' + videoAttributes['title'] + '">' + videoAttributes['title'] + '</a></h4><div class="text">' + videoDescription + '</div></div>');
        obj.append(e);
    }
    
    //////////////////////////////////////////////////////
    //Search the media in given playlist and draw
    function drawMoVideoPlaylistFeed(divID, numOfItem, playlist){
        if (playlist.length > 0){
            playlist = moVideoParam['playlist'];
        }
        //Init Get Token
        getMoVideoToken();
        var searchParam = new Array();
        searchParam['includeMedia'] = 'true';
        searchParam['mediaLimit'] = numOfItem;
        queryString = "?token="+moVideoParam['token'];
        for(i in searchParam){
            if(searchParam[i] != ""){
                queryString = queryString + "&" + i + "=" + searchParam[i];
            }
        }
        //If search
        url = "api/rest/playlist/"+playlist+queryString;
        $.ajax({
            type: "GET",
            url: url,
            dataType: "xml",
            async: false,
            //Success Return
            success: function(xml) {
                 $('#'+divID+" .temp").remove();
                 $(xml).find('mediaList').each(function(){
                    $(this).find('media').each(function(){
                        var videoAttributes = new Array();
                            videoAttributes['playlist'] = playlist;
                            videoAttributes['title'] = $(this).find('title').text();
                            videoAttributes['image'] = $(this).find('imagePath').text()+'cropped/128x72.png';
                            videoAttributes['description'] = $(this).find('description').text();
                            var id;
                            $(this).find('id').each(function(){
                               id = $(this).text();                    
                               if (id!== null) {
                                  return false;
                               }
                            });
                            videoAttributes['id'] = id;
                            appendMedia($('#'+divID),videoAttributes);
                    });                
                });

            }
            //End Success
        });
    }
    //////////////////////////////////////////////////////
    //Search the media api by given tagString
    function getMovideoIDByTag(tagString){
        //Init Get Token
        getMoVideoToken();
        var searchParam = new Array();
        //Param reference from http://support.movideo.com/entries/95326-api-media-search-media
        searchParam['pageSize'] = 1;
        searchParam['paged'] = 'false';
        searchParam['tags'] = '["' + tagString + '"]';
        queryString = "?token="+moVideoParam['token'];
        for(i in searchParam){
            if(searchParam[i] != ""){
                queryString = queryString + "&" + i + "=" + searchParam[i];
            }
        }
        //If search
        url = "api/rest/media/search"+queryString;
        //url = "http://localhost/search.php"+queryString;
        $.ajax({
            type: "GET",
            url: url,
            dataType: "xml",
            async: false,
            //Success Return
            success: function(xml) {
                moVideoParam['mediaId'] = $(xml).find('id:first').text();
            }
            //End Success
        });
    }
    //////////////////////////////////////////////////////
    function getMoVideoToken(){
        var scope= this;
        if(moVideoParam['token'] == ""){
            $.ajax({
                type: "GET",
                //url: "http://localhost/session.php",
                url: "/api/rest/session?key="+moVideoParam['apiKey']+"&applicationalias="+moVideoParam['appFeedAlias'],
                dataType: "xml",
                async: false,
                //Success Return
                success: function(xml) {
                    $(xml).find('session').each(function(){
                        moVideoParam['token'] = $(this).find('token').text();
                    });
                }
            });
        }
    }
    //////////////////////////////////////////////////
    //Callback for initialised 
    function updateFacebookInfo(media){
         $('#debug').append('Playing Now:' + media.id);
         $('#debug').append('Playing Now:' + media.title);
    }
 ////////////////////////////////////////////////////////////////////////////////////////////////////////////
 //Return Eqivalent Movideo Playlist Name 
        function getPlaylistName(type, value){
            return $("input["+type+"="+value+"]").attr("mplaylist");    
        }
        //Return Eqivalent Movideo Playlist Code 
        function getPlaylistCode(type, value){
            return $("input["+type+"="+value+"]").attr("mplaylistcode");    
        }
        
        //Return Eqivalent Movideo Playlist Index 
        //Required for calling movideo playlist 
        function getPlaylistID(type, value){
             var playlistID = $("input["+type+"="+value+"]").attr("mplaylistId");
            //If no playlist return will default to default playlist
            if(playlistID == undefined){
                return "";
            } else {
                return playlistID;    
            }
        }
        //Return Eqivalent Kit Channel Name 
        //Required for returning video channel header
        function getKitChannel(type, value){
            return $("input["+type+"="+value+"]").attr("kit");    
        }