// insert a style-tag, which hides the menu lists before they are shown
$('<style type="text/css">#mainMenu ul ul, #pageMenu ul ul, .form .text label, .form .postalcodeCity label, .showProducts input { display: none; }</style>').appendTo('head');

// jquery initialization script
$(function(){
	
	$.Lightbox.construct({
		show_info: false,
		show_extended_info: false
	});

	
	// open/close the different levels in the main menu.
	$('#mainMenu li').hoverIntent(function(){ // on mouseover
		$('ul:first',this).fadeIn();
	},function(){ // on mouseout
		$('ul:first',this).fadeOut();
	});
	
	// open/close the different levels in the site menu.
	$('#pageMenu li ul').prev().click(function(){
		$(this).next().slideToggle();
		return false;
	});
	
	// replace text in input fields
	$('.jsInput').toggleInputText();
	
	$('.contentMenu span').each(function(){
		$(this).css({
			display: 'block',
			position: 'relative',
			top: '44%',
			marginTop: '-' + ($(this).height()/2) + 'px'
		});
	});
	
	$('.productlist table form').submit(function(){
		$.post("post.aspx",{
				_Function: this._Function.value,
				_ReturnTo: 'dynamic.aspx?data=basket&template=basketpreview',
				productid: this.productid.value,
				quantity: this.quantity.value,
				_Message: this._Message.value
			}, function(data){
					var startCut = data.indexOf('<a');
					var endCut = data.indexOf('</a>') + 4; 
					$('.basketpreview').html($(data.substring(startCut,endCut)));
			},'html');
			
		$('.added',this).show(1,function(a){
			setTimeout(function(){
				$('.added').fadeOut('slow');
			},1000)
		});
		return false;
	});
	
	// mouseover on the camera on productlist pages
	$('.productlist .camera a').lightbox().hoverIntent(function(){ // on mouseover 
		var imgMed = $(this).attr('rel');
		// if the element does not exist yet, create it
		if ($('.medium',this).length == 0){
			$(this).append('<img src="' + imgMed + '" class="medium"/>');
		}
		$('.medium',this).show();
	}, function(){ // on mouseout
		$('.medium',this).hide();
	});
	
	$('.showProducts select').change(function(){
		$(this).closest('form').submit();
	});
	
	
// ### old playground imagegallery, disabled since it's not needed anymore
/*
	
	// cycle plugin for imagegalleries on the playground pages
	$('.playgroundPage .imagegallery').cycle({
  	fx: 'turnDown',
  	speed: 'fast',
  	timeout: 0,
  	pager: '#nav',
  	pagerAnchorBuilder: function(idx, slide){
  		// return selector string for existing anchor 
				return '.imagegallerynav li:eq(' + idx + ') a';
			}
	});
	
	$('.playgroundPage .imagegallery, .playgroundPage .imagegallerynav').hide();
	
	$('#jsViewplaygroundimages').toggle(function(){
		$('.playgroundPage .imagegallery, .playgroundPage .imagegallerynav').fadeIn();
		return false;
	},function(){
		$('.playgroundPage .imagegallery, .playgroundPage .imagegallerynav').fadeOut();
		return false;
	});
*/

	
	
});


// jQuery plugin to replace the text in input fields on focus/blur
(function($){
	$.fn.toggleInputText = function(){
		var elements = this;
		return (elements.each(function(){
			var el = $(this), title = el.attr('title');
			if (el.val() == "") {el.val(title)};
			el.focus(function(){
				if(el.val() == title) {el.val("")};
			}).blur(function(){
				if(el.val() == "") {el.val(title)};
			});
		}));
	};
})(jQuery);