// JavaScript Document
//Harry Bomrah
var orbitDesktopAPI = function(){
	o = this;
	this.windowcounter = 0;
	this.notify = function(msg,type,time){
		//takes 3 arguments
		//msg (string) : message to display
		//type (string : success, imp, alert
		//time (int) : duration for notification in seconds
		var $notify = $('#orbitnote'),
			ani_method = { direction: 'up',easing: 'easeInOutQuint' },
			img, n_height;
		if(!type)type="";
		switch(type){
			case "alert":
				img = "exclamation-sign";
			break;
			case "imp":
				img = "warning-sign";
			break;
			case "success":
				img = "ok";
			break;
			default:
				img = "exclamation-sign";
			break;
		}
		if( !time || time > 20) time = 5000; else time = time*1000;

		var sign = '<div class="note_type"><span class="sign icon-'+ img +'"></span></div>',
			message = '<div class="note_message">'+ msg +'</div>',
			item = '<div class="note_holder admbg">'+ sign + message +'</div>';

		$(item)
			.prependTo($notify)
			.stop(1,1)
			.toggle('slide', ani_method, 300)
			.delay(time)
			.toggle('slide', ani_method, 800);
		$notify.find('.note_holder:hidden').remove();
	};
	this.executeFunc = function(func,callbackFn){
		//takes 2 arguments 
		//func (object Function) : the function which has to be executed 
		//callbackFn (object function) : the function for callback
		func.call(this);
		if(callbackFn){
			callbackFn.call(this,func);
		}
	};
	this.sortJSON = function(field, reverse, primer){
		//takes 3 arguments
		//field (string): it is the field on which sorting has to be done
		//reverse (boolean): this is for asc or desc
		//primer (method): for example pass parseInt for converting the field attribute in integer
	   var key = function (x) {return primer ? primer(x[field]) : x[field]};
	   return function (a,b) {
	       var A = key(a), B = key(b);
	       return ((A < B) ? -1 : (A > B) ? +1 : 0) * [-1,1][+!!reverse];                  
	   }
	};
	this.rgb2hex = function(rgb) {
		// takes 1 parameter
		// rgb (string) : pass rgb string to convert 
	    rgb = rgb.match(/^rgb\((\d+),\s*(\d+),\s*(\d+)\)$/);
	    var hex = function(x) {
	        return ("0" + parseInt(x).toString(16)).slice(-2);
	    }
	    return "#" + hex(rgb[1]) + hex(rgb[2]) + hex(rgb[3]);
	};
	this.changeWallpaper = function(wallpaper){
		//takes 1 parameter
		//wallpaper (string): takes wallpaper and sets the wallpaper
		$("#thmbackground").attr("src",wallpaper);
		o.saveWallpaper(wallpaper);
	};
	this.confirm = function(settings,callbackfn){
		//takes set of arguments in array
		//settings.buttons (array string) : name of buttons to be displayed
		//settings.highlighted  (integer) : takes either 1 or 2.. will highlight the button which is passed
		//settings.message (string) : takes a string and message to be displayed on the confirmbox
		//callbackfn (object function) : function to callback

		if(!settings.buttons)settings.buttons=["Ok","Cancel"];
		if(settings.highlighted!=1 && settings.highlighted!=2)settings.highlighted=2;
		if(!settings.message)settings.message = "Are you sure?"
		var $confirm = $("#orbitdiag");
		$confirm.find(".diag_action a").removeClass("thmc1 thmtxt").addClass("admbg admtxt");
		$confirm.find(".diag_message").text(settings.message);
		if(settings.highlighted == 1)
			$confirm.find("#diag_confirm").removeClass("admbg admtxt").addClass("thmc1 thmtxt");
		if(settings.highlighted == 2)
			$confirm.find("#diag_cancel").removeClass("admbg admtxt").addClass("thmc1 thmtxt");
		$confirm.find("#diag_confirm").text(settings.buttons[0]).one("click",function(){
			$confirm.stop(1,1).fadeOut();
			$confirm.find(".diag_action a").unbind();
			if(typeof callbackfn=="function")
				callbackfn.call(this,true);
		});
		$confirm.find("#diag_cancel").text(settings.buttons[1]).one("click",function(){
			$confirm.stop(1,1).fadeOut();
			$confirm.find(".diag_action a").unbind();
			if(typeof callbackfn=="function")
				callbackfn.call(this,false);
		});
		$confirm.stop(1,1).fadeIn();
	};
	this.getArrayfromJson = function(tjson,key){
		// returns array and takes json object and key
		//json (json object) : json object from which the key has to be extracted
		// key (string) : key which has to be extracted 
		var tempArray = new Array;
		$.each(tjson,function(i,val){
			tempArray.push(val[key]);
		})
		return tempArray;
	};
	this.appWindow = function(options,callbackfn){
		//takes set of arguments as array and gives callback
		//settings.method (string) : like open and close 
		//settings.title (string) : the window title
		//settings.extUrl (boolean) : true for opening external url else false
		//settings.appid (string) : appid.. user can use this appid to open or close or refresh the window
		//settings.url (string) : it is the url if you want to force external url to open in appwindow... you have to pass extURL in appid if you want to open externalurl
		var settings = {
			method : "open",
			extUrl : true,
			title : "New Window"
		}
		$.extend(settings,options);
		if(!settings.appid){
			o.notify("Invalid AppID.","imp",3);
			return;
		}
		var isWindow = ($("div.app_frame[data-app="+settings.appid+"]").length > 0 ? $("div.app_frame[data-app="+settings.appid+"]") : null);
		if(isWindow){
			o.minimizeBarManager.maximize(isWindow);
			return;
		}
		if(settings.method == 'open'){
			var appurl = "http://www.rulingcom.com";
			o.windowcounter++;
			if(settings.extUrl){
				if(settings.url){
					
					if(settings.url.substr(0,4)!="http")settings.url = "http://"+settings.url;
					var urlregex = new RegExp("^(http:\/\/www.|https:\/\/www.|ftp:\/\/www.|www.){1}([0-9A-Za-z]+\.)");
					if(urlregex.test(settings.url))
						appurl = settings.url;
					else{
						o.notify("Invalid URL","alert",3);
						return;
					}
				}
				else{
					o.notify("Invalid URL","alert",3);
					return;
				}
			}
			var whtml = $('<div id="app_frame_'+o.windowcounter+'" class="thmc2 app_frame" data-app="'+settings.appid+'" data-title="'+ settings.title +'"><div class="app_frame_header"><div class="dtitle hh2 hp"><span class="thmtxth">'+settings.title+'</span></div><span class="icon-remove hh2 hp thmtxt"></span><span class="icon-minus hh2 hp thmtxt"></span></div><div id="holder_'+o.windowcounter+'" class="app_holder clear"><iframe src="'+appurl+'" frameborder="0" scrolling="auto"></iframe></div></div>');
			$("body").append(whtml).css('overflow','hidden');
			var parentwindow = $("#app_frame_"+o.windowcounter);
			var app_holder_height = parentwindow.height() - 60;
			var app_holder_width = parentwindow.width();
			parentwindow.find("iframe").attr({"height":app_holder_height,"width":app_holder_width});
			parentwindow.find(".app_holder").height(app_holder_height);
			parentwindow.find(".icon-remove").click(function(){
				o.appWindow.close(parentwindow);
			});
			parentwindow.find(".icon-minus").click(function(){
				o.appWindow.minimize(parentwindow);
			})

		}

		this.appWindow.close = function(win){
			win.remove();
		}

		this.appWindow.minimize = function(win){
			o.minimizeBarManager.minimize(win);
			var minimizeBar = $('#minimizebar'),
				minimizeHeight = minimizeBar.height();
			minimizeBar
			.stop(1,1)
			.delay(2000)
			.animate({
			//'bottom': -minimizeHeight
				'opacity': 0
			}, 900, 'easeInOutQuint');

			minimizeBar.on({
				mouseenter: function(){
					$(this)
					.stop(1,1)
					.animate({
						'opacity': 1
					}, 900, 'easeInOutQuint');
				},
				mouseleave: function(){
					$(this)
					.stop(1,1)
					.delay(2000)
					.animate({
					//'bottom': -minimizeHeight
						'opacity': 0
					}, 900, 'easeInOutQuint');
				}
			});
		}

		if(typeof callbackfn=="function"){
			callbackfn.call(this,parentwindow);
		}
		return parentwindow;
	};
	this.toolPopup = function(settings){
		if(settings == "destroy"){
			$(".desktop_toolpopup").hide("fold",function(){
				$(this).remove();
				$(this).parent().css("position","");
			})
			return;
		}
		if(settings.parent){
			if(settings.parent.find("div.desktop_toolpopup").length == 0){
				settings.height = (!settings.height ? "50px" : settings.height);
				settings.width = (!settings.width ? "150px" : settings.width);
				settings.html = (!settings.html ?  "" : settings.html);

				//Events
				settings.onClose = (!settings.onClose ?  function(){} : settings.onClose);
				settings.beforeOpen = (!settings.beforeOpen ?  function(){} : settings.beforeOpen);
				settings.onOpen = (!settings.onOpen ?  function(){} : settings.onOpen);
				settings.beforeClose = (!settings.beforeClose ?  function(){} : settings.beforeClose);
				//Events end

				var leftpos = settings.parent.outerWidth();
				var toolpopup = $("<div class='desktop_toolpopup' style='height:"+settings.height+";width:"+settings.width+"; left:"+leftpos+"px;'>"+settings.html+"</div>");
				settings.parent.css("position","relative");
				settings.parent.prepend(toolpopup);
				toolpopup.click(function(event){
					event.stopPropagation();
				});
				settings.beforeOpen.call(this);
				toolpopup.show("fold",function(){
					settings.onOpen.call(this);
					$(document).unbind("click");
					$(document).one("click",function(e){
						settings.beforeClose.call(this);
						toolpopup.hide("fold",function(){
							toolpopup.remove();
							settings.parent.css("position","");
							settings.onClose.call(this);
						});						
					})
				});				
			}
		}
	};
	this.filepathSplitter = function(path){
		return path.split(/\\/g);
	}
};
orbitDesktopAPI.prototype.notifyImgPath = "temp";
orbitDesktopAPI.prototype.wallpaperPath = "temp";
