jQuery.noConflict();
/*function alert(s){console.log(s);}*/

function InsertMailToTag( userName, domainName){
	var EmailId;
	var atSign = "&#64;"
	var fullStop = "&#46";
	
	EmailId = userName;
	EmailId = "" + EmailId + atSign; 
	EmailId = EmailId + domainName;
	
	document.write( "<a href='mail" + "to:" + EmailId + "'>" + EmailId +"</a>" );
}

function getInternetExplorerVersion() {
	var rv = -1; /* Return value assumes failure. */
	if (navigator.appName == 'Microsoft Internet Explorer') {
		var ua = navigator.userAgent;
		var re = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
		if (re.exec(ua) != null)
			rv = parseFloat(RegExp.$1);
	}
	return rv;
}

function checkVersion() {
	var isIE = false;
	var ver = getInternetExplorerVersion();
	if (ver > -1) {
		var isIE = true;
	}
	return isIE;
}
 
/* Put all your code in your document ready area */
jQuery(document).ready(function($){
	/* Do jQuery stuff using $
	$('.ui-selectmenu-menu').css({"position":"absolute", "overflow":"hidden"});
	$('.ui-selectmenu-status').css({"position":"absolute", "overflow":"hidden"}); */
	
	/* Update totals if clicked in yellow box, (As apposed to pressing enter) TODO Implement AJAX if time permits! */
	$('.cart_window .cart_prod_qty input').blur(function() {
		$(this).parent().submit();
	});
	/*
	$('cart_window .cart_prod_qty form').submit(function() {
		$ajax({
			url: 'index.php'
			date: serialize
		}); return false;
	} 
	*/ 		
	// Remove select from Country		
	$('#country_field option[value=0]').remove();
	
	/* JQuery UI DropDowns - Required to make them stick and not drop down the bottom page (Particularly in IE7) */
	if (checkVersion()) {
	
	} else {
		$('.ui-selectmenu-menu').css({"position":"absolute"});
		$('.ui-selectmenu-status').css({"position":"absolute"});     	
	}
	
	// button rounding
	function roundButtons(){
		if(!checkVersion()){
			$('.round').corner();
			$('.round').corner(); /* test for double rounding */
		}
	}
	
	// generic button fade attached to .jbtn with the live handler
	function createJbtn(){
		$('.jbtn').live('mouseover mouseout', function(event) {
			if (event.type == 'mouseover') {
				$(this).fadeTo("fast", 0.5)
			} else {
				$(this).fadeTo("fast", 1)
			}
		});
	}
	
	if($('.noTop').length){
		$('.itemBackToTop').remove();
	}
	
	// to black button fade
	$(".blackbtn").mouseover(function(){
		$(this).css({"backgroundColor" : "#000000","color":"#FFFEF3"});
	}).mouseout(function(){
		$(this).css({"backgroundColor" : "#FFFEF3","color":"#000000"});
	});
	
	$('#copy').prependTo($('#footerContent .menu')).wrap('<li style="margin-left:0;"></li>');
	
	$('select#jflanglistselect').selectmenu({
		style:'dropdown',
		width:130
	});
	
	/* style select element */
	
	$('#country_form').wrap('<li id="country_switch"></li>');
	$('#country_switch').appendTo($('#footerContent .menu'));
	
	$('#country_switch select').selectmenu({
		handleWidth: 20
	});
	
	$('select.inputboxquantity').selectmenu({
		
	});

	 $("select#ordering option[value='category']").remove();
	 $('select#ordering').selectmenu({
		style:'dropdown',
		width:90
	}).change(function(){
		this.form.submit()
	});
	
	$('select#limit').selectmenu({
		style:'dropdown',
		width:44
	});

   $('#miscItems_cont select').selectmenu({
		style:'dropdown',
		maxHeight:140,
		width:168
   });        
	
	$('#checkout_cont select').each(function(){
	if($(this).parent('.dateCell').length){
	   //$('#checkout_cont td.dateCell select').selectmenu('destroy');
	   $(this).selectmenu({
		 style:'dropdown',
		 maxHeight:140,
		 width:81
	   });          
	}
	else{
	  $(this).selectmenu({
		 style:'dropdown',
		 maxHeight:140,
		 width:168
	   });          
	}
  });
	
	/* Drop Downs 
	$('select#state').selectMenu({
		style:'dropdown',
		width: 600
	});
	$('select#country').selectMenu({
		style:'dropdown',
		width: 600
	});     	
	$('select#country_field').selectMenu({
		style:'dropdown',
		width: 600
	});      	
	*/

	$('#country_field').bind('change', function(){
		smallDelay = window.setTimeout(updateStates, 500);
	});
	
	function updateStates(){
		$('#checkout_cont select#state').selectmenu('destroy');
		$('#checkout_cont select#state').selectmenu({
			style:'dropdown',
			maxHeight:140,
			width:168
		}); //regenerate the custom select menu
		cleanup_selectmenus();
	}
	
	$('legend.sectiontableheader').remove();
	//$('#agreed_field').prependTo($('.agreed_field'));
	$('#agreed_field').prependTo($('#agreed_div'));
	$('#agreed_div strong').remove();
	
	// get rid of the home item from the menu
	// insert the cat_nav after the products item in the main nav
	//$('div.moduletable_menu .menu li:first:first-child').remove();
	//$('ul.sub_nav_list').insertAfter($("ul.menu li a span:contains('Shop Online')").closest('li')).wrap('<li class="sub_nav" />');
	
	//$('ul.menu li.active ul').insertAfter($("ul.menu")).wrap('<li class="sub_nav" />');
	
	//$('ul.menu li.active ul').each(
	// For each hottie, run this code. The "indIndex" is the
		// loop iteration index on the current element.
		//function(){
			// Bind the onclick event to simply alert the
			// iteration index value.
			//$(this).insertAfter($(this).closest('li.parent')).wrap('<li class="sub_nav" />');
		//}
	//);
	
	$('ul.menu li.active ul:first').addClass('sub_nav_list').insertAfter($('ul.menu li.active ul:first').closest('li.parent')).wrap('<li class="sub_nav" />');
	$('ul.sub_nav_list li').addClass('mainlevel');
	$('ul.sub_nav_list li ul li').addClass('sublevel');
	$('ul.sub_nav_list li ul').insertAfter($('ul.sub_nav_list li ul').parent()).children().unwrap();/**/
	
	// $('ul.sub_nav_list li ul li');
	// $("#active_menu ~ li.mainlevel:first").addClass("group_parent");
   
	if($('.thumb_list').length){
		$('.thumb_list').layout();
	}
	
	if($('.prods_window').length){
		if($('#product_list').outerWidth(false) > $('.prods_window').outerWidth(false)){

			// if there are more products than will fit in the view port
			// adjust the positions and neccessary styles of the involved elemens
			// prods_slider_left, prods_window, prods_slider_right
			$('.prods_slider_left').css({"left" : $('#cat_desc').position().left+$('#cat_desc').outerWidth(false)+30});
			$('.prods_slider_right').css({"left" : $('.prods_window').position().left+$('.prods_window').outerWidth(false)});
			prodsSliderLeftInit = $('.prods_window').position().left;
			$('.prods_window').css({"position": "absolute", "left" : $('#cat_desc').position().left+$('#cat_desc').outerWidth(false)+50});
			prodsWinInit = $('.prods_window').position().left;
			prodsWinWidthInit = $('.prods_window').outerWidth(false);
			// show the scroll left button and bind a function to it to animate the product list
			$('.show_all_prods').css({"display":"block"})
			$('.prods_slider_left').css({"display":"block"});
			
			// adjust the css of the .prods_window
			//$('.prods_window').css({"left":"-6px"});
			
			// showing events
			$('.show_all_prods').click(function(){
				showAllProds();
			});
			$('.show_all_prods').mouseenter(function(){
				showMoreProdsTab();
			});
			$('.show_all_prods').mouseleave(function(){
				hideMoreProdsTab();
			});
			$('.prods_slider_left').mouseenter(function(){
				showMoreProdsTab();
			});
			$('.prods_slider_left').mouseleave(function(){
				hideMoreProdsTab()
			});
			$('.prods_slider_left').click(function(){
				showAllProds();
			});
			
			//hiding events
			$('.prods_slider_right').mouseover(function(){
				showCloseProdsTab();
			});
			$('.prods_slider_right').mouseout(function(){
				hideCloseTab()
			});
			$('.prods_slider_right').click(function(){
				hideAllProds();
			});
		}
		//
	}
	
	function showAllProds(){
		$('.thumb_list').layout();
		$('.prods_slider_right').css({"display":"block"});
		$('#cat_desc').fadeOut('fast');
		$('.prods_slider_left').animate(
			{
				left:$('#prods_cont').position().left
			},
			{
				queue:true,
				duration:400
			}
		)
		$('.prods_window').animate(
			{
				left:$('#prods_cont').position().left+30,
				width:$('.prods_window').parent().outerWidth(false)-30
			},
			{
				queue:true,
				duration:400
			}
		)
	}
	
	function hideAllProds(){
		$('.prods_slider_right').css({"display":"none"});
		$('.prods_window').animate(
			{
				left:prodsWinInit,
				width:prodsWinWidthInit
			},
			{
				queue:true,
				duration:400
			}
		);
		$('.prods_slider_left').animate(
			{
				left:prodsSliderLeftInit
			},
			{
				queue:true,
				duration:400
			}
		);
		$('#cat_desc').fadeIn('slow');
		$('.thumb_list').layout();
	}
	
	function showMoreProdsTab(){
		//get the position of the placeholder element
		var pos = $(".prods_slider_left").offset();
		var width = $(".prods_slider_left").outerWidth(true);
		var height = $(".prods_slider_left").height();
		
		$('.more_prods').animate(
			{
				left: pos.left+width-8+"px",
				top:pos.top+(height/2)-($('.more_prods').outerHeight()/2)+"px",
				display: 'toggle'
			},
			{
				queue:true,
				duration: 0
			}
		)
	}
	
	function hideMoreProdsTab(){
		$('.more_prods').css({"display":"none"});
	}
	
	function showCloseProdsTab(){
		//get the position of the placeholder element
		var pos = $(".prods_slider_right").offset();
		var width = $(".prods_slider_right").outerWidth(false);
		var height = $(".prods_slider_right").height();
		
		$('.close_prods').animate(
			{
				left: pos.left-($('.close_prods').outerWidth(false)),
				top:pos.top+"px",
				display: 'toggle'
			},
			{
				queue:true,
				duration: 0
			}
		)
	}
	
	function hideCloseTab(target){
		$('.close_prods').css({"display":"none"});
	}
	
	/*********************************************************************/
	
	
	/** similar function to slide the subcategories through a view port **/
	if($('#prod_cats_cont').length){
		
		// if there are more categories than will fit in the view port
		if($('#prod_cats_cont .thumb_list').outerWidth(false) > $('#prod_cats_cont').outerWidth(false)){
			
			// position the right_scroller since it's absolute position means we need to move it
			var rightPos = $('#prod_cats_cont').offset().left+$('#prod_cats_cont').outerWidth(false);

			$('.cats_slider_left').css({'left':$('#prod_cats').offset().left});
			$('.cats_slider_right').css({'left':$('#prod_cats').offset().left+$('#prod_cats').outerWidth(false)-$('.cats_slider_right').outerWidth(false)});
			$('#prod_cats_cont').css({'overflow':'hidden'});
			// grab some vars
			//
			var listWidth = $("#prod_cats_cont .thumb_list").outerWidth(false);
			var viewWidth = $("#prod_cats_cont").width();
			var max = $("#prod_cats_cont .thumb_list").position().left;
			var min = max-(listWidth-viewWidth);
			// show the scroll left and scroll right buttons and bind
			// functions to them to animate the category list
			
			// Determine if slider Left or Right should be displayed to the user or not.
			// console.log("listWidth: " + listWidth + " ,viewWidth: " + viewWidth + " ,max: " + max + " ,min: " + min + " ,positionLeft: ");
			$("#prod_cats_cont .thumb_list").position().left;
			var positionLeft = $("#prod_cats_cont .thumb_list").position().left;
			var showLeft = positionLeft - max;
			var showRight = positionLeft - min;
			//console.log("showLeft: " + showLeft);
			//console.log("showRight: " + showRight);
			
			$('.cats_slider_left').css({"display":"block"});
			$('.cats_slider_right').css({"display":"block"});
			
			// animating events
			$('.cats_slider_left').mouseenter(function(){
				$('#scrollLeft').toggle();
				toggleCatsLeftTab($(this));
			});
			$('.cats_slider_left').mouseleave(function(){
				$('#scrollLeft').toggle();
				toggleCatsLeftTab($(this));
			});
			
			$('.cats_slider_left').click(function(){
				animateCats("right");
			});
			
			$('.cats_slider_right').mouseenter(function(){
				$('#scrollRight').toggle();
				toggleCatsRightTab($(this));
			});
			$('.cats_slider_right').mouseleave(function(){
				$('#scrollRight').toggle();
				toggleCatsRightTab($(this));
			});
			
			$('.cats_slider_right').click(function(){
				animateCats("left");
			});
		}
	}
	
	function toggleCatsLeftTab(targetEle){
		$('#scrollLeft').animate(
			{
				left: targetEle.position().left+targetEle.outerWidth(false)-8,
				top: targetEle.position().top+(targetEle.height()/2)-($('.more_prods').outerHeight()/2)+6
			},
			{
				queue:true,
				duration: 0
			}
		);
	}
	function toggleCatsRightTab(targetEle){
		$('#scrollRight').animate(
			{
				left: targetEle.position().left-$('#scrollRight').outerWidth(false)-6,
				top: targetEle.position().top+(targetEle.height()/2)-($('.more_prods').outerHeight()/2)+6
			},
			{
				queue:true,
				duration: 0
			}
		);
	}
	
	function animateCats(dir){
		// if the categories are animating return the fuction
		if( $('#prod_cats_cont .thumb_list').is(':animated') ) {
			return false;
		}
		// to animate the cat thumbs determine the width of the viewport
		// and the thumb_list along with max and min
		var posLeft = $("#prod_cats_cont .thumb_list").position().left;
		var moveAmount="";
		if(dir=="left"){
			if((posLeft-viewWidth)>min){
				moveAmount = viewWidth;
				//console.log("Left: (min)");
			}else{
				moveAmount = posLeft - min;
				//console.log("Left: ");
				//console.log("moveAmount: " + moveAmount + " ,posLeft: " + posLeft + " ,max: " + max);		    	
			}
		}
		if(dir=="right"){
			if((min-posLeft)>viewWidth){
				moveAmount = -viewWidth;
				//console.log("Right: (min)");
			}else{
				moveAmount = posLeft - max;
				//console.log("Right: ");
				//console.log("moveAmount: " + moveAmount + " ,posLeft: " + posLeft + " ,max: " + max);
			}
		}
		
		var height = $(".cats_slider_left").height();
		// determine how much to move the list by
		$("#prod_cats_cont .thumb_list").animate(
			{
				left: '-='+moveAmount
			},
			{
				queue:true,
				duration: 300
			}
		);

	}
	
	function setMiniCartScroll(){
		/* scroll mini cart if the number of products exceeds the avaiable space */
		if($('#miniCartList').length){
			$('#miniCartList').css({"width":$('#miniCartList').children().size()*150});
			
			if($('#miniCartList').outerWidth(false) > $('#miniCartWin').outerWidth(false)){
				$('#cartScroll').show();
			}
		}
	}
	setMiniCartScroll();

	$('#scrollCartLeft').live('click', function(){
		if($('#miniCartList').position().left<0){
			$('#miniCartList').animate({
							left: '+=150'
						},
						{
							queue:true,
							duration: 300
						});
		}
	});

	$('#scrollCartRight').live('click', function() {
		if($('#miniCartList').position().left>0-($('#miniCartList').outerWidth(false)-$('#miniCartWin').outerWidth(false))){
					$('#miniCartList').animate(
						{
							left: '-=150'
						},
						{
							queue:true,
							duration: 300
						}
					);
				}
	});

	
	if($('#cart_list').outerWidth(false) > $('.cart_window').outerWidth(false)){
		enableScrolling('#checkout_cont', '.cart_window', '#cart_list', $('#cart_list .thumb').outerWidth(true), true);
	}
	
	if($('#checkout_totals').length){
		$('#checkout_totals').insertAfter($('.vmCartModule'));
		$('.vmCartModule').hide();
	} else {
		$('.vmCartModule').show();
	}
	
	if ($('#shop_nav').length){
		$('#shop_nav').appendTo($('#checkout_totals'));
	}
	
	// functions to increase or decrease product amounts
	if($('.increase_product').length){
		$('.increase_product').click(function(){
			updateProdAmount($(this), 'inc');
		});
	}
	
	// functions to increase or decrease product amounts
	if($('.decrease_product').length){
		$('.decrease_product').click(function(){
			updateProdAmount($(this), 'dec');
		});
	}
	
	function updateProdAmount(obj, action){
		target = obj.parentsUntil('.thumb').find('.quantity');
		value = parseInt(target.val());
		if(action == 'inc'){
			target.val(parseInt(target.val())+1);
		}
		if(action == 'dec'){
			target.val(parseInt(target.val())-1);
		}
		// to submit the form target the form and use the .submit function
		target.parent().submit();
	}
	
	$('.remove_product').live('click', function() {
		var target = $(this).parent('form').find('.quantity');
		var value = parseInt(target.val());
		if(value < 1)
			value = 1;
		target.val(parseInt(target.val())-1);
		
		var form = $(this).parent('form');
		var dataString = '';
		form.children('input').each(function(){
			dataString+='&'+$(this).attr('name')+'='+$(this).attr('value');
		});
		dataString = dataString.substring(1);
		$.ajax({
			type: "POST",
			url: form.attr('action'),
			data: dataString,
			success: function(msg){
				updateMiniCarts();
			}
		});
	});


	if($('#checkoutAddAddress').length){
		$('#checkoutNext').insertAfter($('#checkoutAddAddress'));
	};
	
	/*if($('#loginLink').length){
		moveLogin();
	}*/
	
	function moveLogin(){
		$('#loginLink').prependTo($('#miniYourSelection'));
	}
	
	/* 
	 * Function to fade out unselected thumbs in browse 
	 */
	
	if($('.cart_window').length){ /* checkout screen no fade */
	
	} else {
		if($('.thumb').length){
			$('.thumb').hover(
				function () {
					if( !jQuery.browser.msie ) {
					$(this).animate(
							{opacity: 0.4},
							{ queue:false, duration:200 }
						)
					} 
				},
				function () {
					if( !jQuery.browser.msie ) {
					$(this).animate(
							{opacity: 1},
							{ queue:false, duration:200 }
						)
					}
				}
			);
		}				
		/* //Onfade preserve anti-aliasing			
		if (jQuery.browser.msie)						
			$('.browseProductDetailsContainer.span').removeAttribute('filter');	
		*/					
	}
	
	/* remove the unwanted links from the footer login */
	$('#lowerContent #vmLogin').remove();
	$('#lowerContent #vmLoginLinks legend').remove();
	$('#lowerContent .continue').remove();
	
	$('#lowerContent #vmLoginLinks ul').children().not(':last').remove();
	
	$('.c_content_h #vmLoginLinks ul').children(':last').remove();
	
	if($('#vmGreeting').length){
		$('ul.menu_loginmenu').remove();
		//$('#vmGreeting').after('<div style="float:left;margin:0 6px">|</div>');
	}
	if($('ul.menu_loginmenu').length){
		$('#lowerContent .login').css("margin-left","40px");	
	}
	
	/* remove content from the fulltext login */
	$('.itemFullText .logout').remove();
	
	/* validate the contact form */
	$('#ChronoContact_Contact textarea').css({"resize":"none"});
	$('#ChronoContact_Contact a.button').click(function(){
		$('#ChronoContact_Contact').submit();
	});
	
	$('#ChronoContact_Contact').validate({
		rules: {
			emailaddress_txt: {
				required: true,
				email: true
				}
			},
			messages: {
				emailaddress_txt: {
					required: "Please enter a valid email address"
				}
		}
	});
	
	/* validate the newsletter form */
	
	$('#ChronoContact_Newsletter a').click(function(){
		$('#ChronoContact_Newsletter').submit();
	});
	
	$('#ChronoContact_Newsletter').validate({
		rules: {
			emailaddress_txt: {
				required: true,
				email: true
				}
			},
			messages: {
				emailaddress_txt: {
					required: "Please enter a valid email address"
				}
		}
	});
	
	// modifying the map heading positions
	$('#mapOverlay').click(function(){
		hideMapOptions();
	});
	
	function positionMapLists(){
		$('#map_cont .subCategoryContainer').each( function(intIndex){
			$(this).css({
				"z-index":20,
				"top":$('#map_cont').position().top+($('#map_cont').height()/2)-($(this).height()/2),
				"left":$('#map_cont').position().left+($('#map_cont').width()/2)-($(this).width()/2)
			});
		});
	}
	positionMapLists();
	
	function positionMapheaders(){
		$('.mapRegionHeading').each(
			function(intIndex){
				thisLeft = $('#map_cont').position().left+mapHeadingArr[intIndex].left;
				thisTop = $('#map_cont').position().top+mapHeadingArr[intIndex].top;
				$(this).css({"left":thisLeft,"top":thisTop});
			}
		);
	}
	positionMapheaders();
	
	$('.mapRegionHeading').each(
		function(intIndex){
			$(this).click(function(){
				showMapOptions($('.' + this.id));
			});
			if($('#map_cont').hasClass(this.id)){
				showMapOptions($('.' + this.id));
			}
		}
	);
	
	function setCurrent(){
		$('.mapRegionHeading').each(
			function(intIndex){
				if($('#map_cont').hasClass(this.id)){
					showMapOptions($('.' + this.id));
				}
			}
		);
	}
		
	
	function showMapOptions(storeList){
		$('#mapOverlay').css({"cursor":"pointer","top":$('#map_cont').position().top+6,"left":$('#map_cont').position().left+4,"z-index":10, display:'block'});
		$('#mapOverlay').animate(
			{
				opacity: 0.5
			},
			200,
			function() {
				// Animation complete
				
				yOffset = ($('#map_cont').height()/2);
				xOffset = storeList.width()/2;
				storeList.css({"z-index":50, display:"block"});
				//storeList.css({"z-index":20,"top":$('#map_cont').position().top+($('#map_cont').height()/2)-(storeList.height()/2),"left":$('#map_cont').position().left+($('#map_cont').width()/2)-(storeList.width()/2)});
				storeList.animate(
					{
						opacity:1
					},
					200
				);
			}
		);
	}
		
	function hideMapOptions(){
		$('.subCategoryContainer').each(
			function(intIndex){
				$(this).css({"z-index":0});
				$(this).animate(
					{
						opacity:0
					},
					200,
					function(){
						$(this).css('visiblity','hidden');
					}
				);
			}
		);
		
		$('#mapOverlay').css({"cursor":"default","z-index":0});
		$('#mapOverlay').animate(
			{
				opacity: 0
			},
			200
		);
	}
	
	
	if($('#stores_cont #itemListLeading').length){
		$('#stores_cont #itemListLeading').layout();

		$('.lightboxlink').addClass('greyBtn round jbtn').attr('data-corner', '2px');
		enableScrolling('#stores_cont', '.c_content', '#itemListLeading', $('#stores_cont #itemListLeading .itemContainer').outerWidth(true), false);
	}
	
	/* end stores animation code */
	$('.catItemImageGallery h4').remove();
	$('.catItemImageGallery ul.sig-container-single li:first-child').each(
		// For each 'ul.sig-container-single', run this code. The "indIndex" is the counter
		// swap in the btn image for the thumbnail in the gallery
		function(){
			$('.sig-link-wrapper',this).replaceWith($('a',this));
			$('.sig-image',this).replaceWith('see gallery');
			$('.sig-pseudo-caption',this).remove();
			$('.sig-caption',this).remove();
			$('a',this).css({"height":"auto","width":"auto"});
			$('a',this).addClass('greyBtn round jbtn');
			$('a',this).attr('data-corner', '2px');
		}
	);
	
		
	// latest section adjustments
	if($('#catsItems_cont #itemListLeading').length){
		$('#catsItems_cont #itemListLeading').layout();
		enableScrolling('#catsItems_cont', '.itemList', '#itemListLeading', $('#catsItems_cont #itemListLeading .itemContainer').outerWidth(true), false);
	}
	
	// thinking page scroll bar
	if($('#thinking_cont .itemListSubCategories').length){
		$('#thinking_cont .itemListSubCategories').layout();
		enableScrolling('#thinking_cont', '.itemListCategoriesBlock', '.itemListSubCategories', $('#thinking_cont .itemListSubCategories .subCategoryContainer').outerWidth(true), false);
	}
	
	function positionScrollers(topLevel, viewPort, scroller, useFull){
		if(useFull == true){
			// the useFull parameter determines if the container has been created using building block .c_left and .c_right
			$(topLevel+' .slider_left').css({"left" : $(topLevel).position().left, "top" : $(topLevel).position().top,"display":"block"});
			$(topLevel+' .slider_right').css({"left" : $(topLevel).position().left + $(topLevel).width(),"top" : $(topLevel).position().top,"display":"block"});
		}else{
			$(topLevel+' .slider_left').css({"left" : $(topLevel+' .c_left').position().left,"display":"block"});
			$(topLevel+' .slider_right').css({"left" : $(topLevel+' .c_right').position().left-20,"display":"block"});
		}
		// position the indicator tabs
		$('#scrollLeft').css({
			"left": $(topLevel+' .slider_right').position().left-$('#scrollLeft').width()-6,
			"top": $(topLevel+' .slider_right').position().top+$(topLevel+' .slider_right').height()/2-$('#scrollLeft').height()/2
		});
		$('#scrollRight').css({
			"left": $(topLevel+' .slider_left').position().left+$(topLevel+' .slider_left').width()-6,
			"top": $(topLevel+' .slider_left').position().top+$(topLevel+' .slider_left').height()/2-$('#scrollRight').height()/2
		});
		// define the min and max scroll values
		scrollMax = $(topLevel + ' ' + scroller).position().left;
		scrollMin = $(topLevel + ' ' + scroller).position().left - $(topLevel + ' ' + scroller).outerWidth(false) + $(topLevel + ' ' + viewPort).width();
	}
	
	// function to enable scrolling on K2 categories
	function enableScrolling(topLevel, viewPort, scroller, scrollWidth, useFull){
		if($(topLevel + ' ' + scroller).outerWidth(false) > $(topLevel + ' ' + viewPort).outerWidth(false)+50){
			// show and position the scrollers
			positionScrollers(topLevel, viewPort, scroller, useFull);

			// add the events
			$(topLevel+' .slider_left').hover(function(){$('#scrollRight').show()},function(){$('#scrollRight').hide()});
			$(topLevel+' .slider_right').hover(function(){$('#scrollLeft').show()},function(){$('#scrollLeft').hide()});

			// animation functions
			$(topLevel+' .slider_right').click(function(){
				/*alert($(topLevel + ' ' + scroller).position().left+" "+scrollWidth+" "+scrollMin);
				alert($(topLevel + ' ' + scroller).outerWidth(false)+" "+$(topLevel + ' ' + viewPort).outerWidth(false));
				alert($(topLevel + ' ' + scroller).outerWidth(true)+" "+$(topLevel + ' ' + viewPort).outerWidth(true));
				alert($(topLevel + ' ' + scroller).position().left +" "+  $(topLevel + ' ' + scroller).width()+" "+$(topLevel + ' ' + viewPort).width());
				alert( " ");*/
				
				// if the item is animating at present return the function
				if( $(topLevel + ' ' + scroller).is(':animated') ) {
					return false;
				}
				
				// conditional animation
				if($(topLevel + ' ' + scroller).position().left - scrollWidth >= scrollMin){
					$(topLevel + ' ' + scroller).animate(
						{
							left: "-="+scrollWidth
						},
						{
							queue:true,
							duration: 300
						}
					);
				}
			});
			$(topLevel+' .slider_left').click(function(){
				if( $(topLevel + ' ' + scroller).is(':animated') ) {
					return false;
				}
				// conditional animation
				if($(topLevel + ' ' + scroller).position().left + scrollWidth <= scrollMax){
					$(topLevel + ' ' + scroller).animate(
						{
							left: "+="+scrollWidth
						},
						{
							queue:true,
							duration: 300
						}
					);
				}
			});
		}
	};
	
	if($('#catsItems_cont.itemView').length){
		$('#container, #catsItems_cont').css({"height":"auto"});
		$('.k2BreadcrumbsBlock').prependTo($('.itemHeader'));
		$('.k2BreadcrumbsBlock .bcTitle').remove();
		$('.k2BreadcrumbsBlock span:last-child').remove();
		$('.k2BreadcrumbsBlock span:last-child').remove();
	}else if($('#miscItems_cont.itemView').length){
		$('#container, #catsItems_cont').css({"height":"auto"});
		$('.k2BreadcrumbsBlock').prependTo($('.itemHeader'));
		$('.k2BreadcrumbsBlock .bcTitle').remove();
		$('.k2BreadcrumbsBlock span:last-child').remove();
		$('.k2BreadcrumbsBlock span:last-child').remove();
	}else{
		$('.k2BreadcrumbsBlock').remove();
	}
	
	/* thinking section mods*/
	if($('#thinking_cont .itemListSubCategories').length){
		$('#thinking_cont .itemListSubCategories').layout();
	}
	
	
	if($('#searchPage').length){
		$('#container').css("height","auto");
	};
	
	/*if($('#miscItems_cont.enquire .itemAttachmentsBlock').length){
		$('#miscItems_cont.enquire .itemAttachmentsBlock span').text('Is your enquiry commercial?');
	}*/
	
	/* error message positioning */
	
	$('.shop_error').insertAfter($('.checkoutHeader'));
	if($('#shopLogin').length){
		$('#system-message').appendTo($('#shopLogin'));
	}
	if($('#billingInfo').length){
		$('#system-message').appendTo($('#billingInfo'));
	}
	if($('#vmLogin').length){
		$('#system-message').appendTo($('#vmLogin'));
	}
	if($('#checkout_cont').length && !$('#shopLogin').length){
		$('#system-message').appendTo($('#adminForm'));
	}
	if($('#shop_intro_cont').length){
		$('#system-message').appendTo($('#shop_intro_cont'));
	}
	
	$("a.add_product").click( function(){
		// it submits the form it is contained within
		handleAddToCart( $(this).parents("form").attr('id') );
		/* setMiniCartScroll(); */
	});
	
	/*if($('#enquireLinks').length){
		$('.itemAttachmentsBlock').empty();
		$('.itemIntroText').prependTo($('.itemAttachmentsBlock'));
	}*/
	
	/* reposition functions */
	
	function content_resize(){
		if($('.mapRegionHeading').length){
			positionMapheaders();
			positionMapLists();
			$('#mapOverlay').css({"cursor":"pointer","top":$('#map_cont').position().top+6,"left":$('#map_cont').position().left+4,"z-index":10});
		}
		if($('.cats_slider_left').length){
			$('.cats_slider_left').css({'left':$('#prod_cats').offset().left});
			$('.cats_slider_right').css({'left':$('#prod_cats').offset().left+$('#prod_cats').width()-$('.cats_slider_right').width()});
		}
		if($('#stores_cont').length){
			positionScrollers('#stores_cont', '.c_content', '#itemListLeading', false);
		}
		if($('#cart_list').length){
			positionScrollers('#checkout_cont', '.cart_window', '#cart_list', true);
		}
	}

	$( window ).wresize( content_resize );
	
	cleanup_selectmenus();
	createJbtn();
	roundButtons();
	
});

function cleanup_selectmenus(){
	jQuery('.ui-selectmenu-menu').each(function(){
		jQuery(this).children('li').each(function(){
			if(jQuery(this).children('a').text() == 'undefined')
			  jQuery(this).remove();
		});
	});
}

