/**
 * autor Zbigniew Jędraczka mr.zbigniew@gmail.com
 *
 * Skrypt ma za zadanie poruszać planetami
 *
 * wersja: 1.0
 *
 */

var positions = new Array();
var objects = new Array();
var menuItems = 0;
var animation = 1000;
var pX = 0;
var pY = 0;

$(document).ready(function(){
    $.each($('#menu1 span'),function(id,el){
        $(el).css('display','none');
        
    })
});

$('#menu1').ready(function(){
    var menuItems = $('#menu1').children();

    var radian = (Math.PI * 2) / menuItems.size();

    var a = 470;
    var b = 100;

    pX = $('body').outerWidth() / 2;
    pY = 180;

    var maxHeight = 259;
    var maxWidth = 259;

    var minHeight = 150;
    var minWidth = 150;
    var heightR = (maxHeight - minHeight) / (menuItems.size() / 2);
    var widthR = (maxWidth - minWidth) / (menuItems.size() / 2);
    
    for(i = 0; i < menuItems.size(); i++){
        positions[i] = new Object();
        var wT = Math.ceil(100 / ( menuItems.size()-1 ));        
        positions[i].zIndex = 100 - (wT * (i < Math.ceil( menuItems.size()/2) ? i : (menuItems.size() - i) ) );
        positions[i].width = 150 - (wT * (i < Math.ceil( menuItems.size()/2) ? i : (menuItems.size() - i) ) );
        if(i == 0){
            positions[i].width = maxHeight;
            positions[i].y = -130;
            positions[i].x = pX;
            positions[i].zIndex = 200;
        }
        positions[i].x = Math.ceil(pX + (a * Math.cos( (2 * Math.PI / 4) * 3 + i * radian) - positions[i].width / 2) );
        positions[i].y = Math.ceil(pY - (b * Math.sin( (2 * Math.PI / 4) * 3 + i * radian )));

        //console.log(positions[i].width);
        
        objects[i] = new Object();
        objects[i].obj = $(menuItems[i]);
        objects[i].nr = i;
        

        $(menuItems[i]).css('position','absolute');
        $(menuItems[i]).css('zIndex',100);
        $(menuItems[i]).css('top',positions[0].y);
        $(menuItems[i]).css('left',positions[0].x);
        $(menuItems[i]).css('width',positions[0].width);
    }    

    $.each(objects,function(id,el){        
        el.obj.click(function(e){
            e.preventDefault();
            reloadTo(el.nr);
        });
    });

    reload();

    $('#menu1').css('width',$('#content').css('width'));
    $('#menu1').css('height','570px');
    $('#logo').css('zIndex',150);
    $('#logo span').toggle();

    $('#navb').click(function(){
        rb();
    });

    $('#navf').click(function(){
        rf();
    });

});

function rf(){
    $.each(objects,function(id,el){
        if(el.nr == 0){
            el.nr = objects.length - 1;
        }
        else{
            el.nr = el.nr - 1;
        }
    })
    reload();
}

function rb(){
    $.each(objects,function(id,el){
        if(el.nr == objects.length - 1){
            el.nr = 0;
        }
        else{
            el.nr = el.nr + 1;
        }
    })
    reload();
}


function reload(){
    $.each(objects,function(id,el){        
        el.obj.animate({
            'top' : positions[el.nr].y,
            'left' : positions[el.nr].x + (el.id == 0 ? -50 : 0),
            'zIndex' : positions[el.nr].zIndex,
            'width' : positions[el.nr].width
        },animation,function(){
            el.obj.unbind('click');
            el.obj.click(function(e){
                if(el.nr == 0){
                    window.location = el.obj.children().href;
                }
                else{
                    e.preventDefault();
                    reloadTo(el.nr);
                }
            })
        })
    });
    
    if(chkAnim()){
        $('#logo').animate({
            'opacity': 0.8,
            'zIndex':150

        },(animation / 2),function(){
            $('#logo').animate({
                'opacity': 1,
                'zIndex' : 150
            },(animation / 2),function(){})
        })
    }
}

function reloadTo(nr){
    var tmpanimation = animation;
    animation = 300;
    if(nr < objects.length / 2 ){
        for(i = 0; i < nr; i++){
            rf();
        }
    }else{
        for(i=0;i<objects.length - nr; i++){
            rb();
        }
    }
    animation = tmpanimation;
}
/*var idel = 0;
$('document').ready(function(){
    $('#tekst').ready(function(){
        var ele = $('#tekst p');
        $.each(ele,function(id,pele){
            if(id > 0){
                $(pele).toggle();
            }
        });
        idel = 0;
    
    });
    
    $('#tnavb').click(function(e){
        $($('#tekst p')[idel]).toggle(2000);
        if(idel == 0){
            idel = $('#tekst p').size()-1;
        }else{
            idel--;
        }
        $($('#tekst p')[idel]).toggle(2000);
    });

    $('#tnavf').click(function(e){
        $($('#tekst p')[idel]).toggle(2000);
        if(idel == $('#tekst p').size()-1){
            idel = 0;
        }else{
            idel++;
        }
        $($('#tekst p')[idel]).toggle(2000);
    });
});*/

jQuery.fn.topLink = function(settings) {
	settings = jQuery.extend({
		min: 1,
		fadeSpeed: 200,
		ieOffset: 50
		}, settings);
		return this.each(function() {
		//listen for scroll
		var el = $(this);
		el.css('display','none'); //in case the user forgot
		$(window).scroll(function() {
			if(!jQuery.support.hrefNormalized) {
				el.css({
					'position': 'absolute',
					'top': $(window).scrollTop() + $(window).height() - settings.ieOffset
				});
			}
			if($(window).scrollTop() >= settings.min)
			{
				el.fadeIn(settings.fadeSpeed);
			}
			else
			{
				el.fadeOut(settings.fadeSpeed);
			}
		});
	});
};
$(document).ready(function() {
	$('.do_gory').topLink({
		min: 200,
		fadeSpeed: 500
	});
	//smoothscroll
	$('.do_gory').click(function(e) {
		e.preventDefault();
		$.scrollTo(0,500);
	});
});
