/* ----------------------------
   home.js
---------------------------- */

/* ----------------------------
	 Smart Phone
---------------------------- */
//if(navigator.userAgent.indexOf('Android') != -1 || navigator.userAgent.indexOf('iPhone') != -1 || navigator.userAgent.indexOf('iPod') != -1){
/*	if(loadCookie('smpflg')){
		location.href = 'http://www.mistral-cosme.com/smp/';
		makesmpflg('0');
	} else {*/
	
	
	function makesmpflg(smpdata){
    if(smpdata){
        //有効期限
        c_date = new Date(); 
        n = c_date.getTime() + 1000*60*60*24*365;
        c_date.setTime(n); 
        kigen = c_date.toGMTString();
        userDomain = location.hostname.replace(/www\./,"");

        //cookieの書き出し
        document.cookie = "smpflg="+smpdata+"; domain="+userDomain+"; path=/; expires=" + kigen;    }
}

function loadCookie(loadValue){
    if(loadValue){
        var c_data = document.cookie + ";";
        c_data = unescape(c_data);
        var n_point = c_data.indexOf(loadValue);
        var v_point = c_data.indexOf("=",n_point) + 1;
        var end_point = c_data.indexOf(";",n_point);
        if(n_point > -1){
            c_data = c_data.substring(v_point,end_point);
            return c_data;
        }/**/
    }
}

var spsdomain = 'http://' + location.hostname;
var spssmpdomain = spsdomain + '/smp/';
var ref       = location.href;

if ((navigator.userAgent.indexOf('iPhone') > 0 && navigator.userAgent.indexOf('iPad') == -1) || navigator.userAgent.indexOf('iPod') > 0 || navigator.userAgent.indexOf('Android') > 0) {
    if(loadCookie('smpflg')){
			if(loadCookie('smpflg') == 1){
									var Url = null;
									// desc 
									if((ref.match(/^(http):\/\/([0-9a-z\.\-:]+?)\/SHOP\/[0-9a-zA-Z\-\_]+?\.html$/))!= null){
											var Array  = ref.split("\/");
											var ItemNo = Array[4].split("\.html");
											var Url = spssmpdomain + "item/" + ItemNo[0] + ".html";
	
									// group
									}else if(ref.match(/^(http):\/\/([0-9a-z\.\-:]+?)\/SHOP\/(g{1}[0-9]+?)\/list\.html$/) != null ){
											 var Array = ref.split("\/");
											 var groupId = Array[4].match( /\d+/g );
											 Url    = spssmpdomain + "list.php?type=class&group=" + groupId;
	
									// category
									}else if(ref.match(/^(http):\/\/([0-9a-z\.\-:]+?)\/SHOP\/([0-9\-\_\/]+?)\/list\.html$/) != null ){
											var Array   = ref.split("\/");
											var mcatId  = Array[4];
											var scatId  = Array[5];
											
											if(scatId  != 'list.html'){
													Url    = spssmpdomain + "list.php?type=class&scat=" + scatId;
											}else{
													Url    = spssmpdomain + "list.php?type=class&mcat=" + mcatId;
											}
										
									}
									if( Url == null){
												 Url     = spssmpdomain;
									}
	
					document.write('<link href="' + spssmpdomain + 'css/pcsmp.css" rel="stylesheet" type="text/css">');
					document.write('<div class="shiny-blue"><a href="' + Url + '" onclick="makesmpflg(\'0\')">スマートフォン版はこちら</a></div>');
			}else{
				 makesmpflg('0');
				 location.href = spssmpdomain;
			}
		} else {
				makesmpflg('0');
				 location.href = spssmpdomain;
		}
}
/*}*/




	
//}

/* ----------------------------
 		Main Visual
---------------------------- */
function mainVisual(){
	var _target = $('#contents .main .stage ul');
	var _speed = 9000;
	var tgObj = [];
	var offset = $('#container').offset();
	var timer;
	
	function init(){
		var l = $('li',_target).length;
		var m = l * 3 - l
		$('li',_target).each(function(){
			tgObj.push($(this));
		});
		
		var ct = 0;
		for(var i = 0; i < m; i++){
			tgObj[ct].clone().appendTo(_target);
			ct += 1;
			if(ct >= l) ct = 0;
		}
		move();
	}
	
	function move(){
		auto();
		_target.animate({
			left: '-=420px'
		},_speed,'linear',function(){
			if($(this).offset().left <= -420){
				var ol = _target.offset().left;
				$('li:first',this).appendTo(this).parent().css({left:ol + 420});
			}
		});
	}
	
	function auto(){
		timer = setTimeout(function(){
			move();
		},_speed);
	}
	
	$('#contents .main .stage ul').offset({left:offset.left});	
	init();
}

/* ----------------------------
 		Main Visual Resize ( for IE6 )
---------------------------- */
function mainVisualResize(){
	var _target = $('#contents .stage');
	var winWidth = function(){return $(window).width()};
	
	$(window).load(function(){

			$('#contents .stage').width(winWidth());

	});
	
	$(window).resize(function(){
		if(winWidth() > _target.width()){
  		$('#contents .stage').width(winWidth());	
		}
	});
	
}

/* ----------------------------
 		News
---------------------------- */
function newsTicker(){
	var _target = $('#contents .news ul');
	var _data = 'update/news/news.xml';
	var _delay = 5000;
	var _speed = 500;
	var _limit = 5;
	var timer;
	
	function init(){
		$.ajax({
			url: _data,
			dataType: 'xml',
			success: function(data){
				$('item',data).each(function(n){
					if(n == _limit){ return false; }
					
					var date = $('date',this).text();
					var text = $('text',this).text();
					text = text.replace(/\r\n/g, "<br />");
					text = text.replace(/(\n|\r)/g, "<br />");
					var url = $('url',this).text();
					var new_icon = $('new_icon',this).text() == 'yes' ? true : false;
					var win = $('window',this).text() == 'yes' ? true : false;
					$('<li><p class="date">' + date + '</p><p class="text">' + text +'</p></li>').appendTo(_target).hide();
					
					if(new_icon) $('li:last',_target).addClass('new');
					if(url){
						$('li:last .text',_target).wrapInner('<a href="' + url + '"></a>');
						if(win) $('li:last .text a',_target).attr('target','_blank');
					}
					
				});
				$('li:first',_target).show();
				auto();
			}
		});	
	}
	
	function fade(){
		$('li:first',_target).fadeOut(_speed,function(){
			$(this).appendTo(_target);
			$('li:first',_target).fadeIn(_speed,function(){
				auto();
			});
		});
	}
	
	function auto(){
		timer = setTimeout(function(){
			fade();
		},_delay);
	}
	init();
}

$(function(){
	mainVisual();
	newsTicker();
});
