var categories = [ "books", "magazines", "websites", "identities", "graphics",
		"exhibitions", "scenography" ];
var catSelected = [];
$(window).load(function() {
	$.getScript("js/jquery.cycle.all.js", function() {
		$.getScript("js/jquery.slider.js", function() {
			$.getScript("js/jquery.swfobject.js", function() {
				$.getScript("js/jquery.json.js", function() {
					init();
					carrusel();
				});
			});
		});
	});
});
function activate(){
	$('#content li').find("div").click(function() {
		document.location.href = $(this).attr("link");
	});
}
function init() {
	$(".contNoComercial").slider( {
		left : "#prevBtn",
		rigth : "#nextBtn"
	});
	if ($("#swf").length > 0) {
		$('#swf').flash( {
			// test.swf is the flash document
			swf : 'swf/proxibuchstaben.swf',
			height : 258,
			width : 945

		});
	}
	$(".contNoComercial").find("li").click(
			function() {
				if ($(this).attr("link") != 1)
					document.location.href = "noncomercial.php?name="
							+ $(this).attr("title");
				else
					window.open($(this).attr("url"));
			});
	$(".submenu li").find("a#design").css("background-position", "0px -16px");
	$(".submenu li").find("a#design").attr("data-active", 1);

	$(".submenu li a").click(function() {
		if ($(this).attr("data-active") == 0) {
			$(this).css("background-position", "0px -16px");
			$(this).attr("data-active", 1);
			showCategories($(this).attr("id"));
		} else {
			$(this).css("background-position", "0px 0px");
			$(this).attr("data-active", 0);
			removeCat($(this).attr("id"))
		}
		return false;
	});
	$(".conejo").click(function() {
		if ($(this).attr("type") == 0) {
			$(this).attr("type", 1);
			$("#nav").animate( {
				height : 380
			}, 600);
		} else {
			$(this).attr("type", 0);
			$("#nav").animate( {
				height : 0
			}, 600);
		}
	});
	activate();
	
}
function carrusel() {

	$('#content li.carrusel').cycle( {
		fx : 'scrollLeft',
		speed : 300,
		timeout : 3000,
		pause : 1
	});
	$('#content li.carrusel').cycle('pause');
	$("#content li.carrusel").bind("mouseenter", function() {
		$(this).cycle('resume');
	});
	$("#content li.carrusel").bind("mouseleave", function() {
		$(this).cycle('pause');
	});
}
function showCategories(cat) {

	if (cat != "design") {
		catSelected.push(cat.toLowerCase());
		if ($(".submenu li").find("a#design").attr("data-active") == 1) {
			$(".submenu li a#design").attr("data-active", 0);
			$(".submenu li a#design").css("background-position", "0px 0px");
		}
	} else {
		catSelected=[];
		$(".submenu li").find("a:not(#design)").attr("data-active", 0);
		$(".submenu li").find("a:not(#design)").css("background-position",
				"0px 0px");

	}
	var data_ = {cat : $.toJSON(catSelected)};
	if(catSelected.length==0)var data_ = {cat : "all"}; 
	$.post("new/admin/tofront/refreshHome.php", data_, function(e) {
		if (e.status)
			$("#content").html(e.list);
			activate();
		
	}, "json");
}
function removeCat(cat) {
	if (cat != "design") {
		var _in = false;
		var newCat = [];
		for (i = 0; i < catSelected.length; i++) {
			if (catSelected[i] != cat.toLowerCase())
				newCat.push(catSelected[i].toLowerCase());
		}
		catSelected = newCat;

		if (catSelected.length > 0) {

			for (i = 0; i < categories.length; i++) {
				$("#content").find("li#" + categories[i] + "").hide();
			}
			for (m = 0; m < catSelected.length; m++) {
				$("#content").find("li#" + catSelected[m] + "").show();
			}
		} else {

			for (i = 0; i < categories.length; i++) {
				$("#content").find("li#" + categories[i] + "").show();
			}

			$(".submenu li").find("a#design").attr("data-active", 1);
			$(".submenu li").find("a#design").css("background-position",
					"0px -16px");
		}

		// if(!_in)catSelected.push(cat);
	} else {

		if (catSelected.length == 0) {
			for (i = 0; i < categories.length; i++) {
				$("#content").find("li#" + categories[i] + "").show();
			}
		} else {
			for (i = 0; i < categories.length; i++) {
				$("#content").find("li#" + categories[i] + "").hide();
			}
			for (i = 0; i < catSelected.length; i++) {
				$(".submenu li").find("a#" + catSelected[i] + "").attr(
						"data-active", 1);
				$(".submenu li").find("a#" + catSelected[i] + "").css(
						"background-position", "0px -16px");

				$("#content").find("li#" + catSelected[i] + "").show();
			}
		}

	}

	var data_ = {
		cat : $.toJSON(catSelected)
	};
	if (catSelected.length == 0)
		data_ = {
			cat : "all"
		}
	$.post("new/admin/tofront/refreshHome.php", data_, function(e) {
		if (e.status)
			$("#content").html(e.list);
			activate();
		
	}, "json");

}

