$(document).ready(function() {
    $('.top_logo').css('cursor','pointer');
    $('.top_logo').parent().css('background','transparent');
    $('.top_logo').click(function(){
	document.location = $('#box_menu').find('img[alt=aktualnosci]').parent().attr('href');
    });

	Cufon.DOM.ready(function() {
		Cufon.replace('a.techniczna',{hover: true});
		Cufon.replace('.techniczna');
		align_column(); //wyrównianie wysokości column
		setTimeout(align_column,1000);
		setTimeout(align_column,5000);
	});
	//$('.left_column_cont, .right_column_cont').resize(align_column);
	init_lightbox();
	$('.rep').each(function(i,v){
	    var rep = $(this);
	    rep.click(function(){
		$('.rep').removeClass('activ_theatre');
		rep.addClass('activ_theatre');
		var id = rep.attr('id');
		$('.one_day').hide();
		$('.'+id).show();
		Cufon.refresh();
	    })
	    if(i==0){
		rep.addClass('activ_theatre');
		var id2 = rep.attr('id');
		$('.one_day').hide();
		$('.'+id2).show();
	    }
	});
	init_calendar();
	init_repertuar();
 });
 $(window).resize(function(){
	 align_column();
 });
function init_repertuar(){
    $('.spectacle_link').each(function(){
	var sl = $(this);
	var desc = sl.find('.desc').html();
	var ah;
	var nh;
	sl.mouseenter(function(){
	    $('.repertoire_desc h3').show();
	    $('#rep_desc').html(desc);
	    align_column();
	});
	
	sl.mouseleave(function(){
	    $('.repertoire_desc h3').hide();
	    $('#rep_desc').html('');
	    align_column();
	});
	
    });
}
function init_calendar(){
    var dymek = $('.data_dymek');
    var dcont = dymek.find('.dymek_cont');
    $('.data_small:not(:empty)').each(function(){
	var pol = $(this);
	if(pol.find('.c').html()!=''){
	    pol.css('font-weight','bold');
	}
	//pol.css('cursor','default');
	pol.mouseenter(function(){
	    if(pol.find('.c').html()!=''){
		dymek.show();
		dcont.html(pol.find('.c').html());
		dymek.css('top',(pol.offset().top-dymek.height()-10)+'px');
		dymek.css('left',(pol.offset().left+10)+'px');
	    }else{
		dymek.hide();
	    }
	});
    });
    $('.calendar').mouseleave(function(){
	dymek.hide();
    });
}
 function align_column(){
	//setTimeout(function(){$('body').append($('.left_column_cont').height()+', '+$('.right_column_cont').height())},2000);
	//$('.box_footer').height('auto');
	$('#box_menu').height('auto');

	var nh = $(window).height()-100;
	if(nh<$(document).height()-100){
		nh = $(document).height()-100;
	}

	$('#box_menu').height(nh);
	var bf = nh-$('#main_cont').outerHeight()-12;
	//$('.box_footer').height(bf);
 }

function select_thumb(c,nr){
	var cont = c.find('.img_cont');
	var img = cont.find('img').eq(nr);
	var img_pos = img.offset().left-cont.offset().left;
	var big_img = c.find('#big_img');
	var main_params = big_img.attr('src').split('-').pop().split('.').shift();
	var img_src = img.attr('src').split('.');
	var ext = img_src.pop();
	var img_rest = img_src.join('.').split('-');
	var img_params = img_rest.pop();
	var name = img_rest.join('-');
	var new_url = name+'-'+main_params+'.'+ext;
	big_img.attr('src',new_url);

	var new_pos = Math.round((cont.parent().width()-cont.find('img').eq(0).width())/2-img_pos);
	cont.animate({
		marginLeft: new_pos+'px'
	},200);
}

//.light_box_video

 function init_lightbox(){
     $('.link_video').each(function(){
	var lv = $(this);
	var player_code = lv.find('.hc').html();
	if(player_code!=''&&player_code!=null){
	    lv.click(function(){
		var lbv = $('.light_box_video');
		lbv.fadeIn('fast');
		$('#cover').fadeTo(200,0.7);
		lbv.html(player_code);
		lbv.css('marginLeft',-lbv.width()/2+'px');
		lbv.css('marginTop',-lbv.height()/2+'px');
	    })
	}
     });
    if($('.img_cont').find('img').length>0){
	$('#cover').click(function(){
		$(this).fadeOut('fast');
		$('.light_box').fadeOut("fast");
		$('.light_box_video').fadeOut("fast",function(){$('.light_box_video').html('');});
	});

	$('.link_lightbox').each(function(){
		var link = $(this);
		var lid = link.attr('id').substr(2);
//		alert(lid);
		var gid = $('#g_'+lid);
		link.click(function(){
			$('#cover').fadeTo(200,0.7);
			//gid.prev().show().animate({opacity: 0.7}, "fast");
			gid.fadeIn("slow",function(){
				select_thumb(gid,0);
			});
		});
		gid.find('.img_cont').find('img').each(function(i,v){
			var im = $(this);
			im.click(function(){
				select_thumb(gid,i);
			});
		});
	});
    }
}

