$(document).ready(function() {   	
	currURL = window.location.href;	

	if(currURL == "http://bid.nitkeauctions.com/cgi-bin/mndetails.cgi?nitke2" || "http://bid.nitkeauctions.com/cgi-bin/mndetails.cgi?nitke1") {
	$("#wrapper table td").css({"font-size":"13px","padding":"6px"});
	$("#wrapper table td a").css({"padding":"0 3px","line-height":"150%"});
	$("#wrapper table img").css({"border":"solid 1px #ccc","padding":"5px"});
	}
	
	//$('#NavLinks').append('<a href="javascript:history.go(-1)">Go Back</a>');
		
	$("#SearchArea").find(":submit").attr("value","Find Items"); 
	
	$("#SubmitBids").next().css("background","#174576"); 
	
	$("#DataTable tr:even").addClass("even"); 
	
	var searchCategory = $("#TableTop td:first").html();

	var tableHeaderInfo = $("#TableTop td:last").text().split("-");
	var arrayCount = tableHeaderInfo.length;
		
	$("#DataTable")
		.before('<h2 id="searchCat" style="padding:10px;background:#F8ED98;text-align:center;">'+searchCategory+'</h2>')
		.before('<div class="tableHeaderInfo">'+tableHeaderInfo[arrayCount-1]+'<span><a class="printPage" style="float:right;padding-right:10px;" href="#">Print Items</a><a style="float:right;padding-right:10px;" href="#" ONCLICK="history.go(-1)">Go Back</a></span></div>');
	
	$("#SelectCat").prepend('<h3>Select a Category</h3>');
			
	$('#DataTable td:contains("ended")').filter(function() {
		if ($(this).text()=="ended") {
			$(this).css({"color":"red","text-transform":"capitalize"});
		}
	});
	
	$('#DataTable td:nth-col(3)').css("text-align", "left");
	
	$('#NavLinks a:contains("Register")').filter(function() {
		if ($(this).text()=="Register") {
			$(this).remove();
		}
	});
	
	$('#DataTable strong:contains("Reserve Met!")').filter(function() {
		if ($(this).text()=="Reserve Met!") {
			$(this).css("color","green");
		}
	}); 
	
	keyword = $('input[name="keyword"]').val();

	if (keyword) {
		$('#maxaContent').highlight(keyword, 1, 'highlight'); 
	}
			
	$("#DataTable tbody td:last-child i a").css({"text-decoration":"underline","color":"blue"});
	
	$("#DataTable tr:first td:first strong").text("Item #");
		
	$('#wrapper p:contains("No data found")').append('<a id="noData" style="padding-left:10px;font-size:16px;font-weight:bold;" href="javascript:history.go(-1)">Go Back</a>');
	
	if ( $('#noData').length ) {
		$("#searchIndicator").hide(); 
	}
	
	$("#NavLinks br:first-child").remove();
	
	auctionLinks = new Array();
	list = new Array();
	
	$("#auctionList").load("http://bid.nitkeauctions.com/cgi-bin/mncal.cgi?nitke [id*=auction_title]",{},function(responseText, textStatus) {
		
		if(textStatus == 'success') {	
			$("#auctionList [id*=auction_title]").each(function(i) {
				list[i] = $(this).text();
				auctionLinks[i] = '<a href="/cgi-bin/mndetails.cgi?nitke'+[i+1]+'">'+list[i]+'</a>';
			});
			
			if(auctionLinks.length > 0) {			
				for(var i=0;i < auctionLinks.length; i++) {
					$(".subLinks").append(auctionLinks[i]);
					$('.selectAuction').append('<option value="nitke'+(i + 1)+'">'+list[i]+'</option>');
				}
			}else{
			
				$("#auctionList").load('http://bid.nitkeauctions.com/cgi-bin/mndetails.cgi?nitke1 #auction_title',{},function() {					
					title = $("#auctionList").text();
					$('.subLinks').append('<a href="/cgi-bin/mndetails.cgi?nitke1">'+title+'</a>');
					$('.selectAuction').append('<option value="nitke1">'+title+'</option>');
				});		
			}
		}
			
	});
		
	activeWorkspace = $('#SubmitBids input[name="auction"]').attr("value");
	if (activeWorkspace == null) {
	activeWorkspace = "nitke1"
	}
	
	$('.login input[name="auction"]').attr("value",activeWorkspace);	
	
	$('.registerLink').attr("href",'/terms.php?auction='+activeWorkspace);
	
	function getCookie(c_name)
	{
	if (document.cookie.length>0)
	  {
	  c_start=document.cookie.indexOf(c_name + "=");
	  if (c_start!=-1)
	    {
	    c_start=c_start + c_name.length+1;
	    c_end=document.cookie.indexOf(";",c_start);
	    if (c_end==-1) c_end=document.cookie.length;
	    return unescape(document.cookie.substring(c_start,c_end));
	    }
	  }
	return "";
	}
	
	
	function deleteCookie(name) {
		var expdate = new Date();
		expdate.setTime(expdate.getTime() - 100);
		document.cookie = name += "=; expires=" + expdate.toGMTString();
	}
	
	function setCookie(c_name,value,expiredays)
	{
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie=c_name+ "=" +escape(value)+
	((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
	}
	
	cred = 0;
	
	if(currURL == 'http://bid.nitkeauctions.com/cgi-bin/mnlistf.cgi') {
		cred = 'fail';
	}
	
	if($('#NavLinks').length) {
		returnToAuctionRaw = $('#NavLinks a:last').attr('href');
		returnToAuction = returnToAuctionRaw.split('/refresh');
		backToCat = returnToAuction[0].replace("mndetails","mnlist");
		
		if(returnToAuctionRaw.indexOf("nitke1")!=-1) {
			cred = 1;
			cookie = getCookie('nitke1bidder');
		}else if(returnToAuctionRaw.indexOf("nitke2")!=-1) {
			cred = 2;
			cookie = getCookie('nitke2bidder');
		}
	}
	
	
	if(currURL !== 'http://bid.nitkeauctions.com/cgi-bin/mnlistf.cgi') {
		$('.login').submit(function() {
			if($('.selectAuction').val() == "Select Auction") {
				alert('Your Bidder Login is Auction-specific. You must select an Auction from the drop-down list prior to logging in.');
				return false;
			}else {
					bidderNumber = "";
				$('input[name="bidder"]').each(function() {
					bidderNumber += $(this).val();
				});
				if($('.selectAuction').val() == "nitke1") {
					setCookie('nitke1bidder',bidderNumber,1);
				}else if($('.selectAuction').val() == "nitke2"){
					setCookie('nitke2bidder',bidderNumber,1);
				}
			}
		});
	}
	
	if(currURL !== 'http://bid.nitkeauctions.com/cgi-bin/mnlistf.cgi') {
		$('form[name="bidform"]').submit(function() {
				bidderNumber = "";
			$('input[name="bidder"]').each(function() {
				if($(this).val() !== ""){
					bidderNumber = $(this).val();
				}
			});
			
			if($(this).find('input[name="auction"]').val() == "nitke1") {
				setCookie('nitke1bidder',bidderNumber,1);
			}else if($(this).find('input[name="auction"]').val() == "nitke2"){
				setCookie('nitke2bidder',bidderNumber,1);
			}
		});
	}
	
	if(currURL.indexOf('nitke1/refresh')!=-1) {
		deleteCookie('nitke1bidder');
	}else if(currURL.indexOf('nitke2/refresh')!=-1) {
		deleteCookie('nitke2bidder');
	}
	
	
	
	if(currURL == 'http://bid.nitkeauctions.com/cgi-bin/mnlistb.cgi' && $('#noData').length) {
		$('#noData').parent().html('<span style="color:#999;font-size:28px;">0 Bids.</span><br><span style="font-size:18px;">Currently, you are not bidding on any item in this auction.</span> <a id="noData" style="padding-left:10px;font-size:16px;font-weight:bold;" href="'+backToCat+'/category/ALL">Return to Auction</a>');
		$('#navLogin').html('<form class="bidform" method="post" action="/cgi-bin/mnlistb.cgi"><span>Welcome, Bidder #' + cookie + '</span>&nbsp;&nbsp;<input id="reviewBids" type="submit" name="review" value=" " /><input type="hidden" name="auction" value="'+activeWorkspace+'" />&nbsp;&nbsp;<a href="http://bid.nitkeauctions.com/cgi-bin/mndetails.cgi?'+activeWorkspace+'/refresh">Log Off</a></form>');	
		$("#maxaContent .loginBox").prepend('<h1 id="auctionName">Your Bid Results</h1>');
	}else if(cred !== 'fail' && cred !== 0 && $('form[name="firstform"]').length < 1) {
	$('#navLogin').html('<form class="bidform" name="bidform" method="post" action="/cgi-bin/mnlistb.cgi"><span>Welcome, Bidder #' + cookie + '</span>&nbsp;&nbsp;<input id="reviewBids" type="submit" name="review" value=" " /><input type="hidden" name="auction" value="'+activeWorkspace+'" />&nbsp;&nbsp;<a href="http://bid.nitkeauctions.com/cgi-bin/mndetails.cgi?'+activeWorkspace+'/refresh">Log Off</a></form>');
	}else if(cred == 'fail') {
		$('#navLogin').prepend('<div style="font-size:14px;color:red;font-weight:bold;">Error logging in.</div><strong>Forgot your password?</strong>  <a style="display:inline" href="/profile.php">Click Here</a><p>Try logging in again.</p>');	
		$('#maxaContent .loginBox').prepend('<h1 id="auctionName">Error logging in.</h1>');	
	}
	
	if(currURL == 'http://bid.nitkeauctions.com/cgi-bin/mnlistb.cgi' && $('form[name="viewform"]').length) {
		$('#searchCat').append(' You are currently bidding on the following items... <a href="'+backToCat+'/category/ALL">View All Lots</a>');
	} 

		
	if ($("#SelectCat").length) {
		var auctionTitle = $("#TableTop td:last-child").text();
		$("#maxaContent .loginBox").prepend('<h1 id="auctionName">'+auctionTitle.split(" - ",1)+'</h1>');
	}
	else if ($("[id*=auction_title]").length == 2) {
		var auctionTitle = "Choose an auction or search by keyword.";
		$("#maxaContent .loginBox").prepend('<h1 id="auctionName">'+auctionTitle+'</h1>');
	}
	else if ($("[id*=auction_title]").length == 1) {
		var auctionTitle = $("#auction_title").text();
		$("#maxaContent .loginBox").prepend('<h1 id="auctionName">'+auctionTitle+'</h1>');
	}else if ($('form[name="calenform"]').length) {
		var auctionTitle = "Your Search Results";
	}

	$('#DataTable').livequery(function() { 	 		 	
		$("#searchIndicator").fadeOut(1200); 		 
	});
			
	$('#PhotoArea a').each(function() {
		if($(this).children('img').length) {
			$(this).addClass('thickbox');
			newhref = $(this).children('img').attr('src');
			thickboxlink = newhref.replace("t.jpg",".jpg");
			$(this).attr("href",thickboxlink);
		}
	});
	
	$('head').append('<script type="text/javascript" src="http://bid.nitkeauctions.com/js/thickbox-compressed.js"></script>');
		
	var height = $(document).height();
	$('#emailFormHolder').css('height', height + 'px' );
	
	$("#emailFormHolder").click(function() {
		$(this).hide();
		$("#emailForm").hide();
	    $(".emailItemForm").hide();
	    $(".askQuestionForm").hide();
		$("#thumbPreview").html("");
	});
	
	$('.askQuestionForm input[type="submit"]').before('<textarea style="overflow:auto;height:60px;" name="question"></textarea><br>');

	$(".itemAction").livequery('click', function() {
			$(".itemURL").attr("value",currURL);
	    	aucName = $("#auctionName").text();
	    	itemnumber = $(this).closest("td").prev().prev().text();
			$("#emailFormHolder").fadeIn("normal");
			$("#emailForm").fadeIn("normal");
			rowsrc = $(this).parent().parent().html();
			nobutton = rowsrc.replace('<span action=\"email\" class=\"itemAction\"><img style="border: 1px solid rgb(204, 204, 204); padding: 5px;\" src=\"/images/emailItem.gif\" alt=\"Email\" border=\"0\" height=\"16\" width=\"86\"></span>',"");
			infowithlinks = nobutton.replace(/\/cgi-bin/gi,'http://bid.nitkeauctions.com/cgi-bin');
			makedoublequote = infowithlinks.replace("\'","\"");
			$("#thumbPreview").prepend('<h3 style="text-align:center;color:#888;">Item #'+itemnumber+'</h3>');
			if($(this).attr("action") == "email"){
	    		$(".emailItemForm").show();
	    	}else if($(this).attr("action") == "ask"){
	    		$(".askQuestionForm").show();
	    	}
			emailItemOptions = { 
				success: emailItemResponse,
				type: 'POST',
				data: {item_info:makedoublequote, item_num:[itemnumber], auction_name:[aucName]}
			};          			
			
			$(".emailItemForm").ajaxForm(emailItemOptions);
			$(".askQuestionForm").ajaxForm(emailItemOptions);

	});
	
	
	$("#signupImage").click(function() {
		$('#signup').html('<form style="padding-left:10px;" id="joinEmailForm" action="http://bid.nitkeauctions.com/processSubscription.php" method="post">Your Name:<br><input style="margin-bottom:5px;" type="text" name="contact_name"><br>Email:<br><input type="text" name="email"><br><input id="submit" type="submit" value=" "></form>');
		
	var params = {
		beforeSubmit : antesDe,
		success: afterSubmit
	};
	
	$("#joinEmailForm").ajaxForm(params);
		
	});

	function antesDe() {
		$('#signup').html('<div><img src="../images/ajax-loader2.gif"></div>');
	}
	
	function afterSubmit() {
		$("#signup").html('Email Submitted. Thank you!');
		setTimeout('$(\"#signup\").fadeOut(\"slow\")', 2000);
	}	
	
	//alert(currURL+'\n'+cred+'\n'+getCookie('nitke1bidder')+'\n'+getCookie('nitke2bidder'));
	
	if(currURL.indexOf("mnlist.cgi")!=-1 && $('#PhotoArea').length==0) {
		bottomHeader = $('#DataTable tr:first').html();
	
		$('#wrapper').append('<table class="bottomHeader"><tr>'+bottomHeader+'</tr></table>');
		
		$('form').submit(function() {
			if ($(this).find('input[name="bidder"]').val() != "") {
				bidder = $(this).find('input[name="bidder"]').val();
				password = $(this).find('input[name="password"]').val()
				$('input[name="bidder"]').val(bidder);
				$('input[name="password"]').val(password);
			}
		});
		
		bottomHeaderWidth = $('#DataTable').width();
		cellwidths = new Array();
		$('#DataTable td').each(function(i) {
			cellwidth = $(this).width();
			cellwidths[i] = cellwidth;
		});
		
		for(var i in cellwidths) {
			$('.bottomHeader td:eq('+i+')').css("width", cellwidths[i]+"px");
		}
		
		floatingbidform = $("#SubmitBids tr td:first").html();
		$('#wrapper form[name="bidform"]').append('<div class="bottomBidForm">'+floatingbidform+'</div>');

		$('.bottomHeader').css("width",bottomHeaderWidth+"px");	
		$('.bottomBidForm').css({"width":bottomHeaderWidth+"px"});
			
		
		$(window).resize(function() {
			bottomHeaderWidth = $('#DataTable').width();
		    $('.bottomHeader').css("width",bottomHeaderWidth+"px");
			$('.bottomBidForm').css("width",bottomHeaderWidth+"px");	
	    });
				
		$(window).scroll(function () {
			scrollpos = 510;
			if(document.documentElement.scrollTop > 0) {
				scrolledpix = document.documentElement.scrollTop;
			}else{
				scrolledpix = document.body.scrollTop;
			}
	    	
	    	if(scrollpos < scrolledpix){
	      		$(".bottomHeader").fadeIn("slow"); 
	      		$(".bottomBidForm").fadeIn("slow");
	    	}else{
	      		$(".bottomHeader").fadeOut("slow"); 
	      		$(".bottomBidForm").fadeOut("slow");
	    	}
	    });
	    
	}
		
	$('.printPage').livequery('click',function() {
	toprint = $('#wrapper').html();
	aucName = $("#auctionName").text();
	$('#printContainer').html('<h2 style="font-size:21px;padding:15px 0;text-align:center;">'+aucName+'</h2><div style="font-size:16px;text-align:center;padding:10px 0;color:#666;">Nitke Sales, Inc. PO Box 1639 Rhinelander, WI 54501 (715)362-6162</div><div style="color:#666;font-size:16px;text-align:center;">Item descriptions, years, models and hours are not guaranteed. Bidder assumes all responsibility to inspect equipment prior to bidding.</div>'+toprint+'<div style="padding:10px 0;font-size:16px;text-align:center;color:#666;">&copy;2010 Nitke Sales, Inc. PO Box 1639 Rhinelander, WI 54501 (715)362-6162</div>');
	window.print();
	});


});  
	
function delayedRemoval() {
	$("#emailForm").fadeOut();
	$("#emailFormHolder").fadeOut();
	$("#success").hide();
}

function emailItemResponse(responseText, statusText) { 
	$(".emailItemForm").hide();
	$(".askQuestionForm").hide();
	$("#thumbPreview").html("");
	$(".emailItemForm input").val("");  
	$("#success").fadeIn("normal");
	setTimeout ('delayedRemoval()', 1500 );
}