jQuery.fn.safeMailTo = function() {
	return this.each(function(){
 
		var mailtoHref = $(this).attr('href');
		mailtoHref = mailtoHref.replace(mailtoHref,mailtoHref);
		mailtoHref = mailtoHref.replace("{abols}","@");
	//	mailtoHref = mailtoHref.replace("[dot]",".");
 
		var mailtoText = $(this).text();
		mailtoText = mailtoText.replace("{abols}","@");
	//	mailtoText = mailtoText.replace("[dot]",".");
		$(this).text(mailtoText);
 
		var mailtoTitle = mailtoHref.replace("mailto:","Email: ");
		$(this).attr('title',mailtoTitle);
 
		$(this).click(function(){
			window.location.href = mailtoHref;
			return false;
		});
	});
}

function replaceText(oldText, newText, node){ 
  node = node || document.body; // base node 

  var childs = node.childNodes, i = 0;

  while(node = childs[i]){ 
    if (node.nodeType == 3){ // text node found, do the replacement
      if (node.textContent) {
        node.textContent = node.textContent.replace(oldText, newText);
      } else { // support to IE
        node.nodeValue = node.nodeValue.replace(oldText, newText);
      }
    } else { // not a text mode, look forward
      replaceText(oldText, newText, node); 
    } 
    i++; 
  } 
}



/*====================================
on doc ready
====================================*/

$(document).ready(function() {
           
		//Salabo hover bold efektu (jāuzmanās ar gariem menu elementiem)
       // $(".menu-galvena-izvelne-container ul li").each( function () {
		
                //$(this).width($(this).children().innerWidth());
        // });
       // $(".menu-galvena-izvelne-container ul li a").mouseenter(function() {    
               // $(this).width($(this).parent().innerWidth());
               // $(this).addClass("active");   
				
       // });
       // $(".menu-galvena-izvelne-container ul li a").mouseleave(function() {
               // $(this).removeClass("active");
               // $(this).addClass("no-padding");    
       // }); 

	   $('a[href^=mailto:]').safeMailTo();

	  // $('#content').replaceText("{abols}", "@" );
	
	$("#content:contains({abols})").each(function() { 
    replaceText('{abols}', '@', this); 
		});
		
	$("#post-single-body:contains({abols})").each(function() { 
    replaceText('{abols}', '@', this); 
		});	

		
	
	//mazo bildishu parslegshanai
	      $(".post-single-smimg span").each( function () {
		
                $(this).click(function() {
				var swap = $(this).attr("id");
				
				$("#post-single-img").html($("#hid"+swap).html());

				});
         });





	//paslēpj email formu
	$('body').click(function() {
	
	$('#ajax-email-form').hide();
	$("#ajax-email-input").val('E-pasta adrese')
	});
	
	//on enter email formai
	
	$('#ajax-email-input').keypress(function(e) {
        if(e.which == 13) {
		sendemail()
		return false;
        }
    });

	
	//neslēpj ja kliko uz tā paša elementa
	$('#ajax-email-form').click(function() {
	
	return false;

	});
	
	//aizvāc iepriekšējo tekstu ja tāds tur ir bijis
	$("#ajax-email-input").focus(function(){
	$("#ajax-email-input").val('')
	});  

	
//galerija
//$('a.lightbox').lightBox();	

	$("a[rel=galerijasgrupa]").fancybox({
		'transitionIn'		: 'none',
		'transitionOut'		: 'none',
		'titlePosition' 	: 'over',
		'titleFormat'		: function(title, currentArray, currentIndex, currentOpts) {
			return '<span id="fancybox-title-over">Attēls ' + (currentIndex + 1) + ' no ' + currentArray.length + (title.length ? ' &nbsp; ' + title : '') + '</span>';
		}
	});

	
	/* Using custom settings */
	
// ---<--- draugiem.lv un facebook share
	$('.fb').click(function(e){
		openW('http://www.facebook.com/sharer.php?u='+encodeURIComponent(window.location)+'&t='+encodeURIComponent($('h1.font-georgia').text()));
		e.preventDefault();
		return 0
	});
	$('.dr').click(function(e){
		openW('http://www.draugiem.lv/say/ext/add.php?title='+encodeURIComponent($('h1.font-georgia').text())+'&link='+encodeURIComponent(window.location)+'&titlePrefix='+encodeURIComponent(location.hostname));
		e.preventDefault();
		return 0
	});
// --->--- draugiem.lv un facebook share

	
});        


/*====================================
Parāda email formu
====================================*/
function addemail(){

$('#ajax-email-form').show();

}

function sendemail(){
var emails = $("#ajax-email-input").val()
$.post("/wp-admin/admin-ajax.php", { action:'my_action', jaunumupasts:emails },
   function(data){
   // vēl jāprecizē kapēc wp dod 1/0 data beigās
     if(data.match("^"+'ok')=='ok') {
		 $('#ajax-email-form-err').removeClass('err');
		 $("#ajax-email-form-err").html('E-pasts pievienots!')
		 
	 }
	 else {
		 $('#ajax-email-form-err').addClass('err');
		 $("#ajax-email-form-err").html('Pārbaudi e-pastu')
	 }
   });

}


