/*
 * Author:		Adam Collison [adam.collison@digitaloverload.co.uk]
 * Agency:		Digital Overload Ltd.
 * Client: 		BGD Group
 * Project: 	Nissan Dealer Network / Aftersales
 * Comment:		Main Scripts
 */


$(document).ready(function() { 
			
	$('.external').attr('target','_blank');
	$('.external').attr('title','[This link will open in a new window]');
			
	$('.header > ul > li').mouseover(function() {
		
		$(this).addClass('selected');
		
		$(this).children('ul').show();
		
		$(this).mouseout(function() { 
			$(this).children('ul').hide();										 
			$(this).removeClass('selected');
		});
	
	});
	
	$('.sub-content > ul > li > a').hover(
		function() {
			$(this).css({'text-decoration':'none'});
			$(this).parent().addClass('btn-selected');
			$(this).siblings('.glow').css({'background-position':'0 -5px'});
				
		}
		,
		function() {
			$(this).parent().removeClass('btn-selected');
			$(this).siblings('.glow').css({'background-position':'0 0'});
		}
	);
	
	$('.feature-links > li > a').hover(function() {
											   
		$(this).css({'text-decoration':'none'});
	
		$(this).parent().addClass('btn-selected');
		$(this).siblings('.glow').css({'background-position':'0 -5px'});
			
	}
	,function() {
		$(this).parent().removeClass('btn-selected');
		$(this).siblings('.glow').css({'background-position':'0 0'});
	});
		
		
	$('.vehicles .img img').hover(function() {
										   
		var oldSrc = $(this).attr('src');
		
		if(oldSrc.indexOf("-selected.jpg") == -1) {
			var newSrc = oldSrc.replace(/.jpg/,"-selected.jpg");
			$(this).attr('src',newSrc);
			$(this).addClass('highlight');
		}
			
	}
	,function() {
		
		var oldSrc = $(this).attr('src');
	
		if(!$(this).hasClass('selected')) {
			var newSrc = oldSrc.replace(/-selected.jpg/,".jpg");
			$(this).attr('src',newSrc);
			$(this).removeClass('highlight');
		}
		
	});
	

	var chosenDate;
	$('#prevWeek img').attr('src','/media/images/btn-previous-week-text.gif');

	
	var StartPoint = parseInt($('#dateRange').attr('class'));
	$('#nextWeek').click(function() {

		StartPoint = parseInt(StartPoint) + 7;
		chosenDate = $('#chosenDate').val();
		
		$('#weekStartInput').attr('value',StartPoint);
	
		$('#dateRange td.dateObject').each(function(){
			var weekStart = parseInt(StartPoint) + parseInt($(this).attr('headers'));
			var td = $(this);
			$.ajax({
				type: "GET",
			   	url: "/media/ajax/service-date.php",
			   	data: "weekStart=" + weekStart + "&chosenDate=" + chosenDate,
			   	dataType: "text",
			   	success: function(data, textStatus) {
			   		td.html(data);
			   	}
			});
		});
		
		
		return false;
	
	});
	$('#prevWeek').click(function() {

		StartPoint = parseInt(StartPoint) - 7;
		chosenDate = $('#chosenDate').val();

		$('#weekStartInput').attr('value',StartPoint);
								  
		$('#dateRange td.dateObject').each(function(){
			var weekStart = parseInt(StartPoint) + parseInt($(this).attr('headers'));
			var td = $(this);
			$.ajax({
				type: "GET",
			   	url: "/media/ajax/service-date.php",
			   	data: "weekStart=" + weekStart + "&chosenDate=" + chosenDate,
			   	dataType: "text",
			   	success: function(data, textStatus) {
			   		td.html(data);
			   	}
			});
		});
						
		return false;
	
	});
	
	$("input[name='date']").click(function(){
		$('#chosenDate').val($(this).val());
	});
	
	var currentVarientPanel = '';
	
	$('#vehicle-selector a').click(function(){

		id = $(this).attr('rel');
		var ajaxURL = '/includes/ajax/new-vehicle-model-panel.php?model-id=' + id;
				
		if(ajaxURL == currentVarientPanel)
		{
			return false;	
		}
		currentVarientPanel = ajaxURL;

		$.ajax({
			type: "GET",
			url: ajaxURL,
			dataType: "html",
			success: function(data) {
				$('.featured-vehicle').fadeOut('fast',function() {
				
					$('#vehicle-selector .img a').each(function(){
						if($(this).attr('rel') == id)
						{
							$(this).children('img').addClass('selected');
						}
						else 
						{
							$(this).children('img').removeClass('highlight');
							$(this).children('img').removeClass('selected');
							$(this).children('img').attr('src',$(this).children('img').attr('src').replace(/-selected.jpg/,'.jpg'));	
						}
					})
					
					$('.featured-vehicle').replaceWith(data).fadeIn('normal');
					attachTermsAction();
					attachPrintAction();
				
				})
				
			}
		});
		
		return false;
	
	});
	
		$('#used-vehicle-selector a').click(function(){

		id = $(this).attr('rel');
		var ajaxURL = '/includes/ajax/used-vehicle-model-panel.php?model-id=' + id;
				
		if(ajaxURL == currentVarientPanel)
		{
			return false;	
		}
		currentVarientPanel = ajaxURL;

		$.ajax({
			type: "GET",
			url: ajaxURL,
			dataType: "html",
			success: function(data) {
				$('.featured-vehicle').fadeOut('fast',function() {
				
					$('#used-vehicle-selector .img a').each(function(){
						if($(this).attr('rel') == id)
						{
							$(this).children('img').addClass('selected');
						}
						else 
						{
							$(this).children('img').removeClass('highlight');
							$(this).children('img').removeClass('selected');
							$(this).children('img').attr('src',$(this).children('img').attr('src').replace(/-selected.jpg/,'.jpg'));	
						}
					})
					
					$('.featured-vehicle').replaceWith(data).fadeIn('normal');
					attachTermsAction();
					attachPrintAction();
				
				})
				
			}
		});
		
		return false;
	
	});
	
	$('#new-vehicle-selector a').click(function(){
	
		id = $(this).attr('rel');
		var ajaxURL = '/includes/ajax/offer-variant-panel.php?variant-id=' + id;

		var imgLinkObject = $(this).parents('li').children('.img').children('a');
		
		if(ajaxURL == currentVarientPanel || imgLinkObject.children('img').hasClass('selected'))
		{
			return false;	
		}
		currentVarientPanel = ajaxURL;

		$.ajax({
			type: "POST",
			url: ajaxURL,
			dataType: "html",
			success: function(data) {
				$('.featured-vehicle').fadeOut('fast',function() {
				
					$('#new-vehicle-selector a img').each(function(){
						$(this).removeClass('highlight');
						$(this).removeClass('selected');
						$(this).attr('src',$(this).attr('src').replace(/-selected.jpg/,'.jpg'));											   
					})
					
					$('.featured-vehicle').replaceWith(data).fadeIn('normal');
					attachTermsAction();
					attachPrintAction();
					var newSrc = imgLinkObject.children('img').attr('src').replace(/.jpg/,'-selected.jpg');
					imgLinkObject.children('img').attr('src',newSrc);

					imgLinkObject.children('img').addClass('selected');
				})
				
			}
		});
		
		return false;
	
	});	
	
	$('.vehicle-content .equipment, .vehicle-content .spec').hide();
	
	//if a used vehicle tab is clicked
	$('.vehicle-details .actions li a').click(function() {
		
		//hide all other first
		$('.vehicle-content div').hide();
		
		//make all buttons inactive
		$('.vehicle-details .actions li a img').each(function() {
			if($(this).attr('src').indexOf('-inactive.gif') == -1) 
			{
				var src = $(this).attr('src').replace(/.gif/,'-inactive.gif');
				$(this).attr('src',src)
			}
		});
		
		var src = $(this).children('img').attr('src').replace(/-inactive.gif/,'.gif');
		$(this).children('img').attr('src',src)
		
		var section = $(this).attr('id');
		$('.'+section).fadeIn('fast');	
		
		return false;
		
	});
	
	//preload images and fade first image
	var thumbCounter = 0;
	$('.thumbs li a img').each(function() {
		thumbCounter++;
		if(thumbCounter == 1)
		{
			$(this).fadeTo('fast',0.5);				 
		}
		var src = $(this).attr('src').replace(/w=120&h=75/,'w=250&h=156');
		$('body').append('<img src="'+src+'" class="preloader" />');
	});
	
	/* Used Vehicle image preview */
	$('.thumbs li a').click(function() {
		//fade back all images to 100%
		$('.thumbs li a img').fadeTo('fast',1);				 
		$(this).children('img').fadeTo('fast',0.5);				 
		var src = $(this).children('img').attr('src').replace(/w=120&h=75/,'w=250&h=156');
		$('#large-vehicle-image').fadeTo('fast',0.01,function() {
			$(this).attr('src',src);
			$(this).fadeTo('fast',1);
		});	
		return false;
	});
	
	$('.print').css({ display:'block' });
	$('.print a').click(function() {
		window.print();		
		return false;
	});
	
	$('.terms-link').click(function() {
		$('.terms').slideToggle('fast');	
		return false;
	});
	

});

	
function getDateTime(data) {
	$('#chosenDate').val(data);
}

function attachTermsAction() {
	
	$('.terms-link').bind("click",function() {
		$('.terms').slideToggle('fast');	
		return false;
	});
	
}

function attachPrintAction() {
	
	$('.print a').bind("click",function() {
		window.print();		
		return false;
	});
	
}
