
var intVidCount=1;

//Create container for add more
function addMore()
{
    if($('addMVid').rows.length<4)
    {
        var lastRow=$('addMVid').rows.length;
        var objTable=$('addMVid');
        
        var nxtVal=Number(lastRow) + Number(1);
        var rmvIndex=Number(nxtVal) - Number(1);
        //Adding row
        var tRow=objTable.insertRow(lastRow);
        
        var tCell1=tRow.insertCell(0);
        tCell1.innerHTML="YouTube ID: ";
        
        var tCell1=tRow.insertCell(1);
        tCell1.innerHTML="<input id='txt"+nxtVal+"'/>";
        
        var tCell1=tRow.insertCell(2);
        tCell1.innerHTML="<a class='addMore' href='javascript:void(0)' onclick='delRow(this)'>Remove</a>";
        
    }
    else
    {
        alert("You cannot add video more than 5.One parent and four child videos.");
    }
}


//Delete row
function delRow(objLnk)
{
    var objTable=$('addMVid');
    //Delete Table row
    objTable.deleteRow(objLnk.parentNode.parentNode.rowIndex);
    
}



function showAddPanel()
{
     $('scheduleVideo').grow();
}


    //Hide method
     function Hide(type)
     {
        if(type==1)
        {
            //schedule video
             Effect.BlindUp('mnShcVideoCont');
             document.getElementById("lnkSchHide").style.display="none";
             document.getElementById("lnkSchShow").style.display="Block";
        }
        if(type==2)
        {
            //Related Stories
             Effect.BlindUp('mnRelStorCont');
             document.getElementById("lnkRelHide").style.display="none";
             document.getElementById("lnkRelShow").style.display="Block";
        }
        if(type==3)
        {
            //Menu
             Effect.BlindUp('mnMenuCont');
             document.getElementById("lnkMenuHide").style.display="none";
             document.getElementById("lnkMenuShow").style.display="Block";
        }
     }
     
     //Show method
     function Show(type)
     {
        if(type==1)
        {
            //schedule video
             Effect.BlindDown('mnShcVideoCont');
             document.getElementById("lnkSchShow").style.display="none";
             document.getElementById("lnkSchHide").style.display="block";
        }
        if(type==2)
        {
             //Related Stories
             Effect.BlindDown('mnRelStorCont');
             document.getElementById("lnkRelShow").style.display="none";
             document.getElementById("lnkRelHide").style.display="block";
        }
        if(type==3)
        {
            //Menu
             Effect.BlindDown('mnMenuCont');
             document.getElementById("lnkMenuShow").style.display="none";
             document.getElementById("lnkMenuHide").style.display="Block";
        }
     }


//Select video

function selectVid(obj)
{

    //Change images
    
    obj.src="images/imgselect.gif";
    
    if($("slide1") && obj.id!="slide1")
    {
        $("slide1").src="images/imgNextVid.gif";
    }
    if($("slide2") && obj.id!="slide2")
    {
        $("slide2").src="images/imgNextVid.gif";
    }
    if($("slide3") && obj.id!="slide3")
    {
        $("slide3").src="images/imgNextVid.gif";
    }
    if($("slide4") && obj.id!="slide4")
    {
        $("slide4").src="images/imgNextVid.gif";
    }
    if($("slide5") && obj.id!="slide5")
    {
        $("slide5").src="images/imgNextVid.gif";
    }
    
}

/********** AJAX FUNCTIONS *******************/

//InsertSchedule video
function scheduleVideo()
{
            var strYoutubeID=document.getElementById('txtYoutubeID').value
            var strStartDate=document.getElementById('ctl00_ContentPlaceHolder1_txtStartDate').value;
            var strEndDate=document.getElementById('ctl00_ContentPlaceHolder1_txtEndDate').value;
            //Get Video ID
            strYoutubeID=strYoutubeID.substring(strYoutubeID.lastIndexOf("=")+1);
                        
            if(strStartDate!="" && strEndDate!="" && strYoutubeID!="")
            {
                //Date objects
                var startDate=new Date(strStartDate);
                var endDate=new Date(strEndDate);
                
                if(endDate>startDate)
                {
                    //Insert parent video
                    new Ajax.Request('Service.asmx/InsertScheduleDate',
                    {   
                        postBody:"{strVideoID:'"+strYoutubeID+"',strStartDate:'"+strStartDate+"',strEndDate:'"+strEndDate+"'}",
                        method: 'post', contentType:'application/json; charset=utf-8',
                        onSuccess: onInsertVideoSuccess,
                        onFailure: onInsertVideoFailure
                    });
                    
                    
                    //add More videos
                     var rCount=$('addMVid').rows.length;
                     var objTable=$('addMVid');   
                    
                    if(rCount>0)
                    {
                            for(var intCount=0;intCount<rCount;intCount++)
                            {
                               
                                  //Get Text value
                                   var objTxt=objTable.rows[intCount].cells[1].childNodes[0];
                                   
                                   var strUrl=objTxt.value;
                                   strUrl=strUrl.substring(strUrl.lastIndexOf("=")+1);
                                  
                                   //Add child videos
                                  new Ajax.Request('Service.asmx/InsertScheduleDate',
                                  {   
                                    postBody:"{strVideoID:'"+strUrl+"',strStartDate:'"+strStartDate+"',strEndDate:'"+strEndDate+"'}",
                                    method: 'post', contentType:'application/json; charset=utf-8',
                                    onSuccess: onInsertVideoSuccess,
                                    onFailure: onInsertVideoFailure
                                  });
                               
          
                            }
                        
                    }
                   
                  
                }
                else
                {
                    //Not valid date
                    alert("End date should be greater than start date.");        
                }   
            }
            else
            {
                alert("All fields are mandatory");
            }
}
   
 //Ajax success function     
function onInsertVideoSuccess(result)
{
    alert("Insert Sucessfully");
     __doPostBack('ctl00_ContentPlaceHolder1_updSchVideo', '');
}
 
//Ajax Failure function       
function onInsertVideoFailure(result)
{
         alert('Ajax Error');
}


//Update schedule video
function UpdateVideo()
{
            //Get all the values for update
            var strTitle=document.getElementById('ctl00_ContentPlaceHolder1_txtTitle').value;
            //get Youtube id
            strTitle=strTitle.substring(strTitle.lastIndexOf("=")+1);
            alert(strTitle);
            var strVideoID=document.getElementById('ctl00_ContentPlaceHolder1_hdnVideoID').value;
            var strScheduleID=document.getElementById('ctl00_ContentPlaceHolder1_hdnSchID').value;
            
            if(strTitle!="")
            {
                    //Updating video via ajax
                    new Ajax.Request('Service.asmx/UpdateScheduleVideo',
                          {   
                            postBody:"{strScheduleID:'"+strScheduleID+"',strYouTubeID:'"+strTitle+"'}",
                            method: 'post', contentType:'application/json; charset=utf-8',
                            onSuccess: onUpdateVideoSuccess,
                            onFailure: onUpdateVideoFailure
                          });
            }
            else
            {
                alert("All fields are mandatory");                
            }
            
          
} 

 //Ajax success function        
function onUpdateVideoSuccess(result)
{
    alert("Video has been update successfully.");
    document.getElementById('ctl00_ContentPlaceHolder1_txtTitle').value="";
}
 
 //Ajax failure function        
function onUpdateVideoFailure(result)
{
    alert("Ajax Error.");
}


//Delete Schedule Video
function DeleteVideo()
{
        var objTable=document.getElementById('tblSchVideo');
        var strID="";
        var arrID=new Array();
        var intFlag=0;
        //get checked checkboxes
        for(var intCount=0;intCount<objTable.rows.length;intCount++)
        {
            //Only rows not table header
            if(intCount!=0)
            {
               
                //Get id of check box
                var objCheck=document.getElementById(objTable.rows[intCount].cells[7].childNodes[0].id);
               
                if(objCheck.checked)
                { 
                   //Get Schedule id
                    arrID[intFlag]=objCheck.id.substring(3);
                    intFlag++;
                }
            }
        }
        
        //Get array value
        for(var intCount=0;intCount<arrID.length;intCount++)
        {
            if(intCount==0)
            {
               strID+=arrID[intCount];
            }
            else
            {
                strID+=","+arrID[intCount];            
            }
        }
        
        if(arrID.length!=0)
        {
           
            //Delete schedule video by ajax
            new Ajax.Request('Service.asmx/DeleteScheduleVideo',
            {   
                            postBody:"{strScheduleID:'"+strID+"'}",
                            method: 'post', contentType:'application/json; charset=utf-8',
                            onSuccess: OnVideoDeleteSuccess,
                            onFailure: OnVideoDeleteFailure
            });
            
        }
        else
        {
            alert("Select checkbox to delete");
        }
        
        
     }
     
function OnVideoDeleteSuccess(result)
{
      
    //Refresh update panel
    alert("Schedule video has been deleted successfully");        
    __doPostBack('ctl00_ContentPlaceHolder1_updSchVideo', '');
       
}
     
function OnVideoDeleteFailure(result)
{
    alert('Ajax Error');
}

function addMenu()
{
            //Get playlist url
            var strURL=document.getElementById('txtPlayUrl').value;
            var strName=document.getElementById('txtMenuUrl').value;
            if(strURL!="")
            {
                //Get Playlist id
                strURL=strURL.substring(strURL.lastIndexOf('/')+1);
                     new Ajax.Request('Service.asmx/InsertPlaylistDetails',
                    {   
                                postBody:"{strName:'"+strName+"',strPlaylistID:'"+strURL+"'}",
                                method: 'post', contentType:'application/json; charset=utf-8',
                                onSuccess: OnaddMenuSuccess,
                                onFailure: OnaddMenuFailure
                    });
              
            }
}

function OnaddMenuSuccess(result)
{
    alert("Menu has been addded successfully");
}

function OnaddMenuFailure(result)
{
    alert("Ajax Error");
}

//Update menu detail
function UpdateMenu()
{
    var txtMenu=$F('ctl00_ContentPlaceHolder1_txtMuName');
    var txtUrl=$F('ctl00_ContentPlaceHolder1_txtUrl')
    var strPlaylistId=$F('ctl00_ContentPlaceHolder1_hdnPlaylistID');
    if(txtMenu !="" && txtUrl!="")
    {
        //Get youtube playlist id
        txtUrl=txtUrl.substring(txtUrl.lastIndexOf('/')+1);
        
        //Ajax method for update
        new Ajax.Request('Service.asmx/UpdateMenu',
                          {   
                            postBody:"{strPlaylistID:'"+strPlaylistId+"',strMenuName:'"+txtMenu+"',strUrl:'"+txtUrl+"'}",
                            method: 'post', contentType:'application/json; charset=utf-8',
                            onSuccess: onUpdateMenuSuccess,
                            onFailure: onUpdateMenuFailure
                          });
    }
    else
    {
        alert("All fields are mandatory");
    }
    
     
}

function onUpdateMenuSuccess(result)
{
    
        alert("Update sucessfully");
       
}

function onUpdateMenuFailure(result)
{
    alert("Ajax Error:"+result);
}

        

function deleteEvent()
{
     var intType=0;
        
       if($('tblEvent'))
       {
         if($('tblEvent').rows.length!=0)   
         {
             var objTable=document.getElementById('tblEvent');
             intType=1;
         }
       }
       if($('tblPrgram'))
       {
            if($('tblPrgram').rows.length!=0)
            {
                var objTable=document.getElementById('tblPrgram');
                intType=2;
            }
       }
       
       if($('tblInterview'))
       {
            if($('tblInterview').rows.length!=0)
            {
                var objTable=document.getElementById('tblInterview');
                intType=3;
            }
       }
       
       
        var strID="";
        var arrID=new Array();
        var intFlag=0;
        //get checked checkboxes
        for(var intCount=0;intCount<objTable.rows.length;intCount++)
        {
            //Only rows not table header
            if(intCount!=0)
            {
                //Get id of check box
                var objCheck=document.getElementById(objTable.rows[intCount].cells[3].childNodes[0].id);
       
                if(objCheck.checked)
                { 
                   //Get id
                    arrID[intFlag]=objCheck.id.substring(3);
                    intFlag++;
                }
            }
        }
        //Get array value
        for(var intCount=0;intCount<arrID.length;intCount++)
        {
            if(intType==1)
            {
                 //Delete events by ajax
                 
                new Ajax.Request('Service.asmx/DeleteEvents',
                {   
                                postBody:"{strEventID:'"+arrID[intCount]+"'}",
                                method: 'post', contentType:'application/json; charset=utf-8',
                                onSuccess: OnEventDeleteSuccess,
                                onFailure: OnEventDeleteFailure
                });
            }
            if(intType==2)
            {
                 new Ajax.Request('Service.asmx/DeletePrograms',
                {   
                                postBody:"{strProgramID:'"+arrID[intCount]+"'}",
                                method: 'post', contentType:'application/json; charset=utf-8',
                                onSuccess: OnEventDeleteSuccess,
                                onFailure: OnEventDeleteFailure
                });
            }
             if(intType==3)
            {
                 new Ajax.Request('Service.asmx/DeleteInterview',
                {   
                                postBody:"{strInterviewID:'"+arrID[intCount]+"'}",
                                method: 'post', contentType:'application/json; charset=utf-8',
                                onSuccess: OnEventDeleteSuccess,
                                onFailure: OnEventDeleteFailure
                });
            }
           
        }
        
        if(arrID.length!=0)
        {
           
           
            
        }
        else
        {
            alert("Select checkbox to delete");
        }
         //__doPostBack('ctl00_ContentPlaceHolder1_udpEvent', '');  
}


function OnEventDeleteSuccess(result)
{
    //Refresh updatepanel
    __doPostBack('ctl00_ContentPlaceHolder1_udpEvent', '');   
  
}

function OnEventDeleteFailure(result)
{
    alert("Ajax Error");
}

//Open popup
function Openpopup(strImgPath)
{
   
   
     //Set height and width
	 $('shad').style.height="1485px";
	 $('shad').style.width=document.body.clientWidth;
	 
	 //Set Image
	
	 $('imgFull').src="upload/"+strImgPath;
	
	 
	 //Display divs
	 $('shad').style.display="inline";
	 $('popup').style.display="inline";
      
}

//Close popup
function closepop()
{
     //Hide divs
     $('shad').style.display="none";
	 $('popup').style.display="none";
}

//Delete Menu
function DeleteMenu()
{
    if($('tblMenu').rows.length!=0)   
    {
    
        objTable=$('tblMenu')
        
             var strID="";
            var arrID=new Array();
            var intFlag=0;
            //get checked checkboxes
            for(var intCount=0;intCount<objTable.rows.length;intCount++)
            {
                //Only rows not table header
                if(intCount!=0)
                {
                    //Get id of check box
                    var objCheck=document.getElementById(objTable.rows[intCount].cells[3].childNodes[0].id);
           
                    if(objCheck.checked)
                    { 
                       //Get id
                        arrID[intFlag]=objCheck.id.substring(4);
                        intFlag++;
                    }
                }
            }
            //Get array value
            for(var intCount=0;intCount<arrID.length;intCount++)
            {
                    
                         //Delete Menu by ajax
                                              
                       new Ajax.Request('Service.asmx/DeleteMenu',
                        {   
                                        postBody:"{strMenuID:'"+arrID[intCount]+"'}",
                                        method: 'post', contentType:'application/json; charset=utf-8',
                                        onSuccess: OnDeleteMenuSuccess,
                                        onFailure: OnDeleteMenuFailure
                        });
                  
            
            }
    }
}

function OnDeleteMenuSuccess(result)
{
    //Refresh update panel
     __doPostBack('ctl00_ContentPlaceHolder1_updMenu', '');

}

function OnDeleteMenuFailure(result)
{

}


//Refresh Featured Video
function refreshVideo(videoID,auto,height,width,type,action)
{
           
            //values for action
            //action=0 when image button is pressed
            //action=1 when next button is pressed
            //action=2 when previous button is pressed 
            
            var objVideo="<object height='"+height+"' width='"+width+"'>";
         
            //Set video id
            objVideo+="      <param name='movie' value='http://www.youtube.com/v/" + videoID + "&amp;fs=1&amp;rel=0&amp;color1=0x2b405b&amp;color2=0x6b8ab6&amp;border=1&amp;fs=1'>";
            objVideo+="      <param name='movie' value='http://www.youtube.com/v/" + videoID + "&amp;fs=1&amp;rel=0&amp;color1=0x2b405b&amp;color2=0x6b8ab6&amp;border=1&amp;fs=1'>";
            objVideo+="      <param name='allowFullScreen' value='true'>";
	        objVideo+="      <param name='wmode' value='transparent'>";
            objVideo+="      <embed src='http://www.youtube.com/v/" + videoID + "&amp;fs=1&amp;egm=0&amp;rel=0&amp;color1=0x000000&amp;color2=0x000000&amp;border=0&amp;autoplay=";
           
            //Set Autoplay
            objVideo+=""+               auto+"&amp;showinfo=0&amp;iv_load_policy=3&amp;cc_load_policy=1' type='application/x-shockwave-flash' allowfullscreen='true' wmode='transparent' width='"+width+"' height='"+height+"'>";
            objVideo+="      </embed>";
            objVideo+="</object>";
           
            //Set Video player
            document.getElementById('vid'+type).innerHTML=objVideo;
            
            //Change images(selection image)
            
            var btnPressed="";
            
            var obj="";
            
            if(action==1)
            {
                btnPressed="slide"+(Number(type)+Number('1'));
                obj=$(btnPressed);
            }
            
            if(action==2)
            {
                btnPressed="slide"+(Number(type)-Number('1'));
                obj=$(btnPressed);
            }
            
            if(action!=0)
            {
                //Select image
                obj.src="images/imgselect.gif";
                
                if($("slide1") && obj.id!="slide1")
                {
                    $("slide1").src="images/imgNextVid.gif";
                }
                if($("slide2") && obj.id!="slide2")
                {
                    $("slide2").src="images/imgNextVid.gif";
                }
                if($("slide3") && obj.id!="slide3")
                {
                    $("slide3").src="images/imgNextVid.gif";
                }
                if($("slide4") && obj.id!="slide4")
                {
                    $("slide4").src="images/imgNextVid.gif";
                }
                if($("slide5") && obj.id!="slide5")
                {
                    $("slide5").src="images/imgNextVid.gif";
                }
            }
                      
}

function createVideo(videoID,auto,height,width)
{
  
            //var objVideo="<iframe class='youtube-player' type='text/html' width='"+width+"' height='"+height+"' src=http://www.youtube.com/embed/" + videoID  + " frameborder='0'></iframe>";
            var objVideo="<object height='"+height+"' width='"+width+"'>";
         
            //Set video id
            objVideo+="      <param name='movie' value='http://www.youtube.com/v/" + videoID + "&amp;fs=1&amp;rel=0&amp;fs=1'>";
            objVideo+="      <param name='movie' value='http://www.youtube.com/v/" + videoID + "&amp;fs=1&amp;rel=0amp;fs=1'>";
            objVideo+="      <param name='allowFullScreen' value='true'>";
	        objVideo+="      <param name='wmode' value='transparent'>";
            objVideo+="      <embed src='http://www.youtube.com/v/" + videoID + "&amp;fs=1&amp;egm=0&amp;rel=0&amp;autoplay=";
           
            //Set Autoplay
           objVideo+=""+               auto+"&amp;showinfo=0&amp;iv_load_policy=3&amp;cc_load_policy=1' type='application/x-shockwave-flash' allowfullscreen='true' wmode='transparent' width='"+width+"' height='"+height+"'>";
           objVideo+="      </embed>";
           objVideo+="</object>";
           
            //return Video player
           
            return objVideo ;
    
}

function setMainVideo(img,type,videoID)
{
    var strVideoPlayer="";
    strVideoPlayer=createVideo(videoID,'0','257','419');
    $("ctl00_ContentPlaceHolder1_dvMainVideo").innerHTML=strVideoPlayer;
       
    if(type==0)
    {
      $("ctl00_ContentPlaceHolder1_hTitle").innerHTML=$("ctl00_ContentPlaceHolder1_hdn1Title").value;
      $("ctl00_ContentPlaceHolder1_pDescription").innerHTML=$("ctl00_ContentPlaceHolder1_hdn1Desc").value;
    }
    if(type==1)
    {
      $("ctl00_ContentPlaceHolder1_hTitle").innerHTML=$("ctl00_ContentPlaceHolder1_hdn2Title").value;
      $("ctl00_ContentPlaceHolder1_pDescription").innerHTML=$("ctl00_ContentPlaceHolder1_hdn2Desc").value;
    }
    if(type==2)
    {
      $("ctl00_ContentPlaceHolder1_hTitle").innerHTML=$("ctl00_ContentPlaceHolder1_hdn3Title").value;
      $("ctl00_ContentPlaceHolder1_pDescription").innerHTML=$("ctl00_ContentPlaceHolder1_hdn3Desc").value;
    }
    if(type==3)
    {
      $("ctl00_ContentPlaceHolder1_hTitle").innerHTML=$("ctl00_ContentPlaceHolder1_hdn4Title").value;
      $("ctl00_ContentPlaceHolder1_pDescription").innerHTML=$("ctl00_ContentPlaceHolder1_hdn4Desc").value;
    }
   if(type==4)
   {
      $("ctl00_ContentPlaceHolder1_hTitle").innerHTML=$("ctl00_ContentPlaceHolder1_hdn5Title").value;
      $("ctl00_ContentPlaceHolder1_pDescription").innerHTML=$("ctl00_ContentPlaceHolder1_hdn5Desc").value;
   }
 
   
    
    $("ctl00_ContentPlaceHolder1_dvMainVideo").innerHTML = "<div>"+strVideoPlayer+"</div>";
    
  
    //this.id.parentNode.className="bot_cor_sel";
    
    var nodes=$('ctl00_ContentPlaceHolder1_dvThumbnail').select('div');
    for(var intCount=0;intCount<= nodes.length-1;intCount++)
    {
        nodes[intCount].className="bot_cor";    
    }
    $(img).parentNode.className="bot_cor_sel";
   
}





