  function clearform( formid ) {
    $(':input',formid)
    .not(':button, :submit, :reset, .noclear')
    .val('')
    .removeAttr('checked')
    .removeAttr('selected');
  }
   
  function updateLineNumbers() {
     var lineCounter = 1;
     $.each($("#setlist li"),function(i,item){
       if( ($(item)[0].id).substring(5) > 9999999990 ) {
         lineCounter = 1;
       } else {
         $(item).html($(item).html().replace(/^\d+/,lineCounter));
         lineCounter++;
       }
     });
   }
   

  
 
$(document).ready(function() {
  
  //focus op het eerste lege veld
  jQuery('form input[value=""]:first').focus();
  
 /**
  * open links met class 'blank' in nieuw venster
  **/
  $(function(){
    $('a.blank').click(function(){
      window.open(this.href);
      return false;
    });
  });   

  //slides op de voorpagina
  var fadebgcolor="#303437";
  $('.slides').each(function (i, el) {
    el.style.display = 'block';
    $(el).cycle({
      fx: 'fade',
      speed: 2000,
      timeout: (5000 + (Math.round(Math.random() * 9) * 100)),
      next: el,
      pause: 1,
      delay: (-4000 - (Math.round(Math.random() * 9) * 100)),
      random: 1
    });
  });

  $('.tt').cluetip({
    positionBy: 'mouse',
    fx: {
      open:       'fadeIn', // can be 'show' or 'slideDown' or 'fadeIn'
      openSpeed:  'slow'
    },
    hoverIntent: {
      sensitivity:  3,
      interval:     50,
      timeout:      1000
    },
    tracking: false,
    splitTitle: " _ ",
    arrows: true,
    cluetipClass: 'jtip'
  });

  $('.showLyrics').click(function() {
    var url = this.href;
    var titel = this.title.split('_')
    var dialog = $('<div style="display:hidden"></div>').appendTo('body');
    if( url.indexOf('songedit.php') > 0 ) var width = 500;
    // load remote content
    dialog.load(
      url, 
      {},
      function (responseText, textStatus, XMLHttpRequest) {
        if( url.indexOf('songedit.php') > 0 )
          dialog.dialog({ title: titel[0], width:500 });
        else
          dialog.dialog({ title: titel[0] });
      }
    );
    //prevent the browser to follow the link
    return false;
  });

  $('.confirm').click(function(){
    var delid =  $(this)[0]['id'];
    var dialog = $('#pop')
      .html('Weet je dat nou wel zeker?')
      .dialog({
        autoOpen: false,
        draggable: true,
        resizable: true,
        show:'slide',
        hide:'slide',
        title:'Bevestig',
        modal:true,
        buttons: {
           'Verwijder': function() {
             var strpos = delid.indexOf('_');
             var rowid  = delid.substr( (strpos + 1),( delid.length - (strpos-1) ) );
             $.get('/fact/edit.php', {deleteRow:rowid,factId:188});
             $('#rownumber_'+rowid).fadeOut('slow');
             console.log( 'deleting row ' + rowid );
             $(this).dialog('close');
           },
           'Cancel': function() {
             $(this).dialog('close');
           }
         }

    });
    dialog.dialog('open');
    return false;
  });

  /**
  * turn links into add bookmark
  */
	// add a "rel" attrib if Opera 7+
	if(window.opera) {
		if ($("a.jqbookmark").attr("rel") != ""){
			$("a.jqbookmark").attr("rel","sidebar");
		}
	}

	$("a.jqbookmark").click(function(event){
		event.preventDefault();
		var url = this.href;
		var title = this.title;

		if (window.sidebar) { // Mozilla Firefox Bookmark
			window.sidebar.addPanel(title, url,"");
		} else if( window.external ) { // IE Favorite
			window.external.AddFavorite( url, title);
		} else if(window.opera) { // Opera 7+
			return false; // do nothing
		} else {
			 alert('Unfortunately, this browser does not support the requested action,'
			 + ' please bookmark this page manually.');
		}

	});
	
	/**
	* ideeenbus
	*/
  
  $('#find').click(getsongs);
  $('#getthesong').click(getsong);
  $('#opnieuw').click(resetSearchform);
	
  function getsongs( ) {
    var searchfor = $('#searchfor').val();
    var searchin = $('#searchin').val();
    $.ajax({
      url: "http://cddb.lutzweb.net/findsong/",
      data: "func=getsongs&searchfor=" + searchfor + "&searchin=" + searchin,
      dataType:'jsonp',
      success: function(data){
        if(data == null) 
          return; //geen data, we gaan niet verder
        completeSearchform(data);
      }
    })              
  };

  function completeSearchform( data ) {
    $('#searchfor').replaceWith('<select id="fsongIdList"><option value="">[ Selecteer song ]</option></select>');
    var numresults = data.length;
    for(i=0;i<numresults;i++) {
      $('#fsongIdList').append('<option value="'+data[i]['id']+'">'+unescape(data[i]['title'])+' - '+ data[i]['artist']+'</option>');
    }
    $('#find').replaceWith('<input type="button" id="submit" disabled="disabled" value=" Vul maar in ... "/>');
  
    $('#fsongIdList').change(function(){
      clearform( '#songform' );
      if( $('#fsongIdList').val() != '' ) {
        var songid = $('#fsongIdList').val();
        $('#submit').removeAttr('disabled');
        $('#submit').click(getsong( songid ));                
      }
    });
  };

  function resetSearchform() {
    $('#fsongIdList').replaceWith('<input type="text" name="fSearchfor" id="searchfor"/>');
    $('#submit').replaceWith('<input type="button" id="find" value="Zoek"/>');
    $('#searchfor').focus();
    $('#find').click(getsongs);
    clearform('#cddbsearchform');
    clearform( '#songform' );
  };
  
  function getsong( id ) {
    var searchfor = $('#searchfor').val();
    var searchin = $('#searchin').val();
    $.ajax({
      url: "http://cddb.lutzweb.net/findsong/",
      data: "func=getsong&id=" + id,
      dataType:'jsonp',
      contentType: "application/x-www-form-urlencoded;charset=ISO-8859-15",
      success: function(data){
        if(data == null) 
          return; //geen data, we gaan niet verder
        fillSongForm( data );
      }
    })              
  };
  
  function fillSongForm( data ) {
    $('#title').val( data['title'] );
    $('#artist').val( data['artist'] );
    $('#writtenby').val( data['writtenby'] );
    $('#lyrics').val( data['lyrics'] );
    $('#album').val( data['album'] );
    $('#year').val( data['jaar'] );
  };
	/**
	* /ideeenbus
	*/
});


