var GB_ROOT_DIR = "/public/scripts/greybox/";

$().ready(function(){
	if ($("#userStory").length > 0){
		$("#soubory_nazvy").attr("id","soubory_nazvy-1");
		$("#soubory").after($("#soubory_nazvy-1"));
		$("#soubory").after("&nbsp;Popis:&nbsp;");
		$("input.multifile").change(function(){
			$(this).attr("name","soubory[]");
			$(this).attr("id","soubory-1");
			
			addElementFile($(this))
		});
	}
	$("div.menu-kategorie ul li:has(ul)").addClass("menu-expandeable");
	
	$("div.menu-kategorie ul li ul").css("display","none");
	
	$("div.menu-kategorie ul li.menu-expandeable").hover(function(){
		$(this).toggleClass("menu-expandeable-hover");
	},function(){
		$(this).toggleClass("menu-expandeable-hover");		
	});
	
	
	subm = $("div.menu-kategorie ul li.menu-expandeable a[href='"+window.location.pathname+"']").parent().parents("ul").show();
	$("div.menu-kategorie ul li a[href='"+window.location.pathname+"']").addClass("menu-current");
	
	$("div.menu-kategorie ul li.menu-expandeable a").click(function(){
		if ( $(this).parent().children("ul").css("display") == "none") {
			$(this).parent().children("ul").show();
			$(this).parent().removeClass("menu-expandeable");
			$(this).parent().addClass("menu-expanded");
			return false;
		}else if ($(this).parent().hasClass("menu-expanded")){
			$(this).parent().children("ul").hide();
			$(this).parent().removeClass("menu-expanded");
			$(this).parent().addClass("menu-expandeable");
			return false;
		}
		
	});
	
	externalLinks();	
});

function externalLinks() { 
    if (!document.getElementsByTagName) return; 
   var anchors = document.getElementsByTagName("a"); 
       for (var i=0; i<anchors.length; i++) { 
          var anchor = anchors[i];
          if (anchor.getAttribute("href") && anchor.getAttribute("rel") != "internal"  && (!anchor.getAttribute("id") || anchor.getAttribute("id") && anchor.getAttribute("id").indexOf("recaptcha")<0)) {
            anchor.target = "_blank"; 
          }
       } 
} 

function addElementFile(el) {
	ix = el.attr("id").charAt(8);
	elt = $("#soubory_nazvy-"+ix);
	ix ++;
	if ($("#soubory-"+ix).length == 0){
		elt.after('<br /><input name="soubory[]" id="soubory-'+ix+'" size="20" class="multifile" type="file" />&nbsp;Popis:&nbsp;<input name="soubory_nazvy[]" id="soubory_nazvy-'+ix+'" class="multifile-name" type="text" />');
		$("#soubory-"+ix).change(function(){
			addElementFile($(this));
		});
	}
}