function idzdo(adres, czas)
{
    setTimeout("location.href = '" + adres + "'", czas);
}

function limitZnakow(pole, ilosc, komunikat)
{
    if (pole.value.length > ilosc) 
    {
        pole.value = pole.value.substring(0, ilosc);
    }
}

function wyczyscInput(input, tekst)
{
    if (input.value == tekst) 
    {
        input.value = '';
    }
}

var wysokosc_ekranu = 0;
var skalowanie = 0;
var i = 1;


function skaluj(auto)
{
    if (skalowanie == 1) 
    {
        return;
    }
    skalowanie = 1;
    
    var content = $('.content').children().children();
    var nowa_wysokosc = $(window).height();
    var wysokosc_content = wysokosc_ekranu - $('.belka_top').outerHeight() - $('.belka_bottom').outerHeight() - 10; //Minus margin
    if (nowa_wysokosc != wysokosc_ekranu || auto == 1) 
    {
        wysokosc_ekranu = nowa_wysokosc;
        wysokosc_content = wysokosc_ekranu - $('.belka_top').outerHeight() - $('.belka_bottom').outerHeight() - 10; //Minus margin
        var szerokosc_content = $(window).width();
        
        $('.content').css('height', wysokosc_content);
        
        if (wysokosc_content < 570) 
        {
            var proportion = 0.7656500802; //    width/height
            var width = ($(content).offset().left) + 200; // Content / 3,5
            width = (width > 477 ? 477 : width);
            
            var height = (width / proportion);
            if (height > 517) // content - top nav
            {
                while (height > 517) 
                {
                    width -= 5;
                    height -= 5;
                }
            }
            
            $('.zubr img').attr(
            {
                'width': width,
                'height': height
            });
        }
        else 
        {
            $('.zubr img').attr(
            {
                'width': 477,
                'height': 623
            });
        }
        
        $('.zubr').css('top', 53 + Math.abs((wysokosc_content < 570 ? 570 : wysokosc_content) - parseInt($('.zubr img').height())) / 2); // 53 - top nav; 570 min content;
    }
    /* CONTENT */
    
    var margin;
    
    if (!isNaN(wysokosc_content) && wysokosc_content != 'undefined') 
    {
        if (($('#content_4').length == 0 || $('#content_4').is(':hidden')) && $(content).height() > 70) 
        {
            margin = (wysokosc_content - $(content).height()) / 2;
        }
        else 
        {
            margin = (wysokosc_content - 600) / 2;
        }
        
        if ($(content).hasClass('boks_wiek') && margin < 35) 
        {
            $('.zubr').css('top', 87);
            $(content).css('margin-top', 35);
        }
        else if (margin > 0) 
        {
            $(content).css('margin-top', margin);
            
            setTimeout(function()
            {
                checkBoxOffset(content, margin);
            }, 5);
        }
        else if ($('#content_4').length != 0 && !$('#content_4').is(':hidden')) 
        {
            $(content).css('margin-top', -33);
        }
        else 
        {
            $(content).css('margin-top', 0);
        }
    }
    skalowanie = 0;
    
    /* * */
    setTimeout(function()
    {
        skaluj(0);
    }, 1000);
}

function checkBoxOffset(content, margin)
{
    var excepted_offset = margin + $('.belka_top').outerHeight();
    
    if ($(content).offset().top > excepted_offset) 
    {
    
        while ($(content).offset().top > excepted_offset) 
        {
            $(content).css('margin-top', (--margin) + 'px');
        }
    }
}

var pozycja_suwaka = 0;
var t = null,t2 = null;
var userAction = false;
function content(id)
{
    try 
    {
        document.getElementById('soundplayer').volumeOn();
    } 
    catch (e) 
    {
    }
    
    natura_slajd = 0;
    naturaSlajd(1);
    for (var i = 1; i <= 5; i++) 
    {
        $('#content_' + i).hide();
    }
    
    $('.menu ul li a').each(function(item)
    {
    
        $(this).removeClass('active');
        if (item == (id - 1)) 
        {
            $(this).addClass('active');
        }
    });
    
    $('#content_' + id).show();
    skaluj(1);
}

var natura_slajd = 1;
function naturaSlajd(skladnik)
{
	
	$('.natura_slajdy > div').hide();
    
    natura_slajd = natura_slajd + skladnik;
    
    natura_slajd = natura_slajd > 4 ? 4 : (natura_slajd < 1 ? 1 : natura_slajd);
    
    $('#slajd_' + natura_slajd).show();
    
    $('.boks_natura_ciemnozlotego').attr('class','boks_natura_ciemnozlotego slide_'+natura_slajd+'_inside');
    
    
    if (natura_slajd == 4) 
    {
        $('.natura_strzalki a:eq(0)').css('background-position', '-100px 0');
        $('.natura_strzalki a:eq(1)').css('background-position', '-45px 0');
    }
    else if (natura_slajd == 1) 
    {
        $('.natura_strzalki a:eq(0)').css('background-position', '0 0');
        $('.natura_strzalki a:eq(1)').css('background-position', '-144px 0');
    }
    else 
    {
        $('.natura_strzalki a:eq(0)').css('background-position', '-100px 0');
        $('.natura_strzalki a:eq(1)').css('background-position', '-144px 0');
    }
}

function incrementView()
{
    document.getElementById('soundplayer').volumeOff();
    $.post('/urodziny,incrementPlays', 
    {
        'uri': location.href
    }, null, 'json');
}

$(document).ready(function()
{
    skaluj(0);
    startForm();
     
    $('.boks_wiek input, #day_field input, #month_field input, #year_field input').keyup(function()
    {
        if ($(this).val().length == $(this).attr('maxlength') && !isNaN($(this).val())) 
        {
            if (!$(this).next()[0]) 
            {
                return $(this).parent().next().children('input:first').focus();
            }
            $(this).next().focus();
        }
    });
    
    $('#upload_field p').file().choose(function(e, input) {
    	$("#upload_field input").remove();
    	$("#upload_field p").append(input);
    	$("#upload_field input").hide();
    	$("#upload_field input").attr('name', 'file');
        
	    var name = $(input).val();
	      
	      if (name.lastIndexOf('\\') > 0) 
	      {
	          name = name.substr(name.lastIndexOf('\\') + 1)
	      }
	      
	      $("#upload_field p span").html(name);
	      $("#upload_field .jQueryTextShadow").remove();
    	});

    $("#awards .icons a").click(function()
    {
        $("#awards .items > div").hide();
        
        if ($(this).hasClass('chair')) 
        {
            $("#awards #awards_chair").show();
        }
        else if ($(this).hasClass('watch')) 
        {
            $("#awards #awards_watch").show();
        }
        else if ($(this).hasClass('gps')) 
        {
            $("#awards #awards_gps").show();
        }
        else if ($(this).hasClass('camera')) 
        {
            $("#awards #awards_camera").show();
        }
        
    });
	
	$("#get_mp3").click(function()
	{
		_gaq.push(['_trackEvent', 'Urodziny', 'Pobranie prezentu', '/pobierz_prezent']);
	});
});



var customTextarea = new function(){
    	
    	
    	var text = null;    	
    	var instance = this;
    	
    	var params = {
    		rowHeight: 16,
    		rowsVisible: 5,
    		slider: {
    			max: 63,
    			step: 1
    		}
    		
    	}
    	
    	this.init = function(){
    		text = $("#description_input");
    		var rows = 0;
    		text.keyup(function(e){
    			rows = calculateRows(this,e.keyCode);
    			if(rows > params.rowsVisible) {
    				$("#textarea_slider .ui-slider-handle, #textarea_slider, #textarea_slider_wrapp").show();
    				$("#textarea_slider").slider( "option", "value", 1 );
    				text.css({
    					position: 'absolute',
    					left: 0,
    					top: -1*params.rowHeight*(rows-params.rowsVisible),
    					height: '1000px'
    				});
    			}else{
    				$("#textarea_slider .ui-slider-handle, #textarea_slider, #textarea_slider_wrapp").hide();
    				$("#textarea_slider").slider( "option", "value", params.slider.max );
					text.css({
    					position: 'absolute',
    					left: 0,
    					top: 0,
    					height: '1000px'
    				});
    			}
    		});
    		$("#textarea_slider").slider({
		        orientation: 'vertical',
				step: params.slider.step,
		        value: params.slider.max,
				max: params.slider.max,
				min: 1,
		        slide: function(event, ui)
		        {					
					if(rows > params.rowsVisible) {
						$("#textarea_slider .ui-slider-handle, #textarea_slider, #textarea_slider_wrapp").show();
						text.css({
	    					position: 'absolute',
	    					left: 0,
	    					top: (((ui.value/params.slider.max)*(rows-params.rowsVisible))-(rows-params.rowsVisible))*params.rowHeight,
	    					height: '1000px'
	    				});
					}
		        },
		        change: function(event, ui)
		        {
					//return textareaSlide(ui.value);
		        },
		        stop: function(event, ui)
		        {
					//return textareaSlide(ui.value);
		        },
		        create: function()
		        {
		        	$("#textarea_slider .ui-slider-handle, #textarea_slider, #textarea_slider_wrapp").hide();
		        }
		    });
    		
    	} 
    	
    	var calculateRows = function(elm,keyCode){
    		
    		var elm = $(elm);
    		var html = elm.val();
    		var chars = html.length;    				
    		var lines = html.split('\n');
    		var count = lines.length;
    		for(var i in lines){
    			l = lines[i];
    			var add = ((l.length%50 > 0)? 1 : 0);
    			var c = Math.round((l.length-25)/50)+add-1;
    			if(c > 0) count = count + c;    			
    		}
    		return count;
    	}
    	
    	$(document).ready(function(){
    		instance.init();
    	});
    	
}



if(typeof API == 'undefined')
	API = {};

API.slider = new function() {

	var stack = Array();
	this.create = function(params) {
		var index = stack.push(new slider(params));
		return stack[index - 1];
	}
	this.getStack = function() {
		return stack;
	}
	
	var slider = function(p){    	
    	
    	var text = null;    	
    	var instance = this;
    	
    	var params = {
    		rowHeight: 16,
    		rowsVisible: 5,
    		slider: {
    			max: 100,
    			step: 0.1
    		} ,
    		start: 1,
    		animation: {
    			time: 2000
    		},
    		autochange: 15000	
    	}
    	jQuery.extend(true, params, p);
    	var slider = null,s1 = null,s2 = null,b1 = null,b2 = null,usertouch = false,t = {},lock = {up: false, down: false};
    	this.init = function(){ 
    		s1 = $('.slider_slogan_1');
    		s2 = $('.slider_slogan_2');
    		
    		b1 = $('.bg_1');
    		b2 = $('.bg_2');
    		$('.ui-slider-handle').append(s1);
    		slider = $("#slider");
    		slider.slider({
		        orientation: 'horizontal',
				step: params.slider.step,
		        value: params.slider.start,
				max: params.slider.max,
				min: 1,
		        slide: function(event, ui)
		        {					
					instance.backgrounds(ui.value);
					instance.texts(ui.value);
					if(ui.value > 1){
						instance.autochange(false);
					}
		        },
		        stop: function(event, ui)
		        {
					instance.save(ui.value);
		        }
		    });
		    var pos = instance.get();
		    
		    usertouch = (pos > 1);
		    pos && slider.slider( "option", "value", pos );		    
		    instance.backgrounds(pos);
			instance.texts(pos);
			instance.autochange(true);
    	}
		        	
    	this.backgrounds = function(value){
		    var opacity_value = new Array(2);
    		opacity_value['opacity'] = parseFloat((value / 50 * 50) / 100);
			opacity_value['filter'] = value * value / 50;			
			b1.css(
			{
			    opacity: parseFloat(1 - opacity_value['opacity'])/*,
			    filter: 'alpha(opacity=' + parseFloat(100 - opacity_value['filter']) + ')'*/
			});
			b2.css(
			{
			    opacity: parseFloat(opacity_value['opacity'])/*,
			    filter: 'alpha(opacity=' + parseFloat(opacity_value['filter']) + ')'*/
			});
    	}
    	this.texts = function(pos,stop){
    		
    		if(stop){
    			s1.stop(true,true);
    			s2.stop(true,true);
    		}
    		if(pos > 58){
	    		s1.hide();
	    		s2.show();
	    		lock.up = true;
	    		lock.down = false;
	    	}else{
	    		s2.hide();
	    		s1.show();
	    		lock.up = false;
	    		lock.down = true;
	    	}
    		/*try{
    			t.text && clearTimeout(t.text);
    		}catch(e){}
    		t.text = setTimeout(function(){
    			instance.texts(pos,true);			
    		},700);*/
    	}
    	this.save = function(value){
			try{
				t.save && clearTimeout(t.save);
			}catch(e){}
			t.save = setTimeout(function(){		
				$.cookie('_zubr_track', value);
			},200);
    	}
    	this.get = function(){
    		return ($.cookie('_zubr_track') || 0);
    	}
    	this.autochange = function(run){
    		var pos = instance.get();

    		try{
    			clearTimeout(t.autochange);	
    		}catch(e){}
    		if(run && !usertouch){
    			t.autochange = setTimeout(function(){
    				instance.animate();
    			},params.autochange);
    		}
    	}
    	this.animate = function(){
    		
    		var pos = 1;
    		var time = params.animation.time;
    		var steps = ((params.slider.max/params.slider.step) - 1);
    		var stime = time/steps;
			
    		var opacity_value = new Array(2);
			var value = params.slider.max;
    		opacity_value['opacity'] = parseFloat((value / 50 * 50) / 100);
			opacity_value['filter'] = value * value / 50;
    		$('.ui-slider-handle').animate({
    			left: '621'
    		},time,function(){
    			instance.save(value);
    		});
    		b1.animate(
			{
			    opacity: parseFloat(1 - opacity_value['opacity'])/*,
			    filter: 'alpha(opacity=' + parseFloat(100 - opacity_value['filter']) + ')'*/
			},time);
			b2.animate(
			{
			    opacity: parseFloat(opacity_value['opacity'])/*,
			    filter: 'alpha(opacity=' + parseFloat(opacity_value['filter']) + ')'*/
			},time);
			setTimeout(function(){
				instance.texts(params.slider.max);
			},time*0.45);	
			
    	}
    	$(document).ready(this.init);
    }
}
API.slider.create();

