﻿<!--
////// COOKIES //////
    function createCookie(name,value,days) {
        var expires;
    	if (days) {
    		var cookieDate = new Date();
    		cookieDate.setTime(cookieDate.getTime()+(days*24*60*60*1000));
    		expires = "; expires="+cookieDate.toGMTString();
    	} else {
    	    expires = "";
    	}
        document.cookie = name+"="+value+expires+"; path=/";
    }
    
    function readCookie(name) {
    	var nameEQ = name + "=";
    	var ca = document.cookie.split(';');
    	for(var i=0;i < ca.length;i++) {
    		var c = ca[i];
    		while (c.charAt(0)==' ') {
    		    c = c.substring(1,c.length);
    		}
    		if (c.indexOf(nameEQ) === 0) {
    		    return c.substring(nameEQ.length,c.length);
    		}
    	}
    	return null;
    }
    
    function eraseCookie(name) {
    	createCookie(name,"",-1);
    }

////// THEME //////

    // checkVersion is called from here to prevent the flicker between pages on
    // the cg version which was happening when it was called from onload
    checkVersion();

    function checkVersion() {
        var version = readCookie('theme');
        if (version == 'cg') {
            swapTheme(version);
        }
    }

    function swapTheme(version) {
       var i, a, main;
       for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    	 if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
    	   if(a.getAttribute("title") == 'theme') {
    	     if(a.href.indexOf('/css/'+version+'.css')<0) {
    	        //trace('Swapping theme to', version);
           	   a.href='/css/'+version+'.css';
           	   createCookie('theme',version,2);
    	     }
	        swapImages(version);
    	   }
    	 }
       }
    }
    
    function lozengeRollover(areaIndex) {
        var theImage = document.getElementById('lozengeImage');
        if (theImage) {
            var testImage = new Image();
            if (areaIndex>=0) {
                var newSrc = theImage.src.replace('.cg.gif', '_' + areaIndex + '.cg.gif');
                newSrc = newSrc.replace('.sk.gif', '_' + areaIndex + '.sk.gif');
                testImage.src = newSrc;
            }
            else {
                var _src = theImage.src;
                //replace an underscore and a 1 or 2 digit number and a dot with just a dot
                testImage.src = _src.replace(/_[0-9]{1,2}\./gi, ".");
            }
            theImage.src = testImage.src;
        }
    }
    
    function swapImages(version) {
        //trace('Swapping images to',version);
        for (x in document.images) {
            if (document.images[x].src) {
                document.images[x].src = document.images[x].src.replace('.'+getOtherTheme(version)+'.','.'+version+'.');
                if (document.images[x].name == 'miscImg') {
                    swapImgType(document.images[x], version);
                }
            }
        }
    }
    
    function swapImgType(img, version) {
        // 'miscImg' is now a gif for skecthed versions and jpeg for normal
        if (version == 'cg') {
            document.images[x].src = document.images[x].src = document.images[x].src.replace('.gif','.jpg');
        }
        else {
            document.images[x].src = document.images[x].src = document.images[x].src.replace('.jpg','.gif');
        }
    }

    function getOtherTheme(thisTheme) {
        if (thisTheme == 'cg') {
            return 'sk'
        }
        else {
            return 'cg'
        }
    }
    
    function hideVersionSelector() {
        var vsElement = document.getElementById('VersionSelector');
        if (vsElement) {
            vsElement.style.backgroundPosition = '-1000px -400px';
        }
    }
    
    function showVersionSelector() {
        var vsElement = document.getElementById('VersionSelector');
        if (vsElement) {
            vsElement.style.backgroundPosition = '735px 17px';
        }
    }
     
////// Portfolio //////
    function portfolioPreview(img, mainImgId) {
        if (img) {
            var mainImg = document.getElementById(mainImgId);
            if (mainImg) {
                mainImg.src = img.src.replace(".th.",".fs.");
            }
        }
    }

////// Zoomer //////
    function setZoom(img, dir, width, height, tp, lp, zIndex, delay) {
      setTimeout(function() {
        if (img.dir==dir) {
          img.style.width=width+'px';
          img.style.height=height+'px';
          img.style.top=tp+'px';
          img.style.left=lp+'px';
          img.style.margin = '0 0 '+ tp*2 +'px 0';
          img.style.zIndex=zIndex;
          img.parentNode.parentNode.style.zIndex=zIndex;
        }
      }, delay);
    }

    function larger(img, width, height) {
        if (readCookie('theme') == 'cg') {
          var heightInc = (height - getPixels(img.style.height))/10;
          var widthInc = (width - getPixels(img.style.width))/10;
         
          img.dir='rtl';
          now=parseInt(img.style.zIndex);
          for (i=now+1; i<=10; i++) {
            w=width-((10-i)*widthInc);
            h=height-((10-i)*heightInc);
            tp=((0-i)*heightInc)/2;
            lp=((0-i)*widthInc)/2;
            setZoom(img, 'rtl', w, h, tp, lp, i, 20*(i-now));
          }
        }
    }

    function smaller(img, width, height) {
        if (readCookie('theme') == 'cg') {
          var heightInc = (getPixels(img.style.height) - height)/10;
          var widthInc = (getPixels(img.style.width) - width)/10;

          img.dir='ltr';
          now=parseInt(img.style.zIndex);
          for (i=now-1; i>=0; i--) {
            w=width+((i+1)*widthInc);
            h=height+((i+1)*heightInc);
            tp=((0-i)*heightInc)/2;
            lp=((0-i)*widthInc)/2;
            setZoom(img, 'ltr', w, h, tp, lp, i, 20*(now-i));
          }
        }
    }
    
    function getPixels(val) {
        var pat = new RegExp("[0-9][0-9]{0,2}"); //finds 1-3 numbers
        return parseInt(pat.exec(val));
    }

    function changeButton(controlID, text, cssClass) {
        var control = document.getElementById(controlID);
        if (control) {
            control.value = text;
            control.title = text;
            control.className = 'btn ' + cssClass;
        }
    }

/// Misc ////

    function preLoad(src) {
        var preLoad = new Image();
        preLoad.src = src;
    }
    
    function NewWindow(mypage, myname, w, h, scroll) {
    var winl = (screen.width - w) / 2;
    var wint = (screen.height - h) / 2;
    winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars='+scroll+',resizable'
    win = window.open(mypage, myname, winprops)
    if (parseInt(navigator.appVersion) >= 4) { win.window.focus(); }
    }
    
//// DEBUG ////

function trace(check, value) {
    var tracer = document.getElementById('trace');
    if (tracer) {
        if (value) {
            tracer.innerHTML = tracer.innerHTML + check + ' = ' + value + '<br>';
        }
        else {
            tracer.innerHTML = tracer.innerHTML + check + '<br>';
        }
    }
}

-->