var updateflag = {};
var topicurl = {};
var topicdate = {};
var topictitle = {};
var topicorder = [];
var tickerend;
var tickerstep = 1;
var tickerspeed = 20;
var lrflag = 1;
var lrflag_b = 1;
var lrflag_c = 1;
var rand;
var randfac = 75;
var randspeed = 2000;
var first = true;
var first_b = true;
var first_c = true;
var UPDATE_SPAN = 2419200000;
$(document).ready(function(){
	$.get('http://www.commons-sense.net/about/author/commons/',function(data){
		var current = new Date;
		$(data).find('div').each(function(){
			var src = $(this).find('a').attr('href');
			var ary = src.replace(/^http\:\/\/www\.commons\-sense\.net\/([^\/]+)\/(\d{4})\/(\d{2})\/(\d{2})\/.+$/,'$1|$2|$3|$4').split('|');
			var dt = new Date(ary[1],ary[2]-1,ary[3]);
			topicorder.push(ary[0]);
			if (current.getTime() - dt.getTime() < UPDATE_SPAN) {
				updateflag[ary[0]] = 1;
				topicurl[ary[0]] = src;
				topicdate[ary[0]] = dt;
				topictitle[ary[0]] = $(this).find('a').text();
			}
		});
		var ticker = '';
		if (updateflag['gallery-man']) {
			$.get(topicurl['gallery-man'],function(datagallery){
				var img = ($(datagallery).find('.topic img').length) ? '<img src="'+$(datagallery).find('.topic img').attr('src')+'" alt="" onload="setMargin(this);" />' : '';
				$('body').append('<div id="popup_gallery_man"><div class="popupthumbnail">'+img+'</div><div class="popupdesc"><div class="popupdate">'+getDateString(topicdate['gallery-man'])+'</div><div class="popuptitle">'+topictitle['gallery-man']+'</div></div></div>');
				$('#popup_gallery_man div').click(function(){
					location.href = 'http://www.commons-sense.net/gallery-man/';
				}).hover(function(){$('#popup_gallery_man div').css({'color':'#76bcc7'});},function(){$('#popup_gallery_man div').css({'color':'black'});});
			});
			ticker += '<div id="ticker1" class="tickerchild"><a href="'+topicurl['gallery-man']+'">gallery更新:'+getDateString(topicdate['gallery-man'])+' '+topictitle['gallery-man']+'</a></div>';
		}
		if (updateflag['shopping-man']) {
			$.get(topicurl['shopping-man'],function(datashopping){
				var img = ($(datashopping).find('.topic img').length) ? '<img src="'+$(datashopping).find('.topic img').attr('src')+'" alt="" onload="setMargin(this);" />' : '';
				$('body').append('<div id="popup_shopping_man"><div class="popupthumbnail">'+img+'</div><div class="popupdesc"><div class="popupdate">'+getDateString(topicdate['shopping-man'])+'</div><div class="popuptitle">'+topictitle['shopping-man']+'</div></div></div>');
				$('#popup_shopping_man div').click(function(){
					location.href = 'http://www.commons-sense.net/shopping-man/';
				}).hover(function(){$('#popup_shopping_man div').css({'color':'#76bcc7'});},function(){$('#popup_shopping_man div').css({'color':'black'});});
			});
			ticker += '<div id="ticker2" class="tickerchild"><a href="'+topicurl['shopping-man']+'">shopping更新:'+getDateString(topicdate['shopping-man'])+' '+topictitle['shopping-man']+'</a></div>';
		}
		if (updateflag['blog-b']) {
			$.get(topicurl['blog-b'],function(datablogb){
//				var img = ($(datablogb).find('#urabody img').length) ? '<img src="'+$(datablogb).find('#urabody img').attr('src')+'" alt="" onload="setMargin(this);" />' : '';
			    var img = ($(datablogb).find('#urabody img').length);
			    if( img ){
				img = '<img src="'+$(datablogb).find('#urabody img').attr('src')+'" alt="" onload="setMargin(this);" />'; 
				$('body').append('<div id="popup_ura_man"><div class="popupthumbnail">'+img+'</div><div class="popupdesc"><div class="popupdate">'+getDateString(topicdate['blog-b'])+'</div><div class="popuptitle">'+topictitle['blog-b']+'</div></div></div>');
			    }else{
				$('body').append('<div id="popup_ura_man" style="padding:76px 45px 55px 78px;"><div class="popupdesc"><div class="popupdate" style="text-align:center;">'+getDateString(topicdate['blog-b'])+'</div><div class="popuptitle" style="text-align:center;">'+topictitle['blog-b']+'</div></div></div>');
				$('.popupdesc').each(function(){
				    var d = Math.round((56-$(this).height())/2);
				    if (d>0) $(this).css('padding-top',d+'px');
				    //$(this).css({'display':'table-cell','vertical-align':'middle'});
				});
			    }
//				$('body').append('<div id="popup_ura_man"><div class="popupthumbnail">'+img+'</div><div class="popupdesc"><div class="popupdate">'+getDateString(topicdate['blog-b'])+'</div><div class="popuptitle">'+topictitle['blog-b']+'</div></div></div>');
				$('#popup_ura_man div').click(function(){
					location.href = 'http://www.commons-sense.net/blog-b/?man';
				}).hover(function(){$('#popup_ura_man div').css({'color':'#76bcc7'});},function(){$('#popup_ura_man div').css({'color':'black'});});
			});
		    ticker += '<div id="ticker4" class="tickerchild"><a href="'+topicurl['blog-b']+'">ura-comosen更新:'+(getDateString(topicdate['blog-b']))+' '+topictitle['blog-b']+'</a></div>';

		}
		for (var i=0;i<topicorder.length;i++) {
			var b = topicorder[i];
			if (b.match(/^blog\-/) && b != 'blog-b' && updateflag[b]) {
				$.get(topicurl[b],function(databloga){
				    var img = $(databloga).find('.topic img').length
				    if(img){
					img = '<img src="'+$(databloga).find('.topic img').attr('src')+'" alt="" onload="setMargin(this);" />';
					$('body').append('<div id="popup_blog_man"><div class="popupthumbnail">'+img+'</div><div class="popupdesc"><div class="popupdate">'+getDateString(topicdate[b])+'</div><div class="popuptitle">'+topictitle[b]+'</div></div></div>');
				    }else{
					$('body').append('<div id="popup_blog_man"><div class="popupdesc"><div class="popupdate" style="text-align:center;">'+getDateString(topicdate[b])+'</div><div class="popuptitle" style="text-align:center;">'+topictitle[b]+'</div></div></div>');
					$('.popupdesc').each(function(){
					    var d = Math.round((56-$(this).height())/2);
					    if (d>0) $(this).css('padding-top',d+'px');
					    //$(this).css({'display':'table-cell','vertical-align':'middle'});
					});
				    }
					$('#popup_blog_man div').click(function(){
						location.href = 'http://www.commons-sense.net/'+b+'/?man';
					}).hover(function(){$('#popup_blog_man div').css({'color':'#76bcc7'});},function(){$('#popup_blog_man div').css({'color':'black'});});
				});
//			ticker += '<div id="ticker3" class="tickerchild"><a href="'+topicurl[b]+'">blog譖ｴ譁ｰ:'+getDateString(topicdate[b])+' '+topictitle[b]+'</a></div>';
			ticker += '<div id="ticker3" class="tickerchild"><a href="'+topicurl[b]+'">blog更新:'+getDateString(topicdate[b])+' '+topictitle[b]+'</a></div>';
			break;
			}
		}
		for (var i=0;i<topicorder.length;i++) {
			var be = topicorder[i];
		    if (be.match(/^beauty\-/) && updateflag[be]) {
			    $.get(topicurl[be],function(databeauty){
				var img = '<img src="'+$(databeauty).find('.authorimg img').attr('src').replace('.png','_s.png')+'" style="max-width:45px" alt="" onload="setMargin(this);" />';
				$('body').append('<div id="popup_beauty_man"><div class="popupthumbnail">'+img+'</div><div class="popupdesc"><div class="popupdate">'+getDateString(topicdate[be])+'</div><div class="popuptitle">'+topictitle[be]+'</div></div></div>');
				$('#popup_beauty_man div').click(function(){
				    location.href = 'http://www.commons-sense.net/'+be+'/';
				}).hover(function(){$('#popup_beauty_man div').css({'color':'#76bcc7'});},function(){$('#popup_beauty_man div').css({'color':'black'});});
			    });
			    ticker += '<div id="ticker3" class="tickerchild"><a href="'+topicurl[be]+'">beauty更新:'+getDateString(topicdate[be])+' '+topictitle[be]+'</a></div>';
			    break;
			}
		}
		//if (updateflag['blog-a']) {
		//	$.get(topicurl['blog-a'],function(databloga){
		//		var img = ($(databloga).find('.topic img').length) ? '<img src="'+$(databloga).find('.topic img').attr('src')+'" alt="" onload="setMargin(this);" />' : '';
		//		$('body').append('<div id="popup_blog_man"><div class="popupthumbnail">'+img+'</div><div class="popupdesc"><div class="popupdate">'+getDateString(topicdate['blog-a'])+'</div><div class="popuptitle">'+topictitle['blog-a']+'</div></div></div>');
		//		$('#popup_blog_man div').click(function(){
		//			location.href = 'http://www.commons-sense.net/blog-a/?man';
		//		}).hover(function(){$('#popup_blog_man div').css({'color':'#76bcc7'});},function(){$('#popup_blog_man div').css({'color':'black'});});
		//	});
		//	ticker += '<div id="ticker3" class="tickerchild"><a href="'+topicurl['blog-a']+'">blog更新:'+getDateString(topicdate['blog-a'])+' '+topictitle['blog-a']+'</a></div>';
		//}
		$('.popupdesc').each(function(){
			var d = Math.round((56-$(this).height())/2);
			if (d>0) $(this).css('padding-top',d+'px');
			//$(this).css({'display':'table-cell','vertical-align':'middle'});
		});
	    var message_text = 'この度の東日本大震災に伴い、お亡くなりになられた方々のご冥福をお祈りし、ご遺族の皆様に深くお悔やみ申し上げます。また被災された方々や震災に伴う原発事故により、不自由な生活を余儀なくされている方々にお見舞い申し上げるとともに、この困難を乗り越え一日も早く、復興することを心よりお祈り申し上げます。このような状況において日々と変わらぬ活動を続けること、また雑誌やWEBを通して、それを見る国内外の皆様に楽しさや希望を届けることで被災地を支えることができればと思います。    佐々木 香    Editor in Chief    commons&sense / commons&sense man';
	    ticker += '<div id="ticker_ms" class="tickerchild">'+message_text+'</div>';
		if (ticker != '') {
			$('body').append('<div id="ticker">'+ticker+'</div>');
			tickerend = 0;
			$('.tickerchild').each(function(){
				$(this).css('left',tickerend+'px');
				tickerend += $(this).outerWidth();
			});
			setInterval(moveTicker,tickerspeed);
//		    $('.tickerchild').css({'right':'0px'});
//		    $('#ticker').css({'z-index':'10'});
//		    moveTicker2();
/*		    $('.tickerchild').css({'right':'0px'});
		    var child = $('.tickerchild')
		    for (var i = 0; i < child.size(); i++ ){
			    child[i].style.webkitTransform = 'translate(100px,0 )';
		    }
*/
//                    $('.tickerchild').css('cssText','@-webkit-animation-name:move');
//		    $('.body').css('cssText','@-webkit-keyframes move{ 0% { right: 0;} 25% { right: 25%;} 70% { right: 500px;} 100% { right: 1000px;} }');

		}
	    $('body').append('<div id="balloon" style="position:absolute;left:-145px;top:'+$(window).height()+'px;"><a href="#"><img src=/images/balloon/balloon_m.png /></a></div>');
	    $('body').append('<div id="balloon_b" style="z-index:2;position:absolute;left:'+$(window).width()+'px;top:'+$(window).height()+'px;"><a href="/beauty-top/"><img src=/images/balloon/launch_balloon.png /></a></div>');
	    $('body').append('<div id="balloon_c" style="z-index:2;position:absolute;left:'+$(window).width()+'px;top:'+$(window).height()+'px;"><a href="/blog-b/"><img src=/images/balloon/ballon_blue.png /></a></div>');
	    setInterval(moveRandom,randspeed);
	    moveBalloon();
		setTimeout(function(){
			moveBalloon_b();
			setInterval(moveRandom_b,randspeed);
	    },1000);
		setTimeout(function(){
			moveBalloon_c();
			setInterval(moveRandom_c,randspeed);
	    },15000);
	});
});
var monthString = ['January','February','March','April','May','June','July','August','September','October','November','December'];
function getDateString(dt) {
	return monthString[dt.getMonth()] + ' ' + dt.getDate() + ', ' + dt.getFullYear();
}
function setMargin(img) {
	var iw = $(img).width();
	var ih = $(img).height();
	if (iw<45) $(img).css('margin-left',Math.round((45-iw)/2)+'px');
	if (ih<56) $(img).css('margin-top',Math.round((56-ih)/2)+'px');
	$('.popupdesc').each(function(){
		var d = Math.round((56-$(this).height())/2);
		if (d>0) $(this).css('padding-top',d+'px');
		//$(this).css({'display':'table-cell','vertical-align':'middle'});
	});
}
function moveTicker() {
	tickerend -= tickerstep;
	$('.tickerchild').each(function(){
	    var l = $(this).position().left-tickerstep;
		if (l+$(this).outerWidth() < 0) {
			l = (tickerend>$('#backimage').width()) ? tickerend : $('#backimage').width();
			tickerend = l + $(this).outerWidth();
		}
		$(this).css('left',l+'px');
		l += $(this).outerWidth();
	});
}
/*
function moveTicker2() {
    var child = $('.tickerchild')
    var left = $('#ticker').outerWidth();
    for (var i = 0; i < child.size(); i++ ){
	    child[i].style.webkitTransition = "-webkit-Transform 70s linear"
	    child[i].style.webkitTransform = 'translate3d(-'+left*3+'px,0,0 )';
	    child[i].style.mozTransition = "-moz-Transform 40s linear"
	    child[i].style.mozTransform = 'translate(-'+left*3+'px,0 )';
    }

//    $('.tickerchild').each(function(){console.log($(this).each().style.webkitTransform = 'translate(1px,0 )'});
}
*/
function moveBalloon(){
    var width = $(window).width();
    var height = $(window).height();
    var swidth = $('#balloon').width();
    var sheight = $('#balloon').height();
    if( lrflag ){
		$('#balloon').css('left',-swidth+40+'px');
		$('#balloon').css('top',height-50+'px');
		if( first == true){
			$('#balloon').css('left','0px');
			$('#balloon').css('top',height-170+'px');
			first = false;
		}

		$('#balloon').animate({left:width-50+'px',top:-sheight+'px'},43500,"linear",function(){
			lrflag = 0;
			setTimeout(moveBalloon,7500);
		});
    }else{
		$('#balloon').css('left',width-40+'px');
		$('#balloon').css('top',height-50+'px');
		$('#balloon').animate({left:-swidth+50+'px',top:-sheight+'px'},43500,"linear",function(){
			lrflag = 1;
			setTimeout(moveBalloon,3000);
		});
    }
}
function moveBalloon_b(){
    var width = $(window).width();
    var height = $(window).height();
    var swidth = $('#balloon_b').width();
    var sheight = $('#balloon_b').height();
    if( !lrflag_b ){
		$('#balloon_b').css('left',-swidth+40+'px');
		$('#balloon_b').css('top',height-50+'px');
		if( first_b != true){
			$('#balloon_b').css('left','0px');
			$('#balloon_b').css('top',height-170+'px');
			first_b = true;
		}

		$('#balloon_b').animate({left:width-50+'px',top:-sheight+'px'},43500,"linear",function(){
			lrflag_b = 1;
			setTimeout(moveBalloon_b,7500);
		});
    }else{
		$('#balloon_b').css('left',width-40+'px');
		$('#balloon_b').css('top',height-50+'px');
		$('#balloon_b').animate({left:-swidth+50+'px',top:-sheight+'px'},43500,"linear",function(){
			lrflag_b = 0;
			setTimeout(moveBalloon_b,3000);
		});
    }
}
function moveBalloon_c(){
    var width = $(window).width();
    var height = $(window).height();
    var swidth = $('#balloon').width();
    var sheight = $('#balloon').height();
    if( lrflag_c ){
		$('#balloon_c').css('left',-swidth+40+'px');
		$('#balloon_c').css('top',height-50+'px');
		if( first_c == true){
			$('#balloon_c').css('left','0px');
			$('#balloon_c').css('top',height-170+'px');
			first_c = false;
		}

		$('#balloon_c').animate({left:width-50+'px',top:-sheight+'px'},43500,"linear",function(){
			lrflag_c = 0;
			setTimeout(moveBalloon_c,7500);
		});
    }else{
		$('#balloon_c').css('left',width-40+'px');
		$('#balloon_c').css('top',height-50+'px');
		$('#balloon_c').animate({left:-swidth+50+'px',top:-sheight+'px'},43500,"linear",function(){
			lrflag_c = 1;
			setTimeout(moveBalloon_c,3000);
		});
    }
}
function moveRandom(){

    if( (($(window).height()-$('#balloon').offset().top) < ($('#balloon').height() / 2)) || 
	($('#balloon').offset().top < -($('#balloon').height() / 2)) ){
	return;
    }
    if( rand == '' || rand < 0 ){
	rand = Math.random()*randfac;
    }else{
	rand = Math.random()*-randfac;
    }
    $('#balloon').animate({'margin-left':rand+'px'},{queue:false,duration:randspeed});
}
function moveRandom_b(){ 

    if( (($(window).height()-$('#balloon_b').offset().top) < ($('#balloon_b').height() / 2)) || 
	($('#balloon_b').offset().top < -($('#balloon_b').height() / 2)) ){
	return;
    }
    if( rand == '' || rand < 0 ){
	rand = Math.random()*randfac;
    }else{
	rand = Math.random()*-randfac;
    }
    $('#balloon_b').animate({'margin-left':rand+'px'},{queue:false,duration:randspeed});
}
function moveRandom_c(){ 

    if( (($(window).height()-$('#balloon_c').offset().top) < ($('#balloon_c').height() / 2)) || 
		($('#balloon_b').offset().top < -($('#balloon_c').height() / 2)) ){
		return;
    }
    if( rand == '' || rand < 0 ){
		rand = Math.random()*randfac;
    }else{
		rand = Math.random()*-randfac;
    }
    $('#balloon_c').animate({'margin-left':rand+'px'},{queue:false,duration:randspeed});
}

