$(document).ready(function(){             
	
	 DD_belatedPNG.fix('#logo, a span.end, #header #main-navigation li a span.icon, #header #main-navigation li.home a, #footer ul li h3 .icon, #wrapper #header #sub-navigation, #wrapper #search-strip, #wrapper #feature-container #featured-listing, #wrapper #slides-nav-controls #next, #wrapper #slides-nav-controls #prev, #header #sub-navigation .call strong, #page h1 span, .infobar, .pages, .contact, .call img, .callto a span, .footer-banner');
	
	// slideshow
	if($('#featured-listing').children('li').length > 1){
		$('#feature-container .panel-head h3').after('<div id="slides-nav-controls"><span id="prev"><</span><span id="next">></span></div>');
	}
	
	$('#featured-listing')
	.cycle({
		fx:'fade',
		speed:  500,
		timeout: 5000,
		prev: '#prev',
		next: '#next'
	});
	
	$('.home #slideshow')
	.cycle({
		fx:'fade',
		speed:  500,
		timeout: 5000,
		prev: '#prev',
		next: '#next'
	});
	
	$('#tbtestimonials-widget')
	.cycle({
		fx:'fade',
		speed:  500,
		timeout: 5000
	});
	
    $("#search-strip select, #search-strip input:checkbox, .vehicle-inspection input:radio, #search-strip input:file, #search-strip input:submit, .finance-calculator input:submit, #Search input:submit, #Search input:radio, #Search select, #Search input:checkbox, #Search select, form.customcontactform input:submit, #Contact input:submit, #Contact select, .vehicle-inspection input:submit, .finance-calculator input, .finance-calculator select").uniform();      
	$('#uniform-model').prepend('<div id="model-loader" class="loader"></div>');
	$('#uniform-make').prepend('<div id="make-loader" class="loader"></div>');  
    var arrInps = [];
    arrInps.push( { obj: document.getElementById("model"), defaultValue: "Model (Any)" } );
    
    for (var i=arrInps.length; i--;)
    {
        var inp = arrInps[i];
        inp.obj.value = inp.defaultValue;
        inp.obj.onclick = getInpEvent(inp);
    }
    $('#main-navigation li ul').css('z-index', '9999');
	
	// Top nav dropdowns
	var contwidth = $('#wrapper').width(), // get wrapper width
		offset = parseInt($('#main-navigation li ul').css('padding-left'), 10), // add any left padding of the nav container to the offset variable as a starting value
		tabwidths = [], posleft, $dropdown, dropdownwidth, tabidx, tabheight;
	
   
	// loop through all top level lis
	$('#main-navigation li ul').each(function(i) {
		tabwidths[i] = $(this).outerWidth(); // use true to include the margin as well
		$dropdown = $(this); // find the dropdown
		tabheight = $(this).outerHeight(); 
		
		// skip the first tab
		if(i > 0) {
			offset += tabwidths[--i]; // update offset variable with the previous tab's width
		}

		if($dropdown) { // check we have a dropdown to show
            dropdownwidth = $(this).width() * 1.9; // calculate dropdown width
			$dropdown.css({
				'width': dropdownwidth + 'px' // set dropdown div width
			});
			dropdownwidth = $dropdown.outerWidth(); // update dropdownwidth with the outer width of the dropdown incase it contains any padding
			
			// check to see if the dropdown is too big to be positioned either way
			if(offset + dropdownwidth > contwidth && dropdownwidth - tabwidths[i] > offset) {
				// centre align the dropdown relative to the wrapper
				posleft = Math.round(offset - ((contwidth - dropdownwidth) / 2));
				$dropdown.css('left', '-' + posleft + 'px');
			}
			else {
				// check if the offset + dropdown width is greater than wrap width
				if(offset + dropdownwidth > contwidth) {
					// position dropdown to the right
					$dropdown.css({
						'left': 'auto',
						'right': '0'
					});
				}
			}
		}
	});

	// handle hiding and showing of dropdowns on tab hover
	$('#main-navigation li:has(ul)').hover(
		function() {
			$(this).children('ul').show(); // show the div
			$('#search-strip form select').hide();
		},
		function() {
			$(this).children('ul').hide();
			$('#search-strip form select').show(); 
		}
	);
	
	$('#main-navigation li ul').hover(
		function() {
			$('#search-strip form select').css('visibility','hidden');
		},
		function() {
			$('#search-strip form select').css('visibility','visible'); 
		}
	);
	
	$(function() {
           var zIndexNumber = 15000;
           // Put your target element(s) in the selector below!
           $("div").each(function() {
                   $(this).css('zIndex', zIndexNumber);
                   zIndexNumber -= 5;
           });
    });

   //var iframe = $('<iframe />', {
   //		css: {
   //			'width': '980px',
   //			'height': '55px',
   //			'position': 'absolute',
   //			'background-color': 'transparent',
   //			'border': '0',
   //			'left': '0',
   //			'top': '0',
   //			'allowTransparency': 'true'
   //		},
   //		'src': '#',
   //		'allowTransparency': 'true',
   //		'frameBorder': '0'
   //	})
   //	.appendTo('#search-strip form');

    
		 $('#search-strip #make').change(function() {
		 	$.ajax({
		 		type : 'POST',
		 		url : 'http://www.drivewisecontracts.co.uk/search-post.php',
		 		data: {
		 			make : $(this).val(),
		 			type : $('#search-strip #category').val(),
		 			model : $('#search-strip #model').val()
		 		},
		 		dataType: 'json',
		 		cache: false,
		 		beforeSend: function() {
		 	       $('#model-loader').show();
		 		   $('#uniform-model span').html('');
		 		   $('#uniform-model #model').html('');
		 	    },
		 	    complete: function(){
		 	       $('#model-loader').hide();
		 	    },
		 		success: function(data) {
		 		    $('#uniform-model #model').html(data.models);
		 			var firstmodel = $('#uniform-model #model option:first').html();
		 			$('#uniform-model span').html(firstmodel);
		 			$('#model-loader').hide();
		 		}
		 	});
		 	return false;
		});
	
		$('#search-strip #category').change(function() {
			$.ajax({
				type : 'POST',
				url : 'http://www.drivewisecontracts.co.uk/search-post.php',
				data: {
					type : $(this).val(),
					make : '',
					model : ''
				},
				dataType: 'json',
				cache: false,
				beforeSend: function() {
			       $('#make-loader').show();
			       $('#uniform-make span').html('');
				   $('#uniform-make #make').html('');
				   $('#model-loader').show();
			       $('#uniform-model span').html('');
				   $('#uniform-model #model').html('');
			    },
			    complete: function(){
			       $('#make-loader').hide();
				   $('#model-loader').hide();
			    },
				success: function(data) {
					$('#uniform-make #make').html(data.makes);
					$('#uniform-model #model').html(data.models);
					$('#make-loader').hide();
					var firstmake = $('#uniform-make #make option:first').html();
					var firstmodel = $('#uniform-model #model option:first').html();
					$('#uniform-make span').html(firstmake);
					$('#uniform-model span').html(firstmodel);
					console.log(data.debug)
					$('#model-loader').hide();
				}
			});
			return false;
	   });
});

    
function getInpEvent(inp){
    return function() {
        if (this.value == inp.defaultValue){
            this.value = "";
        }
    }
}
