function equalHeight(group)
{
    tallest = 0;
    group.each(function()
    {
        thisHeight = $(this).height();
        if (thisHeight > tallest)
        {
            tallest = thisHeight;
        }
    });
    if (minHeightCheck() == true)
    {
        group.css("min-height", tallest + "px");
    }
    else
    {
        //browser does not support min-height so we must use height instead	
        group.css("height", tallest + "px");
    }
}

//We need to see if the browser supports min-height css property
function minHeightCheck()
{
    var id = "minheightsupport" + (new Date).getTime();
    $("<div></div>").attr("id", id).css({ height: "1px", minHeight: "2px", overflow: "hidden", border: "none", padding: "0", margin: "0" }).appendTo("body");
    var iscorrectheight = document.getElementById(id).offsetHeight == 2;
    $("#" + id).remove();
    return iscorrectheight;
}

$(document).ready(function()
{
    //Magazine titles list
    if ($(".magazine_titles").length > 0)
    { //Check if magazine titles are displayed on this page
        $(".magazine_titles ul").hide(); //Hide sub items
        //Add plus icon and when it is clicked show the sub items and minus icon
        $(".magazine_titles > li").addClass("plus").click(function()
        {
            $(this).toggleClass("minus").find("ul").slideToggle("fast", function()
            {
                equalHeight($(".equal-height")); //adjust height of content area												 
            });
        });

        // expand the default panel
        var categoryid = $.query.get('categoryid');
        if (categoryid != "")
        {
            var pnl = document.getElementById("Category_" + categoryid);
            if (pnl != null)
            {
                $(pnl).toggleClass("minus").find("ul").slideToggle("fast", function()
                {
                    equalHeight($(".equal-height")); //adjust height of content area												 
                });
            }
        }
    }

    //Book titles list
    if ($(".book_titles").length > 0)
    {
        if ($(".book_titles").attr("title").length == 0)
        {
            //Check if book titles are displayed on this page
            $(".book_titles ul").hide(); //Hide sub items
            //Add plus icon and when it is clicked show the sub items and minus icon
            $(".book_titles > li").addClass("plus").click(function()
            {
                $(this).toggleClass("minus").find("ul").slideToggle("fast", function()
                {
                    equalHeight($(".equal-height")); //adjust height of content area												 
                });
            });

            // expand the default panel
            var categoryid = $.query.get('categoryid');
            if (categoryid != "")
            {
                var pnl = document.getElementById("Category_" + categoryid);
                if (pnl != null)
                {
                    $(pnl).toggleClass("minus").find("ul").slideToggle("fast", function()
                    {
                        equalHeight($(".equal-height")); //adjust height of content area												 
                    });
                }
            }
        }
    }

    if ($("#media_kit_builder").length > 0)
    { //Check if media kit builder is displayed on this page
        $("#media_kit_builder .magazine").hide();
        $("#media_kit_builder .category th").addClass("plus").click(function()
        {
            $(this).toggleClass("minus").closest("tbody").find(".magazine").toggle(); //.css("display","table-row")
            equalHeight($(".equal-height")); //adjust height of content area
        });
    }

    //Equalise column heights
    $("#aside").addClass("equal-height");
    $("#title_sub_nav").addClass("equal-height");
    $("#content").addClass("equal-height");
    equalHeight($(".equal-height"));


    //Fly out nav
    $(".popout_sub_nav").css("display", "block");
    $("#title_sub_nav ul").height($("#title_sub_nav").height() - 29); //Set height of nav
    $("#category_sub_nav ul").height($("#title_sub_nav").height() - 60); //Set height of nav
    $(".popout_sub_nav ul").jScrollPane({ dragMaxHeight: 23, scrollbarWidth: 2 }); //Add scroll bar
    $(".popout_sub_nav").hide(); //Hide by default

    $("#search_title").hover(function(e)
    {
        e.stopPropagation();
    });

    //Show nav on hover
    $("#search_title").hover(
	    function()
	    {
	        $(".popout_sub_nav").hide();
	        $("#title_sub_nav").show();
			$(".popout_sub_nav_nz").hide();
	        $("#title_sub_nav_nz").show();
	    },
	    function()
	    {
	        $("#title_sub_nav").hide();
			$("#title_sub_nav_nz").hide();
	    }
	);

    $("#search_category").hover(
	    function()
	    {
	        $(".popout_sub_nav").hide();
	        $("#category_sub_nav").show();
			$(".popout_sub_nav_nz").hide();
	        $("#category_sub_nav_nz").show();
	    },
	    function()
	    {
	        $("#category_sub_nav").hide();
			$("#category_sub_nav_nz").hide();
	    }
	);

    $("#search_website").hover(
	    function()
	    {
	        $(".popout_sub_nav").hide();
	        $("#website_sub_nav").show();
	    },
	    function()
	    {
	        $("#website_sub_nav").hide();
	    }
	);

    /*
    //hide nav when clicking elsewhere on the page
	
	$("#search_title").click(function(e) {
    e.stopPropagation();
    });
	
	$("#search_category").click(function(e) {
    e.stopPropagation();
    });
	
	$("#search_website").click(function(e) {
    e.stopPropagation();
    });
	
	$(document).click(function(){  
    $("#title_sub_nav").hide();
    $("#category_sub_nav").hide();
    $("#website_sub_nav").hide();
    });
    */

    //Keyword search
    /*
    $("#search_keyword").height(30).click(function() {
    $(this).height(60);
    });
    */

    $("#search_keyword input").focus(function()
    {
        if ($(this).attr("value") == "enter keyword")
        {
            $(this).val("");
        }
    });
});

var infoTypes = new Array("contacts", "rates", "specs", "profile", "digital", "deadlines");
var mediaKitBuilder = new Array();

function addCategory(categoryID)
{
    var exist = false;

    for (i = 0; i < mediaKitBuilder.length; i++)
    {
        exist = (mediaKitBuilder[i][0] == categoryID) ? true : exist;
    }

    if (!exist)
    {
        mediaKitBuilder.push(new Array(new String(categoryID)));
    }
}

function addMagazine(categoryID, magazineID)
{
    var found = false;

    for (i = 0; i < mediaKitBuilder.length && !found; i++)
    {
        if (mediaKitBuilder[i][0] == categoryID)
        {
            found = true;

            mediaKitBuilder[i].push(new Array(new String(magazineID)));
        }
    }
}

function check_all(obj, type)
{
    var i;

    if (type == '' || type == 'all')
    {
        // check all the cb's

        for (var i = 0; i < infoTypes.length; i++)
        {
            var cb = document.getElementById("cb_" + infoTypes[i]);

            if (cb != null)
            {
                cb.checked = obj.checked;
            }
        }

        for (var i = 0; i < mediaKitBuilder.length; i++)
        {
            var cb = document.getElementById("cb_" + mediaKitBuilder[i][0]);

            if (cb != null)
            {
                cb.checked = obj.checked;

                check_category(cb, mediaKitBuilder[i][0], type);
            }
        }
    }
    else
    {
        // check all the cb's
        for (var i = 0; i < mediaKitBuilder.length; i++)
        {
            var cb = document.getElementById("cb_" + mediaKitBuilder[i][0] + "_" + type);

            if (cb != null)
            {
                cb.checked = obj.checked;

                check_category(cb, mediaKitBuilder[i][0], type);
            }
        }

    }

}

function check_category(obj, categoryID, type)
{
    var found = false;

    if (type == '' || type == 'all')
    {
        // check all the cb's for this category
        for (var i = 0; i < infoTypes.length; i++)
        {
            var cb = document.getElementById(obj.id + "_" + infoTypes[i]);
            if (cb != null)
            {
                cb.checked = obj.checked;
            }
        }

        // check all the cb's for each mag in this category
        for (var i = 0; i < mediaKitBuilder.length && !found; i++)
        {
            if (mediaKitBuilder[i][0] == categoryID)
            {
                found = true;

                for (var x = 1; x < mediaKitBuilder[i].length; x++)
                {
                    var mag_cb = document.getElementById("cb_" + categoryID + "_" + mediaKitBuilder[i][x]);

                    if (mag_cb != null)
                    {
                        mag_cb.checked = obj.checked;

                        check_mag(mag_cb);
                    }
                }
            }
        }
    }
    else
    {
        // check all the cb's for each mag in this category
        var cb = document.getElementById(obj.id + "_" + type);
        if (cb != null)
        {
            cb.checked = obj.checked;
        }

        // check all the cb's for each mag in this category
        for (var i = 0; i < mediaKitBuilder.length && !found; i++)
        {
            if (mediaKitBuilder[i][0] == categoryID)
            {
                found = true;

                for (var x = 1; x < mediaKitBuilder[i].length; x++)
                {
                    var mag_cb = document.getElementById("cb_" + categoryID + "_" + mediaKitBuilder[i][x] + "_" + type);

                    if (mag_cb != null)
                    {
                        mag_cb.checked = obj.checked;
                    }
                }
            }
        }
    }

    if (!obj.checked)
    {
        check(obj, categoryID, null, type);
    }
}

function check_mag(obj)
{
    // check all the cb's for this magazine
    for (i = 0; i < infoTypes.length; i++)
    {
        var cb = document.getElementById(obj.id + "_" + infoTypes[i]);
        if (cb != null)
        {
            cb.checked = obj.checked;
        }
    }
}

function check(obj, categoryID, magazineID, type)
{
    if (!obj.checked)
    {
        // uncheck the group cb's
        var cb = document.getElementById("cb_all");
        if (cb != null)
        {
            cb.checked = obj.checked;
        }

        cb = document.getElementById("cb_" + type);
        if (cb != null)
        {
            cb.checked = obj.checked;
        }

        // uncheck the category
        cb = document.getElementById("cb_" + categoryID);
        if (cb != null)
        {
            cb.checked = obj.checked;
        }

        cb = document.getElementById("cb_" + categoryID + "_" + type);
        if (cb != null)
        {
            cb.checked = obj.checked;
        }

        if (magazineID != undefined && magazineID != null)
        {
            // uncheck the magazine
            cb = document.getElementById("cb_" + categoryID + "_" + magazineID);
            if (cb != null)
            {
                cb.checked = obj.checked;
            }
        }
        else
        {
            var found = false;
            for (var i = 0; i < mediaKitBuilder.length && !found; i++)
            {
                if (mediaKitBuilder[i][0] == categoryID)
                {
                    found = true;

                    for (var x = 1; x < mediaKitBuilder[i].length; x++)
                    {
                        var mag_cb = document.getElementById("cb_" + categoryID + "_" + mediaKitBuilder[i][x]);

                        if (mag_cb != null)
                        {
                            mag_cb.checked = obj.checked;
                        }
                    }
                }
            }
        }
    }
}

function viewMediaKitPdf(e)
{
    try
    {
        window.open("media-kit-pdf.axd?targetUrl=Content.aspx%3fid=2860%26" + escape(getMediaKitUrl()), "pdf", "");
    }
    catch (e)
    {
        alert(e.description);
    }

    return false;
}

function viewMediaKitHtml(e)
{
    // centre the popup
    openCenteredPopup("media-kit-display.htm?" + getMediaKitUrl(), "mediaKit");

    return false;
}

function getMediaKitUrl()
{
    var categoryID, contentID;
    var infoType, url;
    var cb;
    var qsKeys, magazineIDs;
    var i, c, x, y;
    var binaryID;
    var exists;

    qsKeys = new Array();
    magazineIDs = new Array();

    // build array for the qs
    qsKeys["magazines"] = new BigInt("0");

    for (i = 0; i < infoTypes.length; i++)
    {
        qsKeys[infoTypes[i]] = new BigInt("0");
    }

    for (i = 0; i < mediaKitBuilder.length; i++)
    {
        categoryID = mediaKitBuilder[i][0];

        for (c = 1; c < mediaKitBuilder[i].length; c++)
        {
            contentID = mediaKitBuilder[i][c];

            for (x = 0; x < infoTypes.length; x++)
            {
                infoType = infoTypes[x];

                cb = document.getElementById("cb_" + categoryID + "_" + contentID + "_" + infoType);

                if (typeof (cb) != 'undefined' && cb.checked)
                {
                    binaryID = new BigInt(cb.value);

                    exists = false;

                    for (y = 0; y < magazineIDs.length; y++)
                    {
                        if (magazineIDs[y] == contentID)
                        {
                            exists = true;
                            break;
                        }
                    }

                    if (!exists)
                    {
                        qsKeys["magazines"] = bigint_plus(qsKeys["magazines"], binaryID);

                        magazineIDs.push(contentID);
                    }
                    
                    qsKeys[infoType] = bigint_plus(qsKeys[infoType], binaryID);
                }
            }
        }
    }

    // build the url
    url = "magazines=" + qsKeys["magazines"];

    for (i = 0; i < infoTypes.length; i++)
    {
        url = url + "&" + infoTypes[i] + "=" + qsKeys[infoTypes[i]];
    }

    return url;

}

function viewMagazineMediaKit(link)
{
    openCenteredPopup(link.href, "magazineMediaKit");

    return false;
}

function openCenteredPopup(url, windowName)
{
    var left = 0;
    var top = 0;
    var width = 750;
    var height = 500;

    if (document.all || document.layers)
    {
        left = (screen.availWidth - width) / 2;
        top = (screen.availHeight - height) / 2;
    }
    else
    {
        left = (screen.width - width) / 2;
        top = (screen.height - height) / 2;
    }

    window.open(url, windowName, "height=" + height + ",width=" + width + ",toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,resizeable=yes,left=" + left + ",top=" + top);
}
