function toHex(n) {
 n = parseInt(n,10);
 if (isNaN(n)) return "00";
 n = Math.max(0,Math.min(n,255));
 return "0123456789ABCDEF".charAt((n-n%16)/16)
      + "0123456789ABCDEF".charAt(n%16);
}

function rgbToHex(R,G,B) {return toHex(R)+toHex(G)+toHex(B)}

var RGBtoHEX = function(color) {
    return "#"+$.map(color.match(/\b(\d+)\b/g),function(digit){
      ('0' + parseInt(digit).toString(16)).slice(-2)
    }).join('');
};


var fadeTime = 300;

function getCurrentHex()
{
    var color = $('.web').css('color').replace(/[^0-9,]{1,}/g, "").split(',');
    var colorHex = rgbToHex(color[0], color[1], color[2]);
    return colorHex;
}
function getOnHoverColor()
{
    if (getCurrentHex() == 'C6C6C6') return 'C6C6C6';
    return '595959';
}
function getOnHoverOutColor()
{
    if (getCurrentHex() == 'C6C6C6') return '595959';
    return 'C6C6C6';
}

$(document).ready(function(){
    Cufon.replace(".category p", {color: '#' + getOnHoverColor()})
    $("#close_button").click(function(){
        $("#page_content").fadeOut(fadeTime, function(){$("#main_content").fadeIn(fadeTime, function(){}); document.location = '#'});
    });
    $(".links ul li a").click(function(){
	var _url = this.getAttribute('href')[0] == '#' ? this.getAttribute('href').substr(1) : this.getAttribute('href');
        $("#loaded_page_content").load(_url); 
        $("#main_content").fadeOut(fadeTime, function(){
            $("#page_content").fadeIn(fadeTime, function(){});
            Cufon.replace("#loaded_page_content .cufon");
        });
        
//        return false;
    });
    $(".links ul li a").mouseover(function(){
        Cufon.replace(".category p", {color: '#' + getOnHoverOutColor()})
        if (this.getAttribute('rel') && this.getAttribute('rel') != null) {
            $.each(this.getAttribute('rel').split(' '), function(index, value) {
                Cufon.replace(".category p." + value, {color: '#' + getOnHoverColor()});
            });
        }
    });
    $(".links ul li a").mouseout(function(){
        Cufon.replace(".category p", {color: '#' + getOnHoverColor()})
    });
    $.stylesheetInit();
    $('#toggler').bind('click', function(e) { 
        $.stylesheetToggle();
        Cufon.replace(".category p", {color: '#' + getOnHoverColor()})
        return false; 
    });
});
