$(document).ready(function() {
  //alert($('.oglb').css('display'));
  if($('.oglb').css('display') == 'none') {
    $('.oglb').prepend('<span id="oglbZamknij"><span id="oglbZamknijBut">zamknij X</span></span>');
    $("input[@name='za_wariant']").click( function() {
      enablePopupBubble();
      enableClearTextarea();
    });
    /* pierwsze odpalenie */
    enablePopupBubble();
    //showTooltip(1);
  }
  enableClearTextarea();
});

/* interfejs :: tooltipy na textarea oraz na helpico */

function enablePopupBubble() {
$('#trescHolder').each(function () {
  var distance = 10;
  var time = 0;
  var hideDelay = 100;

  var hideDelayTimer = null;

  var beingShown = false;
  var shown = false;
  var trigger = $('.edytor', this);
  var info = $('.oglb', this).css('opacity', 1);  

  $('#oglbZamknijBut').click(function () {
   if (hideDelayTimer) clearTimeout(hideDelayTimer);
    hideDelayTimer = setTimeout(function () {
      hideDelayTimer = null;
      info.animate({
        top: '+=' + distance + 'px',
        opacity: 0
      }, time, 'swing', function () {
        shown = false;
        info.css('display', 'none');
      });
    }, hideDelay);    
    return false;    
  });

  $([trigger.get(0)]).click(function () {
    if (hideDelayTimer) clearTimeout(hideDelayTimer);
    if (beingShown || shown) {
                    // don't trigger the animation again
      return;
    } else {
      // reset position of info box
      beingShown = true;
      info.css({
        top: -149,
        left: -1,
        display: 'block'
      }).animate({
        top: '-=' + distance + 'px',
        opacity: 1
      }, time, 'swing', function() {
        beingShown = false;
        shown = true;
      });
    }
    return false;
  }).mouseout(function () {    
   /*
   if (hideDelayTimer) clearTimeout(hideDelayTimer);
    hideDelayTimer = setTimeout(function () {
      hideDelayTimer = null;
      info.animate({
        top: '+=' + distance + 'px',
        opacity: 0
      }, time, 'swing', function () {
        shown = false;
        info.css('display', 'none');
      });
    }, hideDelay);
    */
    return false;
  });
});
}

/*
* help tooltip
*/
$(document).ready(function() {
	//Select all anchor tag with rel set to tooltip
  var helpIsShown = false;
  var tip = '';
	$('.helpIcoLink').mouseover(function(e) {
		//Grab the title attribute's value and assign it to a variable
		tip = $(this).attr('title');
    
		//Remove the title attribute's to avoid the native tooltip from the browser
		
    //alert(tip);
		//Append the tooltip template and its value
		if(!helpIsShown) {
      $(this).attr('title','');
      $(this).append('<div id="tooltipHelp" class="toolPopupClass"><div class="toolPopupTop"></div><div class="toolPopupMid">'+tip+'</div><div class="toolPopupBot"></div></div>');
      //Set the X and Y axis of the tooltip
      //alert($('.toolPopupClass').height());
      var wysokosc = $('.toolPopupClass').height();
      var pos = $(this).offset()
      //alert(pos.left);
      $('#tooltipHelp').css('top', pos.top - wysokosc - 7 );
      $('#tooltipHelp').css('left', pos.left - 206 );
      helpIsShown = true;
    }
		//Show the tooltip with faceIn effect
		//$('#tooltipHelp').fadeIn('500');
		//$('#tooltipHelp').fadeTo('10',0.9);
	}).mousemove(function(e) {

		//Keep changing the X and Y axis for the tooltip, thus, the tooltip move along with the mouse
		//$('#tooltip').css('top', e.pageY + 10 );
		//$('#tooltip').css('left', e.pageX + 20 );

	}).mouseout(function() {

		//Put back the title attribute's value
		$(this).attr('title',tip);
    helpIsShown = false;
		//Remove the appended tooltip template
		$(this).children('div#tooltipHelp').remove();

	});

});

function enableClearTextarea() {
  $('textarea[name=za_edytor]').click(function() {    
    if($('textarea[name=za_edytor]').val() == "wprowadź treść ogłoszenia...") {
      //$('textarea[name=za_edytor]').val() = "";        
        $('textarea[name=za_edytor]').val('');
        $('input[name="za_znakow"]').val('0');
    }
    if($('textarea[name=za_edytor]').val() == ". wprowadź treść ogłoszenia...") {
      $('textarea[name=za_edytor]').val('. ');
      $('input[name="za_znakow"]').val('0');
    }
  });
}


// blokada entera w ogloszeniach 

$(document).ready(function(){    
    $("#za_edyt").keypress(function(e){
    	//alert(e.which);
    	if(e.which == 13) {
    		return false;
    	}
    });
});

$(document).ready(function(){
 $("#sub_pay").click(function() {
 var kod=$("#kod_pay").val();
     if(kod!=''){
         $.ajax({
            type: "POST",
            url: "pay_rabaty.php",
            data: "kod="+kod,
            success: function(msg){
                self.location="cms,57,zamowienia.htm?podsumowanie=1";
            }
          });         
     $("#alert").html("&nbsp;");
     }else{        
         $("#alert").html("&nbsp;<img src='images/valid_no.png' alt='Kod niepoprawny' title='Kod niepoprawny'>");
     }
    });
});
