
$(document).ready(function() {

	//select all the a tag with name equal to request-track
        $('a[name=request-track]').click(function(e) {
		//Cancel the link behavior
		e.preventDefault();

		//Get the A tag
		var id = $(this).attr('href');

                ////////////////////////
                var unic_id = $(this).attr('id');
                var author = $('#author_'+unic_id).attr('value');
                var title = $('#title_'+unic_id).attr('value');

                $('#request_name').val('');
                $('#request_message').val('');
                
                var strlen_max = 65 - (author.length + title.length);

                $('#chars_left').html(strlen_max);

                //alert(strl_tg_len);

                $('#author').html(author);
                $('#title').html(title);
                ////////////////////////



		//Get the screen height and width
		var maskHeight = $(document).height();
                //var maskWidth = $(window).width();
		var maskWidth = $(document).width();

		//Set heigth and width to mask to fill up the whole screen
		$('#mask').css({'width':maskWidth,'height':maskHeight});

		//transition effect
		$('#mask').fadeIn(1000);
		$('#mask').fadeTo("slow",0.8);

                

		//Get the window height and width
		var winH = $(window).height();
                //var winH = $(document.body).height();
		var winW = $(window).width();

                var winScrollTop = $(window).scrollTop();

                //alert(winScrollTop);

		//Set the popup window to center
                // Просто гениально! Сначала ищится центр веб страницы... потом центр нашего див бокса
                // Центры совмещаются
                // $(id).height() means that script search height div box that has id="dialog"
                // Т.е. в href мы передаем какой именно бокс мы будем трогать...
		//$(id).css('top',  winH/2-$(id).height()/2);
                $(id).css('top',  winH/2-$(id).height()/2+winScrollTop);
		$(id).css('left', winW/2-$(id).width()/2);

		//transition effect
		$(id).fadeIn(2000);

                
                $('#request_message,#request_name').bind('keyup', function() {
                    
                    var strlen_name = $('#request_name').attr('value').length;
                    var strlen_message = $('#request_message').attr('value').length;

                    var strlen_name_message = strlen_name + strlen_message;

//                    if( obj != null && ( input_length >= meta_length ) ){
//                        obj.value = obj.value.substring( 0, obj.value.length - 1  );
//                    }

                    //alert(strlen_name_message+' '+strlen_max);
                    
                    if( strlen_name_message > strlen_max ) {
                        //alert(strlen_name_message+' '+strlen_max);
                        var correct_input_vals = $(this).attr('value').substring( 0, $(this).attr('value').length - 1  );
                        $(this).val(correct_input_vals);
                        strlen_name_message -= 1;
                    }

                    $('#chars_left').html(strlen_max - strlen_name_message);

                });
                

                //send-request
                $('#send-request').click(function (e) {
                        //Cancel the link behavior
                        e.preventDefault();

                        $('#mask').hide();
                        $('.window').hide();

                        var requester_name = $('#request_name').attr('value');
                        var requester_message = $('#request_message').attr('value');
                        var requester_captcha = $('#request_captcha').attr('value');

                        //alert(requester_name);

                        var full_query = $('input[name=current-url-query]').attr('value');

                        var formated_query = full_query+"&song="+unic_id+"&name="+requester_name+"&msg="+requester_message+"&rc="+requester_captcha;

                        window.location.href = formated_query;

                });

                $('#refresh-captcha').click(function (e) {
                    
                    e.preventDefault();

                    var session_name = $('input[name=session_name]').attr('value');
                    var get_session_id_now = $('input[name=get_session_id_now]').attr('value');
                    var random_me = Math.round(Math.random() * (999999999 - 100000000) + 100000000);

                    //alert(random_me);


                    //alert("<img src=\"./kcaptcha/?"+session_name+"="+get_session_id_now+"&dummy="+random_me+"\" />");

                    //alert(session_name+' '+get_session_id_now);

                    $('#captcha-image-set').replaceWith("<div id=\"captcha-image-set\"><img src=\"./kcaptcha/?"+session_name+"="+get_session_id_now+"&dummy="+random_me+"\" /></div>");
                    //$('#captcha-image-set-debug').replaceWith("<div id=\"captcha-image-set-debug\"><textarea style=\"width: 100%; height: 100px;\"><img src=\"./kcaptcha/?"+session_name+"="+get_session_id_now+"&"+random_me+"="+random_me+"\" /></textarea></div>");

                    //alert();

                });

                //if close button is clicked
//                $('.window .close').click(function (e) {
//                    //Cancel the link behavior
//                    e.preventDefault();
//
//                    $('#mask').hide();
//                    $('.window').hide();
//                });

                //if close button is clicked
                $('#closewindow').click(function (e) {
                    //Cancel the link behavior
                    e.preventDefault();

                    $('#request_message,#request_name').unbind('keyup');

                    $('#mask').hide();
                    $('.window').hide();
                });

                //if mask is clicked
                $('#mask').click(function () {
                    //Cancel the link behavior
                    //e.preventDefault();

                    $('#request_message,#request_name').unbind('keyup');
                    
                    $(this).hide();
                    $('.window').hide();
                });




        });

        

	

});
