var blocks = Array();

function redirect(url){
	//location.href=url;
	//location.reload();
	window.location.href=url;
	//window.location.href.assign(url)
}

function get_window_dimensions(){
	var myWidth = 0, myHeight = 0;
  	if( typeof( window.innerWidth ) == 'number' ) {
  	  //Non-IE
  	  myWidth = window.innerWidth;
  	  myHeight = window.innerHeight;
  	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
  	  //IE 6+ in 'standards compliant mode'
  	  myWidth = document.documentElement.clientWidth;
  	  myHeight = document.documentElement.clientHeight;
  	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
  	  //IE 4 compatible
  	  myWidth = document.body.clientWidth;
  	  myHeight = document.body.clientHeight;
  	}
  	var tmp = new Array(2);
  	tmp[0] = myWidth;
  	tmp[1] = myHeight;
	return tmp;
}

function get_scroll_location(){
	var scroll_x,scroll_y;
	if (self.pageYOffset) // all except Explorer
	{
		scroll_x = self.pageXOffset;
		scroll_y = self.pageYOffset;
	}
	else if (document.documentElement && document.documentElement.scrollTop)
	{	// Explorer 6 Strict
		scroll_x = document.documentElement.scrollLeft;
		scroll_y = document.documentElement.scrollTop;
	}
	else if (document.body) // all other Explorers
	{
		scroll_x = document.body.scrollLeft;
		scroll_y = document.body.scrollTop;
	}
	var tmp = new Array(2);
	tmp[0] = scroll_x;
	tmp[1] = scroll_y;
	return tmp;
}


function new_window(id, top, left){
	/* if no id create a new id */	
	if(typeof id == "undefined"){ 
		var date = new Date;
		var id = date.getTime();
	}
	
	if(!JQ("#" + id).is("div")){
		//JQ("#main").prepend('<div id="' + id + '" class="window"><div class="header"><a class="window_close left" title="Stäng fönster" href="#"><img src="include/images/window_close.gif" alt="Stäng fönster"></a><div class="header_text clearfix"></div></div><div class="content" id="' + id + '_content"></div></div>');
		JQ("#page").prepend('<div class="window" id="' + id + '">'+
								  		'<table class="window_layout">'+
											'<tr>'+
												'<td class="cell top_left transparent"></td>'+
												'<td class="cell horizontal_top transparent"></td>'+
												'<td class="cell top_right transparent">'+
													'<div class="window_close"></div>'+
												'</td>'+
											'</tr>'+
											'<tr>'+
												'<td class="cell vertical_left transparent"></td>'+
												'<td class="cell content transparent"></td>'+
												'<td class="cell vertical_right transparent"></td>'+
											'</tr>'+
											'<tr>'+
												'<td class="cell bottom_left transparent"></td>'+
												'<td class="cell horizontal_bottom transparent"></td>'+
												'<td class="cell bottom_right transparent"></td>'+
											'</tr>'+
										'</table>'+
								  '</div>');
		var window = JQ("#" + id);
		var content = window.children().filter('.content');
	
		if(typeof height === "undefined"){
			height = "300px";
		}
		
		if(typeof width === "undefined"){
			width = "auto";
		}		

		window.css('left', left);
		window.css('top', top);
		window.css('z-index', z_index);		
	/*	window.css('width', width);
		window.css('height', height);
	
		content.css('width', width);
		content.css('height', height);
	*/	
		window.slideDown('medium',function(){ block_ui('unblock');});
	}else{
		return 0;
		//var window = JQ("#" + id);
		//close_window(id);
		//windows.pop();
		//return 0;
		//var content = window.children().filter('.content');
		//var title_bar = JQ("#" + id + ' .header_text');
		/*window.css('top', scroll_y + y_offset);
		window.html();
		window.show();
		*/
	}
	//init_master();
	windows.push(id);
	return id;
}

function close_window(id){
	//block_ui('block');
	var window = JQ(id);
	var content = window.children().filter('.content');			
	window.slideUp('medium', 
		function(){
			content.html('');
			window.remove();
			block_ui('unblock');
		}
	);
}

function block_ui(task){
	var scroll = get_scroll_location();
	JQ('#block_ui').css('left', scroll[0]);
	JQ('#block_ui').css('top', scroll[1]);
	
	if(task == "unblock"){ 
		if(blocks.length <= 1){
			JQ('#block_ui').hide();
		}
		blocks.pop();
		return 1;
	}
	else if(task == "block"){
		if(blocks.length == 0){
			JQ('#block_ui').show();
		}
		blocks.push(1);
		return 1;
	}
}

