$(function() {
	var current = $('body').attr('class');



  var bg = Math.floor(Math.random()*5);
  $('body').addClass('bg' + bg);

	var postfix = '_o';
	$('a.hover img').not('[src*="'+ postfix +'."]').each(function() {
			var img = $(this);
			var src = img.attr('src');
			var src_on = src.substr(0, src.lastIndexOf('.'))
									+ postfix
									+ '.png';
			img.hover(
				function() {
					img.attr('src', src_on);
				},
				function() {
					img.attr('src', src);
				});
		});

		var proxy_url = "";
		var query = "aurora3D_japan";
		var api_url = proxy_url + "http://twitter.com/statuses/user_timeline/" + query + ".json?page=1&callback=?";
		$.getJSON(api_url, function(json) {
			var html = json[0].text.linkify().linkuser().linktag().replace(/<a/g,'<a target = "_blank"');
			$('#ghTwitter > p').html(html);
		});

		var proxy_url = "/wordpress/wp-content/themes/aurora_v1.4/crossdomain-proxy.php?url=";
		var space_url = "http://hbksw1.stelab.nagoya-u.ac.jp/rblogtoday.txt";
		var aurora_url = "http://hbksw1.stelab.nagoya-u.ac.jp/kplogtoday.txt";

		$("#ghuSpacerWeatherData").load(proxy_url + space_url, {limit: 25}, function(){
			var rb_data = $("#ghuSpacerWeatherData").html();
			var data_array = rb_data.split(" ");
			var rb = data_array[1];
			var img = "http://aurora3d.sakura.ne.jp/wordpress/wp-content/themes/aurora_v1.4/img/";
			if (rb < 30 ) {
				img += "ghUtility_sw_icon_0.png";
			} else if (rb >= 30 && rb < 60) {
				img += "ghUtility_sw_icon_1.png";
			} else if (rb >= 60) {
				img += "ghUtility_sw_icon_2.png";
			}
			var img_tag = "<img src='" + img +  "'/>";
			$("#ghuSpacerWeather > div.icon > a").html(img_tag);
		});
		

		$("#ghuAuroraWeatherData").load(proxy_url + aurora_url, {limit: 25}, function(){
			var kp_data = $("#ghuAuroraWeatherData").html();
			var data_array = kp_data.split(" ");
			var kp = data_array[1];
			var img = "http://aurora3d.sakura.ne.jp/wordpress/wp-content/themes/aurora_v1.4/img/";
			if (kp < 3) {
				img += "ghUtility_au_icon_0.png";
			} else if (kp == 3) {
				img += "ghUtility_au_icon_1.png";
			} else if (kp >= 4 && kp <7) {
				img += "ghUtility_au_icon_2.png";
			} else if (kp >= 7) {
				img += "ghUtility_au_icon_3.png";
			}
			var img_tag = "<img src='" + img +  "'/>";
			$("#ghuAuroraWeather > div.icon > a").html(img_tag);
		});

		var swframe_url = "http://hbksw1.stelab.nagoya-u.ac.jp/rbforecast.html";
		var auframe_url = "http://hbksw1.stelab.nagoya-u.ac.jp/kpforecast.html";

		$("#swframe").load(proxy_url + swframe_url, {limit: 25}, function(){
/*
			$("#swframe img").each(function() {
				alert($(this).attr('src'))
			});
*/
		});

		$("#auframe").load(proxy_url + auframe_url, {limit: 25}, function(){
		});
		
		setTheme();

setSlide();

});

String.prototype.linkify = function() {
	return this.replace(/[A-Za-z] + :\/\/[A-Za-z0-9-_] + \.[A-Za-z0-9-_:%&;\?\/. = ] + /g,function(m) {
		return m.link(m);
	});
};

String.prototype.linkuser = function() {
	return this.replace(/[@] + [A-Za-z0-9-_] + /g,function(u) {
		return u.link("http://twitter.com/" + u.replace("@",""));
	});
};

String.prototype.linktag = function() {
	return this.replace(/[] + [A-Za-z0-9-_] + /,function(t) {
		return t;
	});
};



    function setTheme() {
      var theTheme = false;
      var hashes = location.href.slice(location.href.indexOf('?') + 1).split('&');
      for (var i = 0; i < hashes.length; i++) {
        var theHash = hashes[i].split('=');
        if (theHash[0] == 'theme') {
          var theTheme = theHash[1];
        }
      }
      if (theTheme) {
        $('a').each(function(){
          if ($(this).attr('href')) {
            if ($(this).attr('href').indexOf('?') == -1) {
              $(this).attr('href', $(this).attr('href') + '?theme=' + theTheme);
            } else {
              $(this).attr('href', $(this).attr('href') + '&theme=' + theTheme);
            }
          }
        });
      }
    }



    function setSlide() {
      var theDuration = 200;
      var fadeDuration = 500;
      $('div.slide-containter a.prev').hide();
      $('div.slide-containter').each(function(){
        var currentSlide = 0;
        var slideCount = $(this).find('div.slide').length;
        var animating = false;
        var slideWidth = $(this).find('div.slide').width();
        var slides = $(this).find('div.slides');
        var nextButton = $(this).find('a.next');
        var prevButton = $(this).find('a.prev');
        var fade = false;
        if ($(this).hasClass('fade')) {
          var fade = true;
          slides.css('position', 'relative');
          slides.find('div.slide').css({
            position: 'absolute',
            top: 0,
            left: 0,
          }).hide();
          slides.find('div.slide').eq(currentSlide).show();
        }

        slides.width(slideWidth * slideCount)
              .height($(this).find('div.slide').height());
        prevButton.hide();
        $(this).find('a.prev, a.next').click(function(){
          if (!animating) {
            animating = true;
            var prevSlide = currentSlide;
            if ($(this).hasClass('next')) {
              currentSlide++;
            } else if ($(this).hasClass('prev')) {
              currentSlide--;
            }

            if (fade) {
              slides.find('div.slide').eq(currentSlide).css('z-index', '1')
              .fadeIn(fadeDuration, function(){
                $(this).css('z-index', '0');
                slides.find('div.slide').eq(prevSlide).hide();
                animating = false;
              });
            } else {
              slides.animate({
                left: - currentSlide * slideWidth + 'px'
              }, {
                duration: theDuration,
                easing: 'jswing',
                complete: function() {
                  animating = false;
                }
              });
            }

            if (currentSlide + 1 >= slideCount) {
              nextButton.hide();
            } else {
              nextButton.show();
            }
            if (currentSlide <= 0) {
              prevButton.hide();
            } else {
              prevButton.show();
            }
          }
          return false;

        });
      });
    }




