// JavaScript Document

jQuery.fn.slideLeftHide = function( speed, callback ) { this.animate( { width: "hide", paddingLeft: "hide", paddingRight: "hide", marginLeft: "hide", marginRight: "hide" }, speed, callback ); }
jQuery.fn.slideLeftShow = function( speed, callback ) { this.animate( { width: "show", paddingLeft: "show", paddingRight: "show", marginLeft: "show", marginRight: "show" }, speed, callback ); }

function hide(id) {
	$("#"+id).slideUp("slow", function() {
		$("#"+id).empty();
	});
}
function show(id) {
	$("#"+id).slideDown("slow");
}

function showHide(id) {
	$("#"+id).toggle("slow");
}

function showImg(PostID, BildeID) {
	$('#Main_'+PostID).hide();
	$('#Small_'+PostID).show();
	$('#XL_'+PostID).show();
	$.post("/ajax/showImg.php", { PostID: PostID, BildeID: BildeID }, function(data) {
		$('#XL_'+PostID).html(data);
	});
}

function sendSkjema(id) {
	$.post("/ajax/sendSkjema.php", $("#Skjema_" + id).serialize(), function(data) { 
		$("#Skjema_" + id + "_response").empty();
		$("#Skjema_" + id + "_response").html(data).fadeIn(700);
	});
}

function getAnsatte(id) {
	var height = $('#ansatte_container').height();
	var heightMain = $('#ansatte_main').height();
	//alert(height);
	if(height > 0) {
		$('#ansatte_container').height(height);
	}
	$('#ansatte_main').fadeOut('200', function() {
		//$('#ansatte_container').height(height);
		$.post("/ajax/getAnsatte.php", { KatID: id }, function(data) { 
			$("#ansatte_main").html(data).fadeIn('200');;
		});
	});
}

function expand(kid, parent) {
	//alert(kid + ' ' + parent);
	//$('#'+div).hide('explode', {}, 1000, function() {
	//$('#'+div).addClass('spalte_left4');
	/*
	$('#main_div_'+parent).slideUp('200', function() {
		$.post('/ajax/hentSide.php', { Kid: kid, Parent: parent }, function(data) {
			$('#main_div_'+parent).html(data).slideDown('200'); //show('slide', {}, 1000);
			//$.scrollTo('#container_'+parent, 200, { offset: { top: -160 }});
		});
	});
	*/
	//});
	//$('#'+div).effect("scale", { percent: 400, scale: 'box' }, 2000);
	//$('#main_div_'+parent).animate({ width:'toggle'} , 4000);
	var height = $('#parent_'+parent).height();
	//$('#height_'+parent).val(height);
	$('#parent_'+parent).css('min-height', height);

	$('#main_div_'+parent).hide('slide', { direction: 'right' }, 500, function () {
		$.post('/ajax/hentSide.php', { Kid: kid, Parent: parent }, function(data) {
			$('#main_div_'+parent).html(data).effect('slide', { direction: 'left' }, 500);
			var main_height = $('#main_div_'+parent).height();
			//$('#height_'+parent).val(main_height);
			if (main_height < height) {
				$('#parent_'+parent).css('min-height', main_height);
			}
		});
	});	
}

function getGallery(GalleriID, SideID) {
	$.post('/ajax/getGallery.php', { GalleriID: GalleriID, SideID: SideID }, function(data) {
		$('#galleri_'+SideID).html(data);
	});
}

function loadSideInlay(ParentID, SideID) {
	//var height = $('#inlay_container_'+ParentID).height();
	//$('#height_'+ParentID).val(height);
	//$('#parent_'+parent).css('min-height', height);
	//$('a',$(this)).stop().animate({'marginLeft':'-85px'},200);
	//$('#inlay_'+ParentID).animate({'marginLeft': '-900px'}, 200, function() {
		//$('#inlay_'+ParentID).css({'marginLeft': '0px'});
	//$('#inlay_'+ParentID).effect('slide', { direction: 'left' }, 500, function () {
	 //.attr({visibility:'hidden'})
	$("li.tabs").removeClass("tabs tab_current").addClass("tabs");
	$('#tab_'+SideID).addClass('tab_current');
	$('#inlay_'+ParentID).animate({
			//opacity: 'hide',
			//width: 'hide',
			left: '1000' //window.innerWidth-1000
			//top: window.innerHeight-90
			//left: 1000 // parseInt($('#inlay_'+ParentID).css('paddingLeft'),10) == 0 ? $('#inlay_'+ParentID).outerWidth() : 0 
			//paddingLeft: 1000
			}, 200, function () {
			
		$.post('/ajax/getSideInlay.php', { SideID: SideID }, function(data) {
			$('#inlay_'+ParentID).css({'left': '0'});
			$('#inlay_'+ParentID).html(data); //show('slide', { direction: 'left' }, 200).html(data);
			var h = $('#inlay_'+ParentID).height();
			$('#inlay_container_'+ParentID).css({'min-height': h});
			/*
			var inlay_height = $('#inlay_'+ParentID).height();
			$('#inlay_height_'+ParentID).val(inlay_height);
			//$('#height_'+parent).val(main_height);
			
			if (inlay_height < height) {
				$('#inlay_'+ParentID).css('min-height', main_height);
			}
			*/
		});
	});	
}

function goTo(div, id, top) {
	//alert(id);
	$.scrollTo('#'+div, 200, { offset: { top: -130 }});
	$("li.menyLinkLi").removeClass("menyLinkLi menyLinkLi_active").addClass("menyLinkLi");
	$('#link_'+id).addClass('menyLinkLi_active');
}


/*
$(document).ready(function(){
	$('.main_div').each(function(){
		$(this).css('opacity','0.5');
	});
	
	$(window).scroll(function(){
		$('.main_div').each(function(){
			$(this).css('opacity','0.5');
		});
	
		var webpage = $("body");
		var webpage_height = webpage.height();
 
		if ($(this).scrollTop() == webpage_height) {
			$('.main_div:last-child').css('opacity','1');
		} else {
			$('.main_div.inView()').css('opacity','1');
		}
	});
});
*/
var mapdiv;
var geocoder;
var map;
function initialize(div, tittel) {
	geocoder = new google.maps.Geocoder();
	var latlng = new google.maps.LatLng(-34.397, 150.644);
	var myOptions = {
		zoom: 14,
		center: latlng,
		mapTypeId: google.maps.MapTypeId.ROADMAP
	}
	map = new google.maps.Map(document.getElementById(div), myOptions);
	mapdiv = div;
}

function codeAddress(address) {
	//var address = document.getElementById(address).value;
	geocoder.geocode( { 'address': address}, function(results, status) {
		if (status == google.maps.GeocoderStatus.OK) {
			map.setCenter(results[0].geometry.location);
			var marker = new google.maps.Marker({
					map: map, 
					position: results[0].geometry.location
			});
		} else {
			//alert("Geocode was not successful for the following reason: " + status);
			$('#'+mapdiv).html('Kunne ikke laste kart');
		}
	});
}

function findMap(div, address) {
	var geocoder = new google.maps.Geocoder();
	var latlng = new google.maps.LatLng(-34.397, 150.644);
	var myOptions = {
		zoom: 15,
		center: latlng,
		mapTypeId: google.maps.MapTypeId.ROADMAP
	}
	var map = new google.maps.Map(document.getElementById(div), myOptions);
	
	geocoder.geocode( { 'address': address}, function(results, status) {
		if (status == google.maps.GeocoderStatus.OK) {
			map.setCenter(results[0].geometry.location);
			var marker = new google.maps.Marker({
					map: map, 
					position: results[0].geometry.location
			});
		} else {
			//alert("Geocode was not successful for the following reason: " + status);
			$('#'+div).html('Kunne ikke laste kart');
		}
	});
}

$(function() {
	//scrollpane parts
	var scrollPane = $( ".scroll-pane" ),
		scrollContent = $( ".scroll-content" );
	
	//build slider
	var scrollbar = $( ".scroll-bar" ).slider({
		slide: function(event, ui) {
        if (scrollContent.width() > scrollPane.width()) {
            scrollContent.animate({
                "margin-left": Math.round(ui.value / 100 * (scrollPane.width() - scrollContent.width()))
            }, 20);

        } else {
            scrollContent.animate({
                "margin-left": 0
            }, 20);
        }
    }
		/*
		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 );
			}
		}
		*/
	});
	
	//append icon to handle
	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();
	
	//change overflow to hidden now that slider handles the scrolling
	scrollPane.css( "overflow", "hidden" );
	
	//size scrollbar and handle proportionally to scroll distance
	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 );
	}
	
	//reset slider value based on scroll content position
	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 );
	}
	
	//if the slider is 100% and window gets larger, reveal content
	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 );
			}
	}
	
	//change handle position on window resize
	$( window ).resize(function() {
		resetValue();
		sizeScrollbar();
		reflowContent();
	});
	//init scrollbar size
	setTimeout( sizeScrollbar, 10 );//safari wants a timeout
});
