/// ===============================================================================================
/// Maker group [start]
/// ===============================================================================================

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

CONTENTVIEW_MAKER.ShowMaker = function(targetId){
	CONTENTVIEW_MAKER.maker_targetDiv = targetId;
    I18N.i18nReplaceText();
    $('#dlgMaker').show();
    $('#dlgMaker').draggable();
    CONTENTVIEW_MAKER.Maker_handleColorPickerEvent();
    $('#dlgMaker').center();
};

CONTENTVIEW_MAKER.touchStart_BtnOk_Maker = function(e){
    e.preventDefault();
    CONTENTVIEW_MAKER.Maker_dspOK_click();
};

CONTENTVIEW_MAKER.touchStart_BtnCancel_Maker = function(e){
    e.preventDefault();
    CONTENTVIEW_MAKER.Maker_dspCancel_click();
};

CONTENTVIEW_MAKER.Maker_handleColorPickerEvent = function (){    
    
    if(CONTENTVIEW_GENERAL.avwUserEnvObj.isIos() || CONTENTVIEW_GENERAL.avwUserEnvObj.isAndroid()){
        $('#dlgMaker .colorpicker').unbind('mouseenter');
        $('#dlgMaker .colorpicker').unbind('mouseleave');
        $('#makerColorwrapper').unbind('mouseleave');
        
        document.getElementById('Maker_btnOk').addEventListener('touchstart',CONTENTVIEW_MAKER.touchStart_BtnOk_Maker,false);
        document.getElementById('Maker_btnCancel').addEventListener('touchstart',CONTENTVIEW_MAKER.touchStart_BtnCancel_Maker,false); 
    }
    else{
        $('#dlgMaker .colorpicker').unbind('mouseenter');
        $('#dlgMaker .colorpicker').unbind('mouseleave');
        $('#makerColorwrapper').unbind('mouseleave');
        
        $('#dlgMaker .colorpicker').bind('mouseenter', CONTENTVIEW_MAKER.Maker_colorPickerMouseEnterFunction);
        $('#dlgMaker .colorpicker').bind('mouseleave', CONTENTVIEW_MAKER.Maker_colorPickerMouseLeaveFunction);
        $('#makerColorwrapper').bind('mouseleave', CONTENTVIEW_MAKER.Maker_colorWrapperMouseLeaveFunction);
    }
    
    //$('#Maker_btnOk').live('click', CONTENTVIEW_MAKER.Maker_dspOK_click);
    $("#Maker_btnOk").on({
    	'click touchend': function(ev){
    		CONTENTVIEW_MAKER.Maker_dspOK_click(ev);
    		return false;
    	},
    	'touchstart touchmove': function(){
    		//これを入れないと次にダイアログを開くと表示位置が大きくズレる
    		return false;
    	}
    });
    
    //$('#Maker_btnCancel').live('click', CONTENTVIEW_MAKER.Maker_dspCancel_click);
    $("#Maker_btnCancel").on({
    	'click touchend': function(ev){
    		CONTENTVIEW_MAKER.Maker_dspCancel_click(ev);
    		return false;
    	},
    	'touchstart touchmove': function(){
    		//これを入れないと次にダイアログを開くと表示位置が大きくズレる
    		return false;
    	}
    });

    CONTENTVIEW_MAKER.Maker_SetDefaultValue();

    //$('#marker-small-text').live('click', CONTENTVIEW_MAKER.markerSmallTextClick);
    $("#marker-small-text").on({
    	'click touchend': function(ev){
    		CONTENTVIEW_MAKER.markerSmallTextClick(ev);
    		return false;
    	},
    	'touchstart touchmove': function(){
    		//これを入れないと次にダイアログを開くと表示位置が大きくズレる
    		return false;
    	}
    });
    
    //$('#marker-medium-text').live('click', CONTENTVIEW_MAKER.markerMediumTextClick);
    $("#marker-medium-text").on({
    	'click touchend': function(ev){
    		CONTENTVIEW_MAKER.markerMediumTextClick(ev);
    		return false;
    	},
    	'touchstart touchmove': function(){
    		//これを入れないと次にダイアログを開くと表示位置が大きくズレる
    		return false;
    	}
    });
    
    //$('#marker-large-text').live('click', CONTENTVIEW_MAKER.markerLargeTextClick);
    $("#marker-large-text").on({
    	'click touchend': function(ev){
    		CONTENTVIEW_MAKER.markerLargeTextClick(ev);
    		return false;
    	},
    	'touchstart touchmove': function(){
    		//これを入れないと次にダイアログを開くと表示位置が大きくズレる
    		return false;
    	}
    });
    
    //$('#marker-oversize-text').live('click', CONTENTVIEW_MAKER.markerOversizeTextClick);
    $("#marker-oversize-text").on({
    	'click touchend': function(ev){
    		CONTENTVIEW_MAKER.markerOversizeTextClick(ev);
    		return false;
    	},
    	'touchstart touchmove': function(){
    		//これを入れないと次にダイアログを開くと表示位置が大きくズレる
    		return false;
    	}
    });
    
    $('#dlgMaker .colorpicker').die();
    $('#dlgMaker .colorpicker').live('click', CONTENTVIEW_MAKER.Maker_colorPickerClickFunction);
    //$("#dlgMaker .colorpicker").on({
    //	'click touchend': function(ev){
    //		CONTENTVIEW_MAKER.Maker_colorPickerClickFunction(ev);
    //		return false;
    //	},
    //	'touchstart touchmove': function(){
    //		//これを入れないと次にダイアログを開くと表示位置が大きくズレる
    //		return false;
    //	}
    //});
    
};

CONTENTVIEW_MAKER.markerSmallTextClick = function(){
    $('#maker-size-small').attr('checked','checked');
    $('#maker-size-medium').removeAttr('checked');
    $('#maker-size-large').removeAttr('checked');
    $('#maker-size-oversize').removeAttr('checked');
    CONTENTVIEW_MAKER.Maker_rdo1_click();
};

CONTENTVIEW_MAKER.markerMediumTextClick = function(){
    $('#maker-size-small').removeAttr('checked');
    $('#maker-size-medium').attr('checked','checked');
    $('#maker-size-large').removeAttr('checked');
    $('#maker-size-oversize').removeAttr('checked');
    CONTENTVIEW_MAKER.Maker_rdo2_click();
};

CONTENTVIEW_MAKER.markerLargeTextClick = function(){
    $('#maker-size-small').removeAttr('checked');
    $('#maker-size-medium').removeAttr('checked');
    $('#maker-size-large').attr('checked','checked');
    $('#maker-size-oversize').removeAttr('checked');
    CONTENTVIEW_MAKER.Maker_rdo3_click();
};

CONTENTVIEW_MAKER.markerOversizeTextClick = function(){
    $('#maker-size-small').removeAttr('checked');
    $('#maker-size-medium').removeAttr('checked');
    $('#maker-size-large').removeAttr('checked');
    $('#maker-size-oversize').attr('checked','checked');
    CONTENTVIEW_MAKER.Maker_rdo4_click();
};

// Choose type of eraser, and draw to canvas
CONTENTVIEW_MAKER.Maker_chooseType = function(typeValue, color) {
    var fillColor;
    CONTENTVIEW_MAKER.maker_Size = typeValue;
    var canvas = document.getElementById('Maker_cvPreviewmaker');
    
    if(color == null || color == 'undefined'){
        fillColor = "f8ff00";
    }
    else{
        fillColor = '#'+ color;
    }
    
    if (canvas.getContext) {
        
        // use getContext to use the canvas for drawing
        var ctx = canvas.getContext('2d');

        // Draw shapes
        ctx.clearRect(0, 0, canvas.width, canvas.height);
        
        ctx.fillStyle = fillColor;
        ctx.strokeStyle = fillColor;
        ctx.fillRect((eval(canvas.width)/2) - (eval(typeValue)/2), (eval(canvas.height)/2) - (eval(typeValue)/2), typeValue, typeValue);
    }
};

// Set default value for maker.
CONTENTVIEW_MAKER.Maker_SetDefaultValue = function() {
    var typeValue = undefined;
    var makerColor = ClientData.maker_color();
    typeValue = ClientData.maker_size();
    CONTENTVIEW_MAKER.maker_Size = typeValue;
    
    if(makerColor != null || makerColor != undefined){
    	CONTENTVIEW_MAKER.maker_chooseColor = makerColor;
    }
    else{
    	CONTENTVIEW_MAKER.maker_chooseColor = 'f8ff00';
        makerColor = 'f8ff00';
    }
    
    if (typeValue == 5) {
        $("#maker-size-small").attr('checked', 'checked');
        $("#maker-size-small").focus();
    }
    else if (typeValue == 12.5) {
        $("#maker-size-medium").attr('checked', 'checked');
        $("#maker-size-medium").focus();
    }
    else if (typeValue == 25) {
        $("#maker-size-large").attr('checked', 'checked');
        $("#maker-size-large").focus();
    }
    else if (typeValue == 50) {
        $("#maker-size-oversize").attr('checked', 'checked');
        $("#maker-size-oversize").focus();
    }
    else {
    	CONTENTVIEW_MAKER.maker_Size = 10;
        $("#maker-size-small").attr('checked', 'checked');
        $("#maker-size-small").focus();
    }
     
    //START FIXCSS
    //$('#dlgMaker .colorpicker[colorId="'+makerColor+'"]').css('border','1px solid #e6e6e6');
    $('#dlgMaker .colorpicker[colorId="'+makerColor+'"]').addClass('picker-choose-on');
    $('#dlgMaker .colorpicker[colorId="'+makerColor+'"]').removeClass('picker-choose-off');
    //END FIXCSS
    CONTENTVIEW_MAKER.Maker_chooseType(CONTENTVIEW_MAKER.maker_Size, makerColor);
};

CONTENTVIEW_MAKER.Maker_colorPickerMouseLeaveFunction = function() {
    //START FIXCSS
    $(this).addClass('picker-choose-off');
    $(this).removeClass('picker-choose-on');
    $('#dlgMaker .colorpicker[colorId="'+CONTENTVIEW_GENERAL.makerColor+'"]').addClass('picker-choose-on');
    $('#dlgMaker .colorpicker[colorId="'+CONTENTVIEW_GENERAL.makerColor+'"]').removeClass('picker-choose-off');
    //END FIXCSS
};

CONTENTVIEW_MAKER.Maker_colorPickerMouseEnterFunction = function() {
    //START FIXCSS
    //$(this).css('border','1px solid #e6e6e6');
    $(this).removeClass('picker-choose-off');
    $(this).addClass('picker-choose-on');
    //END FIXCSS
    var color = $(this).attr('colorId');
    CONTENTVIEW_MAKER.Maker_chooseType(CONTENTVIEW_MAKER.maker_Size, color);
};

CONTENTVIEW_MAKER.Maker_colorPickerClickFunction = function() {
    //START FIXCSS
    //$('#dlgMaker .colorpicker[colorId="' + CONTENTVIEW_MAKER.maker_chooseColor + '"]').css('border', '1px solid white');
    //$(this).css('border','1px solid #e6e6e6');
    $('#dlgMaker .colorpicker[colorId="' + CONTENTVIEW_MAKER.maker_chooseColor + '"]').addClass('picker-choose-off');
    $('#dlgMaker .colorpicker[colorId="' + CONTENTVIEW_MAKER.maker_chooseColor + '"]').removeClass('picker-choose-on');
    $(this).addClass('picker-choose-on');
    $(this).removeClass('picker-choose-off');
    //END FIXCSS
    CONTENTVIEW_MAKER.maker_chooseColor = $(this).attr('colorId');
    CONTENTVIEW_MAKER.Maker_chooseType(CONTENTVIEW_MAKER.maker_Size, CONTENTVIEW_MAKER.maker_chooseColor);
};

CONTENTVIEW_MAKER.Maker_rdo1_click = function() {
	CONTENTVIEW_MAKER.Maker_chooseType(5, CONTENTVIEW_MAKER.maker_chooseColor);  
};
CONTENTVIEW_MAKER.Maker_rdo2_click = function() {
	CONTENTVIEW_MAKER.Maker_chooseType(12.5, CONTENTVIEW_MAKER.maker_chooseColor);
};
CONTENTVIEW_MAKER.Maker_rdo3_click = function() {
	CONTENTVIEW_MAKER.Maker_chooseType(25, CONTENTVIEW_MAKER.maker_chooseColor);
};
CONTENTVIEW_MAKER.Maker_rdo4_click = function() {
	CONTENTVIEW_MAKER.Maker_chooseType(50, CONTENTVIEW_MAKER.maker_chooseColor);
};

// Event of button OK
CONTENTVIEW_MAKER.Maker_dspOK_click = function() {

    // Set selected value to local storage
    var typeValue = undefined;

    if ($("#maker-size-small").attr('checked') == 'checked') {
        typeValue = 5;
    }
    else if ($("#maker-size-medium").attr('checked') == 'checked') {
        typeValue = 12.5;
    }
    else if ($("#maker-size-large").attr('checked') == 'checked') {
        typeValue = 25;
    }
    else if ($("#maker-size-oversize").attr('checked') == 'checked') {
        typeValue = 50;
    }

    if(ClientData.maker_color() != CONTENTVIEW_MAKER.maker_chooseColor){
    	CONTENTVIEW_GENERAL.imgMarkerTemp = CONTENTVIEW_GENERAL.canvas_marker.toDataURL("image/png;");
    
        var img = new Image();
        img.onload = function () {
        	CONTENTVIEW_GENERAL.context_draw.drawImage(img, 0, 0, CONTENTVIEW_GENERAL.canvas_draw.width, CONTENTVIEW_GENERAL.canvas_draw.height);
        };
        img.src = CONTENTVIEW_GENERAL.imgMarkerTemp;    
        
        CONTENTVIEW_GENERAL.context_marker.clearRect(0, 0, CONTENTVIEW_GENERAL.canvas_marker.width, CONTENTVIEW_GENERAL.canvas_marker.height);
        
        ClientData.maker_color();
    }
    // Set value to local
    ClientData.maker_size(typeValue);
    ClientData.maker_color(CONTENTVIEW_MAKER.maker_chooseColor);    
    CONTENTVIEW_MAKER.maker_Size = typeValue;
    CONTENTVIEW_GENERAL.makerColor = CONTENTVIEW_MAKER.maker_chooseColor;
    
    CONTENTVIEW_MAKER.maker_targetDiv.hide();
    
    /*enable button */
    CONTENTVIEW_MARKING.enableButtonMarking();
    ClientData.IsAddingMarking(true);
    CONTENTVIEW_GENERAL.isAddingMarking = ClientData.IsAddingMarking();
    CONTENTVIEW_GENERAL.makerSize = ClientData.maker_size();
    CONTENTVIEW_GENERAL.makerColor = ClientData.maker_color();
    
    /* unlock dialog overlay */
    $("#overlay").hide();
    $('#dlgMarking_imgMakerOption').removeClass();
    $('#dlgMarking_imgMakerOption').addClass('maker_option_hover');
};

// Event of button cancel
CONTENTVIEW_MAKER.Maker_dspCancel_click = function() {

	CONTENTVIEW_MAKER.maker_targetDiv.hide();
    /*enable button */
	CONTENTVIEW_MARKING.enableButtonMarking();
    ClientData.IsAddingMarking(true);
    CONTENTVIEW_GENERAL.isAddingMarking = ClientData.IsAddingMarking();
    
    /* unlock dialog overlay */
    $("#overlay").hide();
    
    $('#dlgMarking_imgMakerOption').removeClass();
    $('#dlgMarking_imgMakerOption').addClass('maker_option_hover');
};

CONTENTVIEW_MAKER.Maker_colorWrapperMouseLeaveFunction = function() {
	CONTENTVIEW_MAKER.Maker_chooseType(CONTENTVIEW_MAKER.maker_Size, CONTENTVIEW_MAKER.maker_chooseColor);
    //START FIXCSS
    //$('#dlgMaker .colorpicker[colorId="' + CONTENTVIEW_MAKER.maker_chooseColor + '"]').css('border', '1px solid #e6e6e6');
    $('#dlgMaker .colorpicker[colorId="' + CONTENTVIEW_MAKER.maker_chooseColor + '"]').addClass('picker-choose-on');
    $('#dlgMaker .colorpicker[colorId="' + CONTENTVIEW_MAKER.maker_chooseColor + '"]').removeClass('picker-choose-off');
    //END FIXCSS
};

$(function () {
	//CONTENTVIEW_MAKER.ready();
});

CONTENTVIEW_MAKER.ready = function(){
	CONTENTVIEW_MAKER.maker_Size = undefined;
	CONTENTVIEW_MAKER.maker_targetDiv = undefined;
	CONTENTVIEW_MAKER.maker_chooseColor = undefined;
};

/// ===============================================================================================
/// Maker group [ end ]
/// ===============================================================================================