var r = 140;
var alphas = [];
var count = [];
var current = 3;
var intervalMove;
var rotation = 1;
var status = false;
var ratio = 0.015;
var info = {};


var res;

String.prototype.format = function(zmienne){
    return this.replace(/{([\d\w]+)}/g, function() {        
        var zmienna=zmienne[arguments[1]];
        if (typeof zmienna=='number') return zmienna;
        return zmienna? zmienna: '';
    });
}

function move(){
    $("#wheel div").each(function(i,item){
    	item2 = $(item);
        alphas[i] += ratio;
		count[i]++;
        item.style.left = r + r * Math.cos(alphas[i]) + 'px';
        item.style.top = r + r * Math.sin(alphas[i]) + 'px';
	
	if(count[i] > Math.PI * 2.5 / ratio){
	    var objects = $("#wheel").children();
	    setInfo(objects[current]);
	    if(current == 0)
		    current = 0;
	    else current--;
	    stop();
	    $("#wheel div").each(function(i2,item){
		alphas[i2] -= Math.PI * 2 * rotation;
		count[i2] = 0;
	    });
	    setTimeout('start()', 3000);
	    return;
	    
	}
    });
}

function start(){
	status = true;
    clearInterval(intervalMove);
    intervalMove = setInterval("move()", 15);
}

function stop(){
    status = false;
    clearInterval(intervalMove);
}

function setInfo(wheel_div){
	var cat = $(wheel_div).attr('cat');
	$("#start-info .content").html('<div class="ajax-loader"><img src="/media/images/ajax-loader.gif" /></div>');
	$("#start-info h1 a").html($(wheel_div).children("a").children("img").attr('title'))
	.attr('href', $(wheel_div).children("a").attr('href'));
	if(info[cat.toLowerCase()] != undefined)
		$("#start-info .content").html(info[cat.toLowerCase()]);
	else {	
		$.ajax({
			url: "pages/pages/oferta" + cat.toLowerCase() + ".html",
			type: 'get',
			success: function(data){
				var dataIndex = data.indexOf("//--></script>");
				if(dataIndex != -1)
					data = data.substr(dataIndex+16);
				$("#start-info .content").html(data);
				info[cat.toLowerCase()] = data;
			}
		});
	}
    $("#start-info *").fadeIn('200');
    	
}
function hide_auto_text() {
	$("#auto-text").fadeOut('slow');
}
function auto() {	
	/* auto */
	$("#auto-img").animate({
			left: (window.innerWidth - 1000) / 2
		}, 2000, function(){
		$("#auto-text").animate({width: '786px'}, 2900, function(){
			setTimeout('hide_auto_text()', 3000);
		});
	});
	$("#auto-img").animate({
		left: window.innerWidth
		}, 6000, function(){
		$(this).fadeOut('fast');
	});
}

$().ready(function(){
				/* oferta-calendar */
				//$("#oferta-calendar").html(oferta_calendar_template);
				$("#oferta-calendar").calendarWidget({file:'/calendar.json'});
				
				$("#page .slider").sudoSlider({
											prevNext: false,
											fade: true,
											auto:true,
											continuous: true,
											pause: '1500'
							});
				$('.tooltip').tipTip();
				if($("#wheel").is("*")){
					var objects = $("#wheel").children();
					setInfo(objects[0]);
					$("#wheel div").each(function(i,item){
												count[i] = 0;
												alphas[i] = i * Math.PI * 0.5;
								});
								start();
								$("#wheel div").mouseenter(function(){
									var wheel_div = $(this);
									var cat =$(this).attr('cat');
												if(status==true) stop();
												setInfo(wheel_div);        
								});
								$("#wheel div").mouseout(function(){
												if(status==false) start();    
								});
				}
				$("#page").animate({opacity:1},'1000');
				
				$("#map-info").hover(function(){
				});
				$(".map-branch").hover(function(){
					$("#map-info").children('h4').html($(this).attr('address'));
					$("#map-info").children('a').attr('href', $(this).attr('href'));
					$("#map-info").css({display:'block'});
					$("#map-info").animate({opacity:0.8}, 'slow');
				});
				$("#top").mouseleave(function(){
					$("#map-info").animate({opacity:0.0}, 'fast', function(){
						$("#map-info").css({display:'none'});
					});
				});
			
				$(".more").click(function(){
								if($(".more-text").is("*"))
												$(".more-text").remove();
				else {
								$(this).after('<div class="more-text"><div class="ajax-loader"><img src="/media/images/ajax-loader.gif" /></div></div>');
								$.ajax({
												url: "/more/more.html",
												type: 'get',
												success: function(data){
																var dataIndex = data.indexOf("//--></script>");
																if(dataIndex != -1)
																data = data.substr(dataIndex+16);
																$(".more-text").html(data);
												}
										});
					}
				});
				
				/* slider2 */
				$('#top-slideshow').crossSlide({
					speed: 30,
					fade: 1
						}, [
					{ src: '/media/slideshows/top/tn_IMAG0061.jpg', dir: 'up'   },
					{ src: '/media/slideshows/top/tn_IMGP0007.JPG',   dir: 'down' },
					{ src: '/media/slideshows/top/tn_IMGP0013.JPG',  dir: 'up'   },
					{ src: '/media/slideshows/top/tn_IMGP0028.JPG',  dir: 'down'   },
					{ src: '/media/slideshows/top/tn_IMGP0029.JPG',  dir: 'up'   },
					{ src: '/media/slideshows/top/tn_IMGP0086.JPG',  dir: 'down'   },
					{ src: '/media/slideshows/top/tn_IMGP0172.JPG',  dir: 'up'   },
					{ src: '/media/slideshows/top/tn_IMGP0240.JPG',  dir: 'down'   },
					{ src: '/media/slideshows/top/tn_IMGP0249.JPG',  dir: 'up'   },
					{ src: '/media/slideshows/top/tn_IMGP0275.JPG',  dir: 'down'   },
					{ src: '/media/slideshows/top/tn_Obraz417.jpg',  dir: 'up'   },
					{ src: '/media/slideshows/top/tn_Obraz432.jpg',  dir: 'down'   },
						]);	
				
				setTimeout('auto()', 3000);
				
				/* kontakt */
				$("#kontakt-form").submit(function(){
								$(this).attr('action', 'http://www.piero.waw.pl' + window.location.pathname);
				});
				
				/* wheel */
				$("#wheel a").click(function(){
								window.location.href = 'filie.html?' + $(this).attr('href');
								return false;
				});
		
		       $("#osrodek").change(function(){
                    var branch = $(this).val();
                    $("#data_rozpoczecia").attr('disabled', 'disabled');
                    $.ajax('/' + branch + '/'+branch+'.calendar.xml', {
                        success: function(response){
                            res = $(response);
                            $("#data_rozpoczecia").html('');
                            $(response).find('entry').each(function(i,e){
                                var entry = $(e).text();
                                var entry_pos = entry.indexOf('Kiedy: ') + 7;
                                var entry_pos2 = entry.indexOf('&nbsp;') - entry_pos;
                                var date = entry.substr(entry_pos, entry_pos2);
                                $("#data_rozpoczecia").append('<option>' + date + '</option>');
                            });
                            $("#data_rozpoczecia").removeAttr('disabled').val('').focus();
                        },
                    });
                });    
                $(".post_code").focus(function(){
                    if($(this).val() == '00-000'){
                        $(this).val('');
                    }
                });
});

