﻿$(document).ready(function()
{
	RenderAccordion();
});

//function used to setup our own accordion functionality
function RenderAccordion() {
	//setup the initial state
	$('ul.accordion').each(
		function() {		
			//add menu index
			$(this).find('li > a.header').each(
			    function(i) {
			        this.menuIndex = i;
			    }
			);
			
			//hide all content divs
			$(this).find('li > a.header').each(
			    function() {
			        hideAllPanels(this);
			    }
			);
			
				// get links that are selected
			if(($(this).hasClass('criteria-accordion'))) {

				hideAllPanels($(this).find('a#cmdmodels'));
				
				//if this is the initial page start with models, otherwise default to advanced search
				if($('input.postcode').val() == '') {
					showSelectedPanel($(this).find('li.criteria-models a.header'));
					//alert('No PostCode: ' + $('input.postcode').val());
				}
				else {
					//alert('PostCode Present: ' + $('input.postcode').val());
					showSelectedPanel($(this).find('li.criteria-advanced a.header'));
				}
			}
			else {
				//alert('Should not get here');
				var selectedLinks = $(this).find('li.selected a.header');
				//if (selectedLinks == null || selectedLinks.length == 0)
				selectedLinks = $(this).find('a.header:first');
	            
				// show content
				selectedLinks.each(
					function() {
						showSelectedPanel(this);
					}
				);
			}
		}
	);
	
	//setup the click events
	$('ul.accordion li a.header').click(
		function(event)	{
		
		
				
		//prevent the default click through
		event.preventDefault();
		
		hideAllPanels(this);
		
		showSelectedPanel(this);
	});
}

function showSelectedPanel(link)
{
    // now show the selected one
	$(link).next().show();
	
	// select link
	if (!$(link).hasClass("selected"))
	    $(link).addClass("selected");
	   
	// set scroll panel
	// we need a calc this prior as it differs between criteria, compare and vehicle view
	var height = $(link).next().height();
	if($(link)[0].id.indexOf('SearchCriteria') > -1) {
		$(link).next().find('.scroll-pane')
		.jScrollPane(
		{
			scrollbarWidth : 16,
			dragMinHeight : 30,
			width:250,
			height:height
		});
	}
	else {
		$(link).next().find('.scroll-pane')
		.jScrollPane(
		{
			scrollbarWidth : 16,
			dragMinHeight : 30,
			height:height
		});
	}
	
	if($(link)[0].id.indexOf('models') > -1) {
		DestroySliders();
	}
	else if($(link)[0].id.indexOf('advanced') > -1) {
		DestroySliders();
		RenderSliders();
	}
	
	// set menu index
	var menuIndex = $("form.criteria-panel input#menuindex");
	if (menuIndex != null && menuIndex[0] != null && link.menuIndex != null)
	    menuIndex[0].value = link.menuIndex;
	menuIndex = $("form.header input#menuindex");
	if (menuIndex != null && menuIndex[0] != null && link.menuIndex != null)
		menuIndex[0].value = link.menuIndex;
}

function hideAllPanels(link)
{
    // hide all content divs
	$(link).parents('ul.accordion').find('li > div.content').each(
		function() {
			$(this).hide();
			//$(this).css('height','0px');
			//$(this).css('overflow','hidden');
		}
	);
	
	// un select all a links
	$(link).parents('ul.accordion').find('li > a.header').each(
	    function() {
	        if ($(this).hasClass("selected"))
	            $(this).removeClass("selected");
	    }
	);
}