/**
 * @require jQuery 1.4.2+
 * @require constants.js
 * @require util.js
 * @require common.js
 * 
 * @version 1.0.0
 */
jQuery.js.detail = {};

/**
 * 物件をキープ
 */
jQuery.js.detail.keep = function() {
	
	// 初期処理 
	this.xhr = j$.ajax( {
		type : 'POST',
		url : j$.constants.url.keepCheck,
		data : 'value=' + j$('#hiddenValue').val(),
		success : function(response) {
			if (response) {
				if (response != '1') {
					j$('.saveKeep').addClass('clickable');
					j$('.saveKeep').find('img').attr('src', j$.constants.img.detailKeep);
					j$('.saveKeep').find('img').hover(function(){j$.js.rollover.over(this);}, function(){j$.js.rollover.out(this);});
				}
			}
		},
		error : function(xhr) {
			if (xhr.status != 0) {
				j$.js.common.overlay.show();
				if (xhr.status == 400) {
					j$.js.common.popup.showMessage(xhr.responseText);
				} else {
					j$.js.common.popup.showMessage('エラーが発生しました。');
				}
				j$.js.common.overlay.closeEvent();
			}
		}
	});
	
	// イベント
	j$('.saveKeep.clickable').live('click', function() {
		var target = j$(this);
		j$.js.common.overlay.show();
		j$.js.common.popup.loading();
		this.xhr = j$.util.cancelRequest(this.xhr);
		this.xhr = j$.ajax( {
			type : 'POST',
			url : j$.constants.url.keepSet,
			data : 'value=' + j$('#hiddenValue').val(),
			success : function(response) {
				if (response) {
					j$.js.common.keep.headerCount(response);
					j$('.saveKeep').find('img').attr('src', j$.constants.img.detailKept);
					j$('.saveKeep').removeClass('clickable');
					j$('.saveKeep').find('img').unbind('mouseover').unbind('mouseout');
					j$.js.common.popup.showMessage('キープ物件を追加しました。');
				} else {
					j$.js.common.popup.showMessage('キープ物件の追加に失敗しました。');
				}
				j$.js.common.overlay.closeEvent();
			},
			error : function(xhr) {
				if (xhr.status != 0) {
					if (xhr.status == 400) {
						j$.js.common.popup.showMessage(xhr.responseText);
					} else {
						j$.js.common.popup.showMessage('エラーが発生しました。');
					}
					j$.js.common.overlay.closeEvent();
				}
			}
		});
		return false;
	});
};

/**
 * 画像切り替え
 */
jQuery.js.detail.changeImage = function() {
	
//	j$('.changeImage').click(function() {
//		j$('#mainImage').attr('src', j$(this).find('img').attr('src'));
//		return false;
//	});
};

/**
 * 物件閲覧履歴保存
 */
jQuery.js.detail.articleHistorySet = function() {
	
	// 初期処理 
	this.xhr = j$.ajax( {
		type : 'POST',
		url : j$.constants.url.articleHistorySet,
		data : 'value=' + j$('#hiddenValue').val(),
		success : function(response) {
			// 処理なし
		},
		error : function(xhr) {
			// 処理なし
		}
	});
	
};

/** 初回読み込み処理 */
j$( function() {
	j$.js.detail.keep();
	j$.js.detail.changeImage();
	j$.js.common.loadPopup();
	j$.js.common.recommend(j$.constants.url.partsDetailRecommend, 'value=' + j$('#hiddenValue').val(), function(response) {
		if (!j$.js.common.str.isBlank(response)) {
			j$('#partsRecommendList').html(response);
			j$('#partsRecommend').show();
		}
	});
	j$.js.common.history(j$.constants.url.partsPropertyHistory, null, function(response) {
		if (!j$.js.common.str.isBlank(response)) {
			j$('#partsHistoryList').html(response);
			j$('#partsHistory').show();
		}
		j$.js.detail.articleHistorySet();
	});
	
    
	//j$.js.popupCarouselImage.openEvent('a.changeImage');

    j$("a[rel=image]").fancybox({
        'type'              : 'image',
        'transitionIn'		: 'none',
        'transitionOut'		: 'none',
        'titlePosition' 	: 'over',
        'padding'			: '0',
        'titleFormat'		: function(title, currentArray, currentIndex, currentOpts) {
            return '<span id="fancybox-title-over">' + (currentIndex + 1) + ' / ' + currentArray.length + (title.length ? ' &nbsp; ' + title : '') + '</span>';
        }
    });
    
});

