var Teaser = {
	initialize: function(section, subsection, teaser_container, teaser_base_url )
	{
		Teaser.section = section;
		Teaser.subsection = subsection;
		Teaser.teaserContainer = teaser_container;
		Teaser.teaserUrl = teaser_base_url + section + '/' + subsection + '.html';
		Teaser.teaserPropertyUrl = teaser_base_url + 'propertyload';
		Teaser.teaserTranslationSaveUrl = teaser_base_url + 'translationsave';
		Teaser.teaserSwitchSaveUrl = teaser_base_url + 'switchsave';
		Teaser.cookieSid = 'sid';
		Teaser.cookieRmSid = 'rm_sid';
		Teaser.isTranslation = 0;
		Teaser.switchTournaments = new Array();
		Teaser.translationTournaments = new Array();
		
		Teaser.getUserId();
		
		if (Teaser.getCookie('uteaser_' + Teaser.userId))
		{
			Teaser.getCookieProperty(Teaser.getCookie('uteaser_' + Teaser.userId));
			Teaser.update();
		}
		else
		{
			if (Teaser.userId)
			{
				var loadRequest = new Ajax.Request(Teaser.teaserPropertyUrl,
					{
						method: 'post',
						requestHeaders: {Accept: 'application/json'},
						onSuccess: Teaser.fillProperty
					}
				);
			}
			else
			{
				Teaser.setDefaultProperty();
				Teaser.setCookieProperty();
				Teaser.update();
			}
		}
	},
	
	setDefaultProperty: function()
	{
		Teaser.isTranslation = 0;
		Teaser.switchTournaments = new Array();
		Teaser.translationTournaments = new Array();
	},
	
	getUserId: function()
	{
		Teaser.userId = 0;
		Teaser.isUser = 0;
		
		if (Teaser.getCookie(Teaser.cookieRmSid))
		{
			Teaser.isUser = 1;
			Teaser.userId = Teaser.getCookie(Teaser.cookieRmSid);
		}
		else if (Teaser.getCookie(Teaser.cookieSid))
		{
			Teaser.isUser = 1;
			Teaser.userId = Teaser.getCookie(Teaser.cookieSid);
		}
	},
	
	getSectionId: function()
	{
		if (Teaser.subsection)
		{
			return Teaser.subsection;
		}
		else
		{
			return Teaser.section;
		}
	},
	
	fillProperty: function(response )
	{
		var data = response.responseText.evalJSON();
		Teaser.isUser = data.is_user;
		if (Teaser.isUser)
		{
			Teaser.isTranslation = 0;
			var current_section = Teaser.getSectionId();
			
			if (data.translation)
			{
				Teaser.translationTournaments = data.translation.split(",");
				for (var i = Teaser.translationTournaments.length-1; i >= 0; i--)
				{
					if (Teaser.translationTournaments[i].split("=")[0] == current_section)
					{
						Teaser.isTranslation = Teaser.translationTournaments[i].split("=")[1];
					}
				}
			}
			else
			{
				Teaser.translationTournaments = new Array();
			}
			
			if (data.tournaments)
			{
				Teaser.switchTournaments = data.tournaments;
			}
			else
			{
				Teaser.switchTournaments = new Array();
			}
		}
		else
		{
			Teaser.setDefaultProperty();
		}
		
		Teaser.setCookieProperty();
		
		Teaser.update();
	},
	
	getCookieProperty: function(cookie_data)
	{
		if (cookie_data)
		{
			var translation_array = cookie_data.split(":")[0];
			var switch_array = cookie_data.split(":")[1];
			var current_section = Teaser.getSectionId();
			
			if (translation_array)
			{
				if (translation_array.length == 1)
				{
					Teaser.isTranslation = translation_array;
					
					Teaser.translationTournaments = new Array();
					if (Teaser.isTranslation)
					{
						Teaser.translationTournaments[Teaser.translationTournaments.length] = current_section + "=" + Teaser.isTranslation;
					}
				}
				else
				{
					Teaser.translationTournaments = translation_array.split(",");
					
					Teaser.isTranslation = 0;
					var translation_flag = 1;
					for (var i = Teaser.translationTournaments.length-1; i >= 0; i--)
					{
						if ((Teaser.translationTournaments[i].split("=")[0] == current_section) && translation_flag)
						{
							Teaser.isTranslation = Teaser.translationTournaments[i].split("=")[1];
							translation_flag = 0;
						}
					}
				}
			}
			else
			{
				Teaser.translationTournaments = new Array();
				Teaser.isTranslation = 0;
			}
			
			if (switch_array)
			{
				Teaser.switchTournaments = switch_array.split(",");
			}
			else
			{
				Teaser.switchTournaments = new Array();
			}
		}
	},
	
	setCookieProperty: function()
	{
		var cookie_data = Teaser.translationTournaments.toString() + ':' + Teaser.switchTournaments.toString();
		
		var expire = new Date();
		if (Teaser.userId == Teaser.getCookie(Teaser.cookieSid))
		{
			var expire = '';
		}
		else
		{
			var expire = new Date(Date.parse(Date())+63072000000);
		}
		
		Teaser.setCookie('uteaser_' + Teaser.userId, cookie_data, expire, '/');
	},
	
	setTranslation: function()
	{
		var post_section = Teaser.getSectionId();
		var saveRequest = new Ajax.Request(Teaser.teaserTranslationSaveUrl,
			{
				parameters: {
							is_translation: Teaser.isTranslation,
							section_id: post_section
				},
				method: 'post',
				onComplete: function(response) {
					var data = response.responseText.evalJSON();
					
					Teaser.checkTranslationSave(Teaser.isTranslation)
				}
			}
		);
	},
	
	checkTranslationSave: function(is_translation)
	{
		Teaser.getCookieProperty(Teaser.getCookie('uteaser_' + Teaser.userId));
		
		var current_section = Teaser.getSectionId();
			
		if (is_translation)
		{
			Teaser.translationTournaments[Teaser.translationTournaments.length] = current_section + "=" + is_translation;
		}
		else
		{				
			for (var i = Teaser.translationTournaments.length-1; i >= 0; i--)
			{
				if (Teaser.translationTournaments[i].split("=")[0] == current_section)
				{
					Teaser.translationTournaments.splice(i, 1);
				}
			}
		}
		Teaser.isTranslation = is_translation;
		
		Teaser.setCookieProperty();
		Teaser.update();
	},
	
	setSwitch: function(tournament_id, is_show)
	{
		var saveRequest = new Ajax.Request(Teaser.teaserSwitchSaveUrl,
			{
				parameters: {
							tournament_id: tournament_id,
							is_show: is_show
				},
				method: 'post',
				onComplete: function(response) {
					var data = response.responseText.evalJSON();
					
					Teaser.checkSwitchSave(tournament_id, is_show)
				}
			}
		);
	},
	
	checkSwitchSave: function(tournament_id, is_show)
	{
		Teaser.getCookieProperty(Teaser.getCookie('uteaser_' + Teaser.userId));
		
		if (is_show)
		{
			Teaser.switchTournaments[Teaser.switchTournaments.length] = tournament_id;
		}
		else
		{
			for (var i = Teaser.switchTournaments.length-1; i >= 0; i--)
			{
				if (Teaser.switchTournaments[i] == tournament_id)
				{
					Teaser.switchTournaments.splice(i, 1);
				}
			}
		}
		
		Teaser.setCookieProperty();
	},
	
	roll: function()
	{
		if ($('teaser_loader'))
		{
			$('teaser_loader').show();
		}
		
		Teaser.isTranslation = 1;
		Teaser.setTranslation();
		
		return false;
	},
	
	unroll: function()
	{
		if ($('teaser_loader'))
		{
			$('teaser_loader').show();
		}
		
		Teaser.isTranslation = 0;
		Teaser.setTranslation();
		
		return false;
	},
	
	switchOff: function(id, is_save)
	{
		if ($(id + '_teaser'))
		{
			$(id + '_teaser').hide();
		}
		if ($(id + '_teaser_right'))
		{
			$(id + '_teaser_right').hide();
		}
		if ($(id + '_spanoff'))
		{
			$(id + '_spanoff').hide();
		}
		if ($(id + '_spanon'))
		{
			$(id + '_spanon').show();
		}
		
		if (is_save)
		{
			Teaser.setSwitch(id, 1);
			
			return false;
		}
	},
	
	switchOn: function(id, is_save)
	{
		if ($(id + '_teaser'))
		{
			$(id + '_teaser').show();
		}
		if ($(id + '_teaser_right'))
		{
			$(id + '_teaser_right').show();
		}
		if ($(id + '_spanoff'))
		{
			$(id + '_spanoff').show();
		}
		if ($(id + '_spanon'))
		{
			$(id + '_spanon').hide();
		}
		
		if (is_save)
		{
			Teaser.setSwitch(id, 0);
		}
		
		return false;
	},
	
	update: function()
	{
		var myAjax = new Ajax.Updater(
			{success: Teaser.teaserContainer}, 
			Teaser.teaserUrl, 
			{ 
				parameters: {
							is_translation: Teaser.isTranslation
				},
				method: 'get',
				onComplete: Teaser.switchAll
			}
		);
	},
	
	switchAll: function()
	{
		for (var i = 0; i < Teaser.switchTournaments.length; i++)
		{
			Teaser.switchOff(Teaser.switchTournaments[i], 0);
		}
	},
	
	setCookie: function(name, value, expires, path, domain, secure) 
	{
		document.cookie = name + "=" + escape(value) +
			((expires) ? "; expires=" + expires : "") +
			((path) ? "; path=" + path : "") +
			((domain) ? "; domain=" + domain : "") +
			((secure) ? "; secure" : "");
	},

	getCookie: function(name) 
	{
		var cookie = " " + document.cookie;
		var search = " " + name + "=";
		var setStr = null;
		var offset = 0;
		var end = 0;
		if (cookie.length > 0) 
		{
			offset = cookie.indexOf(search);
			if (offset != -1) 
			{
				offset += search.length;
				end = cookie.indexOf(";", offset)
				if (end == -1) 
				{
					end = cookie.length;
				}
				setStr = unescape(cookie.substring(offset, end));
			}
		}
		
		return(setStr);
	}
}

var section;
var subsection;
if (section == undefined)
{
	section = 0;
}
if (subsection == undefined)
{
	subsection = 0;
}

Teaser.initialize(section, subsection, 'stat_teaser', '/stat/teaser/');
