// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
//
// Title : G-Forces Web Management Ltd Main Home Page functions
// Author : Marlon King / marlon.king@gforces.co.uk / G-Forces Web Management Ltd
//
// Description : Functionality for home page
//
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

var cmsTimer;

function prevCms( halt ){

	if ( halt ) {
		stopRotating = true;
	}

	switch( currentCms ){
		case 'offersNew':
			offersNewCount = ( offersNewCount > 0 ) ? offersNewCount - 1 : ( offersNew.length - 1 );
			fetchCms( offersNewCount, offersNew, 'offersCmsArea', true );
		break;
		case 'offersUsed':
			offersUsedCount = ( offersUsedCount > 0 ) ? offersUsedCount - 1 : ( offersUsed.length - 1 );
			fetchCms( offersUsedCount, offersUsed, 'offersCmsArea', true );
		break;
		case 'offersAftersales':
			offersAftersalesCount = ( offersAftersalesCount > 0 ) ? offersAftersalesCount - 1 : ( offersAftersales.length - 1 );
			fetchCms( offersAftersalesCount, offersAftersales, 'offersCmsArea', true );
		break;
		case 'offersCommercial':
			offersCommercialCount = ( offersCommercialCount > 0 ) ? offersCommercialCount - 1 : ( offersCommercial.length - 1 );
			fetchCms( offersCommercialCount, offersCommercial, 'offersCmsArea', true );
		break;
		case 'offersFord':
			offersFordCount = ( offersFordCount > 0 ) ? offersFordCount - 1 : ( offersFord.length - 1 );
			fetchCms( offersFordCount, offersFord, 'offersCmsArea', true );
		break;
		case 'offersMazda':
			offersMazdaCount = ( offersMazdaCount > 0 ) ? offersMazdaCount - 1 : ( offersMazda.length - 1 );
			fetchCms( offersMazdaCount, offersMazda, 'offersCmsArea', true );
		break;
		case 'offersFiat':
			offersFiatCount = ( offersFiatCount > 0 ) ? offersFiatCount - 1 : ( offersFiat.length - 1 );
			fetchCms( offersFiatCount, offersFiat, 'offersCmsArea', true );
		break;
		case 'offersKia':
			offersKiaCount = ( offersKiaCount > 0 ) ? offersKiaCount - 1 : ( offersKia.length - 1 );
			fetchCms( offersKiaCount, offersKia, 'offersCmsArea', true );
		break;
		case 'offersEag':
			offersEagCount = ( offersEagCount > 0 ) ? offersEagCount - 1 : ( offersEag.length - 1 );
			fetchCms( offersEagCount, offersEag, 'offersCmsArea', true );
		break;
		case 'offersScrappage':
			offersScrappageCount = ( offersScrappageCount > 0 ) ? offersScrappageCount - 1 : ( offersScrappage.length - 1 );
			fetchCms( offersScrappageCount, offersScrappage, 'offersCmsArea', true );
		break;
	}
}

function prevWallpaper(){

	wallpapersCount = ( wallpapersCount > 0 ) ? wallpapersCount - 1 : ( wallpapers.length - 1 );
	fetchCms( wallpapersCount, wallpapers, 'wallpapersCmsArea',true );
}

function changeCmsArea( newArea ){

	stopRotating = true;

	currentCms = newArea;

	switch( currentCms ){
		case 'offersNew':
			fetchCms( offersNewCount, offersNew, 'offersCmsArea', true );
		break;
		case 'offersUsed':
			fetchCms( offersUsedCount, offersUsed, 'offersCmsArea', true );
		break;
		case 'offersAftersales':
			fetchCms( offersAftersalesCount, offersAftersales, 'offersCmsArea', true );
		break;
		case 'offersCommercial':
			fetchCms( offersCommercialCount, offersCommercial, 'offersCmsArea', true );
		break;
		case 'offersFord':
			fetchCms( offersFordCount, offersFord, 'offersCmsArea', true );
		break;
		case 'offersMazda':
			fetchCms( offersMazdaCount, offersMazda, 'offersCmsArea', true );
		break;
		case 'offersFiat':
			fetchCms( offersFiatCount, offersFiat, 'offersCmsArea', true );
		break;
		case 'offersKia':
			fetchCms( offersKiaCount, offersKia, 'offersCmsArea', true );
		break;
		case 'offersVolvo':
			fetchCms( offersVolvoCount, offersVolvo, 'offersCmsArea', true );
		break;
		case 'offersScrappage':
			fetchCms( offersScrappageCount, offersScrappage, 'offersCmsArea', true );
		break;
		case 'offersEag':
			fetchCms( offersEagCount, offersEag, 'offersCmsArea', true );
		break;
	}

	clearTimeout( cmsTimer );

}

function nextCms( halt ){

	if ( halt ) {
		stopRotating = true;
	}

	switch( currentCms ){
		case 'offersNew':
			offersNewCount = ( offersNewCount < ( offersNew.length - 1 ) ) ? offersNewCount + 1 : 0;
			fetchCms( offersNewCount, offersNew, 'offersCmsArea', true );
		break;
		case 'offersUsed':
			offersUsedCount = ( offersUsedCount < ( offersUsed.length - 1 ) ) ? offersUsedCount + 1 : 0;
			fetchCms( offersUsedCount, offersUsed, 'offersCmsArea', true );
		break;
		case 'offersAftersales':
			offersAftersalesCount = ( offersAftersalesCount < ( offersAftersales.length - 1 ) ) ? offersAftersalesCount + 1 : 0;
			fetchCms( offersAftersalesCount, offersAftersales, 'offersCmsArea', true );
		break;
		case 'offersCommercial':
			offersCommercialCount = ( offersCommercialCount < ( offersCommercial.length - 1 ) ) ? offersCommercialCount + 1 : 0;
			fetchCms( offersCommercialCount, offersCommercial, 'offersCmsArea', true );
		break;
		case 'offersFord':
			offersFordCount = ( offersFordCount < ( offersFord.length - 1 ) ) ? offersFordCount + 1 : 0;
			fetchCms( offersFordCount, offersFord, 'offersCmsArea', true );
		break;
		case 'offersMazda':
			offersMazdaCount = ( offersMazdaCount < ( offersMazda.length - 1 ) ) ? offersMazdaCount + 1 : 0;
			fetchCms( offersMazdaCount, offersMazda, 'offersCmsArea', true );
		break;
		case 'offersFiat':
			offersFiatCount = ( offersFiatCount < ( offersFiat.length - 1 ) ) ? offersFiatCount + 1 : 0;
			fetchCms( offersFiatCount, offersFiat, 'offersCmsArea', true );
		break;
		case 'offersKia':
			offersKiaCount = ( offersKiaCount < ( offersKia.length - 1 ) ) ? offersKiaCount + 1 : 0;
			fetchCms( offersKiaCount, offersKia, 'offersCmsArea', true );
		break;
		case 'offersVolvo':
			offersVolvoCount = ( offersVolvoCount < ( offersVolvo.length - 1 ) ) ? offersVolvoCount + 1 : 0;
			fetchCms( offersVolvoCount, offersVolvo, 'offersCmsArea', true );
		break;
		case 'offersEag':
			offersEagCount = ( offersEagCount < ( offersEag.length - 1 ) ) ? offersEagCount + 1 : 0;
			fetchCms( offersEagCount, offersEag, 'offersCmsArea', true );
		break;
		case 'offersScrappage':
			offersScrappageCount = ( offersScrappageCount < ( offersScrappage.length - 1 ) ) ? offersScrappageCount + 1 : 0;
			fetchCms( offersScrappageCount, offersScrappage, 'offersCmsArea', true );
		break;
	}

}

function nextWallpaper(){

	wallpapersCount = ( wallpapersCount < ( wallpapers.length - 1 ) ) ? wallpapersCount + 1 : 0;
	fetchCms( wallpapersCount, wallpapers, 'wallpapersCmsArea', true );
}

function rotateOffers(){

	cmsTimer = setTimeout( "changeViaRotate(currentCms)", 6000 );

}

function rotateOffersFranchise(){

	cmsTimerFranch = setTimeout( "changeViaRotateFranchise()", 6000 );

}

function changeViaRotateFranchise(){
	if ( !stopRotating ) {
		nextCms();
		rotateOffersFranchise();
	}
}



function changeViaRotate(cms){

	if( currentCms != cms ){

		// Someone has manually changed the offers so stop rotation.
	}else{

		switch( cms ){

			case 'offersFord':
				fetchCms( offersFordCount, offersFord, 'offersCmsArea', false );
				currentCms = 'offersMazda';
			break;

			case 'offersMazda':
				fetchCms( offersMazdaCount, offersMazda, 'offersCmsArea', false );
				currentCms = 'offersFiat';
			break;

			case 'offersFiat':
				fetchCms( offersFiatCount, offersFiat, 'offersCmsArea', false );
				currentCms = 'offersKia';
			break;

			case 'offersKia':
				fetchCms( offersKiaCount, offersKia, 'offersCmsArea', false );
				currentCms = 'offersScrappage';
			break;
			case 'offersVolvo':
				fetchCms( offersVolvoCount, offersVolvo, 'offersCmsArea', false );
				currentCms = 'offersVolvo';
			break;

			case 'offersAftersales':
				fetchCms( offersAftersalesCount, offersAftersales, 'offersCmsArea', false );
				currentCms = 'offersNew';
			break;

			case 'offersCommercial':
				fetchCms( offersCommercialCount, offersCommercial, 'offersCmsArea', false );
				currentCms = 'offersAftersales';
			break;

			case 'offersNew':
				fetchCms( offersNewCount, offersNew, 'offersCmsArea', false );
				currentCms = 'offersUsed';
			break;

			case 'offersUsed':
				fetchCms( offersUsedCount, offersUsed, 'offersCmsArea', false );
				currentCms = 'offersScrappage';
			break;

			case 'offersScrappage':
				fetchCms( offersScrappageCount, offersScrappage, 'offersCmsArea', false );
				currentCms = 'offersFord';
			break;
			/*
			case 'offersEag':
				fetchCms( offersEagCount, offersEag, 'offersCmsArea', false );
				currentCms = 'offersFord';
			break;
            */
		}
		rotateOffers();
	}
}

function displayLoadingCms( element ){

	$( element ).innerHTML = '<div style="padding:80px 0px 0px 145px;"><img src="' + baseHref + '/images/layup/loading.gif" alt="loading..." style="margin-left:10px;" /><br />Loading...</div>';
}

function fetchCms( cmsCount, cmsArray, cmsElement, loadShow ){


    if( $('totalNumberWallpaper') ){
        $('totalNumberWallpaper').innerHTML = wallpapers.length;
        $('currentNumberWallpaper').innerHTML = wallpapersCount+1;
    }

    if( $('totalNumber') ){
        $('totalNumber').innerHTML = cmsArray.length;
        $('currentNumber').innerHTML = cmsCount+1;
    }


	if( loadShow ){
		displayLoadingCms( cmsElement );
	}
	var target = 'ajaxHome.php';
	var params = 'type=nextCmsItem&id=' + cmsArray[cmsCount];

	var myAjax = new Ajax.Updater((cmsElement),target,
	{
		method: 'post',
		parameters: params

	});
}


function prevGroupNews( $area ){

	switch( $area ){
		case 'news':
			currentEagNewsCount = ( currentEagNewsCount > 0 ) ? currentEagNewsCount - 1 : ( newsEag.length - 1 );
			fetchGroupNews( currentEagNewsCount, newsEag, 'newsEagContentArea', true );
			$('eagCount').update( ( currentEagNewsCount + 1 ) + ' of ' + newsEag.length );
		break;

		case 'community':
			currentCommunityCount = ( currentCommunityCount > 0 ) ? currentCommunityCount - 1 : ( community.length - 1 );
			fetchGroupNews( currentCommunityCount, community, 'communityContentArea', true );
			$('communityCount').update( ( currentCommunityCount + 1 ) + ' of ' + community.length );
		break;

		case 'careers':
			currentCareersCount = ( currentCareersCount > 0 ) ? currentCareersCount - 1 : ( careers.length - 1 );
			fetchGroupNews( currentCareersCount, careers, 'careersContentArea', true );
			$('careersCount').update( ( currentCareersCount + 1 ) + ' of ' + careers.length );
		break;
	}
}


function nextGroupNews( $area ){

	switch( $area ){
		case 'news':
			currentEagNewsCount = ( currentEagNewsCount < ( newsEag.length - 1 ) ) ? currentEagNewsCount + 1 : 0;
			fetchGroupNews( currentEagNewsCount, newsEag, 'newsEagContentArea', true );
			$('eagCount').update( ( currentEagNewsCount + 1 ) + ' of ' + newsEag.length );
		break;

		case 'community':
			currentCommunityCount = ( currentCommunityCount < ( community.length - 1 ) ) ? currentCommunityCount + 1 : 0;
			fetchGroupNews( currentCommunityCount, community, 'communityContentArea', true );
			$('communityCount').update( ( currentCommunityCount + 1 ) + ' of ' + community.length );
		break;

		case 'careers':
			currentCareersCount = ( currentCareersCount < ( careers.length - 1 ) ) ? currentCareersCount + 1 : 0;
			fetchGroupNews( currentCareersCount, careers, 'careersContentArea', true );
			$('careersCount').update( ( currentCareersCount + 1 ) + ' of ' + careers.length );
		break;
	}


}

//-------------------FRANCHISE LEVEL--------------------------

function prevNews(){
	currentNewsCount = ( currentNewsCount > 0 ) ? currentNewsCount - 1 : ( news.length - 1 );
	fetchNews( currentNewsCount, news, 'newsContentArea' );

	$('newsCountCurrent').update( ( currentEagNewsCount + 1 ) + ' of ' + newsEag.length );
}

function nextNews(){
	currentNewsCount = ( currentNewsCount < ( news.length - 1 ) ) ? currentNewsCount + 1 : 0;
	fetchNews( currentNewsCount, news, 'newsContentArea' );

	$('newsCountCurrent').update( ( currentEagNewsCount + 1 ) + ' of ' + newsEag.length );
}

function prevEagNews(){
	currentEagNewsCount = ( currentEagNewsCount > 0 ) ? currentEagNewsCount - 1 : ( newsEag.length - 1 );
	fetchNews( currentEagNewsCount, newsEag, 'newsEagContentArea' );
}

function nextEagNews(){
	currentEagNewsCount = ( currentEagNewsCount < ( newsEag.length - 1 ) ) ? currentEagNewsCount + 1 : 0;
	fetchNews( currentEagNewsCount, newsEag, 'newsEagContentArea' );
}

function displayLoading( element ){

	$( element ).innerHTML = '<div style="padding:80px 0px 0px 105px;"><img src="' + baseHref + '/images/layup/loading.gif" alt="loading..." style="margin-left:10px;" /><br />Loading...</div>';
}

function displayGroupLoading( element ){

	$( element ).innerHTML = '<div style="padding:80px 0px 0px 170px;"><img src="' + baseHref + '/images/layup/loading.gif" alt="loading..." style="margin-left:10px;" /><br />Loading...</div>';
}

function fetchNews( currentNewsCount, news, newsElement ){

    if( newsElement == 'newsEagContentArea' ){
        $('totalNumberEAGNews').innerHTML = news.length;
        $('currentNumberEAGNews').innerHTML = currentNewsCount+1;
    }else{
        $('totalNumberNews').innerHTML = news.length;
        $('currentNumberNews').innerHTML = currentNewsCount+1;
    }

    displayLoading( newsElement );
	var target = 'ajaxHome.php';
	var params = 'type=nextNewsItem&id=' + news[currentNewsCount];

	var myAjax = new Ajax.Updater((newsElement),target,
	{
		method: 'post',
		parameters: params

	});
}

function fetchGroupNews( currentNewsCount, news, newsElement, showLoad ){


	if( showLoad )displayGroupLoading( newsElement );
	var target = 'ajaxHome.php';
	var params = 'type=nextGroupNewsItem&id=' + news[currentNewsCount];

	var myAjax = new Ajax.Updater((newsElement),target,
	{
		method: 'post',
		parameters: params

	});
}