//--home.customise_content.js--
// COLOR WHEEL //
var colorDialogField;
var mouseX, mouseY;
document.onmousemove = getMousePosition;
	
function getMousePosition(e) 
{
    try {
        if (!e) {e = window.event};
        if (e.pageX) {
            mouseX = e.pageX;
        } else if (e.clientX) {
            mouseX = e.clientX + (document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft);
        } else 	{
            mouseX = null;
        }
        if (e.pageY) {
            mouseY = e.pageY;
        } else if (e.clientY) {
            mouseY = e.clientY + (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop);
        } else {
            mouseY = null;
        }
    } catch(e) {
        // This is here so that the page doesn't throw errors when there is no
        // document.  That seems to happen in IE when the mouse is moving while the
        // page is loading.
    }
}
	
function openColorDialog(f, e) {
    colorDialogField = f;	
    // Show dialog
    var d = document.getElementById('colorPickerDialog');
    if (d != null) {
        d.style.top = mouseY+"px";
        d.style.left = mouseX+"px";
        d.style.visibility = "visible";
        // This "shim" is here to keep windowed elements like <select>
        // boxes from poking holes in the colorPickerDialog <div>.
        var s = document.getElementById('colorPickerShim');
        if (s != null) {
            s.style.top = mouseY+"px";
            s.style.left = mouseX+"px";
            s.style.visibility = "visible";
        }
    }
    // Give control of the mouse position to the color dialog.
    document.onmousemove = getDialogMousePosition;
}
	
function closeColorDialog(f) {
    // Hide dialog
    var d = document.getElementById('colorPickerDialog');
    if (d != null) {
        d.style.visibility = "hidden";
                    // Hide the shim
        var s = document.getElementById('colorPickerShim');
        if (s != null) {
            s.style.visibility = "hidden";
        }
    }
    try {
        if (colorDialogCallback != undefined) {
            eval(colorDialogCallback);
        }
    } catch(ex) {}
    // Reclaim control of the mouse position back to the main
    // window so that the next dialog can be positioned.
    document.onmousemove = getMousePosition;
}
	
function setColor(c) {
    var s = document.getElementById(colorDialogField+'ColorSwatch');
    var h = document.getElementById(colorDialogField);
    if (s != null && h != null) {
        s.style.backgroundColor = c;
        h.value = c;
    }
    closeColorDialog();
}

/************************************************************/
// This code was adapted from the fantastic 4096 Color Wheel
// by Jemima Pereira (http://www.ficml.org/jemimap/style/color/wheel.html)
// Modified by John Watson for use as a popup.
threec = new Array("#666666", "#555555", "#545657"); // the three colors
// HSV conversion algorithm adapted from easyrgb.com
function hsv2rgb(Hdeg,S,V) {
    H = Hdeg/360;     // convert from degrees to 0 to 1
    if (S==0) {       // HSV values = From 0 to 1
        R = V*255;     // RGB results = From 0 to 255
        G = V*255;
    B = V*255;}
    else {
        var_h = H*6;
        var_i = Math.floor( var_h );     //Or ... var_i = floor( var_h )
        var_1 = V*(1-S);
        var_2 = V*(1-S*(var_h-var_i));
        var_3 = V*(1-S*(1-(var_h-var_i)));
        if (var_i==0)      {var_r=V ;    var_g=var_3; var_b=var_1}
        else if (var_i==1) {var_r=var_2; var_g=V;     var_b=var_1}
        else if (var_i==2) {var_r=var_1; var_g=V;     var_b=var_3}
        else if (var_i==3) {var_r=var_1; var_g=var_2; var_b=V}
        else if (var_i==4) {var_r=var_3; var_g=var_1; var_b=V}
        else               {var_r=V;     var_g=var_1; var_b=var_2}
        R = Math.round(var_r*255);   //RGB results = From 0 to 255
        G = Math.round(var_g*255);
        B = Math.round(var_b*255);
    }
    return new Array(R,G,B);
}

function rgb2hex(rgbary) {
    cary = new Array; 
    cary[3] = "#";
    for (i=0; i < 3; i++) {
        cary[i] = parseInt(rgbary[i]).toString(16);
        if (cary[i].length < 2) cary[i] = "0"+ cary[i];
        cary[3] = cary[3] + cary[i];
        cary[i+4] = rgbary[i]; //save dec values for later
    }
    // function returns hex color as an array of three two-digit strings
    // plus the full hex color and original decimal values
    return cary;
}

function webRounder(c,d) {//d is the divisor
    //safe divisor is 51, smart divisor is 17 
    thec = "#";
    for (i=0; i<3; i++) {
        num = Math.round(c[i+4]/d) * d; //use saved rgb value
        numc = num.toString(16);
        if (String(numc).length < 2) numc = "0" + numc;
        thec += numc;
    }
    return thec;
}

function hexColorArray(c) { //now takes string hex value with #
    threec[2] = c[3];
    threec[1] = webRounder(c,17);
    threec[0] = webRounder(c,51);
    return false;
}

function greyMoved(x, y) {
    if (x < 10) {
        // Pure black
        c = rgb2hex(hsv2rgb(0, 0, 0));
    } else if (x > 140) {
        // Pure white
        c = rgb2hex(hsv2rgb(0, 0, 1));
    } else {
        // Shades of grey
        c = rgb2hex(hsv2rgb(0, 0, (x-10)/130));
    }
    hexColorArray(c);
    hoverColor();
}

function getDialogMousePosition(e) {
    if (document.layers) {
        x = e.layerX;
        y = e.layerY;
    }
    else if (document.all) {
        x = event.offsetX;
        y = event.offsetY;
    }
    else if (document.getElementById) {
        x = (e.pageX - document.getElementById("colorPickerDialog").offsetLeft);
        y = (e.pageY - document.getElementById("colorPickerDialog").offsetTop);
    }
    if (x > 150) { return false; }
    if (y > 150) { greyMoved(x, y); return false; }
    cartx = x - 75;
    carty = 75 - y;
    cartx2 = cartx * cartx;
    carty2 = carty * carty;
    cartxs = (cartx < 0)?-1:1;
    cartys = (carty < 0)?-1:1;
    cartxn = cartx/75;                      //normalize x
    rraw = Math.sqrt(cartx2 + carty2);       //raw radius
    rnorm = rraw/75;                        //normalized radius
    if (rraw == 0) {
        sat = 0;
        val = 0;
        rgb = new Array(0,0,0);
    }
    else {
        arad = Math.acos(cartx/rraw);            //angle in radians 
        aradc = (carty>=0)?arad:2*Math.PI - arad;  //correct below axis
        adeg = 360 * aradc/(2*Math.PI);  //convert to degrees
        if (rnorm > 1) {    // outside circle
            rgb = new Array(255,255,255);
            sat = 1;
            val = 1;
        }
        //else rgb = hsv2rgb(adeg,1,1);
        else if (rnorm >= .5) {
            sat = 1 - ((rnorm - .5) *2);
            val = 1;
            rgb = hsv2rgb(adeg,sat,val);
        }
        else {
            sat = 1;
            val = rnorm * 2;
        rgb = hsv2rgb(adeg,sat,val);}
    }
    c = rgb2hex(rgb);
    hexColorArray(c);
    hoverColor();
    return false;
}

function hoverColor() {
    var e = document.getElementById("colorPickerDialog");
    e.style.backgroundColor = threec[2];
    return false;
}

function pickColor() {
    setColor(threec[2]);
    return false;
}

//
////////////////////////
//

function SelectTab(tab) 
{
	for(var i = 0;i <= 5;i++) 
	{
		if ($('tabselect_'+i) == null) 
		{
			break;
		}
		if (i == tab)
		{
			$('#tabselect_'+tab).show();
			$('#tabselect_'+tab+'_tab').css('background-color','#f2f7c1');
			$('#tabselect_'+tab+'_tab').css('border-bottom','#e7580d  5px solid');
			$('#tabselect_'+tab+'_tab').css('padding','5px 5px 3px 5px');
			$('#tabselected').val(tab);
		}
		else
		{
			$('#tabselect_'+i).hide();
			$('#tabselect_'+i+'_tab').css('background-color','silver');
			$('#tabselect_'+i+'_tab').css('border-bottom','#e7580d 0px solid');
			$('#tabselect_'+i+'_tab').css('padding','5px 5px 5px 5px');
		}
	}
}

function selectColor(field) {
	var dialog = document.getElementById('colorPicker_'+field);
	if (dialog != null) {
		dialog.style.visibility = "visible";
	}
}

function setLayout() {
	var e = document.getElementById("orientation1");
	var sample = document.getElementById("sample");
	if (e.checked) {
		sample.src = base_url + 'media/images/landscape.png';
	} else {
		sample.src = base_url + 'media/images/portrait.png';
	}
}
function gotoFlickrPage(page)
{
	$.get(site_url + 'customize/ajax_flickr_photos/'+page, function(data){
		$('#myflickr').html(data);
		$('#photoselected').val(0);
	});
	return true;
}

function selectFlickrPhotoDiv(photo, i)
{
	$('#photoselected').val(photo);
	$('#tabselect_message').text('Selected #' +i);
	$('[id^=flickr_photo_div_]').css('background','');	
	$('#flickr_photo_div_'+i).css('background','gainsboro');
}
function selectFlickrPhoto(el, i)
{
	$('#photoselected').val(el.value);
	$('#tabselect_message').text('Selected #' +i);
	$('[id^=flickr_photo_div_]').css('background','');
		
	$('#flickr_photo_div_'+i).css('background','gainsburo');
}
function selectPhotoDiv(photo, i)
{
	$('#photoselected').val(photo);
	$('#tabselect_message').text('Selected upload #' +i);
	$('[id^=photo_div_]').css('background','');	
	$('#photo_div_'+i).css('background','gainsboro');
}
function selectPhoto(el, i)
{
	$('#photoselected').val(el.value);
	$('#tabselect_message').text('Selected upload #' +i);
	$('[id^=photo_div_]').css('background','');	
	$('#photo_div_'+i).css('background','gainsburo');
}
function doDelete(i)
{
	$.get(site_url + 'customize/ajax_deleteupload/'+i, function(data){
		$('#myuploads').html(data);
		$('#photoselected').val(0);
	});
	return true;
}

