﻿/// <reference path="~/Scripts/ByteStorm/_BSC_Interface.js" />
BSC.UI.W = function(cw, path)
{
	this.id = cw.ID;
	this.identifier = cw.Identifier;
	this.guid = BSC.U.NewGUID();
	this.wguid = cw.WindowID;
	this.page = cw.PageKey;
	this.template = cw.Template;
	this.query = cw.Query;
	this.querytemplate = cw.QueryString;
	this.cachehtml = cw.CacheHtmlLocal;
	this.tab = cw.TabKey;
	this.tabshow = cw.TabKeyToShow;
	this.tabgroup = cw.TabGroupID;
	this.showtabs = cw.ShowTabs;
	this.scriptkey = cw.ScriptID;
	this.path = cw.Path;
	this.uri = cw.p;
	this.key = cw.Key;
	this.data = cw.Data;
	this.accessid = cw.AccessID;
	this.title = cw.Title;
	this.cleanpath = cw.cp;
	this.zindex = BSC.UI.Ws.GetNextZIndex();
	var wid = BSC.D.Ws.I.Get(this.wguid);
	this.width = (wid) ? wid.w : 0;
	this.height = (wid) ? wid.h : 0;
	this.left = (wid) ? wid.l : 0;
	this.top = (wid) ? wid.t : 0;
	this.minwidth = (wid) ? wid.mw : 350;
	this.minheight = (wid) ? wid.mh : 200;
	this.script = wid.script;
};

BSC.UI.W.prototype = {
    id: null, // WindowData GUID
    identifier: null, // Id of the menu
    template: null,
    guid:null, // Random GUID
    tab:null, // Current Tab
    tabshow: null, // Current Tab to show
    tabgroup: null,
    query: null, // Current Query
    querytemplate:null, // Querystring template
    title:'', // Current Window Title in bar
    zindex:null, // Current ZIndex
    path: null, // Path
    cleanpath: null, // Path without query
    cachehtml: false, // Cache output html in local cache
    showtabs: false, // Path
    minimized:false, // Window Is minimized
    maximized:false, // Window Is maximized
    minwidth:350, // Windows minimum width
    minheight:200, // Window minimun height
    width:0, // Windows current width
    height:0, // Windows current height
    left:0, // Windows current left position
    top:0, // Windows current top position
    prop:{}, // Windows porperties
    script:null,
    newtab:true,
    newpage:true,
    loadcontent:true,
    components:{}, 
    wwp:{},// WindowWidthProperties
    whp:{}// WindowHeightProperties
}; 

BSC.UI.W.prototype.Init = function()
{
	BSC.UI.Ws.AddWIDGuid(this.wguid);
	BSC.UI.Ws.Add(this.guid, this);
	$("#"+this.id).css({left:this.left,top:this.top});
	BSC.UI.W.SetSize(this.guid,this.width,this.height);
	BSC.UI.W.SetPosition(this.guid,this.left, this.top);
	$("#" + this.guid + "_Template").addClass(this.template);

	// Scroll to top
	$('#' + this.guid + '_content').stop().scrollTo({ top: 0 }, 200);
};

BSC.UI.W.prototype.Key = function()
{
	return this.scriptkey;
};

/// Focus
/// --------------------------
BSC.UI.W.Focus = function(guid, animate)
{
	if (!guid) return;
	var w = BSC.UI.Ws.Get(guid);
	var zindex = BSC.UI.Ws.GetNextZIndex();
	var uiw = $("#" + guid);
	minimized = false;
	uiw.removeClass("inactive");
	uiw.css("z-index", zindex);
	$('#Taskbar_' + guid).addClass("active");
	BSC.UI.Ws.SetActiveId(guid);

	// Change Uri
	BSC.P.ChangeURI(w.uri);

	if (animate)
	{
		var uiws = $("#WindowDragging");
		uiws.css({ left: w.left, top: w.top, width: w.width, height: w.height, display: "block", opacity: 1, background: 'white', zindex: zindex + 1 });
		uiw.css("display", "block");
		uiws.animate({ opacity: 0 }, 250, function() { $("#WindowDragging").css("display", "none"); });
	}
	else
		uiw.css("display", "block");

	BSC.E.Trigger(BSC.E.SubscribeTypes.Focus, [guid], w.scriptkey);
};

/// Blur
/// --------------------------
BSC.UI.W.Blur = function(guid)
{
	if (!guid) return;
	$("#" + guid).addClass("inactive");
	$('#Taskbar_' + guid).removeClass("active");

	var w = BSC.UI.Ws.Get(guid);
	BSC.E.Trigger(BSC.E.SubscribeTypes.Blur, [guid], w.scriptkey);
};

/// Reload
/// --------------------------
BSC.UI.W.Reload = function(guid)
{
	if (!guid) return;
	var w = BSC.UI.Ws.Get(guid);
	BSC.UI.HideWindowAlert(guid);
	if (!BSC.E.Trigger(BSC.E.SubscribeTypes.Reload, [guid], w.scriptkey))
		BSC.P.LoadHtml(guid);
};

/// Bookmark
/// --------------------------
BSC.UI.W.Bookmark = function(guid)
{
	if (!guid) return;
	var w = BSC.UI.Ws.Get(guid);
	if (!BSC.E.Trigger(BSC.E.SubscribeTypes.Bookmark, [guid], w.scriptkey))
	{
		var data = { id: null, type: 'misc', uri: w.uri, title: w.title, callback: null, data: { header: "Bookmark saved"} };
		BSC.UI.Bookmark.Add(data);
	}
};

/// GetContentDim
/// --------------------------
BSC.UI.W.GetContentDim = function(guid)
{
	if (!guid) return;
    var w = parseInt($("#"+guid+"_content").css("width"));
    var h = parseInt($("#"+guid+"_content").css("height"));
    return {w:w, h:h};
};

BSC.UI.W.EventBind = function(guid)
{
	if (!guid) return;
	var w = BSC.UI.Ws.Get(guid);
	if (!w) return;
	$("#" + guid + "_tabs_content_ul").click(BSC.P.OnTabClick);
	$('#' + guid).bind('mousedown', guid, BSC.UI.W.OnMousedown);

	BSC.E.Trigger(BSC.E.SubscribeTypes.Bind, guid, w.scriptkey);

	BSC.E.Trigger(BSC.E.SubscribeTypes.Load, guid, w.scriptkey);
	BSC.E.MapHrefs();
	try
	{
		$("#" + guid + "_Template textarea.expand").autogrow();
	}
	catch (e)
	{ }
};

BSC.UI.W.Unbind = function(guid)
{
    var w = BSC.UI.Ws.Get(guid);
    if (!w) return;
    BSC.E.Trigger(BSC.E.SubscribeTypes.Unbind, guid, w.scriptkey);
    $("#" + guid).unbind();
    $("#" + guid + "_tabs_content_ul").unbind();
    $("#" + guid + " a").unbind();
};

/// Close
/// --------------------------
BSC.UI.W.Close = function(guid)
{
    var w = BSC.UI.Ws.Get(guid);
    if (!w) return;
	// Remove ALL event listners from window DOM
	BSC.UI.W.Unbind(guid);
	var uis = $("#WindowDragging");
	if (!w.minimized)
		uis.css({
			left: w.left,
			top: w.top,
			width: w.width,
			height: w.height,
			display: "block",
			background: 'black',
			opacity: "0.5"
		});
	// Remove window from DOM
	$("#" + guid).remove();
	// Remove Taskbar window item from DOM
	$("#Taskbar_" + guid).remove();
	var wi = parseInt($("#TaskbarList").css("width"));
	var cw = parseInt($("#Taskbar").css("width")) - 49;
	var newW = wi - 125;
	$("#TaskbarList").css("width", newW);
	//	if(newW<cw)
	//	{
	//		$("#TaskbarNext").addClass("inactive");
	//		$("#TaskbarNext").unbind("click",BSC.P.OnTaskbarNext);
	//		$("#TaskbarList").animate({left:0},100);
	//	}

	// If this window was active set active to null
	if (BSC.UI.Ws.GetActiveId() === guid) BSC.UI.Ws.SetActiveId(null);
	// Remove window from list

	if (!w.minimized)
		uis.animate({ opacity: 0 }, 200, function() { $("#WindowDragging").css("display", "none"); });
	BSC.UI.Ws.Remove(guid, w.wguid);
	// Change history/uri path
	BSC.P.ChangeURI("/", null);
};

/// Restore
/// --------------------------
BSC.UI.W.Restore = function(guid)
{
    var w = BSC.UI.Ws.Get(guid);
    if (!w) return;
	var activeID = BSC.UI.Ws.GetActiveId();
	var uiw = $("#"+guid);
	// Restore from min to max
	if(w.maximized && w.minimized)
	{
		BSC.UI.W.Maximize(guid);
		return;
	}
	uiw.css("left",w.left);
	uiw.css("top", w.top);
	uiw.removeClass("max");
	uiw.removeClass("inactive");
	BSC.UI.W.SetSize(guid,w.width,w.height);

	if (activeID !== guid) 
	{
		$('#Taskbar_'+guid).addClass("active");
		BSC.UI.Ws.SetActiveId(guid);
		activeID = guid;
		BSC.P.ChangeURI(w.path);
	}

	$("#" + activeID).css("z-index", BSC.UI.Ws.GetNextZIndex());
	if (!w.maximized)
	{
		var uis = $("#WindowDragging");
		uis.css({left:0,top:$(document).height(),width:150,height:0,display:"block",background:'black',opacity:0});
		uis.animate({ left: w.left, top: w.top, width: w.width, height: w.height, opacity: "0.5" }, 350, function()
		{
			$("#" + activeID).css("display", "block");
			$("#WindowDragging").css("display", "none");
		});
	}
	else
	{
		$("#" + activeID).css("display", "block");
	}

	
	BSC.UI.Ws.Get(guid).maximized = false;
	BSC.UI.Ws.Get(guid).minimized = false;
	var p = { guid: guid };
	BSC.E.Trigger(BSC.E.SubscribeTypes.Restore, p, w.scriptkey);
};

/// Maximize
/// --------------------------
BSC.UI.W.Maximize = function(guid)
{
    var w = BSC.UI.Ws.Get(guid);
    if (!w) return;
	var zindex = BSC.UI.Ws.GetNextZIndex();
	var uiw = $("#"+guid);
	
	// Save size and position
	if(!w.minimized || !w.maximized)
	{
		w.width = parseInt(uiw.css("width"));
		w.height = parseInt(uiw.css("height"));
		w.left = parseInt(uiw.css("left"));
		w.top = parseInt(uiw.css("top"));
	}
	/// UI
	uiw.addClass("max");
	uiw.removeClass("inactive");
	uiw.css("left",0);
	uiw.css("top",44);
	uiw.css("z-index",zindex);
	uiw.css("display","block");
	BSC.UI.W.SetSize(guid,$(window).width()+9,$(window).height()-57);	
	
	BSC.UI.Ws.Get(guid).minimized = false;
	BSC.UI.Ws.Get(guid).maximized = true;
	
	if(BSC.UI.Ws.GetActiveId()!==guid) 
	{
		$('#Taskbar_'+guid).addClass("active");
		BSC.UI.Ws.SetActiveId(guid);
		BSC.P.ChangeURI(w.path);
	}
	var p = { guid: guid };
	BSC.E.Trigger(BSC.E.SubscribeTypes.Maximize, p, w.scriptkey);
};

/// Minimize
/// --------------------------
BSC.UI.W.Minimize = function(guid)
{
    var w = BSC.UI.Ws.Get(guid);
    if (!w) return;
	// UI
	// Hide window DOM
	var uiw = $("#" + guid);
	uiw.css("display", "none");
	//uiwindow.css("display","none");
	var uiws = $("#WindowDragging");
	uiws.css({ left: w.left, top: w.top, width: w.width, height: w.height, display: "block", background: 'black', opacity: "0.5" });
	uiws.animate({ left: 100, top: $(document).height(), width: 150, height: 0, opacity: 0 }, 350);
	// Set Task item to inactive
	$("#Taskbar_" + guid).removeClass("active");
	// Save size and position
	w.minimized = true;
	
	if (!w.maximized)
	{
		w.width = parseInt(uiw.css("width"));
		w.height = parseInt(uiw.css("height"));
		w.left = parseInt(uiw.css("left"));
		w.top = parseInt(uiw.css("top"));
	}
	// If this window was active set active to null
	if (BSC.UI.Ws.GetActiveId() === guid)
	{
		BSC.UI.Ws.SetActiveId(null);
		BSC.P.ChangeURI("/");
	}
	var p = { guid: guid };
	BSC.E.Trigger(BSC.E.SubscribeTypes.Minimize, p, w.scriptkey);
};



/// Close
/// --------------------------
BSC.UI.W.CloseActive = function()
{
    if(BSC.UI.Ws.GetActiveId())
    	BSC.UI.W.Close(BSC.UI.Ws.GetActiveId());
};

/// DragStart
/// --------------------------
BSC.UI.W.DragStart = function(e)
{
	/// <summery>ByteStorm.UI.W.DragStart</summery>
	/// <params name="e">the event</params>
	/// <returns>void</returns>

	BSC.UI.DisableSelection($(BSC.UI.Ws.ActiveUIDrag));
	// Calcutale delta mouse position
	var dX = e.pageX-BSC.UI.Ws.ActiveDrag.prop.pX;
	var dY = e.pageY-BSC.UI.Ws.ActiveDrag.prop.pY;
	// Threshold = 4px, mouse needs to move more than 4px before dragging
	if(Math.abs(dX)>4||Math.abs(dY)>4) {
		// Activate shadow DIV, hide window
		//ByteStorm.UI.Ws.ActiveUIDrag.css("display","none"); 
		BSC.UI.Ws.ActiveUIDragShadow.css("display","block");
		// Set position on shadow DIV
		BSC.UI.Ws.ActiveUIDragShadow.css({left:BSC.UI.Ws.ActiveDrag.left+dX,top:Math.min(Math.max(BSC.UI.Ws.ActiveDrag.top+dY,51),($(window).height()-35))});	
	};
	return false;
};

/// DragStop
///----------------------------------
BSC.UI.W.DragStop = function(e)
{

	BSC.UI.EnableSelection($(BSC.UI.Ws.ActiveUIDrag));
	// Remove mouse event listners on document
	$().unbind('mousemove',BSC.UI.W.DragStart).unbind('mouseup',BSC.UI.W.DragStop);
	// Set new position to window from shadow DIV
	BSC.UI.Ws.ActiveUIDrag.css({left:BSC.UI.Ws.ActiveUIDragShadow.css("left"),top:BSC.UI.Ws.ActiveUIDragShadow.css("top")});
	// Hide shadow and show window
	BSC.UI.Ws.ActiveUIDragShadow.css("display","none");
	//ByteStorm.UI.Ws.ActiveUIDrag.css("display","block");
	// Save DOM properties on window object
	BSC.UI.Ws.ActiveDrag.left = parseInt(BSC.UI.Ws.ActiveUIDragShadow.css("left"));
	BSC.UI.Ws.ActiveDrag.top = parseInt(BSC.UI.Ws.ActiveUIDragShadow.css("top"));
	// Dispose temp dragging variables
	BSC.UI.Ws.ActiveDrag = null;
	BSC.UI.Ws.ActiveUIDrag = null;
	BSC.UI.Ws.ActiveUIDragShadow = null;

	var w = BSC.UI.Ws.Get(BSC.UI.Ws.GetActiveId());
	var p = { guid:BSC.UI.Ws.GetActiveId() };
	BSC.E.Trigger(BSC.E.SubscribeTypes.DragStop, p, w.scriptkey);
};

/// StartResize
///----------------------------------
BSC.UI.W.StartResize = function(e)
{
	/// <summery>ByteStorm.UI.W.StartResize</summery>
	/// <params name="e">the event</params>
	/// <returns>void</returns>
	BSC.UI.DisableSelection($(BSC.UI.Ws.ActiveUIDrag));
	
	// Calcutale delta mouse position
	var dX = e.pageX-BSC.UI.Ws.ActiveDrag.prop.pX;
	var dY = e.pageY-BSC.UI.Ws.ActiveDrag.prop.pY;
	// Only resize window inside content browser
	if(e.pageY<BSC.P.MaxTop+1 || e.pageY>$(window).height()-BSC.P.maxBottom)  return;
	// Threshold = 4px, mouse needs to move more than 4px before resizing
	if(Math.abs(dX)>4||Math.abs(dY)>4) {
		// Activate shadow DIV, hide window
		//ByteStorm.UI.Ws.ActiveUIDrag.css("display","none"); 
		BSC.UI.Ws.ActiveUIDragShadow.css("display","block");
		// Cache window properties
		var w = wo = BSC.UI.Ws.ActiveDrag.width;
		var mw = BSC.UI.Ws.ActiveDrag.minwidth;
		var h = ho = BSC.UI.Ws.ActiveDrag.height;
		var mh = BSC.UI.Ws.ActiveDrag.minheight;
		var l = BSC.UI.Ws.ActiveDrag.left;
		var t = BSC.UI.Ws.ActiveDrag.top;
		var p = BSC.UI.Ws.ActiveDrag.prop;
		var uids = BSC.UI.Ws.ActiveUIDragShadow;
		// Resizing South East
		if(p.d=='se') { w=Math.max(w+dX,mw); h=Math.max(h+dY,mh); }
		// Resizing East
		else if(p.d=='e') { w=Math.max(w+dX,mw); }
		// Resizing South
		else if(p.d=='s') { h=Math.max(h+dY,mh); }
		// Resizing West
		else if(p.d=='w') { w=Math.max(w-dX,mw); uids.css("left",l+Math.min(dX,wo-mw)); }
		// Resizing South West
		else if(p.d=='sw') { h=Math.max(h+dY,mh); w=Math.max(wo-dX,mw); uids.css("left",l+Math.min(dX,wo-mw)); }
		// Resizing North West
		else if(p.d=='nw') { w=Math.max(w-dX,mw); uids.css("left",l+Math.min(dX,wo-mw)); h=Math.max(h-dY,mh); uids.css("top",t+Math.min(dY,ho-mh)); }
		// Resizing North
		else if(p.d=='n') { h=Math.max(h-dY,mh); uids.css("top",t+Math.min(dY,ho-mh)); }
		// Resizing North East
		else if(p.d=='ne') { w=Math.max(w+dX,mw); h=Math.max(h-dY,mh); uids.css("top",t+Math.min(dY,ho-mh)); }
		// Set new width and height to shadow DIV
		uids.css({width:w,height:h});
	}
	return false;
};

/// StopResize
///----------------------------------
BSC.UI.W.StopResize = function(e)
{
	/// <summery>ByteStorm.UI.W.StopResize</summery>
	/// <params name="e">the event</params>
	/// <returns>void</returns>
	BSC.UI.EnableSelection($(BSC.UI.Ws.ActiveUIDrag));
	// Remove mouse event listners on document
	$().unbind('mousemove',BSC.UI.W.StartResize).unbind('mouseup',BSC.UI.W.StopResize);
	// Set width and height to window
	BSC.UI.W.SetSize(BSC.UI.Ws.ActiveDrag.guid,parseInt(BSC.UI.Ws.ActiveUIDragShadow.css("width")),parseInt(BSC.UI.Ws.ActiveUIDragShadow.css("height")));
	// Set position to window
	BSC.UI.Ws.ActiveUIDrag.css({left:parseInt(BSC.UI.Ws.ActiveUIDragShadow.css("left")),top:parseInt(BSC.UI.Ws.ActiveUIDragShadow.css("top"))});
	// Hide shadow and show window
	BSC.UI.Ws.ActiveUIDragShadow.css("display","none");
	//ByteStorm.UI.Ws.ActiveUIDrag.css("display","block");
	
    var p = {
        guid:BSC.UI.Ws.ActiveDrag.guid, 
        newWidth:parseInt(BSC.UI.Ws.ActiveUIDragShadow.css("width")), 
        newHeight:parseInt(BSC.UI.Ws.ActiveUIDragShadow.css("height")),
        deltaWidth:parseInt(BSC.UI.Ws.ActiveUIDragShadow.css("width")) - BSC.UI.Ws.ActiveDrag.width,
        deltaHeight:parseInt(BSC.UI.Ws.ActiveUIDragShadow.css("height")) - BSC.UI.Ws.ActiveDrag.height
    };

	// Save DOM properties on window object
    BSC.UI.Ws.ActiveDrag.width = parseInt(BSC.UI.Ws.ActiveUIDrag.css("width"));
	BSC.UI.Ws.ActiveDrag.height = parseInt(BSC.UI.Ws.ActiveUIDrag.css("height"));
	BSC.UI.Ws.ActiveDrag.left = parseInt(BSC.UI.Ws.ActiveUIDrag.css("left"));
	BSC.UI.Ws.ActiveDrag.top = parseInt(BSC.UI.Ws.ActiveUIDrag.css("top"));

	// Trigger event
	var w = BSC.UI.Ws.Get(BSC.UI.Ws.ActiveDrag.guid);
	BSC.E.Trigger(BSC.E.SubscribeTypes.StopResize, p, w.scriptkey);

	// Dispose temp dragging variables
	BSC.UI.Ws.ActiveDrag = null;
	BSC.UI.Ws.ActiveUIDrag = null;
	BSC.UI.Ws.ActiveUIDragShadow = null;
};

///----------------------------------
BSC.UI.W.OnMousedown = function(e)
{
	/// <summery>ByteStorm.UI.W.Mousedown</summery>
	/// <params name="e">the event</params>
	/// <returns>void</returns>
	if (BSC.M.ActiveId != null) { var uisubmenu = $("#submenu_" + BSC.M.ActiveId); uisubmenu.unbind(); uisubmenu.css({ display: 'none' }); BSC.M.ActiveId = null; }
	// Blur menu
	BSC.UI.HideHelpBubble();
	BSC.M.HideActiveSubmenu();
	$("#menu").css("z-index", 999);
	// Get Element from event
	var el = BSC.E.GetTarget(e);
	// Cache Element element id
	var guid = e.data;
	var w = BSC.UI.Ws.Get(guid);
	// Cache window DOM
	var uiwindow = $("#" + guid);

	// BOOKMARK
	///----------------------------------
	if (el.id == guid + "_bookmark")
	{
		BSC.UI.W.Bookmark(guid);
		// Event Stop
		e.preventDefault(); e.stopPropagation();
		return;
	}
	// RELOAD
	///----------------------------------
	if (el.id == guid + "_reload")
	{
		BSC.UI.W.Reload(guid);
		// Event Stop
		e.preventDefault(); e.stopPropagation();
		return;
	}
	// CLOSE
	///----------------------------------
	if (el.id == guid + "_close")
	{
		BSC.UI.W.Close(guid);
		// Event Stop
		e.preventDefault(); e.stopPropagation();
		return;
	}
	// MINIMIZE
	///----------------------------------
	if (el.id == guid + "_min")
	{
		BSC.UI.W.Minimize(guid);
		// Event Stop
		e.preventDefault(); e.stopPropagation();
		//
		return;
	}

	// Set Window Focus
	///----------------------------------
	if (BSC.UI.Ws.GetActiveId() !== guid)
	{
		if (BSC.UI.Ws.GetActiveId())
			BSC.UI.W.Blur(BSC.UI.Ws.GetActiveId());
		BSC.UI.W.Focus(guid);
		BSC.P.ChangeURI(w.uri);
	}
	// HELP
	///----------------------------------
	if (el.id == guid + "_help")
	{
		alert("help");
		return;
	}

	// HELP
	///----------------------------------
	if (el.id == guid + "_bookmark")
	{
		if (!BSC.D.P.IsLoggedIn())
		{
			BSC.UI.AlertLogin(w.path);
			return;
		}
		BSC.UI.Alert("Add", "Cancel", "Add Bookmark", "Title: " + w.title + "<br>Url:" + w.path, BSC.UI.Bookmark.Add, { id: null, type: "misc", title: w.title, uri: w.path, callback: BSC.UI.Bookmark.Confirm, data: null }, "bookmark");
		return;
	}


	// MAXIMIZE
	///----------------------------------
	if (el.id == guid + "_max")
	{
		// IE, reset hover on max 
		// $(el).css("background","transparent url('/beautifulpeoplecdn/images/window/top_btn.gif') no-repeat 66% 0");
		// 
		if (w.maximized)
		{
			BSC.UI.W.Restore(guid);
			// Event Stop
			e.preventDefault(); e.stopPropagation();
			return;
		}
		BSC.UI.W.Maximize(guid);
		// Event Stop
		e.preventDefault(); e.stopPropagation();
		return;
	}
	// Window maximized, don't activate drag or resize	
	if (w.maximized) return;

	///----------------------------------	
	while (guid !== el.id)
	{
		// Dragging
		if (el.id == guid + "_tt")
		{
			var p = {}; if (uiwindow.css('position') != 'relative') { try { uiwindow.position(p); } catch (e) { } }
			w.prop = { pX: e.pageX, pY: e.pageY };
			BSC.UI.Ws.ActiveDrag = w;
			BSC.UI.Ws.ActiveUIDrag = uiwindow;
			BSC.UI.Ws.ActiveUIDragShadow = $("#WindowDragging");
			/*ByteStorm.UI.Ws.ActiveUIDragShadow.css({left:w.left,top:w.top,width:w.width,height:w.height,background:'transparent',border:'1px solid red',opacity:1});*/
			BSC.UI.Ws.ActiveUIDragShadow.css({ left: w.left, top: w.top, width: w.width, height: w.height, background: 'black', opacity: "0.5" });
			$().mousemove(BSC.UI.W.DragStart).mouseup(BSC.UI.W.DragStop);
			return;
		}
		// Resize
		if (el.id == guid + "_wbr" ||
			el.id == guid + "_mr" ||
			el.id == guid + "_tr" ||
			el.id == guid + "_wbl" ||
			el.id == guid + "_ml" ||
			el.id == guid + "_tl" ||
			el.id == guid + "_td" ||
			el.id == guid + "_wbtd")
		{
			var d =
				(el.id == guid + "_wbr") ? 'se' :
				(el.id == guid + "_mr") ? 'e' :
				(el.id == guid + "_tr") ? 'ne' :
				(el.id == guid + "_wbl") ? 'sw' :
				(el.id == guid + "_ml") ? 'w' :
				(el.id == guid + "_tl") ? 'nw' :
				(el.id == guid + "_td") ? 'n' :
				(el.id == guid + "_wbtd") ? 's' :
				'';
			var p = {}; if (uiwindow.css('position') != 'relative') { try { uiwindow.position(p); } catch (e) { } }
			w.prop = { pX: e.pageX, pY: e.pageY, d: d };
			BSC.UI.Ws.ActiveDrag = w;
			BSC.UI.Ws.ActiveUIDrag = uiwindow;
			BSC.UI.Ws.ActiveUIDragShadow = $("#WindowDragging");
			BSC.UI.Ws.ActiveUIDragShadow.css({ left: w.left, top: w.top, width: w.width, height: w.height, background: 'black', opacity: "0.5" });
			//w.CacheProperties();
			$().mousemove(BSC.UI.W.StartResize).mouseup(BSC.UI.W.StopResize);
			return;
		}

		// Get Parent Element
		el = el.parentNode;
	}
};

BSC.UI.W.SetTabs = function(guid)
{
	var w = BSC.UI.Ws.Get(guid);
	var tabs = w.showtabs;

	var height = w.height;
	var content_c_c = $("#" + guid + "_wmc_tc_c");
	var content_c_c_c = $("#" + guid + "_wmc_tc_c_c");
	var main = $("#" + guid);
	var mainHeight = parseInt(main.css('height'));
	var content = $("#" + guid + "_mc");
	var whp_content = (mainHeight - parseInt(content.css('height')));
	var t = (tabs) ? BSC.UI.Ws.Prop.ContentHeightTabs : BSC.UI.Ws.Prop.ContentHeightNoTabs;
	content_c_c_c.css("height", height - whp_content - t);
		
	// Show / Hide tabs
	if(tabs) $("#"+guid+"_tabs_content").show();
	else $("#"+guid+"_tabs_content").hide();
};

BSC.UI.W.SetSize = function(guid,width,height)
{
	/// <summery>ByteStorm.UI.W.SetSize</summery>
	/// <params name="id">id of the window in the list "ByteStorm.D.Ws.List"</params><params name="width">new window width</params><params name="height">new window height</params>
	/// <returns>void</returns>
	// Cache DOM objects
	var main = $("#"+guid);
	var top = $("#"+guid+"_tt");
	var topmenu = $("#"+guid+"_topmenu");
	var content = $("#"+guid+"_mc");
	var content_l = $("#"+guid+"_ml");
	var content_r = $("#"+guid+"_mr");
	var bottom = $("#"+guid+"_wbt");
	var content_c_l = $("#"+guid+"_left");
	var content_c_c = $("#"+guid+"_wmc_tc_c");
	var content_c_r = $("#"+guid+"_right");
	var content_c_c_c = $("#"+guid+"_wmc_tc_c_c");
	// Cache DOM properties
	var mainWidth = parseInt(main.css('width')); 
	var mainHeight = parseInt(main.css('height')); 
	var wwp_top = (mainWidth-parseInt(top.css('width')));
	var wwp_topmenu = (mainWidth-parseInt(topmenu.css('width')));
	var wwp_content = (mainWidth-parseInt(content.css('width')));
	var wwp_bottom = (mainWidth-parseInt(bottom.css('width')));
	var whp_content = (mainHeight-parseInt(content.css('height')));    
	// Change DOM properties
	main.css("width",width);
	top.css("width",width-wwp_top);
	topmenu.css("width",width-wwp_topmenu);
	content.css("width",width-wwp_content);
	bottom.css("width",width-wwp_bottom);
	main.css("height",height);
	content.css("height",height-whp_content);
	content_l.css("height",height-whp_content);
	content_r.css("height",height-whp_content);
	content_c_l.css("height",height-whp_content-BSC.UI.Ws.Prop.ContentHeight);
	content_c_c.css("height",height-whp_content-BSC.UI.Ws.Prop.ContentHeight);
	content_c_r.css("height",height-whp_content-BSC.UI.Ws.Prop.ContentHeight);
	var w = BSC.UI.Ws.Get(guid);
	var t = (w.showtabs) ? BSC.UI.Ws.Prop.ContentHeightTabs : BSC.UI.Ws.Prop.ContentHeightNoTabs;
	content_c_c_c.css("height",height-whp_content-t);
};

/// SetPosition
/// --------------------------
BSC.UI.W.SetPosition = function(guid, left, top)
{
	$("#"+guid).css({left:left,top:top});
};


/// SetDefaultSizePosition
/// --------------------------
BSC.UI.W.SetDefaultSizePosition = function(guid, wguid)
{
    var wid = BSC.D.Ws.I.Get(wguid);
    $("#" + guid).css({ left: wid.l, top: wid.t });
    BSC.UI.W.SetSize(guid, wid.w, wid.h);
    BSC.UI.Ws.Get(guid).width = wid.w;
    BSC.UI.Ws.Get(guid).height = wid.h;
    BSC.UI.Ws.Get(guid).left = wid.l;
    BSC.UI.Ws.Get(guid).top = wid.t;
};

