// JavaScript Document

$(document).ready(function() {
	
  	$( '.endstatement' ).eq( Math.floor(Math.random()*$( '.endstatement' ).length) ).show();
	
	var pathname = window.location.pathname;
		
	if(pathname == "/Om-os/Nyheder.aspx" || pathname == "/Om-os/Nyheder/Nyheds-visning.aspx") {
		$("#topnavigation ul li").eq(2).addClass("topactiveitem");
		$("#topnavigation ul li").eq(4).addClass("notactive");
	}
	
	if(pathname == "/Kontakt/Projekttilbud.aspx") {
		$("#topnavigation ul li").eq(5).addClass("topactiveitem_orange");
		$("#topnavigation ul li").eq(6).addClass("notactive");
	}
		
	$(".tilbuditem").eq(0).show();
	$(".tilbudheader").eq(0).addClass("activeitem");

	// Form select styling
	$("select").select_skin();

	keywords = 
	[
	 "/files/billeder/baggrund/1.jpg", 
	 "/files/billeder/baggrund/2.jpg", 
	 "/files/billeder/baggrund/3.jpg", 
	 "/files/billeder/baggrund/4.jpg"
	]
	
	var keyword = keywords[Math.floor(Math.random()*keywords.length)]
	
	$(".changebgpic").css('background', 'url('+keyword+') no-repeat 50% 0');
	
	
	$(".tilbud1 .field:odd").addClass("right");
	$(".tilbud2 .field:odd").addClass("right");
	$(".tilbud3 .field:odd").addClass("right");
	$(".tilbud4 .field:odd").addClass("right");
			
	
	$(".changepic").click(function() {
	
		var billedlink = $(this).attr('rel');
		
		$(".largepic").html('<img src="/admin/public/getimage.aspx?width=630&amp;height=460&amp;image='+billedlink+'" alt="" border="" />');
			return false;
	});
	
	
	var scrollPane = $( ".scroll-pane" ),
	scrollContent = $( ".scroll-content" );
	
	var scrollbar = $( ".scroll-bar" ).slider({
		slide: function( event, ui ) {
			if ( scrollContent.width() > scrollPane.width() ) {
				scrollContent.css( "margin-left", Math.round(
					ui.value / 100 * ( scrollPane.width() - scrollContent.width() )
				) + "px" );
			} else {
				scrollContent.css( "margin-left", 0 );
			}
		}
	});
	
	var handleHelper = scrollbar.find( ".ui-slider-handle" )
	.mousedown(function() {
		scrollbar.width( handleHelper.width() );
	})
	.mouseup(function() {
		scrollbar.width( "100%" );
	})
	.append( "<span class='ui-icon ui-icon-grip-dotted-vertical'></span>" )
	.wrap( "<div class='ui-handle-helper-parent'></div>" ).parent();
	
	scrollPane.css( "overflow", "hidden" );
	
	function sizeScrollbar() {
		var remainder = scrollContent.width() - scrollPane.width();
		var proportion = remainder / scrollContent.width();
		var handleSize = scrollPane.width() - ( proportion * scrollPane.width() );
		scrollbar.find( ".ui-slider-handle" ).css({
			width: handleSize,
			"margin-left": -handleSize / 2
		});
		handleHelper.width( "" ).width( scrollbar.width() - handleSize );
	}
	
	function resetValue() {
		var remainder = scrollPane.width() - scrollContent.width();
		var leftVal = scrollContent.css( "margin-left" ) === "auto" ? 0 :
			parseInt( scrollContent.css( "margin-left" ) );
		var percentage = Math.round( leftVal / remainder * 100 );
		scrollbar.slider( "value", percentage );
	}
	
	function reflowContent() {
			var showing = scrollContent.width() + parseInt( scrollContent.css( "margin-left" ), 10 );
			var gap = scrollPane.width() - showing;
			if ( gap > 0 ) {
				scrollContent.css( "margin-left", parseInt( scrollContent.css( "margin-left" ), 10 ) + gap );
			}
	}
	
	$( window ).resize(function() {
		resetValue();
		sizeScrollbar();
		reflowContent();
	});

	setTimeout( sizeScrollbar, 10 );
		




abcdedf = $(".scroll-content-item").size();
widthabc = $(".scroll-content-item").width();

scrollwidth = (abcdedf * (widthabc + 10));

$(".scroll-content").css('width', scrollwidth + 'px');
		

  $("#forsidetop_slider").everyTime(8000, 'topstorytimer', function () {
	  sliderpush('forsidetop_slider', 'forsidetop');  
  });


    forsidetop_index = 0;
    forsidetop_count =  $(".banneritem").size();
    forsidetop_width = 1600;

      //navknapper
      ii=0;
      var navcontent ='<ul class="navitems">';
      while (ii < forsidetop_count) {

        if (ii == 0) {
        	var isAktiv = "active";
        } else {
        	var isAktiv = "";
        }

        var navcontent = navcontent + '<li class="'+isAktiv+'"><a href="javascript:void(0);" rel="'+ii+'" style="display: block; height: 9px"></a></li>';
        ii++;

      }
	  var navcontent = navcontent + '</ul>';


      $("#forsidetop_nav").html(navcontent);
	  
      $("#forsidetop_nav a").live("click", function() {

        var rel = $(this).attr('rel');
        forsidetop_index = rel;
        sliderpush('forsidetop_slider', 'forsidetop', $(this));
        $("#forsidetop_slider").stopTime();

      });

	$("div.banneritem div.bannerdesc div.text a:eq(0)").addClass('firsta');
	$("div.banneritem div.bannerdesc div.text a:eq(1)").addClass('lasta');
	$("div.banneritem div.bannerdesc div.text a:eq(3)").addClass('lasta');
	$("div.banneritem div.bannerdesc div.text a:eq(5)").addClass('lasta');
	$("div.banneritem div.bannerdesc div.text a:eq(7)").addClass('lasta');
	$("div.banneritem div.bannerdesc div.text a:eq(9)").addClass('lasta');
	$("div.banneritem div.bannerdesc div.text a:eq(11)").addClass('lasta');

	$(".footerli").mouseenter(function() {
		$(this).addClass("activewhite");
		$(this).find(".onover").show();
		Cufon.replace('#endrow .navigation .header');

	}).mouseleave(function() {
		$(this).removeClass("activewhite");
		$(this).find(".onover").hide();
		Cufon.replace('#endrow .navigation .header');
	});
		
	$(".tilbudheader").click(function() {
		$(".tilbuditem").hide();
		$(".tilbudheader").removeClass("activeitem");
		$(this).next(".tilbuditem").show();
		$(this).addClass("activeitem");
	});
	
	
	var tilbudcount = $('.tilbudheader').size();
	if(tilbudcount > 0) {
		var hash = location.hash;
		hash = hash.replace( /^#/, '' );
		
		if(hash) {
			$(".tilbudheader").removeClass("activeitem");
			
			if(hash == "industri") { 
				$(".tilbud div").eq(0).css('z-index', '10000'); 
				$(".tilbudheader").eq(0).addClass("activeitem");
			};
			if(hash == "idret") { $(".tilbud div").eq(1).css('z-index', '10000'); $(".tilbudheader").eq(1).addClass("activeitem");};
			if(hash == "landbrug") { $(".tilbud div").eq(2).css('z-index', '10000'); $(".tilbudheader").eq(2).addClass("activeitem");};
			if(hash == "ridesport") { $(".tilbud div").eq(3).css('z-index', '10000'); $(".tilbudheader").eq(3).addClass("activeitem");};
		}
		else
		{
			$(".tilbud div").eq(0).css('z-index', '10000');
		}
	}
	
	
});


function sliderpush(sliderdiv, prefix, clickelement) {

				
	index = window[prefix+'_index'];
	var count = window[prefix+'_count'];
	var width = window[prefix+'_width'];

	if (clickelement) {
	
	var thisdirection = clickelement.attr('rel');
	

	if (thisdirection == 'zap_v') {

		index--;		

		if (index >= 0) {

			var forsidetopmoveto = width * index;
			$("#"+sliderdiv+"").animate({
				left: '-' + forsidetopmoveto +'px' 
				
			});			

		} else {
			index++;	
		}


	} else if (thisdirection == 'zap_h') {

		index++;	
		
		if (index < count) {

			var forsidetopmoveto = width * index;
									
			$("#"+sliderdiv+"").animate({
				left: '-' + forsidetopmoveto +'px' 
				
			});	
		

		} else {
			
			
			index = 0;
			var forsidetopmoveto = width * index;
									
			$("#"+sliderdiv+"").animate({
				left: '-' + forsidetopmoveto +'px' 
				
			});	
			
			
			
			//index--;	
		}					


	} else {
			
		var forsidetopmoveto = width * index;
		
		$("#"+sliderdiv+"").animate({
			left: '-' + forsidetopmoveto +'px' 
			
		});		
			
		
	}

	} else {

		index++;	
		
		if (index < count) {

			var forsidetopmoveto = width * index;
									
			$("#"+sliderdiv+"").animate({
				left: '-' + forsidetopmoveto +'px' 
				
			});			

		} else {
			index = 0;
			
			var forsidetopmoveto = width * index;
			
			$("#"+sliderdiv+"").animate({
				left: '-' + forsidetopmoveto +'px' 
				
			});		
		}	
		


	}

	$("#"+prefix+"_nav ul li").removeClass("active");
	$("#"+prefix+"_nav ul li").eq(index).addClass("active");

	window[prefix+'_index'] = index;

	//$("#debug").html(index + '-' + forsidetopmoveto);

}
