$(function() {

	$('.jqtransform').jqTransform();

	$('.vertical-scroll,').jScrollPane({ 'showArrows' : true, 'scrollbarWidth' : 21, 'scrollbarMargin': 2 });

	$('#notice .close').live('click', function() {
		close_notice();
		return false;
	});

	$('#home-slider .theslider .buttons a').live('click', function() {
		var container = $('#home-slider .theslider .images .content').not(':animated');
		if (!container.length) {
			return false;
		}
		var images = '#home-slider .theslider .images .slider_image .reflected';
		var possibleMoves = $(images).length;
		var firstElement = $(images+':first');
		var lastElement = $(images+':last');
		var currentElement = $('#home-slider .theslider .images .big .reflected');
		var nextElement = "";

		if ($(this).hasClass('next')) {
			nextElement = currentElement.parent().next().find("img");
			if (nextElement.length == 0) {
				nextElement = firstElement;
				delta = 151;
			} else {
			    delta = 207;
			}

            if($(this).hasClass('semana')){
                if(esperaoPrimeiroClick == true){
                    if(busy == true){return false;}

                    busy = true;
                    var elemento = $('#texto-eventos-Semana .item:first');
                    var novoElemento = elemento.clone();
                    novoElemento.css('width', '385px');
                    novoElemento.css('margin-right', '0px');
                    $('#texto-eventos-Semana').append(novoElemento);
                   // $('#servicos_rail .item_servicos_portfolio:first *').remove();
                    elemento.animate({marginLeft: '-390px'}, 500, function(){elemento.remove(); busy = false;});
                }
            }else if($(this).hasClass('porai')){
                if(EsperaPrimeiroClick2 == true){
                    if(busy3 == true)
                        return false;
                    busy3 = true;
                    var elemento = $('#texto-evento-oquevem .item:first');
                    var novoElemento = elemento.clone();
                    novoElemento.css('width', '385px');
                    novoElemento.css('margin-right', '0px');
                    $('#texto-evento-oquevem').append(novoElemento);
                   // $('#servicos_rail .item_servicos_portfolio:first *').remove();
                    elemento.animate({marginLeft: '-390px'}, 500, function(){elemento.remove(); busy3 = false;});
                }
            }

			//nextSong(nextElement.attr("ref"));
		} else if ($(this).hasClass('prev')) {
			nextElement = currentElement.parent().prev().find("img");
			if (nextElement.length == 0) {
				nextElement = lastElement;
			}


            if($(this).hasClass('semana')){
                if(esperaoPrimeiroClick == true){
                    if(busy1 == true){ return false; }
                    busy1 = true;
                    var elemento = $('#texto-eventos-Semana .item:last');
                    var novoElemento = elemento.clone();
                    novoElemento.css('width', '385px');
                    novoElemento.css('margin-left','-390px');
                    $('#texto-eventos-Semana').prepend(novoElemento);
                    elemento.remove();
                    $('#texto-eventos-Semana .item:first').animate({marginLeft: '0px'},500, function(){busy1 = false;});
                }
            }else if($(this).hasClass('porai')){
                if(EsperaPrimeiroClick2 == true){
                    if(busy4 == true) return false;
                    busy4 = true;
                    var elemento = $('#texto-evento-oquevem .item:last');
                    var novoElemento = elemento.clone();
                    novoElemento.css('width', '385px');
                    novoElemento.css('margin-left','-390px');
                    $('#texto-evento-oquevem').prepend(novoElemento);
                    elemento.remove();
                    $('#texto-evento-oquevem .item:first').animate({marginLeft: '0px'},500, function(){busy4 = false;});
                }
            }

			//nextSong(nextElement.attr("ref"));
			delta = 151;
		}

		container.animate({
			'left': (-1 * $(nextElement).position().left + delta) + 'px'
		});

		currentElement.parent().animate({
			'width': '134px',
			'height': '262px',
			'marginTop': '30px'
		});
		currentElement.animate({
			'width': '134px',
			'height': '192px'
		});
		currentElement.next().animate({
		    'width': '134px'
		});
		nextElement.parent().animate({
			'width': '190px',
			'height': '272px',
			'marginTop': '0'
		});
		nextElement.animate({
			'width': '190px',
			'height': '272px',
			'marginTop': '0'
		});
		nextElement.next().animate({
		    'width': '190px'
		});

		currentElement.parent().removeClass('big').addClass('thumb');
		nextElement.parent().removeClass('thumb').addClass('big');

		return false;
	});


	$('#home-slider2 .theslider .buttons a').live('click', function() {
		var container = $('#home-slider2 .theslider .images .content').not(':animated');
		if (!container.length) {
			return false;
		}
		var images = '#home-slider2 .theslider .images .slider_image .reflected';
		var possibleMoves = $(images).length;
		var firstElement = $(images+':first');
		var lastElement = $(images+':last');
		var currentElement = $('#home-slider2 .theslider .images .big .reflected');
		var nextElement = "";

		if ($(this).hasClass('next')) {
			nextElement = currentElement.parent().next().find("img");
			if (nextElement.length == 0) {
				nextElement = firstElement;
				delta = 151;
			} else {
			    delta = 207;
			}

            if($(this).hasClass('semana')){
                if(esperaoPrimeiroClick == true){
                    if(busy == true){return false;}

                    busy = true;
                    var elemento = $('#texto-eventos-Semana .item:first');
                    var novoElemento = elemento.clone();
                    novoElemento.css('width', '385px');
                    novoElemento.css('margin-right', '0px');
                    $('#texto-eventos-Semana').append(novoElemento);
                   // $('#servicos_rail .item_servicos_portfolio:first *').remove();
                    elemento.animate({marginLeft: '-390px'}, 500, function(){elemento.remove(); busy = false;});
                }
            }else if($(this).hasClass('porai')){
                if(EsperaPrimeiroClick2 == true){
                    if(busy3 == true)
                        return false;
                    busy3 = true;
                    var elemento = $('#texto-evento-oquevem .item:first');
                    var novoElemento = elemento.clone();
                    novoElemento.css('width', '385px');
                    novoElemento.css('margin-right', '0px');
                    $('#texto-evento-oquevem').append(novoElemento);
                   // $('#servicos_rail .item_servicos_portfolio:first *').remove();
                    elemento.animate({marginLeft: '-390px'}, 500, function(){elemento.remove(); busy3 = false;});
                }
            }

			//nextSong(nextElement.attr("ref"));
		} else if ($(this).hasClass('prev')) {
			nextElement = currentElement.parent().prev().find("img");
			if (nextElement.length == 0) {
				nextElement = lastElement;
			}


            if($(this).hasClass('semana')){
                if(esperaoPrimeiroClick == true){
                    if(busy1 == true){ return false; }
                    busy1 = true;
                    var elemento = $('#texto-eventos-Semana .item:last');
                    var novoElemento = elemento.clone();
                    novoElemento.css('width', '385px');
                    novoElemento.css('margin-left','-390px');
                    $('#texto-eventos-Semana').prepend(novoElemento);
                    elemento.remove();
                    $('#texto-eventos-Semana .item:first').animate({marginLeft: '0px'},500, function(){busy1 = false;});
                }
            }else if($(this).hasClass('porai')){
                if(EsperaPrimeiroClick2 == true){
                    if(busy4 == true) return false;
                    busy4 = true;
                    var elemento = $('#texto-evento-oquevem .item:last');
                    var novoElemento = elemento.clone();
                    novoElemento.css('width', '385px');
                    novoElemento.css('margin-left','-390px');
                    $('#texto-evento-oquevem').prepend(novoElemento);
                    elemento.remove();
                    $('#texto-evento-oquevem .item:first').animate({marginLeft: '0px'},500, function(){busy4 = false;});
                }
            }

			//nextSong(nextElement.attr("ref"));
			delta = 151;
		}

		container.animate({
			'left': (-1 * $(nextElement).position().left + delta) + 'px'
		});

		currentElement.parent().animate({
			'width': '134px',
			'height': '262px',
			'marginTop': '30px'
		});
		currentElement.animate({
			'width': '134px',
			'height': '192px'
		});
		currentElement.next().animate({
		    'width': '134px'
		});
		nextElement.parent().animate({
			'width': '190px',
			'height': '272px',
			'marginTop': '0'
		});
		nextElement.animate({
			'width': '190px',
			'height': '272px',
			'marginTop': '0'
		});
		nextElement.next().animate({
		    'width': '190px'
		});

		currentElement.parent().removeClass('big').addClass('thumb');
		nextElement.parent().removeClass('thumb').addClass('big');

		return false;
	});



	$('.artists-toggler a').live('click', function() {
		var rel = $(this).attr('rel');
		$('.artists-toggler a').removeClass('active');
		if (rel == 'show') {
			$('#artists').slideDown();
		} else {
			$('#artists').slideUp();
		}
		$(this).addClass('active');
		return false;
	});

	//$('.slider-gallery .slider .content a').live('click', function() {
	//	return false;
	//});

	$('.slider .buttons a').live('click', function() {
        var slider = $(this).parents('.slider:eq(0)');
        var fragments = slider.find('.content li');
        var container = slider.find('.content');

        var currentPage = slider.data('position');
        if (!currentPage) {
            currentPage = 1;
        }
        var fragments_count = fragments.length;
        var fragmet_width = 106;
        var perPage = 1;
        var numPages = Math.ceil(fragments_count/perPage) - 2;
        var stepMove = fragmet_width*perPage;
        var firstPosition = 0;
        var lastPosition = -1*((numPages-1)*stepMove);

        if ($(this).hasClass('next')) {
            currentPage ++;
            if (currentPage > numPages) {
                slider.data('position', 1);
                container.animate({'left': firstPosition});
                return false;
            }
            container.animate({'left': -((currentPage - 1)*stepMove)});
        }
        if ($(this).hasClass('prev')) {
            currentPage --;
            if (currentPage < 1) {
                slider.data('position', numPages);
                container.animate({'left': lastPosition});
                return false;
            }
            container.animate({'left': -((currentPage-1)*stepMove)});
        }
        slider.data('position', currentPage);

        return false;
    });

});

function close_notice() { $('#notice').slideUp(); }

function nextSong(id){
    $.post("/artists/load_homepage_data/", { id: id },
        function(html){
            $('#homepage_player').html(html);
    });
}
