var myTimeout = null;
var sel = 0;
var items = null;
var tabContainers = null;
var lightboxLinks = null;
var tabs = null;
var startTimeout = 10000;
var timeout = 10000;
var debug = null;
var lightboxmap = null;

var isOpen = false;
var lightboxmap = false;


function showLightbox(id) {
	
	updateOverlay();
	if( isOpen == false ) return;
	jQuery("#bluecover").before(jQuery(id).get());
	jQuery(id + " a.lukk").click(doOverlayClose);
	var top = Math.round((jQuery(window).height() - jQuery(id).height() )/2) + jQuery(window).scrollTop();
	
	if(top < 30) {
		top = 30;
	}
	jQuery(id).css({
		display:'block',
		top: top
	});
	
	if( jQuery(id + " .gmap_attribute").length > 0 ) {
		//google.load("maps", "2.x");
		if (GBrowserIsCompatible()) {
			//var lightboxmap = new GMap2(document.getElementById(mapdata.mapid));
	        var startPoint;
	        var zoom;
	        if(mapdata.lat && mapdata.lon)
	        {
	          startPoint = new GLatLng(mapdata.lat,mapdata.lon);
	          zoom=13
	        }
			//console.log(mapdata);
			lightboxmap = new GMap2(document.getElementById(mapdata.mapid));
			lightboxmap.setCenter(startPoint, zoom);
			//console.log(lightboxmap);
			lightboxmap.addOverlay(new GMarker(startPoint));
			lightboxmap.setCenter(startPoint, zoom);
			lightboxmap.addControl(new GSmallMapControl());
	    }
	}
	
}
function updateOverlay() {
	if( isOpen == false ) return;
	// set the window background for the overlay. i.e the body becomes darker
	jQuery('#bluecover').css({"position":"absolute", "display":"block", width:jQuery(document).width(), height:jQuery(document).height(), top:jQuery(window).scrollTop(), left:jQuery(window).scrollLeft()});
}
function doOverlayOpen() {
	isOpen = true;
	showLightbox(jQuery(this).attr("href"));
	jQuery('#bluecover').css({opacity:0}).animate( {opacity:0.9}, 100 );
	jQuery(window).bind('resize',updateOverlay);
	return false;
}
function doOverlayOpenId(id) {
	//console.log("show: " + jQuery(this));
	isOpen = true;
	showLightbox(id);
	jQuery('#bluecover').css({opacity:0}).animate( {opacity:0.9}, 100 );
	jQuery(window).bind('resize',updateOverlay);
	return false;
}
function doOverlayOpenNewsletter() {
	isOpen = true;
	showLightbox("lightbox_newsletter");
	jQuery('#bluecover').css({opacity:0}).animate( {opacity:0.9}, 100 );
	jQuery(window).bind('resize',updateOverlay);
	return false;
}

function doOverlayClose() {
	isOpen = false;
	jQuery('.lightbox').css( 'display', 'none' );
	jQuery("#bluecover").unbind('resize', updateOverlay);
	// now animate the background to fade out to opacity 0
	// and then hide it after the animation is complete.
	jQuery("#bluecover").animate( {opacity:0.1}, 600 ).hide();
	return false;
}


jQuery(document).ready(function() {
	
	
	/* Løse problem med mange stedsnavn under Arrangementlokaler */
	if(jQuery('#left-column li.active ul').length != 0) {
		var col_height = jQuery('#left-column').height();
		var sub_col_height = jQuery('#left-column li.active ul').height();
		if(sub_col_height > col_height) {
			jQuery('#left-column').height(sub_col_height + 20);
		}
	}
	
	
	
	lightboxLinks = jQuery('a.lightbox_button'); 
	jQuery(".end-registration a.cancel").click(doOverlayClose);

	lightboxLinks.each(function(i) { 
		jQuery(this).bind('click', doOverlayOpen);
	});
	

	tabContainers = jQuery('.tabs-content'); 
	tabs = jQuery('a.tabs');
	items = tabs.size();
	
	tabs.each( function(i) { 
	  jQuery(this).click(function() { 
		tabContainers.hide().filter(this.hash).show(); 
		var parent = jQuery(this).parent().parent().get(0);
		
		jQuery(parent).siblings().removeClass("selected before_selected after_selected");
		jQuery(parent).removeClass("before_selected after_selected");
		jQuery(parent).prev().addClass("before_selected");
		jQuery(parent).next().addClass("after_selected");
		jQuery(parent).addClass("selected");
		stopTimeout();
		sel = i;
		return false;
	  }); 
	 });
	tabs.eq(0).click();
	if(tabs.eq(0).hasClass("animate")) {
		myTimeout =  setTimeout('trigger()', startTimeout);
	}
	
	// Lightbox media
	media = jQuery('.media .video');
	media_links = jQuery('.video-gallery ul a');
	media_lightbox_links = jQuery('a.lightbox_button_media');
	
	media_lightbox_links.each(function(i) { 
		jQuery(this).bind('click', function(e) {
			doOverlayOpenId(this.hash);
			jQuery(jQuery(this).attr('rel')).click();
			
			if(jQuery('.video-gallery ul li').size() > 4) {
				//console.log(jCarouselLite());
				jQuery(".video-gallery").jCarouselLite({
					btnNext: ".video-gallery-wrapper a.previous",
					btnPrev: ".video-gallery-wrapper a.next",
					visible: 5,
					scroll: 3
				});
			}
			
		});
	});
	
	
	
	media_links.live("click", function() { 
		
		media.hide().filter(this.hash).show(); 
		var parent = jQuery(this).parent().get(0);
		jQuery(parent).siblings().removeClass("active");
		jQuery(parent).addClass("active");
		return false;
	});
	
	var fancy_hover = jQuery('.content-view-line .attribute-image img, .content-view-banner-line .attribute-image img, , .content-view-thumb .attribute-image img');
	fancy_hover.bind( 'mouseover', function() {
		jQuery(this).fadeTo('fast', 0.5);
		})
		.bind( 'mouseout', function() {
		jQuery(this).fadeTo('fast', 1);
		});
		
		var fx_line_hover = jQuery('.info-courses .content-view-line, .course-overview .content-view-children .content-view-line, .content-search .content-view-line, .person-content .content-view-children .content-view-line')
		fx_line_hover.hover(
		      function () {
		        jQuery(this).addClass("line_hover").contents().find("img").fadeTo('fast', 0.5).unbind();
		      }, 
		      function () {
		        jQuery(this).removeClass("line_hover").contents().find("img").fadeTo(0, 1);
		      }
		    ).bind('click', function() {
				window.location = $(this).contents().find("a").eq(0).attr('href');
				return false;
			});
	
	/* selection of event place */
		jQuery("input.select_event").click(function() {
			var node_id = jQuery( this ).val();
			location='/coursefilter/redirect/' + node_id;
		});
});

function trigger() {
	sel = sel + 1;
	if(sel > this.items - 1) {
		sel = 0;
	}
	tabs.eq(sel).click(); 
	resetTimeout();
}

function resetTimeout() {
	myTimeout =  setTimeout('trigger()', this.timeout);
}
function stopTimeout() {
	//myTimeout = null;
	myTimeout =  setTimeout('trigger()', this.timeout);
	
}
function stopTimeout() {
	clearTimeout(myTimeout);
}

function getNewsletterText(formfield, text) {
	if(formfield.value == text) {
		formfield.value = '';
	} else if(formfield.value == '') {
		formfield.value = text;
	}
}


