/*
 * Copyright (c) 2006-2007 Collactive. All rights reserved.
 */
// Collactive functionality
if (typeof(Collactive) == 'undefined') {
	Collactive = {};
}

// Constants
Collactive.COVER_HPADDING = 5;
Collactive.COVER_VPADDING = 5;
Collactive.SESSION_STORAGE_PREFIX = "session_";
Collactive.WIZARD_SPACING = 5;
Collactive.USERNAME_MIN_SUFFIX = 3;
Collactive.USERNAME_MAX_SUFFIX = 4;
Collactive.USERNAME_MAX_LENGTH = 11;
Collactive.PASSWORD_LENGTH = 8;
Collactive.COUNTRY_LIST = Collactive_getCountryList(); // see end of script
Collactive.MONTH_LIST = Collactive_getMonthList(); // see end of script

Collactive.addBindMethod = function(obj) {
	obj.bind = function(f) {
	     var thisObject = this;
	     return function() {
	       try {
		       var args2 = [];
		       for (var i = 0; i < arguments.length; i++) {
			     args2.push(arguments[i]);
		       }
		       return f.apply(thisObject, args2);
	       } catch (e) {
		       // TODO log this error.
	       }
	     };
	 };
};
	
Collactive.bind = function(self, func) {
	var thisObject = self;
 			return function() {
	   	var args = [];
		for (var i = 0; i < arguments.length; i++) {
 					args.push(arguments[i]);
   				}
   				return func.apply(thisObject, args);
	}
};

// Proxy utilities
Collactive.ProxyUtils = {
    
    // Are we in the proxy now?
    isProxy: function() {
        return document.location.hostname.match(/proxy.*collactive\.com$/);
    },
    
    
    // Returns the name of the proxy
    getProxyHost: function() {
        if (typeof(getPointConfig) != "undefined") {
            // In action page - get proxy host from configuration
            return getPointConfig().proxyHost;
        }
        
        var host = "" + document.location.host;
        // Assumption: current document location is proxied (otherwise we wouldn't be here...)
        var proxy = host.replace(/^[\w\.\-_]+\.(\w+\.collactive\.com(?:\:\d+)?)/, "$1")
        return proxy;
    },


    // Returns the name of the proxy without the port
    getProxyHostOnly: function() {
        var host = "" + document.location.host;
        // Assumption: current document location is proxied (otherwise we wouldn't be here...)
        var proxy = host.replace(/^[\w\.\-_]+\.(\w+\.collactive\.com)(?:\:\d+)?/, "$1")
        return proxy;
    },


    // Navigates to a certain URL, optionally through the proxy
    navigateTo: function(url, proxify) {
        if (proxify) {
            url = Collactive.ProxyUtils.proxify(url)
        } else {
            url = Collactive.ProxyUtils.unproxify(url);
        }
        
        document.location.href = url;
    },


    // Proxifies a URL
    proxify: function(url) {
        if (url == null) {
            return null;
        }

        if (url.match(/^\/\//)) {
            url = "http:" + url;
        }

        if (!url.match(/^https?:\/\//)) {
            // Relative is returned as is - works if we are in a .proxy.collactive.com domain
            return url;
        }

        if (url.match(Collactive.ProxyUtils.getProxyHost())) {
            return url;
        }

        if (url.match(/^https:/)) {
            return "http://" + Collactive.ProxyUtils.getProxyHost() + "/" + url;
        } else {
            return url.replace(/^(https?:\/\/[^\/]+)/, "$1." + Collactive.ProxyUtils.getProxyHost());
        }
    },


    // Unproxifies a URL
    unproxify: function(url) {
        if (typeof(url) == 'object') {
            url = "" + url;
        }

        var result;
        result = url.match(/^(http?:\/\/[\w\.\-_]+)\.\w+\.collactive\.com(?:\:\d+)?(\/.*)?$/);
        if (result != null) {
            return result[1] + result[2];
        }

        result = url.match(/^https?:\/\/\w+\.collactive\.com(?:\:\d+)?\/(https?:\/\/.+)$/);
        if (result != null) {
            return result[1];
        }

        return url;
    },


    // Proxifies all the links in the document
    proxifyAllLinks:function() {
        for (var i in document.links) {
            if (document.links[i].href != null) {
                document.links[i].href = this.proxify(document.links[i].href);
            }
        }
    },

    // Proxifies all the links in the document
    proxifyAllForms:function() {
        for (var i in document.forms) {
            if (document.forms[i].action != null) {
                document.forms[i].action = this.proxify(document.forms[i].action);
            }
        }
    },
	
	callPreviousOnLoad: function(func) {
		// In IE we take over onload, so we must call YouTube's functions
		if (navigator.appName.indexOf("Microsoft") != -1) {
		    try {
		        func.call();
		    } catch (e) { }
		}
	}
};


function Collactive_getMonthList() {
    return ["January", "February", "March", "April", "May", "June",
            "July", "August", "September", "October", "November", "December"];
}


function Collactive_getCountryList() {
    return {
        US: "United States",
        AF: "Afghanistan",
        AL: "Albania",
        DZ: "Algeria",
        AS: "American Samoa",
        AD: "Andorra",
        AO: "Angola",
        AI: "Anguilla",
        AG: "Antigua and Barbuda",
        AR: "Argentina",
        AM: "Armenia",
        AW: "Aruba",
        AU: "Australia",
        AT: "Austria",
        AZ: "Azerbaijan",
        BS: "Bahamas",
        BH: "Bahrain",
        BD: "Bangladesh",
        BB: "Barbados",
        BY: "Belarus",
        BE: "Belgium",
        BZ: "Belize",
        BJ: "Benin",
        BM: "Bermuda",
        BT: "Bhutan",
        BO: "Bolivia",
        BA: "Bosnia and Herzegovina",
        BW: "Botswana",
        BV: "Bouvet Island",
        BR: "Brazil",
        IO: "British Indian Ocean Territory",
        VG: "British Virgin Islands",
        BN: "Brunei",
        BG: "Bulgaria",
        BF: "Burkina Faso",
        BI: "Burundi",
        KH: "Cambodia",
        CM: "Cameroon",
        CA: "Canada",
        CV: "Cape Verde",
        KY: "Cayman Islands",
        CF: "Central African Republic",
        TD: "Chad",
        CL: "Chile",
        CN: "China",
        CX: "Christmas Island",
        CC: "Cocos (Keeling) Islands",
        CO: "Colombia",
        KM: "Comoros",
        CG: "Congo",
        CD: "Congo - Democratic Republic of",
        CK: "Cook Islands",
        CR: "Costa Rica",
        CI: "Cote d'Ivoire",
        HR: "Croatia",
        CU: "Cuba",
        CY: "Cyprus",
        CZ: "Czech Republic",
        DK: "Denmark",
        DJ: "Djibouti",
        DM: "Dominica",
        DO: "Dominican Republic",
        TP: "East Timor",
        EC: "Ecuador",
        EG: "Egypt",
        SV: "El Salvador",
        GQ: "Equitorial Guinea",
        ER: "Eritrea",
        EE: "Estonia",
        ET: "Ethiopia",
        FK: "Falkland Islands (Islas Malvinas)",
        FO: "Faroe Islands",
        FJ: "Fiji",
        FI: "Finland",
        FR: "France",
        GF: "French Guyana",
        PF: "French Polynesia",
        TF: "French Southern and Antarctic Lands",
        GA: "Gabon",
        GM: "Gambia",
        GZ: "Gaza Strip",
        GE: "Georgia",
        DE: "Germany",
        GH: "Ghana",
        GI: "Gibraltar",
        GR: "Greece",
        GL: "Greenland",
        GD: "Grenada",
        GP: "Guadeloupe",
        GU: "Guam",
        GT: "Guatemala",
        GN: "Guinea",
        GW: "Guinea-Bissau",
        GY: "Guyana",
        HT: "Haiti",
        HM: "Heard Island and McDonald Islands",
        VA: "Holy See (Vatican City)",
        HN: "Honduras",
        HK: "Hong Kong",
        HU: "Hungary",
        IS: "Iceland",
        IN: "India",
        ID: "Indonesia",
        IR: "Iran",
        IQ: "Iraq",
        IE: "Ireland",
        IL: "Israel",
        IT: "Italy",
        JM: "Jamaica",
        JP: "Japan",
        JO: "Jordan",
        KZ: "Kazakhstan",
        KE: "Kenya",
        KI: "Kiribati",
        KW: "Kuwait",
        KG: "Kyrgyzstan",
        LA: "Laos",
        LV: "Latvia",
        LB: "Lebanon",
        LS: "Lesotho",
        LR: "Liberia",
        LY: "Libya",
        LI: "Liechtenstein",
        LT: "Lithuania",
        LU: "Luxembourg",
        MO: "Macau",
        MK: "Macedonia - The Former Yugoslav Republic of",
        MG: "Madagascar",
        MW: "Malawi",
        MY: "Malaysia",
        MV: "Maldives",
        ML: "Mali",
        MT: "Malta",
        MH: "Marshall Islands",
        MQ: "Martinique",
        MR: "Mauritania",
        MU: "Mauritius",
        YT: "Mayotte",
        MX: "Mexico",
        FM: "Micronesia - Federated States of",
        MD: "Moldova",
        MC: "Monaco",
        MN: "Mongolia",
        MS: "Montserrat",
        MA: "Morocco",
        MZ: "Mozambique",
        MM: "Myanmar",
        NA: "Namibia",
        NR: "Naura",
        NP: "Nepal",
        NL: "Netherlands",
        AN: "Netherlands Antilles",
        NC: "New Caledonia",
        NZ: "New Zealand",
        NI: "Nicaragua",
        NE: "Niger",
        NG: "Nigeria",
        NU: "Niue",
        NF: "Norfolk Island",
        KP: "North Korea",
        MP: "Northern Mariana Islands",
        NO: "Norway",
        OM: "Oman",
        PK: "Pakistan",
        PW: "Palau",
        PA: "Panama",
        PG: "Papua New Guinea",
        PY: "Paraguay",
        PE: "Peru",
        PH: "Philippines",
        PN: "Pitcairn Islands",
        PL: "Poland",
        PT: "Portugal",
        PR: "Puerto Rico",
        QA: "Qatar",
        RE: "Reunion",
        RO: "Romania",
        RU: "Russia",
        RW: "Rwanda",
        KN: "Saint Kitts and Nevis",
        LC: "Saint Lucia",
        VC: "Saint Vincent and the Grenadines",
        WS: "Samoa",
        SM: "San Marino",
        ST: "Sao Tome and Principe",
        SA: "Saudi Arabia",
        SN: "Senegal",
        CS: "Serbia and Montenegro",
        SC: "Seychelles",
        SL: "Sierra Leone",
        SG: "Singapore",
        SK: "Slovakia",
        SI: "Slovenia",
        SB: "Solomon Islands",
        SO: "Somalia",
        ZA: "South Africa",
        GS: "South Georgia and the South Sandwich Islands",
        KR: "South Korea",
        ES: "Spain",
        LK: "Sri Lanka",
        SH: "St. Helena",
        PM: "St. Pierre and Miquelon",
        SD: "Sudan",
        SR: "Suriname",
        SJ: "Svalbard",
        SZ: "Swaziland",
        SE: "Sweden",
        CH: "Switzerland",
        SY: "Syria",
        TW: "Taiwan",
        TJ: "Tajikistan",
        TZ: "Tanzania",
        TH: "Thailand",
        TG: "Togo",
        TK: "Tokelau",
        TO: "Tonga",
        TT: "Trinidad and Tobago",
        TN: "Tunisia",
        TR: "Turkey",
        TM: "Turkmenistan",
        TC: "Turks and Caicos Islands",
        TV: "Tuvalu",
        UG: "Uganda",
        UA: "Ukraine",
        AE: "United Arab Emirates",
        GB: "United Kingdom",
        US: "United States",
        VI: "United States Virgin Islands",
        UY: "Uruguay",
        UZ: "Uzbekistan",
        VU: "Vanuatu",
        VE: "Venezuela",
        VN: "Vietnam",
        WF: "Wallis and Futuna",
        PS: "West Bank",
        EH: "Western Sahara",
        YE: "Yemen",
        ZM: "Zambia",
        ZW: "Zimbabwe"
    };
}
