$(document).ready( function() {
  initPoll();
  initPhotoGalleryTooltips();
  if ($('#username').size())
    $('#username').focus();
  else
    $('#searchquery').focus();

  if ($('#my_page_select_news_box').size()) {
	$("#my_page_select_news_box").change( function() {
		loadActionFromSelect(this, 'news_box');
	});
	loadAction($('#my_page_select_news_box option:first').attr('value'), 'news_box');
  }
  
  $('a.deleteLink').click(function(){
	  return confirm("Czy jesteś pewien że chcesz usunąć ten element?");
  });
  
	// banners
	$('.banner').each(function(e){
			var src = $(this).find('span').text();
			var width = $(this).attr('width');
			var height = $(this).attr('height');
			/*
			$(this).flash({
				swf: 	src,
				width: 	width,
				height: height,
			});
			*/
			$(this).html('<object data="'+src+'" width="'+width+'" height="'+height+'" type="application/x-shockwave-flash"><param name="movie" value="button.swf"/><param name="wmode" value="transparent"></object>');

			
			$(this).find('object').addClass("flash_banners");
			//$(this).find('object').live(function() { alert('__'); });

	});

  // kontrolujemy awatary
  $('.wnAvatar').maxSize({
	width: 100,
	height: 100
  });
  
});

function loadAction(url, domElement, hideIndicator) {
  if (!hideIndicator)
    $("#"+domElement).html('<img src="/images/loading-indicator.gif" alt="loading..." />');
  $("#"+domElement).load(url, null, function(){
  });
}

function inviteFriend(friendUserId) {
 $.post("/user/user/invitefriend", { id: friendUserId },
  function(data){
   if (data.result == true)
     alert(data.msg);
   else
     alert('Wystapil blad: '+data.msg);
 }, "json");
}

function changeMainCategory(id) {
  loadAction('/resource/resource/selectmaincategory?category_id='+id, 'naglowektop', true);
}


function addPhotoAddFields() {
  if (typeof addPhotoAddFields.counter == 'undefined')
    addPhotoAddFields.counter = 0;
  ++addPhotoAddFields.counter;
  if (addPhotoAddFields.counter < 15)
    $("#photoadd"+addPhotoAddFields.counter).show();
}

function initPoll() {
  $("#pollvotingform").submit(pollFormProcess);
  animateResults();
}

function pollFormProcess(event) {
  event.preventDefault();
  var pollAnswerId = $("input[@name='poll']:checked").attr("value");
  var pollId = $("input[@name='poll_id']").attr("value");

  $("#pollcontainer").fadeOut("slow",function(){
    $(this).empty();
    $("#pollcontainer").load("/poll/poll/vote", {poll_answer_id: pollAnswerId, poll_id: pollId}, function() {
	    $("#pollcontainer").fadeIn("slow",function(){
	    animateResults();});
    });
  });

}

function animateResults(){
  $("#pollresults div").each(function(){
      var percentage = $(this).next().text();
      $(this).css({width: "0%"}).animate({
				width: percentage}, 'slow');
  });
}

function initPhotoGalleryTooltips() {
	$("a.photodetails").tooltip({
    showURL: false 
});
}

function loadActionFromSelect(selectElement, where) {
  val = $(selectElement).attr('value');
  loadAction(val, where);
}
