jQuery.event.add(window, "load", function(){
	$("#global_header .logo_pre").addClass("logo");
	$("#global_header .logo").removeClass("logo_pre");
	
	var interval = setInterval(function(){
		clearInterval(interval);
		pageInit();
	},2000);
	
});

function pageInit(){
	initialize();
	
	
	
	
	setInterval(function(){
		setLoadingLump();
	},5000);
}

function setLoadingLump(){
	$(function(){
		if($("#grouponpon0 .users ul li").length){
			var firstW = $("#grouponpon0 .users ul li:first").width();
			$("#grouponpon0 .users ul li:first").hide(1000,function(){
					$("#grouponpon0 .users ul").append($(this));
					$(this).show(1000);
				});
		}
		
		
	});
}

var map;
var myLatlng = new google.maps.LatLng(35.663577, 139.699348);

var MY_MAPTYPE_ID = 'LIGHT';

function initialize() {
    
  var stylez = [
    {
      featureType: "road",
      elementType: "geometry",
      stylers: [
        { hue: "#32C7F9" },
        { saturation: 100 }
      ]
    },
    
    {
      featureType: "landscape",
      elementType: "geometry",
      stylers: [
        { hue: "#32C7F9" },
        { saturation: 75 },
        { lightness: -100}
      ]
    },
    {
      featureType: "all",
      elementType: "labels",
      stylers: [
        { hue: "#32C7F9" },
        { saturation: 100 }
      ]
    }
  ];

  var mapOptions = {
    zoom: 14,
    center: myLatlng,
    mapTypeControlOptions: {
       mapTypeIds: [google.maps.MapTypeId.ROADMAP, MY_MAPTYPE_ID]
    },
    mapTypeId: MY_MAPTYPE_ID
  };

  map = new google.maps.Map(document.getElementById("map_canvas"),
      mapOptions);
  
  var marker = new google.maps.Marker({
      position: myLatlng, 
      map: map, 
      title:"Hello World!"
  }); 
  
  var styledMapOptions = {
    name: "　LIGHT　"
  };

  var jayzMapType = new google.maps.StyledMapType(stylez, styledMapOptions);
  
  map.mapTypes.set(MY_MAPTYPE_ID, jayzMapType);
  
  $(function(){
		var count=0;
	    $.ajax({
	      scriptCharset: 'utf-8',
	      type: "GET",
	      url: 'php/rss.php',
	      success: function(json) {
	        $.each(json.posts, function() {
	          if(this.attachments.length && count<5){
	          	var ow = this.attachments[0].images.thumbnail.width;
	          	var oh = this.attachments[0].images.thumbnail.height;
	          	var sw = 128;
	          	var sh = Math.floor(oh*(sw/ow));
	          	if(sh>128){
	          		sh = 128;
	          		sw = Math.floor(ow*(sh/oh));
	          	}
	          	var mx = Math.floor((128-sw)*0.5);
	          	var my = Math.floor((128-sh)*0.5);
	          	$("#blog ul").append('<li><div class="image"><a href="'+this.url+'" target="_blank"><img src="'+this.attachments[0].images.thumbnail.url+'" width="'+sw+'" height="'+sh+'" style="margin:'+my+'px '+mx+'px '+my+'px '+mx+'px" /></a></div><p class="label"><a href="'+this.url+'" target="_blank">'+this.title+'</a></p></li>');
	          	count++;
	          }
	        });
	      },
	      dataType: 'json'
	    });
	    
	    
	    $.ajax({
	      scriptCharset: 'utf-8',
	      type: "GET",
	      url: 'php/user.php',
	      success: function(json) {
	      	$(".count_rest").text(json.rest+"人");
	      	if(json.rest==0){
	      		$(".count_rest_all").text("クーポン成立");
	      	}
	      	$(".count_now").text(json.total+"/"+json.max+"人");
	      	if(json.max-json.total > 0){
	      		$("#grouponpon0 .btn").html('<a href="http://www.raw-fi.com/php/oauth.php" target="_blank"><img src="common/img/btn_groupon.jpg" width="220" height="60" alt="TwitterアカウントでクーポンGET!!" /></a>');
	      		$("#grouponpon1 .btn").html('<a href="http://www.raw-fi.com/php/oauth.php" target="_blank"><img src="common/img/btn_groupon_l.jpg" width="420" height="80" alt="TwitterアカウントでクーポンGET!!" /></a>');
	      	}else{
	      		$("#grouponpon0 .btn").html('<img src="common/img/btn_groupon_end.jpg" width="220" height="60" alt="配布終了しました" />');
	      		$("#grouponpon1 .btn").html('<img src="common/img/btn_groupon_l_end.jpg" width="420" height="80" alt="配布終了しました" />');
	      	}
	      	var userCount=0;
	        $.each(json.user, function() {
	          if(count<15){
	          	$("#grouponpon0 .users ul").append('<li class="clearfix"><div class="icon"><a href="http://twitter.com/'+this.screen_name+'" target="_blank"><img src="'+this.profile_image_url+'" width="24" height="24" /></a></div><div class="name"><a>'+this.screen_name+'</a></div></li>');
	          }
	          
	        	$("#grouponpon1 .right ul").append('<li><a href="http://twitter.com/'+this.screen_name+'" target="_blank"><img src="'+this.profile_image_url+'" width="32" height="32" /></a></li>');
	        	userCount++;
	          
	        });
	      },
	      dataType: 'json'
	    });
	    
	    //ust
	    $(".ust_container").append('<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="400" height="300" id="utv162365"><param name="flashvars" value="autoplay=false&amp;brand=embed&amp;cid=958771"/><param name="allowfullscreen" value="true"/><param name="allowscriptaccess" value="always"/><param name="movie" value="http://www.ustream.tv/flash/live/1/958771"/><embed flashvars="autoplay=false&amp;brand=embed&amp;cid=958771" width="400" height="300" allowfullscreen="true" allowscriptaccess="always" id="utv162365" name="utv_n_615319" src="http://www.ustream.tv/flash/live/1/958771" type="application/x-shockwave-flash" /></object>');
	    //$(".twitter_container").after('');
	    $(".facebook_container").append('<iframe src="http://www.facebook.com/plugins/likebox.php?href=http%3A%2F%2Fwww.facebook.com%2Fpages%2FRaw-Fi%2F149288745119334&amp;width=400&amp;colorscheme=dark&amp;connections=10&amp;stream=true&amp;header=false&amp;height=555" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:402px; height:557px;" allowTransparency="true"></iframe>');
	    
	    //$("#grouponpon0 .counter ul").html('<li class="hatebu"><a href="http://b.hatena.ne.jp/entry/http://www.raw-fi.com/" class="hatena-bookmark-button" data-hatena-bookmark-title="Raw-Fi" data-hatena-bookmark-layout="standard" title="このエントリーをはてなブックマークに追加"><img src="http://b.st-hatena.com/images/entry-button/button-only.gif" alt="このエントリーをはてなブックマークに追加" width="20" height="20" style="border: none;" /></a><script type="text/javascript" src="http://b.st-hatena.com/js/bookmark_button.js" charset="utf-8" async="async"></script></li><li class="twitter"><a href="http://twitter.com/share" class="twitter-share-button" data-url="http://www.raw-fi.com/" data-text="Raw-Fi 3D | 渋谷UNDERBAR 12/23 15:00-OPEN!!" data-count="horizontal" data-via="raw_fi">Tweet</a><script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script></li><li class="facebook"><script src="http://connect.facebook.net/en_US/all.js#xfbml=1"></script><fb:like href="http://www.raw-fi.com/" layout="button_count"></fb:like></li>');
	    
	});
	
	
}
