if(typeof ASL==="undefined"||!ASL){var ASL={};}

ASL.dialog = {
	instance: null,
	init: function() {
		ASL.dialog.instance = $('<div></div>').dialog({
			autoOpen:false,
			modal:true
		});
		$('.asl-dialog').unbind('click.aslDialog').bind('click.aslDialog', function(e) {
			e.preventDefault();
			ASL.dialog.open($(this));
		});
	},
	open: function(el) {
		var opts = $.extend({},el.data('asldialog')),
			ctn = '';
		// Get a default dialog width if options width is not set or 0 or auto;
		if(typeof opts.width == 'undefined' || parseInt(opts.width == 0) || opts.width == 'auto') {
			var orig = $('#mainContainer'); // Primary container test
			if(!orig.length) {
				orig = $('body'); // Default fallback, if #mainContainer is not found
			}
			opts.width = orig.width()*.9; //90% of the width
		} else {
			// Default total padding of jQuery ui dialog is 1.2em on each side. Add this calculated padding to the requested width
			opts.width = parseInt(opts.width) + ((parseInt($('body').css('font-size')) * 1.2)*2);
		}
		
		// Gest a default dialog height if options is not set or 0 or auto
		if(typeof opts.height == 'undefined' || parseInt(opts.height == 0) || opts.height == 'auto') {
			opts.height = parseInt($(window).height()*.8); // 80% of the viewport
		}
		
		// Retrieve the dialog title
		if(el.attr('title') && typeof opts.title == 'undefined') {
			opts.title = el.attr('title');
		}
		// Modal display options
		opts.modal = ((typeof opts.modal != 'undefined') ? (!!opts.modal) : true);
		
		// Loop to sel all current dialog options
		for(opt in opts) {
			if(opts.hasOwnProperty(opt)) {
				ASL.dialog.instance.dialog('option', opt, opts[opt]);
			}
		}
		
		// If content target is defined, retrieve it
		if(typeof opts.content != 'undefined' && !!opts.content) {
			ctn = $(opts.content);
		}
		
		// If content is empty or not found and the current element is an anchor, build an iframe with the source.
		if((!!ctn || ctn.length == 0) && el.get(0).tagName=='A') {
			ctn = $('<iframe width="100%" scrolling="auto" height="100%" frameborder="0" marginheight="0" marginwidth="0" name="asl-dialog-content" id="asl-dialog-content" src="'+el.attr('href')+'"  allowTransparency="true"></iframe>');
		}

		// Throw an error and return false if no content is available
		if(!ctn || ctn.length == 0) {
			throw 'Dialog content is not set for "'+el+'"!';
			return false;
		}
		
		// Remove actual dialog content and append the new content beafore oppening the dialog.
		ASL.dialog.instance.empty().append(ctn).dialog('open');
		
	}
};
$(document).ready(function()
{
	
	$('.leadButton').removeAttr('onclick');
	//Shadowbox.init();
	ASL.dialog.init();
	
	// Bind the events for the search feature.
	bindSearchEvent();
	bindSearchClickEvent();
	bindDropdownClickEvent();
	bindDocClick();

	// Initiates the slider on the home page for used vehicles
	var featuredUsed = new Shiftr(
	{
		bindTo: '#featuredUsedVehiclesInner',
		animation: 'slide_horizontal',
		jumpAmount: 1
	}); 

});
$(window).load(function() {
	// Page content height fixing
	var $cc = $('#contentContainer'),
			$cw = $('div.contentWrapper:first'),
			cch = $cc.height()+parseInt(($cc.css('padding-top')||0),10)+parseInt(($cc.css('padding-bottom')||0),10);
	
	if(cch <= $cw.height()){
		$cc.css('min-height', $cw.height() - (cch-$cc.height()));
	}
});

/*
Written by Kyle Somogyi
*/
var qs=false;

function bindDropdownClickEvent() {
  $('.searchContainer .searchDropdown li').click(function() {
    $(this).parent().parent().prev().html($(this).html());
    $(this).parent().parent().prev().attr('value', $(this).attr('value'));
    var dropdown = $(this).parent().parent().attr('id');
    if(dropdown == "makeDropdown") {
	  $('#modelDropdown  li:first').click();
      $('#bodytypeDropdown  li:first').click();
    }
    hideDD();
	$('#usedModel').show();
    $(this).unbind('click');
    SetToSearchModel();
	//SetToSearchNewModel();
  });
}

var bindSearchEvent = function() {
	$('div.searchContainer .searchField').click(function(){
		if($(this).next().hasClass('dd')) {
			hideDD();
			if($(this).attr("id")=="usedMake") {
				$('#usedModel').show();
			}
		} else {
			hideDD();
			$(this).next().addClass('dd').show();
			if($(this).attr("id")=="usedMake") {
				$('#usedModel').hide();
			}
		}
	});
}

function bindSearchClickEvent() {
	$('.searchContainer .submit_button').click(function(e) {
	e.preventDefault();
	var target = $(this).hasClass('submit_button_new') ? 'new-inventory' : 'used',
		prefix = (target == 'new-inventory' ? 'new' : 'used'),
		make = validateSearch($('#'+prefix+'Make').attr('value')),
		model = validateSearch($('#'+prefix+'Model').attr('value')),
		price = validateSearch($('#'+prefix+'Price').attr('value'));
	window.location=target+'.php?s2=1&s0=0&search.make='+make+'&search.model='+model+'&search.price_end='+price;
  });
}


function validateSearch(val){
	return (val<=0 ? '':val);
}

function hideDD() {
  $('.searchContainer .dd').hide().removeClass('dd');
}

function bindFeaturedEvents()
{
	$('#featuredHeader .tab').click(function()
	{
		if($(this).hasClass('active') == false)
		{
			$('#featuredContainer .tab.active').removeClass('active').addClass('deactive');
			$(this).addClass('active').removeClass('deactive');
			var tt = $(this).attr('tab');
			$('#featuredBody .active').css('z-index', 20);
			$('#featuredBody #'+tt).css('z-index', 18).fadeIn('fast');
			$('#featuredBody .active').fadeOut('fast').removeClass('active');
			$('#'+tt).addClass('active');

			$('#featuredHeader .tab').unbind('click');
			bindFeaturedEvents();
		}
	});
}

function addBookmark(url, title)
{
	if(document.all)
		window.external.AddFavorite(url, title);
	else if(window.sidebar)
		window.sidebar.addPanel(title, url,'');
	else
		alert('To add a bookmark to this page, please press Ctrl+D.')
}

//Document click event to hide open search dropdowns
function bindDocClick()
{
  $(document).click(function(e)
  {
	$('#usedModel').show();
	if(!$(e.target).hasClass('searchField')) hideDD();
  });
}

function toggleSearchField(_elemID_tbh, _elemID_tbs)
{
	document.getElementById(_elemID_tbh).style.display = "none";
	document.getElementById(_elemID_tbs).style.display = "block";
}

