
$().ready(function(){

//print function
  $('.print_page').click(function(){print();});

//css file : text resize
  textResize();  

// form validation	
	formValidation();

//ticker code
  $.get("/site/stocks/ticker.php", function(data){ ticker(data); });
//stockwatch
  stockWatch();

});	

function stockWatch(stock){
  url="/site/stocks/getStocks.php?stock=";
  $("#avalStocks a").each(function(){
    $(this)
      .click(function(){
        thisStock = $(this).attr("href");
        thisStock = thisStock.replace(/\/resources\/stocks\/\?stock=/,"");
        $.getJSON(url+thisStock, function(data){ showStock(data); });
      })
      .click(function(){ return false; });
  });
}

function showStock(stock){
  stocks = this["stock"];
  
  for (i in stock){ 
    $("#"+i).text(stock[i]);
  }
   
}

function textResize(){
  $("#nav_helpers ul").append('<li>Text <a href="?large" class="text_large">A</a> <a href="?normal" class="text_normal">A</a></li>');
  
  var c = readCookie('style');
  if (c) stylizeCookie(c);
  
  
  $(".text_large").click( function(){
    stylizeCookie("fcu_large");
    return false;
  });
  $(".text_normal").click( function(){
    stylizeCookie("fcu_normal");
    return false;
  });
}

function stylizeCookie(cssSize){
  if(cssSize != "fcu_normal"){
    $('head').append('<link rel="stylesheet" id="resizeCSS" href="/site/css/'+cssSize+'.css" type="text/css" media="screen, projection" />');
  } else {
    $('#resizeCSS').remove();  
  }
  createCookie('style', cssSize, 365);  
}

// cookie functions http://www.quirksmode.org/js/cookies.html
function createCookie(name,value,days)
{
	if (days)
	{
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var 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);
}
// /cookie functions

//sifr
var gil = {
  src: '/site/scripts/gillsanslight.swf'
};

var anz = {
  src: '/site/scripts/anziano.swf'
};

sIFR.useStyleCheck = true;
sIFR.activate(anz);

sIFR.replace(anz, {
  selector: '.tmpl_index #content h2',
	css: ['.sIFR-root {color:#192952; text-align:center; }',
				'.sIFR-root {kerning:true}'],
	wmode: 'transparent'
});

sIFR.replace(anz, {
  selector: '.tmpl_content #content h2',
	css: ['.sIFR-root {color:#192952;}',
				'.sIFR-root {kerning:true}'],
	wmode: 'transparent'
});

//stock ticker functions
function ticker(stocks){
	$("#ticker").html(stocks).css("position","relative");
	the_left = parseInt($("#ticker").width()) + "px";
	$("#ticker_stocks").css( {left: the_left, position:"relative"} );
	
	ticker_scroll();
}

function ticker_scroll(){
  ticker = $("#ticker_stocks");
  ticker_width = $("#ticker_stocks").width();
  
  if(ticker.css("left") <= $("#ticker").css("left")){
    the_left = parseInt($("#ticker").width()) + "px";
    ticker.css("left",the_left);
    ticker_scroll();
  }else{
    ticker.animate({left:(-1)*ticker_width},60000,"",function(){ticker_scroll();});
  }
}


function formValidation(){
  formValid = '';
	
	$('form :input').blur(function(){
		if( $(this).is('.required')) {
			if(this.value == '') {
				this.value = $(this).attr('rel');
				formValid++;
			};
		};
	});
	
	$('form textarea').blur(function(){
		if( $(this).is('.required')) {
			if(this.value == '') {
				this.value = $(this).attr('rel');
				formValid++;
			};
		};
	});
	
	$('form :input').focus(function(){
		if( $(this).is('.required')) {
			if(this.value == $(this).attr('rel')) {
				this.value = '';
				if(formValid != 0){
					formValid--;
				}
			};
		};
	});
	
	$('form textarea').focus(function(){
		if( $(this).is('.required')) {
			if(this.value == $(this).attr('rel')) {
				this.value = '';
				if(formValid != 0){
					formValid--;
				}
			};
		};
	});
	
	$('form :submit').focus(function(){
		if( formValid != 0){
			alert("Please fill out the required information.");
			return false;
		}
	});
}