$(function() {
	//show and hide works #leftnav accordions	
	
	$("#leftnav").animate({
	  //  opacity: 0.20,
	    marginLeft: '-260px'
	  }, 800, function() {
	    // Animation complete.
	  });
	$("li>ul.submenu").slideUp(500).removeClass("shown");
	$(" #leftnav").mouseenter(function(){
		$(this).stop().animate({
		  //  opacity: 0.90,
		    marginLeft: '0px'
		  }, 500, function() {
		    // Animation complete.
		  });
		});

	$(" #leftnav").mouseleave(function(){
		$(this).stop().animate({
		 //   opacity: 0.20,
		    marginLeft: '-260px'
		  }, 500, function() {
		    // Animation complete.
			
		  });
		});
	
	
	//show ad hide thumbs panel
	$("#overlay").hover(
		function(){  
	        $(this).stop().animate({paddingRight: '25px'},500).addClass("shown"); 
		},
		function(){  
	        $(this).stop().animate({paddingRight: '0px'},500).removeClass("shown"); 
	});
	//show and hide thumbs
	$("#thumbs1 a").hover(
		function(){  
	        $(this).stop().animate({paddingRight: '22px'},500).addClass("shown"); 
		},
		function(){  
			if ($(this).hasClass('activeslide')){
				$(this).stop().animate({paddingRight: '22px'},500).addClass("shown");
				$(this).siblings().stop().animate({paddingRight: '0px'},500).removeClass("shown");
			} else {
			$(this).stop().animate({paddingRight: '0px'},500).removeClass("shown");
         }
	});
	
	$("#thumbs1 a").click(
		function(){  
			if ($(this).siblings().hasClass('activeslide')){
				$(this).siblings().stop().animate({paddingRight: '0px'},500).removeClass("shown");
         }
	});
	//show ad hide panel
	$("#infopanel").hover(
		function(){  
	        $(this).stop().animate({marginRight: '0px', easing: 'easeout'},1200).addClass("shown"); 
		},
		function(){  
	        $(this).stop().animate({marginRight: '-464px', easing: 'easeout'},1000).removeClass("shown"); 
	});
	
	$("#vdeoframe #infopanel").hover(
		function(){  
	        $(this).stop().animate({marginRight: '-16px', easing: 'easeout'},1200).addClass("shown"); 
		},
		function(){  
	        $(this).stop().animate({marginRight: '-464px', easing: 'easeout'},1000).removeClass("shown"); 
	});
	// Options for SuperBGImage
	$.fn.superbgimage.options = {
		id: 'superbgimage', // id for the containter
		z_index: 0, // z-index for the container
		inlineMode: 0, // 0-resize to browser size, 1-do not resize to browser-size
		showimage: 1, // number of first image to display
		vertical_center: 1, // 0-align top, 1-center vertical
		transition: 1, // 0-none, 1-fade, 2-slide down, 3-slide left, 4-slide top, 5-slide right, 6-blind horizontal, 7-blind vertical, 90-slide right/left, 91-slide top/down
		transitionout: 1, // 0-no transition for previous image, 1-transition for previous image
		randomtransition: 0, // 0-none, 1-use random transition (0-7)
		showtitle: 0, // 0-none, 1-show title
		slideshow: 0, // 0-none, 1-autostart slideshow
		slide_interval: 5000, // interval for the slideshow
		randomimage: 0, // 0-none, 1-random image
		speed: 'slow', // animation speed
		preload: 1, // 0-none, 1-preload images
		onShow: superbgimage_show, // function-callback show image
		onClick: superbgimage_click, // function-callback click image
		onHide: superbgimage_hide, // function-callback hide image
		onMouseenter: superbgimage_mouseenter, // function-callback mouseenter
		onMouseleave: superbgimage_mouseleave, // function-callback mouseleave
		onMousemove: superbgimage_mousemove // function-callback mousemove
	};

	// initialize SuperBGImage
	$('#thumbs1').superbgimage();

	// update superbgimage options
	function update_superbgOptions() {

		// speed
		var newspeed = 0;
		if (($("input[name='optspeed']").val() == 'slow') || ($("input[name='optspeed']").val() == 'normal') || ($("input[name='optspeed']").val() == 'fast')) {
			newspeed = $("input[name='optspeed']").val(); 
		} else {
			newspeed = parseInt($("input[name='optspeed']").val(), 10); 
			if (isNaN(newspeed)) {
				newspeed = 'slow';
				$("input[name='optspeed']").val('slow');
			}
		}
		
		// slidshow interval
		var newinterval = parseInt($("input[name='optinterval']").val(), 10); 
		if (isNaN(newinterval)) {
			newinterval = 5000;
			$("input[name='optinterval']").val(newinterval);
		}
		if ($.superbg_slideshowActive) { // restart slideshow
			clearInterval($.superbg_interval);
			return $('#thumbs').startSlideShow();
		}

		// transition out
		var newtransitionout = 0;
		if ($("input[name='opttransout']:checked").val() == 'on') {
			newtransitionout = 1;
		} else {
			newtransitionout = 0;
		}

		// random transition
		var newrandomtransition = 0;
		if ($("input[name='optrandomtrans']:checked").val() == 'on') {
			newrandomtransition = 1;
		} else {
			newrandomtransition = 0;
		}
		
		// random image
		var newrandomimage = 0;
		if ($("input[name='optrandom']:checked").val() == 'on') {
			newrandomimage = 1;
		} else {
			newrandomimage = 0;
		}

		// onclick-callback
		if ($("input[name='optclick']:checked").val() == 'on') {
			onclickfunc = superbgimage_click;
			$('#superbgimage img').each(function() { // add click-callback to all images
				$(this).unbind('click').click(function(){ superbgimage_click($(this).attr('rel')); });
			});	
		} else {
			onclickfunc = null;
			$('#superbgimage img').each(function() { // remove click-callback from all images
				$(this).unbind('click');
			});	
		}

		// onshow-callback
		if ($("input[name='optshow']:checked").val() == 'on') {
			onshowfunc = superbgimage_show;
		} else {
			onshowfunc = null;
		}
		
		// update options
		$.fn.superbgimage.options = { 
			transition: parseInt($("#transition").val(), 10),
			speed: newspeed,
			slide_interval: newinterval,
			transitionout: parseInt(newtransitionout, 10),
			randomtransition: parseInt(newrandomtransition, 10),
			randomimage: parseInt(newrandomimage, 10),
			onClick: onclickfunc,
			onShow: onshowfunc
		};
		
		
	
	}

	// hide options
	$("#options").css('height','15px').css('padding', '0px').addClass('hidden').children().hide();
	$("#options .legend").show();
	
	// hide set 2
	$("#thumbs2").hide().addClass('hidden');
	
	// fade overlay with controls, fade container to display titles
/*	$('#overlay').fadeTo('slow', 0.75);
	$('#showtitle').fadeTo('slow', 0.40);
	$('#showtitle').hover(
		function () {
			$(this).fadeTo('fast', 1.00);
		},
		function () {
			$(this).fadeTo('fast', 0.40);
		}
	);
*/
	// prev slide
	$('a.prev').click(function() {
		return $('#thumbs').prevSlide();
	});

	// next slide
	$('a.next').click(function() {
		return $('#thumbs').nextSlide();
	});

	// start slideshow
	$('a.start').click(function() {
		update_superbgOptions();
		return $('#thumbs').startSlideShow();
	});

	// stop slideshow
	$('a.stop').click(function() {
		my_slideshowActive = false;
		return $('#thumbs').stopSlideShow();
	});

	// load image set 1
	$('a.loadset1').click(function(){
		$('#thumbs1').stopSlideShow();
		$('#thumbs2').stopSlideShow();
		my_slideshowActive = false;
		$('#showtitle').hide();
		$('#thumbs2').hide().addClass('hidden');
		$('#thumbs1').superbgimage({ reload: true }).show().removeClass('hidden');
		return false;
	});

	// load image set 2
	$('a.loadset2').click(function(){
		$('#thumbs1').stopSlideShow();
		$('#thumbs2').stopSlideShow();
		my_slideshowActive = false;
		$('#showtitle').hide();
		$('#thumbs1').hide().addClass('hidden');
		$('#thumbs2').superbgimage({ reload: true }).show().removeClass('hidden');
		return false;
	});	
	
	// change transition with selectbox
	$("#transition").change(function() {
		update_superbgOptions();
	});	

	// change option speed
	$("input[name='optspeed']").change(function() {
		update_superbgOptions();
	});		
	
	// change option slide_interval
	$("input[name='optinterval']").change(function() {
		update_superbgOptions();
	});		

	// change option transitionout
	$("input[name='opttransout']").click(function() {
		update_superbgOptions();
	});	
	
	// change option randomtransition
	$("input[name='optrandomtrans']").click(function() {
		update_superbgOptions();
	});
	
	// change option randomimage
	$("input[name='optrandom']").click(function() {
		update_superbgOptions();
	});	

	// change option onClick-callback
	$("input[name='optclick']").click(function() {
		update_superbgOptions();
	});	
	
	// change option onShow-callback
	$("input[name='optshow']").click(function() {
		update_superbgOptions();
	});		

	// toggle fieldsets
	$(".legend").click(function() {
		if ($(this).parent().hasClass('hidden')) {
			$(this).parent().css('height', 'auto').css('padding', '10px').removeClass('hidden').children().show();
			$(this).show().css('display', 'block');
		} else {
			$(this).parent().css('height','15px').css('padding', '0px').addClass('hidden').children().hide();
			$(this).show().css('display', 'block');
		}
	});	

	// toggle overlay
	$("h1 a").click(function() {
		$(this).blur();
		if ($("#overlay").hasClass('hidden')) {
			$("#overlay").css('height','auto').removeClass('hidden').children().show();
			if ($('#thumbs1').hasClass('hidden')) {
				$('#thumbs1').hide();
			}
			if ($('#thumbs2').hasClass('hidden')) {
				$('#thumbs2').hide();
			}
		} else {
			$("#overlay").css('height','100px').addClass('hidden').children().hide();
			$("h1").show();
		}
		return false;
	});	
	
});

/*end document.ready*/

// function callback on hiding image
function superbgimage_hide(img) {
	// $('#showtitle').hide();
}

// function callback on showing image
// get title and display it
function superbgimage_show(img) {
    $('#thumbs1 a').each(function() {
        var id = $(this).html().replace(/<\/?(?!\!)[^>]*>/gi, '');
        if (id == img)
        {
            if ($(this).attr('data-bg') == 'dark')
            {
                $('body').addClass('dark');
            }
            else
            {
                $('body').removeClass('dark');
            }
        }

    });
//when image is shown, change contents of div#spec to match the title attribute of current #thumbs1 a
			$('#infobox #spec p').html($('#thumbs1 a' + "[rel='" + img + "']").attr('title'));
}

// function callback on clicking image, show next slide
function superbgimage_click(img) {
    if ($('#thumbs1 > a').size() > 1) {
        $('#thumbs').nextSlide();
    }
}

my_slideshowActive = false;

// function callback onmouseenter, stop slideshow, show pause-indicator
function superbgimage_mouseenter(img) {
	if ($.superbg_slideshowActive) {
		my_slideshowActive = true;
		if ($('#pause').length == 0) { 
			$('body').prepend('<div id="pause"><img src="pause.png" \/><\/div>');
		}
		$('#pause').css('position', 'absolute').css('z-index', 3).show();
		return $('#thumbs').stopSlideShow();
	}
}

// function callback onmouseleave, start slideshow, hide pause-indicator
function superbgimage_mouseleave(img) {
	if (my_slideshowActive && ($('#pause').length > 0) && ($('#pause').css('display') == 'block'))  { 
		$('#pause').hide();
		return $('#thumbs').startSlideShow();
	}	
}

// function callback onmousemove, show and move pause-indicator
function superbgimage_mousemove(img, e) {
	if (my_slideshowActive && ($('#pause').length > 0)) { 
		$("#pause").css("top",(e.pageY + 20) + "px").css("left",(e.pageX + 20) + "px").show();
	}
}



