// Dynamic extension for web site "Skola pro vycvik vodicich psu" by CJ Neti

// Set up

if (typeof($)=="undefined")  throw("Required JQuery library is not loaded.");
$(function(){
// Adding ARIA properties to the layout
$("div.menu-main").attr({
 role:"navigation",
 "aria-labelledby":"hdr-menu-main"
});
$("div.menu-context").attr({
 role:"navigation",
 "aria-labelledby":"hdr-menu-context"
});
$("#content").attr({
 role:"main",
 "aria-labelledby":"hdr-content"
}).focus();
$("#footer").attr({
 role:"contentinfo",
 "aria-labelledby":"hdr-footer"
});
$("#logobox").attr("role","banner");
$("div.complementary").attr("role","complementary");
$("div.dialog").attr("role","dialog");
$(".alert, .fail").attr("role","alert");
// Adding ARIA and other properties to specific elements
$("form .required").attr("aria-required",true);
$("form .disabled").attr("disabled","disabled");
});

// Common functions

function openWindow(uri)
{
return (!window.open(uri));
}

function openPopup(uri)
{
return (!window.open(uri,'popup','width=200,height=200'));
}

function goBack()
{
history.go(-1);
return false;
}

// Forum functions

function focusEmpty(form)
{
var found=false;
$(".required:input",form).each(function(){
if ($(this).val()=="")
{
$(this).focus();
found=true;
}
return !found;
});
return found;
}

function toggleForumApp(appMain,appForm,appFocus)
{
var speed=500;
appMain="#"+appMain;
appForm="#"+appForm;
$(appMain).slideToggle(speed,function(){
$(appForm).slideToggle(speed,function(){
if ($(appMain).is(":visible"))
{
$(appMain+" p.alert, "+appMain+" p.fail").remove();
if (typeof($(appMain).data("appFocus"))!="undefined")
{
$(appMain).data("appFocus").focus();
$(appMain).removeData("appFocus");
}
}
else if (typeof(appFocus)!="undefined") $(appMain).data("appFocus",appFocus);
$(appForm+" form").each(function(){
if ($(appForm).is(":visible")) focusEmpty(this);
else this.reset();
});
});
});
return false;
}

function validateFilledForm(form)
{
invalid=focusEmpty(form);
if (invalid) alert("Nejsou vyplněna všechna povinná pole.");
return !invalid;
}

function syncConfirm(checkbox)
{
submit=$(":input[type='submit']",checkbox.form);
if (checkbox.checked) submit.removeAttr("disabled");
else submit.attr("disabled","disabled");
}

// ForumEvents class

ForumEvents.prototype={
bindReply:function(replyClass,appForm)
{
appMain=this.appMain;
$("."+replyClass).click(function(){
post=$(this).parents(".forum-post");
quote=$("#"+appForm+" .quote");
if (post.length>0)
{
subject=$("span.subject",post).text();
if (subject.substr(0,4).toUpperCase()!="RE: ") subject="Re: "+subject;
$("#"+appForm+" :input[name='subject']").val(subject);
$(".author",quote).text($("em",post).text());
if ($("em",post).hasClass("trusted")) $(".author",quote).addClass("trusted");
else $(".author",quote).removeClass("trusted");
$(".date",quote).text(post.parents(".day").find("h3").text());
$(".time",quote).text($("i",post).text());
$("blockquote",quote).empty().append($("p",post).clone());
quote.show();
}
else quote.hide();
$("#"+appForm+" :input[name='reply']").val($(".id",post).attr("name"));
return toggleForumApp(appMain,appForm,this);
});
},

bindSeal:function(sealClass,appSeal,activator)
{
aggregator=new ForumActionAggregator(this.appMain,appSeal);
if (!aggregator.registerActivator(activator)) return;
$("."+sealClass).click(function(){
aggregator.toggle(this);
});
$($("."+sealClass).get(0).form).bind("submit",function(){
return false;
});
}

}
function ForumEvents(appMain)
{
this.appMain=appMain;
}

// ForumActionAggregator class

ForumActionAggregator.prototype={

toggle:function(sender){
post=$(sender).parents(".forum-post");
before=this.posts.length;
if (sender.checked) this.posts.push(post);
else
{
var found=-1;
$.each(this.posts,function(i){
if ($(".id",this).attr("name")==$(".id",post).attr("name")) found=i;
return (found==-1);
});
if (found>=0) this.posts.splice(found,1);
}
if (this.activator!=null)
{
if (this.posts.length>0&&before==0) this.activator.removeAttr("disabled");
else if (this.posts.length==0&&before>0) this.activator.attr("disabled","disabled");
}
},

update:function(ref){
sumList=$("ul",ref.sumComponent);
$(">*:not(.template)",sumList).remove();
$.each(ref.posts,function(){
item=$("li.template",sumList).clone();
$(":input",item).removeAttr("disabled").val($(".id",this).attr("name"));
$("span",item).text($("span:not(.subject)",this).text());
item.removeClass("template").show();
sumList.append(item);
});
},

clear:function(){
this.sumComponent.hide();
this.posts=new Array();
},

registerActivator:function(control){
if (this.activator!=null)
{
this.activator.unbind("click");
this.activator.removeAttr("disabled");
}
if (control!=null)
{
control=$("#"+control);
if (control.length==0) return false;
if (this.posts.length==0) control.attr("disabled","disabled");
ref=this;
control.click(function(){
ref.update(ref);
return toggleForumApp(ref.appMain,ref.appSum,this);
});
}
this.activator=control;
return true;
}

}
function ForumActionAggregator(appMain,appSum)
{
this.appMain=appMain;
this.appSum=appSum;
this.sumComponent=$("#"+appSum);
this.clear();
this.registerActivator(null);
}

// SectionSwitcher class

SectionSwitcher.prototype={

show:function(ref,section)
{
$("div:visible",ref.container).each(function(){
if (ref.speed>0) $(this).slideUp(ref.speed);
else $(this).hide();
element=$(ref.selector+"."+$(this).attr("class"),ref.container);
element.click(function(){
ref.show(ref,this);
return true;
}).attr("href",element.data("hrefbak")).removeClass("selected");
});
element=$("div."+$(section).attr("class"),ref.container);
if (ref.speed>0) element.slideDown(ref.speed);
else element.show();
window.location.hash=$(section).attr("href");
$(section).unbind("click").removeAttr("href").addClass("selected");
}

}
function SectionSwitcher(container,defaultSection)
{
this.container=$("."+container);
if ($(">:header",this.container).length>0)
{
$(">ul",this.container).hide();
this.selector=":header";
this.speed=500;
}
else
{
this.selector="ul";
this.speed=0;
}
this.selector=">"+this.selector+" a";
ref=this;
$(this.selector,this.container).each(function(){
$(this).click(function(){
ref.show(ref,this);
return true;
}).data("hrefbak",$(this).attr("href"));
});
$("div:visible",this.container).hide();
$(this.selector+"[href='#"+defaultSection+"']",this.container).click();
}
