$(document).ready(function(){
	
	var url;
	
	//Widow Resizing Code
	$(window).resize(function(){
		redraw();
	
	});
		
	//Cell selection code
	applyStyleToCells();
		
	limitSubCategories();
	
	$(".menu > div > div").load("./site-menu.html", function(){postMenuLoad()});
	
	giveAnchorsToCells();
	
	removeDeadImages();
	
	var src;
	
	$("a.lightbox").each(function(){
		src = $(this).children("img").attr('src');
	
		$(this).lightBox();
	
	});
	//Fjerner 1'ste elementet i breadCrumb
//	$(".bread-crumbs div a:first").hide();
	

});

function mailaddress(){
	mailwindow=window.open ("catmail.asp", "mailform",",width=420,height=100");
	mailwindow.moveTo(400,400);
}

function removeDeadImages(){

	$(".imgs img").each(function(){
		
		if ($(this).attr('src') == ''){

//Fjerner elementet
			$(this).parent('li').remove();
			
//Sūtter sti til default billede, ucomment linje ovenfor hvis du skal bruge den.			
//			$(this).attr('src', 'sti til billede')
			
		}
	
	});


}


function giveAnchorsToCells(){

	var location;
	
	location = window.location;

	$("div.content > ul > li").each(function(i){
		i++;
		$(this).attr('id', 'cell-'+i);
	})	
		
	redraw();

};





function postMenuLoad() {
$(".menu a").hover(function(){
			$(this).animate({'color': '#aaa'}, 100);
		
		},function(){
			$(this).animate({'color': '#fff'}, 100);
		});

		$(".menu ul li").each(function(){		
			if ($("body").attr('title') == $(this).children('a').attr('title') && $("body").attr('title')){
				$(this).children('a').css({'color':'#aaa'});
				$(this).css({'background' : 'url(./Images/generic/menu-pressed.png) repeat-x 0 -1'});
				
			};
			
		});
		
		
	
		$(".menu input.text").fadeTo(0, 0.8);
		
		
		$(".menu input.text").focus(function(){
			$(this).fadeTo(100, 1);
			
			$(this).css({'color':'#000'});
			
			if ($(this).attr('value') == 'S\u00F8g'){
				$(this).attr('value', '');}
		});
		$("ul.products li:nth-child(even)").css({'background-color':'#E9E9E9'});
		$("ul.products li:nth-child(odd)").css({'background-color':'#F0F0F0'});
		
	

		if(($(".specs li").length % 2) !=0){
			$(".specs ul").append('<li></li>');
		}
	
		$("body").show();	
	
		redraw();

}

function redraw() {
			
	
	if(($("div.detail").height() % 50)!=0){
		$("div.detail").css({'height':($("div.detail").height() + 50 - ($("div.detail").height() % 50))});
	}
	
	
	if(($("div.desc").height() % 50)!=0){
		$("div.desc").css({'height':($("div.desc").height() + 50 - ($("div.desc").height() % 50)-2)});

	}	
	
	
	//Margin on the left and right side
	var minMargin = 10;
	
	//total content width
	var maxWidth = 1024;

	if ($(window).width() > maxWidth){
	
	
	$("div.content ul.categories ").css({'width' : '100%'});
	$("div.content ul.products").css({'width' : '100%'});
	$("div.header").css({'width' : '100%'});
	$("div.menu").css({'width' : '100%'});
	$("div.detail").css({'width' : '100%'});
	$("div.bread-crumbs").css({'width' : '100%'});
	width = $(window).width()-maxWidth;			
	width = (width/2);
	
		if (width <= minMargin){
			$("div.detail div").css({'left' : minMargin});
			$("div.bread-crumbs div").css({'left' : minMargin});
			$("div.product").css({'left' : minMargin});
			$("div.page").css({'left' : minMargin});
			$("div.menu > div").css({'left' : minMargin});
			$("div.content ul.categories  li div").css({'left' : minMargin});
			$("div.content ul.categories  li img").css({'left' : minMargin+600});
			$("div.header h1").css({'right':	minMargin });
			$("div.header h2").css({'right':	minMargin });

		}else{
			$("div.detail div").css({'left' : width});
			$("div.bread-crumbs div").css({'left' : width});		
			$("div.product").css({'left' : width});
			$("div.page").css({'left' : width});
			$("div.menu > div").css({'left' : width});
			$("div.content ul.categories  li div").css({'left' : width});
			$("div.content ul.categories  li img").css({'left' : width+600});
			$("div.header img").css({'left': width+743 });
		}
	}else{
	$("div.product").css({'left' : minMargin});
	$("div.content ul.products > li > div").css({'width' : maxWidth-minMargin});
	$("div.detail div").css({'left' : minMargin});
	$("div.bread-crumbs div").css({'left' : minMargin});
	$("div.menu > div").css({'left' : minMargin});
	$("div.page").css({'left' : minMargin});
	$("div.content ul.categories li div").css({'left' : minMargin});
	$("div.content ul.categories  li img").css({'left' : minMargin+600});
	$("div.header img").css({'left': minMargin+743 });
	$("div.content ul.categories").css({'width' : maxWidth});
	$("div.content ul.products").css({'width' : maxWidth});
	$("div.header").css({'width' : maxWidth});
	$("div.menu").css({'width' : maxWidth});
	$("div.detail").css({'width' : maxWidth});
	$("div.bread-crumbs").css({'width' : maxWidth});

	}

}

function applyStyleToCells() {
	
	var cellCount;
	cellCount = $('div.content > ul.categories > li').size();

	if (cellCount <=0 && ($('div.content > ul.products > li').size() == 0)){
		$('div.content > ul.categories > li').addClass("big-cell")
	}else{
		$('div.content > ul.categories > li').addClass("small-cell")

	}
}

function limitSubCategories(){

	var categoryCount;
	var i;
	var url;
	
	var maxBigCellCategories = 1000;
	var maxSmallCellCategories = 1000;
	
	
	$("ul.categories > li > div > ul").each(function(i){
		categoryCount = $(this).children('li').size();

		url = $(this).siblings("h1").children("a").attr("href");
		
		if ($("li.small-cell").size()){
		
		if (categoryCount > maxSmallCellCategories){
		
			for(i=maxSmallCellCategories;i <= categoryCount ; i++){	
				$(this).children('li').eq(maxSmallCellCategories).remove();
			}
			
			$(this).append('<li><a href="' + url + '#cell-' + (maxSmallCellCategories+1) + '" style="_padding-top:3px;text-decoration:none">' + ' +' + (categoryCount - maxSmallCellCategories) + ' Kategorier' + '</a></li>');
		}};
		
		if ($("li.big-cell").size()){
		
			if (categoryCount > maxBigCellCategories){
		
			for(i=maxBigCellCategories;i <= categoryCount ; i++){	
				$(this).children('li').eq(maxBigCellCategories).remove();
			}
			
			$(this).append('<li><a href="' + url + '#cell-' + (maxBigCellCategories+1) + '" style="_padding-top:3px;text-decoration:none">' + ' +' + (categoryCount - maxSmallCellCategories) + ' Kategorier' + '</a></li>');
			}}
		});

};
