var visualisatie = function(requestBaseUrl) {
	var me = this;
	var requestBaseUrl = requestBaseUrl;
};

var visualisatieBeheer = function(requestBaseUrl) {

	var me = this;
	var requestBaseUrl = requestBaseUrl;
	this.currentStartYear = 0;
	
	var $beheer = $([]);
	var $beheerMelding = $([]);
	var $scenarios = $([]);
	var $scenariosHeadYears = $([]);
	var $scenariosHeadTijdbalk = $([]);
	var $scenariosBody = $([]);
	var $scenarioTitel = $([]);
	var $scenarioOmschrijving = $([]);
	var $scenarioLijnKleur = $([]);
	var $scenarioLijnStijl = $([]);
	var $visualisatieGrafiek = $([]);
	var $visualisatieGrafiek = $([]);
	var $yearSpinnerLeft = $([]);
	var $yearSpinnerRight = $([]);
	
	var fxTimer;
	var bHasChanges = false;
	var timestamp = new Date();
	var currentYear = timestamp.getFullYear();
	var navigatingYears = false;
	
	var SCENARIO_TYPE_CUSTOM = 1;
	var SCENARIO_TYPE_AUTONOME_ONTWIKKELING = 4;
	var SCENARIO_TYPE_GEREALISEERDE_UITSTOOT = 3;
	var SCENARIO_TYPE_VERWACHTE_UITSTOOT = 2;
	
	var GRAFIEK_LINE_STYLE_SOLID = 1;
	var GRAFIEK_LINE_STYLE_DASH = 2;
	var GRAFIEK_LINE_STYLE_DOT = 3;
	
	var LEFT = 0;
	var RIGHT = 1;
	
	var scenarioDefaultColors = {
		SCENARIO_TYPE_AUTONOME_ONTWIKKELING: "#000000",
		SCENARIO_TYPE_GEREALISEERDE_UITSTOOT: "#5400ff",
		SCENARIO_TYPE_VERWACHTE_UITSTOOT: "#5400ff",
		scenario1: "#0000ff",
		scenario2: "#ff9900",
		scenario3: "#99cc00",
		other: "#000000"
	};
	
	var toSimpleColor = function(pElement, pColor) {
		$("div#popup_scenario").find("div.simpleColorContainer").remove();
		if (pColor !== null && pColor !== undefined) {
			pElement.simpleColor({
				defaultColor: pColor
			});
		} else {
			pElement.simpleColor();
		}
		$("div#popup_scenario").find("input.simpleColorSelectButton,input.simpleColorCancelButton").button();
	};
		
	// Melding weergeven en verbergen na 2 seconden
	this.showMessage = function(psMessage, pbShowAsError) {
		if (pbShowAsError === true) {
			$beheerMelding.addClass("error");
		}
		if (!fxTimer) {
			$beheerMelding.html(psMessage).fadeIn("fast");
		} else {
			$beheerMelding.html(psMessage).show();
		}
		clearTimeout(fxTimer);
		fxTimer = setTimeout(function(){
			me.hideMessage();
		}, 2000);
	};
	
	// Forceren melding te verbergen
	this.hideMessage = function() {
		$beheerMelding.fadeOut("fast", function(){
			$beheerMelding.removeClass("error").empty();
		});
	};
	
	// Initiële functie
	this.init = function() {
		$beheer = $("div#ve_beheer");
		$beheerMelding = $beheer.find("div.ve_melding");
		$scenarios = $beheer.find("table#ve_scenarios");
		$scenariosHeadYears = $scenarios.find("thead tr.years");
		$scenariosHeadTijdbalk = $scenarios.find("thead tr.tijdbalk");
		$scenariosBody = $scenarios.find("tbody");
		$yearSpinnerLeft = $scenarios.find("th#year-spinner-left");
		$yearSpinnerRight = $scenarios.find("th#year-spinner-right");
		
		// velden in scenario popup
		$scenarioTitel = $("input#input_scenario_titel");
		$scenarioOmschrijving = $("textarea#input_scenario_omschrijving");
		$scenarioLijnKleur = $('input#input_scenario_lijn_kleur');
		$scenarioLijnStijl = $('select#input_scenario_lijn_stijl');
		
		// grafiek holder
		$visualisatieGrafiek = $("div#visualisatie-chart");

		// huidige visualisatiedetails en scenarios opvragen
		me.getVisualisatiedetails();
		me.getScenarios();
		me.getScenariosVisueel();
		
		// event handler voor opslaan scenarios data
		$beheer.find("a#ve_visualisatie_opslaan").click(function(){
			me.submitVisualisatiedetails();
		});
		
		// event handler voor exporteren van scenarios data naar csv
		$beheer.find("a#ve_jaargegevens_exporteren").click(function(){
			me.exportToCsv();
		});

		// event handler voor tonen popup voor toevoegen scenario
		$beheer.find("a#ve_scenario_toevoegen").click(function(){
			var iAantalScenarios = me.getAantalScenarios();
			var sLineColor = scenarioDefaultColors.other;;
			var $linestyleSelectbox = $("select#input_scenario_lijn_stijl");
			
			if (iAantalScenarios == 3) {
				sLineColor = scenarioDefaultColors.scenario1;
				$linestyleSelectbox
			} else if (iAantalScenarios == 4) {
				sLineColor = scenarioDefaultColors.scenario2;
			} else if (iAantalScenarios == 5) {
				sLineColor = scenarioDefaultColors.scenario3;
			}
			
			if (iAantalScenarios >= 3) {
				$linestyleSelectbox.find("option[value='" + GRAFIEK_LINE_STYLE_DASH + "']").attr("selected", "selected");
			}

			$scenarioLijnKleur.val(sLineColor);
			toSimpleColor($scenarioLijnKleur, sLineColor);
			
			customPopup("div#popup_scenario", {  
				title: "Scenario toevoegen",
				resizable: false,
				buttons: {
					Opslaan: function() {
						if ($scenarioTitel.val() !== "") {
							me.scenarioToevoegen($scenarioTitel.val(), $scenarioOmschrijving.val(), $scenarioLijnKleur.val(), $scenarioLijnStijl.val());
							me.clearScenarioInputs();
							$("div#popup_scenario:ui-dialog").dialog("close");
							me.getScenarios(me.currentStartYear);
							me.getScenariosVisueel();
						}
					},
					Annuleren: function() {
						me.clearScenarioInputs();
						$("div#popup_scenario:ui-dialog").dialog("close");
					}
				}
			});
		});
		
		// event handler voor opslaan jaargegevens scenario
		$beheer.find("a#ve_jaargegevens_opslaan").click(function(){
			me.submitScenariogegevens();
		});
		
		// navigatie handlers
		var navigateYearOffset = 0;
		var newYear = 0;
		var navigateYears = function(direction, offset) {
			if (direction === LEFT) {
				navigateYearOffset--;
			} else if (direction === RIGHT) {
				navigateYearOffset++;
			}
			newYear = parseInt(me.currentStartYear) + navigateYearOffset;
		};
		
		$beheer.find("div#ve_pijl_links").mousehold(150, function(){
			navigateYears(LEFT);
			$yearSpinnerLeft.html("<span class='year-spinner'>" + newYear + " &laquo;</span>");
		}).mousedown(function(){
			clearTimeout(fxTimer);
			fxTimer = setTimeout(function(){
				$yearSpinnerLeft.show();
			}, 500);
		}).mouseup(function(){			
			$yearSpinnerLeft.find("span.year-spinner").fadeOut("fast");
			me.getScenarios(newYear);
			navigateYearOffset = 0;
		});
		
		$beheer.find("div#ve_pijl_rechts").mousehold(150, function(){
			navigateYears(RIGHT);
			$yearSpinnerRight.html("<span class='year-spinner'>&raquo; " + (newYear + 9) + "</span>");
		}).mousedown(function(){
			clearTimeout(fxTimer);
			fxTimer = setTimeout(function(){
				$yearSpinnerRight.show();
			}, 500);
		}).mouseup(function(){
			$yearSpinnerRight.find("span.year-spinner").fadeOut("fast");
			me.getScenarios(newYear);
			navigateYearOffset = 0;
		});
		
	};
	
	// Nieuw scenario toevoegen
	this.scenarioToevoegen = function(pTitel, pOmschrijving, pLijnKleur, pLijnStijl) {
		$.ajax({
			url: requestBaseUrl + "scenarioToevoegen",
			type: "POST",
			async: false,
			data: {
				ajax: 1,
				titel: pTitel,
				omschrijving: pOmschrijving,
				lijn_kleur: pLijnKleur,
				lijn_stijl: pLijnStijl
			},
			success: function (pResult) {
				me.showMessage(pResult.message, !pResult.result);
			},
			dataType: "json"
		});
	};
	
	// Scenario bewerken
	this.scenarioBewerken = function(pId, pTitel, pOmschrijving, pLijnKleur, pLijnStijl) {
		$.ajax({
			url: requestBaseUrl + "scenarioToevoegen/1",
			type: "POST",
			async: false,
			data: {
				ajax: 1,
				id: pId,
				titel: pTitel,
				omschrijving: pOmschrijving,
				lijn_kleur: pLijnKleur,
				lijn_stijl: pLijnStijl
			},
			success: function (pResult) {
				me.showMessage(pResult.message, !pResult.result);
			},
			dataType: "json"
		});
	};
	
	// Direct een nieuwe visualisatie aanmaken
	this.visualisatieAanmaken = function(callback) {
		$.ajax({
			url: requestBaseUrl + "visualsiatieAanmaken",
			type: "POST",
			data: {
				ajax: 1
			},
			success: function (pResult) {
				if (typeof callback == "function") {
					callback(pResult);
				}
			},
			dataType: "json"
		});
	};
		
	// Visualisatiedetails opslaan
	this.submitVisualisatiedetails = function() {
		var $titel = $beheer.find("input#ve_titel");
		var $omschrijving = $beheer.find("textarea#ve_omschrijving");
		var $jaarVan = $beheer.find("input#ve_jaar_van");
		var $jaarTot = $beheer.find("input#ve_jaar_tot");
		
		$.ajax({
			url: requestBaseUrl + "visualsiatiedetailsOpslaan",
			type: "POST",
			data: {
				ajax: 1,
				titel: $titel.val(),
				omschrijving: $omschrijving.val(),
				jaar_van: $jaarVan.val(),
				jaar_tot: $jaarTot.val()
			},
			success: function (pResult) {
				me.showMessage(pResult.message, !pResult.result);
				me.getScenariosVisueel();
			},
			dataType: "json"
		});
	};
	
	// Scenario data exporteren naar CSV
	this.exportToCsv = function() {
		location.assign(requestBaseUrl + "exportToCsv" + "/" + me.currentStartYear)
	};
	
	// Jaargegevens van scenario opslaan
	this.submitScenariogegevens = function() {
		var objScenarios = [];
		var arrScenarioData = [];
		var bHasError = false;
		var iScenarioType = SCENARIO_TYPE_CUSTOM;

		// jaargegevens verzamelen
		$scenarios.find("tr.scenario").each(function(i){
			var $this = $(this);
			iScenarioType = $this.data("scenario_type");
			objScenarios[i] = {};
			objScenarios[i].id = $this.data("scenario");
			objScenarios[i].type = iScenarioType;
			$(this).find("td.year").each(function(j){
					iValue = $(this).find("input").val();
					if (iScenarioType == SCENARIO_TYPE_CUSTOM || iScenarioType == SCENARIO_TYPE_AUTONOME_ONTWIKKELING) {
						if (iValue < 0) {
							bHasError = true;
						}
					}
			
					arrScenarioData[j] = {
						year: $(this).data("year"),
						value: iValue
					};
			});
			objScenarios[i].data = arrScenarioData;
			arrScenarioData = [];
		});
		
		if (bHasError == true) {
			me.showMessage("Er kan geen negatieve uitstoot worden ingevoerd.", true);
			return false;
		}
	
		// jaargegevens opslaan
		$.ajax({
			url: requestBaseUrl + "scenarioDataOpslaan",
			type: "POST",
			async: false,
			data: {
				ajax: 1,
				start_year: me.currentStartYear,
				scenario_data: JSON.stringify(objScenarios, null)
			},
			success: function (pResult) {
				bHasChanges = false;
				me.getScenarios(me.currentStartYear);
				me.getScenariosVisueel();
			},
			dataType: "json"
		});
		
		return true;
	};
	
	// Visualisatiedetails opvragen
	this.getVisualisatiedetails = function() {
		var $titel = $beheer.find("input#ve_titel");
		var $omschrijving = $beheer.find("textarea#ve_omschrijving");
		var $jaarVan = $beheer.find("input#ve_jaar_van");
		var $jaarTot = $beheer.find("input#ve_jaar_tot");
		
		me.showMessage("Laden...", false);
		
		$.ajax({
			url: requestBaseUrl + "getVisualsiatiedetails",
			type: "POST",
			data: { ajax: 1 },
			success: function (pResult) {
				if (pResult.result === false) {
					me.showMessage(pResult.message, true);
				} else {
					$titel.val(pResult.data.titel);
					$omschrijving.val(pResult.data.omschrijving);
					$jaarVan.val(pResult.data.jaar_van);
					$jaarTot.val(pResult.data.jaar_tot);
				}
				me.hideMessage();
			},
			dataType: "json"
		});
	};
	
	// Aantal scenarios opvragen (die op dit moment worden weergegeven)
	this.getAantalScenarios = function() {
		return $scenariosBody.find("tr.scenario").length;
	};
	
	// Scenario grid resetten
	this.resetScenarios = function() {
		bHasChanges = false;
		$scenariosHeadYears.find("th.year").remove();
		$scenariosHeadTijdbalk.find("th.year").remove();
		$scenariosBody.empty();
	};
	
	// Scenario verwijderen
	this.removeScenario = function(scenarioId) {
		customPopup("#popup_confirm_remove_scenario", {
			title: "Scenario verwijderen",
			resizable: false,
			buttons: {
				Ok: function() {
					$("#popup_confirm_remove_scenario").dialog("close");
					$.ajax({
						url: requestBaseUrl + "removeScenario",
						type: "POST",
						data: {
							ajax: 1,
							scenario_id: scenarioId
						},
						success: function (pResult) {
							me.showMessage(pResult.message, !pResult.result);
							if (pResult.result === true) {
								$scenariosBody.find("tr#scenario" + scenarioId).remove();
							}
						},
						dataType: "json"
					});
				},
				Annuleren: function() {
					$("#popup_confirm_remove_scenario").dialog("close");
				}
			}
		});
	};
	
	// Scenario invoervelden wissen
	this.clearScenarioInputs = function() {
		$scenarioTitel.val("");
		$scenarioOmschrijving.val("");
		$scenarioLijnKleur.val("#000000");
		$scenarioLijnStijl.find("option:first").attr("selected", "selected");
	};
	
	// Scenarios opvragen
	this.getScenarios = function(startYear) {		
		me.showMessage("Laden...", false);
		
		// scenario data opslaan als deze intussen zijn aangepast
		if (bHasChanges == true) {
			if (me.submitScenariogegevens() == false) {
				me.showMessage("Fout bij opslaan scenario gegevens.");
				return false;
			}
		}

		if (startYear !== undefined && !isNaN(startYear)) {
			me.currentStartYear = startYear;
		} else {
			me.currentStartYear = 0;
		}
		
		// scenario data opvragen via xhr
		$.ajax({
			url: requestBaseUrl + "getScenarios",
			type: "POST",
			data: {
				start_year: me.currentStartYear,
				ajax: 1
			},
			success: function (pResult) {
				if (pResult.result === false) {
					me.showMessage(pResult.message, true);
				} else {
					me.currentStartYear = pResult.data.start_year;
					me.resetScenarios();

					// jaar kolommen toevoegen
					for(var iYearIndex=0; iYearIndex<pResult.data.years.length; iYearIndex++) {
						$scenariosHeadYears.find("th:last").before("<th class='year'>" + pResult.data.years[iYearIndex] + "</th>");
						$scenariosHeadTijdbalk.find("th#ve_links").after("<th class='year'><div>&nbsp;</div></th>");
					}
					
					// scenario data toevoegen aan tabel
					var year = 0;
					var scenario = {};
					var scenarioDataHtml;
					var scenarioRowHtml;
					var iYearInputValue = "";

					for(iScenarioIndex in pResult.data.data) {
						scenarioDataHtml = "";
						scenarioRowHtml = "";
						
						scenario = pResult.data.data[iScenarioIndex];
						for(iYearIndex in scenario.data) {			
							if (scenario.data[iYearIndex][1] == 0) {
								iYearInputValue = "";
							} else {
								iYearInputValue = scenario.data[iYearIndex][1];
							}
							scenarioDataHtml += "<td class='year' data-year='" + scenario.data[iYearIndex][0] + "'><input type='text' class='txt_input input_year center width-50' name='ve_grid_years[]' value='" + iYearInputValue + "' /></td>";
						}
						
						if (scenario.scenario_type > 1) {
							scenarioDataHtml += "<td class='actions'></td>";
						} else {
							scenarioDataHtml += "<td class='actions'>" +
																		"<a href='javascript:;'><span class='icon icon-remove remove-scenario'></span></a>" +
																	"</td>";
						}

						scenarioRowHtml = "<tr id='scenario" + scenario.scenario_id + "' class='scenario scenariotype" + scenario.scenario_type + "' data-scenario='" + scenario.scenario_id + "' data-titel='" + scenario.titel + "' data-omschrijving='" + scenario.omschrijving + "' data-lijn_kleur='" + scenario.lijn_kleur + "' data-lijn_stijl='" + scenario.lijn_stijl + "' data-scenario_type='" + scenario.scenario_type + "'>";
						scenarioRowHtml += "<td><a href='javascript:;' title='" + scenario.omschrijving + "' class='edit-scenario'>" + scenario.titel + "</a></td>";
						scenarioRowHtml += scenarioDataHtml + "</tr>";
						
						$scenariosBody.append(scenarioRowHtml);
					}
					
					// gerealiseerde co2-uitstoot row instellen
					$scenariosBody.find("tr.scenariotype" + SCENARIO_TYPE_GEREALISEERDE_UITSTOOT + " td.year").each(function(i){
						var $this = $(this);
						//if ($this.data("year") >= currentYear) {
							$this.find("input").addClass("marked").attr("disabled", "disabled");
						//}
					});
					
					// verwachte co2-uitstoot row instellen
					$scenariosBody.find("tr.scenariotype" + SCENARIO_TYPE_VERWACHTE_UITSTOOT + " td.year").each(function(i){
						var $this = $(this);
						//if ($this.data("year") < currentYear) {
							$this.find("input").addClass("marked").attr("disabled", "disabled");
						//}
					});
					
					// event handeler voor het verwijderen van scenarios
					$scenariosBody.find("span.remove-scenario").click(function(){
						var scenarioId = $(this).closest("tr.scenario").data("scenario");
						me.removeScenario(scenarioId);
					});
					
					// event handeler voor het bewerken van scenario
					$scenariosBody.find("a.edit-scenario").click(function(){
						var $scenarioRow = $(this).closest("tr.scenario");
						
						var scenarioId = $scenarioRow.data("scenario");
						var scenarioTitle = $scenarioRow.data("titel");
						var scenarioOmschrijving = $scenarioRow.data("omschrijving");
						var scenarioLijnKleur = $scenarioRow.data("lijn_kleur");
						var scenarioLijnStijl = $scenarioRow.data("lijn_stijl");
						var scenarioType = $scenarioRow.data("scenario_type");
					
						if (scenarioType > SCENARIO_TYPE_CUSTOM) {
							$scenarioTitel.attr("disabled", "disabled");
						}
					
						$scenarioLijnKleur.val(scenarioLijnKleur);
					
						toSimpleColor($scenarioLijnKleur, scenarioLijnKleur);
						
						customPopup("#popup_scenario", {  
							title: "Scenario bewerken",
							resizable: false,
							buttons: {
								Opslaan: function() {
									if ($scenarioTitel.val() !== "") {
										me.scenarioBewerken(scenarioId, $scenarioTitel.val(), $scenarioOmschrijving.val(), $scenarioLijnKleur.val(), $scenarioLijnStijl.val());
										me.clearScenarioInputs();
										$("#popup_scenario:ui-dialog").dialog("close");
										if (scenarioType > SCENARIO_TYPE_CUSTOM) {
											$scenarioTitel.removeAttr("disabled");
										}
										me.getScenarios(me.currentStartYear);
										me.getScenariosVisueel();
									}
								},
								Annuleren: function() {
									me.clearScenarioInputs();
									$("#popup_scenario:ui-dialog").dialog("close");
									if (scenarioType > SCENARIO_TYPE_CUSTOM) {
										$scenarioTitel.removeAttr("disabled");
									}
								}
							}
						});
						
						// scenario velden vullen
						$scenarioTitel.val(scenarioTitle);
						$scenarioOmschrijving.val(scenarioOmschrijving);
						$scenarioLijnStijl.val(scenarioLijnStijl);
					});
					
					// bij houden of het grid is aangepast
					$scenariosBody.find("input.input_year").keyup(function(){
						var $this = $(this);
						if ($this.val() !== 0 && $.trim($this.val()) !== "") {
							bHasChanges = true;
						}
					});
				}
				me.hideMessage();
			},
			dataType: "json"
		});
	};
	
	// Scenario data opvragen en weergeven in vorm van grafiek
	this.getScenariosVisueel = function() {		
		$.ajax({
			url: requestBaseUrl + "getScenariosVisueel",
			type: "POST",
			data: { ajax: 1 },
			success: function (pResult) {
				if (pResult.result === true) {
					if (pResult.data.html !== undefined) {
						$visualisatieGrafiek.html(pResult.data.html);
					}
					me.showMessage(pResult.message, !pResult.result);
				}
				
				// visualisatie grafiek vernieuwen
				$visualisatieGrafiek.find("a#ve_chart_refresh").click(function(){
					me.getScenariosVisueel();
				});
			},
			dataType: "json"
		});
	};
};

/*
 * jQuery simple-color plugin
 * @requires jQuery v1.4.2 or later
 *
 * See http://recursive-design.com/projects/jquery-simple-color/
 *
 * Licensed under the MIT license:
 *   http://www.opensource.org/licenses/mit-license.php
 *
 * Version: 1.0.0 (201102151633)
 */
(function(c){c.fn.simpleColor=function(b){var m=["990033","ff3366","cc0033","ff0033","ff9999","cc3366","ffccff","cc6699","993366","660033","cc3399","ff99cc","ff66cc","ff99ff","ff6699","cc0066","ff0066","ff3399","ff0099","ff33cc","ff00cc","ff66ff","ff33ff","ff00ff","cc0099","990066","cc66cc","cc33cc","cc99ff","cc66ff","cc33ff","993399","cc00cc","cc00ff","9900cc","990099","cc99cc","996699","663366","660099","9933cc","660066","9900ff","9933ff","9966cc","330033","663399","6633cc","6600cc","9966ff","330066",
"6600ff","6633ff","ccccff","9999ff","9999cc","6666cc","6666ff","666699","333366","333399","330099","3300cc","3300ff","3333ff","3333cc","0066ff","0033ff","3366ff","3366cc","000066","000033","0000ff","000099","0033cc","0000cc","336699","0066cc","99ccff","6699ff","003366","6699cc","006699","3399cc","0099cc","66ccff","3399ff","003399","0099ff","33ccff","00ccff","99ffff","66ffff","33ffff","00ffff","00cccc","009999","669999","99cccc","ccffff","33cccc","66cccc","339999","336666","006666","003333","00ffcc",
"33ffcc","33cc99","00cc99","66ffcc","99ffcc","00ff99","339966","006633","336633","669966","66cc66","99ff99","66ff66","339933","99cc99","66ff99","33ff99","33cc66","00cc66","66cc99","009966","009933","33ff66","00ff66","ccffcc","ccff99","99ff66","99ff33","00ff33","33ff33","00cc33","33cc33","66ff33","00ff00","66cc33","006600","003300","009900","33ff00","66ff00","99ff00","66cc00","00cc00","33cc00","339900","99cc66","669933","99cc33","336600","669900","99cc00","ccff66","ccff33","ccff00","999900","cccc00",
"cccc33","333300","666600","999933","cccc66","666633","999966","cccc99","ffffcc","ffff99","ffff66","ffff33","ffff00","ffcc00","ffcc66","ffcc33","cc9933","996600","cc9900","ff9900","cc6600","993300","cc6633","663300","ff9966","ff6633","ff9933","ff6600","cc3300","996633","330000","663333","996666","cc9999","993333","cc6666","ffcccc","ff3333","cc3333","ff6666","660000","990000","cc0000","ff0000","ff3300","cc9966","ffcc99","ffffff","cccccc","999999","666666","333333","000000","000000","000000","000000",
"000000","000000","000000","000000","000000"];b=c.extend({defaultColor:this.attr("defaultColor")||"#FFF",border:this.attr("border")||"1px solid #000",cellWidth:this.attr("cellWidth")||10,cellHeight:this.attr("cellHeight")||10,cellMargin:this.attr("cellMargin")||1,boxWidth:this.attr("boxWidth")||"115px",boxHeight:this.attr("boxHeight")||"20px",columns:this.attr("columns")||16,insert:this.attr("insert")||"after",buttonClass:this.attr("buttonClass")||"",colors:this.attr("colors")||m,displayColorCode:this.attr("displayColorCode")||
false,colorCodeAlign:this.attr("colorCodeAlign")||"center",colorCodeColor:this.attr("colorCodeColor")||"#FFF"},b||{});this.hide();b.totalWidth=b.columns*(b.cellWidth+2*b.cellMargin);if(c.browser.msie)b.totalWidth+=2;b.totalHeight=Math.ceil(b.colors.length/b.columns)*(b.cellHeight+2*b.cellMargin);c.simpleColorOptions=b;this.each(function(){var a=c.simpleColorOptions,i=c("<div class='simpleColorContainer' />"),n=this.value&&this.value!=""?this.value:a.defaultColor,d=c("<div class='simpleColorDisplay' />");
d.css("backgroundColor",n);d.css("border",a.border);d.css("width",a.boxWidth);d.css("height",a.boxHeight);i.append(d);if(a.displayColorCode){d.text(this.value);d.css("color",a.colorCodeColor);d.css("textAlign",a.colorCodeAlign)}var k=c("<input type='button' value='Selecteer' class='simpleColorSelectButton "+a.buttonClass+"'>");i.append(k);var j=c("<input type='button' value='Annuleren' class='simpleColorCancelButton "+a.buttonClass+"'>");i.append(j);j.hide();k.bind("click",{container:i,input:this,cancel_button:j,
display_box:d},function(e){c(this).hide();e.data.cancel_button.show();if(e.data.container.chooser)e.data.container.chooser.show();else{var g=c("<div class='simpleColorChooser'/>");g.css("border",a.border);g.css("margin","0px");g.css("margin-top","3px");g.css("width",a.totalWidth+"px");g.css("height",a.totalHeight+"px");e.data.container.chooser=g;e.data.container.append(g);for(var l=0;l<a.colors.length;l++){var f=c("<div class='simpleColorCell' id='"+a.colors[l]+"'/>");f.css("width",a.cellWidth+"px");
f.css("height",a.cellHeight+"px");f.css("margin",a.cellMargin+"px");f.css("cursor","pointer");f.css("lineHeight",a.cellHeight+"px");f.css("fontSize","1px");f.css("float","left");f.css("backgroundColor","#"+a.colors[l]);g.append(f);f.bind("click",{input:e.data.input,chooser:g,select_button:k,cancel_button:j,display_box:d},function(h){h.data.input.value="#"+this.id;h.data.display_box.css("backgroundColor","#"+this.id);h.data.chooser.hide();h.data.cancel_button.hide();h.data.display_box.show();h.data.select_button.show();
a.displayColorCode&&h.data.display_box.text("#"+this.id)})}}});j.bind("click",{container:i,select_button:k,display_box:d},function(e){c(this).hide();e.data.container.find(".simpleColorChooser").hide();e.data.display_box.show();e.data.select_button.show()});c(this).after(i)});return this};c.fn.closeSelector=function(){this.each(function(){var b=c(this).parent().find("div.simpleColorContainer");b.find(".simpleColorCancelButton").hide();b.find(".simpleColorChooser").hide();b.find(".simpleColorDisplay").show();
b.find(".simpleColorSelectButton").show()});return this}})(jQuery);

/**
 * jQuery mousehold plugin - fires an event while the mouse is clicked down.
 * Additionally, the function, when executed, is passed a single
 * argument representing the count of times the event has been fired during
 * this session of the mouse hold.
 *
 * @author Remy Sharp (leftlogic.com)
 * @date 2006-12-15
 * @example $("img").mousehold(200, function(i){  })
 * @desc Repeats firing the passed function while the mouse is clicked down
 *
 * @name mousehold
 * @type jQuery
 * @param Number timeout The frequency to repeat the event in milliseconds
 * @param Function fn A function to execute
 * @cat Plugin
 */

jQuery.fn.mousehold = function(timeout, f) {
	if (timeout && typeof timeout == 'function') {
		f = timeout;
		timeout = 100;
	}
	if (f && typeof f == 'function') {
		var timer = 0;
		var fireStep = 0;
		return this.each(function() {
			jQuery(this).mousedown(function() {
				fireStep = 1;
				var ctr = 0;
				var t = this;
				timer = setInterval(function() {
					ctr++;
					f.call(t, ctr);
					fireStep = 2;
				}, timeout);
			})

			clearMousehold = function() {
				clearInterval(timer);
				if (fireStep == 1) f.call(this, 1);
				fireStep = 0;
			}
			
			jQuery(this).mouseout(clearMousehold);
			jQuery(this).mouseup(clearMousehold);
		})
	}
}
