/**
 * SWFAddress v1.1: Deep linking for Flash - 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
 *
 * Recommended Dependency:  
 * SWFObject v1.5 - (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.Browser = new function() {
    var agent = navigator.userAgent;
    this.supported = false;
    this.ie = false;
    this.gecko = false;
    this.safari = false;
    this.opera = false;
    if (/MSIE/.test(agent)) {
        this.ie = true;
        this.supported = parseFloat(agent.substring(agent.indexOf('MSIE') + 4)) >= 6;
    } else if (/AppleWebKit/.test(agent)) {
        //this.safari = true;
        //this.supported = parseFloat(agent.substring(agent.indexOf('Safari') + 7)) >= 312;
				this.safari = true;
				var safariVersion = parseFloat(agent.substring(agent.indexOf('Safari') + 7)); this.supported = ((safariVersion >= 312) && (safariVersion<522));
    } else if (/Opera/.test(agent)) {
        this.opera = true;
        this.supported = parseFloat(navigator.appVersion) >= 9.02;
    } else if (/Firefox/.test(agent)) {
        this.gecko = true;
        this.supported = parseFloat(agent.substring(agent.indexOf('Firefox') + 8)) >= 1;
    } else if (/Camino/.test(agent)) {
        this.gecko = true;
        this.supported = parseFloat(agent.substring(agent.indexOf('Camino') + 7)) >= 1;
    } else if (/Netscape/.test(agent)) {
        this.gecko = true;
        this.supported = parseFloat(agent.substring(agent.indexOf('Netscape') + 9)) >= 8;
    } else if (/Mozilla/.test(agent) && /rv:/.test(agent)) {
        this.gecko = true;
        this.supported = parseFloat(agent.substring(agent.indexOf('rv:') + 3)) >= 1.8;
    }
    if (!this.supported && top.location.href.indexOf('#') != -1) {
        document.write('<meta http-equiv="refresh" content="0;url=' + top.location.href.substr(0, top.location.href.indexOf('#')) + '" />');
    }
}
asual.util.Cookie = new function() {
	
	
	
    this.write = function(value) {
        var date = new Date();
        date.setTime(date.getTime() + (3*8640000));
        var expires = '; expires=' + date.toGMTString();
        document.cookie = 'swfaddress=' + value + expires + '; path=' + top.location.pathname;
    }
    this.read = function() {
        var name = 'swfaddress=';
        var cookies = document.cookie.split(';');
        for (var i = 0, c; c = cookies[i]; i++) {
            while (c.charAt(0) == ' ') {
                c = c.substring(1);
            }
            if (c.indexOf(name) == 0) {
                return c.substring(name.length);
            }
        }
    }    
}
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.detachEvent){
            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);
        }
    }
    if (asual.util.Browser.ie || asual.util.Browser.safari) {    
        this.addListener(window, 'unload', asual.util.Functions.bindAsListener(unload, this));    
    }
}
asual.SWFAddress = new function() {
    var supported = asual.util.Browser.supported;
    var iframe, form, textarea;
    var swfaddr, swfid;
	swfid = soID;
    var swftitle = document.title;
    var swflength = history.length;
    var swfhistory = new Array();
    var js = 'swfaddress1_2.js';
    var html = 'swfaddress.html';
    var getHash = function() {
        var index = top.location.href.indexOf('#');
        if (index != -1) {
            return top.location.href.substring(index);
        }
        return '';
    }
    var hash = getHash();
    var listen = function() {
        if (asual.util.Browser.safari) {
            if (swflength != history.length) {
                swflength = history.length;
                if (typeof swfhistory[swflength - 1] != 'undefined') {
                    hash = swfhistory[swflength - 1];
                }
                update();
            }        
        } else if (asual.util.Browser.ie) {
            if (hash != getHash()) {
                asual.SWFAddress.setValue(getHash().replace(/#/g, ''));
            }
            if (document.title != swftitle) {
                asual.SWFAddress.setTitle(swftitle);
            }
        } else {
            if (hash != top.location.hash) {
                hash = top.location.hash;
                update();
            }
        }
    }
    var update = function() {
        var addr = hash.replace(/#/g, '');
        if (addr != swfaddr) {
            swfaddr = addr;
			if(so.call){
				//TODO why not available right away?
				so.call("onAddressChange", addr);
			}
			
        }
    }
    var loadSuccess = function() {
        if (iframe.contentWindow && iframe.contentWindow.location) {
            var win = iframe.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 != getHash()) {
                update();            
                top.location.hash = hash;
            }
        }
    }
    var load = function() {
        if (asual.util.Browser.ie || asual.util.Browser.safari) {
            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 id="swfaddress-iframe" src="' + html + getHash().replace(/#/g, '?') + '" frameborder="no" scrolling="no"></iframe>';
            document.body.appendChild(content);
            content.style.position = 'absolute';
            content.style.left = content.style.top = '-9999px';
            iframe = content.getElementsByTagName('iframe')[0];
        }
        if (asual.util.Browser.ie) {
            asual.util.Events.addListener(iframe, 'load', asual.util.Functions.bindAsListener(loadSuccess, this));  
        }
        if (asual.util.Browser.safari) {
            form = document.createElement('form');
            form.id = 'swfaddress-form';        
            form.method = 'get';
            form.innerHTML += '<textarea id="swfaddress-textarea"></textarea>';
            content.appendChild(form);
            textarea = form.getElementsByTagName('textarea')[0];
            var cookie = asual.util.Cookie.read();
            if (textarea.value != '') {
                swfhistory = textarea.value.split(',');
            } else if (typeof cookie != 'undefined') {
                swfhistory = cookie.split(',');
            }
        }
    }

    
    this.getId = function() {
        if (!supported) return null;
        return swfid;
    }
    this.setId = function(id) {
        if (!supported) return null;    
        swfid = id;
    }
    this.getTitle = function() {
        if (!supported) return null;    
        return swftitle;
    }
    this.setTitle = function(title) {
        if (!supported) return null;
        if (title == 'null') {
            title = '';
        }        
        if (typeof title != 'undefined') {
            swftitle = unescape(title);
            top.document.title = swftitle;
        }
    }
    this.getStatus = function() {
        if (!supported) return null;    
        return swfstatus;
    }
    this.setStatus = function(status) {
        if (!supported) return null;
        if (status == 'null') {
            status = '';
        }        
        if (typeof status != 'undefined') {
            swfstatus = status;
            top.status = top.location.href.substr(0, top.location.href.indexOf('#')) + '#' + swfstatus;
        } else {
            top.status = '';
        }
    }
    this.getValue = function() {
        if (!supported) return null;
        var addr = hash.replace(/#/gi, '');
		//alert("getValue"+addr);
		//so.call("getValue", addr);
        return addr;
    }
    var track = function() {
        if (typeof urchinTracker != 'undefined'){
            //var path = top.location.pathname + this.getValue();
            //path = path.replace(/\/\//, '/');
            //path = path.replace(/^\/$/, '');
            //urchinTracker(path);
        }
    }
    this.setValue = function(addr) {
        if (!supported) return null;
        if (addr == 'null') {
            addr = '';
        }
        //alert("Track: "+addr);
		//pageTracker._trackPageview(addr);
		track_internal(addr);
		//urchinTracker(addr);
        if (swfaddr == addr) {
            return;
        }
        var checkaddr;
        hash = '#' + addr;
        update();
        swfaddr = addr;

        if (asual.util.Browser.safari) {
            form.action = hash;
            swfhistory[history.length] = hash;
            textarea.value = swfhistory;
            asual.util.Cookie.write(swfhistory);
            swflength = history.length + 1;            
            form.submit();
        } else {
            top.location.hash = hash;
        }
        if (asual.util.Browser.ie) {
            var win = iframe.contentWindow;
            var query = '?' + getHash().substring(hash.indexOf('#') + 1);
            win.location.assign(win.location.pathname + query);
        }
        track.call(this);
    }    
    if (!supported) {
        return;
    }
    if (asual.util.Browser.safari) {
        for (var i = 1; i < swflength; i++) {
            swfhistory.push('');
        }
        swfhistory.push(top.location.hash);
    }
    if (asual.util.Browser.ie) {
        if (hash == '') {
            top.location.hash = '#';
        } else {
            top.location.hash = getHash();
        }        
    }
    setInterval(listen, 50);
    asual.util.Events.addListener(window, 'load', asual.util.Functions.bindAsListener(load, this));    
    swfaddr = this.getValue();
    track.call(this);
    update.call(this);
}
if (typeof deconcept != 'undefined' && deconcept.SWFObject) {
    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;
