
jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        // CAUTION: Needed to parenthesize options.path and options.domain
        // in the following expressions, otherwise they evaluate to undefined
        // in the packed version for some reason...
        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};

var playerTime = 0;
var playerPaused = false;

$(document).ready(function() {
	$.post("/functions/mp3Request.php", { hash: mp3hash },requestRespond,"json");
});

function requestRespond(data){
    if(data.status == "ok"){
	var path = data.path;

	var solution;
	if($.browser.webkit){
	    solution = "flash,html";
	}else{
	    solution = "html,flash";
	}
	
	$("#hidden-player").jPlayer({
		ready: function () {
			$(this).jPlayer("setMedia", {
				mp3: path
			});

			var tmpTime = $.cookie('playerTime');
			if(tmpTime){
				playerTime = Number(tmpTime);
			}

			var tmpPaused = $.cookie('playerPaused');
			if(tmpPaused){
				if(tmpPaused == "true"){
					setPaused(true);
				}else{
					setPaused(false);
				}
			}

			if(playerPaused){
				$(this).jPlayer("pause",playerTime);
			}else{
				$(this).jPlayer("play",playerTime);
			}

			$(this).bind($.jPlayer.event.timeupdate,function(event) {
				playerTime = event.jPlayer.status.currentTime;
			});

			$(this).bind($.jPlayer.event.pause,function(event) {
				setPaused(true);
			});

			$(this).bind($.jPlayer.event.play,function(event) {
				setPaused(false);
			});

			$(this).bind($.jPlayer.event.ended,function(event) {
				setPaused(true);
			});
		},
		swfPath: "/js/Jplayer.swf",
		supplied: "mp3",
		solution: solution
	});

	$(".controls-play").click(function () { playMusic(); });
	$(".controls-pause").click(function () { pauseMusic(); });

    }
}


function setPaused( paused_bl ){
	playerPaused = paused_bl;

	if(playerPaused) {
		$(".controls-play").show();
		$(".controls-pause").hide();
	}else{
		$(".controls-play").hide();
		$(".controls-pause").show();
	}
}

function playMusic(){
	setPaused(false);
	$("#hidden-player").jPlayer("play",playerTime);
}

function pauseMusic(){
	setPaused(true);
	$("#hidden-player").jPlayer("pause",playerTime);
}

$(window).bind('beforeunload', function() {
	$.cookie('playerTime', playerTime, { expires: 60, path: '/', domain: 'www.jsonchannel.com'});
	$.cookie('playerPaused ', playerPaused , { expires: 60, path: '/', domain: 'www.jsonchannel.com'});
});

