/**
 * yukinosho
 */
jQuery.noConflict();
var j$ = jQuery;
var base_url = '';

j$(document).ready(function() {

	base_url = j$("span#base_url").text();

	if ( j$("#dateinput").size() > 0 ) {
		j$("#dateinput").datepicker();
	}

	if ( j$("#slide-wedding").size() > 0 ) {
		j$("#slide-wedding").slider({time: 12,speed: 5});
	}

	if ( j$("#map_canvas").size() > 0 ) {
		draw_map();
	}

	if ( j$("#catalog_box_list").size() > 0 ) {
	    get_catalog_box_list();
	}

	box_get();

	j$("input#btn_box_add").click(function() {
	    var id = j$("span#web_contract_id").text();
	    box_add(id);
	});

	j$("input#btn_zip").click(function() {
        var zip1 = j$("input#zip1").val();
        var zip2 = j$("input#zip2").val();
        searchZip(zip1, zip2, 'address');
    });

	j$("input#btn_zip_sub").click(function() {
        var zip1 = j$("input#zip_sub1").val();
        var zip2 = j$("input#zip_sub2").val();
        searchZip(zip1, zip2, 'address_sub');
    });
	if ( j$(".photoTam").size() > 0 ) {
        j$(".photoTam").jCarouselLite({
            btnNext: ".next",
            btnPrev: ".prev",
            vertical: false,
            hoverPause:true,
            mouseWheel: true,
            visible: 5,
            speed:400,
            scroll:1
        });
	}
	if ( j$("a[rel='colorbox_yukinosho']").size() > 0 ) {
	    var i     = 0;
	    var hrefs = new Array();
	    if ( j$(".centTam").size() > 0 ) {
	        j$(".centTam").children("a[rel='colorbox_yukinosho']").each(function() {
	            var href = j$(this).attr('href');
	            var flag = false;
	            for (j = 0; j < hrefs.length; j++) {
	                if (hrefs[j] == href) {
	                    flag = true;
	                }
	            }
	            if (flag) {
	                j$(this).attr('rel', '');
	                j$(this).attr('href', '#');
	            } else {
	                hrefs[i] = href;
	                i++;
	            }
	        });
	    }
	    j$("a[rel='colorbox_yukinosho']").colorbox({transition:"fade", speed:400, width:800, height:800});
	}
	if ( j$("a[rel='colorbox_yukinosho_main']").size() > 0 ) {
	    j$("a[rel='colorbox_yukinosho_main']").colorbox({transition:"fade", speed:400});
	}
	for (i=1; i<=10; i++) {

	    key = "#calendar_photo_"+i;
	    if ( j$(key).size() > 0 ) {
	        j$(key+" .calendar_photo_carousel").jCarouselLite({
	            btnNext: key+" .next",
	            btnPrev: key+" .prev",
	            vertical: false,
	            hoverPause:true,
	            mouseWheel: true,
	            visible: 5,
	            speed:400,
	            scroll:1
	        });
	    }

	    key = "a[rel='colorbox_calendar"+i+"']";
	    if ( j$(key).size() > 0 ) {
	        j$(key).colorbox({transition:"fade", speed:400, width:800, height:700});
	    }
	}

    member_get();
    bookmark_get();
    history_get();
});

function box_add(id) {
    j$.post(base_url+'catalog/box_add', {n:id}, function(xml) {
        var status = j$(xml).find("status").text();
        if (status == 1) {
            box_get();
            get_catalog_box_list();
        } else {
            location.href = base_url+'catalog';
        }
    });
}

function box_del(id) {
	j$.post(base_url+'catalog/box_del', {n:id}, function() {
		box_get();
		get_catalog_box_list();
		var expr = '#'+id;
		if ( j$(expr).size() > 0 ) {
		    j$(expr).closest('span').children('a:eq(0)').attr('class', 'jqTransformCheckbox');
		}
	});
}

function box_get() {

    var catalog_box = '<p class="cSubHonbun wid192 mt05">会員限定メニューです</p>';
    j$("div#boxCatalog").html(catalog_box);

    j$.post(base_url+'catalog/box_get', {f:0,n:5}, function(xml) {
        var status = j$(xml).find("status").text();
        if (status == 1) {
            catalog_box = '<ul class="catalogboxUl">';
            j$(xml).find("catalog").each(function() {

                catalog_box += '<li>';
                catalog_box += '<a href="'+j$(this).find("url").text()+'">';
                catalog_box += j$(this).find("client_name").text();
                catalog_box += '</a>';
                catalog_box += '<a href="javascript:void(0);" onclick="box_del(';
                catalog_box += "'"+j$(this).find("web_contract_id").text()+"'"+');">';
                catalog_box += '<img src="'+base_url+'images/gomi.png"  alt="削除" class="ml05" />';
                catalog_box += '</a>';
                catalog_box += '</li>';
            });
            catalog_box += '</ul>';
            catalog_box += '<a href="'+base_url+'catalog"><img src="'+base_url+'images/catalogRequest.png" alt="資料請求する" class="mt10" /></a>';
            j$("div#boxCatalog").html(catalog_box);
        }
    });
}

function searchZip(zip1, zip2, name)
{
    var zip    = zip1+zip2;
    var state  = null;
    var city   = null;
    var addr   = null;
    var result = '住所が検索できませんでした';
    if (! zip) {
        alert('郵便番号が未入力です');
    } else {
    	j$.post(base_url+"zip_proxy.php", {zn:zip}, function(xml) {
            j$(xml).find("value").each(function() {
                if (j$(this).attr("state") != undefined) {
                    state = j$(this).attr("state");
                }
                if (j$(this).attr("city") != undefined) {
                    city = j$(this).attr("city");
                }
                if (j$(this).attr("address") != undefined) {
                    addr = j$(this).attr("address");
                }
            });
            if ((state) && (city) && (addr)) {
                result = state+city+addr;
            }
            j$("input#"+name).val(result);
        });
    }
}

function draw_map() {

	var lat   = j$("span#map_lat").text();
	var lng   = j$("span#map_lng").text();
	var title = j$("span#map_title").text();
	var zip   = j$("span#map_zip").text();
	var addr  = j$("span#map_addr").text();
	var tel   = j$("span#map_tel").text();

	var centerLat = parseFloat(lat)+0.001;
	var myLatlng = new google.maps.LatLng(lat, lng);
    var centerLatlng = new google.maps.LatLng(centerLat, lng);
	var myOptions = {
      zoom: 16,
      center: centerLatlng,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    }
    var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);

    var contentString = '<div><b>'+title+'</b><br/>〒'+zip+'<br/>'+addr+'<br/>'+tel+'</div>';
    var infowindow = new google.maps.InfoWindow({
        content: contentString
    });

    var marker = new google.maps.Marker({
        position: myLatlng,
        map: map,
        title: title
    });
    /*
    infowindow.open(map, marker);
    google.maps.event.addListener(marker, 'click', function() {
        infowindow.open(map, marker);
    });
    */
}

function member_get() {

    var subplantitle_box = '会員限定メニュー';
    j$("div#boxSubPlanTitle").html(subplantitle_box);

    j$("#btnBookmarkAdd").hide();
    j$("#linkBookmarkView").hide();

    j$("div#boxPreLogin").show();
    j$("div#boxAfterLogin").hide();
    j$("div#boxAfterLoginLink").hide();
    j$("div#boxReviewInsert").hide();

    j$.post(base_url+'member/get', {}, function(response) {
        var text = new String(response);
        if (text.length) {
            var tmps = text.split(":");
            var status           = tmps[0];
            var subplantitle_box = tmps[1]+' さんの<br />ブライダルプラン';
            var catalog_cnt      = tmps[2];
            var fair_cnt         = tmps[3];

            if (status == 1) {
                j$("div#boxSubPlanTitle").html(subplantitle_box);

                j$("div#boxPreLogin").hide();
                j$("div#boxAfterLogin").show();
                j$("div#boxAfterLoginLink").show();
                j$("#linkBookmarkView").show();

                if ((j$("#page_title").size() > 0) && (j$("#page_url").size() > 0)) {
                    j$("#btnBookmarkAdd").show();
                }
                if (j$("#review_insert").size() > 0) {
                    j$("div#boxReviewInsert").show();
                }
                j$("#catalog_cnt").html(catalog_cnt);
                j$("#fair_cnt").html(fair_cnt);
            }
        }
    }, 'text');
}

function bookmark_get() {

    var bookmark_box = '<p class="cSubHonbun wid192 mt05">会員限定メニューです</p>';
    j$("div#boxBookmark").html(bookmark_box);

    j$.post(base_url+'bookmark/get', {}, function(xml) {
        var status = j$(xml).find("status").text();
        if (status == '1') {
            bookmark_box = '<ul class="checkUl">';
            j$(xml).find("bookmark").each(function() {
                bookmark_box += '<li>';
                bookmark_box += '<a href="'+j$(this).find("url").text()+'">';
                bookmark_box += j$(this).find("title").text();
                bookmark_box += '</a>';
                bookmark_box += '</li>';
            });
            bookmark_box += '</ul>';
            j$("div#boxBookmark").html(bookmark_box);
        }
    });
}

function bookmark_add() {

    if ((j$("#page_title").size() == 0) ||
        (j$("#page_url").size() == 0)) {
        return;
    }

    var title = j$("#page_title").text();
    var url   = j$("#page_url").text();

    j$.post(base_url+'bookmark/add', {t:title,u:url}, function(xml) {
        var message = j$(xml).find("message").text();
        if (message) {
            alert(message);
        }
        bookmark_get();
    });
}

function history_get() {

    var title       = '';
    var url         = '';
    var history_box = '<p class="cSubHonbun wid192 mt05">会員限定メニューです</p>';

    if ((j$("#page_title").size() > 0) &&
        (j$("#page_url").size() > 0)) {
        title = j$("#page_title").text();
        url   = j$("#page_url").text();
    }

    j$("div#boxHistory").html(history_box);

    j$.post(base_url+'history/get', {t:title,u:url}, function(xml) {
        var status = j$(xml).find("status").text();
        if (status == '1') {

            history_box = '<ul class="checkUl">';
            j$(xml).find("history").each(function() {
                history_box += '<li>';
                history_box += '<a href="'+j$(this).find("url").text()+'">';
                history_box += j$(this).find("title").text();
                history_box += '</a>';
                history_box += '</li>';
            });
            history_box += '</ul>';
            j$("div#boxHistory").html(history_box);
        }
    });
}

function get_catalog_box_list()
{
    if ( j$("#catalogList").size() == 0 ) {
        return;
    }
    if ( j$("#boxCatalogRequestButton").size() > 0 ) {
        j$('#boxCatalogRequestButton').hide();
    }

    j$('#catalogList').empty();
    j$.post(base_url+'catalog/box_get', {}, function(xml) {
        var status = j$(xml).find("status").text();
        if (status == 1) {

            j$('#catalogList').append('<thead><tr>'
                    +'<th class="cL01">写真</th>'
                    +'<th class="cL02">エリア</th>'
                    +'<th class="cL03">カテゴリ</th>'
                    +'<th class="cL04">企業、会場、店舗</th>'
                    +'<th class="cL05"></th>'
                    +'</tr></thead>');
            j$('#catalogList').append('<tbody>');
            j$(xml).find("catalog").each(function() {

                var web_contract_id = j$(this).find("web_contract_id").text();
                var catalog_notice  = '';
                var image_tag       = '';
                if (j$(this).find("catalog_flag").text() == '0') {
                    catalog_notice = ' <font color="#f00a81">※資料準備中です</font>';
                }
                if (j$(this).find("src")) {
                    image_tag = '<img src="'+j$(this).find("src").text()+'"'
                                +' width="'+j$(this).find("width").text()+'"'
                                +' height="'+j$(this).find("height").text()+'" />';
                }

                j$('#catalogList').append('<tr>'
                    +'<td class="cL01"><div class="centTam50"><center>'
                    +image_tag
                    +'</center></div></td>'
                    +'<td class="cL02">'+j$(this).find("area_wide_name").text()+'</td>'
                    +'<td class="cL03">'+j$(this).find("category_name").text()+'</td>'
                    +'<td class="cL04">'+j$(this).find("client_name").text()+catalog_notice+'</td>'
                    +'<td class="cL05">'
                    + '<a href="javascript:void(0);" onclick="box_del('
                    + "'"+web_contract_id+"'"+');">'
                    + '<img src="'+base_url+'images/gomi.png"  alt="削除" class="ml05" />'
                    + '</a>'
                    +'</td>'
                    +'</tr>');
            });
            j$('#catalogList').append('</tbody>');
            if ( j$("#boxCatalogRequestButton").size() > 0 ) {
                j$('#boxCatalogRequestButton').show();
            }
        }
    });
}

function set_catalog_box(obj, id)
{
    if (obj.checked) {
        box_del(id);
    } else {
        box_add(id);
        obj.setAttribute("id", id);
    }
}

//{{{ clear_input_file
/*
 * input type="file" の入力値クリア
 *
 * @param  {string} クリアに使用するオブジェクトのID
 * @return {void}
 */
function clear_input_file(objectId)
{
    var obj  = document.getElementById(objectId);
    var html = obj.innerHTML;
    obj.innerHTML = html;
}

// }}}
//{{{ change_calendar_photo_selection
/*
 * カレンダーの写真選択エリア表示変更
 *
 * @param  {number} 選択エリアID
 * @param  {number} 選択エリア数
 * @return void
 */
function change_calendar_photo_selection(id, cnt)
{
    for (i=1; i<=cnt; i++) {
        var key1 = 'div#calendar_photo_'+i;
        var key2 = 'a#calendar_photo_link_'+i;
        j$(key1).css('display', 'none');
        if (j$(key2).hasClass("dNStay")) {
            j$(key2).removeClass("dNStay");
        }
    }

    var key1 = 'div#calendar_photo_'+id;
    var key2 = 'a#calendar_photo_link_'+id;
    j$(key1).css('display', 'block');
    j$(key2).addClass("dNStay");
}

// }}}
// {{{ change_calendar_photo
/*
 * カレンダーの写真変更
 *
 * @param  {string} 写真番号
 * @param  {string} 写真ファイル名
 * @return void
 */
function change_calendar_photo(number, file)
{
    var key = '#preview_photo_'+number;
    var url = base_url+'images/review/m/'+file;
    j$(key).css('background-image', 'url('+url+')');
}

// }}}
// {{{ ハッピーレビュー表示件数変更
function change_review_limit(name)
{
    location.href = base_url+'review/'+j$("select[name='"+name+"']").val();
}

// }}}
//{{{ ハッピーレビュー表示件数変更 (絞り込み検索)
function change_review_limit_search(path, name)
{
    location.href = base_url+'review/search/'+path+j$("select[name='"+name+"']").val();
}

// }}}


