/* * * WINDOW MANAGER FUNCTIONS * * */

var anchor;
var old_anchor;

var seed_base_x = 80;  
var seed_base_y = 80;
var leave_header = true; 
var firstRun = true; 

// Blocks initial coordinates and metadata
window.blocks = {	
	"main" : { 
		"attr" : {
			"width" : 710, 
			"height" : 415, 
			"left" : 178, 
			"top" : 75
		}, 
		"url" : "", 
		//"url" : "/pages/view/front", 
		"open" : true
	}, 
	"menu" : { 
		"attr" : {
			"width" : 170, 
			"height" : 260, 
			"left" : 4, 
			"top" : 170
		}, 
		"url" : "/menu_items/index/0", 
		"open" : true
	}, 
	"quick_search_bar" : { 
		"attr" : {
			"width" : 170, 
			"height" : 64, 
			"left" : 4, 
			"top" : 435
		}, 
		"url" : "", 
		"open" : true, 
		"last" : true
	}, 
	
	/* * * SUBWINDOWS * * */
	
	"fb" : { 
		"attr" : {
			"width" : 280, 
			"height" : 435, 
			"left" : 820, 
			"top" : 88
		}, 
		"url" : "/fb/feeds/view", 
		"open" : false
	}, 
	"twitter" : { 
		"attr" : {
			"width" : 260, 
			"height" : 435, 
			"left" : 800, 
			"top" : 112
		}, 
		"url" : "/twitter/tweets/view", 
		"open" : false
	}, 
	'port_icons': { 
		"attr" : {
			"left": 178,  
			"top" : 484, 
			"width" : 710, 
			"height" : 120
		}, 
		"url" : "", 
		"open" : false
	}
}; 

window.nut_open = false; 



function framework_init() {
	$('#loading_main').hide(); 
	/* UGLY WITH IE
	$('#nut').fadeIn(600, function() {
		if(document.location.hash) 
			nut_pop_open(); 
	}); 
	/**/
	$('#nut').show(); 
	if(firstRun)
		$('#init_hint').show(); 
	if(document.location.hash) 
		nut_pop_open(); 
		
	
	$('#nut a').click( function() {
		if(window.nut_open) {
			//nut_pop_close(); // temporarily disabled 
		} else {
			nut_pop_open(); 
		}
	}); 
	firstRun = false; 
}


function nut_pop_open() {
	if(!window.nut_open) {
		adaptBlocksToBody(); 
		$('#init_hint').hide(); 
		$('#nut_open').show(); 
		$('#nut_closed').hide(); 
		$('#help_wrap').addClass('dimmed'); 
		$('#footer').fadeIn(500); 
		
		h_seeds_in(); 
		h_letters_in(); 
		w_seeds_in();  
		
		window.nut_open = true; 
	}
}


function nut_pop_close() {
	if(window.nut_open) {
		if(!leave_header) {
			$('.fblock, .h_initial, .h_letter').each(function(i, item) {
				$(item).delay(Math.floor(Math.random()*750)).
				animate({ 
					left: seed_base_x, 
					top: seed_base_y, 
					width: [ 20, 'easeInQuad' ], 
					height: [ 20, 'easeInQuad' ]
				}, 600, 'easeInQuart', function(){
					$(this).hide(); 
				}); 
			}); 
		}
		
		var slated = leave_header ? $('.block:visible, .subwindow:visible') : $('.block:visible, .fblock:visible, .subwindow:visible'); 
		var total = slated.length;  
		slated.each( function(i, item) {
			seed_out(i, item, total);
		}); 
		$('#help_wrap').removeClass('dimmed');
	}
}


function h_seeds_in() {
	var offset_x = 580; 
	var padding = 32; 
	
	$('.fblock:hidden,.fblock_spacer:hidden').each(function(i, item) {
		var s = Math.random()*10+45; 
		$(item).css({
			left: seed_base_x + Math.random()*70-35, 
			top: seed_base_y + Math.random()*70-35, 
			width: 20, 
			height: 20
		}). 
		delay(Math.floor(Math.random()*800)).
		show().animate({ 
			left: [ offset_x, 'easeOutCubic' ],  
			top: [ 15 + Math.random()*32-16, 'easeInQuad' ], 
			width: s, 
			height: s
		}, 600, 'linear', function() {
			$(this).find('span').show(); 
		}); 
		
		offset_x += padding + Math.random()*24-16; 
	}); 
}

function h_letters_in() {
	$('.h_letter:hidden, .h_initial:hidden').each(function(i, item) {
		var ltype = $(item).attr('class'); 
		var lw; 
		var lp; 
		var ly;  
		if(ltype == 'h_initial') {
			lw = 45; lp = 178; ly = 13; 
		} else {
			lw = 32; lp = 278; ly = 25
		}
		
		$(item).css({
			left: seed_base_x + Math.random()*70-35, 
			top: seed_base_y + Math.random()*70-35, 
			width: 10, 
			height: 10
		}). 
		delay(Math.floor(Math.random()*800)).
		show().animate({ 
			left: lp + parseInt($(item).attr('rel')),
			top: ly, 
			width: lw, 
			height: lw
		}, 600, 'linear', function() {
			$(this).show();
		}); 
	})
}

function w_seeds_in() {
	$('.block, .subwindow').each(function(i, item) {
		var id = $(item).attr('id'); 
		var obj = window.blocks[id]; 
		var x = obj.attr.left; 
		var y = obj.attr.top; 
		var w = obj.attr.width;
		
		if(obj.open) {
			$(item).delay(i*60).show().animate({
					left: x+w/2, 
					top: [ y, 'easeInQuad' ]
				}, 
				750, 'easeOutCubic', function() {
					$(this).addClass('block-init'). 
					animate(obj.attr, 800, function(){
						if(obj.url && $(this).is('.dynamic')) { 
							load_window_content($(this), obj.url); 
						}
						window_rollDown($(item)); 
						if(obj.last) {
							mainWindow_load_init(); 
							interaction_init(); 
							set_drag_boundaries(); 
						}
					}); 
			}).find('.seed').css(
				{width: 20, height: 20}
			). animate(
				{width: 50, height: 50}, 
				750, 'easeInQuad', function() {
					$(this).css('display', ''); 
				}
			); 
		}
	}); 
}


function seed_out(i, item, total) {
	var id = $(item).attr('id'); 
	var obj = window.blocks[id]; 
	var x = parseInt($(item).css('left')); 
	var y = parseInt($(item).css('top'));
	var w = parseInt($(item).css('width'));
	
	//alert(w);
	$(item).delay(i*180).animate({
		left: Math.floor(x+w/2), 
		top: y, 
		width: 60, 
		height: 60
	}, 
	800, function() {
		$(item).removeClass('block-init').
		//find('.block-window').hide().
		//siblings('.seed').show().
		//parent().
		animate({
			left: seed_base_x + Math.random()*70-35, 
			top: seed_base_y + Math.random()*70-35
		}, 
		750, 'easeInQuart', function() {
			if($(this).hide()) {
				if(i == total-1) {
					check_nut_close(); 
				}; 
			};
		}). 
		find('.seed').animate(
			{width: 20, height: 20}, 
			750, 'easeInQuad', function(){
				if($(this).css({display: ''})) { 
					check_nut_close(); 
				}
			}
		); 
	})
}


function check_nut_close() {
	//var visible = $('.block:visible, .fblock:visible, .subwindow:visible'); 
	var visible = leave_header ? $('.block:visible, .subwindow:visible') : $('.block:visible, .fblock:visible, .subwindow:visible'); 
	if(visible.length == 0) {
		$('#nut_open').hide(); 
		$('#nut_closed').show(); 
		window.nut_open = false; 
	}
}


function interaction_init() {
	//* 
	$('.block, .subwindow').draggable({
		distance: 0, 
		//opacity: 0.7, 
		handle: '.block_header', 
		stack: '.block, .subwindow', 
		zIndex: 99, 
		//snap: '#desktop', 
		//snapMode: 'both',
		//containment: [ 0, 0, $('body').width()-20, $('body').height()-20 ], 
		//containment: 'window', 
		stop: function(event, ui) {
			window.blocks[$(this).attr('id')].attr.left = ui.offset.left; 
			window.blocks[$(this).attr('id')].attr.top = ui.offset.top; 
		}
	})/*
	.click(function() {
		$(this).trigger('drag');
	})/* */
	; 
	$('.resizable').resizable({
		helper: 'ui-resizable-helper', 
		minHeight: 100, 
		minWidth: 100, 
		handles: 'all', 
		stop: function(event, ui) {
			window.blocks[$(this).attr('id')].attr.height = ui.size.height; 
			window.blocks[$(this).attr('id')].attr.width = ui.size.width;
			$(this).find('.scroll').tinyscrollbar(); 
		}
	}); 
	
	$(window).resize(function() {set_drag_boundaries()});

	
	$('.window_roll').unbind().click( function() {
		var b = $(this).parents('.block, .subwindow'); 
		if(b.hasClass('rolled')) {
			window_rollDown(b); 
		} else {
			window_rollUp(b); 
		}
	}); 
	/* */
}


function window_rollUp(b) {
	b.animate({height: 20}, 'fast', function() {
		$(this).find('.block_content').css('padding', 0); 
		img = $(this).find('.window_roll>img');  
		img.attr('src', img.attr('src').replace('up', 'down')); 
		$(this).addClass('rolled').find('.ui-resizable-handle, .scrollbar').hide();
	}); 
}


function window_rollDown(b) {
	var coords = window.blocks[b.attr('id')].attr; 
	b.find('.block_content').css('padding', ''); 
	b.find('.scrollbar').css('display', '');
	b.animate({height: coords.height}, 'fast', function() {
		img = $(this).find('.window_roll>img');  
		img.attr('src', img.attr('src').replace('down', 'up')); 
		$(this).removeClass('rolled').find('.ui-resizable-handle').css('display', '');
	}); 
}


function subWindow_in(id, url) {
	var sw = window.blocks[id]; 
	var block = $('#'+ id); 
	
	if(!url) 
		url = sw.url;
		
	if(!sw.open) {
		var x = sw.attr.left; 
		var y = sw.attr.top; 
		var w = sw.attr.width;
	
		block.css({
			left: seed_base_x, 
			top: seed_base_y, 
			width: 30, 
			height:30, 
			zIndex: 50
		}).show().
		animate({
			left: x+w/2, 
			top: [ y, 'easeInQuad' ]
		}, 600, 'easeOutCubic', function() {
			$(this).addClass('block-init'). 
			//find('.seed').hide().
			//siblings('.block-window').show().  
			//parent().
			animate(sw.attr, function() { 
				load_window_content($(this), url); 
			}); 
		}); 
	}
	load_window_content( block, url);
	window.blocks[id].open = true; 
}


function subWindow_out(id) {
	var sw = window.blocks[id]; 
	var block = $('#'+ id); 

	if(sw.open) {
		seed_out(0, block);
		window.blocks[id].open = false; 
	}
}

function subWindow_toggle(id, url) {
	if(window.blocks[id].open) {
		subWindow_out(id)
	} else {
		subWindow_in(id, url)
	}
}



function set_drag_boundaries() {
	var padY = 20 + parseInt($('#footer').outerHeight()); 
	var padX = 40; 
	$('.block, .subwindow').each(function() {
		if($(this).offset().left >= $('body').width() - padX) 
			$(this).css('left', $('body').width() - padX); 
		if($(this).offset().top >= $('body').height() - padY) 
			$(this).css('top', $('body').height() - padY); 
		
		$(this).draggable( 
			"option", "containment", 
			[ padX - $(this).width(), 0, $('body').width()- padX, $('body').height()- padY ]
		)
	});
}

function adaptBlocksToBody() {
	window.blocks.main.attr.width = Math.min( 900, Math.max(710, $('body').width() - window.blocks.main.attr.left - 20)); 

	window.blocks.main.attr.height = Math.min( 650, Math.max(415, $('body').height() - window.blocks.main.attr.top - 40)); 
}

