/* 
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)
*/
 
var currentHash = null;
//var isSafariBased = (typeof(window.webkit) != "undefined") && window.webkit;
var isSafariBased = navigator.userAgent.toLowerCase().indexOf("safari") != -1;

unFocus.History.addEventListener('historyChange', historyListener);

function GetDefaultFlashPageTitle() {
    
    if(typeof(defaultFlashPageTitle) == "undefined" || defaultFlashPageTitle == null || defaultFlashPageTitle == "") {
        return "Fiat.co.uk | Showroom";
    }
    else {
        return defaultFlashPageTitle;
    }
}

function goForward(hash) {
    goForwardWithTitle(hash, null);
}

/*
Called by Flash
*/
function goForwardWithTitle( hash, title ) {
    currentHash = hash;

    DoubleClickTracking(hash);
    // set the new history hash
    if (!isSafariBased) {
        unFocus.History.addHistory( hash );
    }
    if (title != null) {
        document.title = title;
    }
    else {
        document.title = GetDefaultFlashPageTitle();
    }
    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);
    }
    return;
}

/*
History callback
@param historyHash: The text after the # in the URL
*/
function historyListener( historyHash ) {
    var swfName = "fiat";
    var movie;
    
    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();
}



