
// Cufon
Cufon.replace('div.content div.home-col h2', { fontFamily: 'MetaPlusMedium' });
Cufon.replace('div.content div.box h3');
Cufon.replace('ul.main-nav ul a', {	hover: true });
Cufon.replace('div.content div.main-title h1');
Cufon.replace('div.content div.main-title p');
Cufon.replace('div.content p.intro');
Cufon.replace('div.content div.tab h2');


$(document).ready(function () {
    VTCU.homePagePromo();
    VTCU.contentTabs();
    VTCU.textResize();
    VTCU.rollovers();
    VTCU.preloadImages(["/Images/btn_search_over.png",
                        "/Images/btn_download_over.png",
                        "/Images/btn_read_more_over.png"
                       ]);
    if (VTCU.detectMacXFF()) {
        $('div.navigation div.search input.text').addClass('ffmac');
    }
	 
	 
	 /* Accessible input values
   ---------------- */
     $(".navigation .tools input[type=text]").each(function(){
      var id = $(this).attr("id");
      var val = $(this).val();
      if(this.value == '') this.value = val;
      $(this)
       .focus(function(){
        if(this.value == val) this.value = '';
       }).blur(function(){
        if(this.value == '') this.value = val;
      })
     });

	if ($('.formRow textarea').length > 0) {
		var fld = $('.formRow textarea');
		fld.each(function() {
			var par = $(this).parent();
			var maxLength = par.attr("MaxLength");
			var fldLength = $(this).val().length;
			if (maxLength != null && !isNaN(maxLength)) {
				maxLength = parseInt(maxLength);
				par.find("label").append('<span class="lblMaxLength">Maximum ' + maxLength + ' Chars<span></span></span>');
				var lblCharCount = par.find("label .lblMaxLength span");
				if (fldLength > 0) {
					lblCharCount.html("(Length: " + fldLength + ")");
				}				
				$(this).keyup(function() {
					fldLength = $(this).val().length;
					if (fldLength > maxLength) {
						lblCharCount.css("color", par.find(".required").css("color"));
						lblCharCount.css("font-weight", "bold");
					}
					else {
						lblCharCount.css("color", par.find(".lblMaxLength").css("color"));
						lblCharCount.css("font-weight", "normal");
					}
					if (fldLength > 0) lblCharCount.html("(Length: " + fldLength + ")");
				});
			}
		});
	}
	  	 
});

// VTCU User interface
var VTCU = {

    /**
     * Promo on home page
     */     
    homePagePromo: function () {
        var promo = $('div.content div.promo');
        promo.find('ul.nav').show();
        if (promo.length) {
            promo.tabs({ fx: { opacity: 'toggle', duration: 600} });
            promo.tabs('rotate', 4000, true);
        }   
    },
    
    /**
     * Content tabs
     */     
    contentTabs: function () {
        var content_tabs = $('div.content div.tabs');
        if (content_tabs.length) {
            content_tabs.tabs({ fx: { opacity: 'toggle', duration: 400} });
        }   
    },    
    
    /**
     * Text resize
     */         
    textResize : function(){
        
        // show text resizing links
        var $cookie_name = "fontsize";
        var originalFontSize = $("html").css("font-size");
        var fontSizeElement = $("html");
        
        // if exists load saved value, otherwise store it
        if($.cookie($cookie_name)) {
            var $getSize = $.cookie($cookie_name);
            fontSizeElement.css({fontSize : $getSize + ($getSize.indexOf("px")!=-1 ? "" : "px")}); // IE fix for double "pxpx" error
        } else {
            $.cookie($cookie_name, originalFontSize);
        }

        // Increase font size
        $("div.tools ul.font-size a.increase").bind("click", function() {
            var currentFontSize = fontSizeElement.css("font-size");
            var currentFontSizeNum = parseFloat(currentFontSize);
            var newFontSize = currentFontSizeNum + 1;
            if (newFontSize < 20) {
                fontSizeElement.css("font-size", newFontSize);
                $.cookie($cookie_name, newFontSize);
            }
            return false;   
        });
        
        // Decrease font size
        $('div.tools ul.font-size a.decrease').bind('click', function() {
            var currentFontSize = fontSizeElement.css('font-size');
            var currentFontSizeNum = parseFloat(currentFontSize);
            var newFontSize = currentFontSizeNum - 1;
            if (newFontSize > 14) {
                fontSizeElement.css('font-size', newFontSize);
                $.cookie($cookie_name, newFontSize);
            }
            return false;
        });     
    },      
    
    /**
     * Rollovers buttons
     */
    rollovers: function () {
        $('.rollover').hover(
            function () {
                $(this).attr('src', $(this).attr('src').replace(/.png/, '_over.png'));
            },
            function () {
                $(this).attr('src', $(this).attr('src').replace(/_over.png/, '.png')); 
            }
        );
     },
     
    /**
     * Preload images
     * @param {Array} images array with names of images   
     */               
    preloadImages: function (images) {
        for (var i = 0; i < images.length; i++) {
            var image = new Image();
            image.src = images[i];
        }
    },
    
    detectMacXFF: function() {
        var userAgent = navigator.userAgent.toLowerCase();
        if (userAgent.indexOf('mac') != -1 && userAgent.indexOf('firefox')!=-1) {
            return true;
        }
    }         
}