var currentRecord;
var descriptionWidth = 396;
var tMenuMediaSub = false;
var tMenuMedia = false;
var tMenuFavourite = false;
var loading_page = true;
var current_playlist = 'archive';

function getFlashMovie(movieName) {
  var isIE = navigator.appName.indexOf("Microsoft") != -1;
  return (isIE) ? window[movieName] : document[movieName];
 }

function longTitle(){

    var title = $('#divVideoWrapper #divTitle h1').html();
    titleWidth = ($('#divVideoWrapper #divTitle h1').width());
    free = descriptionWidth - $('#divVideoWrapper #divTitle .divMedia').width();

    /*if(titleWidth > free  ){
        description = title + '<br>'+$('.details_scroll p').html();
        $('.details_scroll p').html(description);
    }*/
    while(titleWidth > free  ){
        newLenght = title.length * (free /titleWidth) - 2;
        title = title.slice(0,newLenght)+'..';
        $('#divVideoWrapper #divTitle h1').html(title);
        titleWidth = ($('#divVideoWrapper #divTitle h1').width());
    }
    $('.details_scroll').jScrollPane({scrollbarWidth:15, showArrows:true, scrollbarMargin:1, dragMinHeight:20});
}
function playPodcasting( data, mtype ){
    //alert(data);
	if (!data) return false;
    //return true;
    data = eval(data);
    $('#divVideoWrapper #divTitle h1').html(data.posting.title);
    $('#divVideoWrapper #divTitle').attr('postingId',data.posting.id);
    $('#divVideo').html('<img height="246" width="320" alt="" src="/images/channelimg2.jpg"/>');
    if(data.posting.video_file==null) $('#divVideoWrapper #divTitle .divMedia .video_m').hide();
        else  $('#divVideoWrapper #divTitle .divMedia .video_m').show();
    if(data.posting.audio_file==null) $('#divVideoWrapper #divTitle .divMedia .audio_m').hide();
        else  $('#divVideoWrapper #divTitle .divMedia .audio_m').show();
    details = '<ul><li>' + data.speaker_name + '</li><li class="separator"/><li>' + data.posting.posted + '</li>';
    if ((data.posting.video_file!=null && data.posting.video_download_main == 1) ||
       (data.posting.audio_file!=null && data.posting.audio_download_main == 1)){
        details += '<li class="separator"/><li style="padding: 0 0 0 5px" >Download:</li>';
        downloadAudio='http://sermonplayer.com/download.php?c='+data.posting.clientid+'-count_audio_download-'+data.posting.id;
        downloadVideo='http://sermonplayer.com/download.php?c='+data.posting.clientid+'-count_video_download-'+data.posting.id;
        if (data.posting.audio_file!=null && data.posting.audio_download_main == 1){
            details += '<li style="padding: 0px"><a href="'+downloadAudio+'">Audio</a></li>'
        }
        if (data.posting.video_file!=null && data.posting.video_download_main == 1){
            details += '<li style="padding: 0px"><a href="'+downloadVideo+'">Video</a></li>'
        }
    }
//    alert(data.posting.clienturl);
    details += data.posting.pdf_file  ?  '<li  class="pdf"><a target="blank" href="'+dataUrl+data.posting.url+'/pdf/'+data.posting.pdf_file+'"/></li>' : '<li style="display: none;" class="pdf"><a href="#"/></li>';
    details += '</ul><div class="clear"/>';
    flashvars = '';
    flashvars += '&image='+data.artwork.path;
    //flashvars += '&id=jopa';

    //start player
    if(mtype == 'auto'){
        mtype='audio';

        if (data.posting.video_file!=null){
            mtype = 'video';
        }
        if (data.posting.live){
            mtype = 'live';
        }
    } else {
        flashvars += '&autostart=true'; // If I click directly on a media icon in this Archive section it should auto-play
    }

    if(mtype=='live'){
        flashvars += '&file='+infoUrl+'/xml/live_posting.php?posting='+data.posting.id   ;//data.posting.live_id+'.flv&streamer='+streamer;
        flashvars += '&autostart=true&viral.oncomplete=false';
    }
    if(mtype=='audio'){
        flashvars+='&file='+infoUrl+'/download.php?c='+data.posting.clientid+'-count_audio_mini-'+data.posting.id+'.mp3' ;
        $('#divVideoWrapper #divTitle .divMedia .video_m').removeClass('video_m_act');
        $('#divVideoWrapper #divTitle .divMedia .audio_m').addClass('audio_m_act');
        }
    if(mtype=='video'){
        flashvars+='&file='+infoUrl+'/download.php?c='+data.posting.clientid+'-count_video_mini-'+data.posting.id+'.mp4'  ;
        $('#divVideoWrapper #divTitle .divMedia .audio_m').removeClass('audio_m_act');
        $('#divVideoWrapper #divTitle .divMedia .video_m').addClass('video_m_act');
    }

   if  (data.posting.speaker_name === "" ||
        data.posting.speaker_name === null ||
        data.posting.speaker_name === undefined
    ) {viral_author =  data.speaker_name}
    else {viral_author =data.posting.speaker_name};
    
    
     if  (true){
    vir_link = 'http://sermon.net'+baseUrl +'/sermonid/'+data.posting.id;}
    else {
    vir_link = baseUrl +'.sermon.net/sermonid/'+data.posting.id;
    };
     
    
    flashvars+=playerString+'&title='+escape(data.posting.title)+'&author=' + escape(viral_author) + '&date=' + escape(data.posting.posted) + '&description=' + escape(data.posting.description) + '&viral.link=' + escape(vir_link) ;
   
   //if ('4182'== idChannelCurrent) {
    //   flashvars += 'vid='+ data.posting.id +'&context=plid%3D'+ playlistId ;
   // }          
     //  if (typeof console == 'object'){ console.log('idChannelCurrent = ' + idChannelCurrent + 'flashvars' + flashvars + '; /n\nplaylistId = ' + current_playlist); }
    so.addParam('wmode','transparent');
    so.addVariable('plugins','adotube');
    so.addVariable('adotube.omlsource','http://www.adotube.com/docs/adapters/OP/sampleOML.xml');
    so.addParam('flashvars',flashvars);
    so.write('divVideo');
    //set Note and comments
    setNotesComment(data.posting.id);
    //set details

    
//	for(k in data){
//	    alert(k+'='+data[k]);
//	}
    data_detal = '';
    if(data.series_name_category!=null && data.series!=null){
	data_detal += '<b>' + data.series_name_category + ' :</b> '; 
	//alert(data.series_name_category);
	data_detal += data.series + '<br>';
    }
    
    if(data.scripture_name_category!=null && data.scripture!=null){
	data_detal +='<b>' + data.scripture_name_category + ' :</b> ';
	data_detal += data.scripture + '<br>';
    } 
    data_detal += (data.scripture_name_category!=null || data.series_name_category!=null ? '<br>' : '') + data.posting.description;

    $('#detailsBox .divTopicDetails').html(details);//
    $('.details_scroll p').html(data_detal);
    
    
    
    /// Descriptions tags
    tags = '<div class="clear"></div>';
    if(data.first_keyword){
        tags+='<span>Tags :</span><ul>';
       // alert(data.keywords.length);
        for( i = 1; i<data.keywords.length; i++){
            tags += '<li><a href="'+tagsUrl+data.keywords[i]+'">'+data.keywords[i] +',</a></li>';
        }
        tags += '<li><a href="'+tagsUrl+data.first_keyword+'">'+data.first_keyword +'</a></li>';
    }
    else {
        tags+='<ul>';
    }
    tags += '<li class="star">'+$('li.star').html()+'</li></ul><div class="clear"></div>';
    $('#divTags').html(tags);
    $('#divTags a').unbind();
    $('#divTags a').click(function() {
            keyword = $(this).html();
            keyword = keyword.replace(',', '');

            searchTag(keyword,channelId);
            return false;
    });
    currentPodcastRating = data.rating;
    ratingOn(data.rating,false);


    $('#star1, #star2, #star3,  #star4,  #star5').unbind();
    $('#star1').click(function(){rate(1,$('#divTitle').attr('postingId'));});
    $('#star2').click(function(){rate(2,$('#divTitle').attr('postingId'));});
    $('#star3').click(function(){rate(3,$('#divTitle').attr('postingId'));});
    $('#star4').click(function(){rate(4,$('#divTitle').attr('postingId'));});
    $('#star5').click(function(){rate(5,$('#divTitle').attr('postingId'));});

    $('#star1').mouseover(function(){ratingOn(1,true);});
    $('#star2').mouseover(function(){ratingOn(2,true);});
    $('#star3').mouseover(function(){ratingOn(3,true);});
    $('#star4').mouseover(function(){ratingOn(4,true);});
    $('#star5').mouseover(function(){ratingOn(5,true);});

    longTitle();

    if (mtype=='audio') {
/*/      share = '<embed src="http://sermon.net/swf/snmv.swf" quality="high" width="294 " name="mpp" align="middle" allowScriptAccess="always" wmode="transparent" type="application/x-shockwave-flash" flashvars="sermonid=%POSTING%&d=http://sermon.net/" pluginspage="http://www.macromedia.com/go/getflashplayer"></embed>';*/
		if ( (1 == noAds) || (1 == mst) )
			{
			share = '<embed src="http://sermon.net/swf/ma_no_ads.swf" quality="high" width="290" height="65" name="mpp" align="middle" allowScriptAccess="always" wmode="transparent" type="application/x-shockwave-flash" flashvars="poid=%POSTING%&d=http://www.sermon.net/" pluginspage="http://www.macromedia.com/go/getflashplayer"></embed>';
		}
		else
			{
			share = '<embed src="http://sermon.net/swf/ma.swf" quality="high" width="290" height="65" name="mpp" align="middle" allowScriptAccess="always" wmode="transparent" type="application/x-shockwave-flash" flashvars="poid=%POSTING%&d=http://www.sermon.net/" pluginspage="http://www.macromedia.com/go/getflashplayer"></embed>';
			}
    } else {
	share = $('#divVideo').html();
    }

    //share = $('#divVideo').html();
    //share = share.replace('&amp;autostart=true',''); //does not autoplay in generating embed
    share = share.replace('%POSTING%', data.posting.id); //does not autoplay in generating embed
//    alert(baseUrl);
    
//    

if ((1 == clientIsFree)  ) {pconf.bookmarkURL =  'http://sermon.net' + baseUrl +'/sermonid/'+data.posting.id;}
    else if (1 == mst)  { pconf.bookmarkURL =   baseUrl +'/sermonid/'+data.posting.id;/*.sermon.net */}
	else { pconf.bookmarkURL =  'http:/' + baseUrl +'.sermon.net/sermonid/'+data.posting.id; }

//if (typeof console == 'object')  console.log('clientIsFree = ' + clientIsFree);
   //if (typeof console == 'object'){ console.log('pconf.bookmarkURL  = ' + pconf.bookmarkURL); }
   
    pconf.emailSubject = data.posting.title;
//alert(data.web);         
    pconf.emailBody = '$sender$ has sent you a link to the following file.<br/> <br/>Title: '+ data.posting.title + '<br/>Speaker: '+data.speaker_name +' <br/>Web Site: '+ data.web +' <br/><br/>Click here to play MP3 / Video file: &nbsp; '+pconf.bookmarkURL+'  <br/>Thank you!';

    pconf.defaultContent  = share;
    Wildfire.initPost('136821', 'divWildfirePost', 390, 180, pconf);

    changeTab('details');

    /*if(loading_page){
        loading_page = false;
        sc = parseInt($('#divTabContent .divTopicLinksActive').offset().top) - parseInt($('.mjn_scroll').offset().top);
        //sc = - parseInt($('.mjn_scroll').offset().top);
        if($('.mjn_scroll').jScrollPane({scrollbarWidth:15, showArrows:true, scrollbarMargin:1, dragMinHeight:20})[0].scrollTo)
            $('.mjn_scroll').jScrollPane({scrollbarWidth:15, showArrows:true, scrollbarMargin:1, dragMinHeight:20})[0].scrollTo(sc,true);
    }*/

    //Set Adds
    var rand_no = Math.floor(Math.random()*100000);
    str = 'http://www.sermon.net/openx/www/delivery/ck.php?n=aec482f9&cb='+rand_no;
    $('#divSponsor a').attr('href',str);
    str = 'http://www.sermon.net/openx/www/delivery/avw.php?zoneid=11&cb='+rand_no+'&n=aec482f9';
    $('#divSponsor img').attr('src',str);
    str = 'http://www.sermon.net/openx/www/delivery/ck.php?n=ae1aceab&cb='+rand_no
    $('.topBanner a').attr('href',str);
    str = 'http://www.sermon.net/openx/www/delivery/avw.php?zoneid=12&cb='+rand_no+'&n=ae1aceab'
    $('.topBanner img').attr('src',str);
}
function listItem( posting ) {
    appendStr = '<div class="divTopicLinks"  postingId="'+posting.id+'" clientId="'+posting.clientid+'"><div class="divImg">';
    appendStr+='<img src="'+posting.podcast_image+'" width="30" height="30" alt="" /></div>';
    appendStr+= '<div class="divDetails"><p class="title">'+posting.title+'</p>';
    appendStr+= '</div><div class="divMedia">';
    appendStr+= posting.video_file ? '<a style="cursor:pointer" class="video"></a>' : '';
    appendStr+= posting.audio_file ? '<a style="cursor:pointer" class="audio"></a>' : '';//dataUrl+data.posting.url
//    alert(posting.url);
    appendStr+= posting.pdf_file  ? '<a class="pdf" target="blank" href="'+dataUrl+posting.url+'/pdf/'+posting.pdf_file+'"/></a>' : '';
    appendStr+= '</div><div class="moredetails"><ul><li>';
    appendStr+= (posting.name == null) ? '' : posting.name;
    appendStr+=  '</li> <li class="separator"/><li>'+posting.posted+'</li><li class="separator"/><li>';
    if (posting.live == 1){
       appendStr+= posting.live_time+'</li><li class="separator"/><li>'
       appendStr+= posting.onAir ? '<span style="color:#95B73F; font-weight:bold;"> ON AIR </span>' : ' OFF AIR ';

    } else {
        appendStr+= posting.count_listeners + ' listens ';
    }
    appendStr+= '</li></ul></div><div class="clear"></div></div>';
    return appendStr;
    
}
function setPodcastingsList(data)
{
    $('#divTabContent').html('');
    var data = eval(data);
    postings = data.postings;   
    if(!postings.length & data.list == 'live'){
        $('#divTabContent').append('<div class="divDetails"><p>No upcoming live broadcasts scheduled at this time for this channel.</p></div>');
    };
    for( i = 0; i<postings.length; i++){
        posting = postings[i];
        $('#divTabContent').append(listItem(posting));
    }
    remapOnClick();
    $('.mjn_scroll').jScrollPane({scrollbarWidth:15, showArrows:true, scrollbarMargin:1, dragMinHeight:20});
}
//alert(baseUrl);
function remapOnClick(){
    $('a.video').unbind();
    $('a.audio').unbind();
    $('a.audio_m').unbind();
    $('a.video_m').unbind();
    $('.divTopicLinks').unbind();
    $('a.audio_m').click(function (event) {
            event.stopPropagation();
            if($(this).hasClass('audio_m_act'))return false;
           posting = $(this).parent().parent().attr('postingId');
            $('#divVideo').html('<img src="images/loading.gif" />');
            $.get(baseUrl,{ajax: 1, sermonid:posting},function(data){
                          playPodcasting(data, 'audio');
                    }
                ,"json");

          $(this).toggleClass('audio_m_act');
          return false;
        });
    $('a.video_m').click(function (event) {
            event.stopPropagation();
            if($(this).hasClass('video_m_act'))return false;
           posting = $(this).parent().parent().attr('postingId');
            $('#divVideo').html('<img src="images/loading.gif" />');
            $.get(baseUrl,{ajax: 1, sermonid:posting},function(data){

                          playPodcasting(data, 'video');
                    }
                ,"json");

          $(this).toggleClass('video_m_act');
          return false;
        });

    $('#playlistsBox a.video').click(function (event) {
            posting = $(this).parent().parent().attr('postingId');
            client  = $(this).parent().parent().attr('clientId');
            url = baseUrl+'?type=video&c='+client+'&sermonid='+posting;
            window.location = url;
            return false;
    });
    $('#playlistsBox a.audio').click(function (event) {
            posting = $(this).parent().parent().attr('postingId');
            client  = $(this).parent().parent().attr('clientId');
            url = baseUrl+'?type=audio&c='+client+'&sermonid='+posting;
            window.location = url;
            return false;
    });
    $('#playlistsBox a.pdf').click(function (event) {
            posting = $(this).parent().parent().attr('postingId');
            client  = $(this).parent().parent().attr('clientId');
            url = baseUrl+'?c='+client+'&sermonid='+posting;
            window.location = url;
            return true;
    });
    $('#playlistsBox .divTopicLinks').click(function (event) {
            event.stopPropagation();
            posting = $(this).attr('postingId');
            client  = $(this).attr('clientId');
            url = baseUrl+'?c='+client+'&sermonid='+posting;
            
            //alert(url);
            
            window.location = url;
            return false;
    });

    $('.divTopicLinks a.pdf').click(function (event) {
        event.stopPropagation();
         $('.divTopicLinks').removeClass('divTopicLinksActive');
            $(this).parent().parent().addClass('divTopicLinksActive');
            posting = $(this).parent().parent().attr('postingId');
            $('#divVideo').html('<img src="images/loading.gif" />');
            $.get(baseUrl,{ajax: 1, sermonid:posting},function(data){
                          playPodcasting(data, 'auto');
                    }
        ,"json");
        return true;
    });
    $('#divTabContent a.video').click(function (event) {
            event.stopPropagation();
            $('.divTopicLinks').removeClass('divTopicLinksActive');
            $(this).parent().parent().addClass('divTopicLinksActive');
            posting = $(this).parent().parent().attr('postingId');
            $('#divVideo').html('<img src="images/loading.gif" />');
            $.get(baseUrl,{ajax: 1, sermonid:posting},function(data){
                          playPodcasting(data, 'video');
                    }
                ,"json");
            return false;
        });
    $('#divTabContent a.audio').click(function (event) {
            event.stopPropagation();
            $('.divTopicLinks').removeClass('divTopicLinksActive');
            $(this).parent().parent().addClass('divTopicLinksActive');
            posting = $(this).parent().parent().attr('postingId');
            $('#divVideo').html('<img src="images/loading.gif" />');
            $.get(baseUrl,{ajax: 1, sermonid:posting},function(data){
                          playPodcasting(data, 'audio');
                    }
                ,"json");


            return false;
        });
    $('#divTabContent .divTopicLinks').click(function(event){
            event.stopPropagation();
            if(!$(this).hasClass('divTopicLinksActive')){
                $('.divTopicLinks').removeClass('divTopicLinksActive');
                $(this).addClass('divTopicLinksActive');
                posting=$(this).attr('postingId');
                event.preventDefault();
                    pconf.bookmarkURL = baseUrl +'/sermonid/'+posting; 
                    //alert(pconf.bookmarkURL);
                     Wildfire.divWildfirePost.applyConfig(pconf);
                    // if (typeof console == 'object'){console.log('pconf'+pconf.bookmarkURL);  }
                $('#divVideo').html('<img src="images/loading.gif" />');
                $.get(baseUrl,{ajax: 1, sermonid:posting, all:1},function(data){
                              playPodcasting(data, 'auto');
                        }
                    ,"json");
            }
            return false;
        });
}
var currentTab = 'details';
var opera = ''; //Opera hack for flash
//alert(selfLink);

function loadChannelPlaylist(playlist,text){
//alert(selfLink);
       
  current_playlist = playlist;
   

            $.get(baseUrl,{ajax: 1, list:playlist},function (data) {
                setPodcastingsList(data);
				if (curPosting) {
					$('.divTopicLinks[postingid='+ curPosting.posting.id +']').addClass('divTopicLinksActive');
					sc = parseInt($('#divTabContent .divTopicLinksActive').offset().top) - parseInt($('.mjn_scroll').offset().top);
                if($('.mjn_scroll').jScrollPane({scrollbarWidth:15, showArrows:true, scrollbarMargin:1, dragMinHeight:20})[0].scrollTo)
                    $('.mjn_scroll').jScrollPane({scrollbarWidth:15, showArrows:true, scrollbarMargin:1, dragMinHeight:20})[0].scrollTo(sc,true);
				}
            },"json");
            $('.divMenu span').hide();
            $('.divMenuMedia span').hide();
            //alert('.divMenuMedia span[playlistid=' + list + ']');
            $('.divMenuMedia span[playlistid=' + playlist + ']').show();
            $('#divTabMenu a.current').text(text);
}
function changeTab(newTab){
    $('#'+currentTab+'Box').toggle();
    $('li.'+currentTab+' a').toggleClass('current');
    currentTab = newTab;
    $('#'+currentTab+'Box').toggle();
    $('li.'+currentTab+' a').toggleClass('current');
    $('.'+currentTab+'_scroll').jScrollPane({scrollbarWidth:15, showArrows:true, scrollbarMargin:1, dragMinHeight:20});
}
function loadPlaylistsList(){
    $.get(baseUrl,{ajax: 1, playlists:1},
        function(data){
            var data = eval(data);
            str = '<ul>';

            for(i in data){
                list = data[i];
                str+='<li><a playlistId="'+list.id_playlist+'" style="cursor: pointer">'+list.title+'</a></li>';
            }
            str+='<hr/><li><a class="create" style="cursor: pointer">Create new</a></li></ul>';
            $('.divMenuFavourite').html(str);
                $('.divMenuFavourite a').click(function(event){
                    if(!$(this).hasClass("create")){
                        playlistId=$(this).attr('playlistId');
                        loadPlaylist(playlistId);
                        event.preventDefault();
                    } else {
                        $.get(baseUrl,{p: 'playlists', act: 'new_playlist'},
                            function(){loadPlaylistsList();});//,setPodcastingsList,"json");
                    }
                });
        },"json");
}
function loadPlaylist(playlistId){
    $.get(baseUrl,{ajax: 1, playlistid:playlistId},
               function(data){
                   var data = eval(data);
                   $('.playlists_scroll').html('');
                   $('#playlistsBox .divTopicDetails').attr('playlistId', data.playlistId);
                   $('#playlistsBox li.current').text(data.name);
                   $('#playlistsBox li.current').unbind();
                   if(data.favorites){
                       $('.divPlaylistEdit').hide();
                   } else {
                       $('#playlistsBox li.current').click(function(){$('.divPlaylistEdit').toggle();});
                   }
                   $('#playlistsBox #renameForm .txt').val(data.name);
                   $('#playlistsBox #renameForm #playlist').val(data.playlistId);
                   for( i = 0; i<data.postings.length; i++){
                        posting = data.postings[i];
                        $('.playlists_scroll').append(listItem(posting));
                    }
                    remapOnClick();
                    $('.playlists_scroll').jScrollPane({scrollbarWidth:15, showArrows:true, scrollbarMargin:1, dragMinHeight:20});
               },
   "json");
}
function setComments(list){
    $('.comment_scroll').html('');
    if(!list.length){ // нет комментов
        $('.comment_scroll').html('<p>There are no comments yet</p>');
    }
    for( i = 0; i<list.length; i++){
        comment = list[i];
        //"description":"11","avatar":"default.jpg"
        appendStr = '<div class="divChatMessage"><div class="divImg">';
        appendStr += '<a href='+dashBoardUrl+'?p=profile&uid='+comment.fk_user+'><img src="'+comment.avatar+'" alt="" width="30" height="30"></a>';
        appendStr += '</div><div class="divDetails">';
        appendStr += comment.description;
        appendStr += '</div><div class="clear"></div></div>';
        $('.comment_scroll').append(appendStr);

    }
    if($('#commentBox').css('display')!='none' & $('.comment_scroll').css('display')!='none'){
        $('.comment_scroll').jScrollPane({scrollbarWidth:15, showArrows:true, scrollbarMargin:1, dragMinHeight:20});
    }
}

function loadComments(postingId){
    $.get(baseUrl,{ajax: 1, comments:postingId},
        function(data){
            var data = eval(data);
            setComments(data);
        },"json");
}

function setNotesComment(postingId){
    loadComments(postingId);
    var soNotes = new SWFObject('/swf/obedit3.swf','mpl','395','182','9');
    soNotes.addParam('wmode','transparent');
    soNotes.addParam('flashvars','&loadURL='+baseUrl+'&tab=notes&script=notesSaved&post='+postingId+'&saveURL='+baseUrl+opera);
    soNotes.write('divNotesArea');
    var soEdit = new SWFObject('/swf/obedit3.swf','mpl','395','182','9');
    soEdit.addParam('wmode','transparent');
    soEdit.addParam('flashvars','&tab=comment&post='+posting+'&saveURL='+baseUrl+'&script=commentSended&no_print=1'+opera);
    soEdit.write('divEditArea');
}

function setChat(channel){
   //var soChat = new SWFObject('/swf/textchatnew.swf?aaaaa','mpl','310 ','31','9');
     var soChat = new SWFObject('/swf/textchat_exit.swf?aaaaa','chat','310 ','31','9');
     soChat.addParam('wmode','transparent');
      soChat.addParam("allowScriptAccess", "always");
   // soChat.addParam('flashvars','chat=chat_'+channel+'&info_url='+infoUrl+'&url='+wowzaUrl+opera);
      soChat.addVariable("chat", 'chat_'+channel);
      soChat.addVariable("info_url", infoUrl);
      soChat.addVariable("url", wowzaUrl);
      if (opera) soChat.addVariable("opera", "1");

    soChat.write('divChat');
}


var chat_visible = false;
function addChatMessage(avatar, msg, name, uid){

    appendStr = '<div class="divChatMessage"><a target="blank" href="/member/'+uid+'/profile"><div class="divImg">';
    appendStr += '<img src="'+avatar+'" alt="" width="30" height="30">';
    appendStr += '</div><div class="divDetails">';
    appendStr += name+': '+msg;
    appendStr += '</div></a><div class="clear"></div></div>';
    $('#divChatBox').append(appendStr);
    if(chat_visible){
        $('#divChatBox').jScrollPane({scrollbarWidth:15, showArrows:true, scrollbarMargin:1, dragMinHeight:20})[0].scrollTo(100000000,true);
    }
}

function slideChat(){
  //  alert('showChat');
    if(!chat_visible){
        chat_visible = true;
        $('#spanChatBox').show('slide',{ direction: 'down' },'slow');
        $('#divChatBox').jScrollPane({scrollbarWidth:15, showArrows:true, scrollbarMargin:0, dragMinHeight:20})[0].scrollTo(100000000,true);
    } else {
        chat_visible = false;
        $('#spanChatBox').hide('slide',{ direction: 'down' },'slow');
    }
}

function notesSaved(){
    $('#signConfirm').fadeIn('slow', function(){
        setTimeout("$('#signConfirm').fadeOut('slow')",3000);
    });

}
function commentSended(){
    $('#divEditArea').hide();
    $('#commentBox #divInfo, .divTopicDetails').show();
    postingId = $('#divVideoWrapper #divTitle').attr('postingId');
    loadComments(postingId);
}


$(document).ready(function(event){
//alert(baseUrl); return 0; 
    $(window).unload(
      function () {
       movie = getFlashMovie('chat');
       if (movie)
         movie.leave();
      }
    );

        //if Opera
    if($.browser.opera){ opera = '&opera=1'; };
        //alert(channelId);
     //$('#spanChatBox').hide('slide',{ direction: 'down' });
    loadComments(posting);
    if(logged){  //logged in

        setChat(channelId);
        setNotesComment(posting);
        $('#commentBox  .plus').click(function(){
                $('#divEditArea').show();
                $('#commentBox #divInfo,#commentBox .divTopicDetails').hide();
         });
        $('li.playlists a').click(function(event){
            if(!$(this).hasClass('current'))changeTab('playlists');
            event.preventDefault();
        });
        $('li.notes a').click(function(event){
            if(!$(this).hasClass('current'))changeTab('notes');
            event.preventDefault();
        });
    }  else { //not logged in
        $('#commentBox  .plus').click(function(){
                LogIn();
                return false;
        });
        $('li.playlists a').click(function(){
                LogIn();
                return false;
        });
        $('li.notes a').click(function(){
                LogIn();
                return false;
        });
        $('#frmChat').submit(function(){
                LogIn();
                return false;
        });
        $('#frmChat .up').click(function(){
                LogIn();
                return false;
        });

    }

 //   $('#divSponsor').click(function(){
 //           LogIn();
//    });
    $('.mjn_scroll').jScrollPane({scrollbarWidth:15, showArrows:true, scrollbarMargin:1, dragMinHeight:20});
    $('.details_scroll').jScrollPane({scrollbarWidth:15, showArrows:true, scrollbarMargin:1, dragMinHeight:20});


    $('li.details a').click(function(event){
            if(!$(this).hasClass('current'))changeTab('details');
            event.preventDefault();
        });
    $('li.share a').click(function(event){
    
 

 
            if(!$(this).hasClass('current'))changeTab('share');
            event.preventDefault();
        });
    $('li.comment a').click(function(event){
            if(!$(this).hasClass('current'))changeTab('comment');
            event.preventDefault();
        });



    //play media buttons
    remapOnClick();

    //Media playlists menu
    $(".divMenuMediaSub li a").click(function (event) {
            loadChannelPlaylist($(this).attr('playlistid'),$(this).text());

            event.preventDefault();
        });

    $('#playlistsBox .viewing').mouseover(function(){
            $('.divMenuFavourite').toggle();
            setTimeout("if(!tMenuFavourite )$('.divMenuFavourite').hide('slow')",3000);
    });

    $('#divTabMenu .media').mouseover(function(){
            $('.divMenuMediaSub').show();
            tMenuMediaSub = 1;
            setTimeout("if(!tMenuMediaSub )$('.divMenuMediaSub').hide('slow')",3000);
    });
    $('#divTabMenu .media').mouseout(function(){
            tMenuMediaSub = 0;
            setTimeout("if(!tMenuMediaSub )$('.divMenuMediaSub').hide('slow')",3000);
    });
    $('.rss').mouseover(function(){
            $('.divMenuMedia').show();
            setTimeout("if(!tMenuMedia )$('.divMenuMedia').hide('slow')",3000);
    });
    $('.divMenuMediaSub').mouseover(function(){
            $(this).show();
            tMenuMediaSub = 1;
    });
    $('.divMenuMediaSub').mouseout(function(){
            $(this).hide();
            tMenuMediaSub = 0;
    });
    //rss sub_menu
    $('.divMenuMedia').mouseover(function(){
            $(this).show();
            tMenuMedia = 1;
    });
    $('.divMenuMedia').mouseout(function(){
            $(this).hide();
            tMenuMedia = 0;
    });
    //another one menu
    $('.divMenuFavourite').mouseover(function(){
            $(this).show();
            tMenuFavourite = 1;
    });
    $('.divMenuFavourite').mouseout(function(){
            $(this).hide();
            tMenuFavourite = 0;
    });

    $('.divMenuFavourite a').click(function(event){
                    if(!$(this).hasClass("create")){
                        playlistId=$(this).attr('playlistId');
                        loadPlaylist(playlistId);
                    } else {
                        $.get(dashBoardUrl,{p: 'playlists', act: 'new_playlist'},
                            function(){loadPlaylistsList();});//,setPodcastingsList,"json");
                    }
                    event.preventDefault();
    });
    //Add to playlist ajax
    $('#playlistsBox .plus').unbind();
    $('#playlistsBox .plus').click(function(){
            playlistId = $(this).parent().attr('playlistId');
            post = $('#divTitle').attr('postingId');
            $.get(dashBoardUrl,{p: 'playlists', act: 'add_to_playlist', playlist:playlistId, post:post},
                function(){loadPlaylist(playlistId);});//,setPodcastingsList,"json");
    });
    //subscribe unubscribe ajax
$('.divSubscribeIcon').click(function(){
var dlt=1;
var next=0;

if($(this).hasClass("divUnSubscribeIcon"))next=confirm('Are you sure?');
else{ next=1; dlt =0};

if(next){
id=$(this).attr('clientid');
subtype=$(this).attr('subtype');
$(this).toggleClass("divUnSubscribeIcon");
$.post(subtype=='friend' ? addFriendsUrl : addFavoritesUrl,
{id:id, where:'subscriptions', dlt:dlt});
}
});

    $('#renameForm').submit( function(){
            url = '';
            url+= dashBoardUrl+'?'+$(this).serialize();
            playlistId = $('#renameForm #playlist').val();
            $.get(url, function(){
                    loadPlaylist(playlistId);
                    loadPlaylistsList();
            });
            return false;
    });

    $('#removeList').click(function(){
           if(confirm("Are you sure, you want do delete list?  \n Removing this playlist will place any records to your favorites playlist.")){
                 var url=dashBoardUrl+'?p=playlists&act=remove_playlist&playlist='+$('#renameForm #playlist').val();
                 $.get(url, function(){
                                        loadPlaylist(idFavorites);
                                        loadPlaylistsList();
                 });
           }
        });

    //loadPlaylist(idFavorites);
  //  longTitle();
  //  pconf.defaultContent  = $('#divVideo').html();
//    Wildfire.initPost('136821', 'divWildfirePost', 390, 180, pconf);
    // for login
        // preload images
    /*var img = ['cancel.png', 'form_bottom.gif', 'form_top.gif', 'loading.gif', 'send.png'];
    $(img).each(function () {
        var i = new Image();
        i.src = 'images/login/' + this;
    });*/

    loadChannelPlaylist(idChannelPlaylist,channelPlaylist);
    playPodcasting(curPosting, mediaType);
});
