$(document).ready(function() {
;(function($){
/**---------------------------
Ravstedhus Content Height Calculator v0.1
----------------------------**/
	function calContentMinHeight() {
		var minHeight = $(window).height() - $('#header_container').outerHeight() - $('#footer_container').outerHeight() - ($('#content_container').outerHeight() - $('#content_container').height());
		$('#content_container').css('min-height',minHeight);
	}
/**---------------------------
Ravstedhus Header Expanders Calculator v0.1
----------------------------**/
	function calExpandersWidth() {
		if ($(window).width() < $('#header_container .content').width()) {
			var width = 0;
		}
		else {
			var width = ($(window).width() - $('#header_container .content').width())/2;
		}
		width = Math.ceil(width);
		$('.header_right_expander').width(width);
		$('.header_left_expander').width(width);
	}
/**---------------------------
GOOD DEAL GRAPHIC v0.1
----------------------------**/	
	function showGoodDealGraphic() {
		$('#ShopContent .prsi').has('img').show();
	}
/**---------------------------
PRIM SUB NAVIGATION ADDITIONS v0.1
----------------------------**/	
	var subHideTimer;
	function createPrimSubNavigation() {
		//remove previous listeners
		$('#ddMenu span').removeAttr('onmouseover');
		$('#ddMenu span').removeAttr('onmouseout');
		$('body').append('<div class="dropdown_nav"></div>');
	
		$('#ddMenu span > a').hover(function(){
			//show that sub captain!
			//if ($(this).next().html() != null) {
				if ($(this).next().html() != null) {
					showPrimSub(this);
				}
			//}
		},function(){
			//hide that sub captain!
			hidePrimSub();
		});
		$('.dropdown_nav').hover(function(){
			//show that sub captain!
			showPrimSub();
		},function(){
			//hide that sub captain!
			hidePrimSub();
		});
	}
	
	function showPrimSub(object) {
		clearTimeout(subHideTimer);
		if (object != null) {
			$('.dropdown_nav').html($(object).next().html());
			$('.dropdown_nav').css('left',$(object).offset().left);
			$(".dropdown_nav").show();
		}
	}
	
	function hidePrimSub() {
		//$('.dropdown_nav').stop().slideUp('fast');
		subHideTimer = setTimeout(function(){ 
			$(".dropdown_nav").hide();
			clearTimeout(subHideTimer);
		},500);
	}
/**---------------------------
PRODUCT LIST HEIGHT ALTERATION
----------------------------**/	
	function runThroughAndCalHeights() {
		$('.plist .plistRowOdd,.plist .plistRowEven').each(function(){
			var heightHeight = 0;
			$(this).find('.prelement .primg img').each(function(){
				var height = $(this).height();
				if (height > heightHeight) {
					heightHeight = height;
				}
			});
			if (heightHeight < 150) {
				var diff = 150 - heightHeight;
				var newHeight = $(this).find('.prelement').height() - diff;
				//prelement
				$(this).find('.prelement').css('height',newHeight);
				//primg
				$(this).find('.primg').css('height',$(this).find('.primg').height() - diff);
			}
		});
	}
/**---------------------------
PAGE TITLE ABOVE SUBNAV
----------------------------**/		
	function setPageTitleInSub() {
		$('.left_pane h2:first-child').text('Menu');
	}
/**---------------------------
TABLE CELLPADDING AND CELLSPACING
----------------------------**/		
	function removeCellSpace() {
		$('#myShoppingCart table').attr('cellspacing','0');
		$('#myShoppingCart table').attr('cellpadding','0');
	}
/**---------------------------
CHANGE PROFILE
----------------------------**/		
	function changeProfileSetup() {
		var someArray = (window.location.host.split('.'));
		var urlStringNoSub;
		
		if ((someArray[0] == 'private') || (someArray[0] == 'pro') || (someArray[0] == 'off')) {
			someArray.shift();
		}
		else if (someArray[0] == 'www'){
			someArray.shift();
		}
		else {
		
		}
		urlStringNoSub = someArray.join('.');
		
		//SET PRIVATE
		$('.profiles .pri a').attr('href','http://www.'+urlStringNoSub);
		//SET PRO
		$('.profiles .pro a').attr('href','http://pro.'+urlStringNoSub);
		//SET OFF
		$('.profiles .off a').attr('href','http://off.'+urlStringNoSub);
	}
/**---------------------------
PRODUCT DETAIL ZOOM
----------------------------**/		
	function productDetailZoomFunction() {
		$('.productimage .ucImageControltrImg').find('img').attr('onclick','');
		$('.productimage .ucImageControltrImg').click(function(){
			//Show large img
		});
	}
/**---------------------------
REMOVE BREAK LINE IN EMPTY CMS EDITOR
----------------------------**/		
	function removeBrInEmptyCms() {
		if ($('#layoutCmsbdy').length > 0) {
			if ($('#layoutCmsbdy').text() == '') {
				$('#layoutCmsbdy').find('br').remove();
			}
		}
	}
/**---------------------------
BASKET ALTERATIONS
----------------------------**/	
	function alterBasketStyle() {
		$('.step2 td.content').each(function(){
			//if (!$(this).text().trim().length) {
			if (!$.trim($(this).text()).length) {
				$(this).css('height','0');
				$(this).css('overflow','hidden');
			}
		});
		$('.step2  table.content table td').has('td:contains("Leveringsadresse")').css('padding-top','59px');
		
		//remove password box
		$('.step2 td').has('#ctl00_plhContent_txtNewNavisionPassword').remove();
		
		//center delete td in first step
		$('#ctl00_plhContent_ShoppingCartPanel table.HeaderBarTables tr').each(function(){
			$(this).find('td:last').css('text-align','center');
		});
		
		$('#myShoppingCart #Table1 td').each(function(){
			var textLength = $.trim($(this).text());
			if (textLength == 0) {
				$(this).css('display','none');
				//$(this).css('background','red');
				//$(this).css('height','0 !important');
			}
		});
		$('#myShoppingCart table table').find('tr td:first-child').css('text-align','left !important');
		$('#myShoppingCart #Table1 tr td').css('border-bottom','1px solid #ccc');
		
		//remove empty td in step3
		$('.step3 td').each(function(){
			//if (!$(this).text().trim().length) {
			if (!$.trim($(this).text()).length) {
				//this removes go to payment as well
				//$(this).css('display','none');
			}
		});
		//align moms right in step 3
		$('.HeaderBarTables#Table2').next().css('float','right');
		$('.HeaderBarTables#Table2').next().find('td').css('text-align','right');
	}
/**---------------------------
INITIATE FUNCTIONS AND LISTENERS
----------------------------**/
	calContentMinHeight();
	calExpandersWidth();
	changeProfileSetup();
	removeCellSpace();
	showGoodDealGraphic();
	createPrimSubNavigation();
	$(window).resize(function() {
		calContentMinHeight();
		calExpandersWidth();
	});
	runThroughAndCalHeights();
	setPageTitleInSub();
	alterBasketStyle();
	removeBrInEmptyCms();
	//productDetailZoomFunction();

/** CHANGE LOG 
v0.1
- PROTOTYPE



-------------------------**/
})(jQuery);
});
