// dmp.co.jp JavaScript

//-------------------------------------
// 初期処理
//-------------------------------------
$(function(){
	
	var is_ajax = false;//,
//		loading = function(target){
//			target.append("<div class\"ajax_loading\">Loading....</div>");
//			return $(".ajax_loading", target);
//		};
	
	$.extend({
		ajaxPosts:function(params, before, after, check){
			
			var loading = null;
			
			if(is_ajax && check) return;
			is_ajax = true;
			
			if(before) before();

			$.ajax({
				type:"POST",
				url:"../cms/wp-admin/admin-ajax.php",
				data:params,
				success:function(data){ 
					is_ajax = false; 
					if(loading != null){ loading.remove(); }
					after(data); 
				},
				error:function(XMLHttpRequest, status, errorThrown){
					is_ajax = false;
				}
			});
		}
	});
	//---------------------------------------
	// Newsリスト取得
	//---------------------------------------
	$.ajaxPosts({
			action: "dmp_get_posts",
			category_name: "news",
			numberposts:"50",
			template:"../wp-content/themes/dmp-ltd/news_posts_template.php"
		},
		null, 
		function(data){
			var post_id = $("li", data).attr("id").replace("post_", "");
			$(".newslist .simpleSlide-tray").append(data);
			simpleSlide();
			showNewsPost(post_id);
		}, false);
	//---------------------------------------
	// Mobile Appsリスト取得
	//---------------------------------------
	$.ajaxPosts({
			action: "dmp_get_posts",
			category_name: "works-apps",
			numberposts:"60",
			template:"../wp-content/themes/dmp-ltd/works_posts_template.php"
		},
		null,
		function(data){
			$(".workslist .works_apps").append($(data).attr("rel", "works_apps_list"));
			simpleSlide();
		}, false);
	//---------------------------------------
	// Movieリスト取得
	//---------------------------------------
	$.ajaxPosts({
			action: "dmp_get_posts",
			category_name: "works-movie",
			numberposts:"60",
			template:"../wp-content/themes/dmp-ltd/works_posts_template.php"
		},
		null,
		function(data){
			$(".workslist .works_movie").append($(data).attr("rel", "works_movie_list"));
			simpleSlide();
		}, false);
	//---------------------------------------
	// Websiteリスト取得
	//---------------------------------------
	$.ajaxPosts({
			action: "dmp_get_posts",
			category_name: "works-website",
			numberposts:"60",
			template:"../wp-content/themes/dmp-ltd/works_posts_template.php"
		},
		null,
		function(data){
			$(".workslist .works_website").append($(data).attr("rel", "works_website_list"));
			simpleSlide();
		}, false);
});




//-------------------------------------
// ワークスコンテンツ取得＆表示
//-------------------------------------


function showWorkPost(post_id, obj){
	
	var className = $(obj).parent().parent().parent().attr("rel");
		$target   = $("." + className),
		moveTop   = $target.prev().offset().top;
	
	$($.browser.safari ? 'body' : 'html').stop(true, true).animate({scrollTop:moveTop}, 600, 'swing', function(){
		
		$.ajaxPosts({
				action: "dmp_get_post",
				id:post_id,
				template:"../wp-content/themes/dmp-ltd/works_post_template.php"
			},
			null,
			function(data){
			
				var appendData = function(data){
					$target.append($(data).css({height:"0px", overflow:"hidden"}));
					wrap_height = $(".works_inner_wrap", $target).height();
					$(".works_wrap", $target).animate({height:wrap_height}, 1000, "easeInOutQuart");
				}
				
				$($(data[2])).addClass(className);
				
				if($(".works_wrap", $target).length){
					$(".works_wrap", $target).animate({height:"0px"}, 800, "easeInOutQuart",
						function(){ 
							$(this).remove(); 
							appendData(data);
						});
				}else{
					appendData(data);
				}
			}, true);
	});
}


//-------------------------------------
// ワークスコンテンツクローズ
//-------------------------------------
function closeWorkPost(obj){
	$(".works_wrap").animate({height:"0px"}, 800, "easeInOutQuart", function(){ $(this).remove(); });
}

//-------------------------------------
// ニュースコンテンツ取得＆表示
//-------------------------------------
function showNewsPost(post_id){
	
	$.ajax({
		type:"POST",
		url:"../cms/wp-admin/admin-ajax.php",
		data:{
			action: "dmp_get_post",
			id:post_id,
			template:"../wp-content/themes/dmp-ltd/news_post_template.php"
		},
		success:function(data){
			$(".newsentry").empty();
			$(".newsentry").append($(data));
		}
	});
	
}

// GA
  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-3814351-7']);
  _gaq.push(['_trackPageview']);
  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();

// CSS
if (navigator.userAgent.indexOf('iPhone') != -1){
document.write('<meta name="viewport" content="width=900">');
document.write('<link rel="stylesheet" href="./css/ios-style.css">');
}else if (navigator.userAgent.indexOf('Android') != -1){
document.write('<meta name="viewport" content="width=900">');
document.write('<link rel="stylesheet" href="./css/ios-style.css">');
}else if (navigator.userAgent.indexOf('iPad') != -1){
document.write('<meta name="viewport" content="width=900">');
document.write('<link rel="stylesheet" href="./css/ios-style.css">');
}else{
document.write('<link rel="stylesheet" href="./css/style.css" media="screen">');
}


//-------------------------------------
// お正月用モーダルコンバット
//-------------------------------------
// $(function(){$("body").append('<div id="glayLayer"></div><div id="overLayer"><img src="img/ty_modal_2012.png"><img src="img/bt_modal_close.png" id="modal-nav"></div>') 
// $("#modal-nav").click(function(){
// $(this).hide() 
// $("#overLayer").hide()
// $("#glayLayer").hide()
// })
// if($.browser.msie && $.browser.version<7){
// $(window).scroll(function(){
// $("#glayLayer").css('top',$(document).scrollTop())
// $("#overLayer").css('top',($(document).scrollTop()+$(window).height()/2) +"px")
// })
// }
// })

