/**
 * @package WordPress
 * @subpackage Layerlab
 */

jQuery(document).ready(function($) {

	convertEmails();

	$("A[rel='external']").attr("target", "_blank"); // Sätter target="_blank" för rel="external"
	$("A").focus(function(){ $(this).blur(); }); // Sätter blur() på alla länkar vid focus()

	getQuote();

	//p2pMenu();

});

function p2pMenu(){
	var cat = document.location.pathname.substring(0, document.location.pathname.substring(0, document.location.pathname.length - 1).lastIndexOf("/"));
	console.log(cat);
	jQuery("#mainMenu A[href*="+ cat +"]").each(function(){
		console.log(this);
		this.className += " current_page_item";
		this.parent.each(function(){
			this.className += " current_page_ancestor current_page_parent";

		});
	});
}
function convertEmails(){
	var spans = document.getElementsByTagName("span");
	var numSpans = spans.length;
	for(var i = 0; i < numSpans; i++){
		if(spans[i] && spans[i].className == "epost"){
			var at = new RegExp("\\(snabel\-a\\)", "i");
			var punkt = new RegExp("\\(punkt\\)", "ig");
			var contact = spans[i].firstChild.data.split("|");

			if (contact.length == 2) {
				var address = contact[1].replace(at, "@").replace(punkt, ".");
               	var theLink = document.createElement("A");
               	theLink.setAttribute("href", "mailto:" + address);
               	theLink.className = "email";
               	theLink.appendChild(document.createTextNode(contact[0]));
               	spans[i].replaceChild(theLink, spans[i].firstChild);
			}
			else {
				var address = spans[i].firstChild.data.replace(at, "@").replace(punkt, ".");
				var theLink = document.createElement("A");
				theLink.setAttribute("href", "mailto:" + address);
				theLink.className = "email";
				theLink.appendChild(document.createTextNode(address));
				spans[i].replaceChild(theLink, spans[i].firstChild);
			}
		}
	}
}

function getQuote(){
	jQuery("#share :nth-child(1)").show();
	var s = document.createElement('script');
	s.setAttribute("type","text/javascript");
	s.setAttribute("src", template_directory + "/aktiekurs.php?callback=showQuote");
	document.getElementsByTagName("head")[0].appendChild(s);
}
function showQuote(t){
	setTimeout(function(){
		jQuery("#share").css("background-image", "none");
		jQuery("#share :nth-child(2) *").html(t.Prices.BidPrice);
		jQuery("#share :nth-child(3) *").html(t.Prices.AskPrice);
		jQuery("#share :nth-child(4) *").html(t.Prices.LatestQuote);
		jQuery("#share :nth-child(5)").html("<a href=\"http://www.aktietorget.se/QuotesInstrument.aspx?Language=1&InstrumentID=SE0003172204\" target=\"_blank\" title=\" Till Layerlab på Aktietorget \"><img alt=\" Aktietorget Logo \" src=\""+ template_directory +"/img/inline/at_logo.gif\"></a>");
		jQuery("#share LI").show();
	}, 1000);
}