var slidespeed = 300;
var prevtimer;
var prevautospeed = 8000;

function previewtimer() {
	$('#preview .btns .next').trigger('click');
}

$(function() {
	$('#topsearchform .TopLabel').remove();
	$('#topsearchform :text').val('enter your search').bind('focus',function() {
		if($(this).val() == 'enter your search') {
			$(this).val('');
		}
	}).bind('blur',function() {
		if($(this).val() == '') {
			$(this).val('enter your search');
		}
	});
	
	if($("#sendfriend").length > 0 ) {
		$(".plinks a[href='#sendtoafriend']").click(function() {
			$('#sendfriend').show();
		});
	}
	
	if($(".tab").length > 1 ) {
		$(".tab:gt(0) table.DialogBox").insertAfter('.tab:eq(0) table.DialogBox:last').hide();
		$('.tab td.DialogTitle.loc').removeClass('loc').children('font.NavigationPath').wrapInner('<a href=""></a>').children('a').appendTo('.tab:eq(0) td.DialogTitle');
		$('.tab:gt(0)').remove();
		$('.tab td.DialogTitle a').each(function(i) {
			if(i==0) {
				$(this).addClass('on').prev().remove()
			}
			$(this).bind('focus',function() { $(this).blur(); }).click(function() {
				$(this).siblings('.on').removeClass('on');
				$(this).addClass('on');
				$('.tab table.DialogBox:visible').hide();
				$('.tab table.DialogBox:eq('+i+')').show();
				return false;
			});
		});
		$(".plinks a[href='#review']").click(function() {
				$('.tab td.DialogTitle a:last').addClass('on').siblings('.on').removeClass('on');
				$('.tab table.DialogBox:visible').hide();
				$('.tab table.DialogBox:last').show();
				$('#reviewform').show().prev().remove();
		});
		$('#reviewform').hide().before('<a href="" id="writereview"><img src="/skin1/images/writereview.jpg" alt="Write Your Own Review" /></a>');
		$('#writereview').click(function() { $(this).next().show(); $(this).remove(); return false; });
		if($('.tab td.DialogTitle a').length < 3) { $('.tab td.DialogTitle a:last').after('<span class="notab"></span>'); }
	}

	if($('#preview').length > 0) {
		$('#preview .imgs a').each(function(i) {
			if(i > 0) { $(this).hide(); }
		});
		$('#preview .btns .dots a').each(function(i) {
			if(i==0) { $(this).addClass('on'); }
			$(this).click(function() {
				clearTimeout(prevtimer);
				prevtimer = setTimeout(previewtimer,prevautospeed);	
				$('#preview .imgs a:eq('+i+')').show().siblings(':visible').hide();
				$(this).addClass('on').siblings('.on').removeClass();
				return false;
			});
		});
		$('#preview .btns .next').click(function() {
			clearTimeout(prevtimer);
			prevtimer = setTimeout(previewtimer,prevautospeed);	
			var vis = $('#preview .imgs a:visible');
			var dot = $('#preview .btns .dots a.on');
			if(vis.next().length > 0) {
				vis.hide().next().show();
				dot.removeClass().next().addClass('on');
			} else { 
				vis.hide().parent().children('a:first').show();
				dot.removeClass().parent().children('a:first').addClass('on');
			}
			return false;
		});
		$('#preview .btns .prev').click(function() {
			clearTimeout(prevtimer);
			prevtimer = setTimeout(previewtimer,prevautospeed);	
			var vis = $('#preview .imgs a:visible');
			var dot = $('#preview .btns .dots a.on');
			if(vis.prev().length > 0) {
				vis.hide().prev().show();
				dot.removeClass().prev().addClass('on');
			} else { 
				vis.hide().parent().children('a:last').show();
				dot.removeClass().parent().children('a:last').addClass('on');
			}
			return false;
		});
		$('#preview .btns a').bind('focus',function() { $(this).blur(); });
		//timer
		prevtimer = setTimeout(previewtimer,prevautospeed);
	}
	
	if($('table.dlog div.prod.feat').length > 0) {
		$('table.dlog div.prod.feat').parent().wrapInner('<div class="slidem"><div class="slidin"></div></div>');
		var sliders = 0;
		$('table.dlog .slidem .slidin').each(function() {
			var prods = $(this).children().length;
			$(this).css('width',(prods*93)+'px').attr('title','0').parent().attr('title',prods);
			if(prods > 5) {
				$(this).parent().parent().append('<a href="" class="left"></a><a href="" class="right"></a>').children('a.left').click(function() { //left
					var slidin = $(this).siblings('.slidem').children('.slidin');
					$(this).next('a.right:hidden').show();
					var ml = parseInt($(slidin).attr('title'));
					if(ml > 0) {
						if(ml > 5) {
							var moveleft = '+=' + (5*93) + 'px';
							ml -= 5;
						} else {
							var moveleft = '+=' + (ml*93) + 'px';
							ml = 0;
						}
						$(slidin).attr('title',ml).animate({ 
							left: moveleft
						}, slidespeed,function() {
							var ml = parseInt($(this).attr('title')) - 1;
							if(ml < 0) { $(this).parent().siblings('.left').hide(); }
						});	
					}
					return false;
				}).bind('focus',function() { $(this).blur(); }).hide().next().click(function() { //right
					var slidin = $(this).siblings('.slidem').children('.slidin');
					$(this).prev('a.left:hidden').show();
					var ml = parseInt($(slidin).attr('title'));
					var maxx = $(slidin).children().length - 5;
					if(ml < maxx) {
						if((maxx - ml) > 5) {
							var moveright = '-=' + (5*93) + 'px';
							ml += 5;
						} else{
							var moveright = '-=' + ((maxx - ml) * 93) + 'px';
							ml = maxx;
						}
						$(slidin).attr('title',ml).animate({ 
							left: moveright
						}, slidespeed,function() {
							var ml = parseInt($(this).attr('title')) + 1;
							var maxx = $(this).children().length - 5;
							if(ml  > maxx) { $(this).parent().siblings('.right').hide(); }
						});	
					}
					return false;
				}).bind('focus',function() { $(this).blur(); });		
			}
		});
	}
	
	if($('#popup').length > 0) {
		$('#popup').appendTo('body').append('<div id="poptop"></div><div id="popmid"></div><div id="popend"></div><div id="poptab"></div><a href="" id="popfix2"></a><a href="" id="popfix"></a>');
		$('.PListImgBox').bind('mouseenter',function() {
			$('#popup #popmid').empty().append('<h2>'+$(this).siblings('a').html()+'</h2><span>'+$(this).siblings('.ProductPrice').html()+'</span><div><b>Member Rating:</b> '+$(this).children('.stars').html()+'<br /><br />'+$(this).siblings('.pdesc').html()+'</div>'+$(this).siblings('form').html());
			var ibox = $(this);
			$('#popup #popmid input:image').css('float','left').click(function() { ibox.siblings('form').submit(); });
			$('#popup #popmid br:last').remove();
			$('#popup #popfix').attr('href',$(this).children('a').attr('href'));
			var offset = $(this).offset();
			$('#popup').css({left:(offset.left),top:(offset.top+19)}).show();
		});	
		$('#popup').bind('mouseleave',function() {
			$(this).hide();
		});	
		$('#popfix2').bind('mouseenter',function() {
			$('#popup').hide();
		});
	}
$('#navlinks li').bind('mouseenter',function() { $(this).addClass('on'); $(this).children('a:first').addClass('on'); }).bind('mouseleave',function() { $(this).removeClass('on'); $(this).children('a:first').removeClass('on'); });
});