var current_sponsor = 0;
var current_image = 0;

function switch_bg(index, callback) {

    if (images[index] == null) return false;

    var img = new Image();

    $(img).load(function () {

        var $old_bg = $("#bg");
        var $new_bg = $old_bg.clone().insertAfter("#bg");

        $new_bg.css( { opacity: 0, backgroundImage: 'url(' + img.src + ')' } );
        $new_bg.animate( {opacity: 1}, 400, function () {
            $old_bg.remove();
            if (typeof(callback) == 'function') {
                callback();
            }
        });

    });

    img.src = '/uploads/page_image/' + images[index];

}

function escGame(ev) {
  if (ev.which == 27) {
	  
	 // $("#game").hide()
	  window.location.href='http://www.tadasblinda.lt/';
	  
	};
}

var c= ''
var pressed=''	

$(document).ready(function()
{	
	$(document).keydown(escGame); 
	
	$(document).keypress(function(e)
	{	
		c = String.fromCharCode(e.keyCode|e.charCode);
		pressed += c
		
		if(pressed.length==12 && pressed=='maistininkas') {
			$("#game").show()
			$("#bg-flash").hide()
			
			pressed = ''
		}
		
		if(pressed.length>12) pressed = ''

	});
	
	$("#game").hide()
	
});


$(function() {

setInterval(function () {
        //$('#slider .slider').children().length == current_sponsor ? current_sponsor = 0 : current_sponsor++;
        //$.slide($('#slider'), current_sponsor);
    }, 2000);

    if (false && images.length > 1) {

        function bg_animation() {
            setTimeout(function () {
                current_image = current_image == images.length - 1 ? 0 : current_image + 1;
                switch_bg(current_image, function () {
                    bg_animation();
                });
            }, 4000);
        };

        bg_animation();

    }

    var js_actions = $('body').attr('class').match(/js\-[a-z_]+/g);

    if (js_actions != null) {
        $.each(js_actions, function () {
            if (typeof(primer[this.substr(3)]) == 'function') {
                primer[this.substr(3)]();
            }
        });
    }

});

primer = {
    gallery : function () {

        $("#subnav a").click(function () {
            if ($(this).parent().is('.act')) {
                return false;
            }
            $("#subnav .act").removeClass('act');
            $(this).parent().addClass('act');
        })

        $(".fotovideo a").fancybox();

    },
    sponsors : function () {

        $("#wrap-sponsors a").each(function () {
            if ($(this).attr('href') != 'javascript:void(0)') {
                $(this).attr('target', '_blank');
            } else {
                $(this).css('cursor', 'default');
            }
        })

        $("#wrap-sponsors a").hover(function () {

            var new_src = $(this).find('img:last').attr('src');
            var old_src = $(this).find('img:first').attr('src');

            $(this).data('src', old_src).find('img:first').attr('src', new_src);

        }, function () {

            $(this).find('img:first').attr('src', $(this).data('src'));

        });

    }
}

jQuery.extend({
    slide: function($container, child_element_no, callback) {
        var $slider = $container.children(":first");
        var new_offset = $slider.children(':eq('+child_element_no+')');
        if (new_offset.length == 0) {
            new_offset = 0;
        } else {
            new_offset = new_offset.offset().left - $container.offset().left - ($slider.offset().left - $container.offset().left);
        }
        $slider.animate( {left: -1 * new_offset}, 600, 'easeOutQuad', callback);
        return this;
    },
    slide_page: function($container, page_no, size, callback) {
        if (typeof(size) == 'undefined') {
            size = parseInt($container.parent().css('width').match(/\d+/));
        }
        var new_offset = size * page_no;
        $container.animate( {left: -1 * new_offset}, 600, 'easeOutQuad', callback);
        return this;
    },
    imgLoad: function(src, loaded_callback) {
        var img = new Image();
        $(img).load( function () {
            loaded_callback(img);
        });
        img.src = src;
        return this;
    },
    dimIn: function (user_css, callback, on_click) {
        $dim = $("#dim");
        if ($dim.length == 0) {
            $dim = $("<div />").attr('id', 'dim').css(
                { position: 'absolute',
                  top: 0,
                  left: 0,
                  width: '100%',
                  height: $(document).height(),
                  zIndex: 99999998,
                  background: '#333',
                  opacity: .4 } );
            $('body').prepend($dim);
        }
        if (on_click != undefined) {
            $dim.click(on_click);
        }
        if ($dim.data('act') == true) return false;
        if (user_css != undefined) {
            $dim.css(user_css);
        }
        return $dim.data('act', true).fadeIn(400, callback);
    },
    dimOut: function (callback) {
        $dim = $("#dim").unbind('click');
        if ($dim.data('act') == true) {
            $dim.fadeOut(400, callback).data('act', false);
        }
    }
});

jQuery.fn.extend({
    center: function () {
        this.css( {position: 'absolute',
                    top: $(document).scrollTop() + (Math.ceil($(window).height()) - this.height()) / 2,
                    left: '50%',
                    marginLeft: -1 * Math.ceil(this.width() / 2)} );

        return this;
    },
	fadeOver: function (speed, callback) {
		c = this.clone();
		c.css( {position: 'absolute',
				 width: this.width(),
				 height: this.height(),
				 display: 'none',
				 zIndex: 2} );
        $tp = this.parent(); // container
		$tp.css( {width: $tp.width(),
				   height: $tp.height()} );

		c.insertBefore(this).fadeIn(speed, function () {
			c.next().remove();
			c.css( {position: 'relative', zIndex: 1} );
			if (typeof(callback) == 'function') callback();
		});
		return this;
	}
});

//- required
function popup(url) {

    if (typeof(window.popuped) == 'undefined') {
        window.popuped = $('<div style="z-index:99999999; display: none; cursor: pointer; position: absolute; padding: 7px; background: #ffffff; box-shadow: 0 0 5px #666; -moz-box-shadow: 0 0 5px #666; -webkit-box-shadow: 0 0 5px #666;" />').appendTo('body').click( function () {
            $(this).find('img').fadeOut(400).end().fadeOut(400);$.dimOut();
        } );
        $('<img style="display: none" />').appendTo(window.popuped);
    }

    $popuped = window.popuped;

    $.dimIn({}, null, function () {
        window.popuped.click();
    });

    var props = {width: 100,
                  height: 100,
                  left: Math.ceil($(window).width() / 2) - 50,
                  top: $(document).scrollTop() + Math.ceil(($(window).height() - 25) / 2) - 20};

    if ($popuped.css('display') == 'none') {
        $popuped.css(props);
    } else {
        $popuped.find('img').fadeOut(400);
    }

    var img = new Image();

    $popuped.fadeIn(400);

    $(img).load( function () {

        $popuped.find('img').attr('src', img.src).end().animate(
            {
                left: Math.ceil(($(window).width() - img.width) / 2),
                top: (($(window).height() - img.height) / 2) + $(document).scrollTop(),
                height: img.height,
                width: img.width
            }, 400, function () {
                $(this).find('img').fadeIn('fast');
            }
        );


    });

    img.src = url;

}

$.easing['jswing'] = $.easing['swing'];

$.extend( $.easing,
{
	def: 'easeOutQuad',
	swing: function (x, t, b, c, d) {
		//alert(jQuery.easing.default);
		return $.easing[$.easing.def](x, t, b, c, d);
	},
	easeInQuad: function (x, t, b, c, d) {
		return c*(t/=d)*t + b;
	},
	easeOutQuad: function (x, t, b, c, d) {
		return -c *(t/=d)*(t-2) + b;
	},
	easeInOutQuad: function (x, t, b, c, d) {
		if ((t/=d/2) < 1) return c/2*t*t + b;
		return -c/2 * ((--t)*(t-2) - 1) + b;
	},
	easeInCubic: function (x, t, b, c, d) {
		return c*(t/=d)*t*t + b;
	},
	easeOutCubic: function (x, t, b, c, d) {
		return c*((t=t/d-1)*t*t + 1) + b;
	},
	easeInOutCubic: function (x, t, b, c, d) {
		if ((t/=d/2) < 1) return c/2*t*t*t + b;
		return c/2*((t-=2)*t*t + 2) + b;
	},
	easeInCirc: function (x, t, b, c, d) {
		return -c * (Math.sqrt(1 - (t/=d)*t) - 1) + b;
	},
	easeOutCirc: function (x, t, b, c, d) {
		return c * Math.sqrt(1 - (t=t/d-1)*t) + b;
	},
	easeInOutCirc: function (x, t, b, c, d) {
		if ((t/=d/2) < 1) return -c/2 * (Math.sqrt(1 - t*t) - 1) + b;
		return c/2 * (Math.sqrt(1 - (t-=2)*t) + 1) + b;
	}
});


