document.observe('dom:loaded', page_loaded); 
var fAuctions;
var fAuctionsIndex = [];
fAuctionsIndex[0] = undefined; /* current index */
fAuctionsIndex[1] = undefined; /* prev index */


var timer_var;
var lock_var;
var timer_var_left;
var lock_var_left;

var gate_var = 1;
var fAuctionsNum = 2;

function undisplay(el) {
	if ((timer_var == 0) && (lock_var == 0)) {
		$(el).hide();
	}
	else {
		setTimeout(undisplay.curry($(el)), 1199);
	} 
}
function page_loaded() {
	if ($('category_contents')) {
		fAuctionsNum = 5;
	}
	else {
		fAuctionsNum = 6;
	}
	$('category').observe('click', function sh(event) { 
		$('partsCategoryMenu').hide(); 

		if ($('categoryMenu').visible()) {
			$('categoryMenu').hide(); 
			return false;
		}

		$('categoryMenu').show(); 
		setTimeout(undisplay.curry($('categoryMenu')), 2199);
		timer_var = 0;
		lock_var = 1;
		if ($(event.target).innerHTML == 'Equipment') {
			event.stop();
		}
	}); 

	$('category').observe('mouseout', function sh(event) { 
		lock_var = 0;
	});


	$('categoryMenu').observe('mouseover', function sh(ele) { 
		$('category').down('a').setStyle({ color:'#fff', background:'url(http://images.dotmed.com/images/topMenu_active_bg.gif) repeat-x left top'}) 
		$('categoryMenu').show(); 
		timer_var = 1;
		lock_var = 0;
	});

	$('categoryMenu').observe('mouseout', function sh(ele)  { 
		$('category').down('a').setStyle({ color:'#000', background:'none'}) 
		timer_var = 0;
		lock_var = 0;
	});

	$('parts_category').observe('click', function sh(event) { 
		$('categoryMenu').hide(); 
		if ($('partsCategoryMenu').visible()) {
			$('partsCategoryMenu').hide(); 
			return false;
		}
		$('partsCategoryMenu').show(); 
		setTimeout(undisplay.curry($('partsCategoryMenu')), 2199);
		timer_var = 0;
		lock_var = 1;
		if ($(event.target).innerHTML == 'Parts') {
			event.stop();
		}
	}); 

	$('parts_category').observe('mouseout', function sh(event) { 
		lock_var = 0;
	});

	$('partsCategoryMenu').observe('mouseover', function sh(ele) { 
		$('parts_category').down('a').setStyle({ color:'#fff', background:'url(http://images.dotmed.com/images/topMenu_active_bg.gif) repeat-x left top'}) 
		$('partsCategoryMenu').show(); 
		timer_var = 1;
		lock_var = 0;
	});

	$('partsCategoryMenu').observe('mouseout', function sh(ele)  { 
		$('parts_category').down('a').setStyle({ color:'#000', background:'none'}) 
		timer_var = 0;
	});
	function licat(ele, event) { 
			var t = $(ele).down('ul'); 
			if (t.visible()) {
				t.hide();
				return false;
			}
			$$('#categories>li>ul').each(function sh(ele) {  ele.hide(); });
			gShow(t, event); 
			setTimeout(gHide.curry(t), 2199); 
			timer_var_left = 1; 
			lock_var_left = 1;
	};

	function gShow2(ele, event) {
		timer_var_left = 1;
	}

	$$('#categories>li').each( 
		function sh(ele) {
			$(ele).observe('click', licat.curry(ele));
			$(ele).observe('mouseout', function(ele) { timer_var_left = 0; lock_var_left = 0; });
			$(ele).observe('mouseover', gShow2.curry(ele)); 
	});
	$$('#categories>li').each( 
		function sh(ele) {
	});
	$$('#categories>li>ul').each(function sh(ele) { 
			lock_var_left = 0;
			$(ele).observe('mouseover', gShow.curry(ele)); 
	}); 
	$$('#categories>li>ul').each(function sh(ele) { 
			$(ele).observe('mouseout', gOut.curry(ele)); 
			lock_var_left = 0;
	}); 

	$$('#leftRefineSearch select').each(function sh(ele) { 
		$(ele).observe('change',  function hig() { setTimeout(refineHighlight, 777); }); 
	});

	if ($('featuredAuctionsItem')) {
		fAuctions = fAuctionsSetup();  
		fAuctionsDisplay(0);  
	}
		

	if ($('subPic0')) {
		$$('#pic .img').each(function sh(ele) {
			$(ele).observe('click', picSwp.curry(ele));
		}); 
		init_pic();
	}

	if ($('feedback_modal')) {
		Modalbox.show($('feedback_modal'), {title: this.title, width: 600});
	}
	if ($('feedback')) {
		setTimeout(function() { new Effect.Fade('feedback'); }, 4000);
	}
	
	if ($('tapMenu_list')) {
		fp_toggle_tapmenu_jgr('popular_search_terms');
	}

}

function picSwp(ele, Event) {
	var nm = $(ele).down('img').getAttribute('src');
	var pat = /\d+_{0,1}\d{0,1}\.\w\w\w$/;
	var listing = nm.match(pat);
	var big = nm;
	if (listing) 
		big = 'http://images.dotmed.com/images/listingpics/' + listing; 
	$('mainPic').writeAttribute({src:nm});
	$('mainPicA').writeAttribute({href:big});
	Event.stop(ele);
}

function refineHighlight() {
	new Effect.Highlight($('leftRefineSearchSubmit'), {duration: 3.0, startcolor:"#ffffff", endcolor: "#00ff00", restorecolor: "#eeefee"});
}
function gShow(id, event) {
	var el = $(id).up('li');
	if ( $(el).getStyle('zIndex') == '3') { 
		//console.log('zIndexwas three');
		null;
	}
	else {
		$(el).setStyle({zIndex:'3'});
	}
	$(id).show();	
	timer_var_left = 1;
	// this is for firefox.. w3 spec sez that z-index can only be set non absolute positioned elements
	// ie7 obeys firefox does not so we need to explicity set it on the absolute positioned ul here.
}
function gOut(id, event) {
	timer_var_left = 0;
}

function gHide(id) {
	if ((timer_var_left == 0) && (lock_var_left == 0)) {
		var el = $(id).up('li');
		$(el).setStyle({zIndex:'2'});
		$(id).hide();	
	}
	else {
		setTimeout(gHide.curry(id), 1199);
	} 
}

function displayFade(namid, event) {
	var etrig = Event.element(event);
	//console.log('Event:' + event.type + ' id ' + etrig.id);
	if (!(etrig.id == namid) || etrig.up('#'+ namid)) {
		//console.log('fading...');
		//new Effect.BlindUp($(namid).down('ul'), { duration: 0.4, queue: 'end' });
		$(namid).down('ul').hide();
		//console.log('about to stop event');
		//console.log('stopped event');
	}
	else  {
 		if (!$(namid).down('ul').visible()) {
			//console.log('appear');
			//new Effect.BlindDown($(namid).down('ul'), { duration: 0.4, queue: 'end' });
			$(namid).down('ul').show();
		}
	}
	//console.log('exiting display Fade');
}

function fAuctionsSetup() {
	//console.log('fAuctionsSetup');
	return $('featuredAuctionsItem').childElements();
}

function fAuctionsDisplay(incoming) {
	var prev = fAuctionsIndex[0];
	if (incoming == prev) { return; }
	if (!prev && prev != 0) {
		$R((incoming * fAuctionsNum), (incoming * fAuctionsNum) + fAuctionsNum - 1).each( function sh(ele) { if (fAuctions[ele]) { fAuctions[ele].show(); }});
		prev = 0;
	}
	else {
		$R(prev * fAuctionsNum, (prev * fAuctionsNum) + fAuctionsNum - 1).each( function sh(ele) { if (fAuctions[ele]) { fAuctions[ele].hide(); }});
		$R((incoming * fAuctionsNum), (incoming * fAuctionsNum) + fAuctionsNum - 1).each( function sh(ele) { if (fAuctions[ele]) { fAuctions[ele].show(); }});
	}

	/* tracking && links */
	fAuctionsIndex[1] = fAuctionsIndex[0];
	fAuctionsIndex[0] = incoming;
	pagingAuction(incoming, fAuctions.length, fAuctionsNum, fAuctionsNum - 1, fAuctionsNum - 1, 'fAuctionsDisplay');
}

function pagingAuction(incoming, len, nscreen, nprev, nnext, fname) {
	// incoming is zero based, so incoming - 1 can mean two previous links, and incoming + 3 means two next links 
	var fhtml = $R(incoming - 1 , incoming + fAuctionsNum).map(generatePagingLink.curry(fname, len, nscreen, incoming, 'number_active'));
	var leftArr;
	var rightArr;

	if (incoming <= 0) { 
		leftArr = ''; 
	} else {
		leftArr = '<a href="#" onclick="' + fname + '(' + (incoming - 1) + '); return false"><img border="0" style="margin-bottom: -6px;" src="http://images.dotmed.com/images/pages_arrow_left.gif" /></a>&nbsp;|&nbsp;'; 
	}

	if (incoming >= (Math.ceil(len/nscreen) -1)) { 
		rightArr = ''; 
	} else {
		rightArr = '&nbsp;|&nbsp;<a href="#" onclick="' + fname + '(' + (incoming + 1) + '); return false"><img border="0" style="margin-bottom: -6px;" src="http://images.dotmed.com/images/pages_arrow.gif" /></a>'; 
	}

	$('number').update(leftArr + fhtml.inject(0, addBars) + ' | of ' + '<a href="#" onclick="' + fname + '(' + ( Math.ceil(len/nscreen) - 1)  + '); return false;">' + Math.ceil(len/nscreen) + '</a> <a href="/auctions/featured/">Featured Auctions</a> Pages' + rightArr);
}

function generatePagingLink(fname, len, nscreen, incoming, cname, index) {
	if (index <= 0) { return; }
	if (index > (Math.ceil(len / nscreen))) { return; }
	if ((index - 1) == incoming) {
		return '<a href="#" class="' + cname + '" onclick="' + fname + '('+ (index - 1) + '); return false;">'+ index + '</a> ';
	}
	else {
		return '<a href="#" onclick="' + fname + '('+ (index - 1) + '); return false;">'+ index + '</a>  ';
	}
}	

function addBars(memo, element) {
		// initial case
		if (!memo) {
			memo = element;
			return memo;
		}
		// undefined element case
		else if (!element) {
			return (memo);
		}
		else {
			return (memo + ' | ' + element);
		}
}

var pic_vis;
var pic_invis;
var pic_first;
var pic_last;
var pic_total;
var pic_max;

function init_pic() {
	var total = $$('#pic .img').map( function(ele) { return ele.down('img').identify(); });
	//console.log(total);
	pic_first = total.first();	
	pic_last = total.last();	

	pic_max = 4;
	pic_vis = total.select( function(ele) { return ($(ele).getStyle('display') == 'inline'); });
	pic_invis = total.select( function(ele) { return ($(ele).getStyle('display') != 'inline'); });

	pic_total = pic_vis.length + pic_invis.length;

	if (pic_invis && pic_total > pic_max) {
		showarrow('R');
	}
/*
	console.log('invis ' + pic_invis);
	console.log('vis ' + pic_vis);
	console.log('----------------------');
*/
}

function mainPicCurrentId() {
	var index = $$('#pic .img').first().down('img').identify().sub('subPic', '');
	var n = parseInt(index, 10);
	return n;
}
function mainPicRight() {
	

	var ishow = pic_invis.shift();
	var ihide = pic_vis.shift();

	$(ihide).setStyle({'display':'none'});
	$(ishow).setStyle({'display':'inline'});	

 
	pic_vis.push(ishow);
	pic_invis.push(ihide);

	if (pic_total > pic_max) {	
		display_arrows();
	}
/*	
	console.log('invis ' + pic_invis);
	console.log('vis ' + pic_vis);
	console.log('----------------------');
*/
}

function mainPicLeft() {


	var ihide = pic_vis.pop();
	var ishow = pic_invis.pop();

	$(ihide).setStyle({'display':'none'});
	$(ishow).setStyle({'display':'inline'});	


	pic_vis.unshift(ishow);
	pic_invis.unshift(ihide);
	
	if (pic_total > pic_max) {	
		display_arrows();
	}

/*
	console.log('invis ' + pic_invis);
	console.log('vis ' + pic_vis);
	console.log('----------------------');
*/
}

function showarrow(ch) {
	$('arrow'+ch).setStyle({'display':'inline'});
}
function hidearrow(ch) {
	$('arrow'+ch).setStyle({'display':'none'});
}

function display_arrows() {
		if (pic_invis.detect( function(ele) { return ele == pic_first; } )) {
		showarrow('L');
	} else {
		hidearrow('L');
	}
		if (pic_vis.detect( function(ele) { return ele == pic_last; } )) {
		hidearrow('R');
	} else {
		showarrow('R');
	}
}
function toggle_help(id) {
	if (document.getElementById(id).style.visibility=='visible') {
		hide_help(id);
	} else {
		show_help(id);
	}
}
function show_help(id) {
		document.getElementById(id).style.visibility='visible';
	}
function hide_help(id) {
		document.getElementById(id).style.visibility='hidden';
  }
function show_stats(id) {
		document.getElementById(id).style.visibility='visible';
	}
function hide_stats(id) {
		document.getElementById(id).style.visibility='hidden';
}

function toggle_help2(id) {
	if (document.getElementById(id).style.display =='none') {
		document.getElementById(id).style.display ='';
	} else {
		document.getElementById(id).style.display ='none';
	}
}

function on_off(id, id2) {
		document.getElementById(id).style.display ='';
		document.getElementById(id2).style.display ='none';
}

function twopointoh_toggle(id) {
    var divid = $(id);
	if (divid.style.display == 'none') {
		new Effect.SlideDown(id);
	} else {
		new Effect.SlideUp(id, {queue: 'end'});
	}
}

var oldBgColors = new Array ();

function highlight(id)
{
	if (document.getElementById) 
	{
		if (!oldBgColors[id])
		{
			oldBgColors[id] = document.getElementById(id).style.backgroundColor ;
			if (!oldBgColors[id]) { oldBgColors[id] = 'transparent' }
			document.getElementById(id).style.backgroundColor = '#CCCCFF';
		}
		else {
			document.getElementById(id).style.backgroundColor = oldBgColors[id] ;
			oldBgColors[id] = '';
		}
	}
}
function limitText(limitField, limitNum) {
	if (limitField.value.length > limitNum) {
			limitField.value = limitField.value.substring(0, limitNum);
	}   
}
function copyText(fromField, toField) {
	var stuff = fromField.value;
	stuff = stuff.replace(/</g,'&lt;');
	stuff = stuff.replace(/>/g,'&gt;');
	toField.innerHTML=stuff.replace(/\n/g,"<br>");;
}
function make_and_toggle(div, parent) { 
	if(!document.getElementById(div)){
		var em=document.createElement('p');
		em.id=div;
		em.appendChild(document.createTextNode(''));
		var par = document.getElementById(parent);
		document.getElementById(parent).appendChild(em);
	}
	toggle_help(parent);
}
function dismiss_announcements() {
	new Effect.Fade('box');
	new Ajax.Request( '/ajax/components/ajax', {   parameters: '_comp=%2Fajax%2Fcomponents%2Fusers%2Fdismiss_announcements' } )
}

function preloadImages(){
	for(var i=0, images=[]; src=arguments[i]; i++){
	images.push(new Image());
	images.last().src = src;
	}
};
