jQuery(document).ready(function($) {

		/* Cufon */
		Cufon.replace('.cufon, .searchResultsh1');

		/* Category boxes */
		$('.bottom-box').hover(function() {
			$(this).find('.2').show();
		}, function() {
			$(this).find('.2').hide();
		});
				
		/* Marquee */
		var marquee = $('#Marquee').marquee('marquee');		
		marquee.hover(function() {
			$(this).trigger('pause');
		}, function() {
			$(this).trigger('unpause');
		});
		
		/* Hacks */
		$('.search-button input').val('Søg');
		
		$('li#Li_CatItem11').click(function(event) {
			event.preventDefault();	
			up();			
		});
	
		$('#LeftColumn h2 em').replaceWith('<span>'+$('#LeftColumn h2 em').html()+'</span>');
		
		$('#FakeBottomCopyright, #BackgroundSlider').click(function() {
			up();	
		});
		
		$('#CloseInspiration').click(function() {
			down();
		});
	
		function up() {
			$('#Content').fadeOut(700);
			$('#Top').delay(700).animate({'marginTop':'-70px'}, 900);
			$('#FakeBottom').hide();
			$('.galleria-thumbnails-container').delay(700).animate({'margin-top':'-119px'}, 900);
			$('.galleria-image-nav-right, .galleria-image-nav-left').show();
			$('#CloseInspiration').delay(900).fadeIn(900);						
		}
		
		function down() {
			$('#CloseInspiration').hide();			
			$('.galleria-image-nav-right, .galleria-image-nav-left').hide();
			$('.galleria-thumbnails-container').animate({'margin-top':'0'}, 900);
			$('#Top').animate({'marginTop':'0'}, 900);			
			$('#Content').delay(900).fadeIn(700);
			$('#FakeBottom').delay(900).fadeIn();
		}
		
        /* Galleria init */
        Galleria.loadTheme('/upload_dir/themes/web/Unik/js/themes/fullscreen/galleria.fullscreen.min.js');
        $slider = $('#BackgroundSlider').galleria({
            dataSource: mainslides,
            transition: 'fade',
            transitionSpeed: 1000,
            preload: 3,
            autoplay: 10000,
            thumbnails: true,
            showImageNav: true,
            showCounter: false,
            showInfo: false,
            thumbFit: false,
            extend: function(options) {
                var gallery = this; // "this" is the gallery instance
                gallery.bind(Galleria.LOADSTART, function(e) {
                    if (gallery.hasInfo() && page_id == 1) {
                        var data = gallery.getData(e.index);
                        var cap = jQuery("#slidecaption");
                        jQuery(".title", cap).html(data.title);
                        jQuery(".content", cap).html(data.description);
                        Cufon.refresh();
                    }
                });
            },
            debug: true,
            imageCrop: true,
            _hideDock: true
        });	
        
    
    if ($('body').hasClass('bodyPageId2')) {
    /* Ajax webshop menu */
    var hash = location.hash;
    // Accordion for Category Box
    jQuery("#categorylist-box-content >ul").addClass('MagicWrapper').find('ul').hide();
    // Start up code
    jQuery("#categorylist-box ul a.current").next('ul').slideDown();
    // Init code
    jQuery("#categorylist-box ul a").click(function(e) {
        // Cancel default event
        e.preventDefault();
        // Animate
        var el = jQuery(this);
        var eldrop = jQuery(this).next('ul');
        if (el.hasClass('current')) {
            //el.removeClass('magic').slideUp();
        } else {
            // This category is a valid click
            
            // Is the click a parent node or a child node?
            if (el.parent().parent().attr('class') == 'MagicWrapper') {
                // Close everything
                jQuery("#categorylist-box ul a.current").next('ul').slideUp().parents('ul').not('ul.MagicWrapper').slideUp();
				$(this).parent().parent().find('.opened').removeClass('opened');
				$(this).parent().addClass('opened');
            }
            // Open new element
            eldrop.slideDown();
        }
        // Remove active marker
        jQuery("#categorylist-box li a.current").removeClass('current');
        // Add active marker
        jQuery(this).addClass('current').parents('li').find('>a').addClass('current');
        // We need to fetch its content with AJAX
        var newUrl = jQuery(this).attr('href');
        loadSite(newUrl);
    });    
    
    loadSite(null, true);
    setInterval(function()
    {
        if (location.hash != hash)
        {
            loadSite();
            hash = location.hash;
        }
    }, 100);

    }
    
    function loadSite(url, activateLink) {
		if (url == null) {
			url = window.location.hash;
			if (url != '') {
				url = url.substring(1);
			}
		}
		if (url.length > 0) {
			jQuery("#MainContentWrapper").load(url + ' #MainContent', function() {
				window.location.hash = '#' + url;				
				hash = '#' + url;
				$('.categories-listing-item').each(function(index) {
					$(this).addClass(getNextColor());
				});
				$('.categories-listing-wrapper').each(function(index) {
					if ((index+1) % 3 == 0) {
						$(this).css('margin-right','0');	
					}						
				});
				if (activateLink) {
					jQuery("#categorylist-box ul a").each(function() {
						if (jQuery(this).attr('href') == url) {
							jQuery(this).parents('li').find('>a').addClass('current');
							jQuery(this).next('ul').addClass('magic').slideDown();
							jQuery(this).parents('ul').not('ul.MagicWrapper').addClass('magic').slideDown();
						}
					});
				}
				Cufon.refresh();
			});
		}
	}        
        
	/* Webshop category juggling */
	var heightForOne = 0;
	var initialHeight = 250;
	var newHeight = 0;
	var origText = 'Se alle <strong>kategorier</strong> <img style="position:relative; top:-2px;" src="/upload_dir/themes/web/Unik/graphics/arrowcat.png" alt="Arrow" />';
	var newText = 'Vis kun 6 <strong>kategorier</strong> <img style="position:relative; top:-2px;" src="/upload_dir/themes/web/Unik/graphics/arrow.jpg" alt="Arrow" />';
	

	$('#ViewAll').css('cursor', 'pointer');
		jQuery('#ViewAll')
		.live('click', function(e) {
			var btnEl = jQuery("#ViewAllBtn");
			var el = jQuery("#categories-listing");
			console.log(el);
			if (el.hasClass('expanded')) {
				// Is expanded
				el.animate({
					height: initialHeight
				}, 500, function() {
					btnEl.html(origText);
					el.removeClass('expanded');
				})
			} else {
				// Is not expanded
				var items = jQuery(">div.categories-listing-wrapper", el);
				initialHeight = el.height();
				heightForOne = 120 + (parseInt(el.css('padding-bottom')));
				newHeight = (Math.ceil(items.length / 3) * heightForOne) + 28;
				el.animate({
					height: newHeight
				}, 500, function() {
					btnEl.html(newText);
					el.addClass('expanded');
				});
			}
		});


	
	/* Webshop category color juggling */
	$('.categories-listing-item').each(function(index) {
			$(this).addClass(getNextColor());
	});

	/* Webshop category margin hack */
	$('.categories-listing-wrapper').each(function(index) {
			if ((index+1) % 3 == 0) {
				$(this).css('margin-right','0');	
			}						
	});
	
	function getNextColor() {
		var count = colors.length;
		var index = Math.floor(Math.random()*count);
		var color = ''+colors.splice(index, 1);
		used[6-count] = color; 
		if (used.length == 6) {
			colors = used.slice(0);
			used = Array();
		}
		if (color == last) {
			var count = colors.length;
			var index = Math.floor(Math.random()*count);
			var color = ''+colors.splice(index, 1);
			used[6-count] = color; 			
		}
		last = color;
		return color;	
	}
    
    //$('#categorylist-box-content ul ul').css('display', 'block');
    
    jQuery("#HeaderContentCartPopupContent #CartBTN").append('<div class="buttonX"><dl><dt class="left"></dt><dt class="middle"><div class="ahref"><a title="Til indk&oslash;bskurv" alt="Til indk&oslash;bskurv" href="/kurv/">Til indk&oslash;bskurv</a></div></dt><dt class="right"></dt></dl></div>');
  
});

	/* Category color vars */
	var last = '';
	var used = new Array();
	var colors = new Array(6);
		colors[0] = 'cat-box-1';
		colors[1] = 'cat-box-2';
		colors[2] = 'cat-box-3';
		colors[3] = 'cat-box-4';
		colors[4] = 'cat-box-5';
		colors[5] = 'cat-box-6';

	/* Galleria vars */
    var page_id = '1';

    if (!Array.prototype.shuffle) {
        Array.prototype.shuffle = function() {
            var s = [];
            while (this.length) s.push(this.splice(Math.random() * this.length, 1)[0]);
            while (s.length) this.push(s.pop());
            return this;
        }
    }

    var activeslides = [];
    
    var mainslides = [];

    mainslides = mainslides.shuffle();     
    

