$(document).ready(function(){
	$('#access li.page_item a').wrap('<span />');
	
	// add indicator to menu
	$('#access .menu>ul>li').append('<div class="indicator"></div>');
	
	// add div to subpages widget
	$('#secondary .widget_subpages').append('<div class="btbar"></div>');
	
	// remove inline dimensions for fluid layout
	// $('figure').css('width', 'auto');
	
		
	// termin "bis"-datum - hide empty span 
	$("span.wplc_end_time").each(function(){
    if ($(this).html().length>5) {
    }
    else {
    $(this).hide();
    }
	});

	// shuffle elements

	 $.fn.shuffle = function() {

        var allElems = this.get(),
            getRandom = function(max) {
                return Math.floor(Math.random() * max);
            },
            shuffled = $.map(allElems, function(){
                var random = getRandom(allElems.length),
                    randEl = $(allElems[random]).clone(true)[0];
                allElems.splice(random, 1);
                return randEl;
           });

        this.each(function(i){
            $(this).replaceWith($(shuffled[i]));
        });

        return $(shuffled);

    };

	$('#sponsors #carousel li').shuffle();
	
	
	// sponsor logo carousel
	// http://caroufredsel.frebsite.nl
	$(window).resize(function() {
		var newWidth = $('#sponsors .inner').width();
		$('#sponsors #carousel').carouFredSel({
			width: newWidth,
			pauseOnHover : true,
			circular    : true,
			infinite    : true,
			direction   : "left",
			items	: {
			  start	: "random"
				},
			scroll	: {
				items	: 4,
				pauseOnHover  : true
				},
			auto		: {
				play			: true,
				items			: 1, // items moved at a move
				pauseDuration	: 4000,
				duration        : 1500,
				delay			: 0 // delay on start (negative is possible too)
			},
		});
	});
	$(window).resize();

});

// dropdown menu add hover-class
$(function(){
    $("#access .menu ul li").hover(function(){
    
        $(this).addClass("hover");
        $('ul:first',this).css('visibility', 'visible');
    
    }, function(){
    
        $(this).removeClass("hover");
        $('ul:first',this).css('visibility', 'hidden');
    
    });
	
    $("#access .menu ul>li:has(ul)").addClass("hassubs");
});


$('#access .menu ul li').append('<div class="indicator"></div>');


// child fix for IE
$(document).ready(function(){
	$("ul li:last-child").addClass("last");
	$("ul li:first-child").addClass("first");

});


// quickviewbox
$(document).ready(function(){
	//Hide (Collapse) the toggle containers on load
	$(".toggle-container").hide(); 

  $('a.trigger, a.close').click(function() {
  $('.toggle-container').toggleClass("active").slideToggle('slow', function() {
	  return false; //Prevent the browser jump to the link anchor
  });  
  
  $('a.close').click(function() {
  $('a.trigger').removeClass("open");
  });  
  
});

  $('a.trigger').click(function() {
      $(this).toggleClass("open");
    });
	
});



// Fluid Image Sizes
// http://unstoppablerobotninja.com/entry/fluid-images/
var imgSizer = {
	Config : {
		imgCache : []
	    ,spacer : "/wp-content/themes/otomo/images/spacer.gif"
	}

	,collate : function(aScope) {
		var isOldIE = (document.all && !window.opera && !window.XDomainRequest) ? 1 : 0;
		if (isOldIE && document.getElementsByTagName) {
			var c = imgSizer;
			var imgCache = c.Config.imgCache;

			var images = (aScope && aScope.length) ? aScope : document.getElementsByTagName("img");
			for (var i = 0; i < images.length; i++) {
				images[i].origWidth = images[i].offsetWidth;
				images[i].origHeight = images[i].offsetHeight;

				imgCache.push(images[i]);
				c.ieAlpha(images[i]);
				images[i].style.width = "100%";
			}

			if (imgCache.length) {
				c.resize(function() {
					for (var i = 0; i < imgCache.length; i++) {
						var ratio = (imgCache[i].offsetWidth / imgCache[i].origWidth);
						imgCache[i].style.height = (imgCache[i].origHeight * ratio) + "px";
					}
				});
			}
		}
	}

	,ieAlpha : function(img) {
		var c = imgSizer;
		if (img.oldSrc) {
			img.src = img.oldSrc;
		}
		var src = img.src;
		img.style.width = img.offsetWidth + "px";
		img.style.height = img.offsetHeight + "px";
		img.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "', sizingMethod='scale')"
		img.oldSrc = src;
		img.src = c.Config.spacer;
	}

	// Ghettomodified version of Simon Willison's addLoadEvent() -- http://simonwillison.net/2004/May/26/addLoadEvent/
	,resize : function(func) {
		var oldonresize = window.onresize;
		if (typeof window.onresize != 'function') {
			window.onresize = func;
		} else {
			window.onresize = function() {
				if (oldonresize) {
					oldonresize();
				}
				func();
			}
		}
	}
}

