﻿
$(document).ready(function () {
    //Tracking outbound links
    $('a.outbound').click(function () {
        var $a = $(this);
        var href = $a.attr('href');
        var category = 'outbound'; // set this to whatever you want
        var event = 'click'; // set this to whatever you want
        var label = href; // set this to whatever you want
        _gaq.push(['_trackEvent', category, event, href]);
        setTimeout('document.location = "' + href + '"', 200);
    });
});


var tallestDiv = 0;

function standardiseArticleHeight(selector) {
    selector.each(setTallestDiv);
    selector.each(function (index) { $(this).height(tallestDiv + 5); })
}

function setTallestDiv(index) {
    if ($(this).height() > tallestDiv) {
        tallestDiv = $(this).height();
    }
}
// Do things that will sometimes call sizeChangeCallback()
function sizeChangeCallback() {
    FB.Canvas.setSize();
}

   function checkEmail(email) {
    var pattern = /^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
    return pattern.test(email);
}
    $(function () {
       
        $("#newsletterSignup").submit(function () {
        valid = true;
        emailadd = $("#news-emailAddress").val();
        name = $("#news-name").val();
        //validate email
        if(!checkEmail(emailadd)){
            $("#feedbackMsg").html("Email is incorrect.")
                        .addClass("newsError");
            valid = false;
        }
        //validate name
        if(name == "" || name == " "){
            $("#feedbackMsg").html("Name is required.")
                        .addClass("newsError");
            valid = false;
        }

        if(valid){
            $.ajax({
                url: "/Newsletters/AddSubscriber",
                type: "POST",
                data: $(this).serialize(),
                success: function(data){
                    //set in CSS errorMsg to display:none
                    if(data.indexOf("Success") != -1){
                        $("#feedbackMsg").removeClass();
                        $("#feedbackMsg").html("<h3>Thank you for subscribing.</h3>"+
                                    "<p>Please check your email and click the confirm link. (Check your spam box if it does not appear in your inbox.)</p>")
                                    .addClass("newsSuccess")
                                    .show(1000);
                    }else{
                        $("#feedbackMsg").removeClass();
                        $("#feedbackMsg").html("<h3>Failed to add you.</h3><p>"+data+"</p>")
                                    .addClass("newsError")
                                    .show(1000);
                    }
                },
            });
        }else{
            $("#feedbackMsg").show(1000);
        }
        return false;
    });

        /* newsletter handlers */
        $('#sign-up').click(function (e) {
            $('#newsletter-subscription').modal({
                minHeight: 450,
                overlayClose: true,
                zIndex: 9999999,
                onOpen: function (dialog) {
                    dialog.overlay.fadeIn('slow', function () {
                        dialog.container.slideDown('slow', function () {
                            dialog.data.fadeIn('slow');
                        });
                    });
                },
                onClose: function (dialog) {
                    dialog.data.fadeOut('slow', function () {
                        dialog.container.slideUp('slow', function () {
                            dialog.overlay.fadeOut('slow', function () {
                                $.modal.close(); // must call this!
                            });
                        });
                    });
                }
            });
            return false;
        });
        $(".xLink").click(function () {
            window.open($(this).attr("href"), "_blank");
            return false;
        });
    });

function CustomSearch() {
    var keyword = document.getElementById("keyword").value;
    var url = "/Search?cx=partner-pub-9353229343941924:5a86y74e054&cof=FORID:9&ie=ISO-8859-1&q=" + keyword
    window.location = url;
    return false;
}


function injectCss() {
    var cssLink = document.createElement("link")
    cssLink.href = "/css/styles.css";
    cssLink.rel = "stylesheet";
    cssLink.type = "text/css";
    //frames['twitter-share-button'].document.body.appendChild(cssLink);
    $(".twitter-share-button").document.body.appendChild(cssLink);
}


function displayEvent(event) {
    $(".eventresult").css("display", "none");
    $("."+$(event).attr("id")).css("display", "block");
    
}

var num_entries;
var number_items;

function pageselectCallback(page_index, jq) {

    var items_per_page = number_items;
    var max_elem = Math.min((page_index + 1) * items_per_page, num_entries);
    var new_content = '';
    $('#Searchresult').empty();
    // Iterate through a selection of the content and build an HTML string
    for (var i = page_index * items_per_page; i < max_elem; i++) {
        new_content = jQuery('#articlePaginator div.result:eq(' + i + ')').clone();
        $('#Searchresult').append(new_content);
    }
    return false;
}

function initPagination(num_items) {
    // count entries inside the hidden content
    num_entries = jQuery('#articlePaginator div.result').length;
    number_items = num_items;
    // Create content inside pagination element
    $("#Pagination").pagination(num_entries, {
        callback: pageselectCallback,
        items_per_page: num_items // Show only ten items per page
    });
}


$(function () {
    // set opacity to nill on page load
    // on mouse over
    $("ul#top_menu li a.non").hover(function () {
        // animate opacity to full
        $("#main_nav").css("overflow", "visible");
        $(this).stop().animate({
            backgroundColor: "#D1D1D1"
        }, "slow");
    },
    // on mouse out
		function () {
		    // animate opacity to nill
		    $("#main_nav").css("overflow", "hidden");
		    $(this).stop().animate({
		        backgroundColor: "#fff"
		    }, "slow");
		});
});

var rows2 = null;
var j = 0;
var randomnumber2 = 0;
var switchDelay2 = 7231;
var length2 = 0;

$("#shift tr").ready(function () {
    $("#shift tr").each(function (index2) {
        this.style.display = "none";
    });
});
$(document).ready(initialiseRows2);

function initialiseRows2() {

    if (rows2 == null) {
        rows2 = $("#shift tr");
        length2 = rows2.length;
        randomnumber2 = Math.floor(Math.random() * (length2 / 2))
        j = randomnumber2 * 2;
    }

    if (j > length2 - 1) {
        j = 0;
    }

    rows2.each(function (index2) {
        if (index2 == j) {
            // $(this).fadeIn(2000);
            this.style.display = "block";
        }
        else {
            //$(this).fadeOut("slow");
            this.style.display = "none";
        }
    });
    j = j + 1;
    setTimeout("initialiseRows2()", switchDelay2);
}

