$j(document).ready(function() {
	box_links_spread.init();
	btns_spread.init();
	reduce.init();
});

box_links_spread = {
	init : function() {
		width_available = 192;
		min_width = 96;
		$j("#boxes div.links").each(function (i) {
			var width_used = 0;
			var kids = $j(this).children();
			var uneven = false;
			kids.each(function(j) {
				width_used += $j(this).width();
				if($j(this).width() > min_width)
					uneven = true;
			});
			if (uneven) {
				kids.each(function(j){
					if ($j(this).width() < min_width) {
						//$j(this).width($j(this).width() + width_available - width_used);
						$j(this).find('span').width($j(this).find('span').width() + width_available - width_used);
					}
				});
			} else {
				kids.each(function(j){
					difference = min_width - $j(this).width();
					$j(this).find('span').width($j(this).find('span').width()+difference);
					//$j(this).width(min_width);	
				});
			}
		});
	}
}

reg_box_links_spread = {
	init : function() {
		width_available = 192;
		min_width = 95;
		$j(".box div.links").each(function (i) {
			var width_used = 0;
			var kids = $j(this).children();
			var uneven = false;
			kids.each(function(j) {
				width_used += $j(this).width();
				if($j(this).width() > min_width)
					uneven = true;
			});
			if (uneven) {
				kids.each(function(j){
					if ($j(this).width() < min_width) {
						//$j(this).width($j(this).width() + width_available - width_used);
						$j(this).find('span').width($j(this).find('span').width() + width_available - width_used);
					}
				});
			} else {
				kids.each(function(j){
					difference = min_width - $j(this).width();
					$j(this).find('span').width($j(this).find('span').width()+difference);
					//$j(this).width(min_width);
				});
			}
		});
	}
}

expandable_box_links_spread = {
	init : function() {
		width_available = 274;
		min_width = 132;
		$j(".expandable div.links").each(function (i) {
			var width_used = 0;
			var kids = $j(this).children();
			var uneven = false;
			kids.each(function(j) {
				width_used += $j(this).width();
				if($j(this).width() > min_width)
					uneven = true;
			});
			if (uneven) {
				kids.each(function(j){
					if ($j(this).width() < min_width) 
						//$j(this).width($j(this).width() + width_available - width_used);
						$j(this).find('span').width($j(this).find('span').width() + width_available - width_used);
				});
			} else {
				kids.each(function(j){
					//$j(this).width(min_width);	
					difference = min_width - $j(this).width();
					$j(this).find('span').width($j(this).find('span').width()+difference);
				});
			}
		});
	}
}

btns_spread = {
	init : function() {
		min_width = 96;
		$j(".btn").each(function (i) {
			if ($j(this).width() < min_width) {
				//$j(this).width(min_width);
				difference = min_width - $j(this).width();
				$j(this).find('span').width($j(this).find('span').width() + difference);
			}
		});
		$j("button").each(function (i) {
			if ($j(this).width() < min_width) {
				//$j(this).width(min_width);
				difference = min_width - $j(this).width();
				$j(this).width($j(this).width() + difference);
			}
		});
	}
}

replace_greeting = {
	init : function() {
		var d = new Date()
		var time = d.getHours()
		
		if (time < 12) {
			Element.update($('greeting'), "Good morning")
		} else if (time>=12 && time<18) {
			Element.update($('greeting'), "Good afternoon")
		} else {
			Element.update($('greeting'), "Good evening")
		} 
	}
}

reduce = {
	init : function() {
		number_reduce = 0;
		number_id = 0;
		reduce_by = 224;
		max_height = 0;
		subsequent = false;
		shadow_dis = 2;
		$j(".item.reduce").each(function (i) {
			var item = $j(this).find(".left");
			// Height
			if ($j(this).find('.left').height() > max_height) 
				max_height = $j(item).height();
			boxes = $j(this).find(".box");
			if(!item.hasClass(".first")) {
				boxes_height = 0
				for (i = 0; i < boxes.length; i++) {
					boxes_height += $j(boxes[i]).height();
				}
				if(max_height > boxes_height) {
					for (i = 0; i < boxes.length; i++) {
						$j(boxes[i]).height(max_height/boxes.length - shadow_dis);
					}
				} else {
					$j(item).height(boxes_height + (boxes.length*shadow_dis));
				}
			} else {
				for (i = 0; i < boxes.length; i++) {
					if ($j(boxes[i]).height() > max_height) 
						max_height = $j(boxes[i]).height();
				}
				if(max_height > $j(item).height())
					$j(item).height(max_height);
				else
					$j(item).height(max_height);
				for (i = 0; i < boxes.length; i++) {
					$j(boxes[i]).height(max_height);
				}
			}
		});
		reg_box_links_spread.init();
		expandable_box_links_spread.init();
	}
}