$(document).ready(function(){
    
	// Set bold and arrow to last item in the menu
   	$("#menu ul li:last a").css({"background": 'transparent url(/media/lotidningen/media/gfx/arrow-rotated.gif) no-repeat scroll right center',fontWeight: 'bolder',padding: '0px 15px 0px 0px'});
   	
   	// Get Smooo-created content and insert at the correct possition in the menu
   	var htmlStr = $("#outerEverything").html() ;
	$("#outerEverything").remove();
	$("#menu ul li:last").append(htmlStr);
	
	//console.log("ove");
	
	var iElementWidth = 0 ;
	var iElementHeight = 0 ;
	var iElementTop = 0 ;
	var iElementLeft = 0 ;
		
    
    // Add listener to the last item in the menu
    $("#menu ul li:last").hover(
    	function (e){
    		
    		// Show the menu
			$("#menu ul li:last span").show();

			// Set highlights
			if ($("#everythingMenu").is(':hidden')) {
				$("#menu ul li:last > a").css({"background": 'transparent url(/media/lotidningen/media/gfx/arrow-rotated.gif) no-repeat scroll right center'});	
			}else{
				$("#menu ul li:last > a").css({"background": 'transparent url(/media/lotidningen/media/gfx/arrow.gif) no-repeat scroll right center'});
			}
			
			position = $("#everythingMenu").position() ;
			/*console.log("Position: " + position.left + ' ' + position.top + "Width: " + $("#everythingMenu").width() + " Height: " + $("#everythingMenu").height());*/
			iElementWidth = $("#everythingMenu").width() ;
			iElementHeight = $("#everythingMenu").height() ;
			iElementTop = position.top ;
			iElementLeft = position.left ;
	    }
	);
	
	$().mousemove(function(e){
	
		//$(".stats").html(iElementWidth+', '+iElementHeight+', '+iElementLeft+', '+iElementTop);
			
			// Check if the cursor is inside the element
			if(e.pageX > iElementLeft && e.pageX < iElementLeft+iElementWidth && e.pageY > iElementTop-20 && e.pageY < iElementTop+iElementHeight){
				
			}else{
				$("#menu ul li:last span").hide();
				// Set highlights
				if ($("#everythingMenu").is(':hidden')) {
					$("#menu ul li:last > a").css({"background": 'transparent url(/media/lotidningen/media/gfx/arrow-rotated.gif) no-repeat scroll right center'});	
				}else{
					$("#menu ul li:last > a").css({"background": 'transparent url(/media/lotidningen/media/gfx/arrow.gif) no-repeat scroll right center'});
				}
			}
			
			//$('#head').append(e.pageX +', '+ e.pageY);
    	
    	
   	}); 

	
  });
  

function reportAbuse(){
	 $("div.imageComment p a").click(function (e) {
    	e.preventDefault();
    	valueNeeded = $(this).attr("rel");

    	if($("#abuseBoxId"+valueNeeded).length == 0){
	    	$(this).after("<div class=\"abuseBox\" id=\"abuseBoxId"+valueNeeded+"\">Klicka \"Anmäl\" för att anmäla det här inlägget<br /><input type=\"button\" value=\"Anmäl\"><br /><br /><a class=\"closeAbuseBox\">Stäng</a></div>");
	    	$("a.closeAbuseBox").click(function () {
    			$(this).parent().remove();
    			//$("div.imageComment p a.abuseButton").addClass("hideAbuseButton");
	    	});
	    	$("div.abuseBox input").click(function () {
	    		$(this).parent().remove();
	    		$.post("abuse.php",{commentId:valueNeeded}, function(data){
					
					if(data == "done"){
						alert("Kommentaren är anmäld, tack för din hjälp!");
					}else{
						alert("Kommentaren kunde inte anmälas, vänligen färsäk igen. Kvarstår problemet vänligen kontakta oss via kontaktformuläret. Tack för din hjälp.");
					}
					
				});

	    		
	    	});

    	}
    });

}

/**************************************/

function closePopup(){
	$(".surveyPopup").remove();
}

function showPopup(){
	$("#smooo").before("<div class=\"surveyPopup\" id=\"surveyPopup\"> Vill du hjälpa till och förbättra innehållet på LO-Tidningen? Du kan hjälpa oss genom att svara på en undersökning. <br /><br /><a class=\"closeSurveyBox\" href=\"javascript:openPopup();\">» JA TACK! Jag vill hjälpa till</a><br /><br /><a class=\"closeSurveyBox closeSurveyNoThanks\" href=\"javascript:closePopup();\">» Nej tack. Stäng popupen</a></div>");
}

function openPopup(){
	closePopup();
	my_url = 'https://www.questback.com/lotidningen/ltm7nypi2s/' ;
	myRef = window.open(my_url,'mywin','left=20,top=20,width=800,height=500,toolbar=0,resizable=1'); 
} 

/**************************************/
function getURLParam(){
	my_url = window.location.href;
				
	var returned_value = my_url.indexOf('?');
				
	if (returned_value==-1){ 
	  my_url = my_url + '?noLayout=true';
	}else{
		my_url = my_url + '&noLayout=true';
	}
			
	myRef = window.open(my_url,'mywin','left=20,top=20,width=600,height=500,toolbar=0,resizable=0,scrollbars=1'); 
	myRef.focus()
} 

function showEverything(){
	$("#everythingMenu").toggle();
}

function fontsizeup() {
  active = getActiveStyleSheet();
  switch (active) {
    case 'A-' : 
      setActiveStyleSheet('A');
      break;
    case 'A' : 
      setActiveStyleSheet('A+');
      break;
    case 'A+' : 
      break;
    default :
      setActiveStyleSheet('A');
      break;
  }
}

function fontsizedown() {
  active = getActiveStyleSheet();
  switch (active) {
    case 'A+' : 
      setActiveStyleSheet('A');
      break;
    case 'A' : 
      setActiveStyleSheet('A-');
      break;
    case 'A-' : 
      break;
    default :
      setActiveStyleSheet('A');
      break;
  }
}

function setActiveStyleSheet(title) {
  var i, a, main;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
      a.disabled = true;
      if(a.getAttribute("title") == title) a.disabled = false;
    }
  }
}

function getActiveStyleSheet() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") && !a.disabled) return a.getAttribute("title");
  }
  return null;
}

function getPreferredStyleSheet() {
  return ('A-');
}

function createCookie(name,value,days) {
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
  }
  else expires = "";
  document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
  var nameEQ = name + "=";
  var ca = document.cookie.split(';');
  for(var i=0;i < ca.length;i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  }
  return null;
}

window.onload = function(e) {
  var cookie = readCookie("style");
  var title = cookie ? cookie : getPreferredStyleSheet();
  setActiveStyleSheet(title);
}

window.onunload = function(e) {
  var title = getActiveStyleSheet();
  createCookie("style", title, 365);
}

var cookie = readCookie("style");
var title = cookie ? cookie : getPreferredStyleSheet();
if (title == 'null') {
  title = getPreferredStyleSheet();
}

setActiveStyleSheet(title);
