/// ===============================================================================================
/// Pen group [start]
/// ===============================================================================================

var pen_Size;
var pen_Color;
var pen_targetDiv;
var pen_chooseColor;

function ShowPen(targetId) {
    pen_targetDiv = targetId;
	i18nReplaceText();
	$('#dlgPen').show();
	$('#dlgPen').draggable({ revert: false});
	Pen_handleColorPickerEvent();
			
	$('#dlgPen').center();		
};

function touchStart_BtnOk_Pen(e){
	e.preventDefault();
	pen_targetDiv.draggable("destroy");
	
	Pen_dspOK_click();
};

function touchStart_BtnCancel_Pen(e){
	e.preventDefault();
	pen_targetDiv.draggable("destroy");
	
	Pen_dspCancel_click();
};

function Pen_handleColorPickerEvent() {
 
	if(avwUserEnvObj.os == "ipad" || avwUserEnvObj.os == "android"){
	 	$('#dlgPen .colorpicker').unbind('mouseenter');
    	$('#dlgPen .colorpicker').unbind('mouseleave');
    	$('#penColorwrapper').unbind('mouseleave');
    	 document.getElementById('Pen_btnOk').addEventListener('touchstart',touchStart_BtnOk_Pen,false);
    	 document.getElementById('Pen_btnCancel').addEventListener('touchstart',touchStart_BtnCancel_Pen,false);
	}
	else{
		
	 	$('#dlgPen .colorpicker').unbind('mouseenter');
    	$('#dlgPen .colorpicker').unbind('mouseleave');
    	$('#penColorwrapper').unbind('mouseleave');
    	
	 	$('#dlgPen .colorpicker').bind('mouseenter', Pen_colorPickerMouseEnterFunction);
    	$('#dlgPen .colorpicker').bind('mouseleave', Pen_colorPickerMouseLeaveFunction);
    	$('#penColorwrapper').bind('mouseleave', Pen_colorWrapperMouseLeaveFunction);
	}
    
	//$('#Pen_btnOk').live('click', Pen_dspOK_click);
    $("#Pen_btnOk").on({
    	'click touchend': function(ev){
    		Pen_dspOK_click(ev);
    		return false;
    	},
    	'touchstart touchmove': function(){
    		//これを入れないと次にダイアログを開くと表示位置が大きくズレる
    		return false;
    	}
    });
	
	//$('#Pen_btnCancel').live('click', Pen_dspCancel_click);
    $("#Pen_btnCancel").on({
    	'click touchend': function(ev){
    		Pen_dspCancel_click(ev);
    		return false;
    	},
    	'touchstart touchmove': function(){
    		//これを入れないと次にダイアログを開くと表示位置が大きくズレる
    		return false;
    	}
    });
	

	Pen_SetDefaultValue();
	
	//$('#pen-small-text').live('click', penSmallTextClick);
    $("#pen-small-text").on({
    	'click touchend': function(ev){
    		penSmallTextClick(ev);
    		return false;
    	},
    	'touchstart touchmove': function(){
    		//これを入れないと次にダイアログを開くと表示位置が大きくズレる
    		return false;
    	}
    });
	
	
	//$('#pen-medium-text').live('click', penMediumTextClick);
    $("#pen-medium-text").on({
    	'click touchend': function(ev){
    		penMediumTextClick(ev);
    		return false;
    	},
    	'touchstart touchmove': function(){
    		//これを入れないと次にダイアログを開くと表示位置が大きくズレる
    		return false;
    	}
    });
	
	//$('#pen-large-text').live('click', penLargeTextClick);
    $("#pen-large-text").on({
    	'click touchend': function(ev){
    		penLargeTextClick(ev);
    		return false;
    	},
    	'touchstart touchmove': function(){
    		//これを入れないと次にダイアログを開くと表示位置が大きくズレる
    		return false;
    	}
    });
	
	//$('#pen-oversize-text').live('click', penOversizeTextClick);
    $("#pen-oversize-text").on({
    	'click touchend': function(ev){
    		penOversizeTextClick(ev);
    		return false;
    	},
    	'touchstart touchmove': function(){
    		//これを入れないと次にダイアログを開くと表示位置が大きくズレる
    		return false;
    	}
    });

	//$('#dlgPen .colorpicker').live('click', Pen_colorPickerClickFunction);
    $("#dlgPen .colorpicker").on({
    	'click touchend': function(ev){
    		Pen_colorPickerClickFunction(ev);
    		return false;
    	},
    	'touchstart touchmove': function(){
    		//これを入れないと次にダイアログを開くと表示位置が大きくズレる
    		return false;
    	}
    });
	
	
};

function penSmallTextClick(){
	$('#pen-size-small').attr('checked','checked');
	$('#pen-size-medium').removeAttr('checked');
	$('#pen-size-large').removeAttr('checked');
	$('#pen-size-oversize').removeAttr('checked');
	Pen_rdo1_click();
};

function penMediumTextClick(){
	$('#pen-size-small').removeAttr('checked');
	$('#pen-size-medium').attr('checked','checked');
	$('#pen-size-large').removeAttr('checked');
	$('#pen-size-oversize').removeAttr('checked');
	Pen_rdo2_click();
};

function penLargeTextClick(){
	$('#pen-size-small').removeAttr('checked');
	$('#pen-size-medium').removeAttr('checked');
	$('#pen-size-large').attr('checked','checked');
	$('#pen-size-oversize').removeAttr('checked');
	Pen_rdo3_click();
};

function penOversizeTextClick(){
	$('#pen-size-small').removeAttr('checked');
	$('#pen-size-medium').removeAttr('checked');
	$('#pen-size-large').removeAttr('checked');
	$('#pen-size-oversize').attr('checked','checked');
	Pen_rdo4_click();
};



// Choose type of eraser, and draw to canvas
function Pen_chooseType(typeValue, color) {
	var fillColor;
	pen_Size = typeValue;
	var canvas = document.getElementById('Pen_cvPreviewPen');

	if (color == null || color == 'undefined') {
		fillColor="#fe0000";
	}
	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.beginPath();

        ctx.arc(canvas.width/2, canvas.height/2, typeValue, 0, 2 * Math.PI, true);
        ctx.fillStyle = fillColor;
        ctx.strokeStyle = fillColor;
        ctx.fill();
        ctx.stroke();
    }
};

// Set default value for Pen.
function Pen_SetDefaultValue() {
    var typeValue = undefined;
    var penColor = ClientData.penOpt_color();
    typeValue = ClientData.penOpt_size();
    pen_Size = typeValue;
    
    if(penColor != null || penColor != undefined){
    	pen_chooseColor = penColor;
    }
    else{
    	pen_chooseColor = 'fe0000';
    	penColor = 'fe0000';
    }
    
    if (typeValue == 5) {
        $("#pen-size-small").attr('checked', 'checked');
        $("#pen-size-small").focus();
    }
    else if (typeValue == 12.5) {
        $("#pen-size-medium").attr('checked', 'checked');
        $("#pen-size-medium").focus();
    }
    else if (typeValue == 25) {
        $("#pen-size-large").attr('checked', 'checked');
        $("#pen-size-large").focus();
    }
    else if (typeValue == 50) {
        $("#pen-size-oversize").attr('checked', 'checked');
        $("#pen-size-oversize").focus();
    }
    else {
        typeValue = 5;
        $("#pen-size-small").attr('checked', 'checked');
        $("#pen-size-small").focus();
    }
    
   Pen_chooseType(typeValue, penColor);
    //START FIXCSS
    //$('#dlgPen .colorpicker[colorId="'+penColor+'"]').css('border','1px solid #e6e6e6');
    $('#dlgPen .colorpicker[colorId="'+penColor+'"]').addClass('picker-choose-on');
    $('#dlgPen .colorpicker[colorId="'+penColor+'"]').removeClass('picker-choose-off');
    //END FIXCSS
};

function Pen_colorPickerMouseLeaveFunction() {

	//START FIXCSS
    //$(this).css('border','1px solid white');    
    //$('#dlgMaker .colorpicker[colorId="'+makerColor+'"]').css('border','1px solid #e6e6e6');
    $(this).addClass('picker-choose-off');
    $(this).removeClass('picker-choose-on');
    $('#dlgPen .colorpicker[colorId="' + pen_chooseColor + '"]').addClass('picker-choose-on');
    $('#dlgPen .colorpicker[colorId="' + pen_chooseColor + '"]').removeClass('picker-choose-off');
    //END FIXCSS
};

function Pen_colorPickerMouseEnterFunction() {
	//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');
	Pen_chooseType(pen_Size, color);
};

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

function Pen_rdo1_click() {
    Pen_chooseType(5, pen_chooseColor);  
};
function Pen_rdo2_click() {
    Pen_chooseType(12.5, pen_chooseColor);
};
function Pen_rdo3_click() {
    Pen_chooseType(25, pen_chooseColor);
};
function Pen_rdo4_click() {
    Pen_chooseType(50, pen_chooseColor);
};

// Event of button OK
function Pen_dspOK_click() {	
    // Set selected value to local storage
    var typeValue = undefined;

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

    // Set value to local
    ClientData.penOpt_size(typeValue);
    ClientData.penOpt_color(pen_chooseColor);
	pen_Size = typeValue;
    //c = pen_chooseColor;    
    
    /*enable button */
	enableButtonMarking();
	ClientData.IsAddingMarking(true);
	isAddingMarking = ClientData.IsAddingMarking();
	penSize = ClientData.penOpt_size();
	penColor = ClientData.penOpt_color();
    
    // Close dialog
	pen_targetDiv.hide();
	
	/* unlock dialog overlay */
	$("#overlay").hide();
	
	$('#dlgMarking_imgPenOption').removeClass();
	$('#dlgMarking_imgPenOption').addClass('pen_option_hover');
};

// Event of button cancel
function Pen_dspCancel_click() {
  	/*enable button */
	enableButtonMarking();
	ClientData.IsAddingMarking(true);
	isAddingMarking = ClientData.IsAddingMarking();
	
    // Close dialog 
    pen_targetDiv.hide();
    
    /* unlock dialog overlay */
	$("#overlay").hide();
	
	$('#dlgMarking_imgPenOption').removeClass();
	$('#dlgMarking_imgPenOption').addClass('pen_option_hover');
  
};

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