$(function(){
//バナー

$.ajax({
url: 'common/files/prj_banner.xml',
success: setBanner,
error: function(){
alert("error")
}
});

//メッセージ
//$("ul#ticker").liScroll();
//setScrollWrap()

$.ajax({
url: 'common/files/prj_msg.xml',
success: setMSG
});

//アンケート投票
$('#vote').click(function(){
return voteEnquete('ticker');
});

//産直マップ
/*
$('#block_map').flash({
    swf : "/common/files/map.swf",
    height: 340,
    width: 810,
    id: 'map',
    name: 'maps',
    wmode: 'transparent'
  })
  */
  //キーワード
  $('#block_keyword > ul > li > a').click(function(){
  /*
  var name = $(this).attr('id').split("_").pop();
  setModal('/words/' + name + '.html');
  */
  setModal($(this).attr('href'), 540);
  return false;
  })
  
});


/**
 * バナープログラム
 */

var intervalId
var prjArr;
var intarvalArr;

function createAncImg(href, src, id){
var elm;
if(href != null){
elm = $('<a href="'+href+'" id="'+id+'"><img src="'+src+'" /></a>');
}else{
elm = $('<a id="'+id+'"><img src="'+src+'" /></a>');
}
return elm;
}

function setBanner(xml){
prjArr = new Array()
//alert($(xml).find('banner').length);
$(xml).find('banner').each(function(){
//ボタンの生成
var obj = {
id: $(this).attr('id'),
thumb: $(this).attr('thumb'),
img: $(this).attr('src'),
href:$(this).attr('href')
}
var a = createAncImg(obj.href, obj.thumb, "prj_"+obj.id);
a.click(onClickBanner);if($.fn.pngfix){
a.children("img[src$=png]").load(function(){
$(this).pngfix();
})
}
var li = $("<li />").append(a);

a.hover(
function(){
if($(this).hasClass('selected') && $(this).attr('href') == 'javascript:void(0)'){
$(this).css('cursor', 'default');
return;
}
$(this).css('cursor', 'pointer');
var id = $(this).attr('id').split('_').pop();
$(this).parent('li').append('<img src="common/img/home/hover.gif" class="hover_click" id="click_'+id+'" />');
},
function(){
var id = $(this).attr('id').split('_').pop();
$('#click_'+id).remove();
})

a.click(function(){
if($(this).hasClass('selected') && $(this).attr('href') == 'javascript:void(0)'){
var id = $(this).attr('id').split('_').pop();
$('#click_'+id).remove();
$(this).css('cursor', 'default');
}
});

prjArr[parseInt(obj.id, 10)] = obj;
$("#banner_li").append(li);
})

/*
prj_arr = $("#banner_li > li > a");
prj_arr.each(function(){
$(this).click(onClickBanner);
})
*/
intarvalArr = prjArr.concat(new Array());
intervalId = setTimeout("autoSlideBanner()", 3000);
//alert($(xml).find('sanchoku').attr("start"))
//カートのhover
$.hoverImage($('#prj_12 > img'));

setBannerImage($(xml).find('sanchoku').attr("start"));

}

function onClickBanner(){
//すでに選択状態だったらリンク先に飛ぶ
if($(this).hasClass("selected")){
return true;
}
//それ以外
var id = $(this).attr("id").split("_").pop();

setBannerImage(id);

//リンク飛ばないように
return false;
}

function setBannerImage(id){

clearTimeout(intervalId);
//とりあえず、全部元に戻す
$("#banner_li > li > a").each(function(){
if($(this).hasClass("selected")){
$(this).removeClass("selected");

if($(this).attr('id') == "prj_12"){
$('#prj_12 > img').unbind('mouseout');
$('#prj_12 > img').unbind('mouseover');
$.hoverImage($('#prj_12 > img'));
}
}
$('#prj_12 > img').swapImage("_on.gif", ".gif");
})

var obj = prjArr[parseInt(id, 10)];
obj.href = obj.href == "javascript:void(0)" ? null : obj.href;
var anc = createAncImg(obj.href, obj.img);
var img = anc.children(0);

img.ready(function(){
img.fadeIn("slow", function(){
var bros = img.prev();
bros.remove();
});
});

$('#box_viewer').append(anc);

$("#prj_" + id).addClass("selected");

if(id == "12"){
//alert(id)
$('#prj_12 > img').swapImage(".gif", "_on.gif");
$('#prj_12 > img').unbind('mouseout');
$('#prj_12 > img').unbind('mouseover');
}

//タイマー
intervalId = setTimeout("autoSlideBanner()", 10000)
}

function autoSlideBanner(){
//alert("autoSlideBanner")
var rnd = Math.floor(Math.random() * (intarvalArr.length - 1)) + 1;

rnd = rnd < 10 ? "0"+rnd : rnd;

setBannerImage(rnd);
}

/**
 * スクロールメッセージ
 */
var scroller;

function setMSG(xml){
var msgs = $(xml).find('message');

msgs.each(function(){
var href = Boolean($(this).attr('href')) ? '><a href="'+$(this).attr('href')+'"' : '';
var tgt = Boolean($(this).attr('target')) ? ' target="' + $(this).attr('target') + '"' : '';
var li = '<li'+href+tgt+'><img src="common/img/home/icon_hotmsg2.gif" alt="" />'+$(this).text()+'</a></li>';
$('#newsticker').append(li);
});

setScrollWrap()
}

function setScrollWrap(){
var w = 0;
$('#newsticker > li').each(function(){
w += $(this).outerWidth() * 1.2;
})
$('#newsticker').css({width:w, cursor:'pointer'});
/*
$('#newsticker').click(function(){
location.href = "enquete/";

})
*/
$('#newsticker').hover(function(){
scroller.stop();
},
function(){
scrollMsg();
})

scrollMsg()
}

function scrollMsg(){
var li = $('#newsticker > li:first');
li.each(function(){

var left = $(this).outerWidth() * -1.2;
var param ={};
param.duration = (left - parseInt($(this).css('marginLeft'), 10)) * -20;
param.complete = nextScroll;
param.easing = "linear";
param.queue = false;

scroller = $(this).animate({marginLeft: left}, param);
})
}

function nextScroll(){
$('#newsticker > li:first').remove().css('marginLeft', 0).appendTo('#newsticker');
scrollMsg();
}

/**
 * 産直マップ
 */
var restartFlag = false;

function setPopUp(flaId){
//alert(flaId);
/*
document.boxForm.boxId.value = flaId;
popWin = window.open("pop.html","pop","width=400,height=300");
*/
var modalparam = {
onClose:function(){
$.modal.close();
closedFnc();
}
}

setModal('/enquete/' + getMemberPage(flaId) + '/member.html', 540, modalparam);

restartFlag = true;
}

function setPopUpAll(){
//alert("「メッセージをすべて見る」がクリックされました。")
setModal('/enquete/members.html', 540);
restartFlag = true; 
}

function closedFnc(){
if(restartFlag){
var flashobj;
if( navigator.appName.indexOf( "Microsoft") != -1) {
flashobj = window.map;
} else {
flashobj = document.maps;
}
flashobj.callFlash("start",true);
}
}
function getMemberPage(id){
var obj ={

























118:"misawa",





170:"nakanishi",





225:"isogawa",







181:"isono",







139:"sato",





175:"yoshiba",





234:"doi",







179:"masubuti",





177:"miyazaki",





236:"oda",







138:"onozuka",





171:"yamashiro",







173:"kenji",





172:"yamamoto",





227:"yamamoto_a",







116:"iwabuchi",







133:"kawahara",





183:"hiraga",







112:"tozawa",





140:"saito",







176:"hiyama",





137:"sugihara",





135:"sugiyama",





136:"matsumoto",





232:"hayashi",







117:"kaji",





115:"yokota",





169:"higa",





134:"ishizuka",







182:"ishihama",





110:"ishizawa",





113:"nishiyama",





114:"suwa",







107:"toyoshita",





119:"suzuki",





229:"abe",







178:"aotani",





174:"iizumi",







180:"katori",





0:""
}
return obj[id];
}
