/********************************************************
//  FILE:           fontastic.js
//  CREATED BY:     Luc Vandal
//  ON:             30/12/2007
//
//  DESCRIPTION:    Alternative to shitty sIFR
//
//  (C) 2007 EDOVIA INC. www.edovia.com
//
********************************************************/

// Globals



//
//	getStyleBySelector
//
//	
//
////////////////////////////////////////////////////////////
function getStyleBySelector( selector )
{
    var sheetList = document.styleSheets;
    var ruleList;
    var i, j;

    /* look through stylesheets in reverse order that
    they appear in the document */
    for (i=sheetList.length-1; i >= 0; i--)
    {
        if (sheetList[i].cssRules)
        {
            ruleList = sheetList[i].cssRules;
        }
        else if (sheetList[i].rules)
        {
            ruleList = sheetList[i].rules;
        }

        for (j=0; j<ruleList.length; j++)
        {
            var bIsType = true;
            
            if (sheetList[i].cssRules)
            {
                bIsType = ruleList[j].type == CSSRule.STYLE_RULE; 
            }
               
            if (ruleList[j].selectorText == selector && bIsType)
            {
                return ruleList[j].style;
            }   
        }
    }

    return null;
}


//
//	hoverItem
//
//	
//
////////////////////////////////////////////////////////////
function hoverItem(item)
{
	if (item != null)
	{
		var itemStyle       = getStyleBySelector("#"+item.id);
        var itemStyleHover  = getStyleBySelector("#"+item.id+":hover");
	
		if (itemStyleHover != null)
		{
			var alt             = urlEncode(item.alt);
            var fontFamily      = itemStyle.fontFamily;
            var backgroundColor = itemStyle.backgroundColor;
            var fontSize        = itemStyle.fontSize;
            var color           = itemStyle.color;
            var hoverColor 		= itemStyleHover.color;
            
            if (backgroundColor == "")
            {
            	backgroundColor = item.longdesc;
            }

            item.src = "/frameworks/DeeJay/includes/fontastic.php?fontText="+alt+"&fontName="+fontFamily+
                            "&backgroundColor="+backgroundColor+"&fontSize="+fontSize+
                            "&fontColor="+hoverColor;
		}
	}
}


//
//	outItem
//
//	
//
////////////////////////////////////////////////////////////
function outItem(item)
{
	if (item != null)
	{
		var itemStyle       = getStyleBySelector("#"+item.id);
        var itemStyleHover  = getStyleBySelector("#"+item.id+":hover");
	
		if (itemStyleHover != null)
		{
			var alt             = urlEncode(item.alt);
            var fontFamily      = itemStyle.fontFamily;
            var backgroundColor = itemStyle.backgroundColor;
            var fontSize        = itemStyle.fontSize;
            var color           = itemStyle.color;
            var hoverColor 		= itemStyleHover.color;
            
            if (backgroundColor == "")
            {
            	backgroundColor = item.longdesc;
            }
            
            item.src = "/frameworks/DeeJay/includes/fontastic.php?fontText="+alt+"&fontName="+fontFamily+
                            "&backgroundColor="+backgroundColor+"&fontSize="+fontSize+
                            "&fontColor="+color;
		}
	}
}


//
//	applyFontastic
//
//	
//
////////////////////////////////////////////////////////////
function applyFontastic()
{
    var items   = $$(".fontastic");
    var count   = items.length;
    var i       = 0;
    
    for (;i<count;i++)
    {
        var item            = items[i];
        var itemStyle       = getStyleBySelector("#"+item.id);
        var itemStyleHover  = getStyleBySelector("#"+item.id+":hover");

        if (item.src == "")
        {
        	var id				= item.id;
            var alt             = urlEncode(items[i].alt);
            var fontFamily      = itemStyle.fontFamily;
            var backgroundColor = itemStyle.backgroundColor;
            var fontSize        = itemStyle.fontSize;
            var color           = itemStyle.color;
            
            // Remove background color from style, we just need it to generate the text
            item.longdesc = backgroundColor;
            itemStyle.backgroundColor = "";

            items[i].src = "/frameworks/DeeJay/includes/fontastic.php?fontText="+alt+"&fontName="+fontFamily+
                            "&backgroundColor="+backgroundColor+"&fontSize="+fontSize+
                            "&fontColor="+color;

            if (itemStyleHover != null)
            {
                items[i].onmouseover = function(){
                    hoverItem(this);
                };
                
                items[i].onmouseout = function(){
                    outItem(this);
                };
            }
        }
    }
}