$(function() {
	$.ajaxSetup({ cache: false });

	rotit = true;
	$("#merkrot").hover( 
		function(){ rotit = false;}, 
		function(){ rotit = true; }
		)

	$("#topline").append("<div id='bannerclone'></div>");
	left = $(window).width()/2-200;
	$("<div id='bancan'></div>").appendTo('body:not(.nobanner)').show();
	$("#bancan").css({
			'position' : 'absolute',
			'left' : 0,
			'top'  : 0,
			'height' : '230px',
			'width' : $(window).width(),
			'overflow' : 'hidden'
			//'background-color': 'blue'
			})
	$("<div id='banner'></div>").appendTo('#bancan') //.show();
	$("#banner").css({'left':left, 'top':'5px'})
	setTimeout('bannerFNT()',1500)
	setTimeout('rotatemerk()',5000)

bannerFNT = function() {
	$.get("nf_banner.php", '',
	function(data) {
		$('#banner').html(data);
		});
	}

rotatemerk = function() {
	if (rotit) { rotatemerk2() }
	setTimeout('rotatemerk()',5000)
	}

rotatemerk2 = function() {
	aantalmerken = $('[id^=merk_]').length
	$('[id^=merk_]').each(
		function(){
			if ($(this).css('display')=="block")
				{
				rotit = false;
				deze = $(this).attr('id').substr(5);
				volgende = ( deze < aantalmerken ) ? parseInt(deze) + 1 : 1;
				$('#merk_'+deze).fadeOut(1000, function(){
					$('#merk_'+volgende).fadeIn(1000, function(){rotit = true;} ) 
					})
				}
			}
		);
	}

	$('#toplogo').css('cursor','pointer').attr('title','Home').click(function(){ location.href='index.php' });
	
	if ( $(window).height() <= 550 )
		{
		$('#onder').hide();
		$('#onderAlt').show();	
		}
	
	$(window).resize(function() {
		if ( $(window).height() <= 550 )
			{
			$('#onder').hide();
			$('#onderAlt').show();	
			}
			else
			{
			$('#onder').show();
			$('#onderAlt').hide();	
			}
		})		
	
	$('div#menu li').hover (
		function () {
			$(this).find('div').first().fadeIn('fast')
			}, 
		function () {
			$(this).find('div').first().fadeOut('slow')
		});
	
	
	$('.cartinner').hover (
		function () {
			if ( $("#terugshop").length ==0 ){$(this).find('div:first-child').fadeIn('fast')}
			}, 
		function () {
			if ( $("#terugshop").length ==0 ){$(this).find('div:first-child').fadeOut('fast')}
		});
	
	$('.accountopties').hover (
		function () {
			if ( $("#terugshop").length ==0 ){$(this).find('div:first-child').fadeIn('slow')}
			}, 
		function () {
			if ( $("#terugshop").length ==0 ){$(this).find('div:first-child').fadeOut('fast')}
		});
	
	zoekvak = $('.subut').attr('title');
	$('form[name=quick_find] input[name=keywords]').val(zoekvak);
	
	$('form[name=quick_find] input[name=keywords]').bind('focus', function() {
		if ( $('form[name=quick_find] input[name=keywords]').val()==zoekvak )
			{
			$('form[name=quick_find] input[name=keywords]').val('');
			}
		});
	
	$('input[name=keywords]').bind('blur', function() {
		if ( $('form[name=quick_find] input[name=keywords]').val()=='' )
			{
			$('form[name=quick_find] input[name=keywords]').val(zoekvak);
			}
		});
	
	$('input.subut').click( function(evt) {
   			evt.preventDefault();
			bxcnt = $('form[name=quick_find] input[name=keywords]').val();
			if (bxcnt=='zoeken' || bxcnt=='search' || trim(bxcnt)=='')
				{
				zoekfout();
				}
				else
				{
				$('input.subut').parent().submit()
				}
			});
	
	
	// ondermenu
	$('#ondersbg').animate({"bottom":"-50px"}, 350,
		function() {
			$('#bord1').animate({"width":"181px", "left":"0px"}, 150 )
			$('#bord2').animate({"width":"140px", "right":"20px"}, 150 )
			$('#bord3').animate({"width":"135px", "right":"27px"}, 150 )
			});
	
	$("div#orderbutton").hover(
		function () {
			$(this).css("background-image","url('images/orderbut01.png')");
		}, 
		function () {
			$(this).css("background-image","url('images/orderbut02.png')");
		});
	
	
	$("div#onderkasA").hover (
		function () {
			$("#kassabutton2").show();
		}, 
		function () {
			$("#kassabutton2").hide();
		});
	
	
	$('[id^=LL_]').hover (
		function () {
			nid = "#Icon" + $(this).attr('id');
			nid2 = "#Txt" + $(this).attr('id');
			$(nid).fadeTo("fast",1)
			$(nid2).css("color", "orange")
		}, 
		function () {
			nid = "#Icon" + $(this).attr('id');
			nid2 = "#Txt" + $(this).attr('id');
			$(nid2).css("color", "#a0023b")
			$(nid).fadeOut("fast",0)
		});
	
	
	$("#nbs").click(function(){
		w=$('#ema').attr('value')
		location.href='http://www.s-trade.nl/s-list/index.php?email='+w
		})
	
	$("div#onder").hover (
		function () {
			$("div#onderbg").stop().animate({"top":"0px"},400);
			$("div#ondertopper").stop().animate({"top":"0px"},400, '', $("#nb_in").fadeIn('fast').children('#ema').focus());
			//$("#nb_in").fadeIn('fast');
		}, 
		function () {
			$("div#onderbg").stop().animate({"top":"75px"},400);
			$("div#ondertopper").stop().animate({"top":"75px"},400, '', $("#nb_in").fadeOut('fast'));
			//$("#nb_in").fadeOut('fast');
		});
	
	// einde ondermenu
	
	
	// product/overzichts-pagina
	$("div.ilink").hover (
		function () {
			$(this).siblings(".ONinfo").fadeTo("fast",.9);
		}, 
		function () {
			$(this).siblings(".ONinfo").fadeOut("fast");
		});

//	$("div.afbeelding").hover (
//		function () {
//			$(this).siblings(".extrainfo").fadeTo("fast",.9);
//		}, 
//		function () {
//			$(this).siblings(".extrainfo").fadeOut("fast");
//		});
		
	$("a.mandje").hover(
		function () {
			$(this).children("img").attr("src","images/mandje2a.png");
		}, 
		function () {
			$(this).children("img").attr("src","images/mandje2b.png");
		});
 	 	
	$("a.mandje").click(
   		function(evt) {
   			evt.preventDefault();
			
//			if ($("#thuis").length>0)
//				{
//	   			a = $(this).parent().parent().find(".prodafb").attr('src');
//				bx= evt.pageX;
//				by= evt.pageY;
//				$("<img src='"+ a +"'>").appendTo("#canvas").css({
//							"position":"absolute",
//							"z-index": "1000",
//							top: (by -150),
//							left: (bx - 310)}).animate({
//								top: '300px',
//								left: '50px',
//								opacity:0.5
//							}, 1200, function(){ $(this).fadeOut(500, function(){ $(this).remove() }  ) })
//   				}
   			
   			if ($(this).attr('id'))
   				{ b = $(this).attr('id') }
   				else
   				{ b = $(this).parent().attr('id') }
   			add2cart("tocart=" + b);
	 	});
	
 	
 	// buttons in dialoog
   	$("a.dialogclose").click(
   		function(evt) {
   			evt.preventDefault();
   			hidedia();
	 	});

	$("a#pwdfgt").toggle(
		function () {
			$("#password").val("");
			$('#password').hide()
			$('#wwxt').show();
			$('#fpw1').hide(); $('#fpw2').show();
			$('#ilt1').hide(); $('#ilt2').show();
			$('#reqnp').val('1');
		}, 
		function () {
			$('#password').show();
			$('#wwxt').hide();
			$('#fpw1').show(); $('#fpw2').hide();
			$('#ilt1').show(); $('#ilt2').hide();
			$('#reqnp').val('0');
		});
   	
	$("a#submitbutton").click(
   		function(evt) {
   			evt.preventDefault();
			var nameval = $("#email_address").val();
			var pwordval = $("#password").val();
			var npwd = $("#reqnp").val();
			dologin("action=process&email_address=" + nameval + "&password=" + pwordval + "&reqnp=" + npwd);
		});

   	$("#inlogbutton").click(
   		function(evt) {
   			evt.preventDefault();
	 		$("#password").val("");
	 		if ( $("#email_address").val() )
	 			{
				aaa = $("#password");
	 			}
	 			else
	 			{
	 			aaa = $("#email_address");	
	 			}
	 		showdia('loginbox',300,170)
	 		aaa.focus();
	 		}
  	);

	$("a#logoffbutton").click(
		function(evt) {
			evt.preventDefault();
			dologoff()
	 		}
		);

   	$("#orderbutton, #orderbut2").click(
   		function(evt) {
   			evt.preventDefault();
   			if ( $("#terugshop").length >0 )
   				{
   				location.href = "index.php"
   				}
   			else if ($("#inlogbutton").length>0)
   				{
   				$('#loginfirst').fadeIn('fast');
   				}
   			else {
   				location.href = "bestellen.php"
   				}
   		});	

	$("#loginfirst").click(
		function(evt) {
			$("#loginfirst").fadeOut('fast');
	 		});

	$("#tweak a").attr("target","_blank").css("font-size","13px");
	$("a[target=_blank]").after('<img src="images/externlink.gif" style="vertical-align:text-bottom;">');



//aanbiedingen

	$(".item .gratis").hover (
		function () {
			$(this).animate( {width : '160px', height: '120px' } )
		}, 
		function () {
			$(this).animate( {width : '80px', height: '60px' } )
		});

	$(".gratis2").hover (
		function () {
			$(this).animate( {width : '280px', height: '210px' } )
		}, 
		function () {
			$(this).animate( {width : '160px', height: '120px' } )
		});

});




function add2cart(a)
	{
     //showdia('waiting',240,120)
	if (a!=-1)
		{
		ajaxdone=false;
		$('body').ajaxSuccess(function() { ajaxdone=true; hidedia(); });
		setTimeout(function(){ if(ajaxdone!=true){ showdia('waiting',240,120) } },1000 )
		}
		
	$.get("ajaxcart.php", a,
    		function(data) {
    			$('#cartdiv').html(data);
    			//hidedia()
    			});
    	}

function dologin(a)
	{
	$.get("ajaxaccount.php", a,
    		function(data) {
    			$('#accountdiv').html(data);
   			add2cart(-1); //update cart-inhoud
    			});
	}

function dologoff()
	{
	$.get("ajaxcart.php", "logoff=true",
    		function(data) {
    			$('#cartdiv').html(data);
    			dologin("");
    			});
	}

function hidedia()
	{
	$('#mask').fadeOut("fast");
   	$('#dialog').fadeOut("fast");
	$('#dialog > div').hide();
	}

function showdia(divnaam,x,y)
	{
	sizedialog(x,y)
	$('#dialog > div').hide();
	$('#'+divnaam).show();
	$('#mask').fadeTo("fast",0.85);
	$('#dialog').fadeTo("fast",0.85);
	}

function sizedialog (w,h)
	{
	$('#dialog').css({
		'width'  : w+'px',
		'height' : h+'px',
		'margin-left' : -(w/2)+'px',
		'margin-top' : -(h/2)+'px'			
		});
	}
	
function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}


