/*
 *	Set-up generic UI interactions that may be present on any screen.
 */
$(document).ready(function()
{		
	// Configure menu-range custom roll-overs.
	$("ul.menu-range li").hover(
		//Handle the roll-over.
		function()
		{
			var $this = $(this);
			$this.addClass($this.attr("id"));
		},
		//Handle the roll-out.
		function()
		{
			var $this = $(this);
			$this.removeClass($this.attr("id"));
		});	

	// Configure any tool-tip rollover actions.
	$(".tool-tip-container").hover(
		//Handle the roll-over.
		function()
		{
			$(this).find(".tool-tip").show();
			//Activate our SIFR.
			applySifr();
		},
		//Handle the roll-out.
		function()
		{
			$(this).find(".tool-tip").hide();
		});	

	//Activate our SIFR on tab change.
	$("ul.ui-tabs-nav > li > a").click(
	function()
	{
		//applySifr();
	});	
	
	//Activate our SIFR on tab change.
	$(".ui-lightbox-container").load(
	function()
	{
		//applySifr();
	});	
	
	// -- the rendering of sliders
	RenderSliders();
	
	RenderNewHtmlLists();
	
	// Add a sophus log for starting a car enquiry
	$("a.log-carenquiry").bind('click.log',
	function()
	{
		sophusLogWithLocation('EnquiryStarted');
	});
	
	// Add a sophus log for starting a car enquiry
	$("a.log-sendtoafriend").bind('click.log',
	function()
	{
		sophusLogWithLocation('SendToAFriendStarted');
	});
	
	$("a.log-leaflet").bind('click.log',
	function()
	{
		sophusLogWithLocation('Leaflet');
	});
	
	
	if($('.imgTickIcon').length > 0) 
	{
		var i;
		//alert($('.imgTickIcon').length);
		for(i = 0; i < $('.imgTickIcon').length; i ++)
		{
			if($('div.page-container')[0].firstChild.textContent == "National Compare")
			{
				$('.imgTickIcon')[i].src = "/assets/images/national/icons/tickIcon.png";
				$('.imgTickIcon')[i].alt = 'Tick Icon';
			}
			else
			{
				$('.imgTickIcon')[i].src = "/assets/images/dealer01/icons/tickIcon.png";
				$('.imgTickIcon')[i].alt = 'Tick Icon';
			}
		}
	}
	
	/********************************/
	$('div.thumbnails').find('span.thumbnails.image').click(
	function()
	{		
		//alert('I am clicked')
		if($(this).find("img")[0].src.indexOf("/assets/images/national/noImage_94x59.jpg") > 0)
		{
			return false;
		}
		else
		{
			$(this).parent().parent().parent().parent().parent()[0].firstChild.src = $(this).find("img")[0].src.replace('&Width=94&Height=59&','&Width=400&Height=263&');
		}
		
	});	
	
	
	/********************************/
	$('div.models.htmllist.clear.filter').find('input.quicksearch-list-button').click(
	function()
	{
	//alert('Right there');
	$('form.panel.quick-seach-panel.mp-search-form.mp-search-count').find('input.text.postcode.mp-control-data')[0].value = $('div.models.htmllist.clear.filter').find('input.quicksearch-list-postcode')[0].value;
	$('form.panel.quick-seach-panel.mp-search-form.mp-search-count').submit();
	});
	
	$('div.vehicle-list-container.results div.vehicle-item ul.menu.cta-menu a.icon.icon-left.compare').click(
	function(){	
		if($(this).text() != 'Remove from compare')
		{
			index = $('div.vehicle-list-container.results div.vehicle-item ul.menu.cta-menu a.icon.icon-left.compare').index(this);
			
			if($('form.criteria-panel.mp-search-form.mp-search-count').find('li.compare').find('div.vehicle-item').length == 3)
			{		
				var parameters = uiBox_GetQueryString("?width=634&height=70&top=10&target=divVehicleListContainer" + index + "&source=divCompareLimitContainer");
				uiBox_Show(parameters);					
				return false;
			}
		}
				
	});
	
	
	$('div.vehicle-container').find('ul.menu.cta-menu').find('a.icon.icon-left.compare').click(
	function(){	
		if($(this).text() != 'Remove from compare')
		{				
			if($('form.criteria-panel.mp-search-form.mp-search-count').find('li.compare').find('div.vehicle-item').length == 3)
			{	
				var parameters = uiBox_GetQueryString("?width=560&height=125&target=divVehicleContainer&source=divCompareLimitWarningContainer");
				uiBox_Show(parameters);	
				return false;								
			}			
		}
	});
	
});

// use third party logging with location replacement
function sophusLogWithLocation(location)
{
	var log = new String(tc_page_definition);
	log = log.replace('{location}',location);
	sophusLogFull(log);
}

// fire multiple third party log requests
function sophusMultipleLogs(logTags)
{
	if (logTags && logTags.length)
	{	
		for(i=0;i < logTags.length; i++)
		{
			sophusLogFull(logTags[i]);
		}
	}
}

// carry out third party logging
function sophusLogFull(logTag)
{	
	if (logTag == '' || logTag.indexOf('{') >= 0)
	{
		return;
	}
	
	if (tc_log)
	{
		tc_log(logTag);
		//alert(logTag);
	}
}

//function used to render all of the htmllists on the page currently
function RenderNewHtmlLists() {
	$('div.htmllist-container-new').each(function() {
		RenderNewHtmlList(this);
	});
}

//function used to render out a single htmllist
function RenderNewHtmlList(htmlListElement) {
	//get each of the htmllist elements
	var htmlListContainer = $(htmlListElement);
	var htmlListUiContainer = htmlListContainer.find('div.ui-htmllist-container-new');
	htmlListUiContainer.htmllistnew({
		delay:1500,
		onChange : function(event,index) {
			alert(index);
		}
	});
}