var cssBackgroundImage="";
function dechex(decimal) {return decimal.toString(16);}
function hexdec(hexadecimal) {return parseInt(hexadecimal,16);} 
function average(array) {
	var sum=0
	var count=0
	$(array).each(function() {sum+=this; count++;});
	return(sum/count);
}
/*document.writeln(average([1, 2, 3, 4, 5]));
document.writeln(dechex(15));
document.writeln(hexdec('b'));*/

function offsetColor(hex, offset) {
	/*document.writeln("<br>"+hex);*/
	if(!offset) {var offset=25;}
	hex = hex.replace(/#/g, '');
	prehex=hex;
	if(hex.length==3) {hex=hex.substr(0, 1)+hex.substr(0, 1) +hex.substr(1, 1)+hex.substr(1, 1) +hex.substr(2, 1)+hex.substr(2, 1);}
	if(hex.length!=6) {return;}
	/*document.writeln("->"+hex);*/
	var red		= hexdec(hex.substr(0, 2));
	var green	= hexdec(hex.substr(2, 2));
	var blue	= hexdec(hex.substr(4, 2));
	/*document.writeln("->"+red+"|"+green+"|"+blue+"<br>");*/
	if (blue>average([red, green])) {
		blue	=((blue+offset)>255)?blue-offset:blue+offset;
	} else if (green>average([red, blue])) {
		green	=((green+offset)>255)?green-offset:green+offset;
	} else if (red>average([blue, green])) {
		red		=((red+offset)>255)?red-offset:red+offset;
	} else {
		blue	=((blue+offset)>255)?blue-offset:blue+offset;
		green	=((green+offset)>255)?green-offset:green+offset;
		red		=((red+offset)>255)?red-offset:red+offset;
	}
	/*document.writeln(red+"|"+green+"|"+blue);*/
	return dechex(red)+dechex(green)+dechex(blue);
	/*return sprintf("%02x", "$red").sprintf("%02x", "$green").sprintf("%02x", "$blue");*/
}
/* document.writeln(offsetColor("#000000", 25)); document.writeln(offsetColor("#000000", 40));*/

function onLoadCSS() {
	if(0==cssBackgroundImage.lenght) {
		getBackgroundImage();
	}
}

function getBackgroundImage() {
	cssBackgroundImage=getcss('body', 'backgroundImage');
}
function setBackgroundImage(str) {
	cssBackgroundImage=str;
}

function updateCSS(selector, value) {
	/*text form edits push a null value, app edits push a 6 digit hex*/
	if(value==null) {
		value=document.getElementById(selector).value;
		checked=document.getElementById(selector).checked;
	}else{
		document.getElementById(selector).value=value;
	}
	var offset0 = offsetColor(value, 25);
	var offset1 = offsetColor(value, 40);
	var offset2 = offsetColor(value, 60);
	switch(selector) {
		case 'bodyColor':
			changecss('body','backgroundColor','#'+value);
			//changecss('input.textarea','backgroundColor','#'+value);
			changecss('textarea','backgroundColor','#'+value);
			changecss('input','backgroundColor','#'+value);
			changecss('select','backgroundColor','#'+value);
			changecss('.colorgrid0', 'backgroundColor', '#'+offset0);
			changecss('.colorgrid1', 'backgroundColor', '#'+offset1);
			changecss('.bigtitle', 'backgroundColor', '#'+offset0);
			changecss('.title', 'backgroundColor', '#'+offset1);
			changecss('.smalltitle', 'backgroundColor', "#"+offset1);
			changecss('#menu, #rightmenu', 'backgroundColor', '#'+value);
			changecss('#content', 'backgroundColor', '#'+value);
		break;
		case 'bodyImgIO':
			if(checked==false){
				getBackgroundImage();
				changecss('body', 'backgroundImage', 'none');
			} else {
				changecss('body', 'backgroundImage', cssBackgroundImage);
			}
		break;
		case 'bodyImgPos':
			changecss('body', 'backgroundPosition', value);
		break;
		case 'bodyImgRep':
			changecss('body', 'backgroundRepeat', value);
		break;
		case 'bodyImgAtt':
			changecss('body', 'backgroundAttachment', value);
		break;
		case 'bodyText':
			changecss('body','color','#'+value);
			changecss('input','color','#'+value);
			changecss('select','color','#'+value);
			changecss('textarea','color','#'+value);
		break;
		case 'bannerColor':
			changecss('#banner','backgroundColor','#'+value);
			changecss('#pagefoot','backgroundColor','#'+value);
		break;
		case 'bannerText':
			changecss('#banner','color','#'+value);
			changecss('#pagefoot','color','#'+value);
		break;
		case 'linkText':
			changecss('a','color','#'+value);
		break;
		case 'linkHoverText':
			changecss('a:hover','color','#'+value);
		break;
		case 'menuColor':
			changecss('#menu dd','backgroundColor','#'+value);
			changecss('#menu dd','borderBottomColor','#'+offset0);
			/**/
			changecss('#menu dt','backgroundColor','#'+offset0);
			changecss('#menu dt','borderBottomColor','#'+offset0);
			changecss('#menu dd a:hover','backgroundColor','#'+offset1);
			changecss('#menu dd.selected','backgroundColor','#'+offset0);
		break;
		case 'menuText':
			changecss('#menu dd','color','#'+value);
			changecss('#menu dd a','color','#'+value);
			/**/
			changecss('#menu dt','color','#'+offset0);
			changecss('#menu dd a:hover','color','#'+offset1);
			changecss('#menu dd.selected','color','#'+offset1);
		break;
		case 'menuHeadColor':
			//changecss('#menu dt','backgroundColor','#'+value);
		break;
		case 'menuHeadText':
			//changecss('#menu dt','color','#'+value);
		break;
		case 'menuLinkHoverColor':
			//changecss('#menu dd a:hover','backgroundColor','#'+value);
		break;
		case 'menuLinkHoverText':
			//changecss('#menu dd a:hover','color','#'+value);
		break;
		case 'menuSelectedColor':
			//changecss('#menu dd.selected','backgroundColor','#'+value);
		break;
		case 'menuSelectedText':
			//changecss('#menu dd.selected','color','#'+value);
		break;
	}
}
function loadColorPicker(object) {
	var selector=object.id; 
	$(document.getElementById(selector)).ColorPicker({eventName:'click',
						color: document.getElementById(selector).value, 
						onChange: function (hsb, hex, rgb){updateCSS(selector,hex);},
						}) 
	.bind('keyup', function(){
							$(document.getElementById(selector)).ColorPickerSetColor(document.getElementById(selector).value);
							});
}

function cutHex(h) {return (h.charAt(0)=="#") ? h.substring(1,7):h;}
function hexToR(h) {return parseInt((cutHex(h)).substring(0,2),16);}
function hexToG(h) {return parseInt((cutHex(h)).substring(2,4),16);}
function hexToB(h) {return parseInt((cutHex(h)).substring(4,6),16);}
function base255Limit(number) { return (number>255) ? number-255:number;}
function hexDifference(hex1,hex2) { return base255Limit( (Math.abs(hex1-hex2)>60) ? hex1:hex1+60);}
function toHex(N) {
	if (N==null) return "00";
	N=parseInt(N); if (N==0 || isNaN(N)) return "00";
	N=Math.max(0,N); N=Math.min(N,255); N=Math.round(N);
	return "0123456789ABCDEF".charAt((N-N%16)/16) + "0123456789ABCDEF".charAt(N%16);
}
function rgbToHex(R,G,B) {return toHex(R)+toHex(G)+toHex(B)}


function stripCharacter(words,character) {
	var spaces = words.length;
	for(var x = 1; x<spaces; ++x){
		words = words.replace(character, "");
	}
	return words;
}

function getcss(theClass, element) {
	var cssRules;
	var added = false;
	for (var S = 0; S < document.styleSheets.length; S++){
		if (document.styleSheets[S]['rules']) {
			cssRules = 'rules';
		} else if (document.styleSheets[S]['cssRules']) {
			cssRules = 'cssRules';
		} else {
			/*no rules found... browser unknown*/
		}
		for (var R = 0; R < document.styleSheets[S][cssRules].length-1; R++) {
			if (document.styleSheets[S][cssRules][R].selectorText.toUpperCase() == theClass.toUpperCase()) { //for some reason JS makes some of the CSS selectorText all upper case, this fixes that.
				if(document.styleSheets[S][cssRules][R].style[element]){
					return document.styleSheets[S][cssRules][R].style[element];
				}
			}
		}
	}
}
function changecss(theClass,element,value) {
	var cssRules;
	var added = false;
	for (var S = 0; S < document.styleSheets.length; S++){
		if (document.styleSheets[S]['rules']) {
			cssRules = 'rules';
		} else if (document.styleSheets[S]['cssRules']) {
			cssRules = 'cssRules';
		} else {
			/*no rules found... browser unknown*/
		}
		for (var R = 0; R < document.styleSheets[S][cssRules].length-1; R++) {
			/*for some reason JS makes some of the CSS selectorText all upper case, this fixes that.*/
			if (document.styleSheets[S][cssRules][R].selectorText.toUpperCase() == theClass.toUpperCase()) {
				if(document.styleSheets[S][cssRules][R].style[element]){
					document.styleSheets[S][cssRules][R].style[element] = value;
					added=true;
					break;
				}
			}
		}
		if(!added){
			if(document.styleSheets[S].insertRule){
				document.styleSheets[S].insertRule(theClass+' { '+element+': '+value+'; }',document.styleSheets[S][cssRules].length);
			} else if (document.styleSheets[S].addRule) {
				document.styleSheets[S].addRule(theClass,element+': '+value+';');
			}
		}
	}
}


function limitcss(theClass,element,value,element2,formLink) {
	if (value.length==7) {
		var cssRules;
		for (var S = 0; S < document.styleSheets.length; S++){
			if (document.styleSheets[S]['rules']) {
				cssRules = 'rules';
			} else if (document.styleSheets[S]['cssRules']) {
				cssRules = 'cssRules';
			} else {
				/*no rules found... browser unknown*/
			}
		
			for (var R = 0; R < document.styleSheets[S][cssRules].length; R++) {
				if (document.styleSheets[S][cssRules][R].selectorText == theClass) {
					if(document.styleSheets[S][cssRules][R].style[element] && document.styleSheets[S][cssRules][R].style[element2]) {
						var e1ColorR=hexToR(value);
						var e1ColorB=hexToB(value);
						var e1ColorG=hexToG(value);
						
						var e2ColorR=hexToR(document.styleSheets[S][cssRules][R].style[element2]);
						var e2ColorB=hexToB(document.styleSheets[S][cssRules][R].style[element2]);
						var e2ColorG=hexToG(document.styleSheets[S][cssRules][R].style[element2]);
						
						e1ColorR=hexDifference(e1ColorR, e2ColorR);
						e1ColorB=hexDifference(e1ColorB, e2ColorB);
						e1ColorG=hexDifference(e1ColorG, e2ColorG);
						var newColor=rgbToHex(e1ColorR, e1ColorG, e1ColorB);
	
						var x=document.getElementById("styleform");
						x.elements[formLink.name].value=newColor;
						changecss(theClass,element,"#"+newColor);
					}
				}
			}
		}
	} else {
		changecss(theClass,element,value);
	}
}

function checkUncheckAll(theElement) {
	var theForm = theElement.form, z = 0;
	for(z=0; z<theForm.length;z++){
		if(theForm[z].type == 'checkbox' && theForm[z].name != 'checkall'){
			theForm[z].checked = theElement.checked;
		}
	}
}

function checkUncheckSome(controller,theElements) {
	var formElements = theElements.split(',');
	var theController = document.getElementById(controller);
	for(var z=0; z<formElements.length;z++){
		theItem = document.getElementById(formElements[z]);
		if(theItem.type){
			if (theItem.type=='checkbox') {
				theItem.checked=theController.checked;
			}
		} else {
			theInputs = theItem.getElementsByTagName('input');
			for(var y=0; y<theInputs.length; y++){
				if(theInputs[y].type == 'checkbox' && theInputs[y].id != theController.id){
					theInputs[y].checked = theController.checked;
				}
			}
		}
	}
}

function changeImgSize(objectId,newWidth,newHeight) {
	imgString = 'theImg = document.getElementById("'+objectId+'")';
	eval(imgString);
	oldWidth = theImg.width;
	oldHeight = theImg.height;
	if(newWidth>0){
		theImg.width = newWidth;
	}
	if(newHeight>0){
		theImg.height = newHeight;
	}
}

function changeColor(theObj,newColor){
	eval('var theObject = document.getElementById("'+theObj+'")');
	if(theObject.style.backgroundColor==null){theBG='white';}else{theBG=theObject.style.backgroundColor;}
	if(theObject.style.color==null){theColor='black';}else{theColor=theObject.style.color;}
	/*alert(theObject.style.color+' '+theObject.style.backgroundColor);*/
	switch(theColor){
		case newColor:
		switch(theBG){
			case 'white':
			theObject.style.color = 'black';
			break;
			case 'black':
			theObject.style.color = 'white';
			break;
			default:
			theObject.style.color = 'black';
			break;
		}
		break;
		default:
		theObject.style.color = newColor;
		break;
	}
}

