	$.fn.center = function(options) {
    /// <summary>Centers the selected items in the browser window. Takes into account scroll position.
    /// Ideally the selected set should only match a single element.
    /// </summary>    
    /// <param name="fn" type="Function">Optional function called when centering is complete. Passed DOM element as parameter</param>    
    /// <param name="forceAbsolute" type="Boolean">if true forces the element to be removed from the document flow 
    ///  and attached to the body element to ensure proper absolute positioning. 
    /// Be aware that this may cause ID hierachy for CSS styles to be affected.
    /// </param>
    /// <returns type="jQuery" />
    var opt = { forceAbsolute: false,
                container: window,    // selector of element to center in
                completeHandler: null
              };
    $.extend(opt, options);
   
    return this.each(function(i) {
        var el = $(this);
        var jWin = $(opt.container);
        var isWin = opt.container == window;

        // force to the top of document to ENSURE that 
        // document absolute positioning is available
        if (opt.forceAbsolute) {
            if (isWin)
                el.remove().appendTo("body");
            else
                el.remove().appendTo(jWin.get(0));
        }

        // have to make absolute
        el.css("position", "absolute");

        // height is off a bit so fudge it
        var heightFudge = isWin ? 2.0 : 1.8;

        var x = (isWin ? jWin.width() : jWin.outerWidth()) / 2 - el.outerWidth() / 2;
        var y = (isWin ? jWin.height() : jWin.outerHeight()) / heightFudge - el.outerHeight() / 2;

		if (y < 0)
			y = 0;
			
		if (x < 0)
			x = 0;
	

        el.css("left", x + jWin.scrollLeft());
        el.css("top", y + jWin.scrollTop());

        // if specified make callback and pass element
        if (opt.completeHandler)
            opt.completeHandler(this);
    });
}

jQuery.fn.exists = function(){return jQuery(this).length>0;}

function gotoURL(url) {
	self.document.location.replace(url);
}

function fullScreen(url) {

	params  = 'width='+screen.width;
	params += ', height='+screen.height;
	params += ', top=0, left=0'
	params += ', fullscreen=yes';
	
	newwin=window.open(url,'windowname4', params);
	if (window.focus) {
		newwin.focus()
	}
	return false;
}	


$(function(){
	$('#smartvue-nvr-select').click(function(){
		$('#overlay').show();
		
		sURL	= "ajax/ajx_multiserver_list_json.php";
		$.get(sURL,function(data){
		
			$('#smartvue-dialog-nvr-content').empty().append(data);
		    $("#servers").treeview({
				 collapsed: false
			});

			$('.server').unbind('click');
			$('.server').click(function(){
				var ipaddress 		= $(this).attr('data-server-ip');
				var callback_ip	 	= $(this).attr('data-callback-ip');
				var session_id		= $(this).attr('data-session-id');
				var user_id			= $(this).attr('data-user-id');
				if (ipaddress) {
						sURL		= "http://"+ipaddress+"/multiserver/connect.php?callbackip="+callback_ip+"&sid="+session_id;
						document.location.replace(sURL);
				} else {
					alert("Server Not Connected.")
				}
			});
		});


		$('#smartvue-dialog-nvr-select').center();
		$('#smartvue-dialog-nvr-select').show();

		$('#smartvue-dialog-nvr-select .dialog-caption b').click(function(){
			$('#smartvue-dialog-nvr-select').hide();
			$('#overlay').hide();
		})


		$('#nvr-select-ok').unbind('click');
		$('#nvr-select-ok').click(function(){
			$('#smartvue-dialog-nvr-select').hide();
			$('#overlay').hide();
		})

		$('#nvr-select-cancel').unbind('click');
		$('#nvr-select-cancel').click(function(){
			$('#smartvue-dialog-nvr-select').hide();
			$('#overlay').hide();
		})
		
	});		
});


function SubTabs() {
	$('.smartvue-tab').click(function(){
		var active_tab_id			= $(this).attr('id');
		var active_tab_element		= "#"+active_tab_id;
		var	active_panel_element	= active_tab_element+"-panel";
		$('.smartvue-tab').each(function(){
			$(this).removeClass('active-tab');
			panel_element	= "#"+$(this).attr('id')+"-panel";
			$(panel_element).hide();
		});
		$(active_tab_element).addClass('active-tab');
		$(active_panel_element).show();
	});	
}	
