/***************************************************
**  Interface Menu Functions
****************************************************/
var RESTART_INTERVAL = -1;
var SMALL = true;

function isopen(name){
  return ($(name).getStyle('display') == 'block');
}
function showaddsong(){
  showsmallvideo();
  $('addsongform').title.value = "";
  $('addsongform').url.value = "";
  otherMenus.show( otherMenus.addsong_menu );
}
function closeaddsong(){
  otherMenus.close( otherMenus.addsong_menu );
}

function showsharemedia( title, url ){
  var params = new Object();
  params.title = title;
  params.url   = url;
  var getsharelinkajax = new Ajax( showsharemediahandler, ajaxError, params );
  var uri = "/media/getsharelink!ajax.action?title="+encodeURIComponent( title ) + "&url=" + encodeURIComponent( url ) + "&" + generateLine()+"=0";
  getsharelinkajax.get( uri, null, false );
  return false;
}

function showsharemediahandler( obj, jsont ){
  var json = eval( '(' + jsont + ')' );
  if( json.status == 'success' ){
     var id = json.id;
     var message = 'Hey come check out this greate song I found on <a href="http://www.flashwidgetz.com">Flashwidgetz.com</a><br><br>' +
                   '<a href="http://www.flashwidgetz.com/mediaconsole/play/' + id +'">' + obj.getParams().title + '</a>';
     $('defaultmessage').innerHTML = message;
     showsmallvideo();
     otherMenus.show( otherMenus.sharemedia_menu );
  }else{
    dialogAlert( json.errorMessage );
  }
}

function sendShareEmail(){
  var myAddress = $('myAddress').value;
  var theirAddress = $('theirAddress').value;
  var obody = $('emailbody').value;
  if( ! isEmail( myAddress ) ){
    dialogAlert('Your email address is invalid');
    return;
  }
  if( ! isEmail( theirAddress ) ){
    dialogAlert('Your friend\'s email address is invalid');
    return;
  }
  var body = $('defaultmessage').innerHTML + "<br><br>" + obody;
  var ajax = new Ajax( sendShareEmailHandler, ajaxError );
  var uri = "/media/sendsharelink!ajax.action";
  var params = new Array();
  params['user']      = myAddress;
  params['recipient'] = theirAddress;
  params['body']      = body;
  ajax.post( uri, params, false );
}

function sendShareEmailHandler( obj, jsont ){
  var json = eval( '(' + jsont + ')' );
  if( json.status == 'success' ){
    $('myAddress').value = "";
    $('theirAddress').value = "";
    $('emailbody').value = "";
    dialogAlertTimeout('Email sent successfully', 2000 );
    closemediashare();
  }else{
    dialogAlert( json.errorMessage );
  }
}

function closemediashare(){
  otherMenus.close( otherMenus.sharemedia_menu );
}
function closemodifysong(){
  otherMenus.close( otherMenus.addsong_menu );
}
function showmodifysong( title, url, id, playlistnumber ){
  showsmallvideo();
  $('modifysongform').id.value = id;
  $('modifysongform').playlistnumber.value = playlistnumber;
  $('modifysongform').title.value = title;
  $('modifysongform').url.value = url;
  otherMenus.show( otherMenus.modifysong_menu );
}
function closemodifysong(){
  otherMenus.close( otherMenus.modifysong_menu );
}
function showcurrentlyplaying(){
  mainMenus.show( mainMenus.search_slider, mainMenus.search_menu, mainMenus.currently_playing_slider, mainMenus.currently_playing );
}
function showmediasearch(){
  //showsmallvideo();
  mainMenus.show( mainMenus.currently_playing_slider, mainMenus.currently_playing, mainMenus.search_slider, mainMenus.search_menu );
}
function showimportplaylisthelp(){
  create_iframe( 'importplaylisthelp', '500', '400', '/iframe/help/importplaylist.jsp', 'Import Playlist Help' );
}
function showimportplaylist(){
  showsmallvideo();
  $('importplaylisterror').innerHTML = '';
  $('importplaylistname').value = '';
  $('importplaylistcode').value = '';
  otherMenus.show( otherMenus.importplaylist_menu );
}
function closeimportplaylist(){
  otherMenus.close( otherMenus.importplaylist_menu );
}
function showcreateplaylist(){
  showsmallvideo();
  $('createplaylisterror').innerHTML = ''
  $('playlistname').value = '';
  otherMenus.show( otherMenus.createplaylist_menu );
}
function closecreateplaylist(){
  otherMenus.close( otherMenus.createplaylist_menu );
}
function showsaveplaylist(){
  if( !loggedIn ){
    dialogAlert('You must login or register before you can save playlist');
    return;
  }else{
    showsmallvideo();
    var Playlist = sessionplaylist_getplaylist();
    if( Playlist == null || Playlist[0] == null || Playlist[0].songs == null || Playlist[0].songs.length == 0 ){
      dialogAlert('You cannot save an empty playlist');
      return;
    }else{
      otherMenus.show( otherMenus.saveplaylist_menu );
    }
  }
}
function closesaveplaylist(){
  otherMenus.close( otherMenus.saveplaylist_menu );
}


function showlargevideo(){
  SMALL = false;
  showcurrentlyplaying();
  PLAYER.switchlargeview();
  $('switch_player').innerHTML = '' +
    '<a href="#" class="whitelink" style="font-size:8pt" onclick="showsmallvideo();return false">Show Mini Video</a>';
}

function showsmallvideo(){
  SMALL = true;
  if( PLAYER.windowstate == PLAYER.SMALL ) return;
  showmediasearch();
  PLAYER.switchsmallview();
  $('switch_player').innerHTML = '' + 
    '<a href="#" class="whitelink" style="font-size:8pt" onclick="showlargevideo();return false">Show Full Video</a>';
}


/***************************************************
**  Action Functions
****************************************************/

var ACTIVEPLAY = null;

function playSong( url, title, button, track, playlist, playlistnumber, id ){
  if( typeof(button) != 'undefined' &&  button != null ){
    var tag = $(button).getAttribute("tag");
    if( tag == "yt" && SMALL == false ){
      showcurrentlyplaying();
    }
  }


  if( ACTIVEPLAY == null ){
    ACTIVEPLAY = new Object();
  }else if( typeof(ACTIVEPLAY.button) != 'undefined' ){
    ACTIVEPLAY.button.onmouseover = ACTIVEPLAY.onmouseover;
    ACTIVEPLAY.button.onmouseout  = ACTIVEPLAY.onmouseout;
    ACTIVEPLAY.button.onmouseout();
  }
  if( button != null ){
    ACTIVEPLAY.button      = button;
    ACTIVEPLAY.onmouseover = button.onmouseover;
    ACTIVEPLAY.onmouseout  = button.onmouseout;
    button.onmouseover     = null;
    button.onmouseout      = null;
    ACTIVEPLAY.onmouseout();
  }
  if( PLAYER == null ) {
    dialogAlertTimeout( 'Sorry the page is still loading', 2000 );
  }else{
    PLAYER.playSong( title, url, track, playlist, playlistnumber, id );
  }
}


/**** add song ******/
function addSong( url, title, handler ){
  var playlistnumber = 0;
  if( loggedIn == false ) playlistnumber = 0;
  else{
    if( LOADEDPLAYLIST.getplaylistnumber() != null ) playlistnumber = LOADEDPLAYLIST.getplaylistnumber();
    else{
      dialogAlert('First select a playlist from "Playlist Options"<br> menu to add song to.');
      return false;
    }
  }
  var title = encodeURIComponent( title );
  var url   = encodeURIComponent( url );
  if( playlistnumber == 0 ){
    if( sessionplaylist_add( url, title ) ){
       dialogAlertTimeout('Song Added Successfully', 1000);
       LOADEDPLAYLIST.view( 0 );
       if( handler ) handler();
       $('addsongform').title.value = "";
       $('addsongform').url.value = "";
    }
  }else{
    $('addsongindicator').setStyle('display', 'block');
    var ajax = new Ajax( addSongHandler, ajaxError, handler );
    var uri = "/media/addplaylistmedia!ajax.action?"+generateLine()+"=0&playlistNumber="+playlistnumber+"&title="+title+"&url="+url;
    ajax.get( uri, null, false );
  }
}
function addSongHandler(  obj, jsont ){
  $('addsongindicator').setStyle('display', 'none');
  var json = eval( '(' + jsont + ')' );
  if(json.status == 'success'){
    var handler = obj.getParams();
    if( handler ) handler();
    dialogAlertTimeout('Song Added Successfully', 1000);
    LOADEDPLAYLIST.viewcurrent();
    $('addsongform').title.value = "";
    $('addsongform').url.value = "";
  }else{
    dialogAlertTimeout(json.errorMessage, 1000);
  }
}

/**** delete playlist ****/
function deleteplaylist( playlistNumber ){
  if( playlistNumber == 0 ){
    dialogAlert('Sorry you cannot delete the temporary playlist');
  }else{
    confirmSubmit(null, deleteplaylistConfirmHandler, { 'playlistNumber':playlistNumber }, "Are you sure you want to delete this playlist?" );
    return false;
  }
}

function deleteplaylistConfirmHandler( value, args ){
  if( value == true ){
    var ajax = new Ajax( deleteplaylistHandler, ajaxError );
    var uri = "/media/deleteplaylist!ajax.action?confirm=true&playlistNumber="+args.playlistNumber+"&"+generateLine()+"=0";
    ajax.get( uri, null, false );
  }
}

function deleteplaylistHandler( obj, jsont ){
  var json = eval( '(' + jsont + ')' );
  if(json.status != 'success'){
      dialogAlert(json.errorMessage);
  }else{
    dialogAlertTimeout('Playlist deleted successfully', 2000 );
    PLAYLISTLIST.load();
  }
}

/**** create playlist ******/
function createplaylist(){
  var value = $('playlistname').value;
  if( value == null || value.length > 26 || value < 1 ){
    $('createplaylisterror').innerHTML = 'The playlist name must be between 1 and 26 characters';
    return false;
  }
  $('creatingplaylistindicator').setStyle('display', 'block');
  var ajax = new Ajax( createplaylistHandler, ajaxError );
  var uri = "/media/createplaylist!ajax.action?playlistname=" + encodeURIComponent(value);
  ajax.get( uri, null, false );
}
function createplaylistHandler( obj, jsont ){
   $('creatingplaylistindicator').setStyle('display', 'none');
   var json = eval( '(' + jsont + ')' );
   if( json.status == 'success' ){
     dialogAlertTimeout( 'Playlist Created Successfully', 1000 );
     closecreateplaylist();
     PLAYLISTLIST.load();
   }else{
     $('createplaylisterror').innerHTML = json.errorMessage;
   }
}

/**** import playlist *****/
function importplaylist(){
  var playlistName = $('importplaylistname').value
  var code = $('importplaylistcode').value;
  if( playlistName.length < 1 ){
    dialogAlert('Please enter a name for your playlist');
    return;
  }
  if( code.length < 1 ){
    dialogAlert('Code field cannot be blank');
    return;
  }
  var ajax = new Ajax( importplaylistHandler, ajaxError );
  var uri = "/media/importplaylist!ajax.action";
  var data = new Object();
  data.playlistName = playlistName;
  data.code = code;
  data.cc = "false";
  $('loadingexternalplaylist').setStyle('display', 'block');
  $('externalplaylistbuttons').setStyle('display', 'none');
  ajax.post( uri, data, false );
}

function importplaylistHandler( obj, jsont ){
  var json = eval( '(' + jsont + ')' );
  $('loadingexternalplaylist').setStyle('display', 'none');
  $('externalplaylistbuttons').setStyle('display', 'block');
  if(json.status == 'success'){
    var playlist = json.Playlist[0];
    var playlistNumber = playlist.playlist_number;
    var playlistName = playlist.name;
    closeimportplaylist();
    PLAYLISTLIST.load();
    LOADEDPLAYLIST.view( playlistNumber );
    dialogAlertTimeout('Import Successful', 3000);
  }else{
    $('importplaylisterror').innerHTML = '<div style="color:#F00">'+ json.errorMessage + '</div>';
  }
}


/**** save playlist ******/
function saveplaylist(){
  var value = $('saveplaylistname').value;
  if( value == null || value.length > 26 || value < 1 ){
    $('saveplaylisterror').innerHTML = 'The playlist name must be between 1 and 26 characters';
    return false;
  }
  $('savingplaylistindicator').setStyle('display', 'block');
  var ajax = new Ajax( saveplaylisthandler, ajaxError );
  var uri = "/media/savesessionplaylist!ajax.action?playlistname=" + encodeURIComponent(value);
  ajax.get( uri, null, false );
}
function saveplaylisthandler( obj, jsont ){
   $('savingplaylistindicator').setStyle('display', 'none' );
   var json = eval( '(' + jsont + ')' );
   if( json.status == 'success' ){
     dialogAlertTimeout( 'Playlist Created Successfully', 1000 );
     closesaveplaylist();
     PLAYLISTLIST.load();
   }else{
     $('saveplaylisterror').innerHTML = json.errorMessage;
   }
}

/**** update song ******/
function updatesong(){
  var id    = $('modifysongform').id.value;
  var title = $('modifysongform').title.value;
  var url   = $('modifysongform').url.value;
  var playlistnumber = $('modifysongform').playlistnumber.value;
  if( id == null || playlistnumber == null ){
    $('modifysongerror').innerHTML = 'Unknown Error';
    return false;
  }
  if( title == null || title < 1 ){
    $('modifysongerror').innerHTML = 'The song title cannot be blank';
    return false;
  }
  if( url == null || url < 1 ){
    $('modifysongerror').innerHTML = 'The url cannot be blank';
    return false;
  }
  if( playlistnumber == 0 ){
    if( sessionplaylist_modify( id, title, url, $('modifysongerror') ) ){
      dialogAlertTimeout( 'Song Updated Successfully!', 1000 );
      closemodifysong();
      LOADEDPLAYLIST.view( 0 );
    }
  }else{
    title = encodeURIComponent( title );
    url   = encodeURIComponent( url );
    var ajax = new Ajax( updatesonghandler, ajaxError, playlistnumber );
    var uri = "/media/updateplaylistmedia!ajax.action?playlistNumber="+
              playlistnumber+"&songId="+id+"&title="+title+"&url="+url+"&"+generateLine()+"=0";
    ajax.get( uri, "", false );
  }
}
function updatesonghandler( obj, jsont ){
   var json = eval( '(' + jsont + ')' );
   if( json.status == 'success' ){
     dialogAlertTimeout( 'Song Updated Successfully', 1000 );
     closemodifysong();
     LOADEDPLAYLIST.view( obj.getParams() );
   }else{
     $('modifysongerror').innerHTML = json.errorMessage;
   }
}

/**** delete song ******/
function deleteSong( playlistnumber, songid ){
  var args = { 'playlistnumber':playlistnumber, 'songid':songid };
  confirmSubmit(null, deleteSongConfirmHandler, args, "Are you sure you want to delete this song" );
  return false;
}
function deleteSongConfirmHandler( value, args ){
  if( value == true ){
    if( args.playlistnumber == 0 ){
      sessionplaylist_delete( args.songid );
      LOADEDPLAYLIST.view( 0 );
    }else{
       var ajax = new Ajax( deleteSongHandler, ajaxError, args.playlistnumber );
       var uri = "/media/deleteplaylistmedia!ajax.action?confirm=true&playlistNumber="+
                 args.playlistnumber+"&songId="+args.songid+"&"+generateLine()+"=0";
       ajax.get( uri, null, false );
    }
  }
}
function deleteSongHandler( obj, jsont ){
  var json = eval( '(' + jsont + ')' );
  if(json.status != 'success'){
      dialogAlert(json.errorMessage);
  }
  LOADEDPLAYLIST.view( obj.getParams() );
}

/**** search options ******/
function selectSearchModeClick( span ){
  var id = $(span).getAttribute('id');
  $(id).set('class', 'mode_sel');
  var cookie = new Hash.Cookie('searchoptions', {duration:1, domain: 'flashwidgetz.com', path: '/'} );  
  if( id == 'mode_video_thumbnail' ){
    cookie.set('showvideo', 'true' ); cookie.set('searchtype', 'video' );
  }else if( id == 'mode_all') cookie.set('searchtype', 'all' );
  else if( id == 'mode_mp3') cookie.set('searchtype', 'mp3' );
}

/**** track info *********/

var PREINFO_INFO = "";
var PREINFO_ACTIONS = "";

function infoSong( url ){
  PREINFO_INFO = $('track_info').innerHTML;
  PREINFO_ACTIONS = $('track_actions').innerHTML;
  $('track_info').innerHTML = url;
  $('track_actions').innerHTML = '<a style="color:#86bd33" href="#" onclick="revertInfo();return false"><b>Close</b></a>';
}

function revertInfo(){
  $('track_info').innerHTML = PREINFO_INFO;
  $('track_actions').innerHTML = PREINFO_ACTIONS;
}

