
var openedPolls = [];

$(document).ready( function() {
	 
	 
	 
	 initPage();

	// confirmator
	
	$('a[data-nette-confirm], button[data-nette-confirm], input[data-nette-confirm]').live('click', function (e) {
		 var el = $(this);
		 if (!confirm(el.attr('data-nette-confirm'))) {
			 e.preventDefault();
			 e.stopImmediatePropagation();
			 return false;
		 }

	 });


	// animation
	
	$('a[data-nette-animation-fadeout]').live('click', function (e) {
		
		var el = $(this);
		var cont = el.attr('data-nette-animation-fadeout');
		
		$('#' + cont).fadeOut(1000);

	 });

	$('a[data-nette-animation-explode]').live('click', function (e) {
		
		var el = $(this);
		var cont = el.attr('data-nette-animation-explode');
		
		$('#' + cont).effect('explode', {}, 1000);

		event.preventDefault();

	 });

	 
	$("a.ajax").live('click', function (event) {
		event.preventDefault();
		$.get(this.href);
	});

    // odeslání na formulářích
    $("form.ajax").live('submit', function (event) {
		$.ajaxSetup({
			success: $.nette.success,
			dataType: 'json'
		});		
        $(this).ajaxSubmit();
		
		event.preventDefault();
    });
	
	$.nette.createSpinner();


	// shower
	
	$("a[data-js-show]").live('click', function(event){
		
		$target = $(this).attr('data-js-show');
		$('#' + $target).slideToggle();
		$(this).hide();
		event.preventDefault();
	});


	$("#userMenuTrigger").live('click', function(){
		$("#userMenu").toggle();
	});
	
	

	 
 });

 function initPage(){
	 
	$('textarea.autogrow').autoGrow();
	 

	 
	if ($("#login ul[class=error]").length == 0) $("#login").hide();
	 
	 
	$("#signIn").click(function(){
		$("#login").slideToggle();
	});
	
	
	$(".clickToShow").hide().after('<p class="clickToShow-link"><a href="#">Zobrazit vše</a></p>');
	
	$(".clickToShow-link > a").live('click', function(){
		$(this).parent().hide();
		$(this).parents().find(".clickToShow").slideDown();
		return false;
	});
	
	// closable

	$(".closable .close").live('click', function(){
		$(this).closest('.closable').fadeOut().remove();
		$('.overlay').remove();
		return false;
	});
	
	// editable
	
	$("a.editable").hide();
	
	var sliding = false;
	
	$("div.editable").mouseenter(function(e){
		if (sliding == false)
		{
			sliding = true;
			$(this).find("a.editable").slideDown('fast', function(){});
		}
	}).mouseleave(function(e){
		$(this).find("a.editable").slideUp('fast', function(){ sliding = false });	
	});

	
	// voter
	
	
	
	$("#voter a").live('mouseenter', function()
	{
		$("#voterText").text($(this).attr('title'));	
		var parts = $(this).attr('class').split('-');
		var num = parseInt(parts[2]);
		for (var i = 1; i <= num; i++ )
		{
			$("#voter a.voter-element-" + i).css({
				'background-position' : '-20px 0px'
			
			});
					
		}
		
	});
	
	$("#voter a").live('mouseleave', function(){
		$("#voter a").css({
			'background-position' : '0px 0px'
		});	
		$("#voterText").text('');
	});		


	
	$(".offerDialog > p").live('click', function(){
			$("div.offerable").fadeIn();
			$(".offerDialog").hide();
			
	});
	$(".offerDialog > p").live('mouseover', function(){
			$(this).css('text-decoration', 'underline');
			
	}).live('mouseout', function(){
			$(this).css('text-decoration', 'none');
			
	});
	
	// flash messages
	
	$("div.flash").each(function(){
		var speed = 50, pause = 100;
		$(this).delay(1000).animate({opacity: 0.3}, pause).animate({opacity: 1}, speed).animate({opacity: 0.3}, pause).animate({opacity: 1}, speed).animate({opacity: 0.3}, pause).animate({opacity: 1}, speed);
	});	
	
	// flash messages
	
	$(".yft").each(function(){
		$(this).effect("highlight", {}, 5000);
	});	
	
	
	/*
	// desc
	$dateDesc = $('#dateDesc');
	var moreLink = $("a.moreLink");
	moreLink.hide();
	if ($dateDesc.height() > 450)
	{
		$height = $dateDesc.height() + $dateDesc.height() * 0.03;
		$dateDesc.append('<div class="bottomFade">').css({
				'height':'400px',
				'overflow':'hidden'
			});
		moreLink.click(function(){
			$dateDesc.animate({
					'height': $height		
				}, 
				{ 
					duration: 1000, 
					specialEasing: {
						width: 'linear',
						height: 'easeOutBounce'
					}
				});
			$(".bottomFade").remove();
			moreLink.hide();
			return false;
		});
		moreLink.show();
	}
	*/


	// user setings
	
	$("div.attendeeMenuButton").live('click', function(){
		
		$menu = $(this).next('div.attendeeMenu');
		
		$menu.slideDown();
	});
	$('div.attendeeMenu').live('mouseleave', function(){
		//console.log('ha');
		$menu.delay(1000).slideUp();
	});

	// antispam
	
	$("input.spam").val('human').closest('tr').hide();
	
	// hidden
	
	$(".hidden").hide();
	
	
	
	// show share attending
	
	$(".showShareAttending").click(function(){
		$("#showShareAttending1").slideDown();
	});

	// tipTip

	$('input[type=text], textarea').tipTip({ 
		maxWidth: 300, 
		edgeOffset: 5, 
		defaultPosition: 'right', 
		delay: 50, 
		activation: 'focus'
	});
	
	$('a').tipTip({ 
		maxWidth: 300, 
		edgeOffset: 5, 
		defaultPosition: 'bottom', 
		delay: 50, 
		activation: 'hover'
	});
	
	
	// facebook
	
	if (FB) {
	
		FB.init({ 
			appId: Facebook.appId, 
			status: true, 
			cookie: true, 
			xfbml: true,
			oauth: true
		});
	}
/*
	$("#fb_button").live('click', function(){
		FB.login(function(response) {
		  if (response.session) {
			window.location = Facebook.loginUrl;
		  } else {
			// user cancelled login
		  }
		}, { perms:'email'});
		
	});
	$("#fb_button_connect").live('click', function(){
		if (typeof(FB) == 'undefined')
		{
			alert('Sorry, but some of your browser plugins blocks the Facebook login. Maybe AdBlock?');
		}
		else{
			FB.login(function(response) {
			  if (response.session) {
				window.location = Facebook.connectUrl;
			  } else {
				// user cancelled login
			  }
			}, { perms:'email,publish_actions'});
		}
		
	});
	*/
	// polls
	
	$("div.poll").each(function(i){
		var poll = $(this);
		var id = poll.children("#pollId").val();
		var rows = poll.children("div.pollRows");
		
		if (openedPolls[id] == 1){
			rows.hide();
			poll.find("a.unfold").removeClass('hidden');
			poll.find("a.fold").addClass('hidden');
		}
		else if (openedPolls[id] == 2){
			rows.show();
			poll.find("a.unfold").addClass('hidden');
			poll.find("a.fold").removeClass('hidden');
		}
		else{
			poll.children("div.folded1").hide();
			poll.find("a.folded1").hide();
		}
		
		

		poll.find("div a.fold").click(function(e){
			
			
			rows.slideUp();
			$(this).addClass('hidden').hide();
			poll.find("a.unfold").removeClass('hidden').show();
			openedPolls[id] = 1;
			e.preventDefault();
		});
		
		poll.find("a.unfold").click(function(e){
			rows.slideDown();
			$(this).addClass('hidden').hide();

			poll.find("a.fold").removeClass('hidden').show();
			openedPolls[id] = 2;
			e.preventDefault();
		});
	});
	
	// openable closable blocks
	
	// hidden
	
	$(".hidden").hide();
	
	// shower
	
	$('a[data-open]').click( function(event){
		
		$target = $(this).attr('data-open');
		$('#' + $target).slideToggle();
        if ($(this).hasClass('up'))
        {
            $(this).removeClass('up');
            $(this).addClass('down');
        }
        else
        {
            $(this).removeClass('down');
            $(this).addClass('up');
        }
		event.preventDefault();
	});	
	
	// center dialogs
	
	$("div.dialog").center();
	
	

}


function scroll(anchor){
    $('html, body').animate({
        scrollTop: $(anchor).offset().top
    }, 1000);
}

var delay = (function(){
  var timer = 0;
  return function(callback, ms){
	clearTimeout (timer);
	timer = setTimeout(callback, ms);
  };
})();

new function($) 
{ 
	$.fn.setCursorPosition = function(pos) 
	{ 
		if ($(this).get(0).setSelectionRange) 
		{ 	
			$(this).get(0).setSelectionRange(pos, pos); 	
		} 
		else if ($(this).get(0).createTextRange) 
		{ 
			var range = $(this).get(0).createTextRange(); 
			range.collapse(true); range.moveEnd("character", pos); 
			range.moveStart("character", pos); range.select(); 
		} 
	} 
	$.fn.center = function () {
	    this.css("position","absolute");
	    this.css("top", ( $(window).height() - this.height() ) / 2 + "px");
	    this.css("left", ( $(window).width() - this.width() ) / 2 + "px");
	    //console.log(( $(window).height() - this.height() ) / 2);
	    return this;
	}
	
} (jQuery);


        
        


