var context = context || "/ialecture";
$(function(){
    $(document).pngFix();
    if(location.hash!="")
        $("nav[rel="+location.hash.substring(1)+"]").trigger('click')
    else if(location.href.indexOf("previous")>0){
        $("#content").css({width:680,"padding-right":30})
        $('#leftnav a:eq(0)').trigger("click")
    } else if(location.href.match(/\/(lecture|speaker|details|rsvp)\.jsp/)){
        $("#content").css({width:580,"padding-right":130})
    } else {
        $("#content").css({width:680,"padding-right":30})
    }

    $("nav").each(function(){
        var href = $(this).find("a").attr("href")
        var img = $(this).find("img").attr("src")
        var alt = $(this).find("img").attr("alt")
        if(img.indexOf("_d.gif")>0)
            $(this).html("<a href='"+href+"' style='display:block;overflow:hidden;'>"+
                "<img class='up' src='"+(img.replace("_d.gif","_u.gif"))+"' alt='"+alt+"'/>"+
                "<img class='down' src='"+img+"' style='position:relative;top:-200px;' alt='"+alt+"'/>"+
                "</a>");
        else
            $(this).html("<a href='"+href+"' style='display:block;overflow:hidden;'>"+
                "<img class='up' src='"+img+"' alt='"+alt+"'/>"+
                "<img class='down' src='"+(img.replace("_u.gif","_d.gif"))+"' style='position:relative;top:-200px;display:none;' alt='"+alt+"'/>"+
                "</a>");
    });
});
$("nav")
    .live("mouseenter",function(){
        var img = $(this).find("img.up").attr("src");
        if(img.indexOf("_u.gif")>0){
            img = img.replace("_u.gif","_o.gif");
            $(this).find("img.up").attr("src",img);
        }
    }).live("mouseout",function(){
        var img = $(this).find("img.up").attr("src");
        if(img.indexOf("_o.gif")>0){
            img = img.replace("_o.gif","_u.gif");
            $(this).find("img.up").attr("src",img);
        }
    }).live("click", function(){
        $(this).find("img")
            .attr("src",function(){
                return $(this).attr("src")
                    .replace("_o.gif","_d.gif")
                    .replace("_u.gif","_d.gif")
            })
        document.title = $(this).find("img").attr("alt")+" - International Arbitration Lecture | Clayton Utz";
        location.hash = $(this).attr("rel");
        $(this).siblings("nav").find("img")
            .attr("src",function(){
                return $(this).attr("src")
                    .replace("_d.gif","_u.gif")
                    .replace("_o.gif","_u.gif")
        });
        var url = $(this).find("a").attr("href");
        _gaq.push(['_trackPageview',url]);
        $("#content").wrapInner("<div class='fadeout'/>").children(".fadeout").fadeOut();
        $.get(url,{
            fmt:"html"
        },function(data){
            $("#content").html(data);
            if(location.hash=="#previous"){
                $("#content").css({width:680,"padding-right":30})
                var year = window.year || "";
                year = ""+year;
                if(year.match(/^\d{4}$/))
                    year = "[title^='"+year+"']"
                else
                    year = ":eq(0)"
                $('#leftnav a'+year).trigger("click")

            } else {
                $("#content").css({width:580,"padding-right":130})
            }
        },"html");
        return false;
    });
$(".ia_link").live("click", function(){
    var url = $(this).attr("href");
    if(url.indexOf("lecture.jsp")>=0)
        $('nav[rel=\'lecture\']').trigger('click');
    else if(url.indexOf("speaker.jsp")>=0)
        $('nav[rel=\'speaker\']').trigger('click');
    else if(url.indexOf("details.jsp")>=0)
        $('nav[rel=\'details\']').trigger('click');
    else if(url.indexOf("rsvp.jsp")>=0)
        $('nav[rel=\'rsvp\']').trigger('click');
    else if(url.indexOf("previous.jsp")>=0)
        $('nav[rel=\'previous\']').trigger('click');
    else {

        window.year = url.match(/\d{4}/);
        if(year.length>0){

            $('nav[rel=\'previous\']').trigger('click');
//            $("#leftnav a[title^='2009']")
        }
    }
    return false;
})
function clearNav(){
    $("nav img")
        .attr("src",function(){
            return $(this).attr("src")
                .replace("_o.gif","_u.gif")
                .replace("_d.gif","_u.gif")
        })
}

$("#leftnav a").live("click", function(){
    document.title = $(this).attr("title")+" - International Arbitration Lecture | Clayton Utz";
    $(this).css("color","#ac0602")
    .siblings("a").css("color","")
    var url = $(this).attr("href");
    _gaq.push(['_trackPageview',url]);
    $.get(url,{
        fmt:"html"
    },function(data){
        $("#previous_lecture").html(data);
        $('#previous_lecture > div').hide();
        $('#previous_lecture > h2').css('cursor','pointer').click(function(){
            var name = document.title + " / " + $(this).text();
            _gaq.push(['_trackEvent','Previous_Lecture', 'Open_Section', name]);
            $(this).css("border-top","1px dashed #CCC")
            .siblings("h2").css("border-top","1px solid #FFF")
            $(this).next("div").css("border-bottom","1px dashed #CCC").slideDown(700)
            .siblings("div").css("border-bottom","1px solid #FFF").slideUp()
        });
        $('#previous_lecture > h2:first').trigger("click")
    },"html");
    return false;
});

$('a.video').live("click", function(){
    var title = $(this).attr("href");
    title = title.substr(0, title.lastIndexOf("."));
    flowplayer("video",context+"/images/flowplayer.cu.nostream.swf",{
        clip:title+".flv",
        plugins:{
            controls: {
                autoHide:'always'
            },
            gatracker: {
                url: context+"/images/flowplayer.analytics.swf",
                trackingMode: "Bridge"
            }
	}
    });
    return false;
})

function initVideo(){
    var playlist = [];
    // Add thumbnail
    playlist.push($("#playlist a img:eq(0)").attr("src"));

    // Add all videos
    var autoPlay = false;
    $("#playlist a").each(function(){
        var title = $(this).attr("href");
        title = title.substr(0, title.lastIndexOf("."));
        playlist.push({
            url:title + ".flv",
            autoPlay:autoPlay
        });
        autoPlay = true;
    })

    var controls = {autoHide:'always'};
    if(playlist.length>2)
        controls = {autoHide:'always',playlist:true};

    // Create player
    flowplayer("video",context+"/images/flowplayer.cu.nostream.swf",{
        playlist:playlist,
	plugins: {
            controls:controls,
            gatracker: {
                url: context+"/images/flowplayer.analytics.swf",
                trackingMode: "Bridge"
            }
        }
    }).play();
}
function initAudio(){
    $("#playlist a").each(function(){
        try{
            var link = $(this).attr("href");
            var id = link.substring(link.lastIndexOf("/")+1,link.lastIndexOf(".mp3"));
            $(this).attr("id",id)
                .html("")
                .addClass("audio");
            flowplayer(id, context+"/images/flowplayer.cu.nostream.swf",{
                clip:{url:link,autoPlay:false},
                plugins:{
                    audio:{
                        url:context+"/images/flowplayer.audio.swf"
                    },
                    gatracker: {
                        url: context+"/images/flowplayer.analytics.swf",
                        trackingMode: "Bridge"
                   }
                }
            })
        }catch(e){}
    });
}
function initAudio_old(audio){
    var playlist = [];

    var autoPlay = false;
    $("#playlist a").each(function(){
        var title = $(this).attr("href");
        playlist.push({
            url:title,
            autoPlay:autoPlay
        });
        autoPlay = true;
    })
    
    var controls = {};
    if(playlist.length>2)
        controls = {playlist:true};

    var fp = flowplayer("audio", context+"/images/flowplayer.cu.nostream.swf",{
        playlist:playlist,
        plugins:{
            audio:{
                url:context+"/images/flowplayer.audio.swf"
            },
            controls:controls,
            gatracker: {
                url: context+"/images/flowplayer.analytics.swf",
                trackingMode: "Bridge"
           }
        }
    }).load();
}
function initPhoto(){
    $("#photos a").lightBox({
        imageLoading:  context+'/images/lightbox-ico-loading.gif',
	imageBtnClose: context+'/images/lightbox-btn-close.gif',
	imageBtnPrev:  context+'/images/lightbox-btn-prev.gif',
	imageBtnNext:  context+'/images/lightbox-btn-next.gif'
    });
}

function rsvp(form){
    var params = $(form).serializeArray();
    var json = {};
    for(i in params)
        json[params[i].name] = params[i].value;
    $(".error").remove();
    if(json.firstname == "" || json.lastname == "")
        rsvpError($(form).find("input[name='firstname']"),"Please enter your name.");
    if(json.email == "")
        rsvpError($(form).find("input[name='email']"),"Please enter your email address.");
    else if(!json.email.match(/^(['_a-z0-9-]+)(\.['_a-z0-9-]+)*@([a-z0-9-]+)(\.[a-z0-9-]+)*(\.[a-z]{2,5})$/i))
        rsvpError($(form).find("input[name='email']"),"Please enter a valid email address.");
    if(json.company == "")
        rsvpError($(form).find("input[name='company']"),"Please enter your company.");
    if(typeof json.attending == "undefined" || json.attending == "")
        rsvpError($(form).find("input[name='attending']:eq(0)"),"Will you be attending?");
    if($(".error").length > 0)
        return false;

    _gaq.push(['_trackEvent','RSVP', 'Submit']);
    $.post(context+"/rsvp.jsp?fmt=html",params,function(data){
        $(form).html(data);
    },"html")
}
function register(form){
    var params = $(form).serializeArray();
    var json = {};
    for(i in params)
        json[params[i].name] = params[i].value;
    $(".error").remove();
    if(json.firstname == "" || json.lastname == "")
        rsvpError($(form).find("input[name='firstname']"),"Please enter your name.");
    if(json.email == "")
        rsvpError($(form).find("input[name='email']"),"Please enter your email address.");
    else if(!json.email.match(/^(['_a-z0-9-]+)(\.['_a-z0-9-]+)*@([a-z0-9-]+)(\.[a-z0-9-]+)*(\.[a-z]{2,5})$/i))
        rsvpError($(form).find("input[name='email']"),"Please enter a valid email address.");
    if(json.company == "")
        rsvpError($(form).find("input[name='company']"),"Please enter your company.");
    if(typeof json.attending == "undefined" || json.attending == "")
        rsvpError($(form).find("input[name='attending']:eq(0)"),"Will you be attending?");
    if($(".error").length > 0)
        return false;

    _gaq.push(['_trackEvent','RSVP', 'Submit']);
    $.post(context+"/register.jsp?fmt=html",params,function(data){
        $(form).html(data);
    },"html")
}
function rsvpError(element, msg){
    msg = "<div class='error' style='position: absolute; margin-left: 250px; color: red;'>" +
        msg +
        "</div>";
    element
        .parents("td")
        .prepend(msg);
}
function rsvpReset(){
    _gaq.push(['_trackEvent','RSVP', 'Reset']);
    $("form input[type='text']").val("");
    $("form input[type='radio']").attr("checked","");
}
// jQuery plugin - preloadImg
(function($) {
  var cache = [];
  // Arguments are image paths relative to the current page.
  $.preloadImg = function() {
    var args_len = arguments.length;
    for (var i = args_len; i--;) {
      var cacheImage = document.createElement('img');
      cacheImage.src = arguments[i];
      cache.push(cacheImage);
    }
  }
})(jQuery)
