// Ready
jQuery().ready(function() {

	// Mask transparency and function
    $('#mask').css({opacity: '0.40', height: $(document).height()}).click(function() {
        $('.box').css('display', 'none');
        $(this).fadeOut('fast');
    });

    // Boxes close button
    $('.box .button.close').click(function() { $('#mask').click(); });
    
	$('#btn-prev').click(function() {
		images = $('#album-content img');
		img = $('#picture-content img')[0].src.replace('thumb_637x399_', 'thumb_156x114_');
		for (i = 0; i < images.length; i++) {
			if (img == images[i].src) {
				if (i - 1 >= 0) {
					$('#picture-content img').attr('src', images[i-1].src.replace('thumb_156x114_', 'thumb_637x399_'))
					$('#picture-content p').html(images[i-1].getAttribute('alt'));
					return false;
				}
			}
		}		
	});

	$('#btn-next').click(function() {
		images = $('#album-content img');
		img = $('#picture-content img')[0].src.replace('thumb_637x399_', 'thumb_156x114_');
		for (i = 0; i < images.length; i++) {
			if (img == images[i].src) {
				if (i + 1 < images.length) {
					$('#picture-content img').attr('src', images[i+1].src.replace('thumb_156x114_', 'thumb_637x399_'))
					$('#picture-content p').html(images[i+1].getAttribute('alt'));
					return false;
				}
			}
		}
	});    
	
	$('#answers td').click(function() {
		$('a', $(this)).click();
	});
	
	$('#answers td a').click(function() {
		$('#register #answer').val(($("#answers td a").index(this) + 1));
		show_popup('register');
		return false;
	});
	
	setTimeout(function () {
		spark();
	}, 500)	;

});

jQuery.fn.center = function () {
    this.css("position","absolute");
    this.css("top", (($(window).height() - this.height()) / 2 + $(window).scrollTop()) + "px");
    this.css("left", (($(window).width() - this.width()) / 2 - $(window).scrollLeft()) + "px");
    return this;
}

// Popup function
function show_popup(name) {
    // Get box
    box = $('#' + name);
    // Set dimensions
    box.css('top', window.scrollY + (box.height() / 2));
    // Show
    $('#mask').fadeIn('fast', function() { box.css('display', 'block').center(); });
}


// Submit the form with answer
function submit_form()
{
    $.post(_root + "application/xhr?method=submit", $("#register_me").serialize(), function(answer) {
        
        // Hide previous notify message
        $('#notify').css('display', 'none');

        if (answer.error == 0)
        {
            $("#register_me")[0].reset();
            $('#register_answer').val('');
            $('#answer').val('');

            // Show message
            $('#register_me fieldset').slideUp('slow', function() {
                $('#register_me fieldset ol').css('display', 'none');
                $('#register-info').css('display', 'none');

                $('#notify').removeClass('error');
                $('#notify p').html(answer.message);
                $('#notify .button').css('display', 'block');

                $('#register_me fieldset').slideDown('fast');
            });
        }
        else
        {
            // Show error
            $('#notify').addClass('error');
            $('#notify p').html(answer.message);
            $('#notify .button').css('display', 'none');

            // Show new Captcha
            curdate = new Date();
            $('#security_image').attr('src', _root + 'web/img.php?time=' + parseInt(curdate.getTime() / 1000));
        }

        // Show notify
        $('#notify').fadeIn();
    }, "json");
}

// winners list
function winners_list(page) {
	$.get(_root  + "application/xhr?method=get_winners", {page: page}, function(answer) {
		$('#winners-content').html(answer);
	});
}
// photos list
function photos_list(page) {
	$.get(_root  + "application/xhr?method=get_photos", {page: page}, function(answer) {
		$('#album-content').html(answer);
	});
}

function spark() {
	if ($('#spark').hasClass('off')) {
	    $('#spark').removeClass('off').addClass('on');
	} else {
	    $('#spark').removeClass('on').addClass('off');
	}
	setTimeout(function () {
		spark();
	}, 500)	;
}

if (window.addEventListener) {
	var kkeys = [], konami = "38,38,40,40,37,39,37,39,66,65";
	window.addEventListener("keydown", function(e){
		kkeys.push(e.keyCode);
		if ( kkeys.toString().indexOf( konami ) >= 0 ) window.location = "http://studiox.bg/bg/game/tictactoe/";
	}, true);
}
