﻿var serverPath = self.location.protocol + '//' + self.location.host;
var languageId = 0;
var callbackUrl = self.location.href;
var timeZoneOffset;

$(function() {
	timeZoneOffset = readCookie('TimeZoneOffset');
	var currentTimeZoneOffset = -(new Date()).getTimezoneOffset();

	if (timeZoneOffset != null) {
		if (currentTimeZoneOffset != timeZoneOffset) {
			timeZoneOffset = currentTimeZoneOffset;
			createCookie('TimeZoneOffset', timeZoneOffset, 365);
		}
	}
	else {
		timeZoneOffset = currentTimeZoneOffset;
		createCookie('TimeZoneOffset', timeZoneOffset, 365);
	}

	var preload;

	preload = new Image();
	preload.src = 'http://images.mindz.com/css/images/loader/center.gif';
	preload = new Image();
	preload.src = 'http://images.mindz.com/css/images/loader/right.png';
	preload = new Image();
	preload.src = 'http://images.mindz.com/css/images/loader/left.png';

	jQuery.fn.fadeIn = function(speed, callback) {
		return this.animate.apply(this, [{ opacity: 'show' }, speed, function() {
			if (jQuery.browser.msie)
				this.style.removeAttribute('filter');
			if (jQuery.isFunction(callback))
				callback.call(this, null);
		} ]);
	};

	jQuery.fn.fadeOut = function(speed, callback) {
		return this.animate.apply(this, [{ opacity: 'hide' }, speed, function() {
			if (jQuery.browser.msie)
				this.style.removeAttribute('filter');
			if (jQuery.isFunction(callback))
				callback.call(this, null);
		} ]);
	};

	jQuery.fn.fadeTo = function(speed, to, callback) {
		return this.animate.apply(this, [{ opacity: to }, speed, function() {
			if (to == 1 && jQuery.browser.msie)
				this.style.removeAttribute('filter');
			if (jQuery.isFunction(callback))
				callback.call(this, null);
		} ]);
	};	
});

function GenerateUrlFriendlyName(input) {
	return input.replace(/[^a-zA-Z0-9-$_*(),]/g, '_');
};

function GetPopup(popup, webservice, options) {
	if (options == undefined) {
		options = { languageId : 0 };
	}
	else if (options.languageId == undefined) {
		options.languageId = 0;
	}

	if (eval('typeof(Show' + popup + ')') == 'undefined') {
		$.getJSON(serverPath + '/WebServices/' + webservice + '.asmx/Get' + popup + 'Html?callback=?',
			{ languageId: options.languageId, form: 'json' },
			function(result) {
				$(result).appendTo($('body'));
				eval('Show' + popup + '(options)');
			});
	}
	else {
		eval('Show' + popup + '(options)');
	}
};

function ShowPopup(link, path, popup, options) {
	var showLoader = (link && link.tagName == 'A');

	if (showLoader) {
		// Show loader image
		var originalLink = $(link);
		var replacementLink = $('<span style="position: relative;"><img style="position: absolute; left: 0px" src="http://images.mindz.com/css/images/loader/left.png" /><img style="position: absolute; right: 0px" src="http://images.mindz.com/css/images/loader/right.png" /></span>');
		replacementLink
			.css('display', 'inline-block')
			//.css('margin-top', (Number(originalLink.css('margin-top').replace('px', '')) + 2) + 'px')
			//.css('margin-bottom', '-' + (originalLink.height() / 5) + 'px')
			.css('position', 'relative')
			.css('top', '0px')
			.css('background', 'url(http://images.mindz.com/css/images/loader/center.gif) no-repeat top')
			.css('width', originalLink.width() + 'px')
			.css('height', originalLink.height() + 'px');
		replacementLink.addClass(originalLink.attr('class'));
		
		originalLink.hide();
		originalLink.after(replacementLink);
	}
	if (options == undefined) {
		options = { languageId: 0 };
	}
	else if (options.languageId == undefined) {
		options.languageId = 0;
	}

	$.getJSON(serverPath + '/WebServices/UserControlWS.asmx/GetUserControlHtml?callback=?', {
		userControl: path + '/' + popup,
		languageId: options.languageId,
		parameters: JSON.stringify(options),
		form: 'json'
	},
	function(result) {
		var $resultContainer = $('#' + popup + 'ResultContainer');
		if ($resultContainer.length > 0) {
			$resultContainer.remove();
		}
		$('<div id="' + popup + 'ResultContainer">' + result + '</div>').appendTo($('body'));
		eval('Show' + popup + '(options)');
		if (showLoader) {
			// Hide loader image
			replacementLink.remove();
			originalLink.show();
		}
	});
};

function ShowControl(path, control, options, target, slideDown, clear, replace) {
	if (!slideDown) {
		target.fadeTo('fast', 0.4);
	}
	if (options == undefined) {
		options = { languageId: 0 };
	}
	else if (options.languageId == undefined) {
		options.languageId = 0;
	}
	if (options.UniqueId == undefined) {
		options.UniqueId = Math.abs(new Date().getTime());
	}

	$.post(serverPath + '/WebServices/UserControlWS.asmx/GetUserControlHtml?form=json', {
		userControl: path + '/' + control,
		languageId: options.languageId,
		parameters: JSON.stringify(options)
	},
	function(result) {
		var $control = $(result);
		if (slideDown) {
			$control.css('display', 'none');
		}
		if (replace) {
			target.replaceWith($control);
		}
		else if (clear) {
			target.html($control);
		}
		else {
			target.append($control);
		}
		if (slideDown) {
			$control.slideDown('slow');
		}
		else {
			target.fadeTo('fast', 1);
		}

		if (eval('typeof(Show' + control + options.UniqueId + ')') != 'undefined') {
			eval('Show' + control + options.UniqueId + '(options)');
		}
		else {
			window.setTimeout(function() {
				if (eval('typeof(Show' + control + options.UniqueId + ')') != 'undefined') {
					eval('Show' + control + options.UniqueId + '(options)');
				}
				else {
					window.setTimeout(function() {
						if (eval('typeof(Show' + control + options.UniqueId + ')') != 'undefined') {
							eval('Show' + control + options.UniqueId + '(options)');
						}
						else {
							// Commented out, because it's giving unwanted results on the plaza overview page
							//alert('Failed to load: ' + control + options.UniqueId);
						}
					}, 10000);
				}
			}, 2000);
		}
	}, 'json');
};

function GetMappingPopup(options) {
	if (typeof(ShowMapping) == 'undefined') {
		$.getJSON(serverPath + '/WebServices/RegisterWS.asmx/GetMappingHtml?callback=?',
			{ languageId: languageId, form: 'json' },
			function(result) {
				$(result).appendTo($('body'));
				if (options) {
					ShowMapping(options);
				}
				else {
					ShowMapping({});
				}
			});
	}
	else {
		if (options) {
			ShowMapping(options);
		}
		else {
			ShowMapping({});
		}
	}
};

function GetInviteToPlazaPopup(options) {
	if (typeof(ShowInviteToPlaza) == 'undefined') {
		$.getJSON(serverPath + '/WebServices/CircleWS.asmx/GetInviteToPlazaHtml?callback=?',
			{ languageId: languageId, plazaId: options.plazaId, form: 'json' },
			function(result) {
				$(result).appendTo($('body'));
				ShowInviteToPlaza(options);
			});
	}
	else {
		ShowInviteToPlaza(options);
	}
};

function GetLostPasswordPopup(emailAddress) {
	if (typeof(ShowLostPassword) == 'undefined') {
		$.getJSON(serverPath + '/WebServices/RegisterWS.asmx/GetLostPasswordHtml?callback=?',
			{ languageId: languageId, form: 'json' },
			function(result) {
				$(result).appendTo($('body'));
				ShowLostPassword(emailAddress);
			});
	}
	else {
		ShowLostPassword(emailAddress);
	}
};

function GetMapChoicePopup(options) {
	if (typeof(ShowMapChoice) == 'undefined') {
		$.getJSON(serverPath + '/WebServices/RegisterWS.asmx/GetMapChoiceHtml?callback=?',
			{ languageId: languageId, form: 'json' },
			function(result) {
				$(result).appendTo($('body'));
				if (options) {
					ShowMapChoice(options);
				}
				else {
					ShowMapChoice({});
				}
			});
	}
	else {
		if (options) {
			ShowMapChoice(options);
		}
		else {
			ShowMapChoice({});
		}
	}
};

function GetRegisterPopup(options) {
	if (typeof(ShowRegister) == 'undefined') {
		$.getJSON(serverPath + '/WebServices/RegisterWS.asmx/GetRegisterHtml?callback=?',
			{ languageId: languageId, callbackUrl: callbackUrl, form: 'json' },
			function(result) {
				$(result).appendTo($('body'));
				if (options) {
					ShowRegister(options);
				}
				else {
					ShowRegister({});
				}
			});
	}
	else {
		if (options) {
			ShowRegister(options);
		}
		else {
			ShowRegister({});
		}
	}
};

function GetLoginPopup(options) {
	if (typeof(ShowLogin) == 'undefined') {
		$.getJSON(serverPath + '/WebServices/RegisterWS.asmx/GetLoginHtml?callback=?',
			{ languageId: languageId, callbackUrl: callbackUrl, form: 'json' },
			function(result) {
				$(result).appendTo($('body'));
				if (options) {
					ShowLogin(options);
				}
				else {
					ShowLogin({});
				}
			});
	}
	else {
		if (options) {
			ShowLogin(options);
		}
		else {
			ShowLogin({});
		}
	}
};

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

jQuery.IsReservedWord = function(word) {
	if (word.toLowerCase() == 'blog'
		|| word.toLowerCase() == 'blogs'
		|| word.toLowerCase() == 'events'
		|| word.toLowerCase() == 'plazas'
		|| word.toLowerCase() == 'wiki'
		|| word.toLowerCase() == 'network'
		|| word.toLowerCase() == 'about'
		|| word.toLowerCase() == 'aggregator'
		|| word.toLowerCase() == 'qna'
		|| word.toLowerCase() == 'polls'
		|| word.toLowerCase() == 'magazine'
		|| word.toLowerCase() == 'circles'
		|| word.toLowerCase() == 'dashboard'
		|| word.toLowerCase() == 'dialogues'
		|| word.toLowerCase() == 'forums') {
		return true;
	}
	else {
		return false;
	}
};
