/// コンテンツ共有画面

//名前空間用のオブジェクトを用意する
var SHARE = {};

SHARE.contentId = "";
SHARE.contentTitle = "";

// Init function of page
$(document).ready(function () {

	//カレンダー表示
	//rome(dlgContentShare_validStartDate, { time: false });
	//rome(dlgContentShare_validEndDate, { time: false });

/*
	$('#dlgContentShare').prop('title', I18N.i18nText('txtShare'));
	$('#dlgContentShare').dialog( {

		open: function() {
			$('#dlgContentShare_maxDlCount').focus();

			$('#dlgContentShare_error').hide();
			$('#dlgContentShare_input').show();
			$('#dlgContentShare_result').hide();
			//OKボタンの有効化
			$(":button:contains('Ok')").prop("disabled", false).removeClass("ui-state-disabled");
		},
		close: function() {
			//flag = false;
		},
		autoOpen: false,
		modal: true,
		width: 370,
		buttons: {
			"Ok": function() {
				//submitForm();
				SHARE.sendContentShare();
			},
			"Cancel": function() {
				$( this ).dialog( "close" );
			}
		}
	});
*/

});

// Show content detail
SHARE.openContentShare = function() {

	//alert("SHARE.contentId:" + SHARE.contentId);
	//$('#dlgContentShare').dialog( 'open' );
	if($('#dlgContentShare_password').val() != ""){
		$('#dlgContentShare_password').val("");
	}
	
	if($('#dlgContentShare_validStartDate').val() != ""){
		$('#dlgContentShare_validStartDate').val("");
	}
	
	if($('#dlgContentShare_validEndDate').val() != ""){
		$('#dlgContentShare_validEndDate').val("");
	}
	
	if($('#dlgContentShare_maxDlCount').val() != ""){
		$('#dlgContentShare_maxDlCount').val("");
	}

	//bughuntでクリックイベントが溜まる問題の対応
	//$("#dlgContentShare_close").click(SHARE.dlgContentShare_close_Click);
	//$("#dlgContentShare_ok").click(SHARE.dlgContentShare_ok_Click);
	$("#dlgContentShare_close").on({
		'click touchend': function(ev){
			SHARE.dlgContentShare_close_Click(ev);
			return false;
		},
		'touchstart touchmove': function(){
			return false;
		}
	});

	$("#dlgContentShare_ok").on({
		'click touchend': function(ev){
			SHARE.dlgContentShare_ok_Click(ev);
			return false;
		},
		'touchstart touchmove': function(){
			return false;
		}
	});
	
	COMMON.lockLayout();

	$("#dlgContentShare").css('z-index', 102);
	$("#dlgContentShare").show();

	$('#dlgContentShare_maxDlCount').focus();
	$('#dlgContentShare_error').hide();
	$('#dlgContentShare_input').show();
	$('#dlgContentShare_result').hide();

	$("#dlgContentShare").center();
	if ($("#dlgContentShare").height() > $(window).height()){
		$("#dlgContentShare").css('top', '0');
	}

	//return false;
};


SHARE.dlgContentShare_close_Click = function(e){

	e.preventDefault();
	if ($('#contentDetail').is(':visible')) {
		// 表示されている場合の処理
	} else {
		// 非表示の場合の処理
		COMMON.unlockLayout();
	}

	$("#dlgContentShare").hide();

};

SHARE.dlgContentShare_ok_Click = function(){

	SHARE.sendContentShare();

};

SHARE.sendContentShare = function() {

	var validStartDate = $('#dlgContentShare_validStartDate').val();
	var validEndDate = $('#dlgContentShare_validEndDate').val();
	var maxDlCount = $('#dlgContentShare_maxDlCount').val();
	var password = $('#dlgContentShare_password').val();

	var validStartDateParam;
	var validEndDateParam;
	if(validStartDate != ''){
		validStartDateParam = validStartDate + " 00:00:00";
	}
	if(validEndDate != ''){
		validEndDateParam = validEndDate + " 23:59:59";
	}

	//console.log("validStartDate:" + validStartDateParam);
	//console.log("validEndDate:" + validEndDateParam);
	//console.log("maxDlCount:" + maxDlCount);
	//console.log("password:" + password);

	var params = {
			sid: ClientData.userInfo_sid(),
			contentId: SHARE.contentId,
			validStartDate: validStartDateParam,
			validEndDate: validEndDateParam,
			maxDlCount: maxDlCount,
			password: password
		};

	// Get content detail
	AVWEB.avwCmsApi(ClientData.userInfo_accountPath(), "getReaderShareUrl", "POST", params,
			function (data) {

				if( data.httpStatus == '200' ){
					$('#dlgContentShare_error').hide();
					$('#dlgContentShare_input').hide();
					$('#dlgContentShare_resultUrl').text(data.url);

					var mailto = 'mailto:?subject=コンテンツを共有しました&body=コンテンツを共有しました。%0D%0A%0D%0A共有URL:%0D%0A' + data.url + '%0D%0A%0D%0A';
					if( password != '' ){
						mailto = mailto + "password:" + password + '%0D%0A%0D%0A';
					}
					if( validStartDate != '' || validEndDate != '' ){
						mailto = mailto + I18N.i18nText('txtUrlValidDate') + validStartDate + "-" + validEndDate + '%0D%0A%0D%0A';
					}

					$('#dlgContentShare_resultMl').attr("href",mailto);

					$('#dlgContentShare_resultFb').click(function () {
						var fbUrl="https://www.facebook.com/sharer.php?u=" + data.url + "&t=test";
						window.open(encodeURI(decodeURI(fbUrl)),
							      'sharewindow',
							      'width=550, height=450, personalbar=0, toolbar=0, scrollbars=1, resizable=1'
							      );
						return false;
					});

					$('#dlgContentShare_resultTw').click(function () {
						var twUrl="http://twitter.com/intent/tweet?text=&url=" + data.url + "&via=Agentec_now";
						window.open(encodeURI(decodeURI(twUrl)),
							      'sharewindow',
							      'width=550, height=450, personalbar=0, toolbar=0, scrollbars=1, resizable=1'
							      );
						return false;

					});

					if( password != '' ){
						$('#dlgContentShare_resultPassword').show();
						$('#dlgContentShare_resultPassword').text("Password:" + password);
					} else {
						$('#dlgContentShare_resultPassword').hide();
					}

					if( validStartDate != '' || validEndDate != '' ){
						$('#dlgContentShare_resultDeliveryTerm').show();
						$('#dlgContentShare_resultDeliveryTerm').text(I18N.i18nText('txtUrlValidDate') +":" + validStartDate + I18N.i18nText('txtDashMark') + validEndDate);
					} else {
						$('#dlgContentShare_resultDeliveryTerm').hide();
					}

					//結果表示
					$('#dlgContentShare_result').show();
					//OKボタンの無効化
					//$(":button:contains('Ok')").prop("disabled", true).addClass("ui-state-disabled");

				} else {
					$('#dlgContentShare_error').show();console.log(data.errorMessage);
					$('#dlgContentShare_error').html(data.errorMessage);
				}
			},
			function (xhr, statusText, errorThrown) {
				if (xhr.responseText && xhr.status != 0) {
					var xhrData = JSON.parse(xhr.responseText);
					$('#dlgContentShare_error').show();
					$('#dlgContentShare_error').html(xhrData.loginErrorMessage);
				}
				else {
					AVWEB.showSystemError();
				}
			}
	);

};