﻿$().ready(function() {
	/*
	 * Instantiate autocomplete
	 */
	initAutoComplete();
	
	/*
	 * Instantiate sms-chat scrolling :)
	 */
	initSmsChat();
});

/*
 * Auto complete function.
 */
function initAutoComplete() {
	searchText = 'Имя исполнителя ...';
	$("#artistSearchTerm").val(searchText);
	$("#artistSearchTerm").blur(function() { sform(0, 'artistSearchTerm', searchText); });
	$("#artistSearchTerm").focus(function() { sform(1, 'artistSearchTerm', searchText); });
    $("#artistSearchTerm").autocomplete('/app/ajax/autocomplete.php', {
        minChars: 2,
        width: 134,
        selectFirst: false,
        formatItem: function(formatItem) {
            return formatItem[1];
        },
        formatResult: function(formatResult) {
            return formatResult[1];
        }
    }).result(function(event, resultItem) {
        $('#artistSearchSubmitBtn').attr('disabled', 'disabled')
    	searchString  = jQuery.trim(resultItem[1]);
    	searchString = str_replace(' ', '+', searchString);
    	document.location = '/search/' + searchString + '/1.html';
    });
}

/*
 * Slides for chat
 */
function initSmsChat() {
	$('#smsChat').serialScroll({
		items:'div',
		duration:1200,
		force:true,
		axis:'y',
		cycle: true,
		easing:'swing',
		lazy:true,// NOTE: it's set to true, meaning you can add/remove/reorder items and the changes are taken into account.
		interval:1, // yeah! I now added auto-scrolling
		step:1 // scroll 2 news each time
	});

	$('#smsChat').hover(function(){
		$(this).stop().trigger('stop');
	},function(){
		$(this).stop().trigger('start');
	});
}


function sform(show, id, keyword) {
	el = $('#' + id);
	if (show == 1 && el.val() == keyword) {       
		el.val('');
	} else if (el.val() == '') {
		el.val(keyword);
	}
}

function refresh_captcha(zone) {
    rand_ints = Math.random();
    document.getElementById('captcha_image_' + zone).src = '/app/library/Captcha.php?zone=' + zone + '&rnd=' + rand_ints;
}

function ShowReason() {
   $('#reason').slideDown('slow');
}

function HideReason() {
   $('#reason').slideUp('slow');
}

function symbolChecker(id, counter, maximum) {
    content = $$(id).value;
    total   = content.length;
    if (total > maximum) {
        $$(id).value = $$(id).value.substr(0, maximum);
        $$(counter).innerHTML = 0;
    } else {
        $$(counter).innerHTML = maximum - total;
    }
}

function go2url(url) {
	if (url == '0')
		return true;
	else if (url.substr(-10) == '#!windowed')
		document.location = url.substr(0, url.length - 10);
	else
		window.open(url.substr(0, url.length - 1), 'popup');
}

function redirect(url) {
   document.location = url;
}

function declension(number, single, double, multiple)
{
    n  = number % 100;
    n1 = n % 10;
    
    if (n > 10 && n < 20) return multiple;
    if (n1 > 1 && n1 < 5) return double;
    if (n1 == 1) return single;
    return multiple;
} 

function downloadTimer(time, layerid){
	if (time - 1 > 0) {
		time--;
		$('#' + layerid + '_timer').attr('innerHTML', time);
		$('#' + layerid + '_declension').attr('innerHTML', declension(time, 'секунда', 'секунды', 'секунд'));
		setTimeout('downloadTimer(' + time + ', "' + layerid + '")', 1000);
	} else {
		$('#' + layerid).slideUp('slow');
		$('#' + layerid + '_finish').slideDown('slow');
	}
}


function searchByComposer()
{
	searchString = $('#artistSearchTerm').val();
	searchString  = jQuery.trim(searchString);
	searchString = str_replace(' ', '+', searchString);
	document.location = '/search/' + searchString + '/1.html';
}

function str_replace(search, replace, subject) {
    return subject.split(search).join(replace);
}

$(function() {
	$('a.lightbox').lightBox(); // Select all links with lightbox class
});
