


////////////////////////// DEVICE Controler  /////////////////////////



function YDevCtl()
{
	
	this.fileRead=function(filename)
	{
		var md = MD5(filename);
		return localStorage.getItem(md);
	}
	this.fileWrite=function(filename,data)
	{
		var filelist = localStorage.getItem("filelist");
		filelist = filelist+","+filename;
		var md = MD5(filename);
		localStorage.setItem(md, data);
		return true;
	}
	this.fileAppend=function(filename,data)
	{
		var md = MD5(filename);
		var od = localStorage.getItem(md);
		localStorage.setItem(md, od+data);
		return true;
	}
	this.getFileSize=function(filename)
	{
		var md = MD5(filename);
		return localStorage.getItem(md).length;
	}
	this.fileDelete=function(filename)
	{
		var md = MD5(filename);
		localStorage.setItem(md, "");
	}
	this.fileExist=function(filename)
	{
		var filelist = localStorage.getItem("filelist");
		filelist = filelist.split(",");
		for(var i=0;i<filelist.length;i++)
		{
			if(filelist[i]==filename)
			{
				return true;
			}
		}
		return false;
	}
	this.showToast=function(message)
	{
		console.info(message);
	}
	this.alertOK=function(title,message)
	{
		alert(message);
	}
	this.alertYesNo=function(title,message)
	{
		return window.confirm(message);
	}
	this.showProgressBar=function(message)
	{
	}
	this.changeProgressBar=function(index)
	{
	}
	this.hideProgressBar=function()
	{
	}
	this.captureCameraImage=function()
	{
	}
	this.setCookie=function(string,value)
	{
		//document.cookie =string;
		setCookie(string,value);
	}
	this.getCookie=function(name)
	{
		//return document.cookie;
		return getCookie(name);
	}
	this.getDeviceId=function()
	{
		if(YMI.getCookie("deviceId")!="")
		{
			return YMI.getCookie("deviceId");
		}
		else
		{
			return ""+parseInt(Math.random()*10000000)+"";
		}
		
	}
	this.getVersionName=function()
	{
		return "web";
	}
	this.chatMessageServiceStart =function()
	{
	}
	this.chatMessageServiceStop=function()
	{
	}
	this.getActivityExtraData=function()
	{
		return "";
	}
	this.clearCache=function()
	{
	}
	this.getDeviceType=function()
	{
		return "browser";
	}
}


if(typeof devctl =="undefined")
{
	devctl=new YDevCtl();
}












///////////////////////////    DEVICE Environment   //////////////////











function YMessageService()
{
	this.callback=Object;
	this.callback.whenMessageReceive=null;
	this.callback.whenError=null;
	this.callback._errorMessage=function(message)
	{
		if(this.whenError!=null)
		{
			this.whenError(message);
		}
	}
	this.callback._messageReceive=function(type,text)
	{
		if(this.whenMessageReceive!=null)
		{
			this.whenMessageReceive(type,text);
		}
	}
	this.start=function()
	{
		devctl.chatMessageServiceStart("mygp",YMI.getCookie("uid"),dev.app.getDeviceId(),"chatMessage");
	}
	this.stop=function()
	{
		devctl.chatMessageServiceStop();
	}
}
function YDeviceDB()
{
	this.callback={};
	this.callback.importComplite=null;
	this.add=function(name)
	{
		return devctl.addDb(name,name+".db");
	}
	this.exec=function(query)
	{
		return $.parseJSON(devctl.execDb(query));
	}
	this.changeDb=function(name)
	{
		return devctl.changeDb(name);
	}
	this.importSql=function(url,callback)
	{
		this.callback.importComplite=callback;
		devctl.importSql(url);
	}
}
function YDeviceAPIIo()
{
    this.fileRead=function(filename)
    {
		return devctl.fileRead(filename);
    }
    this.fileWrite=function(filename,data,callback)
    {
		this.callback.fileWriteDone=callback;
		return devctl.fileWrite(filename,data);
    }
    this.fileAppend=function(filename,data)
    {
		return devctl.fileAppend(filename,data);
    }
	this.getFileSize=function(filename)
	{
		return devctl.getFileSize(filename);
	}
	this.fileDelete=function(filename)
	{
		return devctl.fileDelete(filename);
	}
	this.fileExist=function(filename)
	{
		return devctl.fileExist(filename);
	}
	
	
	
	this.callback=Object;
	this.callback.ioException=null;
	this.callback.fileWriteDone=null;

	this.callback._ioExceptionCallback=function(message)
	{
		if(dev.io.callback.ioException!=null)
		{
			dev.io.callback.ioException(message);
		}
	}
	this.callback._fileWriteDoneCallback=function()
	{
		
		if(dev.io.callback.fileWriteDone!=null)
		{
			dev.io.callback.fileWriteDone();
		}
	}
}

function YDeviceAPIControl()
{
    this.alertOKCallback=null;
    this.alertYESCallback=null;
    this.alertNOCallback=null;
	this.callback= Object;
	this.barcodeCallback=null;
	this.datePickerCallback=null;
	this.timePickerCallback=null;
	this.readBarcode=function()
	{
		devctl.readBarcode();
	}
	this.notification=function(title,text,extraData)
	{
		devctl.makeNotification(extraData,title,text);
	}
	this.callback._timePickerCallback=function(hour,minute)
	{
		
		if(dev.control.timePickerCallback!=null)
		{
			dev.callback.timePickerCallback(hour,minute);
		}
	}
	this.callback._datePickerCallback=function(day,month,year)
	{
		if(dev.control.datePickerCallback!=null)
		{
			month=month+1;
			dev.control.datePickerCallback(day,month,year);
		}
	}
    this.callback._callbackAlertOK=function()
	{
		if(dev.control.alertOKCallback!=null)
		{
			dev.control.alertOKCallback();
		}
	}
    this.callback._callbackAlertYES=function()
    {

		if(dev.control.alertYESCallback!=null)
		{
			dev.control.alertYESCallback();
		}
    }
    this.callback._callbackAlertNO=function()
    {
		if(dev.control.alertNOCallback!=null)
		{
			dev.control.alertNOCallback();
		}
    }
	this.callback._readBarcodeCallback=function(status,value,type)
	{
		
		if(dev.control.barcodeCallback!=null)
		{
			dev.control.barcodeCallback(status,value,type);
		}
	}
	
	
	
	this.timePicker=function()
	{
		devctl.timePicker();
	}
	this.datePicker=function()
	{
		devctl.datePicker();
	}
    this.toast=function(message)
    {
        devctl.showToast(message);
    }
    this.alertOK=function(title,message,callback)
    {
        this.alertOKCallback=callback;
        devctl.alertOK(title,message);
    }
    this.alertYesNo=function(title,message,yesCallback,noCallback)
    {
        this.alertYESCallback=yesCallback;
        this.alertNOCallback=noCallback;
        devctl.alertYesNo(title,message);
    }
    this.showProgress=function(message)
    {
        devctl.showProgressBar(message);
    }
    this.changeProgress=function(i)
    {
        devctl.changeProgressBar(i);
    }
    this.hideProgress=function()
    {
        devctl.hideProgressBar();
    }
	
}
function YDeviceAPIMedia()
{
	this.callback=Object;
	this.callback.mediaSelectionDone=null;
	this.callback._mediaSelectionDoneExec=function(url)
	{
		if(this.mediaSelectionDone!=null)
		{
			this.mediaSelectionDone(url);
		}
	}
	
	this.closeVideoPlayer=function()
	{
		devctl.closeVideoPlayer();
	}
	this.videoPlay=function(url,title)
	{
		devctl.showVideoPlayer(url,title);
	}
	this.videoStop=function()
	{
		devctl.videoStop();
	}
	this.videoPause=function()
	{
		devctl.videoPause();
	}
	this.videoResume=function()
	{
		devctl.videoResume();
	}
	this.youtubeVideoPlay=function(id,title)
	{
		devctl.showYoutubeVideo(id,title);
	}
	
	this.audioPlayerShow=function(title,url)
	{
		devctl.showAutioPlayer(title,url);
	}
	this.audioPlayerPlay=function()
	{
		devctl.audioPlayerPlay();
	}
	this.audioPlayerStop=function()
	{
		devctl.audioPlayerStop();
	}
	this.audioPlayerPause=function()
	{
		devctl.audioPlayerPause();
	}
	this.audioPlayerResume=function()
	{
		devtl.audioPlayerResume();
	}
}
function YDeviceAPIApp()
{
	this.appStatus="open";
	this.onClickBackButton=null;
	this.onStart=null;
	this.onStop=null;
	this.onPouse=null;
	this.onResume=null;
	this.callback=Object;
	this.theam = "blue";
	this.libPath="";
	this.appJs="";
	this.url = new URL(window.location);
	this.initApp=function()
	{
		if(dev.app.libPath=="")
		{
			dev.control.alertOK("JS Library path is configure");
			return;
		}
		if(dev.app.appJs=="")
		{
			dev.control.alertOK("Application base JS file not configure");
			return;
		}
		
		
		dev.app.addCss(dev.app.libPath+"/jquery-ui-1.11.1/jquery-ui.css");
		dev.app.addCss(dev.app.libPath+"/bootstrap/css/bootstrap.css");
		dev.app.addCss(dev.app.libPath+"/css/core.css");
		dev.app.addJs(dev.app.libPath+"/js/yajan.draw.js");
		dev.app.addCss(dev.app.libPath+"/css/yajan.css");
		dev.app.addJs(dev.app.libPath+"/js/function.js");
		dev.app.addJs(dev.app.libPath+"/js/core.js",function()
		{
			dev.app.addCss(dev.app.libPath+"/theams/"+dev.app.theam+".css");
			dev.settings=new YajanAPISetting();
			dev.app.addJs(dev.app.appJs,function()
			{
				
				if(dev.app.run!=null)
				{
					if(dev.app.getBuildVersion()<21)
					{
						//var o = document.getElementsByTagName("body");
						//o[0].innerHTML='<center style="color:#FFF;padding:20px;"><h5>New version has release. Open Google play store and find "Gayatri Teerth - Shantikunj" and install/update it. </h5></center>';
						dev.app.run();
					}
					else
					{
						dev.app.run();
					}
				}
				else
				{
					dev.control.alertOK("dev.app.run is not configured");
					dev.control.alertYesNo("Application","Are you want to reload this application",function()
					{
						if(dev.app.getBuildVersion()>15)
						{
							dev.app.clearCache();
						}
						dev.control.toast("Reloading...");
						window.location.reload(true);
					},
					function()
					{
					});
				}
			});
		});
	}
	this.getUrlParameter=function(name)
	{
		return this.url.searchParams.get(name);
	}
	this.dialContactPhone=function(number)
	{
		devctl.dialContactPhone(number);
	}
	this.sendSms=function(number,text)
	{
		devctl.sendSms(number,text);
	}
	this.getExtraData=function(keyName)
	{
		return devctl.getActivityExtraData(keyName);
	}
	this.debugEnale=function(val)
	{
		devctl.enableJsDebuging(val);
	}
	this.addJs=function(name)
	{
		var callback=null;
		if(arguments.length>1)
		{
			callback=arguments[1];
		}
		var sc = document.createElement("script");
		sc.src=name;
		sc.onload=callback;
		sc.type="text/javascript";
		document.head.appendChild(sc);
	}
	this.setCookie=function(cname,cvalue)
	{
		
		var exp = null;
		if(arguments.length>3)
		{
			exp = arguments[3];
		}
		if(exp!=null)
		{
			var d = new Date();
			d.setTime(d.getTime() + (exdays*24*60*60*1000));
			var expires = "expires="+ d.toUTCString();
			//alert(cname + "=" + cvalue + ";" + expires + ";path=/");
			//devctl.setCookie(cname + "=" + cvalue + ";" + expires + ";path=/");
			devctl.setCookie(cname,cvalue);
		}
		else
		{
			//devctl.setCookie(cname + "=" + cvalue + ";path=/");
			devctl.setCookie(cname,cvalue);
		}
		
		
	}
	this.getCookie=function()
	{
		return devctl.getCookie();
	}
	this.getDeviceId=function()
	{
		return devctl.getDeviceId();
	}
	this.vibratePhone=function(duration)
	{
		devctl.vibratePhone(duration);
	}
	this.addCss=function(name)
	{
		var sc = document.createElement("link");
		sc.rel="stylesheet";
		sc.href=name;
		document.head.appendChild(sc);
	}
	this.clearCache=function()
	{
		devctl.clearCache();
	}
	this.getBuildVersion=function()
	{
		if(typeof devctl.getBuildVersion=="undefined")
		{
			return 21;
		}
		else
		{
			return devctl.getBuildVersion();
		}
	}
	this.getApiVersion=function()
	{
		return devctl.getApiVersion();
	}
	this.getAppId=function()
	{
		if(typeof devctl.getApplicationId == "function")
		{
			return devctl.getApplicationId();
		}
		return "myGP";
	}
	this.getVersionName=function()
	{
		return devctl.getVersionName();
	}
	this.getMACAddress=function(interface)
	{
		return devctl.getMACAddress(interface);
	}
	this.isWifiConnected=function()
	{
		return devctl.isWifiConnected();
	}
	this.getInterfaceList=function()
	{
		return devctl.getInterfaceList();
	}
	this.getIp=function()
	{
		return devctl.getIp();
	}
	this.md5=function(string)
	{
		return devctl.md5(string);
	}
	this.getApiHash=function(data)
	{
		return devctl.getApiHash(data);
	}
	
	this.callback._clickBackButton=function()
	{
		if(dev.app.onClickBackButton!=null)
		{
			dev.app.onClickBackButton();
		}
	}
	this.callback._onStart=function()
	{
		dev.app.appStatus="open";
		if(dev.app.onStart!=null)
		{
			dev.app.onStart();
		}
	}
	this.callback._onResume=function()
	{
		dev.app.appStatus="open";
		if(dev.app.onResume!=null)
		{
			
			dev.app.onResume();
		}
	}
	this.callback._onPouse=function()
	{
		dev.app.appStatus="close";
		if(dev.app.onPouse!=null)
		{
			dev.app.onPouse();
		}
	}
	this.callback._onStop=function()
	{
		dev.app.appStatus="close";
		if(dev.app.onStop!=null)
		{
			dev.app.onStop();
		}
	}
	this.callback.onCallRinging=null;
	this.callback.onCallComplite=null;
	this.callback.onCallDialing=null;
	this.callback.onOptionsItemSelected=null;
	this.callback._onOptionsItemSelected=function(str)
	{
		if(this.onOptionsItemSelected!=null)
		{
			this.onOptionsItemSelected(str);
		}
	}
	this.callback._onCallRinging=function()
	{
		if(typeof this.onCallRinging=="function")
		{
			this.onCallRinging();
		}
	}
	this.callback._onCallComplite=function()
	{
		if(typeof this.onCallComplite=="function")
		{
			this.onCallComplite();
		}		
	}
	this.callback._onCallDialing=function()
	{
		if(typeof this.onCallDialing=="function")
		{
			this.onCallDialing();
		}		
	}
	
	this.textSpeechStart=function(text)
	{
		devctl.speechTextStart(text);
	}
	this.textSpeechStop=function()
	{
		devctl.speechTextStop();
	}
	this.textSpeechOpen=function()
	{
		devctl.speechTextOpen();
	}
	this.textSpeechClose=function()
	{
		devctl.speechTextClose();
	}
	this.setTextSpeechPitch=function(n)
	{
		devctl.setSpeechTextPitch(n);
	}
	this.setTextSpeechRate=function(n)
	{
		devctl.setTextSpeechRate(n);
	}
	this.shareItem=function(title,text,image)
	{
		devctl.shareItem(title,text,image);
	}
	this.openUrl=function(url)
	{
		devctl.openUrl(url);
	}
	this.PayUPayment=function(name,email,mobile,product,amount,f1,f2,f3,f4,f5)
	{
		devctl.PayUPayment(name,email,mobile,product,parseFloat(amount),f1,f2,f3,f4,f5);
	}
	
}

function YMINetApi()
{
	this.networkError=null;
	this.isInternetConnected=function()
	{
		devctl.isInternetConnected();
	}
}
function YDeviceAPI()
{
	this.service=Object;
	this.app = new YDeviceAPIApp();
	this.media=new YDeviceAPIMedia();
    this.io = new YDeviceAPIIo();
    this.control=new YDeviceAPIControl();
    this.net=new YMINetApi();
	this.db = new YDeviceDB();
	this.settings=null;
	this.service.chat = new YMessageService();
	this.version = {libVersion:10,appVersion:this.app.getBuildVersion()};
	this.updateVersion=function()
	{
		dev.app.setCookie("libVersion",this.version.libVersion);
		dev.app.setCookie("appVersion",this.version.appVersion);
		dev.app.setCookie("apiVersion",this.version.apiVersion);
		
		setCookie("libVersion",this.version.libVersion);
		setCookie("appVersion",this.version.appVersion);
		setCookie("apiVersion",this.version.apiVersion);		
		
		if(typeof devctl.getDeviceType != "undefined")
		{
			setCookie("deviceType", devctl.getDeviceType());
			dev.app.setCookie("deviceType",devctl.getDeviceType());
			this.type = devctl.getDeviceType();
		}
		else
		{
			setCookie("deviceType", "mobile");
			dev.app.setCookie("deviceType","mobile");
			this.type = "mobile";
		}
	}
}
dev = new YDeviceAPI();
