function setColorActive(elem)
{
	elem.style.color = "#000000";
}
function setColorInactive(elem)
{
	elem.style.color = "#999999";
}

// Remove class name from body to show the now cufonned titles
function showTitles() {
    document.getElementsByTagName("body")[0].className = "";
}

var min = 11;
var max = 24;
function increaseFontSize() {
    var p = document.getElementsByTagName('p');
    for (i = 0; i < p.length; i++) {
        if (p[i].style.fontSize) {
            var s = parseInt(p[i].style.fontSize.replace("px", ""));
        } else {
            var s = 12;
        }
        if (s != max) {
            s += 1;
        }
        p[i].style.fontSize = s + "px"
    }
}

function decreaseFontSize() {
    var p = document.getElementsByTagName('p');
    for (i = 0; i < p.length; i++) {
        if (p[i].style.fontSize) {
            var s = parseInt(p[i].style.fontSize.replace("px", ""));
        } else {
            var s = 12;
        }
        if (s != min) {
            s -= 1;
        }
        p[i].style.fontSize = s + "px"
    }
}

function navOver(imgTag)
{        
	var theImage = imgTag;	
	if(theImage.src.indexOf("_off.") != -1)	
	{
		theImage.src = theImage.src.replace("_off.","_on.");		
	}
	else
	{
		theImage.src = theImage.src.replace("_on.","_off.");	
	}
}

function toggleCvn() {
    $('.rowCvn img').toggle();
}

function toggleDiv(elem) {
    if ($('#' + elem).is(':visible')) {
        $('#' + elem + ' .btnClose').hide();
        $('#' + elem).fadeOut();
    }
    else {
        $('#' + elem).fadeIn(function () {
            $('#' + elem + ' .btnClose').show();
        });
    }
}

function hideDdls() {
    for (i = 0; i < document.getElementsByTagName("select").length; i++)
        document.getElementsByTagName("select")[i].style.visibility = "hidden";
}

function showDdls() {
    for (i = 0; i < document.getElementsByTagName("select").length; i++)
        document.getElementsByTagName("select")[i].style.visibility = "visible";
}

function hideObject() {
    for (i = 0; i < document.getElementsByTagName("object").length; i++)
        document.getElementsByTagName("object")[i].style.visibility = "hidden";
}

function showObject() {
    for (i = 0; i < document.getElementsByTagName("object").length; i++)
        document.getElementsByTagName("object")[i].style.visibility = "visible";
}
function hideiFrame() {
    for (i = 0; i < document.getElementsByTagName("iframe").length; i++)
        document.getElementsByTagName("iframe")[i].style.visibility = "hidden";
}
function showiFrame() {
    for (i = 0; i < document.getElementsByTagName("iframe").length; i++)
        document.getElementsByTagName("iframe")[i].style.visibility = "visible";
}

var timer;
function applyMenuRollovers()
{
	// Events for the drop down menu.
	/* 
		The nav item has mouse over & exit events to display the drop down
		menus.  Hiding the drop downs is initiated by a timer so that it 
		can be cancelled.
	*/
	$('.nav li a').each(function(){
		$(this).mouseenter(function(){
			clearTimeout(timer);
			hideDropMenu();
			$(this).addClass('over');
			displayDropMenu($(this).attr('id'));
		});
		$(this).mouseleave(function(){
			clearTimeout(timer);
			timer = setTimeout("hideDropMenu()",100);
		});
	});
	$('.dropDownMenus div').each(function()
	{
		$(this).mouseleave(function(){
			clearTimeout(timer);
			timer = setTimeout("hideDropMenu()",100);
		});
		$(this).mouseenter(function(){
			clearTimeout(timer);
		});
	});
}
function removeOver()
{
	// Clear the over class from all nav buttons
	$('.nav li a').each(function(){
		$(this).removeClass('over');
	});
}
function hideDropMenu()
{
	// Hide the menus and remove the 'over' class on the nav items
	$('.dropDownMenus').hide();
	removeOver();
}
function displayDropMenu(linkId)
{
	// First hide all menus, then display the desired menu
	menuToShow = getMenuToShow(linkId);
	$('.dropDownMenus').show();
	$('.dropDownMenus div').hide();
	$('.dropDownMenus #'+ menuToShow).show();
}

/* Popup to add new CPD Points on My AAPM Page */
var addCPD = {
    Show: function () {
        $("#addCPD").show();
    },
    Hide: function () {
        $("#addCPD").hide();
    }
}
var editCPD = {
    Show: function (link) {
        $(".editCPD").hide(); // hide all other edit popups
        $(link).siblings().show();
    },
    Hide: function (link) {
        $(link).parent().hide();
    }
}

function getMenuToShow(linkId)
{
	// Determine the drop menu that should be shown based on the id of the button hover
	var menuToShow;
	switch(linkId)
	{
		case "btnMembership":
			menuToShow = "menuMembership";
			break;
		case "btnEvents":
			menuToShow = "menuEvents";
			break;
		case "btnAbout":
			menuToShow = "menuAboutAAPM";
			break;
		case "btnMedia":
			menuToShow = "menuMediaCentre";
			break;
		case "btnResources":
			menuToShow = "menuResources";
			break;
		case "btnEducation":
			menuToShow = "menuEducation";
			break;
		case "btnEmployment":
			menuToShow = "menuEmployment";
			break;
	}
	return menuToShow;
}

var wideAdList = new Array();
var wideAdCount = 0;
var smallAdList1 = new Array();
var smallAd1Count = 0;
var smallAdList2 = new Array();
var smallAd2Count = 0;

function initAdScroll() {
    // For each of the ad slots, if the relevant array is pouplated, shuffle the ads into a random order, then set them to rotate
    if (wideAdList.length != 0) {
        wideAdList = $.shuffle(wideAdList);
        setInterval("rotateWideAd('.adSlot710')", 15000);
    }
    if (smallAdList1.length != 0) {
        smallAdList1 = $.shuffle(smallAdList1);
        setInterval("rotateSmallAd1('.adSlotSide:eq(0)')", 15000);
    }
    if (smallAdList2.length != 0) {
        smallAdList2 = $.shuffle(smallAdList2);
        setInterval("rotateSmallAd2('.adSlotSide:eq(1)')", 15000);
    }
}


(function ($) {
    $.fn.shuffle = function () {
        return this.each(function () {
            var items = $(this).children().clone(true);
            return (items.length) ? $(this).html($.shuffle(items)) : this;
        });
    }
    $.shuffle = function (arr) {
        for (var j, x, i = arr.length; i; j = parseInt(Math.random() * i), x = arr[--i], arr[i] = arr[j], arr[j] = x);
        return arr;
    }
})(jQuery);

function rotateWideAd(selector) {
    rotateAd(selector, wideAdList, wideAdCount)
    wideAdCount++;
}
function rotateSmallAd1(selector) {
    rotateAd(selector, smallAdList1, smallAd1Count)
    smallAd1Count++;
}
function rotateSmallAd2(selector) {
    rotateAd(selector, smallAdList2, smallAd2Count)
    smallAd2Count++;
}

function rotateAd(selector, adList, cycle) {
    var itemToShow = 0;
    if (cycle != 0)
        itemToShow = cycle % adList.length;
    // Change the image, the url & the background of the span tag (span tag is to enable rounded corners)
    $(selector + ' img').attr('src', adList[itemToShow][0]);
    $(selector).attr('href', adList[itemToShow][1]);
    $(selector).parent().css('background', 'url(' + adList[itemToShow][0] + ') no-repeat center center');
}

function showLoginPopup() {
    $('#loginPopup').modal({
        overlayClose: true
    });
    return false;
}

function emailFriend() {
    // Test fields
    var strRegex = "/^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$";
    $('#emailToFriendError').hide();
	$('#emailToFriend').find('.txtHolder').each(function(){
		$(this).removeClass('formError');
	});
    valid = true;
    if ($('#txtFriendEmail').val() == "") {
        valid = false;
        $('#divFriendEmail').addClass("formError");
        $('#txtFriendEmail').focus();
    }
    else if (($('#txtFriendEmail').val() == "") || !(/^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/.test($('#txtFriendEmail').val()))) {
        valid = false;
        $('#divFriendEmail').addClass("formError");
        $('#txtFriendEmail').focus();
    }
    if ($('#txtFriendFirstName').val() == "") {
        $('#divFriendFirstName').addClass("formError");
        valid = false;
        $('#txtFriendFirstName').focus();
    }
    if ($('#txtYourEmail').val() == "") {
        valid = false;
        $('#divYourEmail').addClass("formError");
        $('#txtYourEmail').focus();
    }
    else if (($('#txtYourEmail').val() == "") || !(/^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/.test($('#txtYourEmail').val()))) {
        valid = false;
        $('#divYourEmail').addClass("formError");
        $('#txtYourEmail').focus();
    }
    if ($('#txtYourFirstName').val() == "") {
        $('#divYourFirstName').addClass("formError");
        $('#txtYourFirstName').focus();
        valid = false;
    }
   
    
    if(valid)
    {
        jQuery.post("/base/emailToFriend/sendEmailToFriend.aspx", {
            txtYourFirstName: $('#txtYourFirstName').val(),
            txtYourEmail: $('#txtYourEmail').val(),
            txtFriendFirstName: $('#txtFriendFirstName').val(),
            lblFriendEmail: $('#txtFriendEmail').val(),
            txtMessage: $('#txtMessage').val(),
            pageUrl: document.location.href
        },
        function (data) {
            if (data.indexOf("True") != -1) {
                $('#emailToFriendError').html("Thanks, an email has been sent to your friend.");
                $('#emailToFriendError').show();
            }
            else {
                $('#emailToFriendError').html("Sorry, an error has occurred. Please try again later.");
                $('#emailToFriendError').show();
            }
        });
    }
    else
    {
        // If there was an error, display the error box with the appropriate message
        $('#emailToFriendError').html("Please fill in the required fields highlighted below.");
        $('#emailToFriendError').show();
    }
}

$(document).ready(function () {
    // Rounded ad images
    $(".adSlot img").each(function () {
        if ($(this).parent().get(0).tagName == "A") {
            $(this).parent().wrap(function () {
                return '<span class="roundedAd" style="background:url(' + $(this).children().attr('src') + ') no-repeat center center; " />';
            });
        }
        if ($(this).parent().get(0).tagName != "A") {
            $(this).wrap(function () {
                return '<span class="roundedAd" style="background:url(' + $(this).attr('src') + ') no-repeat center center; " />';
            });
        }

        $(this).css("opacity", "0");
    });

    /* Rollovers for edit buttons on My AAPM Page */
    $('.cpdTable .btnEdit').each(function () {
        $(this).mouseover(function () {
            $(this).next().show();
        });
        $(this).mouseout(function () {
            $(this).next().hide();
        });
    });
    initAdScroll();
    
    if ($.isFunction($.fn.selectBox))
        $("SELECT").selectBox();
    changePay();
});

function changePay() {
    $('#divPayNow').find('input').each(function()
    {
        if($(this).is(':checked'))
            $(this).parent().addClass('greyBoxPayNowOn');
        else
            $(this).parent().removeClass('greyBoxPayNowOn');
    });
    $('#divPay12Month').find('input').each(function () {
        if ($(this).is(':checked'))
            $(this).parent().addClass('greyBoxPay12MonthOn');
        else
            $(this).parent().removeClass('greyBoxPay12MonthOn');
    });
}
