﻿var Index = new IndexClass();

//=======================================================================
// Index page class.
//=======================================================================
function IndexClass()
{
	var THIS = this;
	var blnExpanderExpanded = false;
	var intTestimonialPage = 1;
	var intFAQPage = 1;
	
	this.Name = 'Index';

	//===================================================================
	// Page load event handler.
	//===================================================================
	this.Page_Load = function Page_Load()
	{
		try
		{
			this.SlideShow = document.getElementById('SlideShow');
			this.LatestNews = document.getElementById('LatestNews');
			this.LatestNewsWrapper = document.getElementById('LatestNewsWrapper');
			this.LatestNewsScroller = document.getElementById('LatestNewsScroller');
			this.Content = document.getElementById('Content');
			this.ContentTable = document.getElementById('ContentTable');
			this.divExpandingContent = document.getElementById('divExpandingContent');
			this.divExpanderContent = document.getElementById('divExpanderContent');
			this.divExpanderTitle = document.getElementById('divExpanderTitle');
			this.divExpanderBottom = document.getElementById('divExpanderBottom');
			this.divExpanderContentShadow = document.getElementById('divExpanderContentShadow');
			
			setInterval( "Index.SlideSwitch()", 10000 );
			
			// Display the latest news.
			this.LatestNewsScroller.className = '';

			if (window.navigator.userAgent.indexOf('iPad') > -1)
			{
				document.addEventListener('touchmove', function(e){ e.preventDefault(); });
				THIS.LatestNewsScroll = new iScroll(THIS.LatestNewsScroller, {checkDOMChanges:false});
			}
		}
		catch(exc)
		{
			Functions.DisplayError(this.Name + '.Page_Load()\n\n' + exc);
		}
		return;
	}
	
	//===================================================================
	// Switch Slide using JQuery.
	//===================================================================
	this.SlideSwitch = function SlideSwitch()
	{
		var $active = $('#SlideShow IMG.active');

		if ($active.length == 0 ) $active = $('#SlideShow IMG:last');

		var $next =  $active.next().length ? $active.next()
			: $('#SlideShow IMG:first');

		$active.addClass('last-active');

		$next.css({opacity: 0.0})
			.addClass('active')
			.animate({opacity: 1.0}, 1000, function() {
				$active.removeClass('active last-active');
			});
		return;
	}
	
	//===================================================================
	// Page unload event handler.
	//===================================================================
	this.Page_UnLoad = function Page_UnLoad()
	{
		try
		{
			this.Dispose();
		}
		catch(exc)
		{
			Functions.DisplayError(this.Name + '.Page_UnLoad()\n\n' + exc);
		}
		return;
	}
	
	//==================================================
	// Dispose.
	//==================================================
	this.Dispose = function Dispose()
	{
		try
		{
			for (Element in THIS)
			{
				if (typeof (THIS[Element]) != 'function')
				{
					if ((THIS[Element] != undefined) || (THIS[Element] != null))
					{
						if (Element != 'undefined')
						{
							THIS[Element] = null;
							delete THIS[Element];
						}
					}
				}
			}

			delete THIS;
		}
		catch(exc)
		{
			Functions.DisplayError(this.Name + '.Dispose()\n\n' + exc);
		}
		return;
	}

	//===================================================================
	// Window onresize event handler.
	//===================================================================
	this.OnWindowResize = function OnWindowResize()
	{
		try
		{
			var intChildNode = 1;
			var intContentOffsetHeight = 4;
			var intNewsOffsetHeight = 0;
			var intNewsOffset = 60;
			var intSlideOffset = 80;
			var intExpanderOffset = 0;
			var intExpanderContainerOffset = 0;
			var intNewsHeaderOffset = 31;

			//various fixed for IE
			if (BrowserDetect.browser == 'Explorer')
			{
				intChildNode = (BrowserDetect.version > 8) ? intChildNode : 0;
				intContentOffsetHeight = (BrowserDetect.version == 9) ? 1 : intContentOffsetHeight;
				intContentOffsetHeight = (BrowserDetect.version == 10) ? 1 : intContentOffsetHeight;
				intNewsOffset = (BrowserDetect.version < 8) ? -4 : intNewsOffset;
				intSlideOffset = (BrowserDetect.version < 8) ? 17 : intSlideOffset;
				intExpanderOffset  = (BrowserDetect.version < 8) ? 63 : intExpanderOffset;
				intExpanderOffset  = (BrowserDetect.version == 9) ? (intExpanderOffset + 1) : intExpanderOffset;
				intExpanderContainerOffset = (BrowserDetect.version == 6) ? 12 : intExpanderContainerOffset;
				intNewsHeaderOffset = (BrowserDetect.version < 8) ? 30 : intNewsHeaderOffset;
			}

			intNewsOffsetHeight = ((BrowserDetect.browser == 'Explorer') && (BrowserDetect.version > 8)) ? 1 : intNewsOffsetHeight;

			// Check if running on OSX.
			if ((BrowserDetect.OS == 'Mac') || (BrowserDetect.OS == 'Linux'))
				intNewsOffsetHeight = intNewsOffsetHeight - 3;
			
			this.LatestNews.style.height = (MasterPage.WindowHeight - THIS.SlideShow.offsetHeight - (intNewsOffset - intNewsOffsetHeight)) + intNewsHeaderOffset + 'px';
			this.LatestNewsWrapper.style.height = (MasterPage.WindowHeight - THIS.SlideShow.offsetHeight - (intNewsOffset - intNewsOffsetHeight)) + 3 + 'px';
			
			// Resize the elements in the content panel.
			for(var intCounter = 0; intCounter < this.ContentTable.rows[1].cells.length; intCounter++)
			{
				var objDiv = this.ContentTable.rows[1].cells[intCounter].childNodes[intChildNode];
				objDiv.style.height = (MasterPage.WindowHeight - THIS.SlideShow.offsetHeight - intSlideOffset) - intContentOffsetHeight + 'px';
			}
			
			if(blnExpanderExpanded)
			{
				this.divExpandingContent.style.height = MasterPage.WindowHeight - 29 + intExpanderOffset + 'px';
				this.divExpanderContent.style.height = MasterPage.WindowHeight - this.ExpanderTakenHeight  - 30 + intExpanderOffset + intExpanderContainerOffset + 'px';
			}
			
			if (THIS.LatestNewsScroll)
				THIS.LatestNewsScroll.refresh();
		}
		catch(exc)
		{
			//Functions.DisplayError(this.Name + '.Page_Resize()\n\n' + exc);
		}
		return;
	}
	
	
	//===================================================================
	// Show Expander
	//===================================================================
	this.ShowExpander = function ShowExpander(ExpandingArea, ShowButtons)
	{
		THIS.aExpanderPrevious = document.getElementById('aExpanderPrevious');
		THIS.aExpanderNext = document.getElementById('aExpanderNext');
		
		if(ShowButtons)
		{
			THIS.aExpanderPrevious.style.display = '';
			THIS.aExpanderNext.style.display = '';
		}
		else
		{
			THIS.aExpanderPrevious.style.display = 'none';
			THIS.aExpanderNext.style.display = 'none';
		}
		
		this.divExpanderTitle.innerHTML = ExpandingArea;		
		this.divExpanderTitle.className = 'ExpanderTitle ' + 'ExpanderTitle' + ExpandingArea.replace(/\./gi, '');
		
		this.divExpandingContent.style.display = 'block';
		
		if(this.ExpanderTakenHeight == null)
		{
			this.ExpanderTakenHeight = this.divExpanderContentShadow.offsetHeight + this.divExpanderTitle.offsetHeight + this.divExpanderBottom.offsetHeight + 2;
			this.divExpanderContentShadow.style.display = 'none';
			this.divExpanderTitle.style.display = 'none';
			this.divExpanderBottom.style.display = 'none';
			
			if (BrowserDetect.browser == 'Explorer')
			{
				var intExpanderContainerOffset  = (BrowserDetect.version == 6) ? 12 : 0;
				this.ExpanderTakenHeight  = (BrowserDetect.version < 8) ? this.ExpanderTakenHeight  - 15 - intExpanderContainerOffset : this.ExpanderTakenHeight;
			}
		}
		
		window.setTimeout('Index.ExpanderExpanding(0,\'' + ExpandingArea + '\')', 50);
		
		return;
	}
	
	//===================================================================
	// Expander Expanding
	//===================================================================
	this.ExpanderExpanding = function ExpanderExpanding(intCurrentHeight, ExpandingArea)
	{
		//extra 1 just to hit the top
		var intMaxHeight = MasterPage.WindowHeight - 30 + 1;		
		
		if (BrowserDetect.browser == 'Explorer')
		{
			intMaxHeight = (BrowserDetect.version < 8) ? intMaxHeight + 63 : intMaxHeight;
			intMaxHeight = (BrowserDetect.version > 8) ? intMaxHeight + 1 : intMaxHeight;
		}
		
		var intCurrentHeight = intCurrentHeight + 50;
		
		//if height is greater than the title and button bar then show them
		if(intCurrentHeight > this.ExpanderTakenHeight)
		{
			this.divExpanderContentShadow.style.display = '';
			this.divExpanderTitle.style.display = '';
			this.divExpanderBottom.style.display = '';
		}
		
		if(intCurrentHeight > intMaxHeight)
		{
			this.divExpandingContent.style.height = intMaxHeight + 'px';
			this.divExpanderContent.style.height = intMaxHeight - this.ExpanderTakenHeight + 'px';
		}
		else
		{
			this.divExpandingContent.style.height = intCurrentHeight + 'px';
			
			if(intCurrentHeight > this.ExpanderTakenHeight)
				this.divExpanderContent.style.height = intCurrentHeight - this.ExpanderTakenHeight + 'px';
		}
		
		if(intCurrentHeight < intMaxHeight)
			window.setTimeout('Index.ExpanderExpanding('+intCurrentHeight +',\''+ ExpandingArea +'\')', 50);
		else
			this.LoadExpanderContent(ExpandingArea, 0);
		return;
	}
		
	//===================================================================
	// Expander Done now get the content then fade it in
	//===================================================================
	this.LoadExpanderContent = function LoadExpanderContent(ExpandingArea, PageIndex)
	{	
		if(ExpandingArea != null)
			THIS.ExpandingArea = ExpandingArea;
		else
			ExpandingArea = THIS.ExpandingArea;
			
		if(PageIndex == -1 && THIS.aExpanderPrevious.className == 'Grey')
		return;
		
		if(PageIndex == 1 && THIS.aExpanderNext.className == 'Grey')
		return;		
		
		var intReferenceID = 0;
		var strFiringProcedure = "";
		blnExpanderExpanded = true;	
		THIS.CurrentPage = 1;
		
		var soapClient = new SoapClient();
		
		switch(ExpandingArea)
		{
			case "About":
				intReferenceID = 2;
				strFiringProcedure = 'CMSContent_SelectByRef';
			break;
			case "Testimonials":
				intReferenceID = 1;
				strFiringProcedure = 'Content_SelectByCategory_WithPaging';
				
				intTestimonialPage = intTestimonialPage + PageIndex;				
				THIS.CurrentPage = intTestimonialPage;
				
				soapClient.AddParameter('ItemsPerPage', 1);
			break;
			case "F.A.Q":
				intReferenceID = 2;
				strFiringProcedure = 'Content_SelectByCategory_WithPaging';
				
				intFAQPage = intFAQPage + PageIndex;				
				THIS.CurrentPage = intFAQPage;
				
				soapClient.AddParameter('ItemsPerPage', 4);
			break;
		}			
		
		soapClient.AddParameter('CurrentPage', THIS.CurrentPage);
		soapClient.AddParameter('ReferenceID', intReferenceID);		
		soapClient.SendRequest("/webservices/Common.asmx", 'http://www.truckfile.co.uk/', strFiringProcedure, CMSContent_Select_Return);
		return;
	}
	
	//================================================
	// CMSContent_Select_Return
	//================================================
	function CMSContent_Select_Return(Response)
	{
		var aResponse = Response.split('|');
		
		if(!CorrectResponse(aResponse))
		return;
		
		var CMSRecordSet = new RecordSet();
		CMSRecordSet.LoadXML(aResponse[1],"CMSContent");
		
		var CMSPageCount = new RecordSet();
		CMSPageCount.LoadXML(aResponse[1],"CMSContent1");				
		
		this.divExpanderContent = document.getElementById('divExpanderContent');
		
		this.divExpanderContent.style.opacity = 0;
		this.divExpanderContent.style.MozOpacity = 0;
		
		var strContent = '';
		var DottedLine = '<div class="CMSSeparater"><div>';
		
		while (!CMSRecordSet.EOF())
		{
			if(strContent != '')
				strContent = strContent + DottedLine;
			
			strContent = strContent + Functions.CleanHTMLContent(CMSRecordSet['Content']);
			
			CMSRecordSet.MoveNext();
		}
		
		var intPageCount = 0;
		
		while (!CMSPageCount.EOF())
		{
			intPageCount = CMSPageCount['Pages'];
			CMSPageCount.MoveNext();
		}
		
		THIS.PageSelector(intPageCount);
		
				
		this.divExpanderContent.innerHTML = strContent;			
		
		window.setTimeout("Index.FadeInExpander(0)", 50);
		return
	}
	
	//===================================================================
	// PageSelector
	//===================================================================
	this.PageSelector = function PageSelector(PageCount)
	{
		if(THIS.CurrentPage > 1)
		{
			THIS.aExpanderPrevious.className = '';
		}
		else
		{
			THIS.aExpanderPrevious.className = 'Grey';
		}
		
		if(THIS.CurrentPage < PageCount)
		{
			THIS.aExpanderNext.className = '';
		}
		else
		{
			THIS.aExpanderNext.className = 'Grey';
		}
		return;
	}
	
	//===================================================================
	// FadeInExpander fade in the content gradually
	//===================================================================
	this.FadeInExpander = function FadeInExpander(intOpacity)
	{		
		intOpacity = intOpacity + 0.1;
				
		this.divExpanderContent.style.opacity = intOpacity;
		this.divExpanderContent.style.MozOpacity = intOpacity;
		
		if(intOpacity < 1)
			window.setTimeout('Index.FadeInExpander('+intOpacity+')', 50); 
		return;
	}
	
	//===================================================================
	// GoReturnBottom
	//===================================================================
	this.ExpanderCollapse = function ExpanderCollapse()
	{
		var intMaxHeight = MasterPage.WindowHeight - this.ExpanderTakenHeight;
		
		window.setTimeout('Index.ExpanderCollapsing(' + intMaxHeight + ')', 50);
		return;
	}
	
	//===================================================================
	// Expander Collapsing
	//===================================================================
	this.ExpanderCollapsing = function ExpanderCollapsing(intCurrentHeight)
	{
		blnExpanderExpanded = false;		
		intCurrentHeight = intCurrentHeight - 50;		
		this.divExpanderContent.innerHTML = '';
		
		//if height is smaller than the title and button bar then hide them
		if(intCurrentHeight < this.ExpanderTakenHeight)
		{
			this.divExpanderContentShadow.style.display = 'none';
			this.divExpanderTitle.style.display = 'none';
			this.divExpanderBottom.style.display = 'none';
		}
		
		//resize the content if the result is below 0 then set all sizes to 0
		if(intCurrentHeight < 0)
		{
			this.divExpandingContent.style.height = '0px';
			this.divExpanderContent.style.height = '0px';
		}
		else
		{
			this.divExpandingContent.style.height = intCurrentHeight + 'px';
			
			if(intCurrentHeight > this.ExpanderTakenHeight)
				this.divExpanderContent.style.height = intCurrentHeight - this.ExpanderTakenHeight + 'px';
		}
		
		// if still can be resized then continue
		if(intCurrentHeight > 0)
			window.setTimeout('Index.ExpanderCollapsing('+intCurrentHeight+')', 50);
		return;
	}
	
	//================================================
	// check response has not errored
	//================================================
	function CorrectResponse(aResponse)
	{
		// Check if an error occured.
		if (aResponse[0] == 'ERROR')
		{
			HtmlPopup.Error(aResponse[1]);
			return false;
		}
		return true;
	}
	
	//===================================================================
	// Article Clicked.
	//===================================================================
	this.ArticleClicked = function ArticleClicked(ContentID)
	{
		window.location = 'News.aspx?Article=' + ContentID;
		return;
	}

	return;
}
