/**
 * SWFAddress v1.0: Deep linking for Flash websites - http://www.asual.com/swfaddress/
 *
 * SWFAddress is (c) 2006 Rostislav Hristov and is released under the MIT License:
 * http://www.opensource.org/licenses/mit-license.php
 *
 * Dependencies: 
 * SWFObject v2.0 - (c) 2006 Geoff Stearns.
 * http://blog.deconcept.com/swfobject/
 */
if(typeof asual == "undefined") var asual = new Object();
if(typeof asual.util == "undefined") asual.util = new Object();
asual.util.Functions = new function() {
    this.extend = function(superclass, subclass) {
        function inheritance() {}
        inheritance.prototype = superclass.prototype;
        subclass.prototype = new inheritance();
        subclass.prototype.constructor = subclass;
        subclass.superConstructor = superclass;
        subclass.superClass = superclass.prototype;
        return subclass;
    }
    this.bindAsListener = function(method, object, win) {
        return function(evt) {
            return method.call(object, evt || ((win) ? win.event : window.event));
        }
    }    
}
asual.util.Events = new function() {
    var cache = new Array();
    this.addListener = function (obj, type, listener) {
        if (obj.addEventListener){
            obj.addEventListener(type, listener, false);
        } else if (obj.attachEvent){
            obj.attachEvent('on' + type, listener);
        } else {
            obj['on' + type] = listener;        
        }
        cache.push({o: obj, t: type, l: listener});
    }
    this.removeListener = function (obj, type, listener) {  
        if (obj.removeEventListener){
            obj.removeEventListener(type, listener, false);
        } else if (obj.attachEvent){
            obj.detachEvent('on' + type, listener);
        } else {
            obj['on' + type] = listener;
        }
    }
    var unload = function() {
        for (var i = 0, evt; evt = cache[i]; i++) {
            asual.util.Events.removeListener(evt.o, evt.t, evt.l);
        }
    }
    this.addListener(window, 'unload', asual.util.Functions.bindAsListener(unload, this));    
}
asual.SWFAddress = new function() {

	var ie = /MSIE/.test(navigator.userAgent);    
	var gecko = /Gecko\//.test(navigator.userAgent);
	var opera = /Opera\//.test(navigator.userAgent);
	var safari = /AppleWebKit/.test(navigator.userAgent);
    
    var container
	var swfaddr;
    var swfid;
    var swftitle = document.title;
    var swflength = history.length;
    var swfhistory = new Array();
    
    var js = 'swfaddress.js';
    var html = 'swfaddress.html';
    var hash = location.hash;    
    
    var listen = function() {
        if (safari) {
            if (swflength != history.length) {
                swflength = history.length;
                hash = swfhistory[swflength - 1];
                update();
            }        
        } else {
            if (hash != location.hash) {
                hash = location.hash;
                update();
            }
        }
    }
    var update = function() {
        var addr = hash.replace(/#/gi, '');
        if (addr != swfaddr) {
            swfaddr = addr;
            if (document && document.getElementById(swfid)) {
                document.getElementById(swfid).setSWFAddressValue(swfaddr);
            }
        }
	}
    var loadSuccess = function() {
        if (container.contentWindow && container.contentWindow.location) {
            var win = container.contentWindow;
            win.document.title = top.document.title = swftitle;
            var src = win.location.href;
            if (src.indexOf('?') > -1) {
                hash = '#' + src.substring(src.indexOf('?') + 1);
            } else {
                hash = '#';
            }
            if (hash != location.hash) {
                update();            
                location.hash = hash;
            }
        }
    }
    var load = function() {
        if (ie) {
            var content = document.createElement('div');
            content.id = 'swfaddress';
            var scripts = document.getElementsByTagName('script');
            for (var i = 0, s; s = scripts[i]; i++) {
                if (s.src.indexOf(js) > -1) {
                    html = (new String(s.src)).replace(js, html);
                }
            }
            content.innerHTML = '<iframe src="' + html + location.hash.replace(/#/gi, '?') + '" frameborder="no" scrolling="no"></iframe>';
            document.body.appendChild(content);
            container = content.getElementsByTagName('iframe')[0];
            container.style.display = 'none';
            asual.util.Events.addListener(container, 'load', asual.util.Functions.bindAsListener(loadSuccess, this));  
        } else if (safari) {
            var content = document.createElement('form');
            content.id = 'swfaddress';        
            content.method = 'get';
            document.body.appendChild(content);
        }
    }
    this.getId = function() {
        return swfid;
    }
    this.setId = function(id) {
        swfid = id;
    }
    this.getTitle = function() {
        return swftitle;
    }
    this.setTitle = function(title) {
        if (title == 'null') {
            title = '';
        }        
        if (typeof title != 'undefined') {
            swftitle = title;
            top.document.title = swftitle;
        }
    }
    this.getValue = function() {
        return hash.replace(/#/gi, '');
    }
    this.setValue = function(addr) {
        if (addr == 'null') {
            addr = '';
        }
        if (swfaddr == addr) {
            return;   
        }
        swfaddr = addr;
        if (addr.indexOf('#') > -1) {
            hash = '#' + addr.substring(addr.indexOf('#') + 1);
        } else {
            hash = '#' + addr;
        }
        update();
        if (safari) {
            var f = document.getElementById('swfaddress');
            f.action = hash;
            swfhistory.push(hash);
            swflength = history.length + 1;            
            f.submit();
        } else {
            location.hash = hash;
        }
        if (ie) {
            var win = container.contentWindow;
            var query = '?' + hash.substring(hash.indexOf('#') + 1);
            win.location.assign(win.location.pathname + query);
        }
    }
    if (ie) {
        if (hash == '') {
            location.hash = '#';
        }        
    } else {
        if (safari) {
            for (var i = 1; i < swflength; i++) {
                swfhistory.push('');
            }
            swfhistory.push(location.hash);
        }
        setInterval(listen, 50);
    }    
    asual.util.Events.addListener(window, 'load', asual.util.Functions.bindAsListener(load, this));    
    swfaddr = this.getValue();
    update();
}
asual.SWFAddressObject = asual.util.Functions.extend(deconcept.SWFObject, 
    function(swf, id, w, h, ver, c, quality, xiRedirectUrl, redirectUrl, detectKey) {
	asual.SWFAddressObject.superConstructor.apply(this, arguments);
    asual.SWFAddress.setId(id);
});	
SWFObject = deconcept.SWFObject = asual.SWFAddressObject;	
SWFAddress = asual.SWFAddress;
