/* 
Flash history management solution 
@notes:				swfName is a variable set via backend generated code 
@author:			Richard Leggett 
@updated 31/03/09:	Steve Smith		|	Decoupled HistoryManager, now relies on MooTools for browser detection. 
										Moved third party history keeper code into seperate file (library/js/thirdParty/historyKeeper.js)
@updated 19/07/11:  Edd Bignell     |   Updated HistoryKeeer to version 2.0B4 (library/js/thirdParty/unFocus-History-p.js) - now works with Chrome.
*/
 
var currentHash = null;
unFocus.History.addEventListener('historyChange', historyListener);

function GetDefaultFlashPageTitle() {    
    if(typeof(defaultFlashPageTitle) == "undefined" || defaultFlashPageTitle == null || defaultFlashPageTitle == "") {
        return "Fiat UK | Fiat New Cars & Used Cars | View New Fiat Car Models";
    }else{
        return defaultFlashPageTitle;
    }
};

function goForward(hash) {
    if(typeof disableHistoryKeeper === "undefined"){ // HistoryKeeper can be disabled by adding disableHistoryKeeper = true to the code infront.
    goForwardWithTitle(hash, null);
    }
};

/*
Called by Flash
*/
function goForwardWithTitle( hash, title ) {
    currentHash = hash;
    DoubleClickTracking(hash);
	highlightModelNavSection(hash);	
    // set the new history hash
    unFocus.History.addHistory(hash);
    if (title != null) {
        document.title = title;
    }else{
        document.title = GetDefaultFlashPageTitle();
    }
	 FiatCoUk.NavManager.showHideModelMenu();
    return;
}
function DoubleClickTracking(hash) {
    //alert(hash); // for debugging
    if (AddDartFloodlightTag != null && AddDartFloodlightTag != 'undefined') {
        // Call the webservice to get the dart tag by hash
        var request = new Request({ url: '/webservices/DartTagService.asmx/GetDartTag', onSuccess: DoubleClickTrackingSucceeded });
        request.send('hashTag=' + hash);
    }
    return;
}
function DoubleClickTrackingSucceeded(rawXml, xmlDocument) {
    var dart = null;
    dart = xmlDocument.lastChild.textContent;
    if (dart == null || dart == '') dart = xmlDocument.text;
    //alert('Dart tag is: ' + dart); // for debugging
    AddDartFloodlightTag(dart);
    return;
}
/*
Called by Flash
*/
function goBackward( hash ) {
    currentHash = hash;
    if(!isSafariBased) {
        history.go(-1);
    }
	FiatCoUk.NavManager.showHideModelMenu();
    return;
}

/*
History callback
@param historyHash: The text after the # in the URL
*/
function historyListener(historyHash) {
    var swfName = "fiat-main-flash";
    var movie;
    if (document.getElementById(swfName) == null || document.getElementById(swfName) == 'undefined') { //if fiat-main-flash does not exist try swfObjectContainer
        swfName = "swfObjectContainer";
    }
	if(document.getElementById(swfName)) {
		movie = document.getElementById(swfName);
	}else{
		movie = ( navigator.appName.indexOf("Microsoft") != -1 ) ? window[swfName] : document[swfName];
	}
    // If link did not originate from Flash movie
	if (currentHash != historyHash && currentHash != null && movie.handleDeepLink) {
		movie.handleDeepLink(historyHash);
    }
    document.title = GetDefaultFlashPageTitle();
}

function highlightModelNavSection(hash) { 
    var section;
    var $el;
    section = hash.toString();
    section = section.slice(section.lastIndexOf('/')+1);
    jQuery('#modelNav li').removeClass('current');
    jQuery('#modelNav li > a[href$="#'+hash+'"]').closest('li').addClass("current");
}



