﻿

function createGraphicText(txt,size,folder, /*optional*/ namediv) //returns a div element with images inside
{
           
			var wofimage = 0;
			var allImgsLoaded=true;
			var newdiv  = false;
            foldername = 'images/model/fonts/';
            textlength = 150;
            if(folder=='m')
            {
                foldername = 'images/model/fonts/';
                textlength = 150;
            }
            if (folder == 'mg') {
                foldername = 'images/model/modelgoldfonts/';
                textlength = 150;
            } 
			if (folder == 'john-carroll') {
                foldername = 'images/shirt/john-carroll-fonts/';
                textlength = 150;
            } 			
            if(folder=='h')
            {
                foldername = 'images/shirt/fonts/';
                textlength = 325;    
            }
            if(folder=='s')
            {
                foldername = 'images/shirt/fonts/';
                textlength = 395;
            }
            if (folder == 'g') {
                foldername = 'images/shirt/goldfonts/';
                textlength = 395;
            }
            if(folder=='p')
            {
                foldername = 'images/preview/fonts/';
                textlength = 595;    
            }
            if (namediv == null ) 
            {
	            namediv = document.createElement("DIV");
	            newdiv = true;
            }
            
            document.getElementById(namediv).innerHTML = '';
            var images = new Array();
            var imagedir = foldername+size+'/'; // + font.substr(0, 1)+"/";
            for ( var i = 0; i < txt.length; i++ ) 
            {
	            var z = txt.substr(i, 1); // gets the current character
	            var img = document.createElement("IMG"); // creates an image object
	            var src = imagedir; // a temporary variable to store the source
	            
	            if (z == " ") 
			    {
				    src += "special/space.png";
			    }
			    else
			    {
			        if (z == "'") 
			            {
				             src += "special/tick.png";
			            }
			            else
			            {
                            if(z == "-") 
                                {
                                    src += "special/dash.png";
                                }
                                else
                                {
                                 if(z == "/") 
                                    {
                                        src += "special/slash.png";
                                    }
                                    else
                                    {
					                    src += (isUpperCase(z) ? "upper" : "lower") + "/" + z + ".png";
					                }
                                }
                        }
                }
                img.src = replaceSpecialChar(src); 
                // set the temporary variable "src" to the "src" attribute of the image object
                //wofimage = wofimage + img.width;
				// added to help with the images being slow
				var imagestring;
				if( img.width<5)
				{
				    if(size=='L'){wofimage = wofimage + 65;imagestring = imagestring + ' ' + '39';}
				    if(size=='M'){wofimage = wofimage + 47;imagestring = imagestring + ' ' + '32';}
				    if(size=='S'){wofimage = wofimage + 25;imagestring = imagestring + ' ' + '5';}
				}
				else
				{
                    wofimage = wofimage + img.width;imagestring = imagestring + '-' + img.width;;
				}
				document.getElementById(namediv).appendChild(img); 
	        } 
	        
		if(wofimage<textlength)
		 {
		    return namediv;
		 }
		 else
		 {
            if(size=='L')
            {
                return namediv = createGraphicText(txt,'M',folder,namediv);
            }
            if(size=='M')
            {
                return namediv = createGraphicText(txt,'S',folder,namediv);
            }
         }      
       }
       
       function createTextGraphics(txt,size,folder, /*optional*/ namediv,textlength) //returns a div element with images inside
       {
           //this function is rewrite of previous function, but is folder agnostic.
           //textlength is set in parameter as well
			var wofimage = 0;
			var allImgsLoaded=true;
			var newdiv  = false;
            //foldername = 'images/model/fonts/';
            //var textlength = 395;
            //if(folder=='m')
            //{
                //foldername = 'images/model/fonts/';
               // textlength = 150;
            //}
            //if(folder=='h')
            //{
                //foldername = 'images/shirt/fonts/';
                //textlength = 325;    
            //}
            //if(folder=='s')
            //{
                //foldername = 'images/guildiconbuilder/font/';
                //textlength = 395;    
            //}
            //if(folder=='p')
            //{
                //foldername = 'images/preview/fonts/';
                //textlength = 595;    
            //}
            if (namediv == null ) 
            {
	            namediv = document.createElement("DIV");
	            newdiv = true;
            }
            
            document.getElementById(namediv).innerHTML = '';
            var images = new Array();
            var imagedir = folder;//+size+'/'; // + font.substr(0, 1)+"/";
            for ( var i = 0; i < txt.length; i++ ) 
            {
	            var z = txt.substr(i, 1); // gets the current character
	            var img = document.createElement("IMG"); // creates an image object
	            var src = imagedir; // a temporary variable to store the source
	            
	            if (z == " ") 
			    {
				    src += "special/" + (isUpperCase(z) ? "upper" : "lower") + "/" + "space.png";
			    }
			    else
			    {
			        if (z == "'") 
			            {
				             src += "special/" + (isUpperCase(z) ? "upper" : "lower") + "/" + "tick.png";
			            }
			            else
			            {
                            if(z == "-") 
                                {
                                    src += "special/" + (isUpperCase(z) ? "upper" : "lower") + "/" + "dash.png";
                                }
                                else
                                {
                                 if(z == "/") 
                                    {
                                        src += "special/" + (isUpperCase(z) ? "upper" : "lower") + "/" + "slash.png";
                                    }
                                    else
                                    {
                                     if(z == "*") 
                                        {
                                           src += "special/" + (isUpperCase(z) ? "upper" : "lower") + "/" + "racespace.png";
                                          }
                                        else
                                        {
					                        src += (isUpperCase(z) ? "upper" : "lower") + "/" + z + ".png";
					                    }
					                 }
                                }
                        }
                }
                img.src = replaceSpecialChar(src); 
                // set the temporary variable "src" to the "src" attribute of the image object
                //wofimage = wofimage + img.width;
				// added to help with the images being slow
				var imagestring;
				if( img.width<5)
				{
				    if(size=='L'){wofimage = wofimage + 65;imagestring = imagestring + ' ' + '39';}
				    if(size=='M'){wofimage = wofimage + 47;imagestring = imagestring + ' ' + '32';}
				    if(size=='S'){wofimage = wofimage + 25;imagestring = imagestring + ' ' + '5';}
				}
				else
				{
                    wofimage = wofimage + img.width;imagestring = imagestring + '-' + img.width;;
				}
				document.getElementById(namediv).appendChild(img); 
	        } 
	        
		if(wofimage<textlength)
		 {
		    return namediv;
		 }
		 else
		 {
            if(size=='L')
            {
                return namediv = createTextGraphics(txt,'M',folder,namediv,textlength);
            }
            if(size=='M')
            {
                return namediv = createTextGraphics(txt,'S',folder,namediv,textlength);
            }
         }      
       }
       
    function isUpperCase(alpha)
    {
        //return alpha >= "A" && alpha <= "Z";
		//srp changed code to support unicode chars
        var code = alpha.charCodeAt(0);
        var code2 = alpha.toLowerCase();
        var code3 = code2.charCodeAt(0);
        if(code == code3)
        {
            return false;
        }
        else
        {
            return true;
        }
    }
    
    function replaceSpecialChar(src)
    {
        var arr=src.split('/');
        var chr = arr[arr.length-1].split('.')[0];

        if(chr == '&' )
        {
            return src.replace(chr,'{amp;}');
        }
        else if(chr == '#' )
        {
            return src.replace(chr,'{hash;}');
        }
        else if(chr == '%' )
        {
            return src.replace(chr,'{per;}');
        }
        return src;
    }
    
    function myShirtFunction(inSite) 
    { 
        //alert( inSite +  "17=" + myShirt17() + "&18=" + myShirt18() + "&19=" + myShirt19() +"&20=" +  myShirt20() + "&21=" + myShirt21() + "&22=" + myShirt22()  + "&23=" +  myShirt23() + "&24=" + myShirt24() + "&25=" + myShirt25() + "&26=" + myShirt26() + "&57=" + myShirt57() + "&61=" + myShirt61());
        document.location.href = inSite +  "17=" + myShirt17() + "&18=" + myShirt18() + "&19=" + myShirt19() +"&20=" +  myShirt20() + "&21=" + myShirt21() + "&22=" + myShirt22()  + "&23=" +  myShirt23() + "&24=" + myShirt24() + "&25=" + myShirt25() + "&26=" + myShirt26() + "&57=" + myShirt57() + "&61=" + myShirt61() + "&stl=" + myShirtStyle();
     }
	function returnMyShirtFunction(inSite) 
    { 
        return inSite +  "17=" + myShirt17() + "&18=" + myShirt18() + "&19=" + myShirt19() +"&20=" +  myShirt20() + "&21=" + myShirt21() + "&22=" + myShirt22()  + "&23=" +  myShirt23() + "&24=" + myShirt24() + "&25=" + myShirt25() + "&26=" + myShirt26() + "&57=" + myShirt57() + "&61=" + myShirt61();
    }
	function preview_openBrWindow(theURL) 
	{
	//theURL = theURL + returnMyShirtFunction(theURL);
	//Change by BORY to:
	//srp has to be urlencoded for utf-8
	//theURL = returnMyShirtFunction(theURL);
	theURL = encodeURI(returnMyShirtFunction(theURL));
	//theURL = returnMyShirtFunction(theURL);
	window.open(theURL,'designpreview','scrollbars=yes,resizable=yes,width=620,height=700');
	}
	 
     function myShirt17() {  try{return Ecode(getHFAH());}catch(er){return '';}    }
     function myShirt18() {  try{return Ecode(getHFEmblemName());}catch(er){return '';}    }
     function myShirt19() {  try{return Ecode(getHFBorderColor());}catch(er){return '';}    }
     function myShirt20() {  try{return Ecode(getHFTabardColor());}catch(er){return '';}    }
     function myShirt21() {  try{return Ecode(getHFRaceName());}catch(er){return '';}    }
     function myShirt22() {  try{return Ecode(getHFClassName());}catch(er){return '';}    }
     function myShirt23() {  try{return Ecode(getHFGuildName());}catch(er){return '';}    }
     function myShirt24() {  try{return Ecode(getHFCharName());}catch(er){return '';}    }
     function myShirt25() {  try{return Ecode(getHFBattleName());}catch(er){return '';}    }
     function myShirt26() {  try{return Ecode(getHFRealmName());}catch(er){return '';}    }
     function myShirt57() {  try{return Ecode(getHFHatColor());}catch(er){return '';}    }
     function myShirt61() {  try{return Ecode(getHFHatSize());}catch(er){return '';}    }
     function myShirtStyle() {  try{return Ecode(getHFShirtStyle());}catch(er){return '';}    }
	 function Ecode(instring)
      {
        // var encodedInputString=escape(instring);
        // encodedInputString=encodedInputString.replace("+", "%2B");
        // encodedInputString=encodedInputString.replace("/", "%2F");        
        // return encodedInputString;
		return instring;
      }


