var btnClicked = "-1";
var shiftY;
var shiftX;
var shiftSigY;
var shiftSigX;
var finaleScaler
var finalscaleW
var finalscaleH
	 
function resizeInteraction(thewidth,theheight) {
	var scale = 0;
	thewidth = String(thewidth).replace("px","");
	theheight = String(theheight).replace("px","");
	/*if(thewidth<theheight){
	 scale = thewidth / (680+70);
	}else{
	 scale = theheight/ (500);
	}*/
	
	/**********************/
	//Modification made for Presenter same logic holds good for Captivate
	
	var divSlideWidth = $(divSlide).width();
	var divSlideHeight = $(divSlide).height();
	
	var scaleW = thewidth / (750);
	var scaleH = theheight/ (498);
	
	if(scaleW<scaleH){
		scale = scaleW
	}else{
		scale = scaleH
	}
	finaleScaler = scale;
	finalscaleW = scaleW
	finalscaleH = scaleH
	/*********************/

	myWidgetiFrame.style.width = parseInt(parseInt(750*scaleW))+"px"
	myWidgetiFrame.style.height = parseInt(parseInt(550*scaleH))+"px"
	
	var iframewidth = String($(myWidgetiFrame).css('width')).replace("px","");
	var iframeheight = String($(myWidgetiFrame).css('height')).replace("px","");

	//$($(myWidgetiFrame).parent().parent()).css("top",(-19*scaleH))
	//$($(myWidgetiFrame).parent().parent()).css("left",(-25*scaleW))
	
	
	var margins = Math.round((5 * scale));
	var bgWidth = ((682) * scale) + "px";
	var bgHeight = (436 * scale) + "px";
	
	margins+="px"
	scale = "scale(" + scale + ")";
	$('#reveal').css('-webkit-transform', scale);
	$('#reveal').css('-moz-transform', scale);
	$('#reveal').css('-o-transform', scale);
	$('#reveal').css('-ms-transform', scale);
	
	$('#reveal').css('-moz-transform-origin', '0 0');
	$('#reveal').css('-webkit-transform-origin', '0 0');
	$('#reveal').css('-moz-transform-origin', '0 0');
	$('#reveal').css('-o-transform-origin', '0 0');
	$('#reveal').css('-ms-transform-origin', '0 0');
	
	$('#certificatewdgt').css('-webkit-transform', 'rotate(0deg)');
	$('#certificatewdgt').css('-moz-transform', 'rotate(0deg)');
	$('#certificatewdgt').css('-o-transform', 'rotate(0deg)');
	$('#certificatewdgt').css('-ms-transform', 'rotate(0deg)');
	$('#certificatewdgt').css('transform', 'rotate(0deg)');
	
	$('#reveal').css('margin-top', margins);
	$('#reveal').css('margin-left', margins);
	
	var bgScale = bgWidth + " " + bgHeight;
	
	if(isDevice && deviceType!="iPad"){
		addPinchFunction();
	}
}

var global = {};
global.scaleFactor = 1;
global.previousScaleFactor = 1;

function addPinchFunction(){
	var container = document.getElementById('certificatewdgtmain');
	var element = document.getElementById('certificatewdgt');
	//var reveal = document.getElementById('reveal');
	element.scaleFactor = 1;
    var thiswidget = new Hammer(element,{
        //transform_always_block: true,
        no_mouseevents: true,
        transform_min_scale: 1,
        //drag_block_horizontal: true,
        //drag_block_vertical: true,
        drag_min_distance: 0
    });

    element.scaleFactor = 1;
    
    var displayWidth = container.clientWidth;
    var displayHeight = container.clientHeight;

    //These two constants specify the minimum and maximum zoom
    var MIN_ZOOM = 1;
    var MAX_ZOOM = 3;

    global.scaleFactor = 1;
    global.previousScaleFactor = 1;
	
	global.elementLeft = 0 ;
	global.elementTop = 0;

    //These two variables keep track of the X and Y coordinate of the finger when it first
    //touches the screen
    var startX = 0;
    var startY = 0;

    //These two variables keep track of the amount we need to translate the canvas along the X
    //and the Y coordinate
    var translateX = 0;
    var translateY = 0;

    //These two variables keep track of the amount we translated the X and Y coordinates, the last time we
    //panned.
    var previousTranslateX = 0;
    var previousTranslateY = 0;

    //Translate Origin variables

    var tch1 = 0, 
        tch2 = 0, 
        tcX = 0, 
        tcY = 0,
        toX = 0,
        toY = 0,
        cssOrigin = "";

    var self = global;
    thiswidget.on("transformstart",function(event){
        event.gesture.stopPropagation();
        event.gesture.preventDefault();
                                                            
        //We save the initial midpoint of the first two touches to say where our transform origin is.
        e = event.gesture;

        tch1 = [e.touches[0].pageX, e.touches[0].pageY],
        tch2 = [e.touches[1].pageX, e.touches[1].pageY];

        tcX = (tch1[0]+tch2[0])/2,
        tcY = (tch1[1]+tch2[1])/2;

        toX = tcX;
        toY = tcY;

        var lSlideBoundingClientRect = element.getBoundingClientRect();
        var left = lSlideBoundingClientRect.left;
        var top = lSlideBoundingClientRect.top;

        cssOrigin = (-(left) + toX)/self.scaleFactor +"px "+ (-(top) + toY)/self.scaleFactor +"px";
		global.cssOrigin = cssOrigin;
    });
    
    thiswidget.on("transform", function(event) {
        event.gesture.stopPropagation();
        event.gesture.preventDefault();
        self.scaleFactor = self.previousScaleFactor * event.gesture.scale;
		
        self.scaleFactor = Math.max(MIN_ZOOM, Math.min(self.scaleFactor, MAX_ZOOM));
        element.scaleFactor = self.scaleFactor;
        transformWdgt(element,self.scaleFactor,cssOrigin);
		addClickHandlers()
    });

    thiswidget.on("transformend", function(event) {
        event.gesture.stopPropagation();
        event.gesture.preventDefault();
        element.scaleFactor = self.scaleFactor;
        self.previousScaleFactor = self.scaleFactor;

        if(event.gesture.stopDetect)
            event.gesture.stopDetect();
		
		addClickHandlers()
		
    });

    var startPos,elemLeft,elemTop;
    thiswidget.on('dragstart',function(event) {
        if(element.scaleFactor == 1){
            //self.fitMovie(event);
            element.scrollDeltaX = 0;
            element.scrollDeltaY = 0;
            return;
        }	
        event.gesture.stopPropagation();
        event.gesture.preventDefault();
		var touches = event.gesture.touches || [event.gesture];
		
		startPos = {pageX:touches[0].pageX,pageY:touches[0].pageY};
		elemLeft = parseFloat(element.style.left);
		elemTop = parseFloat(element.style.top);
		
		addClickHandlers()
		
	});

	thiswidget.on('drag',function(event) {
        if(element.scaleFactor == 1){
            if(((event.gesture.direction == "up") || (event.gesture.direction == "down"))){
                //self.scrollV(event.gesture.deltaY-element.scrollDeltaY);
                element.scrollDeltaY = event.gesture.deltaY;
                self.isScrolling = true;
            }else if(((event.gesture.direction == "left") || (event.gesture.direction == "right"))){
                //self.scrollH(event.gesture.deltaX-element.scrollDeltaX);
                element.scrollDeltaX = event.gesture.deltaX;
                self.isScrolling = true;
            }
            return;
        }    
        event.gesture.stopPropagation();
        event.gesture.preventDefault();
		//var lProjBoundingClientRect = container.getBoundingClientRect();
		//var left = lProjBoundingClientRect.left;
		//var top = lProjBoundingClientRect.top;

        var touches = event.gesture.touches || [event.gesture];
        
        element.style.left = elemLeft + touches[0].pageX - startPos.pageX + "px";
        element.style.top = elemTop + touches[0].pageY - startPos.pageY + "px";
		
		global.elementLeft = (element.style.left);
		global.elementTop = (element.style.top);
		
		addClickHandlers()
	});

    thiswidget.on('dragstop',function(event){
        if(event.gesture.stopDetect)
            event.gesture.stopDetect();
        self.isScrolling = false;
		
		addClickHandlers()
       // self.fitMovie(event);
    })

    function customPreventDefault(ev){
        ev.preventDefault();
    };
	
	//thiswidget.on("touchstart",setGameInteractions);
    //thiswidget.on("touchmove",customPreventDefault);
    //thiswidget.on("touchend",customPreventDefault);
    //thiswidget.on("touchcancel",customPreventDefault);  
	
	//thiswidget.on('dragstart', OnDragStart);
	//thiswidget.on('drag', OnDrag);
	//thiswidget.on('dragstop', OnDragEnd);
}
function transformWdgt(iElem,iScaleFactor,iCssOrigin){
	
    iElem.scaleFactor = iScaleFactor;
    //We're going to scale the X and Y coordinates by the same amount
    var cssScale = "scaleX("+ iScaleFactor +") scaleY("+ iScaleFactor +")";

    iElem.style["webkitTransform"] = cssScale;
    if(iCssOrigin)
        iElem.style["webkitTransformOrigin"] = iCssOrigin;

    iElem.style["transform"] = cssScale;
    if(iCssOrigin)
        iElem.style["transformOrigin"] = iCssOrigin;

    if(iScaleFactor == 1){
        iElem.style.left = "0px";
        iElem.style.top = "0px";

        //if(this.previousScaleFactor > iScaleFactor)
           // cp.adjustResponsiveItems(cp.ReasonForDrawing.kOrientationChangeOrResize);

        this.scaleFactor = 1;
        this.previousScaleFactor = 1;
    }   
}


function resettransformWdgt(){
	var iElem = document.getElementById('certificatewdgt');
	var iScaleFactor =  1;
	var iCssOrigin = global.cssOrigin
	
    iElem.scaleFactor = iScaleFactor;
    //We're going to scale the X and Y coordinates by the same amount
    var cssScale = "scaleX("+ iScaleFactor +") scaleY("+ iScaleFactor +")";

    iElem.style["webkitTransform"] = cssScale;
    if(iCssOrigin)
        iElem.style["webkitTransformOrigin"] = iCssOrigin;

    iElem.style["transform"] = cssScale;
    if(iCssOrigin)
        iElem.style["transformOrigin"] = iCssOrigin;

    if(iScaleFactor == 1){
        iElem.style.left = "0px";
        iElem.style.top = "0px";

        //if(this.previousScaleFactor > iScaleFactor)
           // cp.adjustResponsiveItems(cp.ReasonForDrawing.kOrientationChangeOrResize);

        global.scaleFactor = 1;
        global.previousScaleFactor = 1;
    }   
}

function updateVariablePos(){
	//var shiftY;
	//var shiftX;
	shiftSigY = 0;
	shiftSigX = 0;
	switch(parseInt(currentTheme)){
		case 1:
			shiftY = -50;
			shiftX = 5;//.9?
			shiftSigY = -11;
		break;
		case 2:
			shiftY = -46;
			shiftX = 0;//.9?	
			shiftSigY = -11;
		break;
		case 3:
			shiftY = -42;
			shiftX = -10;//.9?
			shiftSigY = -20;
		break;
		case 4:
			shiftY = -48;
			shiftX = 10;//.9?
		break;
		case 5:
			shiftY = -28;
			shiftX = 20;//.9?
			$('#sig').css('display', 'none');
		break;
		case 6:
			shiftY = -32;
			shiftX = 0;//.9?
			shiftSigY = 13;
		break;
		case 7:
			shiftY = -35;
			shiftX = 0;//.9?
			shiftSigY = -32;
		break;
		case 8:
			shiftY = -51;
			shiftX = 23;//.9?	
			shiftSigY = -21;
		break;
		case 9:
			shiftY = -37;
			shiftX = 6;//.9?
			shiftSigY = -7;
		break;
		case 10:
			shiftY = -20;
			shiftX = 20;//.9?
			$('#sig').css('display', 'none');
		break;
		case 11:
			shiftY = -32;
			shiftX = -5;//.9?
			shiftSigY = -7;
		break;
		case 12:
			shiftY = -40;
			shiftX = 0;//.9?
		break;
		case 13:
			shiftY = -21;
			shiftX = 18;//.9?
			$('#sig').css('display', 'none');
		break;
		case 14:
			shiftY = -57;
			shiftX = 3;//.9?
			shiftSigY = -17;
		break;
		case 15:
			shiftY = -31;
			shiftX = 3;//.9?
		break;
		case 16:
			shiftY = -33;
			shiftX = -12;//.9?
			shiftSigY = 7;
		break;
		case 17:
			shiftY = -25;
			shiftX = 18;//.9?
			$('#sig').css('display', 'none');
		break;
		case 18:
			shiftY = -27;
			shiftX = -4;//.9?
		break;
	}
	
	$('#custom').css('width',"300px")
	//$('#custom').css('text-align',"left")
				
	setNamePos(
		(parseFloat(generalStyles.nameX)+shiftX),
		(parseFloat(generalStyles.nameY)+shiftY)
	);
	setDatePos(
		(parseFloat(generalStyles.dateX)+shiftX),
		(parseFloat(generalStyles.dateY)+shiftY - 2)
	);
	setCustomPos(
		(parseFloat(generalStyles.customX)+shiftX-20),
		(parseFloat(generalStyles.customY)+shiftY)
	);
	setScorePos(
		(parseFloat(generalStyles.scoreX)+shiftX),
		(parseFloat(generalStyles.scoreY)+shiftY)
	);
	setSignaturePos(
		(parseFloat(generalStyles.sigX)+shiftX+50),
		(parseFloat(generalStyles.sigY)+shiftY)
	);	
	setSigPos(
		(parseFloat(450)+shiftSigX),
		(parseFloat(328)+shiftSigY)
	);	
}

function setNamePos(x, y){$('#name').css('left',x+'px');$('#name').css('top', y+'px');}
function setScorePos(x, y){$('#score').css('left',x+'px');$('#score').css('top', y+'px');}
function setCustomPos(x, y){$('#custom').css('left',x+'px');$('#custom').css('top', y+'px');}
function setDatePos(x, y){$('#date').css('left',x+'px');$('#date').css('top', y+'px');}
function setSignaturePos(x, y){$('#sigtext').css('left',x+'px');$('#sigtext').css('top', y+'px');}
function setSigPos(x, y){$('#sig').css('left',x+'px');$('#sig').css('top', y+'px');}

function updateVariableVisibility(){
	if(generalStyles.nameOn=="false"){$('#name').css('display','none');}else{$('#name').css('display','visible');}
	if(generalStyles.dateOn=="false"){$('#date').css('display','none');}else{$('#date').css('display','visible');}
	if(generalStyles.customOn=="false"){$('#custom').css('display','none');}else{$('#custom').css('display','visible');}
	if(generalStyles.scoreOn=="false"){$('#score').css('display','none');}else{$('#score').css('display','visible');}
	if(generalStyles.sigOn=="false"){$('#sigtext').css('display','none');}else{$('#sigtext').css('display','visible');}
}

//$(document).ready(function() {
function addClickHandlers() {
	
	$("#reveal").fadeIn();
	
	
	 //$('ul#tools').prepend('<li class="print"><a href="#print">Click me to print</a></li>');
	 $('#print').click(function() {	
	   	var scale = $('#reveal').css('-webkit-transform');
	 	$('#reveal').css('-webkit-transform' , "scale(1)");
	 	window.print();
	 	$('#reveal').css('-webkit-transform' , scale);	
	   return false;
	 });
	 
	 $('#printtxt').click(function() {											
	   	 	var scale = $('#reveal').css('-webkit-transform');
	 	$('#reveal').css('-webkit-transform' , "scale(1)");
	 	window.print();
	 	$('#reveal').css('-webkit-transform' , scale);	
	   return false;
	 });
	

	$(document).keydown(function(e){	
	//console.log("key press",btnClicked,e.target.firstChild,e.keyCode)
		if(e.keyCode  == 13 || e.keyCode  == 32) {
		
	   	 var scale = $('#reveal').css('-webkit-transform');
	 	$('#reveal').css('-webkit-transform' , "scale(1)");
	 	window.print();
	 	$('#reveal').css('-webkit-transform' , scale);	
	   return false;
	 }});
	 
	var myElement = document.getElementById('content_bg');
	var myElementContainer = document.getElementById('certificatewdgtmain');
	var certificatepinch = new Hammer(myElement);
	
	var tapper = 0;
	var savedWidth;
	var savedHeight;
	/*certificatepinch.on("tap", function(ev) {
			if(isDevice && currentOrientation==0){
				if(tapper ==0){	
				
					myWidgetiFrame.style.width = 1024+"px"
					myWidgetiFrame.style.height = 768+"px"
			
					var scale = finaleScaler*2
					tapper = 1;
					savedWidth = $('#certificatewdgtmain').css("width");
					savedHeight = $('#certificatewdgtmain').css("height");
					$('#certificatewdgtmain').css("width",(1024*finalscaleW)+"px")
					$('#certificatewdgtmain').css("height",(768*finalscaleH)+"px")
					
					$('#certificatewdgtmain').css('overflow', "auto");
					$("#certificatewdgtmain").scrollLeft(135)
					
					scale = "scale(" + scale + ")";
				
					$('#reveal').css('-webkit-transform', scale);
					$('#reveal').css('-moz-transform', scale);
					$('#reveal').css('-o-transform', scale);
					$('#reveal').css('-ms-transform', scale);
					$('#reveal').css('-transform', scale);
					
					$('#reveal').css('-moz-transform-origin', '-20% 0');
					$('#reveal').css('-webkit-transform-origin', '-20% 0');
					$('#reveal').css('-transform-origin', '-20% 0');
					$('#reveal').css('-o-transform-origin', '-20% 0');
					$('#reveal').css('-ms-transform-origin', '-20% 0');
					
					
				}else{
					var scale =finaleScaler
					tapper = 0
					
					scale = "scale(" + scale + ")";
					$('#certificatewdgtmain').css("width",savedWidth)
					$('#certificatewdgtmain').css("height",savedHeight)
					
					$('#certificatewdgtmain').css('overflow', "none");
				
					$('#reveal').css('-webkit-transform', scale);
					$('#reveal').css('-moz-transform', scale);
					$('#reveal').css('-o-transform', scale);
					$('#reveal').css('-ms-transform', scale);
					$('#reveal').css('-transform', scale);
					
					$('#reveal').css('-moz-transform-origin', '0 0');
					$('#reveal').css('-webkit-transform-origin', '0 0');
					$('#reveal').css('-transform-origin', '0 0');
					$('#reveal').css('-o-transform-origin', '0 0');
					$('#reveal').css('-ms-transform-origin', '0 0');
					
				}
			}
		})*/
	
}

//sound values
var theSnd = null;
function pauseSound() {
	if(theSnd != null) // && theSnd.src != wavePath)
	{ theSnd.pause();}
}

function play_sound(url){
	theSnd = new Audio(url);
	theSnd.load();
	theSnd.play();	
}
////////////////////////////////////////////////////////

function setupCustomStyles() {
	$('.items').css('font-weight', 'bold');
	$('#print').css('cursor', 'pointer');
	//$('#printtxt').disableTextSelect();
	if(ID_Print.length<=5){
		$('#printtxt').css('margin-right', '-80px');
	}else{
		$('#printtxt').css('margin-right', '-90px');
	}
	$('#printtxt').text(ID_Print);

	
}