function sayAlert(message, title){
    Shadowbox.open({
        content:    message,
        player:     "html",
        title:      title,
        height:     350,
        width:      350
    });
}


$.fn.wait = function(time, type){
    time = time || 1000;
    type = type || "fx";
    return this.queue(type, function(){
        var self = this;
        setTimeout(function(){
            $(self).dequeue();
        }, time);
    });
};

$.fn.reloadpage = function(time, type){
    time = time || 1000;
    type = type || "fx";
    return this.queue(type, function(){
        var self = this;
        setTimeout(function(){
        	window.location.reload(true);
        }, time);
    });
};

$.fn.resizeImgContainer = function(){
    $(this).closest(".image-container").width($(this).width());
};

//TODO: это костыль
$(window).load(function(){
	$(".image-show > .image-container img, .field .image-container img").resizeImgContainer();
});

$(window).resize(function () {
	if(window.innerWidth) var wh = window.innerWidth;
	else wh = (document.documentElement.clientWidth>0) ? document.documentElement.clientWidth : document.body.clientWidth;
	var move =  Math.floor((wh)/8) * 4;

	$("#player_iframe").css('margin-left', 0);
	$("#player_iframe").css('left', move + 76 + 'px');
});

$(document).ready(function(){
    if(window.innerWidth) var wh = window.innerWidth;
    else wh = (document.documentElement.clientWidth>0) ? document.documentElement.clientWidth : document.body.clientWidth;
    var move =  Math.floor((wh)/8) * 4;

    $("#player_iframe").css('margin-left', 0);
    $("#player_iframe").css('left', move + 76 + 'px');

    $(".image-container").hover(function(){
            $(this).find(".gallery-info").slideDown("fast");
        },function(){
            $(this).find(".gallery-info").slideUp("fast");
        }
    );

    $(".check-default").click(function(){
            $(this).closest("form").submit();
    });


//    $(".image-show .image-container img").load(function(){
//        console.log(".image-show img width = " + $(this).width());
//        $(this).closest(".image-container").width($(this).width());
//    });

    $("#avatrschanges").change(function() {
        var url = "ajax/cca/?id=" + $(this).val();
        var lspan = $(this).parent().find("span");
        $.ajax({
            type: "GET",
            dataType: "json",
            url: url,
            beforeSend: function(){
            },
            success: function(data){
                $(lspan).fadeTo(100, 0.25).wait(100).fadeTo(100, 1);
            },
            complete: function(){
            }
        });
    });

	$('.search_tab, .search_tab_left, .search_tab_right').click(function(){
		if($(this).attr('href') != '#')  {
			$('#newsearchform').attr('action', $(this).attr('href'));
		}
		$('#searchtype').val($(this).attr('rel'));
		$('.search_tab, .search_tab_left, .search_tab_right').removeClass('visible');
		$('.search_tab, .search_tab_left, .search_tab_right').removeAttr('style');
		$(this).addClass('visible');

		if($(this).hasClass('search_tab_left')){
			$(this).css('background', 'transparent url(/static/images/tab_left.png) right no-repeat');
		}
		if($(this).hasClass('search_tab_right')){
			$(this).css('background', 'transparent url(/static/images/tab_right.png) left no-repeat');
		}
		$(this).css('z-index', '1002');
		if (($.browser.msie && parseInt($.browser.version) == 6)) {
			$('.search_input').focus();
			$(this).focus();
		}
		return false;
	});
	$('.search_input input').click(function(){
		$(this).select();
	});

    $(".add-to-playlist").click(function(e){
        $.ajax({
            type    : "POST",
            url     : $(e.target).parent().attr("href"),
            async   : true,
            success : function(){
                $(e.target).fadeTo(100, 0.25).wait(100).fadeTo(100, 1).closest('img').attr('src','/static/images/media_add_done.png');
            }
        });
        return false;
    });

    $(".recommended-tracks .add-to-playlist").click(function(e){
        var recommendePanel = $(this).closest(".recommended-tracks");
        var tracksPanel = recommendePanel.prev(".playlist-tracks");
        var row = $(this).closest("tr");

        $.ajax({
            url     : $(e.target).attr("href"),
            success : function(){
                $(e.target).fadeTo(100, 0.25).wait(100).fadeTo(100, 1);
                row.fadeOut();
                row.appendTo(tracksPanel.find("tbody"));
                row.fadeIn();
            }
        });
        return false;
    });

    $("textarea.textarea").autogrow({
          min_height: 64
    });

//    $(".chat-reply textarea").autogrow({
//          min_height: 96
//    });


    $("#search-filter-tabs").tabs();


    /*$(".media-artist .artist-info").jTruncate({
        length  : 84,
        minTrail: 12,
        moreText: "[+]",
        lessText: "[-]",
        moreAni : "fast",
        lessAni : "fast"
    });*/
    $(".media-artist .artist-info").truncate({
    	max_length  : 84,
        more: "[+]",
        less: "[-]",
        moreAni : "fast",
        lessAni : "fast"
    });

    $('.media-list-gallery .item-list').smileCarousel();

    $(".user-data form").keypress(function (e) {
      if (e.which == 13) {
          return false;
      }
    });

    $(".playpausebtn").click(function (e) {
        var playId = $(e.target).attr('id');
        if ($(e.target).hasClass('play')) {

            $(e.target).removeClass('play');
            $(e.target).addClass('pause');
            document.getElementById('mu' + playId).action('pause');

        }else{
            if (undefined != $('.playpausebtn.play').attr('id')) {
                var playId2 = $('.playpausebtn.play').attr('id');
                document.getElementById('mu' + playId2).action('pause');
            }
            $('.playpausebtn').removeClass('play');
            $('.playpausebtn').addClass('pause');

            $(e.target).removeClass('pause');
            $(e.target).addClass('play');
            document.getElementById('mu' + playId).action('play');
        }
    });


});
