$(document).ready(function(){
    $("a").live("click", function(){
        thisHref = $(this).attr("href");

        if ( $(this).hasClass("controlPlayer") || $(this).hasClass("edit_playlist_a") )
            return false;

        if ( $(this).hasClass("download") )
            return true;

        if ( thisHref.indexOf("?") == 0 )
            {                
                thisHref = location.href + thisHref;

            if ( thisHref.indexOf("?", thisHref.indexOf("?") + 1) > -1 )
                thisHref = thisHref.slice(0, thisHref.indexOf("?"))
                + thisHref.slice(thisHref.indexOf("?", thisHref.indexOf("?") + 1));
            }

        if ( thisHref.indexOf("?") > -1 )
            {
            content = "&content";
            }
        else
            {
            content = "?content";
            }

        title =  "&title=true";

        if ( (thisHref.indexOf("http:") > -1)
            && (thisHref.indexOf("http://poiskmp3.net/") < 0) )
            return true;

        if ( (thisHref.indexOf("mp3/playlist.html") > -1) )
            {
                if ( jwplayer() && $('#container_wrapper').css('display') != 'none' )
                    $("#shadowInfoDiv").css('top', '40px');
                else
                    $("#shadowInfoDiv").css('top', '0px');
                $("#shadowInfoDiv").css('display', 'block');

                $("#content_div").load(thisHref + content,
                    function(){
                        editPlaylist('none', 0);

                        $("#shadowInfoDiv").css('display', 'none');
                    });

                setLocation(thisHref.replace("http://poiskmp3.net", ""));

                $.getJSON(thisHref + content + title, function() {
                    if ( data  != undefined && data['title'] != undefined )
                        document.title = data['title'];
                });
            }
        else
            {
            if ( thisHref.indexOf("http:") > -1 )
                {
                if ( jwplayer() && $('#container_wrapper').css('display') != 'none' )
                    $("#shadowInfoDiv").css('top', '40px');
                else
                    $("#shadowInfoDiv").css('top', '0px');
                $("#shadowInfoDiv").css('display', 'block');

                $("#content_div").load(thisHref + content,
                    function(){
                        $("#shadowInfoDiv").css('display', 'none');
                    });

                setLocation(thisHref.replace("http://poiskmp3.net", ""));

                $.getJSON(thisHref + content + title, function() {
                    if ( data  != undefined && data['title'] != undefined )
                        document.title = data['title'];
                });
                }
            else
                {
                if ( jwplayer() && $('#container_wrapper').css('display') != 'none' )
                    $("#shadowInfoDiv").css('top', '40px');
                else
                    $("#shadowInfoDiv").css('top', '0px'); 
                $("#shadowInfoDiv").css('display', 'block');

                $("#content_div").load("http://poiskmp3.net/" + thisHref + content,
                    function(){
                        $("#shadowInfoDiv").css('display', 'none');
                    });

                setLocation(thisHref.replace("http://poiskmp3.net", ""));

                $.getJSON("http://poiskmp3.net/" + thisHref + content + title, function(data) {
                    if ( data  != undefined && data['title'] != undefined )
                        document.title = data['title'];
                });
                }
            }

        return false;
    });

    if ( location.hash.replace("#", "") )
        {
            location.href = "http://poiskmp3.net" + location.hash.replace("#", "");
        }

    $('#closeBlock').click(
        function ()
        {
        $("#shadowInfoDiv").css('display', 'none');

        return false;
        }
    )
});

function addToPlaylistAndCoocies (player, comment, file)
{
    var exp = new Date();
    var oneYearFromNow = exp.getTime() + (365*24*60*60*1000);
    var playlistCookie = eval("([" + $.cookies.get('playlist1') + "])");

    exp.setTime (oneYearFromNow);

    $.cookies.setOptions({expiresAt: exp});
    if ( $.cookies.get('playlist1') && playlistCookie.length > 2)
        {
        $.cookies.set('playlist1', $.cookies.get('playlist1')
            + ', {"description":"' + encodeURIComponent(comment).replace("\"", "")
            + '","file":"' + file + '"}');

        }
    else
        {
        $.cookies.set('playlist1',
            '{"empty":"empty"}, {"empty":"empty"}, {"description":"' + encodeURIComponent(comment).replace("\"", "")
            + '","file":"' + file + '"}');
    
    if ( jwplayer() )
        {
        $('#container_wrapper').css('display', '');

        if ( jwplayer().playngTrack != undefined )
            {
            jwplayer().playlistItem(jwplayer().playngTrack);
            jwplayer().seek(jwplayer().trackPosition);
            }
        else
            jwplayer().playlistItem(0);
        jwplayer().play(true);
        }
    else
        jwplayer("container").setup({
            flashplayer: "/jwplayer/player.swf",
            playlistfile : "/ajax/playlist.xml",
            'backcolor': '#8268ED',
            'screencolor': '#8268ED',
            "playlist.position": "right",
            "playlist.size": 560,
            height: 40,
            width: '100%',
			"controlbar": "bottom",
			"viral.functions": "false",
			'skin': '/jwplayer/aero.zip'
			});

    jwplayer().onPlaylist(function(event) {
        if ( location.href.indexOf("mp3/playlist.html") > -1 )
            {
            if ( jwplayer().playngTrack != undefined )
                {
                jwplayer().playlistItem(jwplayer().playngTrack);
                jwplayer().seek(jwplayer().trackPosition);
                }

            jwplayer().playlistItem(0);
            }
        else
        {
            jwplayer().playlistItem(jwplayer().playngTrack);
            jwplayer().seek(jwplayer().trackPosition);
        }
        jwplayer().play(true);
     });

    jwplayer().onPlaylistItem(function(event) {
         jwplayer().playngTrack = event.index;
    });

    jwplayer().onComplete(function(event) {
        var playlistLenght = jwplayer().getPlaylist();
        if ( jwplayer().playngTrack + 1 < playlistLenght.length )
            jwplayer().playngTrack =  jwplayer().playngTrack + 1;
        else
            jwplayer().playngTrack = 0;
        jwplayer().playlistItem(jwplayer().playngTrack);
        jwplayer().play(true);
    });

    $('#container_wrapper').attr('style', "top: 0; left: 0; z-index: 90; position: fixed; width: 100%; height: 40px;");
    $('#commonTable').attr('style', "margin-top: 40px;");
    $('#editPlaylistHref').css('display', '');
        }

jwplayer().trackPosition = jwplayer().getPosition();
jwplayer().load("http://poiskmp3.net/ajax/playlist.xml?rnd=" + exp.getTime());

return false;
}

function editPlaylist ( action, item )
{
    var currentTime = new Date();
    var playlistCookie = eval("([" + $.cookies.get('playlist1') + "])");

    if ( action == "delete" && playlistCookie.length <= 3 )
        {
        jwplayer().stop();
        $('#container_wrapper').css('display', 'none');
        $('#commonTable').attr('style', "margin-top: 0px;");
        $('#editPlaylistHref').css('display', 'none');
        }

    jwplayer().trackPosition = jwplayer().getPosition();

    $("#edit_playlist_div").load("http://poiskmp3.net/ajax/edit_playlist.php?rnd=" + currentTime.getTime(),
        {'action' : action, 'item' : item},
    function() {jwplayer().load("http://poiskmp3.net/ajax/playlist.xml?rnd=" + currentTime.getTime());});

    return false;
}

function setLocation(curLoc){
                try {
                  history.pushState(null, null, curLoc);
                  return;
                } catch(e) {}
                location.hash = '#' + curLoc;
        }

function loadSearch()
{
    re = /^\s*$/;
    searchField = $("#search").val();
    if ( searchField.match(re) )
        return false;
    
    thisHref = "/mp3/search.html?ajax&search=" + encodeURIComponent($("#search").val());

    if ( thisHref.indexOf("?") > -1 )
        {
        content = "&content";
        }
    else
        {
        content = "?content";
        }

    title =  "&title=true";

    if ( jwplayer && $('#container_wrapper').css('display') != 'none' )
        $("#shadowInfoDiv").css('top', '40px');
    else
        $("#shadowInfoDiv").css('top', '0px');
    $("#shadowInfoDiv").css('display', 'block');

    $("#content_div").load(thisHref + content,
        function(){
            $("#shadowInfoDiv").css('display', 'none');
        });

    setLocation(thisHref.replace("http://poiskmp3.net", ""));

    $.getJSON(thisHref + content + title, function() {
        if ( data['title'] != undefined )
            document.title = data['title'];
    });

    return false;
}
