/*
Copyright (c) 2008-2010, Gigdoggy Inc.- Gregory Whiteside. All rights reserved.
gwhiteside@fanteraction.com
*/

function initialize_visualize(){
	style_containers = {"background":"","border":"","font":"","extras":""};
	structure_containers = {"display":"","positioning":"","text_structure":"","dimensions":"","margin":"","padding":""};
		
	$(".button2").each(function(){
		$(this).click(function(){$("> .button2", $(this).parent()).removeClass("selected"); $(this).addClass("selected");});
	});
		
	$("#structure_only").click(function(){show_structure();});
	$("#style_only").click(function(){show_normal();});
	$("#reposition").click(function(){enable_sorting();});
	
	bottom_y_coordinates = [];
	for(var elem in css_tree){
		automagic(elem);
	}
	magic_flag = false;
}

function restore_view(){
	if(typeof view != "undefined"){
	if(view == "structure") show_structure();
	else show_normal();
	}
	else show_normal();
}

function show_normal(callback){
	if(view == "structure"){
		view = "normal";
		try{
			$(".selected_item").width(css_backup.width);
		}catch(exception){}
		restore_structure_style();
		clear_automagic();
		try{
			$(css_backup.elem).css({border:"3px dashed #ffac55"});
		}catch(exception){}
		initialize_ui_state();
		fix_outline();
	}
}

function show_structure(){
	//if(typeof color == "undefined") var color = "#aaa"
	if(view == "normal"){
		var color = "#aaa";
		if(mode == "content") color = "blue";
		var skip_classes = ["content_actions"];
		view = "structure"
		var theme = all_css[css_context];
		save_structure_style();
	
		for(var elem in css_tree){
			var ui_elem = $(elem);
			var skip_class = false;
			for(var i = 0; i < skip_classes.length; i++) if($(ui_elem).hasClass(skip_classes[i])) {skip_class = true;break;}
			if((ui_elem.length > 0) && (elem != "body") && (elem != "#container") && (elem != ".selected") && !skip_class){
				var elem_type = $(ui_elem)[0].nodeName.toLowerCase();
				if(elem_type != "input" && elem_type != "button")
				{
					$("#contentHolder").css({background:"#fff"});
					if(elem_type == "a") $(elem).css({color:"#003399"});
					else $(elem).css({color:"#000"});
					$(elem).css({border:"1px solid " + color});
					$(elem).css({background:"#fff"});
					//$(elem).css({background:"#ededed"});
					// awesome color generator: '#'+Math.floor(Math.random()*15592941).toString(16)
				}
			}
		}
		try{
			$(".selected_item").width(css_backup.width);
		}catch(exception){}
		try{
			$(css_backup.elem).css({border:"3px dashed #ffac55"});
		}catch(exception){}
		$("#contentHolder div").css({"-moz-box-shadow":"2px 2px 5px #aaa","-webkit-box-shadow":"2px 2px 5px #aaa;"});
		set_automagic();
		fix_outline();
	}
}

function set_automagic(){
	if(!magic_flag || (typeof magic_flag == "undefined")){
		$(".offset-left").each(function(){
			$(this).css({"margin-left":(parseInt($(this).css("margin-left").replace("px","")) - 1) + "px"});
		});
		$(".offset-top").each(function(){
			$(this).css({"margin-top":(parseInt($(this).css("margin-top").replace("px","")) - 1) + "px"});
		});
		/*$(".offset-width").each(function(){
			var border_width = $(this).outerWidth() - $(this).innerWidth();
			$(this).css({"width":(parseInt($(this).css("width").replace("px","")) - (2) ) + "px"});
		});*/
		magic_flag = true;
	}
}

function clear_automagic(){
	if(magic_flag || typeof magic_flag == "undefined"){
		$(".offset-left").each(function(){
			$(this).css({"margin-left":Math.floor(parseInt($(this).css("margin-left").replace("px","")) + 1) + "px"});
		});
		$(".offset-top").each(function(){
			$(this).css({"margin-top":Math.floor(parseInt($(this).css("margin-top").replace("px","")) + 1) + "px"});
		});
		/*$(".offset-width").each(function(){
			//$(this).css({"width":(parseInt($(this).css("width").replace("px","")) + 2) + "px"});
		});*/
		magic_flag = false;
	}
}

function automagic(elem,appendTo){
		if(typeof appendTo == "undefined") appendTo = false;
		if(typeof bottom_y_coordinates == "undefined") bottom_y_coordinates = [];
		
		// fix all the ui inconsistencies due to borders / paddings overlapping etc.. in wireframe mode
		var x_offset = $(elem).offset().left, y_offset = $(elem).offset().top;
		var height = $(elem).innerHeight(), width = $(elem).innerWidth();
		bottom_y_coordinates.push(y_offset + height);
	
		var parent_x_offset = $(elem).parent().offset().left, parent_y_offset = $(elem).parent().offset().top;
		var parent_width = $(elem).parent().innerWidth();
		
		if($(elem).prev().length > 0)
		var sibling_y_offset = $(elem).prev().offset().top;
	
		if((x_offset - parent_x_offset) <= 1){
			//$(elem).data("offset-left","true");
			$(elem).addClass("offset-left");
		}
		if((y_offset - parent_y_offset) <= 1){
			//$(elem).data("offset-top","true");
			$(elem).addClass("offset-top");
		}
		if(parent_width - width <= 1)$(elem).addClass("offset-width");
		
		if(appendTo) $(elem).addClass("offset-top");
		//if($(elem).data("bottom_y") != "true")
		for(var i = 0; i < bottom_y_coordinates.length; i++){
			var bottom_y = bottom_y_coordinates[i];
			var delta = Math.floor(y_offset - bottom_y);
			if((-1 <= delta) && (delta <= 2)){
				//$(elem).data("bottom_y","true");
				$(elem).addClass("offset-top");
				break;
			}
		}
}

function show_hidden_configuration(){
	$(".hidden_configuration").removeClass("hidden");
}

function show_containers(containers){
	$(".css_elem").addClass("hidden");
	for(var container in containers) eval("$(\"#"+container+"\").removeClass('hidden')");
	$("a",$("#css_menu")).addClass("hidden");
	$("a",$("#css_menu")).each(function(){
		if(default_properties[$(this).attr("links_to")].container in containers) $(this).removeClass("hidden");
	});
}

/*for(var i = 0; i < ordered_css.length; i++){
	var elem = ordered_css[i];
	var ui_elem = $(elem);
	try{
		var elem_type = ""
		try{ elem_type = $(ui_elem)[0].nodeName.toLowerCase();}catch(exception){}
		if(elem_type != "input" && elem_type != "button"){
			try{
			for(var prop in all_css["default"]["default"][elem]){
				eval("$(\"" + elem + "\").css({\"" + prop + "\": \""+ all_css["default"]["default"][elem][prop].trim() + "\"});");
			}}catch(exception){}
			try{
			for(var prop in all_css[css_context]["default"][elem]){
				eval("$(\"" + elem + "\").css({\"" + prop + "\": \""+ all_css[css_context]["default"][elem][prop].trim() + "\"});");
			}}catch(exception){}
		}
	}catch(exception){}
}*/
