$(document).ready(function(){
    /*$.preloadCssImages();*/
	if($('#clan-bend').length ==1) {
		var random = $('#clan-bend').html();
	}
	else {
    	var random = $.randomBetween(1, 8);
	}
	
	$('#mJezik').click(function(){
		var jezik = $('#mJezik').attr('rel');
        document.cookie = 'jezik=' +jezik + '; expires=Thu, 2 Aug 2020 20:47:11 UTC; path=/';
		document.location.reload();
    });
	
    $('#random').css('background-image', 'url(/static/images/random/Random-' + random + '.png)');
    
    $('li.subMenu ul').hover(
        function() {
            $('#mMultimedia').addClass('mMultimediaFix');
        },
        function() {
            $('#mMultimedia').removeClass('mMultimediaFix');
        }
    );
    /*Player*/
    var n_playlists = $('div.playlists ul.playlist').size();
    $('div.playlists ul.mpl1').show();
    $('li.navigation a.next').click(function(){
        return f_Broj(this, 1);
    });
    $('li.navigation a.prev').click(function(){
        return f_Broj(this, -1);
    });
    
    $('body').data('index', 0);
    $('div.showPlayer a').click(function(){
        $('div.playlists').toggle();
        return false;
    });
    $('#pPlay').click(function(){
        try {
            $('#jPlayers').stop();
            $('div.pesmaPlay').show();
            $('div.pesmaPause').hide();
        } catch(e){}
        $('#jPlayer').play();
        return false;
    });
    $('#pStop').click(function(){
        $('#jPlayer').stop();
        return false;
    });
    $('div.playlists a.mp3').each(function(index){
        $('#jPlayer').jPlayer({
            ready: function () {
                f_set(0);
            }
        }).onProgressChange(function(loadPercent, playedPercentRelative, playedPercentAbsolute, playedTime, totalTime) {
            f_Info(loadPercent, playedPercentRelative, playedPercentAbsolute, playedTime, totalTime);
        });
        var Index = index;
        if ($(this).attr('class') === 'mp3') {
            $('body').data('max', index);
            $(this).attr('id', 'pesma' + index);
            $(this).click(function(){
                $('#jPlayer').jPlayer({
                    ready: function () {
                        f_set(Index);
                        f_play();
                    }
                }).onProgressChange(function(loadPercent, playedPercentRelative, playedPercentAbsolute, playedTime, totalTime) {
                    f_Info(loadPercent, playedPercentRelative, playedPercentAbsolute, playedTime, totalTime);
                });
                return false;
            });
        }
    });
    function f_set(Index) {
        $('#pesma' + $('body').data('index')).parent('li').removeClass('played');
        $('body').data('index', Index);
        $('#pesma' + Index).parent('li').addClass('played');
        $('#jPlayer').setFile($('#pesma' + Index).attr('href'));
    };
    function f_play() {
        try {
            $('#jPlayers').stop();
            $('div.pesmaPlay').show();
            $('div.pesmaPause').hide();
        } catch(e){}
        $('#jPlayer').play();
        return false;
    };
    function f_Sledeca() {
        var max = $('body').data('max');
        var index = ($('body').data('index') + 1 < max) ? $('body').data('index') + 1 : 0;
        f_set(index);
        f_play();
    };
    function f_Predhodna() {
        var max = $('body').data('max');
        var index = ($('body').data('index') - 1 >= 0) ? $('body').data('index') - 1 : max;
        f_set(index);
        f_play();
    };
    $('#pPrev').click(function(){
        f_Predhodna();
        return false;
    });
    $('#pNext').click(function(){
        f_Sledeca();
        return false;
    });
    function f_Info(loadPercent, playedPercentRelative, playedPercentAbsolute, playedTime, totalTime) {
        var myPlayedTime = new Date(playedTime);
        var ptMin = (myPlayedTime.getUTCMinutes() < 10) ? "0" + myPlayedTime.getUTCMinutes() : myPlayedTime.getUTCMinutes();
        var ptSec = (myPlayedTime.getUTCSeconds() < 10) ? "0" + myPlayedTime.getUTCSeconds() : myPlayedTime.getUTCSeconds();
        $("#info").text(ptMin + ":" + ptSec);
        var rBr = parseInt($('body').data('index')) + 1;
        $("#infoTxt").text((rBr < 10 ? "0" + rBr : rBr) + ". " + $('#pesma' + $('body').data('index')).text());
    }
});
function f_Broj(obj, n) {
    var klase = $(obj).parent().parent().attr('class');
    var broj = parseInt(klase.match(/\d+/)[0]);
    $('div.playlists ul.mpl' + broj).hide();
    $('div.playlists ul.mpl' + (broj + n)).show();
    return false;
};
function f_Facebook() {
    document.write('<fb:fan profile_id="27138498815" stream="0" connections="5" logobar="0" width="300" height="165"></fb:fan>');
};
/*
 * JQuery Random Plugin
 * 
 * Adds two random number functions to jQuery -
 * one to find a random number and one to find a random number between a max and min limit.
 * 
 * Version 1.0
 * 
 * by Christian Bruun - 23. jan 2009
 * 
 * Like it/use it? Send me an e-mail: rockechris@rockechris.com
 * 
 * License: None. Use and abuse. Comes with no warranty, of course!
 * 
 * 
 * Usage:
 * $.random(int);
 * $.randomBetween(min, max);
 * 
 * Code found at:
 * http://www.merlyn.demon.co.uk/js-randm.htm
 * 
 */
jQuery.extend({
    random: function(X) {
        return Math.floor(X * (Math.random() % 1));
    },
    randomBetween: function(MinV, MaxV) {
      return MinV + jQuery.random(MaxV - MinV + 1);
    }
});