// JavaScript Document

// JQuery ------------------------------------------------------------------------------
function GetCoords()
  {
    var scrollY;
    if (document.all)
    {

      if (!document.documentElement.scrollTop)
        scrollY = document.body.scrollTop;
      else
        scrollY = document.documentElement.scrollTop;
    }
    else
    {
      scrollY = window.pageYOffset;
    }
    return scrollY;
  } 
  
$(document).ready(function() 
	{	
	
	    $("#searchbox").autocomplete("/Lucene/AutoCompleteLuceneHandler.ashx", {
				width: 210,
				scroll: false,
				max: 25,
				delay: 10,
				cacheLength: 1,
				minChars: 1,
				cacheLength: 1,
				matchSubset: 0,
				formatItem: function(data, i, n, value) {
				
				    if(value.split("_")[0] != "" && value.split("_")[2] != "")
				    {				    
				        return "<p><a class=\"kategori\" href="+value.split("_")[1]+">" + value.split("_")[2] + "</a></p>"; // Categori
				    }
				    
				    else
				    {
				        return "<a href=" + value.split("_")[1] + ">" + value.split("_")[0] + "</a>"; // Länk/URL
				    }
				}
			});		

		// Mediaplatta knapp
		
		$('#remote').toggle(function() {
			$('#media_content').cycle('pause');
			$(this).attr({ src: "/img/play.png", title: "Spela bildspelet", alt: "Spela"});
				}, function() {

			$('#media_content').cycle('resume', true);
			$(this).attr({src: "/img/pause.png", title: "Pausa bildspelet", alt: "Pausa"});
        });
		
			
		//$('#media').hover(
//			function() { $('#remote_wrapper').fadeIn(500); },
//			function() { $('#remote_wrapper').fadeOut(500); }
//		 );
		
		//Dölj knapp frän början
		
		//$("#remote_wrapper").hide();
//		$("#remote_wrapper").animate({bottom: "18"}, 2000)
		//$("#remote_wrapper").show().delay(2000);

		// Mediaplatta framsida
		
		$('#media_content') 
		.after('<div id="nav">') 
		.cycle({ 
		fx:     'fade', 
		speed:  '1000', 
		timeout: 15000, 
		pager:  '#nav',
		//prev:   '#prev', 
		//next:   '#next', 
		//before:  onBefore, 
    	//after:   onAfter, 
		cleartype:  1
	
		});
		
		// Mediaplatta thumbs
		
		var size = $('#nav').children().size();
		for (a = 0; a < size; ++a) {
			//setTimeout(null, 500);
			var alt = $('#media_content').children().eq(a).find('img').attr('alt');
			var title = $('#media_content').children().eq(a).find('img').attr('title');
			$('#nav').children().eq(a).addClass('ref' + a).html('<img src="/img/ref_' + alt + '.png" alt="'+alt+'" title="'+title+'" />');
		}
		
		
		$('#nav').children(':first-child').after('<p class="ref"><span>Ett urval av<br />v&aring;ra kunder</span></p>');
		$('#nav').children(':last-child').addClass('end');
		
//		$('#nav').children(':first-child').before('<p><span>Om<br />oss</span></p>');

		//$("#nav").animate({bottom: "+=200"}, 2000)
		  //return false;
		
//		function onBefore() { 
			//$('#nav').html($(this).find('a').class('')); 
			//$('#output').html("Visas: " + $(this).find('img').attr('alt'));
//		}
//		
//		function onAfter() { 
//			$('#output').html("Visas: " + $(this).find('img').attr('alt')); 
//		}
		
		
		// Nyhetsarkiv
		//$('ul#accordion h3:first').addClass('selected');
  		//$('ul#accordion h3:not(.selected)').next().hide();

		$('#accordion').accordion({
			header: 'h3',
			navigation: false,
			active: '.selected',
			autoheight: false,
			clearStyle: true,
			collapsible: true,
			alwaysOpen: false,
			animated: 'slide'
		});

	}

);

// SEARCH BOX ------------------------------------------------------------------------------

$(function() {
	swapValues = [];
	$(".swap_value").each(function(i){
		swapValues[i] = $(this).val();
		$(this).focus(function(){
			if ($(this).val() == swapValues[i]) {
				$(this).val("");
			}
		}).blur(function(){
			if ($.trim($(this).val()) == "") {
				$(this).val(swapValues[i]);
			}
		});
	});
});

//	$(document).ready(function() {
//		$("#searchbox").autocomplete("/AutoCompleteHandler.ashx", {
//			width: 210,
//			scroll: false,
//			max: 20,
//			delay: 10,
//			formatItem: function(data, i, n, value) {
//				return "<a href=" + value.split("_")[1] + ">" + value.split("_")[0] + "</a>"; // Länk/URL
//			}
//		});
//	});

// EXTERNA LÄNKAR BOX ------------------------------------------------------------------------------

function externalLinks() {  
 if (!document.getElementsByTagName) return;  
 var anchors = document.getElementsByTagName("a");  
 for (var i=0; i<anchors.length; i++) {  
   var anchor = anchors[i];  
   if (anchor.getAttribute("href") &&  
       anchor.getAttribute("rel") == "external")  
     anchor.target = "_blank";  
 }  
}  
window.onload = externalLinks;

function toggle_visibility(part) 
{
	var e = document.getElementById(part);
	if(e.style.display == 'none'){
		e.style.display = 'inline';}
	else{
		e.style.display = 'none';}
}

// SÖK AUTOCOMPLETE ------------------------------------------------------------------------------
//$(document).ready(function() {
//	$("#searchbox").autocomplete("/AutoCompleteHandler.ashx", {
//		width: 210,
//		scroll: false,
//		max: 20,
//		delay: 10,
//		cacheLength: 1,
//		minChars: 1,
//		formatItem: function(data, i, n, value) {
//			return "<a href=" + value.split("_")[1] + ">" + value.split("_")[0] + "</a>"; // Länk/URL
//		}
//	});
//});
