function addCommas(nStr)
{
    nStr += '';
    x = nStr.split('.');
    x1 = x[0];
    x2 = x.length > 1 ? '.' + x[1] : '';
    var rgx = /(\d+)(\d{3})/;
    while (rgx.test(x1)) {
        x1 = x1.replace(rgx, '$1' + ',' + '$2');
    }
    return x1 + x2;
}

$.fn.hoverClass = function(c) {
    return this.each(function(){
        $(this).hover( 
            function() { $(this).addClass(c); },
            function() { $(this).removeClass(c); }
        );
        
    });
};

$.fn.clickClass = function(c) {
    return this.each(function(){
        $(this).click( 
            function() { 
            	if ($(this).attr('class') == c)
    			{
    				$(this).removeClass(c);
    			}
    			else
    			{
    				$(this).addClass(c);
    			}
             }
        );
        
    });
};

var min=10;
var max=18;
function increaseFontSize() {
   var p = document.getElementsByTagName('p');
   for(i=0;i<p.length;i++) {
      if(p[i].style.fontSize) {
         var s = parseInt(p[i].style.fontSize.replace("px",""));
      } else {
         var s = 14;
      }
      if(s!=max) {
         s += 1;
      }
      p[i].style.fontSize = s+"px"
   }
}
function decreaseFontSize() {
   var p = document.getElementsByTagName('p');
   for(i=0;i<p.length;i++) {
      if(p[i].style.fontSize) {
         var s = parseInt(p[i].style.fontSize.replace("px",""));
      } else {
         var s = 12;
      }
      if(s!=min) {
         s -= 1;
      }
      p[i].style.fontSize = s+"px"
   }   
}

$(document).ready(
	
	function() 
	{
		/* KEEP CODE FROM HERE DOWN TO NEXT COMMENT BLOCK ON ALL PROJECTS */
		
		$('input#filename').keyup(function()
		{
			jQuery.ajax(
		   	{
		   		type: "GET",
		   		url: "/page/check_filename.html?filename=" + $(this).val() + '&old_filename='+$('#old_filename').val(),
		   		dataType: "html",
		   		timeout: 15000,
		   		error: function() {},
		   		success: function(r) {
		   								if (r == 'success')
		   								{
		   									$("#filename_status").attr({src: '/images/icons/accept.png'}); 
		   									if ($('input#title').val() != '') 
		   										$("#edit_submit").attr('disabled', false);
		   									$("#filename_text").html('');
		   								}
		   								else
		   								{
		   									$("#filename_status").attr({src: '/images/icons/delete.png'}); 
		   									$("#filename_text").html(r);
		   									$("#edit_submit").attr('disabled', true);
		   								}
		   							}
		   	});
		});
		
		$('input#title').keyup(function()
		{
			if ($(this).val() != '')
			{
				$("#title_status").attr({src: '/images/icons/accept.png'}); 
				if ($("#filename_status").attr('src') == '/images/icons/accept.png')
					$("#edit_submit").attr('disabled', false);
		   	}
		   	else
		   	{
		   		$("#title_status").attr({src: '/images/icons/delete.png'}); 
		   		$("#edit_submit").attr('disabled', true);
		   	}
		});
		
		$('.confirm_anchor').click(function()
		{
			return confirm('Are you SURE you want to do this???');
		});
		
		$('a.new_window').click(function()
		{
			window.open($(this).attr('href'));
			return false;
		});

		/* KEEP CODE FROM HERE UP TO PREVIOUS COMMENT BLOCK ON ALL PROJECTS*/

		$("ul.nav li").hover(function(){ $("ul", this).fadeIn("fast"); }, function() { } );
		$("ul.nav li").hoverClass ("hover");

		$('p#hover_class img').hover(function()
		{
			var id = $(this).attr('id');
			$('img#bike_example').attr('src', '/images/'+id+'_example.png');
		});
		
		$('.rollover').hover(function() 
		{
			$(this).css('opacity','0.4').css('border','2px solid #36F'); 
		},
        function() 
        { 
        	$(this).css('opacity','1.0').css('border','2px solid #CD9933');
        });
        
        $('a.view_image').click(function()
        {
        	var href = $(this).attr('href');
        	goImgWin(href,600,400,100,50);
        	return false;
        });
        
		$("a.changer").click(function(){
				if (this.id == 'font_up'){
					increaseFontSize();
				} else if (this.id == 'font_down'){
					decreaseFontSize();
				}
   			return false;
		});

		$('form.test').validate();
		$('form.test_step3').validate();

     });