var bookmark_visible = false;
var login_visible = false;
var tickerctx = { 
	stopscroll: false,
	intervalhandle: null,
	animhandle: null,
	animheight: 0,
	animcount: 0,
	animlimit: 10,
	div1: null,
	div2: null,
	newscursor: 1
};

function tickeranimation() {
	if(tickerctx.animcount > tickerctx.animlimit) { 
		clearInterval(tickerctx.animhandle); 
		tickerctx.div1.html(tickerctx.div2.html());
		tickerctx.div1.css("top", "0px");
		tickerctx.div2.css("top", "0px");
		return; 
	}
	var top = -(tickerctx.animheight * (tickerctx.animcount / 10));
	tickerctx.div1.css("top", top + "px");
	tickerctx.div2.css("top", top + "px");
	tickerctx.animcount++;
}

function tickerinterval() {
	if(tickerctx.stopscroll) return;
	clearInterval(tickerctx.animhandle);
	
	tickerctx.newscursor++;
	var cursor = tickerctx.newscursor % newstickerJson.length;
	var w = tickerctx.div2.find("a").eq(0);
	
	w.attr("href",newstickerJson[cursor].url);
	w.html(newstickerJson[cursor].body);

	tickerctx.animheight = tickerctx.div1.height();
	tickerctx.animcount = 1;
	tickerctx.animhandle = setInterval(tickeranimation,50);
}
$(document).ready(function(){
	tickerctx.div1 = $("#tickerline1");
	tickerctx.div2 = $("#tickerline2")

	$("#tickerframe").mouseover(function(){tickerctx.stopscroll = true;});
	$("#tickerframe").mouseout(function(){tickerctx.stopscroll = false;});

	clearInterval(tickerctx.intervalhandle);
	tickerctx.intervalhandle = setInterval(tickerinterval,6000);
});


var j$ = jQuery;
jQuery(function () {
	
	function toolbar_open() {
		// メニューは隠す
		j$("#login_panel").hide();
		j$("#header-toolbar-bookmark").hide();
		
		// ツールバーのアニメーションの初期位置を設定
		j$("#header-toolbar-open").css("top", "0").show();
		j$("#header-toolbar").css("top", -j$("#header-toolbar").outerHeight()).show();
		
		// ツールバーをアニメーション
		j$("#header-toolbar-open").animate(
			{top: -$("#header-toolbar-open").outerHeight()},
			{complete: function () {
					j$("#header-toolbar").animate({top: 0});
				}
			}
		);
	
		// クッキーに状態を保存
		j$.cookie('header_toolbar_display', 'display', {path:'/'});
	}
	
	function toolbar_close() {
		// メニューは隠す
		j$("#login_panel").hide();
		j$("#header-toolbar-bookmark").hide();
		
		// ツールバーのアニメーションの初期位置を設定
		j$("#header-toolbar").css("top", "0").show();
		j$("#header-toolbar-open").css("top", -j$("#header-toolbar-open").outerHeight()).show();

		// ツールバーをアニメーション
		j$("#header-toolbar-open").show("normal", 
			function () {
			j$("#header-toolbar").animate(
				{top: -j$("#header-toolbar").outerHeight()},
				{complete:function () {
						j$("#header-toolbar-open").animate({top: 0});
					}
				}
			);
		});
		
		// クッキーに状態を保存
		j$.cookie('header_toolbar_display', 'none', {path:'/'});
	}

	// ツールバーの開閉状態をCookieから取得する
	var toolbar_display = j$.cookie('header_toolbar_display');
	
	// Cookieが存在しない場合は、表示する
	if (toolbar_display == null) {
		j$.cookie('header_toolbar_display', 'display', {path:'/'});
		toolbar_display = 'display';
	}

	// ツールバーを表示
	switch (toolbar_display) {
	case 'display' : 
		j$("#header-toolbar-open").hide();
		j$("#header-toolbar").show();
		j$("#header-toolbar").css("top", "0");
		break;
	case 'none' : 
		j$("#header-toolbar").hide();
		j$("#header-toolbar-open").show();
		j$("#header-toolbar-open").css("top", "0");
		break;
	}
	
	
	// 最小化ボタン
	j$("#menu_min").qtip({
		content: "<div>ツールバーを閉じる</div>",
		style: {
			name: 'dark',
			tip:'topLeft',
			fontSize:'12px',
			fontWeight:'bold'
		},
		position:{
			corner:{
				target:'bottomMiddle',
				tooltip:'topLeft'
			}
		}
	}).click(function () {
		toolbar_close();
	});
	
	// 最大化ボタン
	j$("#menu_max").qtip({
		content: "<div>ツールバーを開く</div>",
		style: {
			name: 'dark',
			tip:'topLeft',
			fontSize:'12px',
			fontWeight:'bold'
		},
		position:{
			corner:{
				target:'bottomMiddle',
				tooltip:'topLeft'
			}
		}
	}).click(function () {
		toolbar_open();
	});
	
	// メルマガ
	j$("#menu_mailmag").qtip({
		content:"<div>新着情報をメールで受け取る</div>",
		style:{
			name:'dark',
			tip:'topLeft',
			fontSize:'12px',
			fontWeight:'bold'
		},
		position:{
			corner:{
				target:'bottomMiddle',
				tooltip:'topLeft'
			}
		}
	});		
	// ブックマーク
	j$("#menu_bookmark").qtip({
		content:"<div>このページをブックマークする</div>",
		style:{
			name:'dark',
			tip:'topRight',
			fontSize:'12px',
			fontWeight:'bold'
		},
		position:{
			corner:{
				target:'bottomMiddle',
				tooltip:'topRight'
			}
		}
	});
	
	// ツイッター
	j$("#menu_twitter").qtip({
		content:"<div>twitterに投稿する</div>",
		style:{
			name:'dark',
			tip:'topRight',
			fontSize:'12px',
			fontWeight:'bold'
		},
		position:{
			corner:{
				target:'bottomMiddle',
				tooltip:'topRight'
			}
		}
	});
	
	// ブックマークボタン押下時のイベント
	j$("#id_menu_bookmark").click(function () {		
		if (bookmark_visible) {
			j$("#menu_bookmark").qtip('enable');
			bookmark_visible = false;	
			j$("#header-toolbar-bookmark").animate(
				{height:"toggle"},
				{complete:function() {
					j$("#shim1").width(0);
					j$("#shim1").height(0);
				}}
			);
		}
		else {
			if (login_visible) {
				j$("#login_panel").hide();
				j$("#shim2").width(0);
				j$("#shim2").height(0);
				login_visible = false;
			}
		
		
			j$("#menu_bookmark").qtip('hide');
			j$("#menu_bookmark").qtip('disable');
			j$("#header-toolbar-bookmark").animate(
				{height:"toggle"},
				{complete:function() {
					j$("#shim1").width(j$("#header-toolbar-bookmark").width());
					j$("#shim1").height(j$("#header-toolbar-bookmark").height());
				}}
			);
			bookmark_visible = true;
		}
	
	});
	
	
	// ログインボタン押下時のイベント
	j$("#id_btn_login").click(function () {
		if (login_visible) {
			j$("#login_panel").animate(
				{height:"toggle"},
				{complete:function() {
					j$("#shim2").width(0);
					j$("#shim2").height(0);
				}}
			);
			login_visible = false;
		}
		else {
			if (bookmark_visible) {
				j$("#header-toolbar-bookmark").hide();
				j$("#shim1").width(0);
				j$("#shim1").height(0);
				bookmark_visible = false;
			}
			j$("#login_panel").animate(
				{height:"toggle"},
				{complete:function() {
					j$("#shim2").width(j$("#login_panel").width());
					j$("#shim2").height(j$("#login_panel").height());
				}}
			);
			login_visible = true;
		}
	});
	
	// フォーカスされていたら、背景画像を消す
	$("#id_header_search").focus(function () {
		$(this).css({backgroundImage:'none'});
	}).blur(function() {
		if ($("#id_header_search").val() != "") {
			$("#id_header_search").css({backgroundImage:'none'});
		}
		else {
			$(this).css({backgroundImage:'url(/user_data/packages/default/img/toolbar/search_hdtb_bg.gif)'});
		}
	});

	// 値が入っていた場合は、背景画像を消す
	if ($("#id_header_search").val() != "") {
		$("#id_header_search").css({backgroundImage:'none'});
	}
	else {
		$("#id_header_search").css({backgroundImage:'url(/user_data/packages/default/img/toolbar/search_hdtb_bg.gif)'});
	}
	
});
function add_bookmark(url) {
	j$("#header-toolbar-bookmark").hide();
	window.open(url, "view");

	return false;
}
function bookmark_me() {
	j$("#header-toolbar-bookmark").hide();
	var url = 'http://surveyreport.planidea.jp/';
	var title = '［SurveyReport］マーケティングレポート資料・データ販売';

	if(navigator.userAgent.indexOf("Lunascape") >= 0){ 
		// lunascape
		alert('[Ctrl]キーを押しながら[G]キーを押してください。');
	}else if(window.sidebar) {
		// firefox
		window.sidebar.addPanel(title, url, "");
	}else if($.browser.msie) {
		window.external.AddFavorite(url, title);
		// IE 
	} else {
		alert('ブラウザが提供するブックマーク追加機能をご利用ください。');
	}

	return false;
}


