﻿// Wellards JQuery Scripts



$(document).ready(function() {

    autoAdjustFloatingColumns();

    /*Expands sub menu*/
    $("#xpander").click(function() {
        $(this).toggleClass('closed');
        $("#xpanding").slideToggle("fast");
    });

    $(".datepicker").datepicker({ dateFormat: 'dd/mm/yy' });

    if ($(".dateSelector").exists()) {

        if ($("#dsToday").exists()) {
            $("#dsToday").click(function() {
                $("input[name='startDate']").datepicker("setDate", "0");
                $("input[name='endDate']").datepicker("setDate", "0");
                $(".dateSelector").submit();
                return false;
            });
        }

        if ($("#dsLast7").exists()) {
            $("#dsLast7").click(function() {
                $("input[name='startDate']").datepicker("setDate", "-7");
                $("input[name='endDate']").datepicker("setDate", "0");
                $(".dateSelector").submit();
                return false;
            });
        }

        if ($("#dsLast30").exists()) {
            $("#dsLast30").click(function() {
                $("input[name='startDate']").datepicker("setDate", "-30");
                $("input[name='endDate']").datepicker("setDate", "0");
                $(".dateSelector").submit();
                return false;
            });
        }

        if ($("#dsYear").exists()) {
            $("#dsYear").change(function() {
                var y = $(this).val();
                $("input[name='startDate']").datepicker("setDate", new Date(y, 0, 1));
                $("input[name='endDate']").datepicker("setDate", new Date(y, 11, 31));
                submitFormAsExcel($(".dateSelector"));
                return false;
            });
        }

    }


    /*Main Menu Dropdown (overcomes psuedo class non support in IE6)*/
    $("#mainMenu ul li").mouseover(function() {

        //    Need to get window width and li position to work out which way the dropdown should go
        var windowwidth = $(window).width() - 200;
        var offsetvalue = $(this).offset();

        if (offsetvalue.left > windowwidth) {
            //        If dropdown wont fit, set position to right of li
            $(this).find("ul").css({ left: "auto", right: "0px", display: "block" });
        } else {
            $(this).find("ul").css("display", "block");
        };

    });
    $("#mainMenu ul li").mouseout(function() {
        $(this).find("ul").css("display", "none");
    });


    //						$("ul.expandable li").mouseover(function(){$(this).queue("fx",[]).stop().animate({marginLeft:"0px"} ,300);});
    //						$("ul.expandable li").mouseout(function(){$(this).queue("fx",[]).stop().animate({marginLeft:"-160px"} ,300);});


    // fcb=function(d){ktndata=d;var p=document.getElementsByTagName('img');for(var i in p){p[i].width=p[i].width;p[i].height=p[i].height;p[i].src=d.items[Math.floor(Math.random()*(d.items.length))].media.m;}};if(typeof ktndata=='undefined'){var jp=document.createElement('script');jp.setAttribute('type','text/javascript'); jp.setAttribute('src','http://api.flickr.com/services/feeds/photos_public.gne?tags=gurn&tagmode=any&format=json&jsoncallback=fcb'); document.getElementsByTagName('head')[0].appendChild(jp);}else{fcb(ktndata);}

    $("ul.expandable li").mouseover(function() { $(this).animate({ marginLeft: "0px" }, Math.floor(Math.random() * 300)).next().mouseover().mouseout(); });
    $("ul.expandable li").mouseout(function() { $(this).animate({ marginLeft: "-160px" }, Math.floor(Math.random() * 300)); });


    // Adjust the height of the sidebar TD
    if ($("ul.expandable").length) {
        var resizeElem;
        if (document.getElementById("sideBar")) {
            resizeElem = document.getElementById("sideBar");
        }
        else if (document.getElementById("fMenu")) {
            resizeElem = document.getElementById("fMenu");
        }

        if (resizeElem != null) {
            var sm = document.getElementById("slidingMenu")
            resizeElem.style.height = Math.max(resizeElem.offsetHeight, (sm.offsetHeight + (getTop(sm) - getTop(resizeElem)))).toString() + "px";
        }
    }


    // Adjust the width of the content
    // Use this instead of jquery's width
    if (document.getElementById("mainLayout")) {
        if (document.getElementById("sideBar")) {
            var mt = document.getElementById("mainTable")
            var ml = document.getElementById("mainLayout")
            var sb = document.getElementById("sideBar")
            mt.style.width = Math.max(mt.offsetWidth, (ml.offsetWidth + sb.offsetWidth)).toString() + "px";

        }
    }

    // Adjust external links for IE
    //    if (jQuery.browser.msie) {
    //        $("a.externallink").removeClass("externallink").prepend("<span class='externallink'>&nbsp;</span>");
    //    }

}
				);


// Simple .exists() function
jQuery.fn.exists = function() { return jQuery(this).length > 0; }

function jToggle(selector) {
    $(selector).toggle();
}

function submitFormAsExcel(jQueryForm) {
    var originalAction = jQueryForm.attr("action");
    jQueryForm.attr("action", "excel.ashx" + originalAction);
    jQueryForm.submit();
    jQueryForm.attr("action", originalAction);
}

function autoAdjustFloatingColumns() {
    /*WRONG - THIS CALCULATES ALL COLUMNS, WHETHER IN SAME CONTEXT OR NOT */
    /*matchHeightCol($(".cols2 .listItem"), '2');*/
    /*matchHeightCol($(".cols3 .listItem"), '3');*/

    /* Use .each() to keep context */
    $(".cols2").each(function () {
        matchHeightCol($(this).find(".listItem"), '2');
    });
    $(".cols3").each(function () {
        matchHeightCol($(this).find(".listItem"), '3');
    });
    $(".cols4").each(function () {
        matchHeightCol($(this).find(".listItem"), '4');
    });
    $(".cols5").each(function () {
        matchHeightCol($(this).find(".listItem"), '5');
    });
}

function matchHeightCol(oGroup, nCols) {
    var nRowCount = 0;
    var nColCount = 1;
    var nElemTotal = oGroup.length;
    var nElemCount = 0;
    oGroup.each(function () {
        nElemCount++;
        $(this).addClass('row' + nRowCount);
        if (nColCount == nCols) {
            matchHeight($(this).parent("div[class^='cols']").find('.row' + nRowCount));
            nRowCount++;
            nColCount = 1;
        } else {
            $(this).addClass('rowMargin');
            nColCount++;
            if (nElemTotal == nElemCount) {
                matchHeight($(this).parent("div[class^='cols']").find('.row' + nRowCount));
            }
        }
    });
}



function matchHeight(oGroup) {
    var tallest = 0;
    oGroup.each(function () {
        thisHeight = $(this).height();
        if (thisHeight > tallest) {
            tallest = thisHeight;
        }
    });
    oGroup.each(function () {
        if (tallest > 0) {
            $(this).css({ 'height': tallest });
        } else {
            $(this).css({ 'height': 'auto' });
        }

    });
}
