$(document).ready(function () {
    //# Tabs
    $("#tabs.companies").tabs();
    $("#tabs.media").tabs();

    $("#keywords").keypress(
      function (e) {
          if (e.which == 13) {
              document.location.href = '/search-results.aspx?q=' + $("#keywords").val();
              return false;
          }
      }
    );

    //# Pop up contact form
    $("a.contact").click(function () {
        $("#modal").dialog("open");

        var companyid = $(this).attr("id").replace("company", "");

        if (companyid > 0) {
            var imagePath = "/assets/images/logos/colour/" + companyid + ".gif";
            $(".modallogo").attr("src", imagePath);
        }

        var hiddenField = $("#ctl00_cphContent_ucPopUpContact_hfEmail");
        if (hiddenField.length > 0) {
            hiddenField.val(companyid);
        }
        return false;
    });

    $("#modal").dialog({
        autoOpen: false,
        modal: true,
        resizable: false
    });

    //# Fix issue where modal box is outside of the form
    $("#modal").parent().appendTo($("form:first"));


    //# Images
    //# Cancel the click event - no more lightbox
    $('#article #innercontent #images a.maincontainer').click(function () { return false; });
    $('#article #innercontent #images ul.thumbs li a').click(function () { return false; });
    $('div#images img.mainimage').click(function () { return false; });

    var initimg = $('.mainimage').attr("src");

    jQuery('ul.thumbs a').hover(
        function () {
            var newimg = jQuery(this).attr("href").replace('home/', '');
            jQuery('.mainimage').attr("src", newimg);
        },
        function () {
            jQuery('.mainimage').attr("src", initimg);
        }
    );

    //# Lightbox initialisation
    //$('a[rel="lightbox"]').lightBox();

    //# Video tab for articles
    jQuery("#tabs ul li a").click(function () {
        //load first
        var videoid = $("#videos a:first").find('img').attr("id");
        if (videoid != null) {
            videoid = videoid.replace('vid', '');
            LoadVideo(videoid);
        }
    });

    //# Load video from thumbnail
    jQuery("#videos a").click(function () {
        var videoid = $(this).find('img').attr("id").replace('vid', '');
        LoadVideo(videoid);
        return false;
    });

    //# Equal height panels
    //$('#article .panel p').equalHeight();

    //# Misc
    $("ul.articles li:last").addClass('last');
    $("ul.logos li:last").addClass('last');
});

function LoadVideo(vid) {
    $('#video').empty();
    $('#video').load('/assets/handlers/video.ashx?videoid=' + vid);
}
