// Settings
	
window.$debug = false; 
//window.$serverside = "/";
window.$serverside = ".ashx";
window.$ads = false;


// Usage

if ($debug) __requireJs("/_common/groundlogic/debug/debug_js.js");

__requireJs("/_common/tradesman/richlogic.js");
__requireJs("/_common/tradesman/collector.js");
__requireJs("/_common/tradesman/dashboard.js");
__requireJs("/_common/tradesman/tracker.js");


// Class

var Tradesman = function() {


	var $this = this;


	// Variables

	var $login = false;
	var $theme = "";
	

	// Constructor

	this.Tradesman = function() {
		$(document).ready(function(){ $this.init(); });
		$(window).bind("resize", function(){  $this.layoutFix(); });
	}
	

	// Init
	
	this.init = function() {
		__debugJS.part("Tradesman init");
		// Get login
		$login = __cookieGet('login');
		// Move theme to body
		// Override theme with cookie
		$theme = $("#Template").attr("class");
		var theme = __cookieGet('theme');
		if (theme) $theme = theme;
		$("#Template").removeClass();
		$("body").removeClass();
		$("body").addClass($theme);
		// Set decor
		//$this.themeDecor();
		// Layout
		if ($ads) $this.adsLoad();
		$this.layoutFix();
	}


	// Functions

	// Themes

	this.themeChange = function(theme) {
		// Set cookie
		if (theme == 'reset') {
			__cookieDelete('theme');
		} else {
			__cookieSet('theme',theme);
		}
		// Refresh
		window.location.href = window.location.href;
	}
	window.themeChange = this.themeChange;

	this.themeDecor = function() {
		// Get attributes
		var decor = $("meta[name='decor']").attr('content');
		var textcolor = $("meta[name='textcolor']").attr('content');
		var font = $("meta[name='font']").attr('content');
		// Set shortcuts
	    var theme = ''; if ($theme != "") theme = '.'+$theme+' ';
	    var background = ''; if (decor != "") background = 'background-color: ' + decor +'; ';
	    var color = ''; if (textcolor != "") color = 'color: ' + textcolor +'; ';
	    var fontfamily = ''; if (font != "") fontfamily = 'font-family: ' + font +'; ';
		// Build CSS
	    var css = '';
	    css += '<style type="text/css">' +'\n';
	    css += theme + '#Page { '+fontfamily+' }' +'\n';
	    css += theme + '#MainLogo .LogoText { '+color+' }' +'\n';
	    css += theme + '#MainLogo .LogoBox { '+background+' }' +'\n';
	    css += theme + '#Content .ContentPanel { '+color+' }' +'\n';
	    css += theme + '#Content .ContentPanel { '+background+' }' +'\n';
	    css += theme + '#Content .ContentCase { '+background+' }' +'\n';
	    css += theme + '#Content .ContentCase .CaseText { '+color+' }' +'\n';
	    css += theme + '#Content .ContentCase .CaseText { '+background+' }' +'\n';
	    css += theme + '#Content .CaseNavigation { '+background+' }' +'\n';
	    css += theme + '#Content .CaseNavigation a { '+color+' }' +'\n';
	    css += theme + '#Content .CaseNavigation .Center { '+color+' }' +'\n';
	    css += theme + '#MenuItems a { '+fontfamily+' }' +'\n';
	    css += theme + '#MenuItems ul li:hover { '+background+' }' +'\n';
	    css += theme + '#MenuItems ul li:hover a { '+color+' }' +'\n';
	    css += theme + '#MenuItems ul li:hover ul { '+background+' }' +'\n';
	    css += theme + '#MenuItems ul li:hover ul li { '+background+' }' +'\n';
	    css += theme + '#MenuItems ul li.Active { '+background+' }' +'\n';
	    css += theme + '#MenuItems ul li.Active a { '+color+' }' +'\n';
	    css += theme + '#MenuItems ul li.Active a:hover { '+color+' }' +'\n';
	    css += theme + '#MenuItems ul li.Active:hover ul { '+background+' }' +'\n';
	    css += theme + '#MenuItems ul li.Active:hover ul li { '+background+' }' +'\n';			
	    css += theme + '#MenuItems ul li.Active:hover ul li { '+background+' }' +'\n';			
	    css += theme + '#MenuItems ul li.Collector a { color: #ffffff }' +'\n';
	    css += theme + '#MenuItems ul li.Collector { background-color: #66aadd }' +'\n';
	    css += theme + '#MenuItems ul li.Account a { color: #ffffff }' +'\n';
	    css += theme + '#MenuItems ul li.Account { background-color: #66aadd }' +'\n';
	    css += '</style>';
	    // Insert CSS
	    $("head").append(css);
	}
	

	// Ads

	this.adsLoad = function() {
		var query = "logout";
		if ($login) query = "login";
		var url = "/_common/tradesman/services/tradesman.ads" + $serverside + "?" + query;
		$("#AdsHolder").load(url, function(){
			// Callback:
		});
		if ($login) {
			$("#Ads").addClass("Fixed");
		} else {
			$("#Ads").removeClass("Fixed");
		}
	}
	
	
	// Layout
	
	this.layoutFix = function() {
		if ($(window).width() <= 1000) {
			$("body").css({ "background-position":"800px" });
			$("#PageAlign").css({ "left":"30px", "margin-left":"0" });
		} else {
			$("body").css({ "background-position":"right" });
			$("#PageAlign").css({ "left":"50%", "margin-left":"-470px" });
		}
		if ($(window).width() <= 810) {
			$("body").css({ "overflow-x":"auto" });
		} else {
			$("body").css({ "overflow-x":"hidden" });
		}
		if ($("#Page").width() <= 810) {
			$("#Ads").addClass('Small');
		} else {
			$("#Ads").removeClass('Small');
		}
	}


}


// Create Instance

window.$Tradesman = new Tradesman(); $Tradesman.Tradesman();


