// Function that slides content up and down and fix iframe-size
// javascript:slide('#theDiv');

function slide(theDiv) {
	if ($(theDiv).is(":hidden")) {
		$(theDiv).slideDown("slow");
	} else {
		$(theDiv).slideUp();
	}
};


function fade(theDiv) {
	if ($(theDiv).is(":hidden")) {
		$(theDiv).delay(1000).fadeIn("slow");
	} else {
		$(theDiv).fadeOut("slow");
	}
};

function fadeIn(theDiv) {
	$(theDiv).delay(1000).fadeIn("slow");
	$('.scroll-pane').delay(1200).jScrollPane();
};

function fadeOut(theDiv) {
	$(theDiv).fadeOut("slow");
};

function doMagic(theDiv) {
	$(theDiv).css('position', 'absolute');
	$(theDiv).css('left', '-5000px');
}

function doSecondMagic(theDiv) {
	$(theDiv).css('position', 'static');
	$(theDiv).css('left', '0px');
}


// Function that loads content
// javascript:loadContent('#theDiv','theContent');

function loadContent(theDiv,theContent) {
    $(theDiv).html(theContent);
};

// Function that loads content from URL
// javascript:loadContent('#theDiv','theContent');

function loadURL(theDiv,theContent) {
    $(theDiv).load(theContent);
};

// Functions that shows a div
// javascript:show('#theDiv');

function show(theDiv) {
	$(theDiv).show();
};

// Functions that hides a div
// javascript:hide('#theDiv');

function hide(theDiv) {
	$(theDiv).hide();
};

// Functions that toggles a div
// javascript:toggle('#theDiv');

function toggle(theDiv) {
	$(theDiv).toggle();
};


// Funktion for scrollers

function toggle(theDiv) {
	$(theDiv).toggle();
};


// Chacnge CSS

function changeBackground(theDiv,what,value) {
	$(theDiv).css(""+what+"", ""+value+"");
};


// load articles

function loadURLNew(theDiv,theContent) {
    $(theDiv).load(theContent);
    $(window).unbind('scroll');
	$('img').jail({effect:"fadeIn", speed : "slow"});
};



function loadArticles(theDiv,theURL,theLoadDiv,theLoadHTML,theShownURL,scrollTo,jail)
{
	
	$(theLoadDiv).html(theLoadHTML);
	$.doTimeout( 50, function(){
		$(theDiv).animate({opacity: '+=0'}, 2000).writeCapture().load(theURL, function(){
			kill();
			$(theLoadDiv).css("display", "none");
			$(window).unbind('scroll');
		    $('img').jail({effect:"fadeIn", speed : "slow"});
		     
			checkCookie();
				
			var disqus_shortname = 'feber'; 
			var s = document.createElement('script'); s.async = true;
		    s.type = 'text/javascript';
			s.src = 'http://mediacdn.disqus.com/1311283007/build/system/count.js';
		    (document.getElementsByTagName('HEAD')[0] || document.getElementsByTagName('BODY')[0]).appendChild(s);
		    
		    window.history.pushState("", "", theShownURL);
		    
		    if(scrollTo == 'yes') {
		    	$.scrollTo(theDiv);
		    }
		    
		    $('div.reklamYo').lazyLoadAd({
		    	forceLoad    : false,
		    	debug: false
		    }) ;
		    
		    
		});
	});
};

function showMenu() {
	$('#dasmenu').css("position", "fixed").delay(10000).queue(function() {
		$('#dasmenu').css("position", "absolute");
	});
}
			
			
function kill() {
  /* Reset various Tailsweep vars. before "page reload" */ 
  window.tsAdsByPrio_top = undefined;
  window.tsAdsRendered_top_slot = undefined;
  window.tsAdsRendered_top = undefined;
  window.ts_currentSlot_top = undefined;
  window.ts_top_siteAdMap = undefined;  /* Denna rad är ny */                          
}; 
	

// load comments 
function loadComments(theDiv,theURL) {
	if ($(theDiv).is(":hidden")) {
		$(".showDisqus").html("");
		$(theDiv).load(theURL, function(){
			$(".showDisqus").slideUp("slow");
			$(theDiv).slideDown("slow", function(){
			$.scrollTo(theDiv, 800);
		}).delay(2000);
		});
	} else {
		$(theDiv).html("");
		$(theDiv).slideUp();
	}	
};

// load comments in article 
function loadCommentsArt(theDiv,theURL) {
	if ($(theDiv).is(":hidden")) {
		$(".showDisqus").html("");
		$(theDiv).load(theURL, function(){
			$(".showDisqus").slideUp("slow");
			$(theDiv).slideDown("slow", function(){
		}).delay(2000);
		});
	} else {
		$(theDiv).html("");
		$(theDiv).slideUp();
	}	
};

// load allpics 
function loadAllImages(theDiv,theURL) {
	if ($(theDiv).is(":hidden")) {
		$(theDiv).writeCapture().load(theURL, function(){
			kill();
			$(theDiv).slideDown("slow");
			$('img').jail({effect:"fadeIn", speed : "slow"});
		});
	} else {
		$(theDiv).slideUp();
	}	
};

function loadOnePic(theDiv,theContent) {
	if ($(theDiv).is(":hidden")) {
		$(theDiv).writeCapture().load(theContent, function(){
			kill();
			$(theDiv).slideDown("slow");
			$('img').jail({effect:"fadeIn", speed : "slow"});
		});
	} else {
		$(theDiv).writeCapture().load(theContent, function(){
			kill();
			$(theDiv).slideDown("slow");
			$('img').jail({effect:"fadeIn", speed : "slow"});
		});
	}
}; 


// Load Youtube-video

function loadVideo(youtubeId,theWidth,theHeight) {
				$("#youtube_"+youtubeId+"").html("<object width='"+theWidth+"' height='"+theHeight+"'><param name='movie' value='http://www.youtube.com/v/"+youtubeId+"?rel=0&amp;modestbranding=1&amp;autohide=1&amp;showinfo=0&amp;showsearch=0&amp;wmode=transparent'></param><param name='allowFullScreen' value='true'></param><param name='autoplay' value='1'></param><param name='allowscriptaccess' value='always'></param><embed src='http://www.youtube.com/v/"+youtubeId+"?rel=0&amp;modestbranding=1&amp;autohide=1&amp;showinfo=0&amp;showsearch=0&amp;wmode=transparent&amp;autoplay=1' type='application/x-shockwave-flash' allowscriptaccess='always' allowfullscreen='true' width='"+theWidth+"' height='"+theHeight+"'></embed></object>");
				$("#youtube_overlay_"+youtubeId+"").css("display", "none");
	
};


// Hämta tumnagel till Vimeo
function getVimeoThumb(vimeoVideoID,vimeoThumbID){
	$.getJSON('http://www.vimeo.com/api/v2/video/' + vimeoVideoID + '.json?callback=?', {format: "json"}, function(data) {
    $(vimeoThumbID).attr('src', data[0].thumbnail_large);
});
};

// Load Vimeo-video

function loadVimeoVideo(youtubeId,theWidth,theHeight) {
				$("#youtube_"+youtubeId+"").html("<iframe src='http://player.vimeo.com/video/"+youtubeId+"?title=0&amp;byline=0&amp;portrait=0&amp;autoplay=1' width='"+theWidth+"' height='"+theHeight+"' frameborder='0' webkitAllowFullScreen allowFullScreen></iframe>");
				$("#youtube_overlay_"+youtubeId+"").css("display", "none");
	
};

// Slideshow Carolines

/*
$(function() {
	$(".newsticker-jcarousellite").jCarouselLite({
		mouseWheel: true,
		vertical: true,
		visible: 2,
		circular: true,
		auto:3000,
		speed:1000
	});
});

*/



// This sets a cookie
function setCookie(theCookie,theValue) {
	var standingCookie = $.cookie(theCookie);
	
	if(theValue == null) {
		$.cookie(theCookie, null, { expires: 365, path: '/' });
	}
	else if(standingCookie == '' || standingCookie == null) {
		$.cookie(theCookie, theValue, { expires: 365, path: '/' });
	}
	else if(standingCookie.match(theValue)) {
		/* do nothing */
	}
	else {
		$.cookie(theCookie, standingCookie + "|" + theValue, { expires: 365, path: '/' });
	}
}

// This removes a value from a cookie and checks for |-leftovers
function removeCookieValue(theCookie,theValue) {
	var standingCookie = $.cookie(theCookie);
	
	if(theValue.match(standingCookie) ) {
		
		$.cookie(theCookie, standingCookie.replace(theValue,''), { expires: 365, path: '/' });
		
		var standingCookie = $.cookie(theCookie);
		
		$.cookie(theCookie, standingCookie.replace('||','|'), { expires: 365, path: '/' });
		
		var standingCookie = $.cookie(theCookie);
		var firstChar = standingCookie.charAt(0);
		
		var the_length=standingCookie.length;
		var lastChar=standingCookie.charAt(the_length-1);
		
		if ("|"==firstChar) {
			$.cookie(theCookie, standingCookie.substr(1), { expires: 365, path: '/' });
		}
		
		if ("|"==lastChar) {
			$.cookie(theCookie, standingCookie.slice(0, -1), { expires: 365, path: '/' });
		}
	}
}

// This hides content depending on the cookie hideSites
	function checkCookie() {
				
		var standingCookie=$.cookie('hideSites');
		
		if(standingCookie == '' || standingCookie == null) {
		}
		else {
			var mySplitResult = standingCookie.split("|");
			
			for(i = 0; i < mySplitResult.length; i++) {
				// alert("." + mySplitResult[i]);
				// alert(mySplitResult.length);
				
			$("." + mySplitResult[i]).css("display","none");
			$("." + mySplitResult[i] + "Off").css("display","block");
			$("#" + mySplitResult[i] + "Menu").css("opacity","0.5");
			
		};
	};
};




// Function that handle thumbs up / down
// javascript:submitDegree('${listArticle.id}','1');
// javascript:submitDegree('${listArticle.id}','-1');

function submitDegree(artid,val)
{
	var thumburl = "/updownfeber/?art=" + artid + "&t=" + val;
	var div_id = "temp_" + artid;
	var errid = "thumbsreply_" + artid;
	
	var cookie = $.cookie("thumb-"+errid);
	if(cookie) thumburl = "/updownfeber/?art=-1";
	var req = null; 

	if(window.XMLHttpRequest)
		req = new XMLHttpRequest(); 
	else if (window.ActiveXObject)
		req  = new ActiveXObject(Microsoft.XMLHTTP); 
	req.onreadystatechange = function()
	{ 
		if(req.readyState == 4)
		{
			if(req.status == 200)
			{
				if(req.responseText != "ERROR"){ 
					document.getElementById(div_id).innerHTML = req.responseText;
					$.cookie("thumb-"+errid, "true", { path: "/", expires: 10 });
				
				}
				else  document.getElementById(div_id).innerHTML = "NŒgot blev fel!"  + req.responseText;
			}	
			else	
			{
				//do nothing
			}	
		} 
	}; 
	req.open("GET", thumburl, true); 
	req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); 
	req.send(null); 

} 

/* ny och ännu bättre funktion. 2009-04-25 /w */

function submitThumb2(artid,val)
{
	var thumburl = "/updown/?art=" + artid + "&t=" + val;
	var div_id = "ikon_thumbs_" + artid;
	var errid = "thumbsreply_" + artid;
	
	var cookie = $.cookie("thumb-"+errid);
	var req = null; 

	if(window.XMLHttpRequest)
		req = new XMLHttpRequest(); 
	else if (window.ActiveXObject)
		req  = new ActiveXObject(Microsoft.XMLHTTP); 
	req.onreadystatechange = function()
	{ 
		if(req.readyState == 4)
		{
			if(req.status == 200)
			{
				if(req.responseText != "ERROR"){ 
					document.getElementById(div_id).innerHTML = req.responseText;
					$.cookie("thumb-"+errid, "true", { path: "/", expires: 10 });
				}
				else  document.getElementById(div_id).innerHTML = "Något blev fel!"  + req.responseText;
			}	
			else	
			{
				//do nothing
			}	
		} 
	}; 
	req.open("GET", thumburl, true); 
	req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); 
	req.send(null); 
} 



// Function that moves to the next article with j and k

function scroll(direction) {

    var scroll, i,
            positions = [],
            here = $(window).scrollTop(),
            collection = $('.article');

    collection.each(function() {
        positions.push(parseInt($(this).offset()['top'],10));
    });

    for(i = 0; i < positions.length; i++) {
        if (direction == 'next' && positions[i] > here) { 
scroll = collection.get(i); 
break; 
 }
        if (direction == 'prev' && i > 0 && positions[i] >= here) { 
scroll = collection.get(i-1); 
break; 
 }
    }

    if (scroll) {
        $.scrollTo(scroll, {
            duration: 500       
        });
    }

    return false;
}


$(function() {
    $("#next,#prev").click(function() {        
        return scroll($(this).attr('id'));    
    });
});


$(window).keydown (function(event) {
    if (event) {
        switch (event.which) {
            case 74:  // j
                scroll ('next');
                break;
            case 75:  // k
                scroll ('prev');
                break;
        }
    }
    else {
        switch (event.keyCode) {
            case 37: // key is left
        case 38: // key is up
                scroll ('prev');
                break;
            case 39: // key is right
        case 40: // key is down
                scroll ('next');
                break;
        }
    }
});


