<html>
<head>
<style type="text/css">
body {
	background: #161616 url("images/LightEaster.png") top left repeat;
	margin: 0;
	padding: 0;
        color:#0000bb;
	font: 12px normal Verdana, Arial, Helvetica, sans-serif;
	height: 100%;
}

* {margin: 0; padding: 0; outline: none;}

img {border: none;}

a { 
	text-decoration:none; 
	color:#00c6ff;
}

h1 {
	font: 3em normal Arial, Helvetica, sans-serif;
	padding: 10px;	margin: 0;
	text-align:center;
	color:#bbb;
}

h1 small{
	font: 0.2em normal  Arial, Helvetica, sans-serif;
	text-transform:uppercase; letter-spacing: 0.2em; line-height: 5em;
	display: block;
}

.container {width: 960px; margin: 0 auto; overflow: hidden;}

/* PROGRESS BAR */

ul#progress {
	list-style:none;
	width:125px;
	margin:0 auto;
	padding-top:50px;
	padding-bottom:50px;
}
ul#progress li {
	float:left;
	position:relative;
	width:15px;
	height:15px;
	border:1px solid #fff;
	border-radius:50px;
	margin-left:10px;
	border-left:1px solid #111; border-top:1px solid #111; border-right:1px solid #333; border-bottom:1px solid #333; 
	background:#000;
}

ul#progress li:first-child { margin-left:0; }

.running .ball {
	background-color:#2187e7; 
	background-image: -moz-linear-gradient(90deg, #2187e7 25%, #a0eaff); 
	background-image: -webkit-linear-gradient(90deg, #2187e7 25%, #a0eaff); 

	width:15px;
	height:15px;
	border-radius:50px;
	-moz-transform:scale(0);
	-webkit-transform:scale(0);
	
	//-moz-animation:loading 1s linear forwards;
	//-webkit-animation:loading 1s linear forwards;
}

.running .pulse {
	width:15px;
	height:15px;
	border-radius:30px;
	border: 1px solid #00c6ff;
	box-shadow: 0 0 5px #00c6ff;
	position:absolute;
	top:-1px;
	left:-1px;
	-moz-transform:scale(0);
	-webkit-transform:scale(0);
	
	-webkit-animation:pulse 1s ease-out;
	-moz-animation:pulse 1s ease-out;
}
/*
#layer1 { -moz-animation-delay:0.5s; -webkit-animation-delay:0.5s; }
#layer2 { -moz-animation-delay:1s; -webkit-animation-delay:1s; }
#layer3 { -moz-animation-delay:1.5s; -webkit-animation-delay:1.5s; }
#layer4 { -moz-animation-delay:2s; -webkit-animation-delay:2s; }
#layer5 { -moz-animation-delay:2.5s; -webkit-animation-delay:2.5s; }

#layer7 { -moz-animation-delay:1.5s; -webkit-animation-delay:1.5s; }
#layer8 { -moz-animation-delay:2s; -webkit-animation-delay:2s; }
#layer9 { -moz-animation-delay:2.5s; -webkit-animation-delay:2.5s; }
#layer10 { -moz-animation-delay:3s; -webkit-animation-delay:3s; }
#layer11 { -moz-animation-delay:3.5s; -webkit-animation-delay:3.5s; }
*/
@-moz-keyframes loading {
	0%{-moz-transform:scale(0,0);}
	100%{-moz-transform:scale(1,1);}	
}

@-webkit-keyframes loading {
	0%{-webkit-transform:scale(0,0);}
	100%{-webkit-transform:scale(1,1);}	
}


@-moz-keyframes pulse {
	0%   {-moz-transform: scale(0);opacity: 0;}
    10%  {-moz-transform: scale(1);opacity: 0.5;}
	50%  {-moz-transform: scale(1.75);opacity: 0;}
    100% {-moz-transform: scale(0);opacity: 0;}  
}

@-webkit-keyframes pulse {
	0%   {-webkit-transform: scale(0);opacity: 0;}
	10%  {-webkit-transform: scale(1);opacity: 0.5;}
    50%  {-webkit-transform: scale(1.75);opacity: 0;}
    100% {-webkit-transform: scale(0);opacity: 0;}    
}


/* Full Width Progress Bar */

#content { 
	width:100%; 
	height:5px; 
	margin:50px auto; 
	background:#000;
}

.fullwidth .expand { 
	width:100%; 
	height:1px; 
	margin:2px 0; 
	background:#2187e7; 
	position:absolute;
	box-shadow:0px 0px 10px 1px rgba(0,198,255,0.7);
    -moz-animation:fullexpand 10s ease-out;
	-webkit-animation:fullexpand 10s ease-out;
}

@-moz-keyframes fullexpand {
	0%  { width:0px;}
	100%{ width:100%;}	
}
@-webkit-keyframes fullexpand {
	0%  { width:0px;}
	100%{ width:100%;}	
}

/* Loader Bar */

ul#loadbar {
	list-style:none;
	width:200px;
	margin:0 auto;
	padding-top:50px;
	padding-bottom:75px;
}

ul#loadbar li {
	float:left;
	position:relative;
	width:11px;
	height:26px;
	margin-left:1px;
	border-left:1px solid #111; border-top:1px solid #111; border-right:1px solid #333; border-bottom:1px solid #333; 
	background:#000;
}

ul#loadbar li:first-child { margin-left:0; }

ul#loadbar li.on{
	background-color:#2187e7;  
//background-color:red;
	background-image: -moz-linear-gradient(45deg, #2187e7 25%, #a0eaff); 
	background-image: -webkit-linear-gradient(45deg, #2187e7 25%, #a0eaff);
	width:11px;
	height:26px;
	opacity:0%;
	-webkit-animation:fill 0.5s linear forwards;
	-moz-animation:fill 0.5s linear forwards;
}

ul#loadbar li.off{
        background-color:#2187e7;
background-color:black;
//        background-image: -moz-linear-gradient(45deg, #2187e7 25%, #a0eaff);
  //      background-image: -webkit-linear-gradient(45deg, #2187e7 25%, #a0eaff);
        width:11px;
        height:26px;
        opacity:0%;
        -webkit-animation:fill 1s linear forwards;
        -moz-animation:fill 1s linear forwards;
}

/*
#layerFill1 { -moz-animation-delay:0.5s; -webkit-animation-delay:0.5s; }
#layerFill2 { -moz-animation-delay:1s; -webkit-animation-delay:1s; }
#layerFill3 { -moz-animation-delay:1.5s; -webkit-animation-delay:1.5s; }
#layerFill4 { -moz-animation-delay:2s; -webkit-animation-delay:2s; }
#layerFill5 { -moz-animation-delay:2.5s; -webkit-animation-delay:2.5s; }
#layerFill6 { -moz-animation-delay:3s; -webkit-animation-delay:3s; }
#layerFill7 { -moz-animation-delay:3.5s; -webkit-animation-delay:3.5s; }
#layerFill8 { -moz-animation-delay:4s; -webkit-animation-delay:4s; }
#layerFill9 { -moz-animation-delay:4.5s; -webkit-animation-delay:4.5s; }
#layerFill10 { -moz-animation-delay:5s; -webkit-animation-delay:5s; }
#layerFill11 { -moz-animation-delay:6s; -webkit-animation-delay:6s; }
#layerFill12 { -moz-animation-delay:7s; -webkit-animation-delay:7s; }
#layerFill13 { -moz-animation-delay:8s; -webkit-animation-delay:8s; }
#layerFill14 { -moz-animation-delay:9s; -webkit-animation-delay:9s; }
*/

@-moz-keyframes fill {
	0%{ opacity:0; }
	100%{ opacity:1; }	
}

@-webkit-keyframes fill {
	0%{ opacity:0; }
	100%{ opacity:1; }	
}

/* Trigger button for javascript */

#station{
        font-family: Verdana, Geneva, sans-serif;
        font-size: 2em;
        color:red;
        text-align: center;
        padding: 5px;
}
#display{
        text-decoration:none;
        color:#00c6ff;
        text-align:center;
        padding: 3px;
}

button,.trigger, .triggerFull, .triggerBar {
	background: #000000;
	background: -moz-linear-gradient(top, #161616 0%, #000000 100%);
	background: -webkit-linear-gradient(top, #161616 0%,#000000 100%);
	border-left:1px solid #111; border-top:1px solid #111; border-right:1px solid #333; border-bottom:1px solid #333; 
	font-family: Verdana, Geneva, sans-serif;
	font-size: 0.8em;
	text-decoration: none;
	text-transform: lowercase;
	text-align: center;
	color: #fff;
	padding: 10px;
	border-radius: 3px;
	display: block;
	margin: 0 auto;
	width: 140px;
}
		
button:hover, .trigger:hover, .triggerFull:hover, .triggerBar:hover {
	background: -moz-linear-gradient(top, #202020 0%, #161616 100%);
	background: -webkit-linear-gradient(top, #202020 0%, #161616 100%);
}


</style>


<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>
$(document).ready(function(){
var lastsignal = 0;

try{
var ws = new WebSocket("ws://192.168.1.2:8888/ws"); 
//ws.send("mampus lu!");

 ws.onmessage = function(evt) {
//	alert("open" + evt);
	$("#display").html("The beginning ");
        getFreq(evt.data);
};



 ws.onopen = function(evt) { 
//	alert("open" + evt);
	$("#display").html("Connection ready" + evt);
        //ws.send("Connection ready");
};

}catch(exception){
        $("#display").html(exception);
	alert(exception);
}


function getFreq(jsondata){
        try{
         var obj = jQuery.parseJSON(jsondata);
         strength = obj.level/14*100
         strength = strength.toFixed(2)
         var desc = ""
         if(obj.mute==0)
            desc = obj.stereo;
         else
            desc = "muted";
         data = obj.freq + "FM. Signal strength: " + strength + "% (" + desc + ")";
         $("#display").html(data);

          if(obj.mute)
             $("#mute").text("Unmute");
          else
             $("#mute").text("Mute");
        if(lastsignal<obj.level)
	        increasesignal(lastsignal,obj.level);
        else if(lastsignal>obj.level)
                reducesignal(lastsignal,obj.level);
        $("#station").html("<b>"+obj.freq + "</b> FM");

        }catch(err){
           $("#display").html(err.message);

        }

}

function getStatus() {

$.get('telek.txt', function(data) {
//   alert(data);
   //process text file line by line
   $('#display').html(data.replace('n',''));
});

    setTimeout("getStatus()",1);

}
//getStatus();

    $("#up").click(function(){
	$("#display").html("Searching up the next available frequency");
        ws.send("up");
 	ws.onmessage = function(evt) {
//		alert("open" + evt.data);
		$("#display").html("opening websocket" + evt.data);
		getFreq(evt.data);


	};


 
            console.log("Status:" + data);

            if(data=="ok"){
              //display();

	    }
    });
    $("#down").click(function(){
        $("#display").html("Searching down the next available frequency");
	ws.send("down");
        ws.onmessage = function(evt) {
//              alert("open" + evt.data);
                $("#display").html("opening websocket" + evt.data);
                getFreq(evt.data);


        };


    });

    $("#off").click(function(){
        $("#display").html("Turning off...");
        ws.send("off");
        ws.onmessage = function(evt) {
//              alert("open" + evt.data);
                $("#display").html("Status: " + evt.data);
reducesignal(lastsignal,0);

        };


    });

    $("#mute").click(function(){
        $("#display").html("Muting...");
        ws.send("mute");
        ws.onmessage = function(evt) {
          getFreq(evt.data);
          var obj = jQuery.parseJSON(evt.data);
          if(obj.mute)
             $("#mute").text("Unmute");
          else
             $("#mute").text("Mute");

        };
        

    });

	function increasesignal(start,end){
	var index=start;
//	$('.bar').parent().removeClass( 'on' );
	var delay = setInterval( function(){
  		if ( index <= end ){
    			$('#layerFill'+index).parent().addClass( 'on' );
    			index += 1;
  		}else{
    			clearInterval( delay );
  		}
	}, 500 );

lastsignal=end;

//                    return false;

	}

        function reducesignal(start,end){
        var index=start;
        //$('.bar').parent().removeClass( 'on' );
        var delay = setInterval( function(){
                if ( index > end ){
                        $('#layerFill'+index).parent().removeClass( 'on' );
                        index -= 1;
                }else{
                        clearInterval( delay );
                }
        }, 500 );

lastsignal=end;

//                    return false;

        }


		$('.triggerBar').click(function() {
			reducesignal(lastsignal,1);
		});

});
</script>
</head>

<body>
<h1 small>Raspberry Pi 2 TEA5767 Radio Tuner</h1>
<button id="down">&lt;&lt;Tune Down</button>
<button id="up" >Tune Up&gt;&gt;</button>
<button id="mute" >Mute</button>
<div id="station"></div>
<div id="display"></div>
<!-- LOADER BAR -->
<div class="container">
	<ul id="loadbar">
    <li><div id="layerFill1" class="bar"></div></li>
    <li><div id="layerFill2" class="bar"></div></li>
    <li><div id="layerFill3" class="bar"></div></li>
    <li><div id="layerFill4" class="bar"></div></li>
    <li><div id="layerFill5" class="bar"></div></li>
    <li><div id="layerFill6" class="bar"></div></li>
    <li><div id="layerFill7" class="bar"></div></li>
    <li><div id="layerFill8" class="bar"></div></li>
    <li><div id="layerFill9" class="bar"></div></li>
    <li><div id="layerFill10" class="bar"></div></li>
    <li><div id="layerFill11" class="bar"></div></li>
    <li><div id="layerFill12" class="bar"></div></li>
    <li><div id="layerFill13" class="bar"></div></li>
    <li><div id="layerFill14" class="bar"></div></li>
    </ul>
<button id="off" >Turn off</button>
</div>
<!-- END LOADER BAR -->

</body>

</html>