// {appearance} may have {name:'', class:'', raw_css:'')
function dge(a) {return document.getElementById(a);}
var std_appearances={
	plain_white: {raw_css:'background-color:white;'},
	linear_gradient_old: {raw_css:'background:#A0BFE8 url(||www_base||background/gradient_linear.jpg) repeat-x; height: 100vh;'},
	radial_gradient_new: {raw_css:'background: radial-gradient(ellipse at 50% 30%, white, blue) fixed; background-position: 20%,50%; height: 100vh;'},
	autumn_landscape: {raw_css:'background:#93876c url(||www_base||background/landscape1.jpg) no-repeat fixed; background-size: cover; background-position: 20%, 50%;  height: 100vh;'},
	seascape: {raw_css:'background:#6699c2 url(||www_base||background/seascape1.jpg) no-repeat fixed; background-size: cover; background-position: 20%, 50%; height: 100vh;'},
	web_mountain_image: {raw_css:'background: #3a4053 url(http://bit.ly/2gPLxZ4) no-repeat fixed; background-size: cover; background-position: 50%, 50%; height: 100vh;'},
	colorful_swirl: {raw_css:'background:#c4581d url(||www_base||background/colorful1.jpg) no-repeat fixed; background-size: cover; background-position: 50%, 50%; height: 100vh;'},
	colorful_fractals: {raw_css:'background:#c4581d url(||www_base||background/colorful2.jpg) no-repeat fixed; background-size: cover; background-position: 50%, 50%; height: 100vh;'},
};
var appearances=[];
function setup_appearances()
{
	for (name in std_appearances) {
		std_appearances[name].name=name;
	}

	//add_raw_appearance( 'plain_white', 'login_box_oldststyle');

	// Sample appearances with old style login box
#	add_std_appearance( 'plain_white', 'login_box_oldststyle');
	add_std_appearance( 'linear_gradient_old', 'login_box_oldststyle');
#	add_std_appearance( 'radial_gradient_new', 'login_box_oldststyle');
#	add_std_appearance( 'autumn_landscape', 'login_box_oldststyle');
#	add_std_appearance( 'seascape', 'login_box_oldststyle');
#	add_std_appearance( 'web_mountain_image', 'login_box_oldststyle');
#	add_std_appearance( 'colorful_swirl', 'login_box_oldststyle');
#	add_std_appearance( 'colorful_fractals', 'login_box_oldststyle');

	// Sample appearances with shadow style login box
	//add_std_appearance( 'plain_white', 'login_box_shadow');
#	add_std_appearance( 'linear_gradient_old', 'login_box_shadow');
#	add_std_appearance( 'radial_gradient_new', 'login_box_shadow');
#	add_std_appearance( 'autumn_landscape', 'login_box_shadow');
#	add_std_appearance( 'seascape', 'login_box_shadow');
#	add_std_appearance( 'web_mountain_image', 'login_box_shadow');
#	add_std_appearance( 'colorful_swirl', 'login_box_shadow');
#	add_std_appearance( 'colorful_fractals', 'login_box_shadow');

	// Sample appearances with frosted glass translucent login boxes
	// add_std_appearance( 'plain_white', 'login_box_blurred');
#	add_std_appearance( 'linear_gradient_old', 'login_box_blurred');
	add_std_appearance( 'radial_gradient_new', 'login_box_blurred');
	add_std_appearance( 'autumn_landscape', 'login_box_blurred');
	add_std_appearance( 'seascape', 'login_box_blurred');
	add_std_appearance( 'web_mountain_image', 'login_box_blurred');
	add_std_appearance( 'colorful_swirl', 'login_box_blurred');
	add_std_appearance( 'colorful_fractals', 'login_box_blurred');

	add_std_appearance( 'radial_gradient_new', 'login_box_shadow');
}

function appearance_set(el_id, appearance_index)
{
	var appearance = appearances[appearance_index];

	if(el_id=="appearance_bg"){
		set_login_box(appearance.box);
	}
	var el = dge(el_id);
	if(!el) return;

	el.className="";
	el.style.cssText = '';
	var appearance = appearances[appearance_index];
	if(!appearance) return;
	el.appearance_index = appearance_index;
	el.className = appearance.class;
	el.style.cssText = appearance.raw_css;

||if_set(newlook_propagate)||
	if(el_id=="appearance_bg"){
		var el = dge("loading_ajax");
		if(!el) return;

		var hidden = class_contains(el,'hidden');

		el.className="";
		el.style.cssText = '';
		var appearance = appearances[appearance_index];
		if(!appearance) return;
		el.appearance_index = appearance_index;
		el.className = appearance.class;
		el.style.cssText = appearance.raw_css;

		if (hidden) class_add(el, 'hidden');
	}
||endif||

	set_appearance_cookie('SW-LOGIN-APPEARANCE', appearance.name, appearance.box);
	calculate_sizes();
}
function set_login_box(box)
{
	var el=document.body;
	class_remove(el,'login_box_oldststyle');
	class_remove(el,'login_box_shadow');
	class_remove(el,'login_box_blurred');
	class_add(el,box);
	el.appearance_box=box;
}

function appearance_init(el_id)
{
	setup_appearances();
	var appearance_index = 1;
	var cookie = get_cookie('SW-LOGIN-APPEARANCE');
	if(cookie!=undefined && cookie.length>0){
		var parts=cookie.split('|');
		if(parts.length==2){
			for(var index=0; index<appearances.length; index++){
				var appearance = appearances[index];
				if(appearance.name==parts[0] && appearance.box==parts[1]){
					appearance_index = index;
					break;
				}
			}
		}
	}
//	debugger
//	var el = dge(el_id); 
//	el.style.cssText = 'background:red;';

	appearance_set(el_id, appearance_index);
}
function appearance_increment(e)
{
	var el = dge("appearance_bg");
	if(!el) return;

	var appearance_index = el.appearance_index+1;
	if(appearance_index>=appearances.length){
		appearance_index=0;
	}

	appearance_set("appearance_bg", appearance_index);
}
function add_std_appearance(type, box)
{
	var bg=std_appearances[type];
	if(!bg) return;
	if(bg.class==undefined) bg.class='';
	if(bg.raw_css==undefined) bg.raw_css='';
	appearances.push( {name:bg.name, class:bg.class, raw_css:bg.raw_css, box:box } );
}

function get_cookie(name)
{
    var dc = document.cookie;
    var prefix = name + "=";
    var begin = dc.indexOf("; " + prefix);
    if (begin == -1) {
        begin = dc.indexOf(prefix);
        if (begin != 0) return null;
    } else {
        begin += 2;
    }
    var end = document.cookie.indexOf(";", begin);
    if (end == -1) {
        end = dc.length;
    }
    return unescape(dc.substring(begin + prefix.length, end));
}
function set_appearance_cookie(string, name, box)
{
    var cookieString = string+'='+name+'|'+box+';Max-Age=315360000'; // remember forever (well 10 years anyway)
    document.cookie = cookieString;
}