// JavaScript Document

var siteURI = "http://www.truckandtraileraccess.com/";

/* Document Ready (onLoad) */
/* ------------------------------------------------------------------------- */
$(document).ready(function(){
	// Auction Calendar
	if($("#show_aucCal")){
		initCal();
	}
	
	// Equipment Feed
	if($("#equip_feed")){
		//equipFeed();
	}
	
	// Show those elements that require javascript
	$('.compCheck, .mapTog').toggle();
	
	// Reset compare checkboxes
	$('#compareIDs').attr("value","");
	$('.compCheck').removeAttr('checked');
	
	// Auto focus form fields
	$('#fieldFocus').focus();
	
	// List table alternating row colors
  	$("#list_tbl tr:even, .list_tbl tr:even").css("background-color", "#F4F4F8");
	
	// Keyword search submit w/ enter key
	$("#keyword_search_input").keydown(function(e){
		if (e.keyCode == 13) {
			//alert(siteURI+"equipment/search/desc:"+$(this).val());
			window.location = siteURI+"equipment/search/desc:"+$(this).val();
		}
	});
	
	// Keyword search submit w/ magnifying glass button
	$("#keyword_search_button").click(function(){
		params = $('#keyword_search_input').val();
		
		if(params == 'Enter keywords here (ex. CAT D6R)')
		{
			params = "";
		}
		
		window.location = siteURI+"equipment/search/desc:"+params;
	});
	
	// Colapse all listing rows
	expColapse();
	
	// Keyword search submit w/ enter key
	$(".edit_thumb input").click(function(){
		if($(this).attr('checked') == true){
			$(this).next().stop(true, true).animate({opacity: .25}, 700);
		}else{
			$(this).next().stop(true, true).animate({opacity: 1}, 700);
		}
	});

});
/* ------------------------------------------------------------------------- */


/* Add equipment image source toggle */
/* ------------------------------------------------------------------------- */
function toggleImgSrc(){
	if($('#local_img_td input[type=radio]').is(':checked')){
		$('#url_img_td input[type=text]').attr('disabled', true);
		$('#local_img_td input[type=file]').attr('disabled', false);
	}else{
		$('#local_img_td input[type=file]').attr('disabled', true);
		$('#url_img_td input[type=text]').attr('disabled', false);
	}  
}


/* AuthLevel Select */
/* ------------------------------------------------------------------------- */
function authSel(){
	if(
	   $('select[name=Dealer_ID]').val() != 'fail' && 
	   ($('select[name=AuthLevel]').val() != 2 || $('select[name=AuthLevel]').val() != 3)){
		   $('select[name=AuthLevel] > option[value=3]').attr('selected','selected');
	}
}
/* ------------------------------------------------------------------------- */


/* Dealer Select */
/* ------------------------------------------------------------------------- */
function dealerSel(){
	if($('select[name=AuthLevel]').val() != 2 && $('select[name=AuthLevel]').val() != 3){
		   $('select[name=Dealer_ID] > option[value=fail]').attr('selected','selected');
	}
}
/* ------------------------------------------------------------------------- */


/* Toggle Containers */
/* ------------------------------------------------------------------------- */
function togCont(selector){
	$(selector).toggle('slow');
}
/* ------------------------------------------------------------------------- */


/* Toggle Slider Boxes */
/* ------------------------------------------------------------------------- */
function toggleSlider(contID)
{
	// toggle the slider state (open/close)
	$(contID).slideToggle(350).siblings().toggleClass("slider_arrow").toggleClass("slider_arrow_d");
}
/* ------------------------------------------------------------------------- */


/* Equipment Feed Ajax */
/* ------------------------------------------------------------------------- */
function equipFeed(){
	/* Ajax funciton to pull equipment feed */
	$.ajax({
		type: "POST",
		url: "/equipment/feed/",
		success: function(msg){
			$('#equip_feed ol').html( msg );
		}
	});
}
/* ------------------------------------------------------------------------- */


/* Auction Calendar Ajax */
/* ------------------------------------------------------------------------- */
function initCal(){
	var d = new Date();
		var months = new Array();
		months[0] = "Jan";
		months[1] = "Feb";
		months[2] = "Mar";
		months[3] = "Apr";
		months[4] = "May";
		months[5] = "Jun";
		months[6] = "Jul";
		months[7] = "Aug";
		months[8] = "Sep";
		months[9] = "Oct";
		months[10] = "Nov";
		months[11] = "Dec";
		
		var date = d.getFullYear()+' '+months[d.getMonth()];
		aucCal(date);
}

function aucCal(reqDate){
	/* Ajax funciton to navigate between months */
	
	$.ajax({
		type: "POST",
		url: "/auctions/getAucCal/"+reqDate,
		success: function(msg){
			//alert( msg );
			$('#show_aucCal').html( msg );
			$("#aucCont").fadeIn(500);
		}
	});
}

function aucList(reqDate){
	/* Ajax funciton to pull in auction records for a selected month */
	
	$.ajax({
		type: "POST",
		url: "/auctions/getAucList/"+reqDate,
		success: function(msg){
			//alert( msg );
			$('#aucCont').css("display","none");
			$('#aucCont').html( msg );
			$("#aucCont").fadeIn(500);
			$("#aucList_tbl a:odd").css("background", "#F3F3F3");
		}
	});
}
/* ------------------------------------------------------------------------- */


/* List Table Filter */
/* ------------------------------------------------------------------------- */
function listFilter(inputs, url)
{
	// Explode inputs
	arr = inputs.split(',');
	
	// Loop inputs and add them to url
	jQuery.each(arr, function() {
		if($('input#'+this).val() != ""){
			url = url+"/"+this+':'+$('input#'+this).val().replace(/[^\w\d-_\s]+/g, "");
		}
    });

	// Redirect;
	//alert(url);
	window.location = url;
}
/* ------------------------------------------------------------------------- */


/* Activate Record Toggle */
/* ------------------------------------------------------------------------- */
function activateToggle(ID, addr, mode, div)
{
	uri = addr+"/ID:"+ID+"/Mode:"+mode;
	
	// Ajax call
	$.ajax({
		type: "POST",
		url: uri,
		success: function(msg){
			// Update the toggle div
			$('#'+div).html(msg);
			return true;
		}
	});
}
/* ------------------------------------------------------------------------- */


/* Delete Record */
/* ------------------------------------------------------------------------- */
function deleteRec(ID, addr)
{
	// Ajax call
	$.ajax({
		type: "POST",
		url: addr+"/ID:"+ID,
		success: function(msg){
			$('tr#row_'+ID).html("<td colspan=\"99\" style=\"background:#FF9999;\">"+msg+"</td>").animate({opacity: 1.0}, 3000);
		}
	});
}
/* ------------------------------------------------------------------------- */


/* Compare Equipment */
/* ------------------------------------------------------------------------- */
function selCompare(id, str, mode)
{
	switch(mode)
	{
		case 'add':
			//alert('add '+id);
			$("#ckbx_"+id).attr("onClick", "selCompare('"+id+"', $('#compareIDs').val(), 'rmv');");
			str = str+','+id;
			$('#dLnk_'+id+', #cLnk_'+id).toggle();
			break;
		case 'rmv':
			//alert('rmv '+id);
			$("#ckbx_"+id).attr("onClick", "selCompare('"+id+"', $('#compareIDs').val(), 'add');");
			str = str.replace(','+id, '');
			$('#dLnk_'+id+', #cLnk_'+id).toggle();
			break;
		/*case 'drp':
			//alert('drp '+id);
			str = str.replace(id, '');
			str = str.replace(',,', ',');
			break;*/
	}
	
	$("#compareIDs").attr("value", str);
	//alert(str);
}

function compare()
{
	id = ($('#compareIDs').val()).replace(/^,/, '');
	var lnk = "/equipment/details/id:"+id.replace(/,/g, ' ');
	//alert(lnk);
	window.location = lnk;
}
/* ------------------------------------------------------------------------- */


/* Advanced Search Map */
/* ------------------------------------------------------------------------- */
var states_display = "";
var states_post = "";

function selectRegion(this_image, this_text, this_toggle){
	switch(this_toggle)
	{
		case '1':
			// Toggle the select graphics
			$("#map-"+this_image).fadeIn('slow');
			$("#lnk-"+this_image).attr("href","javascript: selectRegion('"+this_image+"', '"+this_text+"', '0')");
			
			states_display = states_display+", "+this_text;
			states_post = states_post+",'"+this_image+"'";
			break;
		default:
			$("#map-"+this_image).fadeOut('slow');
			$("#lnk-"+this_image).attr("href","javascript: selectRegion('"+this_image+"', '"+this_text+"', '1')");
			states_display = states_display.replace(', '+this_text, '');
			states_post = states_post.replace(",'"+this_image+"'", '');
			break;
	}
	
	$("#form_regions_textarea").html(states_display.replace(', ', ''));
	$("#state").attr("value", states_post.replace(',', ''));
}
/* ------------------------------------------------------------------------- */


/* Expand/Colapse Listing Row */
/* ------------------------------------------------------------------------- */
jQuery.fn.fadeToggle = function(s, fn){
	return (this.is(":visible"))
	? this.fadeOut(s, fn)
	: this.fadeIn(s, fn);
};

function expColapse(ID){
	$("#row_"+ID).stop(true, true).fadeToggle(1000);
}

function expColapseAll(){
	$(".listing_row").stop(true, true).fadeToggle(1000);
}
/* ------------------------------------------------------------------------- */


/* Equipment Detail Image Swap */
/* ------------------------------------------------------------------------- */
function imgSwap(src){
	$("#imgMain img").stop(true, true).animate({opacity: 0}, 700, function() {
		$("#imgMain img").attr('src', src.replace('_th','')).animate({opacity: 0}, 700).animate({opacity: 1}, 700);
	});
}
/* ------------------------------------------------------------------------- */

/* Email tracking and mailto */
/* ------------------------------------------------------------------------- */
function mail(dealer_id, machine_id, type, hash){
	//alert(siteURI+"dealer/email/"+dealer_id+"/"+machine_id+"/"+type+"/"+hash+"/");
	$.ajax({
		type: "POST",
		url: siteURI+"dealer/email/"+dealer_id+"/"+machine_id+"/"+type+"/"+hash+"/",
		success: function(msg){
			if (msg != 'false' && msg.indexOf('mailto:') == 0){
				window.location = msg;
			}
			else
			{
				alert(msg);
			}
		}
	});
}
/* ------------------------------------------------------------------------- */
