$(function() {
	$("input[name='secure_code']").val('siteform').parent().hide();
	$("a[rel*='clearbox[item]']").attr('rel', 'clearboxd[gallery=]');
	$('#m1').addClass("active");			
	// rotator testemoniala
	$('.testimonial .rotator').cycle({
		fx:     'fade',
		speed:  'slow',
		timeout: 10000,
		random: 1,
		cleartype:  true,
		cleartypeNoBg:  true
		});		
	// animate navigation background
	$.extend($.fx.step,{
			backgroundPosition: function(fx) {
				if (fx.state === 0 && typeof fx.end == 'string') {
					var start = $.curCSS(fx.elem,'backgroundPosition');
					start = toArray(start);
					fx.start = [start[0],start[2]];
					var end = toArray(fx.end);
					fx.end = [end[0],end[2]];
					fx.unit = [end[1],end[3]];
				}
				var nowPosX = [];
				nowPosX[0] = ((fx.end[0] - fx.start[0]) * fx.pos) + fx.start[0] + fx.unit[0];
				nowPosX[1] = ((fx.end[1] - fx.start[1]) * fx.pos) + fx.start[1] + fx.unit[1];
				fx.elem.style.backgroundPosition = nowPosX[0]+' '+nowPosX[1];
	
			   function toArray(strg){
				   strg = strg.replace(/left|top/g,'0px');
				   strg = strg.replace(/right|bottom/g,'100%');
				   strg = strg.replace(/([0-9\.]+)(\s|\)|$)/g,"$1px$2");
				   var res = strg.match(/(-?[0-9\.]+)(px|\%|em|pt)\s(-?[0-9\.]+)(px|\%|em|pt)/);
				   return [parseFloat(res[1],10),res[2],parseFloat(res[3],10),res[4]];
			   }
			}
		});	

    $('#form form').submit(function (event) {
        event.preventDefault();
        $.post('/kontakt/post/', $('#form form').serialize(), function (data, textStatus, jqXHR) {
            if (data.mail_sent) {
                $('#form').fadeOut();
                $('#form-success').fadeIn();
                $('#jamstvo').fadeOut();
            } else {
                $('input').removeClass('error');
                $('input').attr('title', '');
                for (f in data) {
                    $('#id_' + f).addClass('error');
                    $('#id_' + f).attr('title', data[f][0]);
                }
            }
        });
    });

    $('#trening').jScrollPane();
	$('.button_black')
		.css( {backgroundPosition: "0 0"} )
		.mouseover(function(){
			$(this).stop().animate(
				{backgroundPosition:"(-284px 0)"}, 
				{duration:300})
			})
		.mouseout(function(){
			$(this).stop().animate(
				{backgroundPosition:"(0 0)"}, 
				{duration:300})
			});
	$('#submit_btn')
		.css( {backgroundPosition: "-159px 0"} )
		.mouseover(function(){
			$(this).stop().animate(
				{backgroundPosition:"(0 0)"}, 
				{duration:300})
			})
		.mouseout(function(){
			$(this).stop().animate(
				{backgroundPosition:"(-159px 0)"}, 
				{duration:300})
			});			

	$('.button_red')
		.css( {backgroundPosition: "-310px 0"} )
		.mouseover(function(){
			$(this).stop().animate(
				{backgroundPosition:"(0 0)"}, 
				{duration:300})
			})
		.mouseout(function(){
			$(this).stop().animate(
				{backgroundPosition:"(-310px 0)"}, 
				{duration:300})
        });

	$('#nav li a')
		.mouseover(function(){
			if (!$(this).hasClass("active"))
				$(this).stop().animate({backgroundPosition:"(0 0)"}, {duration:500});
		})
		.mouseout(function(){
			if (!$(this).hasClass("active"))
				$(this).stop().animate({backgroundPosition:"(0 -120px)"}, {duration:500});
    });

	$('.social')
		.css( {backgroundPosition: "0 -55px"} )
		.mouseover(function(){
			$(this).stop().animate(
				{backgroundPosition:"(0 0px)"}, 
				{duration:500})
			})
		.mouseout(function(){
			$(this).stop().animate(
				{backgroundPosition:"(0 -55px)"}, 
				{duration:500})
    });
	$('#logo')
		.css( {backgroundPosition: "0 -100px"} )
		.mouseover(function(){
			$(this).stop().animate(
				{backgroundPosition:"(0 0px)"}, 
				{duration:500})
			})
		.mouseout(function(){
			$(this).stop().animate(
				{backgroundPosition:"(0 -100px)"}, 
				{duration:500})
    });
	

	var $speed = 1500,
		clearMenuBackground = function () {
			$("#nav li a").removeClass("active");
			$("#nav li a").stop().animate({backgroundPosition:"(0 -120px)"}, {duration:300});
		}, 
		setMenuBackground = function (selector) {
			$(selector).addClass("active");
			$(selector).stop().animate({backgroundPosition:"(0 0px)"}, {duration:300});
		},
        gotoPage = function (pageNumber) {
            return function () {
                $('#content').stop().scrollTo($('#p' + pageNumber), 1500, 
                    {axis: 'x', offset: {left: 0, top: 0}});		
                clearMenuBackground();
                setMenuBackground("#m" + pageNumber);		
                return false;
            }
        },
        i;

	

    for (i=1; i<=5; i++) {
        $('.g' + i).click(gotoPage(i));
    }
});

