var $ = jQuery.noConflict();
if (!window.console){
    window.console = {
        log: function(){}
    };
}
/*var catID= '';
var pageID= '';
var siteURL="/";*/


//クッキー設定
var Cookie = {
    get: function(name) {
        var match = ('; ' + document.cookie + ';').match('; ' + name + '=(.*?);');
        return match ? decodeURIComponent(match[1]) : '';
    },
    set: function(name, value, expires, domain, path, secure) {
        var buffer = name + '=' + encodeURIComponent(value);
        if (typeof expires != 'undefined') buffer += '; expires=' + new Date(expires).toUTCString();
        if (typeof domain != 'undefined') buffer += '; domain=' + domain;
        if (typeof path != 'undefined') buffer += '; path=' + path;
        if (secure) buffer += '; secure';
        document.cookie = buffer;
    }
};

var exp = new Date();
exp.setMonth(exp.getMonth() + 2);

var exp2 = new Date();
exp2.setMonth(exp2.getMonth() -5);

//クッキーセット
function changeSiteMode(val){
	//alert("クッキーにより"+val+"モード");
	var scookie = document.cookie;
	//document.cookie = document.cookie.replace(/sitemode=.*?;/,"");
	//Cookie.set('sitemode', '', exp2, undefined, '/');
	Cookie.set('sitemode', val, exp, undefined, '/');
	var url = location.href;
	if(val == 'html'){
		url = url.replace('#/aboutus/-1','aboutus/アクセスマップ');
		url = url.replace('#/access/-1','access/アクセスマップ2');
		url = url.replace(/#\/.+\//g,"?p=");
		url = url.replace(/#\//g,"");
		url = url.replace(/#/g,"");
		//alert(url)
	}else{
		if(catID){
			url = siteURL+'#/'+catID+pageID;
		}else{
			url = siteURL;
		}
		//alert(url)
	}
	document.location =url;
	return false;
}

/*ロード後実行
=========================================================================================================*/

jQuery(function(){
//ex_lb_set('http://www.cizen.jp/wp/wp-content/uploads/2011/06/fuji-gennsou.jpg');
/*$('body').hide(0,function(){
$('body').show(15000,function(){
	ex_lb_click('http://www.cizen.jp/wp/wp-content/uploads/2011/06/fuji-gennsou.jpg');
})})*/
	//Cookie.set('wp-settings-1', '', exp2, undefined, undefined);
	//alert("scookie "+document.cookie);
	if (Cookie.get('sitemode') == 'html' || $.os.name == 'iphone' || $.os.name == 'ipad' || $.os.name == 'ipod'){
		htmlVer();
	}else{
		if($("#top").length){
			swfVer();
			}else{
			if(catID != ''){
				document.location = siteURL+'#/'+catID+pageID;
			}else{
				document.location = siteURL;
			}
		}
	}
    
    //$('')
});
//HTML版
function htmlVer(){
	
	/*CSS設定
	-----------------------------------------------------------------------------------------------------*/
	if($.os.name != 'iphone' && $.os.name != 'ipad' && $.os.name != 'ipod'){
		$("#wrap").css('overflow','hidden');
		$("#content").css('position','absolute');
	}else{
		$('html,body').css('overflow','inherit');
	}
	$("#footer").css('position','absolute');
	
	if($.browser.name == 'msie'){
		$("#content").css('max-width','none');
	}
	

	/*アニメーション
	-----------------------------------------------------------------------------------------------------*/
	$("#content").css({right:-$(this).width()+'px',opacity:0}).animate({ right: "0",opacity:1},800,"easeOutCubic");

	/*ナビゲーション関係
	-----------------------------------------------------------------------------------------------------*/
	$('#header #nav ul ul').hide();
	$('#header #nav ul li.cat').each(function(){
		_submenu($('a',this),"300");
	});
	
	$('#header #nav a[rel="subm"]').click(function(){
			_submenu($(this),300);
		return false;
		
	});
	
	function _submenu(tgt,speed){
		tgt.parent().find('ul').slideToggle(speed ,function(){_resize();});
	}
    
    $('a[rel="gallery"]').click(function(){
        galleryPopup(this.href);
		return false;
	});

	/*カラー設定
	-----------------------------------------------------------------------------------------------------*/
	function _color(){		
		var hsb_h = Math.floor(Math.random()*360);
		var ballast = 0;
		if(20 < hsb_h && hsb_h < 210){
			ballast = 20;
		}
		if(35 < hsb_h && hsb_h < 125){
			ballast += 10+Math.abs(80 - hsb_h)/4;
		}else if(125 < hsb_h && hsb_h < 215){
			ballast += 10+Math.abs(170 - hsb_h)/4;
		}
		var hsb_s = 80+Math.floor(Math.random()*20);
		var hsb_b = 45+Math.floor(Math.random()*40-ballast);
		
		hsb_s = Math.round(hsb_s*2.55);
		hsb_b = Math.round(hsb_b*2.55);
		
		var rgb = HSVtoRGB(hsb_h,hsb_s,hsb_b);
		//console.log(hsb_h,hsb_s,hsb_b)
		//console.log(rgb)
		var colorset = 'rgb('+rgb.r+','+rgb.g+','+rgb.b+')';
		$('#header h1,#content,footer').css('background-color',colorset);
		$('#header #nav ul li,#header #nav ul li a').css('color',colorset);
		
		
	}
	
	//リファラーでサイト内の移動時のみ色変更
	if(document.referrer.indexOf('cizen.jp') > -1){
		_color();
	}

	/*リサイズ後実行
	-----------------------------------------------------------------------------------------------------*/

	var _delay = (function(){
	  var timer = 0;
	  return function(callback, ms){
		clearTimeout (timer);
		timer = setTimeout(callback, ms);
	  };
	})();
	jQuery(window).resize(function() {
		_delay(function(){
		 _resize();
		}, 300);
	});
	
	var _h = [];
	function _resize() {

		$("#content").css("height","auto");
		_h.b = $("body").height();
		_h.co = $("#content").height();
		_h.he = $("header").height();
		_h.ttl = $("#entryTtl").height();
		if(_h.co < _h.he){
			_h.m = _h.he;
		}else{
			_h.m = _h.co;
		}
		
		
		if($.os.name != 'iphone' && $.os.name != 'ipad' && $.os.name != 'ipod'){
			if(_h.m < _h.b){
				_h.m = _h.b;
			}	
			$("#content").css("height",_h.m-40+"px");
			$("#entry").css("height",_h.b-40-60-_h.ttl+"px");
		}else if(_h.m < _h.b){
			$("#content").css("height",_h.b-46+"px");
		}
	}
	_resize();
}

//swf版
function swfVer(){
	$('body').addClass('flash');
	$('#wrap').empty();
	$('#wrap').html('<div id="flashinner"><p>最新の Adobe Flash Player にアップデートしてください<br /><a href="http://www.adobe.com/go/getflashplayer"><img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Get Adobe Flash player" /></a></p><a href="javascript:void(0);" onClick="changeSiteMode(\'html\'); return false;">HTML Version</a></div>');
	var flashvars = {
		path_l:siteURL,
		path_g:siteURL
	};
	var params = {};
		params.wmode = "transparent";
	var attributes = {
		id: "flashcontent",
		name: "flashcontent"
	};

	swfobject.embedSWF(siteURL+"swf/ver0_3.swf", "wrap", "100%", "100%", "10.0.0", siteURL+"swf/expressInstall.swf", flashvars, params, attributes);
	//swfforcesize
	var obj = document.getElementById("flashcontent");
	if (obj) {
		var forcesize = new SWFForceSize( obj,950,550 );
	}
	
}


/*
via http://d.hatena.ne.jp/ja9/20100903/1283504341
*/
/**
 * HSV配列 を RGB配列 へ変換します
 *
 * @param   {Number}  h         hue値        ※ 0～360の数値
 * @param   {Number}  s         saturation値 ※ 0～255 の数値
 * @param   {Number}  v         value値      ※ 0～255 の数値
 * @return  {Object}  {r, g, b} ※ r/g/b は 0～255 の数値
 */
function HSVtoRGB (h, s, v) {
  var r, g, b; // 0..255

  while (h < 0) {
    h += 360;
  }

  h = h % 360;

  // 特別な場合 saturation = 0
  if (s == 0) {
    // → RGB は V に等しい
    v = Math.round(v);
    return {'r': v, 'g': v, 'b': v};
  }

  s = s / 255;

  var i = Math.floor(h / 60) % 6,
      f = (h / 60) - i,
      p = v * (1 - s),
      q = v * (1 - f * s),
      t = v * (1 - (1 - f) * s)

  switch (i) {
    case 0 :
      r = v;  g = t;  b = p;  break;
    case 1 :
      r = q;  g = v;  b = p;  break;
    case 2 :
      r = p;  g = v;  b = t;  break;
    case 3 :
      r = p;  g = q;  b = v;  break;
    case 4 :
      r = t;  g = p;  b = v;  break;
    case 5 :
      r = v;  g = p;  b = q;  break;
  }

  return {'r': Math.round(r), 'g': Math.round(g), 'b': Math.round(b)};
}


function ex_lb_set(u){
	$('body').append('<a class="ex_lb" href="'+u+'" rel="lightbox"></a>');
}

function ex_lb_click(u){
//$('a[href="'+u+'"].ex_lb').click();
	//window.open(u);
	//return false;
	//alert(u);
	//var win=window.open(u, 'game', 'width=500, height=400, menubar=no, toolbar=no, scrollbars=yes,resizable=no,status=no');
	//win.focus();
	//return false;
}


function galleryPopup(url){
	var win=window.open(url, 'gallery', 'width=910, height=620, menubar=no, toolbar=no, scrollbars=yes,resizable=no,status=no');
	win.focus();
	return false;
};



