jQuery(function($){
	if($("#add-search .toggle").length){
		$("#add-search .toggle").click(function(){
			$(this).parents("#add-search").toggleClass("show-add-button");
			return false;
		});
	}
	
	function changeSelect(el){
		$("select",el).wrap('<div class="select-wrap"></div>').after('<div class="text"><span></span></div><ul></ul>').css({
			position:"absolute",left:"-9999px"
		 });
		 
		var elem = $("select",el).parent();
		
		$("select",el).find("option").each(function(){
			var text=$(this).text();
			var value=$(this).val();
			elem.find("ul").append('<li val="'+value+'"><a href="#">'+text+'</a></li>');
			if($(this).is(":selected")){
				elem.find("li:last").addClass("selected");
				elem.find(".text span").text(text);
			}
		});
		
		$("#header .input").css("margin-right", el.outerWidth() + 16 + "px");
		
		el.find(".select-wrap .text").live("click",function(){
			var element=$(this);
			$(".select-wrap ul").not(element.siblings("ul")).slideUp();
			element.siblings("ul").slideToggle(400);
		});
		
		$("html,body").live("click",function(e){
			if(!$(e.target).parents(".select-wrap").length){
				$(".select-wrap ul").slideUp();
			}
		});
		
		el.find(".select-wrap ul a").live("click",function(e){
			$(this).parent().addClass("selected").siblings().removeClass("selected").parents(".select-wrap").find(".text span").text($(this).text());
			
			$("#header .input").css("margin-right", el.outerWidth() + 16 + "px");
			
			var value=$(this).parent("li").attr("val");
			$(this).parents(".select-wrap").find("select").val(value);
			$(".select-wrap ul").slideUp();
			
			/*var index = $(this).parent("li").index();
			
			switch(index){
				case 0: targetText.text("All files");
				break;
				case 1: targetText.text("Archives");
				break;
				case 2: targetText.text("Audio");
				break;
				case 3: targetText.text("Video");
				break;
				case 4: targetText.text("Docs");
				break;
				case 5: targetText.text("Pics");
				break;
				case 6: targetText.text("Apps");
				break;
				case 7: targetText.text("CD/DVD");
				break;
			}*/
			
			return false;
		});
	}
	
	if($("#search-block .wrap-select").length){
		changeSelect($("#search-block .wrap-select"));
		$("#search-block .select-wrap ul li:first a").addClass("first");
		$("#search-block .select-wrap ul li:last a").addClass("last");
	}
	
	if($("#search-block  .input input").length){
		$("#search-block  .input input").focus(function(){
			$(this).parents(".wrap-input").addClass("focus");
		}).blur(function(){
			$(this).parents(".wrap-input").removeClass("focus");
		});
	}
	
	$("body.index").length ?
		$(window).load(function(){
			$("#search-block  .input input").focus();
		}) : null;
	
	$(".scrollable").length ?
		$(".scrollable").scrollable({
			circular:true
		}) : null;
	
	$(".file-copy-link .line").length ?
		$(".file-copy-link .line .wrap-input input").click(function(){
			$(this).select();
		}) : null;
	
	if($(".file-copy-link .line .copy-wrap").length){
		$(".file-copy-link .line .copy-wrap").each(function(index){
			$(this).attr("id","copy-id-" + index).find("a.copy-link").attr("id","copy-link-" + index);
			
			var clip = new ZeroClipboard.Client();
			ZeroClipboard.setMoviePath( 'http://uma.progerlab.ru/variant-2/js/ZeroClipboard.swf');
			clip.setHandCursor( true );
	 
			var text=clip.setText($(this).parents(".line").find("input[type=text]").val());
	 
			clip.addEventListener('complete', function (client, text) {
				alert("The code:\n" + text + "\n is copied");
			});
	 
			clip.glue("copy-link-" + index, "copy-id-" + index);
		});
	}
	
	if($("#left ul.all-systems").length){
		$("#left ul.all-systems li:gt(4)").hide();
		
		$("#left span.show-more").click(function(){
			if($(this).is(".hide")){
				$(this).text("show more").removeClass("hide").parents(".nav").find("ul.all-systems").find("li:gt(4)").slideUp(200);
			}else{
				$(this).text("hide more").addClass("hide").parents(".nav").find("ul.all-systems").find("li:gt(4)").slideDown(200);
			}
		});
	}
	
	$("#right .our-code textarea").length ?
		$("#right .our-code textarea").click(function(){
			$(this).select();
		}) : null;
});
