$(document).ready(function(){
	if ( $.browser.msie ) {
		$("head").append("<link>");
		css = $("head").children(":last");
		css.attr({ rel:  "stylesheet", type: "text/css", href: "css/style-ie.css" });
	}
	else if ( $.browser.opera ) {
		$("head").append("<link>");
		css = $("head").children(":last");
		css.attr({ rel:  "stylesheet", type: "text/css", href: "css/style-opera.css" });
	}

	$('.sidebar .links a').click(function(){
		var href = $(this).attr("href");
		$('.sidebar .links li.active').removeClass("active");
		$(this).parent().addClass("active");
		$('.sidebar .galleries .gallery').hide();
		$(href).fadeIn();
		$('#body.gallery .picture .item.active').hide();
		$(href+"-pic").addClass("active").fadeIn();
		if ( $.browser.opera || $.browser.msie ) {
			if ( $(href+"-pic .pic span.image-wrapper").length == 0 ) {
				setTimeout( function(){ 
					$( href+' div.thumbs ul li:first a').trigger("click");
				},200);
			}
		}
		return false;
	})

	// First click	
	$('.sidebar .links ul li a:first').trigger("click");

	// Initially set opacity on thumbs and add
	// additional styling for hover effect on thumbs
	var onMouseOutOpacity = 0.67;
	$('#thumbs ul.thumbs li').opacityrollover({
		mouseOutOpacity:   onMouseOutOpacity,
		mouseOverOpacity:  1.0,
		fadeSpeed:         'fast',
		exemptionSelector: '.selected'
	});
	
	var captionOpacity = 0.0;
	$('.gallerific').each(function(){
		var id = $(this).attr('id');
		var $this = $(this);
		$this.find('div.thumbs').galleriffic({
		        delay:                     3000, // in milliseconds
		        numThumbs:                 9, // The number of thumbnails to show page
		        preloadAhead:              -1, // Set to -1 to preload all images
		        enableTopPager:            false,
		        enableBottomPager:         true,
		        maxPagesToShow:            5,  // The maximum number of pages to display in either the top or bottom pager
		        imageContainerSel:         '#'+id+'-pic .pic', // The CSS selector for the element within which the main slideshow image should be rendered
		        controlsContainerSel:      '#'+id+' .controls', // The CSS selector for the element within which the slideshow controls should be rendered
		        captionContainerSel:       '#'+id+'-pic .overlay', // The CSS selector for the element within which the captions should be rendered
		        loadingContainerSel:       '', // The CSS selector for the element within which should be shown when an image is loading
		        renderSSControls:          false, // Specifies whether the slideshow's Play and Pause links should be rendered
		        renderNavControls:         false, // Specifies whether the slideshow's Next and Previous links should be rendered
		        playLinkText:              '',
		        pauseLinkText:             '',
		        prevLinkText:              '',
		        nextLinkText:              '',
		        nextPageLinkText:          'Neste',
		        prevPageLinkText:          'Forrige',
		        enableHistory:             false, // Specifies whether the url's hash and the browser's history cache should update when the current slideshow image changes
		        enableKeyboardNavigation:  true, // Specifies whether keyboard navigation is enabled
		        autoStart:                 false, // Specifies whether the slideshow should be playing or paused when the page first loads
		        syncTransitions:           false, // Specifies whether the out and in transitions occur simultaneously or distinctly
		        defaultTransitionDuration: 500, // If using the default transitions, specifies the duration of the transitions
		        onSlideChange:             function(prevIndex, nextIndex) {
					// 'this' refers to the gallery, which is an extension of $('#thumbs')
					this.find('ul.thumbs').children()
						.eq(prevIndex).fadeTo('fast', onMouseOutOpacity).end()
						.eq(nextIndex).fadeTo('fast', 1.0);
				},
				onPageTransitionOut:       function(callback) {
					this.hide();
					callback();
				},
				onPageTransitionIn:        function() {
					var $this = this;
					var $pagination = $this.find(".pagination"); //$('#thumbs .pagination')
					if ( this.displayedPage != 0 ) {
						$pagination.find("a:first").remove();
					}
					var lastPage = this.getNumPages() - 1;
					if ( this.displayedPage != lastPage ) {
						$pagination.find("a:last").remove();
					}
					$pagination.prepend('<a class="buttonLink prev" title="forrige" href="#"></a>');
					$pagination.append('<a class="buttonLink next" title="Neste" href="#"></a>');
					this.find(".buttonLink.prev").click(function(){ $this.previousPage() })
					this.find(".buttonLink.next").click(function(){ $this.nextPage() })
					//this.fadeIn();
					this.fadeTo('fast', 1.0);
				}
		    });	
	});

	/*	
	var gallery = $('.gallerific .thumbs').galleriffic({
	        delay:                     3000, // in milliseconds
	        numThumbs:                 9, // The number of thumbnails to show page
	        preloadAhead:              -1, // Set to -1 to preload all images
	        enableTopPager:            false,
	        enableBottomPager:         true,
	        maxPagesToShow:            5,  // The maximum number of pages to display in either the top or bottom pager
	        imageContainerSel:         '#gallerific-pic .pic', // The CSS selector for the element within which the main slideshow image should be rendered
	        controlsContainerSel:      '#gallerific .controls', // The CSS selector for the element within which the slideshow controls should be rendered
	        captionContainerSel:       '#gallerific-pic .overlay', // The CSS selector for the element within which the captions should be rendered
	        loadingContainerSel:       '', // The CSS selector for the element within which should be shown when an image is loading
	        renderSSControls:          false, // Specifies whether the slideshow's Play and Pause links should be rendered
	        renderNavControls:         false, // Specifies whether the slideshow's Next and Previous links should be rendered
	        playLinkText:              '',
	        pauseLinkText:             '',
	        prevLinkText:              '',
	        nextLinkText:              '',
	        nextPageLinkText:          'Neste',
	        prevPageLinkText:          'Forrige',
	        enableHistory:             false, // Specifies whether the url's hash and the browser's history cache should update when the current slideshow image changes
	        enableKeyboardNavigation:  true, // Specifies whether keyboard navigation is enabled
	        autoStart:                 false, // Specifies whether the slideshow should be playing or paused when the page first loads
	        syncTransitions:           false, // Specifies whether the out and in transitions occur simultaneously or distinctly
	        defaultTransitionDuration: 500, // If using the default transitions, specifies the duration of the transitions
	        onSlideChange:             function(prevIndex, nextIndex) {
				// 'this' refers to the gallery, which is an extension of $('#thumbs')
				this.find('ul.thumbs').children()
					.eq(prevIndex).fadeTo('fast', onMouseOutOpacity).end()
					.eq(nextIndex).fadeTo('fast', 1.0);
			},
			onPageTransitionOut:       function(callback) {
				this.hide();
				callback();
			},
			onPageTransitionIn:        function() {
				var $this = this;
				var $pagination = $('#thumbs .pagination')
				if ( this.displayedPage != 0 ) {
					$pagination.find("a:first").remove();
				}
				var lastPage = this.getNumPages() - 1;
				if ( this.displayedPage != lastPage ) {
					$pagination.find("a:last").remove();
				}
				$pagination.prepend('<a class="buttonLink prev" title="forrige" href="#"></a>');
				$pagination.append('<a class="buttonLink next" title="Neste" href="#"></a>');
				this.find(".buttonLink.prev").click(function(){ $this.previousPage() })
				this.find(".buttonLink.next").click(function(){ $this.nextPage() })
				//this.fadeIn();
				this.fadeTo('fast', 1.0);
			}
	    });	
	*/
	
	
});
