// Set cufon text replacement
Cufon.replace('#dab-title h2, #add-layers BUTTON', { fontFamily: 'fineliner' });
Cufon.replace('#dab-controls LABEL', { fontFamily: 'fineliner' }); // done seperately to be refreshed when dynamic content loaded

// Commence jQuery awesomeness
$(function(){
	// Make buttons and title divs align with badge layout
	$('#add-layers').css('width', Settings.layout_w+'px');
	$('#dab-title').css('width', Settings.layout_w+'px');
	$('#layout-container').css('width', Settings.layout_w+'px');
	
	// Set local options and initialise layout
	$("#layout").data(Settings);
	$('#layout').jloSpace({
		css: {
			"background": Settings.hex,
			"width": (Settings.layout_w) + 'px',
			"height": (Settings.layout_h) + 'px',
			"-webkit-border-radius": Settings.corners_r+'px',
			"-moz-border-radius": Settings.corners_r+'px',
			"border-radius": Settings.corners_r+'px'
		},
		hex: Settings.hex
	});
	
	DD_roundies.addRule('#layout', Settings.corners_r+'px');

	// Set default colours text and BG
	$.fn.jloColours.defaults.colours = [
		["grey1",191,191,191,"#BFBFBF"],
		["grey2",127,127,127,"#7F7F7F"],
		["grey3",64,64,64,"#404040"],
		["black",0,0,0,"#000000"]
	];
	$.fn.jloColours.defaults.startKey = 'black';
	
	// Initialise control creation buttons
	$('#add-text').jloAddControl({
		controltype: 'txt',
		containerid: '#txt-controls',
		maxControls: 3,
		afteradd: function(label) {
			Cufon.refresh("#dab-controls LABEL");
			newcontrols = $(this);
			newcontrols.find("SELECT").uniform();
			newcontrols.find("INPUT").uniform();
			var listitem = $('<li />');
			$("#layer-list").append(listitem);
			listitem.jloElementListItem({
				chainTo: newcontrols,
				title: label,
				className: 'text-layer-item',
				removed: function() {
					var mychain = $(this).data('chainTo');
					$('#add-text').jloRemoveControl(mychain);
				}
			});
			newcontrols.data('chainTo').chainTo(listitem);
			$("#list-layers").show();
			newcontrols.data('updated', function(newlabel) { listitem.jloElementListItemUpdate(newlabel); });
		}
	});
	$('#add-image').jloAddControl({
		controltype: 'img',
		containerid: '#img-controls',
		maxControls: 2,
		afteradd: function(label) {
			Cufon.refresh("#dab-controls LABEL");
			newcontrols = $(this);
			var upInput = newcontrols.find("INPUT:file");
			
			var listitem = $('<li />')
			$("#layer-list").append(listitem);
			listitem.jloElementListItem({
				chainTo: newcontrols,
				title: label,
				className: 'image-layer-item',
				removed: function() {
					var mychain = $(this).data('chainTo');
					$('#add-image').jloRemoveControl(mychain);
				}
			});
			listitem.chainTo(newcontrols.data('chainTo'));
			
			$("#layer-list").append(listitem);
			$("#list-layers").show();
			newcontrols.data('updated', function(newlabel) { listitem.jloElementListItemUpdate(newlabel); });
			//upInput.uniform();
			//upInput.trigger('click'); // this breaks IE - calls up the open dialog nicely but does not trigger the change event
		}
	});
	
	var saved = function(response) {
		if(response == "saved") {
			var url = '/order-ecobadgelogoonly.php';
			if ($('#layout span.jlo-layer').length > 0)
				url = '/order-ecobadge.php'
			document.location.href = url+"?"+Settings.layoutid_var+"="+Settings.layout_id;
		} else
			alert('Uncaught save error: '+response);
	}
	var saveerror = function(errormsg) {
		$("#proceed-button").removeAttr("disabled").removeClass('disabled');
		alert('Could not save: '+errormsg);
	}
	
	// Only let logged in users proceed to order
	if(Settings.logged_in && Settings.logged_in != "") {
		$("#disabled-message").hide();
		// Disable button and save when clicked
		$("#proceed-button").click(function() {
			$(this).attr("disabled", "disabled").addClass('disabled');
			$('#layout').children().jloMakeData(); // generate layer data before saving
			// save function params: (parentname, childname, action, after, error)
			$('#layout').jloDataSave('layout', 'layer', Settings.web_path+'/inc/save-handler.php', saved, saveerror)
		});
	} else {
		$("#disabled-message").show();
		$("#proceed-button").attr("disabled", "disabled");
	}
	
	// Start off showing nothing listed
	$("#list-layers").hide();
});

