
//opens picker, which will fire pickColor
function openColorPicker(cId, hexColor){
	pickerDialog.show();
	removeActiveColorFrame();
	colorNumber = cId;
	document.getElementById('cBox' + cId).style.borderColor = '#b83102';
	if(isHex(hexColor)){ //'eg old garsila/color10=image 
		setTimeout("pickerDialog.setColor('" + hexColor + "')", 600); //will also fire function pickColor. Timeout needed to set the pickers correctly first time it is displayed
	}
}

var firstPickAlert = true;
function pickColor( p, hexa ) {
	if(firstPickAlert == true){
		infoMessage('<br>Välj en färg och klicka sedan på "visa"<br> så syns ändringarna i förhandsgranksningen längst ner!');
		firstPickAlert = false;
	}
	var theHSB = getHSB(hexa);
	updateOneColor(colorNumber, hexa, theHSB);
}

//removing border from previous color that was edited
function removeActiveColorFrame(){
	if(isNaN(parseInt(colorNumber)) == false){//colorNumber is global variable
		document.getElementById('cBox' + colorNumber).style.borderColor = '#DDD'; 
	}
}

//will set colors both in hidden formfields and color-squares
function updateOneColor(id, hexval, HSB){ //, init
	if( isHex(hexval) ){
		if( eleExists('cBox' + id) ){ //compatible with wizard
			Ext.get( 'cBox' + id ).setStyle({ backgroundImage: 'none'}); //for old color10=image
			Ext.get( 'cBox' + id ).setStyle({ backgroundColor: '#' + hexval}); //color-squares
		}
		document.getElementById( 'color' + id ).value = hexval; //hidden fields

		//auto-generate a color (color8) with sufficient readability when user change background color
		if (templateId == 3){
			if(id=='1' && HSB !== null && HSB != false){ //initializeColor will not pass HSB; not necessary
				HSB[0] = adjustH(HSB[0],10); //turning the Hue 10 degrees
				HSB[1] = invertVal(HSB[1]); //inverting Saturation
				HSB[2] = invertVal(HSB[2]); //inverting Brightness
				var newHex = getHex(HSB[0],HSB[1],HSB[2]);
				document.getElementById( 'color8').value = newHex;
			}
		}
	}
}

function selectOneFormula(f) { 
	colorStr = getColorStr(f);
	aColors = colorStr.split(";");
	H = Ext.get('hue_global_val').getValue();
	
	i = 0;
	while(i < aColors.length){
		
		aOne = aColors[i].split(",");
		h2 = adjustH(H, aOne[0]);
		colorNumber = aOne[3].slice(1,3); //1-2 digits
		
		hexval = getHex(h2,aOne[1],aOne[2]); //HSB
		HSB = new Array(h2,aOne[1],aOne[2]);
		
		updateOneColor(colorNumber, hexval, HSB)
		i++;
	}
	
	makeColorActionStr(ck);
	
}

//fires when new formula is selected from dropdown
function setAllColorFormula() { //formula
	var i = 0;
	while(i < aColorFormula.length){
		formula = aColorFormula[i];
		
		colorStr = getColorStr(formula);
		aColors = colorStr.split(";");
		H = Ext.get('hue_global_val').getValue();
		
		if( window.pickerDialog ){
			window.pickerDialog.hide();
		}
		if( hueSlider ){ //to do: make sure this fires when page is initialized
			hueSlider.setValue(H);
		}

		j = 0;
		while(j < aColors.length){
			aOne = aColors[j].split(",");
			h2 = adjustH(H, aOne[0]);
			id = aOne[3];
			styleType = aOne[4];
			
			hexval = getHex(h2,aOne[1],aOne[2]); //HSB
			HSB = new Array(h2,aOne[1],aOne[2]);
			
				if(styleType == 'bg'){
					$("#" + formula).find("." + id).css("background-color",'#' + hexval);
				}else if(styleType == 'border'){
					$("#" + formula).find("." + id).css("border-color",'#' + hexval);
				}else if(styleType == 'txt'){
					$("#" + formula).find("." + id).css("color",'#' + hexval);
				}
			
			j++;
		}
		i++;
	}
	
}

function isHex(hex){
	var strPattern = /^[0-9a-f]{3,6}$/i;
	return strPattern.test(hex); 
}


function makeColorActionStr(ck){
	var str;
	str = "_update.asp?nav=setAllColors";
	for(i=1;i<noColors +1;i++){
		if(eleExists('color' + i)){
			str = str + "&color" + i + "=" + Ext.get('color' + i).getValue();
		}
	}
	
	//finding out the luminosity 
	if(lumCol > 0){
		var bgHSB = getHSB(Ext.get('color' + lumCol).getValue());
		if (bgHSB !== null){
			str = str + "&lum=" + bgHSB[2];
		}
	}
	
	str = str + "&ck=" + ck;
	wrtAjaxIndicator('sitePreviewColor');
 	window.frames['sitePreviewColor'].location.href=str;
}

function adjustH(val, increment){
	val = parseInt(val);
	increment = parseInt(increment);
	val = (val + increment)%360;
	if ( val<0 ) { 
		val = val+360;
	}
	return val;
}

function invertVal(val){
	val = parseInt(val);
	val = (val + 50) % 100;
	return val;
}

/****************************************************/
/* conversion formula */


function hsb2rgb(v) {
	var max = Math.round(v[2]*51/20);
	var min = Math.round(max*(1 - v[1]/100));
	if (min == max) return new Array(max, max, max);
	var d = max - min;
	var h6 = v[0]/60;
	if (h6 <= 1) return new Array(max, Math.round(min + h6*d), min);
	if (h6 <= 2) return new Array(Math.round(min - (h6 - 2)*d), max, min);
	if (h6 <= 3) return new Array(min, max, Math.round(min + (h6 - 2)*d));
	if (h6 <= 4) return new Array(min, Math.round(min - (h6 - 4)*d), max);
	if (h6 <= 5) return new Array(Math.round(min + (h6 - 4)*d), min, max);
	return new Array(max, min, Math.round(min - (h6 - 6)*d));
}

function rgb2hex(v) {
	rh = parseInt(v[0]).toString(16); if(rh.length < 2) rh = '0'+rh;
	gh = parseInt(v[1]).toString(16); if(gh.length < 2) gh = '0'+gh;
	bh = parseInt(v[2]).toString(16); if(bh.length < 2) bh = '0'+bh;
	//return '#'+rh+gh+bh;
	return rh+gh+bh;
}

function hex2rgb(h) {
	//if (h.length != 7 || h.charAt(0) != '#') return null;
	//if (!validhex(h)) return null;
	var r = parseInt(h.substr(1,2), 16);
	var g = parseInt(h.substr(3,2), 16);
	var b = parseInt(h.substr(5,2), 16);
	if (isNaN(r) || isNaN(g) || isNaN(b)) return null;
	return new Array(r, g, b);
}

function rgb2hsb(v) {
	var max = Math.max(Math.max(v[0], v[1]), v[2]);
	var min = Math.min(Math.min(v[0], v[1]), v[2]);
	var brightness = Math.round(max*20/51);
	if (min == max) return new Array(0, 0, brightness);
	var saturation = Math.round((1 - min/max)*100);
	var d = max - min;
	var hue = Math.round((
		max == v[0] ? 6 + (v[1] - v[2])/d :
		max == v[1] ? 2 + (v[2] - v[0])/d :
		4 + (v[0] - v[1])/d
	)*60) % 360;
	return new Array(hue, saturation, brightness);
}

function validhex(h) {
	if (h.length != 7 || h.charAt(0) != '#') return null;
	return h;
}

function getHex(H,S,B){
	var aTmp = new Array(H,S,B);
	return rgb2hex(hsb2rgb(aTmp));
}

function getHSB(hex){ //with "#"
	hex = "#" + hex;
	if (!validhex(hex)) return null;
	var aTmp = hex2rgb(hex);
	return rgb2hsb(aTmp);
}
	

