$(function(){
	
	$.fn.updateGridDimensions = function()
	{
		var window_height = $(window).height()-15;
		var total_width = 0;
		var total_height = 0;
		$('ul#background-grid li img').each(function(){
			//total_width += $(this).width();
			total_width += $(this).attr("width");
			if($(this).attr("height") > total_height) {
				total_height = $(this).attr("height");
			}
			// if($(this).height() > total_height) {
			// 				total_height = $(this).height();
			// 			}
		});
		
		var ratio = window_height / total_height;
		var new_total_width = 0;
		var new_total_height = 0;
		
		$('ul#background-grid li img').each(function(){
			var new_width = $(this).attr("width")*ratio;
			var new_height = $(this).attr("height")*ratio;
			new_total_width += new_width;
			if (new_height > new_total_height){
				new_total_height = new_height;
			}
			$(this).attr({
				width:new_width,
				height:new_height
			});
			$(this).parent('li').show();
		});
		
		$('ul#background-grid').css({
			'width':new_total_width,
			'height':new_total_height
		});
		
				
	}
	
	$(window).bind('resize',function(){
		$.fn.updateGridDimensions();
	});
	
	$.fn.updateGridDimensions();
	
	// Customize select menu theme
	$('select#category_dropdown').selectmenu({
		style:'dropdown',
		width:160
	});
	

	
	// Handle the category change
	$('select#category_dropdown').bind('change', function(e) {
		e.preventDefault();
		window.location = "/" + $(this).val();
	});
	
	// Handle the info change
	$('select#info_dropdown').bind('change', function(e) {
		e.preventDefault();
		if ($(this).val() != "info"){
			window.location = "/" + $(this).val();
		}
	});
	
	$('.meta .title').bind('click', function(e){
		e.preventDefault();
		$(this).next('.info').toggle("blind");
		$(this).find('a').toggleClass("inactive");
	});
	
	$(document).ready(function(){
		if ($('body.news, body.stockists').length > 0){
			var scroller = null;
			var el = document.getElementById("scroller");
			scroller = new Scrolling.Scroller(el, 400, 410);
			$('.scroll-up a').bind('mouseover',function(e){
				scroller.startScroll(0, -5);
			});
			$('.scroll-up a').bind('mouseout',function(e){
				scroller.stopScroll();
			});
			$('.scroll-down a').bind('mouseover',function(e){
				scroller.startScroll(0, 5);
			});
			$('.scroll-down a').bind('mouseout',function(e){
				scroller.stopScroll();
			});
		}
		
		// Set the selected attribute on the first category dropdown item
		$('#category_dropdown').next('a').addClass('selected');
		
		// Hide the images
		//$('ul#background-grid li img').each(function(){
		//	$(this).css("opacity", 0);
		//});
		
	});
	
});
