$(document).ready( function() {
  newsInitLatestList();
  userPhotoInitTooltips();
  userFriendsInitTooltips();
  questionUserCommentsInitTooltips();
  initDefaultInputTexts();
  questionShowInit();
  initTagCloud();
  toggleAdvSearch();
	skiningSelect();
	fixZindex();
});

function newsInitLatestList() {
  $('.aktualnosci_avatar:first').show();
  
  $('.aktualnosci_link').mouseenter(function() {
	imageId = this.id.substr(17, 2);
	$('.aktualnosci_avatar').hide();
	$('#aktualnosci_avatar_'+imageId).show();
  })
}

function userPhotoInitTooltips() {
  $("a.album_miniatura").tooltip({ 
    bodyHandler: function() {
        return $($(this).attr("tooltipId")).html(); 
    }, 
    showURL: false 
  });
}

function questionShowInit() {
  $(document).ready( function() {
	$('textarea.expandable').focus(function() {
	  $(this).animate({ height:"100px" } , 200);
	});
	$('textarea.expandable').blur(function() {
		$(this).animate({ height:"43px" } , 200);
	})
  });
}

function userFriendsInitTooltips() {
  $("a.show_user_tooltip").tooltip({ 
    bodyHandler: function() {
        return $($(this).attr("tooltipId")).html(); 
    }, 
    showURL: false 
  });
}

function questionUserCommentsInitTooltips() {
  $("a.question_comments_users").tooltip({ 
    bodyHandler: function() {
        return $($(this).attr("tooltipId")).html(); 
    }, 
    showURL: false 
  });
}

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

function flagResource(resourceId) {
	$("#flag-msg-"+resourceId).html('');
	$("#flag-msg-"+resourceId).css('display', 'block');
	$("#flag-msg-"+resourceId).html('<img src="/images/loading-indicator.gif" alt="loading..." />');
    $("#flag-msg-"+resourceId).load('/flag/flag/id/'+resourceId+'/flag/'+$('#flag-value-'+resourceId).val(), null, function() {
    	$("#flag-msg-"+resourceId).fadeOut(5000);
    });
}

function initDefaultInputTexts() {
  commentsDefaultText = "Skomentuj tą wypowiedź ...";
  answerDefaultText = "Odpowiedz na pytanie ...";
  
  if ($('.search_pole').val() == '') {
	  initDefaultInputText(".search_pole", "Wpisz to czego szukasz ...");
  }
  initDefaultInputText("#homepage_username", "login");
  initDefaultInputText("#homepage_password", "hasło");
  initDefaultTextareaText(".dodaj_odpowiedz_right", answerDefaultText);
  initDefaultTextareaText(".dodaj_komentarz_pole", commentsDefaultText);
  
  $('form.dodaj_komentarz').submit(function() {
	enteredText = $( '#' + ($(this).attr('id')) + ' textarea:first' ).val();
	if (enteredText == commentsDefaultText) {
	  alert('Wpisz treść komentarza.');
	  return false;
	}
  });

  $('form.answer_add_form').submit(function() {
	enteredText = $( '#' + ($(this).attr('id')) + ' textarea:first' ).val();
	if (enteredText == answerDefaultText) {
	  alert('Wpisz treść odpowiedzi.');
	  return false;
	}
  });
}

function initDefaultInputText(selector, defaultText) {
	$(selector).val(defaultText);
	
    $(selector).focus( function(){
      fraza = $(selector).val();
      if (fraza == defaultText){
        $(selector).val("");
       }
    });
    $(selector).blur( function(){
      fraza = $(selector).val();
      if (fraza == ""){
        $(selector).val(defaultText);
      }
    });
}

function initDefaultTextareaText(selector, defaultText) {
	$(selector).html(defaultText);
	
    $(selector).focus( function(){
      fraza = $(selector).html();
      if (fraza == defaultText){
        $(selector).html("");
       }
    });
    $(selector).blur( function(){
      fraza = $(selector).html();
      if (fraza == ""){
        $(selector).html(defaultText);
      }
    });
}

function initTagCloud() {
  $("#tagcloud").tagcloud({type:"list",sizemin:10, colormin:"88a5c9", colormax:"2770C9"}).find("li").tsort();
}


var reload_product_id = 0;
var reload_product_page = 0;
var reload_product_old_id = 0;
var reload_product_old_page = 0;
var first_use = 0;

function ajax_reload_produkt(id, page) {
	if (first_use == 0) {
		reload_product_old_id = id;
		reload_product_old_page = page;
	} else {
		reload_product_old_id = reload_product_id;
		reload_product_old_page = reload_product_page;
	}
	reload_product_id = id;
	reload_product_page = page;
}

function ajax_reload_produkt_timeout() {
	if (reload_product_id != reload_product_old_id || first_use == 0) {
		first_use = 1;
		reload_product_old_id = reload_product_id;
		reload_product_old_page = reload_product_page;
		
		if (!reload_product_id)
			return; 
		
		url = '/produkt/'+reload_product_id+'/opinie';
		if (reload_product_page)
			url += '/'+reload_product_page;
		$("#product_opinions").html('<img src="/images/loading-indicator.gif" alt="loading..." />');
		$("#product_opinions").load(url, null, function() {
			Shadowbox.setup("a.shadowbox");
			skiningSelect();
			fixZindex();
		}); 
	}
}

/*function ajax_reload_produkt(id, page) {	
  if (!id)
	return; 
	
  url = '/produkt/'+id+'/opinie';
  if (page)
    url += '/'+page;
  $("#product_opinions").html('<img src="/images/loading-indicator.gif" alt="loading..." />');
  $("#product_opinions").load(url, null, function() {
	  Shadowbox.setup("a.shadowbox");
	  skiningSelect();
	  fixZindex();
  }); 
}*/

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

function ajaxifyProductOpinionsPager(product_id) {
  $('.pager_link').click(function() {
    page_number = $(this).html();
	ajax_reload_produkt(product_id, page_number); 
	return false; 
  });
}

function toggleOpinion(id) {
  element = $('#product_opinion_comments_'+id);
  
  if (element.css('display') == 'none')
    element.show();
  else
	element.hide();
}

function toggleNewsOpinion(id) {
  element = $('#news_opinion_comments_'+id);
  
  if (element.css('display') == 'none')
    element.show();
  else
	element.hide();
}

function togglePhotoOpinion(id) {
	  element = $('#photo_opinion_comments_'+id);
	  
	  if (element.css('display') == 'none')
	    element.show();
	  else
		element.hide();
	}

/************* ADV SEARCH ****************/
function toggleAdvSearch() {
	$('#adv-search-btn').click(function(){
		if($('#advance').val() == 0) {
			$('#advance').val('1');
		} else {
			$('#advance').val('0');
		}
		$('#adv-search').slideToggle("slow");
	});
	
	$('#adv-search .close').click(function(e){
		$('#advance').val('0');
		$('#adv-search').slideUp("slow");
	});
}
/************ SKINING FORM *************/
function skiningSelect() {
	$('.flag').jqTransform();
	var el = $('.flag').find('li:nth-child(1)');
	el.css('display','none');
	el.parent().css({
		'height':64
	});
}
/*function flagMsg(){
	$('.flag').find('li>a').click(function(){
		var parent = $(this).parent().parent().parent().parent();
		$('#'+parent.attr('id')+' .flag-msg').fadeIn(500).text('Zgłoszenie zostało wysłane');
		$('#'+parent.attr('id')+' .flag-msg').fadeOut(5000); 
	});
}*/
/* FIX Z-INDEX for FLAG */
function fixZindex() {
	$('.flag').hover(function(){
		$(this).parents('ul:eq(0)').css('z-index', 11);
		$(this).parents('div:eq(0)').css('z-index', 11);		
	}, function(){
		$(this).parents('ul:eq(0)').css('z-index', 1);
		$(this).parents('div:eq(0)').css('z-index', 1);
	});
}
/***************** HOVER for BUTTONS ******************/
$(function(){
	$('.dodaj_komentarz_submit').hover(function(){
		$(this).css('background-color','#2F0C2A');
	}, function(){
		$(this).css('background-color','#190616');
	});
});
/************* UKRYWANIE BLOCKU Z KOMENTARZAMI ************/
$(function(){
	$('.hidden').css('display','none');
});