var widgetAnimateTime = 500;

var getWidgetBox = function(callback){
  if($("#widgetBox").length) return $("#widgetBox");
  var positionCss = {right: "20px", top: "140px", minWidth: "250px"};
  var widgetBox = $('<div id="widgetBox" class="pod" style="position: absolute; right: -250px; top: 140px; min-width: 250px; min-height: 250px;" title="By clicking here, you may be taken to a  3rd party web site. Third party websites may not follow the same  privacy, security or accessibility standards as those of the South  Valley Bank & Trust site."></div>');
  $("body").append(widgetBox);
  widgetBox.animate(positionCss, widgetAnimateTime, "linear", callback);
  return widgetBox;
};
var removeWidgetBox = function(callback){
  $("#widgetBox").animate({right: "-250px"}, widgetAnimateTime, "linear", function(){
    $("#widgetBox").remove();
    if($.isFunction(callback)) callback();
  });
  return $("#widgetBox").length > 0;
};

var doWidgetBox = function(getWidgetCallback, widgetId){
  var removing = removeWidgetBox(function(){
    if(widgetId == window.currentWidget) return;
    window.currentWidget = widgetId;
    getWidgetBox(getWidgetCallback);
  });
  if(removing) return false;
  window.currentWidget = widgetId;
  var widgetBox = getWidgetBox(getWidgetCallback);
  return false;
};
var weatherWidgetCode = '<embed type="application/x-shockwave-flash" src="http://image.weather.com/web/services/gadgets/images/sandbox.swf?random_number=762" title="By clicking here, you may be taken to a 3rd party web site. Third party websites may not follow the same privacy, security or accessibility standards as those of the South Valley Bank & Trust site." width="290" height="394" id="twcmovie" name="twcmovie" bgcolor="#ffffff" quality="high" seamlesstabbing="false" allowscriptaccess="always" scalemode="noscale" wmode="transparent" flashvars="gid=0">';

//var weatherWidgetCode = '<iframe allowtransparency="true" marginwidth="0" marginheight="0" hspace="0" vspace="0" frameborder="0" scrolling="no" src="http://weather.yahoo.com/badge/?q={1}&u=f&t=trans&l=vertical" title="By clicking here, you may be taken to a 3rd party web site. Third party websites may not follow the same privacy, security or accessibility standards as those of the South Valley Bank & Trust site." height="255px" width="186px"></iframe>';
var getWeatherWidget = function(){
  $("#widgetBox").append(weatherWidgetCode);
  /*
  $("#widgetBox").append('<label for="widgetWeatherInput" style="padding-top: 10px; display: block;">Enter Your City, State:</label>' +
                   '<input id="widgetWeatherInput" /><div id="weatherBox"></div>');
      
  $("#widgetBox").find("#widgetWeatherInput").keypress(function(e){
    if(e.keyCode != 13) return;
    $.cookie("widgetWeatherLocation",$("#widgetWeatherInput").val(), {expires: new Date(2012, 12, 21)});
    $("#weatherBox").empty();
    $("#loaderBox").append(weatherWidgetCode.replace("{1}", encodeURIComponent($("#widgetWeatherInput").val())));
    $("#loaderBox").find("iframe").load(function(){
      $("#weatherBox").append($("#loaderBox").find("iframe").fadeIn());
      $("#loaderBox").empty();
    });
  });
  if($.cookie("widgetWeatherLocation")){
    $("#widgetWeatherInput").val($.cookie("widgetWeatherLocation"));
    $("#loaderBox").append(
      weatherWidgetCode.replace("{1}",
      encodeURIComponent($.cookie("widgetWeatherLocation"))));
    $("#loaderBox").find("iframe").load(function(){
      $("#weatherBox").append($("#loaderBox").find("iframe").fadeIn());
      $("#loaderBox").empty();
    });
  }
  $("#widgetWeatherInput").focus();*/
};
  
var standardWidget = function(widgetId, minWidth){
  if(!minWidth) minWidth = "330px";
  return function(){
    $.getScript("http://cdn.widgetserver.com/syndication/subscriber/InsertWidget.js", function(){
      $("#widgetBox").css({minWidth: minWidth, paddingTop: "10px", paddingBottom: "10px"});
      if(WIDGETBOX && $("#widgetBox").length) WIDGETBOX.renderWidgetInElement(widgetId, "widgetBox");
    });
  };
};
var widgetButton = function(buttonId, widgetFunc){
  return function(){
    $(buttonId).click(function(){
      return doWidgetBox(widgetFunc, buttonId);
    });
  };
};
var setupWeatherWidget = widgetButton("#widgetWeather", getWeatherWidget);
//var setupWeatherWidget = widgetButton("#widgetWeather", standardWidget('b62ea426-840e-406d-9e93-62ee27a65a4a'));
var setupStocksWidget = widgetButton("#widgetStocks", standardWidget('16f7c74c-115c-429b-90b4-13b0aef1b298'));
var setupNewsWidget = widgetButton("#widgetNews", standardWidget('3eb71fc7-f42b-4502-ba9e-1f83d04e7aa7'));
var setupAGWidget = widgetButton("#widgetAG", standardWidget('8d01cd33-5a5b-4624-880a-5087e3c874f1'));

$(document).ready(function(){
  $("body").append('<div style="position: absolute; left; -1000px; top: -1000px;" id="loaderBox"></div>');
  
  setupWeatherWidget();
  setupStocksWidget();
  setupNewsWidget();
  setupAGWidget();
});

jQuery.cookie=function(a,b,c){if(typeof b!='undefined'){c=c||{};if(b===null){b='';c.expires=-1}var d='';if(c.expires&&(typeof c.expires=='number'||c.expires.toUTCString)){var e;if(typeof c.expires=='number'){e=new Date();e.setTime(e.getTime()+(c.expires*24*60*60*1000))}else{e=c.expires}d='; expires='+e.toUTCString()}var f=c.path?'; path='+(c.path):'';var g=c.domain?'; domain='+(c.domain):'';var h=c.secure?'; secure':'';document.cookie=[a,'=',encodeURIComponent(b),d,f,g,h].join('')}else{var j=null;if(document.cookie&&document.cookie!=''){var k=document.cookie.split(';');for(var i=0;i<k.length;i++){var l=jQuery.trim(k[i]);if(l.substring(0,a.length+1)==(a+'=')){j=decodeURIComponent(l.substring(a.length+1));break}}}return j}};

