/**
 * @package WordPress
 * @subpackage Miris AB
 */

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

	convertEmails();
	resizeFooter(60, $);

	$("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()
	$("A[href$='.pdf']").addClass("pdf"); // Sätter class=pdf för alla länkar som är PDF-filer.

	$(document).ready(function() {
        $(".rc").each(function(){
            var div = "<div class=\"rc\" style=\"background-image:"+
                      "url('"+ this.src + "'); width: "+ this.width +"px; "+
                      "height: "+ this.height +"px;\">";
            $(this).replaceWith(div);
        });
    });

	getQuote();

	$(document).everyTime(7000, 'loop', function(){
		if(jQuery(".tmb.active + *").length){ jQuery(".tmb.active + *").click(); }
		else { jQuery(".tmb:first").click(); }
	});

	jQuery(".tmb").click(function(){
		jQuery(".tmb").removeClass("active");
		jQuery("#tmb_current").animate({ left: parseInt($(this).css("left")) - 7 + "px"}, 2000);
		if(jQuery("#ref_2").hasClass("active")){
			jQuery("#ref_1").removeClass("ref1 ref2 ref3 ref4 ref5 ref6");
			jQuery("#ref_1").addClass($(this).attr("id"));
			jQuery("#ref_2").fadeOut(2000);
			jQuery("#ref_2").removeClass("active");
			/*jQuery(".img_text").hide();
			jQuery(".img_text."+ $(this).attr("id")).show();*/
			jQuery(".img_text").fadeOut(1000);
			jQuery(".img_text."+ $(this).attr("id")).fadeIn(2000);
		}
		else {
			jQuery("#ref_2").removeClass("ref1 ref2 ref3 ref4 ref5 ref6");
			jQuery("#ref_2").addClass($(this).attr("id"));
			jQuery("#ref_2").fadeIn(2000);
			jQuery("#ref_2").addClass("active");
			jQuery(".img_text").fadeOut(1000);
			jQuery(".img_text."+ $(this).attr("id")).fadeIn(2000);
		}
		$(this).addClass("active");
	});

	p2pMenu("#mainMenu");

});

function p2pMenu(m){
	var $ = jQuery;
	$(m + " LI").each(function(){
		if(jQuery(".current_page_parent", this).length){
			$(this).addClass("current_page_ancestor");
			var cl = this;
			while($("LI .current_page_parent", cl).length){
				$("LI", cl).addClass("current_page_ancestor");
				cl = $("LI", cl);
			}
		}
	});
}

function getQuote(){
	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 LI").show();
	}, 1000);
}

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 resizeFooter(minHeight, $){
	window.onresize = function() { resizeFooter(minHeight, $); }
	var newHeight = document.body.offsetHeight - $("#top").height() - $("#main").height();
	if(newHeight < minHeight) newHeight = minHeight;
	$("#bottom").css("height", newHeight + 'px');
}
