jQuery(document).ready(function() {

	/*
		Impromptu plugin default settings
	*/
	jQuery.prompt.setDefaults({
		opacity: 0.5, 
		loaded: function() {
			
			jQuery("div.jqi .jqibuttons button").wrapInner('<span></span>');
			
		}
	});
	
	/*
		NyroModal plugin default settings
	*/
	jQuery.nyroModalSettings({
		wrap: { // Wrapper div used to style the modal regarding the content type
			div: '<div class="wrapper"></div>',
			ajax: '<div class="wrapper"></div>',
			form: '<div class="wrapper"></div>',
			formData: '<div class="wrapper"></div>',
			image: '<div class="wrapperImg"></div>',
			gallery: '<div class="wrapperImg"><a href="#" class="nyroModalPrev">Prev</a><a href="#"  class="nyroModalNext">Next</a></div>', // Use .nyroModalPrev and .nyroModalNext to set the navigation link
			swf: '<div class="wrapperSwf"><div class="button_container_red" style="z-index: 10000; float:right; margin: 0"><a href="#" class="nyroModalClose"><span>X</span></a></div></div>',
			iframe: '<div class="wrapperIframe"></div>',
			manual: '<div class="wrapper"></div>'
		},
		minWidth: 150, // Minimum width
		minHeight: 150, // Minimum height
		closeButton: '', // Adding automaticly as the first child of #nyroModalWrapper
		endShowContent: function( elts, settings ) {
			
			if( settings.type == "image" ) {
				
				elts.wrapper.prepend( '<a href="#" class="nyroModalClose" id="closeBut" title="close">Close</a>' );
				
				jQuery("#closeBut").click( function () {
					
					jQuery.nyroModalRemove();
					
				});
				
			}
			
			var nyroModalTabindex = 99999999;
	
			$("[tabindex]").each(function () {
			
				if( parseInt( $(this).attr("tabindex") ) < nyroModalTabindex && parseInt( $(this).attr("tabindex") ) > 999 ) {
				
					nyroModalTabindex = parseInt( $(this).attr("tabindex") );
				
				}
			
			});
			
			$("[tabindex=" + nyroModalTabindex + "]").focus();
		
		},
		endRemove: function() {
			
			$.post(
				'buy.asp',
				{
					Clean: '1'
				}
			);
		
		}
		
	});
	
	/*
		ISOLDA
	*/	
	$("div.isolda:first").find("select[name=ManufacturerCode]").val( "false" );
	
	$("div.isolda").find("form:first").find("a").click(function () {
	
		$(this).parents("form:first").submit();
	
	});
	
	$("div.isolda").find("select[name=ManufacturerCode]").change(function () {
	
		$(this).parents("div.isolda:first").find("select[name=ProductCode]").attr("disabled", true).val( $(this).parents("div.isolda:first").find("select[name=ProductCode]").find("option:first").val() );
		
		$(this).parents("div.isolda:first").find("select[name=ProdGrpCode]").load( 
			"tonersAJAX.asp",
			{
				op: "get_ProdGrpList_byManufacturer",
				ManufacturerCode: $(this).val()
			}
		);
		
		if( $(this).val() == "false" ) {
		
			$(this).parents("div.isolda:first").find("select[name=ProdGrpCode]").attr("disabled", true);
		
		} else {
		
			$(this).parents("div.isolda:first").find("select[name=ProdGrpCode]").removeAttr("disabled");
		
		}
	
	});
	
	$("div.isolda").find("select[name=ProdGrpCode]").change(function () {
	
		$(this).parents("div.isolda:first").find("select[name=ProductCode]").load( 
			"tonersAJAX.asp",
			{
				op: "get_ProductList_byManufacturerAndProdGrp",
				ManufacturerCode: $(this).parents("div.isolda:first").find("select[name=ManufacturerCode]").val(),
				ProdGrpCode: $(this).val()
			}
		);
		
		if( $(this).val() == "false" ) {
		
			$(this).parents("div.isolda:first").find("select[name=ProductCode]").attr("disabled", true);
		
		} else {
		
			$(this).parents("div.isolda:first").find("select[name=ProductCode]").removeAttr("disabled");
		
		}
	
	});
	
	/*
		Flashbar
	*/
	/*
	$("div#message").slideDown("normal").animate({ opacity: 1.0 }, 10000).slideUp("normal");
	*/
	
	/*
		Enterkey to submit all forms
	*/
	
	$(".submit_form").keypress(function (e) {
	
		if( e.which == 13 ) {
		
			if( $(this).parents("form:first").hasClass("nyroModal") ) {
				
				if( $(this).hasClass("dontShowBackground") ) {
					
					$(this).parents("form:first").nyroModalManual({
						showBackground: dontShowBackground
					});
					
				} else {
					
					$(this).parents("form:first").nyroModalManual();
				
				}
			
			} else {
				
				$(this).parents("form:first").submit();
				
			}
			
		}
	
	});
	
	/*
		Buy-related
	*/
	
	$(".buybutton").click(function() {
	
		var theButton = $(this);
		
		$(this).parents("form:first").nyroModalManual({
			showBackground: dontShowBackground
		});
	
	});
	
	$(".buyallbutton").click(function() {
		
		var queryString = "";
		
		jQuery("div#content_wrapper").find("div.product").find("form.nyroModal").each(function() {
			
			if( jQuery.trim( jQuery(this).find("input[name=qty]").val() ) != "" ) {
				
				if( queryString == "" ) {
					
					queryString += "artnr=" + $(this).find("input[name=artnr]").val() + "&qty=" + $(this).find("input[name=qty]").val();
					
				} else {
					
					queryString += "&artnr=" + $(this).find("input[name=artnr]").val() + "&qty=" + $(this).find("input[name=qty]").val();
					
				}
				
			}
			
		});
		
		$.nyroModalManual({
			url: 'buy.asp?' + queryString,
			showBackground: dontShowBackground
		});
		
	});
	
	/*
		Save to favourite list -related
	*/
	jQuery(".AddItemToTmpl").click(function () {
		
		jQuery.nyroModalManual({
			ajax: {
				url: 'AddItemToTmpl.asp',
				type: 'POST',
				data: {
					artnr: $(this).prevAll("form:first").find("input[name=artnr]").val(),
					webbeskr: $(this).prevAll("form:first").find("input[name=webbeskr]").val()
				}
			}
			
		});
		
	});
	
	/*
		productInfo pop-up
	*/
	$(".productInfo").click(function () {
		
		$.nyroModalManual({
			forceType: 'iframe',
			minWidth: 800,
			url: 'productInfo.asp?artnr=' + $(this).prevAll("form:first").find("input[name=artnr]").val(),
			endResize: function () {
				
				$("iframe#nyroModalIframe").contents().find("body").find(".print").click(function () {
					
					//Save original values of body and it's children, with the exception of the nyroModal (which we of course won't touch)
					bodyWidth = $("body").css("width");
					
					var displayValues = [];
					
					elementsVisible = $("body").children().filter(function () {
						
						return ( $(this).css("display") != "none" && $(this).attr("id") != "nyroModalFull" );
						
					});
					
					elementsVisible.each(function () {
						
						displayValues.push( $(this).css("display") );
						
					});

					//Set width to iframe's width (to set the proper printing area)
					$("body, html").css( "width", $("iframe#nyroModalIframe").width() + "px" );
					//Set all other elements to not display, to avoid that elements inside pushes the width of the body/page to be bigger than the iFrame's.
					elementsVisible.css( "display", "none");
					
					//Print iFrame
					nyroModalIframe.print();
					
					//Reset all values to their original
					var i = 0;
					elementsVisible.each(function () {
						
						$(this).css("display", displayValues[i] );
						
						i = i + 1;
						
					});
					$("body, html").css( "width", bodyWidth );
					
				});
				
			}
		});
		
	});
	
	/*
		Print
	*/
	$(".print").live("click", function () {
		
		window.print();
		
	});
	
	/*
		main_navigation.asp - Logga in / Logga ut
	*/
	
	$("div#nav_level_0").find("button.cancel").click(function() {
		
		$(this).siblings("form").submit();
		
	});
	
	/*
		left_navigation.asp - START
	*/
	
	var isFirst = true;
	var regExp
	
	$(".nav_level_1").click(function () {
	
		isFirst = true;
	
	});
	
	$(".nav_level_1").find("li").find("a").click(function () {
	
		if(isFirst) {
		
			if($(this).parents("li:first").is(":has(li)")) {
			
				if($(this).parents("li:first").hasClass("expanded")) {
				
					$(this).parents("li:first").removeClass("expanded");
					$(this).parents("li:first").find("ul:first").slideUp("fast");
					$.cookie('iMenuPosition', $(this).parents("li:first").parent().parent("li").attr("id"));
					
				} else {
				
					$(this).parents("li:first").addClass("expanded");
					$(this).parents("li:first").find("ul:first").slideDown("fast");
					$.cookie('iMenuPosition', $(this).parents("li:first").attr("id"));
					
					$(".nav_level_1").find("li.expanded").not(":has(li#" + $(this).parents("li:first").attr("id") + ")").not("#" + $(this).parents("li:first").attr("id")).each(function () {
						$(this).removeClass("expanded");
						$(this).find("ul:first").slideUp("fast");
					});
				
				}
			
			} else {
				
				$.cookie('iMenuPosition', $(this).parents("li:first").attr("id"));
			
			}

			isFirst = false;
			
		}
	
	});
	
	/*
		Set focus
	*/
	var tabindex = 99999999;
	
	$("[tabindex]").each(function () {
	
		if( parseInt( $(this).attr("tabindex") ) < tabindex && parseInt( $(this).attr("tabindex") ) > 0 ) {
		
			tabindex = parseInt( $(this).attr("tabindex") );
		
		}
	
	});
	
	$("[tabindex=" + tabindex + "]").focus();
	
	/*
		Sök görs med bra länk
	*/
	/*
	$("form[name=frmTopSearch]").submit(function () {
	
		$(this).attr( "action", $(this).attr( "action" ) + "&mode=1&searchstring=" + $(this).find("input[name=searchstring]").val() );
		$(this).submit();
	
	});
	*/
	
});

$(function() {
	
	/*
	$.nyroModalSettings({
		wrap: { // Wrapper div used to style the modal regarding the content type
			div: '<div class="wrapper"></div>',
			ajax: '<div class="wrapper"></div>',
			form: '<div class="wrapper"></div>',
			formData: '<div class="wrapper"></div>',
			image: '<div class="wrapperImg"></div>',
			gallery: '<div class="wrapperImg"><a href="#" class="nyroModalPrev">Prev</a><a href="#"  class="nyroModalNext">Next</a></div>', // Use .nyroModalPrev and .nyroModalNext to set the navigation link
			swf: '<div class="wrapperSwf"><div class="button_container_red" style="z-index: 10000; float:right; margin: 0"><a href="#" class="nyroModalClose"><span>X</span></a></div></div>',
			iframe: '<div class="wrapperIframe"></div>',
			manual: '<div class="wrapper"></div>'
		},
		minWidth: 150, // Minimum width
		minHeight: 150, // Minimum height
		closeButton: '', // Adding automaticly as the first child of #nyroModalWrapper
		endShowContent: function() {
		
			var nyroModalTabindex = 99999999;
	
			$("[tabindex]").each(function () {
			
				if( parseInt( $(this).attr("tabindex") ) < nyroModalTabindex && parseInt( $(this).attr("tabindex") ) > 999 ) {
				
					nyroModalTabindex = parseInt( $(this).attr("tabindex") );
				
				}
			
			});
			
			$("[tabindex=" + nyroModalTabindex + "]").focus();
		
		},
		endRemove: function() {
		
			$.post(
				'buy.asp',
				{
					Clean: '1'
				}
			);
		
		}
		
	});
	*/
	
	$.fn.nyroModal.settings.processHandler = function(settings) {
	
		var from = settings.from;
		
		if( from && from.href && from.href.indexOf('http://www.tingstad.se/ecommercetest/video/') == 0 ) {
		
			$.nyroModalSettings({
				
				type: 'swf',
				height: 160,
				width: 300,
				url: from.href
			});
		
		}
	
	};
	
});

/*
	nyroModal-function to get rid of the bg
*/

function dontShowBackground(elts, settings, callback) {
	elts.bg.css({opacity:0}).fadeTo(0, 0, callback);
}

/*
	NyroModalClose - fix
*/
function closeTheNyroModal() {
	
	$.nyroModalRemove();

}





/*
	SortPage - används av administrationssidorna - från Class_ListView.js
*/
function SortPage(sFrmName,sColName,iMode,sListName) {

	if( $("form[name=" + sFrmName + "]") != null ) {
		
		if( $("form[name=" + sFrmName + "]").attr("action").indexOf('?') == -1 ) {
 		
			$("form[name=" + sFrmName + "]").attr("action", $("form[name=" + sFrmName + "]").attr("action") + '?listsort=' + sColName + '&listname=' + sListName );
			
		} else {
		
			$("form[name=" + sFrmName + "]").attr("action", $("form[name=" + sFrmName + "]").attr("action") + '&listsort=' + sColName + '&listname=' + sListName );
			
		}
		
		$("form[name=" + sFrmName + "]").find("input[name=mode]").val( iMode );
		$("form[name=" + sFrmName + "]").submit();
		
	} else {
	
		$.prompt(
			"No form provided for list view!"
		);
		
	}
}