Type.registerNamespace("MySpace");
Type.registerNamespace("MySpace.UI");

Date.prototype.addMilliseconds = function(value){
	/// <param name="value" type="Number" integer="true"></param>
	this.setTime(this.getTime() + value);return this;
}
Date.prototype.addSeconds = function(value){
	/// <param name="value" type="Number" integer="true"></param>
	this.addMilliseconds(value*1000);return this;
}
Date.prototype.addMinutes = function(value){
	/// <param name="value" type="Number" integer="true"></param>
	this.addMilliseconds(value*60000);return this;
}
Date.prototype.addHours = function(value){
	/// <param name="value" type="Number" integer="true"></param>
	this.addMilliseconds(value*3600000);return this;
}
Date.prototype.addDays = function(value){
	/// <param name="value" type="Number" integer="true"></param>
	this.setDate(this.getDate()+value);return this;
}
Date.prototype.addMonths = function(value){
	/// <param name="value" type="Number" integer="true"></param>
	this.setMonth(this.getMonth()+value);return this;
}
Date.prototype.addYears = function(value){
	/// <param name="value" type="Number" integer="true"></param>
	this.setFullYear(this.getFullYear()+value);return this;
}

//
MySpace.UI.hideElements = function(tagNames, TorF){
	/// <param name="value" type="Array" elementType="String"></param>
	/// <param name="TorF" type="Boolean"></param>
	for(var j=0;j<tagNames.length;j++){
		var t = document.getElementsByTagName(tagNames[j]);
		for(var i=0;i<t.length;i++){t[i].style.visibility = TorF? "hidden" : "";}
	}
}
//Generic window overlay
MySpace.UI._Overlay = function(element){
	/// <param name="element" domElement="true"></param>
	document.body.appendChild(element);
	MySpace.UI._Overlay.initializeBase(this,[element]);
}
MySpace.UI._Overlay.prototype = {
	_interval: null,_fadeIn: false,_opacity: 0,_max: 60,_fadeDelegate:null,_resizeHandler:null, _step:20,
	show: function(){MySpace.UI.hideElements(["iframe","object","embed"],true);this._fade(true);},
	hide: function(){MySpace.UI.hideElements(["iframe","object","embed"],false);this._fade(false);},

	add_fadeComplete:function(handler){
		/// <param name="fadeIn" type="Boolean"></param>
		this.get_events().addHandler("fadeComplete", handler);
	},
	remove_fadeComplete:function(handler){
		/// <param name="fadeIn" type="Boolean"></param>
		this.get_events().removeHandler("fadeComplete", handler);
	},

	initialize:function(){
		var element = this.get_element();
		element.id = "window_overlay";
		element.style.zIndex = "100000";
		element.style.width = "100%";
		this.set_opacity(0);
		Sys.UI.DomElement.setLocation(element,0,0);
		this._setHeight();
		this._resizeHandler = Function.createDelegate(this, this._setHeight);
		$addHandler(window, "resize", this._resizeHandler);
	},
	_setHeight: function(){
		var a=document.body.scrollHeight;//scrolled content
		var b=document.documentElement.clientHeight;//no scroll
		var c=document.documentElement.scrollHeight;//ie7 friendly
		a = ((a>c)?a:c);
		this.get_element().style.height=((a>b)?a:b)+"px";
	},
	_fade: function(fadeIn){
		/// <param name="fadeIn" type="Boolean"></param>
		this._fadeIn = fadeIn;
		if(fadeIn) this.set_visible(true);
		if(!this._fadeDelegate)this._fadeDelegate = Function.createDelegate(this,this._tick);
		this._interval = window.setInterval(this._fadeDelegate, 100);
	},
	_tick: function(){
		if(!this._interval) return;
		var increase = this._step;
		if(!this._fadeIn) increase*=-1;
		var newOpacity = this._opacity + increase;
		if(newOpacity<0) newOpacity = 0;
		else if(newOpacity>this._max) newOpacity = this._max;
		this.set_opacity(newOpacity);
		if(newOpacity<=0 || newOpacity>=this._max){
			window.clearInterval(this._interval);
            var fadeComplete = this.get_events().getHandler("fadeComplete");
            if (fadeComplete) fadeComplete(this, Sys.EventArgs.Empty);
		}
	},
	get_opacity:function(){return this._opacity;},
	set_opacity: function(value){
		/// <param name="value" type="Number" integer="true"></param>
		this._opacity = value;
		var s = this.get_element().style;
		s.opacity = value*.01;
		s.filter = "alpha(opacity="+value+")";
		if(value===0) this.set_visible(false);
	},
	dispose:function(){
		$removeHandler(window, "resize", this._resizeHandler);
		this._fadeDelegate=null;
		MySpace.UI._Overlay.callBaseMethod(this, 'dispose');
	}
}
MySpace.UI._Overlay.registerClass('MySpace.UI._Overlay', Sys.UI.Control);
window.get_overlay = function(){if(!window._overlay)window._overlay=$create(MySpace.UI._Overlay,null,null,null,document.createElement("div"));return window._overlay;};



MySpace.UI._Popup = function(element){
	/// <param name="element" domElement="true"></param>
	this._box = element.firstChild;
	MySpace.UI._Popup.initializeBase(this,[element]);
}
MySpace.UI._Popup.prototype = {
	_box:null,
	_state:null,
	_defaultButton:null,
	_callback:null,

	get_state: function(){return this._state;},
	set_state: function(value){this._state = value;},

	get_title: function(){return this._box.childNodes[1].innerHTML;},
	set_title: function(value){this._box.childNodes[1].innerHTML = value;},

	get_content: function(){return this._box.childNodes[2].innerHTML;},
	set_content: function(value){this._box.childNodes[2].innerHTML = value;},

	get_callback: function(){return this._callback;},
	set_callback: function(value){this._callback = value;},

	add_button: function(text, isDefault){
		var b=document.createElement("input");
		b.type="button";
		b.value=text;
		if(isDefault)
			this._defaultButton = b;
		$addHandlers(b,{click:this._buttonClick},this);
		this._box.lastChild.appendChild(b);
		return b;
	},

	show: function(callBack){
		if(MySpace.UI._Popup._activePopup)throw "A Popup is already active.";
		if(callBack)this._callBack = callBack;
		window.get_overlay().show();
		this.set_visible(true);
		if(this._defaultButton)this._defaultButton.focus();
		MySpace.UI._Popup._activePopup = this;
	},
	_hide: function(){
		window.get_overlay().hide();
		this.set_visible(false);
		MySpace.UI._Popup._activePopup=null;
	},
	_buttonClick: function(e){
		this._hide();
		var cb=this._callBack;
		if(cb)cb(this, e);
	},
	initialize: function(){
		var element = this.get_element();
		//element should always have a parent becuase it should have been created by a template
		element.parentNode.removeChild(element);
		document.body.appendChild(element);
		this._box.firstChild.isCancel=true; //red-x
		$addHandlers(this._box.firstChild,{click:this._buttonClick},this);
		MySpace.UI._Popup.callBaseMethod(this, 'initialize');
	},
	dispose: function(){
		var buttons = this._box.lastChild.childNodes;
		for(var i=0;i<buttons.length;i++)
			$clearHandlers(buttons[i]);
		MySpace.UI._Popup.callBaseMethod(this, 'dispose');
	}
}
MySpace.UI._Popup.registerClass('MySpace.UI._Popup', Sys.UI.Control);
MySpace.UI._Popup._activePopup=null;



MySpace.UI.Popup=function(){throw "Cannot instantiate static class.";}
MySpace.UI.Popup.create=function(content, title, callback){
	/// <param name="content" type="String"></param>
	/// <param name="title" type="String"></param>
	/// <param name="callback" type="Function" mayBeNull="true" optional="true"></param>
	/// <returns type="MySpace.UI.Popup"></returns>
	var temp=document.createElement("div");
	temp.innerHTML="<div class='popup_wrapper' style='z-index:1000001;left:0px;width:100%;display:none;visibility:hidden;'><div class='popup_box'><a class='popup_x'></a><div class='popup_title'></div><div class='popup_content'></div><div class='popup_buttons'></div></div></div>";
	return $create(MySpace.UI._Popup,{title:title, content:content, callback:callback},null,null,temp.firstChild);
}

MySpace.UI.Popup.generic = function(message, buttonText, callback){
	/// <param name="message" type="String"></param>
	/// <param name="buttonText" type="String"></param>
	/// <param name="callback" type="Function" mayBeNull="true" optional="true"></param>
	/// <returns type="MySpace.UI.Popup"></returns>
	var template = document.createElement("div");
	var p = MySpace.UI.Popup.create(message,MySpaceRes.Common.Attention);
	p.add_button(buttonText);
	template.innerHTML=message;
	p.show(callback);
	return p;
}
MySpace.UI.Popup.alert = function(message, callback){
	/// <param name="message" type="String"></param>
	/// <param name="callback" type="Function" mayBeNull="true" optional="true"></param>
	new MySpace.UI.Popup.generic(message,callback,MySpaceRes.Common.Ok);
}
MySpace.UI.Popup.confirm = function(message, callback){
	/// <param name="message" type="String"></param>
	/// <param name="callback" type="Function" mayBeNull="true" optional="true"></param>
	var p=new MySpace.UI.Popup.generic(message,callback,MySpaceRes.Common.Yes);
	p.add_button(MySpaceRes.Common.No);
}
MySpace.UI.Popup.registerClass('MySpace.UI.Popup');



MySpace.WebRequest=function(){throw "Cannot instantiate static class.";}
MySpace.WebRequest.invoke = function(path, useGet, params, onSuccess, onFailure, userContext, timeout) {
	/// <param name="path" type="String"></param>
	/// <param name="useGet" type="Boolean"></param>
	/// <param name="params"></param>
	/// <param name="onSuccess" type="Function" mayBeNull="true" optional="true"></param>
	/// <param name="onFailure" type="Function" mayBeNull="true" optional="true"></param>
	/// <param name="userContext" mayBeNull="true" optional="true"></param>
	/// <returns type="Sys.Net.WebRequest"></returns>
	if (!params) params = {};
	var request = new Sys.Net.WebRequest();
	if (!useGet) {
		if(typeof params==="string")
		var body = (typeof params!=="string")? Sys.Serialization.JavaScriptSerializer.serialize(params) : params;
		if (body === "{}") body = "";
		request.set_body(body);
	}
	request.set_url(Sys.Net.WebRequest._createUrl(path, (useGet)?params:{}));

	request.add_completed(onComplete);
	if (timeout && timeout > 0) request.set_timeout(timeout);
	request.invoke();

	function onComplete(response, eventArgs) {
		if (response.get_responseAvailable()) {
			var statusCode = response.get_statusCode();

			var result = null;
			try {
				var contentType = response.getResponseHeader("Content-Type");
				if (contentType.startsWith("application/json"))
					result = response.get_object();
				else if (contentType.startsWith("text/xml"))
					result = response.get_xml();
				else result = response.get_responseData();
			} catch (ex) {}

			//handle errors
			if ((statusCode < 200) || (statusCode >= 300)) {
				if (onFailure) {
					if (!result) {
						result = new Sys.Net.WebServiceError(false , "WebRequest failed for an unknown reason.", "", "");
					}
					result._statusCode = statusCode;
					onFailure(result, userContext);
				}
				else {//dev doesn't want to handle the error, just alert it
					var error;
					if (result)
						error = result.get_exceptionType() + "-- " + result.get_message();
					else error = response.get_responseData();
					window.alert("WebRequest Failed: "+error);
				}
			}
			else if (onSuccess) {
				onSuccess(result, userContext);
			}
		}
		else {
			var msg;
			if (response.get_timedOut()) msg = "WebRequest timed out.";
			else msg = "WebRequest failed for an unknown reason.";

			if (onFailure) onFailure(new Sys.Net.WebServiceError(response.get_timedOut(), msg, "", ""), userContext);
			else alert(msg);
		}
	}
	return request;
}
MySpace.WebRequest.registerClass('MySpace.WebRequest');



MySpace.CMS=function(){throw "Cannot instantiate static class.";}
MySpace.CMS.cache={};
MySpace.CMS.getContent=function(placementId, callback, context){
	/// <param name="placementId" type="Number"></param>
	/// <param name="callback" type="Function" mayBeNull="true" optional="true"></param>
	/// <param name="context" type="Object" mayBeNull="true" optional="true"></param>
	var cacheItem=MySpace.CMS.cache[placementId];
	if(cacheItem && new Date() < cacheItem.expire) {
		callback(cacheItem.response);
		return null;
	}

	MySpace.WebRequest.invoke("/Modules/Common/HttpHandlers/CMS.ashx", false, "placementId="+placementId, _onComplete, null, context, 0);
	
	function _onComplete(response, eventArgs) {
		var expire = new Date();
		expire.setTime(expire.getTime()+120000);//2 minute cache
		MySpace.CMS.cache[placementId] = {response:response,expire:expire};
		if(callback) callback(response, eventArgs);
	}
	return null;
}
MySpace.CMS.track=function(id){
	/// <param name="id" type="String"></param>
	MySpace.WebRequest.invoke("/Modules/Common/HttpHandlers/CMSClick.ashx?_i="+id, true, null, null, Function.emptyFunction, null, 0);
}
MySpace.CMS.registerClass('MySpace.CMS');



MySpace.Util=function(){throw "Cannot instantiate static class.";}
MySpace.Util.parseNameValuePair=function(nameValuePair, delimiter, modifier){
	/// <param name="nameValuePair" type="String"></param>
	/// <param name="delimiter" type="Regex"></param>
	/// <param name="modifier" type="Function" mayBeNull="true" optional="true"></param>
	/// <returns type="Object"></returns>
	if(!delimiter) delimiter="=";
	if(!nameValuePair || nameValuePair[0]===delimiter) return null;
	var nameValueObject;
	var pos=nameValuePair.search(delimiter);
	if(pos>0){
		nameValueObject={
			name:nameValuePair.substring(0,pos),
			value:nameValuePair.substring(pos+1)
		}
	}
	else nameValueObject={name:nameValuePair,value:""};

	if(modifier) nameValueObject=modifier(nameValueObject);
	return nameValueObject;
}
MySpace.Util.parseNameValuePairs=function(nameValuePairs, pairDelimiter, nameValueDelimiter, nameValueModifier){
	/// <param name="nameValuePairs" type="String"></param>
	/// <param name="pairDelimiter" type="Regex"></param>
	/// <param name="nameValueDelimiter" type="Regex"></param>
	/// <param name="modifier" type="Function" mayBeNull="true" optional="true"></param>
	/// <returns type="Object"></returns>
	var _collection=null,nameValuePair=null;
	var _pairs = nameValuePairs.split(pairDelimiter);
	for(var i=0;i<_pairs.length;i++){
		nameValuePair=MySpace.Util.parseNameValuePair(_pairs[i],nameValueDelimiter,nameValueModifier);
		if(!nameValuePair) continue;
		if(!_collection) _collection={};
		_collection[nameValuePair.name]=nameValuePair.value;
	}
	return _collection;
}
MySpace.Util.registerClass('MySpace.Util');




MySpace.Cookie=function(name, rawValue){
	/// <param name="name" type="String"></param>
	/// <param name="rawValue" type="String"></param>
	this._name = name;
	this._rawValue = rawValue;
	this._value = unescape(rawValue);
	this._values = MySpace.Util.parseNameValuePairs(rawValue,"&",null,this._modifier);
}
MySpace.Cookie.prototype={
	get_name:function(){return this._name;},
	get_value:function(){return this._value;},
	get_values:function(){return this._values;},

	_modifier:function(nameValueObject){
		if(!nameValueObject.value) return null;
		nameValueObject.value=unescape(nameValueObject.value);
		return nameValueObject;
	},
	toString:function(){
		/// <returns type="String"></returns>
		var valuesArray=[];
		var values=this.get_values();
		if(!values) return this.get_value();
		for(var i in values)
			valuesArray[valuesArray.length] = i+"="+escape(values[i]);
		return valuesArray.join("&");
	}
}
MySpace.Cookie.registerClass('MySpace.Cookie');



MySpace._Cookies=function(){
	var nvc = MySpace.Util.parseNameValuePairs(document.cookie,/\s?;\s?/,null);
	for(var name in nvc)
		this[name] = new MySpace.Cookie(name, nvc[name]);
}
MySpace._Cookies.prototype={
	save:function(cookie,domain,expires){
		/// <param name="cookie" type="MySpace.Cookie"></param>
		/// <param name="domain" type="String"></param>
		/// <param name="expires" type="Date"></param>
		var value=cookie.toString();
		var parts=[cookie.get_name()+"="+value];
		if(domain){
			if(domain!=="localhost" && domain.charAt(0)!==".")
				throw Error.invalidOperation("domain must start with '.'");
			parts[1]="domain="+domain;
		}
		if(expires)parts[parts.length]="expires="+expires.toGMTString();
		parts[parts.length]="path=/";
		document.cookie = parts.join("; "); 
		MySpace.Cookies=new MySpace._Cookies();
	},
	remove:function(cookieName, domain){
		/// <param name="cookieName" type="String"></param>
		this.save(new MySpace.Cookie(cookieName,""),domain,new Date().addDays(-1));
	}
}
MySpace._Cookies.registerClass('MySpace._Cookies');
MySpace.Cookies=new MySpace._Cookies();

//Sitewide implimentations of global controls
if(typeof MySpace.Cookies.MSCulture!=='undefined'){
	var d = new Date();
	d.setMonth(0);	
	
	if(typeof updateClientTimeZone !== 'undefined')
	{
	    if(updateClientTimeZone == 1)
            MySpace.Cookies.MSCulture.get_values().clientTimeZone=(d.getTimezoneOffset()/-60)+8; 
	    else
	        MySpace.Cookies.MSCulture.get_values().timeZone=(d.getTimezoneOffset()/-60)+8;
    }
    else
        MySpace.Cookies.MSCulture.get_values().clientTimeZone=(d.getTimezoneOffset()/-60)+8; 
	
	MySpace.Cookies.save(MySpace.Cookies.MSCulture,".myspace.com",new Date().addDays(7));
}

MySpace.UI.Header=function(){throw "Cannot instantiate static class.";}
MySpace.UI.Header.languageLinkClick=function(culture, domain, index){
	/// <param name="culture" type="String"></param>
	/// <param name="domain" type="String"></param>
	/// <param name="index" type="Number"></param>
	MySpace.CMS.getContent("language_toggle"+index,_contentRetrieved);
	function _contentRetrieved(content){
	    var fcontent = content;
        var currentculture = MySpace.Cookies.MSCulture.get_values().PreferredCulture;    
        fcontent = fcontent.replace("COLOR: ; display: none' id='" + currentculture + "->" + culture + "'", "COLOR: blue; display:' id='" + currentculture + "->" + culture + "'");
        fcontent = fcontent.replace("COLOR: ; display: none' id='" + culture + "->" + culture + "'", "COLOR: red; display:' id='" + culture + "->" + culture + "'");        
        fcontent = fcontent.replace("COLOR: ; display: none' id='" + currentculture + "'", "COLOR: blue; display:' id='" + currentculture + "->" + culture + "'");
        fcontent = fcontent.replace("COLOR: ; display: none' id='" + culture + "'", "COLOR: red; display:' id='" + culture + "->" + culture + "'");           
	    
		var p = MySpace.UI.Popup.create(fcontent,MySpaceRes.Common.Attention);
		p.add_button(MySpaceRes.Header.Continue);
		p.add_button(MySpaceRes.Header.Cancel, true).isCancel=true;
		p.set_state({culture:culture, domain:domain});
		p.addCssClass("popupChangeLanguage");
		p.show(_popupCallback);
	}
	function _popupCallback(sender, args){
	    var state=sender.get_state();
		if(args.target.isCancel) //clicked cancel
		{
            Sys.Net.WebServiceProxy.invoke("/Services/GeoLocation.asmx", "DenyPreferredCultureChange", false, {culture:state.culture}, null, null, null, 0);				    
            return;
		}		
		//if they are logged in,there **SHOULD** be a checkbox
		var c=sender._box.childNodes[2].getElementsByTagName("input");
		if(c.length>0 && c[0].checked){//Persist user's language
			Sys.Net.WebServiceProxy.invoke("/Services/GeoLocation.asmx", "SavePreferredCulture", false, {culture:state.culture}, _goToDomain, null, null, 0);
		}
		else
		{
			Sys.Net.WebServiceProxy.invoke("/Services/GeoLocation.asmx", "SetPreferredCulture", false, {culture:state.culture}, _goToDomain, null, null, 0);	
		}				
	}
	function _goToDomain(){//needed for callback
		var t = window.location;
		if(domain.length > 0)
		{
		    window.location = "http://" + domain;
		}
		else
		    window.location.reload(true);
	}
}
MySpace.UI.Header.registerClass('MySpace.UI.Header');




MySpace.IM = function(){throw "Cannot instantiate static class.";}
MySpace.IM.get_info = function(){
	switch(Sys.Browser.agent) {
	case Sys.Browser.InternetExplorer:
		var objMySpaceIMX= new ActiveXObject("MySpaceIMX.MySpaceIMPlugin.1");
		if(typeof objMySpaceIMX ==="undefined") return null;
		var IMVer = (objMySpaceIMX.GetMSIMVersion)? objMySpaceIMX.GetMSIMVersion() : 0;
		return {ver:IMVer,hasSkype:(IMVer>0)};
		break;
	case Sys.Browser.Firefox:
	//case Sys.Browser.Safari:
	//case Sys.Browser.Opera:
		if(!navigator.mimeTypes || navigator.mimeTypes.length < 0) return null;
		var plugin = navigator.mimeTypes["application/myspaceim.1"];
		if(plugin) return {ver:"1.0.719",hasSkype:true};
		else{
			plugin = navigator.mimeTypes["application/myspaceim"]
			if(plugin) return {ver:"0",hasSkype:false};
		}
	default:
		return null;
	}
}




if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();