﻿// Calling of custom scripts

// jQuery No Conflict
var $wickedweb = jQuery.noConflict();
$wickedweb(document).ready(function() {

    // Automatic spacing between top footer UL's
    var footerLeftTopWidth = parseInt($wickedweb('#FooterLeftTop').css('width'));
    var footerLeftTopItemsWidth =
        $wickedweb('#FooterLeftTop .first').attr('offsetWidth')
        + $wickedweb('#FooterLeftTop .second').attr('offsetWidth')
        + $wickedweb('#FooterLeftTop .third').attr('offsetWidth');
    // Round down the remaining padding divided by two..
    var remainingPadding = Math.floor((footerLeftTopWidth - footerLeftTopItemsWidth) / 2);
    // .. and apply that as right hand margins to the first two UL blocks
    $wickedweb('#FooterLeftTop .first, #FooterLeftTop .second').css("margin-right", remainingPadding);

// Hide black image overlays if there's nothing in them.
$wickedweb('.CalloutBoxOverlayBlack:empty').hide();

// Script to reorder the main nav into 3 columns
function reorderNav() {
    var $ul = $wickedweb('#mainMenu > li > a:contains("Exhibitions")').next('ul');
    var $li = $ul.children('li').get();
    $li.sort(function (a, b) {
        var compA = $wickedweb(a).text().toUpperCase();
        var compB = $wickedweb(b).text().toUpperCase();
        return (compA < compB) ? -1 : (compA > compB) ? 1 : 0;
    })
    $wickedweb.each($li, function (idx, itm) { $ul.append(itm); });
    $ul.children('li:contains("Design & Décor Exhibitions")').appendTo($ul);
    $ul.children('li:contains("Building & Architecture Exhibitions")').appendTo($ul);
    $wickedweb('#mainMenu > li > ul').each(function () {
        $wickedweb('<ul class="listCol2"></ul><ul class="listCol3"></ul>').insertAfter($wickedweb(this))
        var s = $wickedweb(this).children('li').size();
        var n = (Math.floor(s / 3) * 2) - 1;
        var n1 = (Math.floor(s / 3)) - 1;
        $wickedweb(this).children('li:gt(' + n + ')').appendTo($wickedweb(this).next().next('ul'))
        $wickedweb(this).children('li:gt(' + n1 + ')').appendTo($wickedweb(this).next('ul'))
        $p = $wickedweb(this).parent();
        while ($p.find('ul:eq(2) li').size() > $p.find('ul:eq(1) li').size() || $p.find('ul:eq(1) li').size() > $p.find('ul:eq(0) li').size()) {
            if ($p.find('ul:eq(2) li').size() > $p.find('ul:eq(1) li').size()) {
                $p.find('ul:eq(2) li:first').appendTo($p.find('ul:eq(1)'));
            }
            if ($p.find('ul:eq(1) li').size() > $p.find('ul:eq(0) li').size()) {
                $p.find('ul:eq(1) li:first').appendTo($p.find('ul:eq(0)'));
            }
        }
    });
}

$wickedweb(function () {
    reorderNav();
});


	// Featured exhibitors scroller on the homepage using cycle plugin
	$wickedweb('#FeaturedExhibitorWrap').cycle({
		fx: 'scrollHorz',
		speed: 'Medium',
		timeout: 0,
		prev: '#FeaturedExhibitorBoxLeft',
		next: '#FeaturedExhibitorBoxRight'
	});



    // Calling the select box script (to style drop down lists)
    if ((navigator.userAgent.match(/iPad/i))) {

    } else {
        $wickedweb("select").sb();
    }

	// List of elements to toggle the News letter box - this is to prevent it firing when a user clicks into an input box
	$wickedweb("#MasterPageNewsLetterArrow").click(function() {

	    newsLetterMenuHeight = $wickedweb("#MasterPageNewsLetter").attr('offsetHeight');

	    // Expand the news letter box contents if its closed, and contract if its open
	    if (newsLetterMenuHeight == "123") {
	        $wickedweb("#MasterPageNewsLetter").animate({ "height": "346px" }, "fast");
	        $wickedweb("#MasterPageNewsLetter input,#MasterPageNewsLetter .sb").fadeIn(500);
	        $wickedweb("#MasterPageNewsLetter select").css("visibility", "visible");
	        $wickedweb('#MasterPageNewsLetterArrow').css("background-image", "url('App_Themes/Mosbuild2012/Images/newsletterBackgroundUpArrow.png')");
	    } else {
	        $wickedweb("#MasterPageNewsLetter").animate({ "height": "113" }, "fast");
	        $wickedweb("#MasterPageNewsLetter input,#MasterPageNewsLetter .sb").fadeOut(300);
	        $wickedweb("#MasterPageNewsLetter select").css("visibility", "hidden");
	        $wickedweb('#MasterPageNewsLetterArrow').css("background-image", "url('App_Themes/Mosbuild2012/Images/newsletterBackgroundDownArrow.png')");
	    }

	});

	$wickedweb('.LoggedOutCMSListMenuLink').click(function(event) {
	    event.preventDefault();
	})

    // Script to open News Archive menu
	$wickedweb("#ArchiveMenuTitle").click(function() {
	    $wickedweb("#ArchiveMenu").slideToggle("fast");
	    $wickedweb("#ArchiveMenuTitle .Image").toggleClass("On");
	});


	
});

// jQuery for the rotating adverts
$wickedweb('#MasterPageSponsoredLinks').cycle({ fx: 'fade', timeout: 10000 });
$wickedweb('#MasterPageMediaPartners').cycle({ fx: 'fade', timeout: 10000 });
$wickedweb('#LeftAdvert').cycle({ fx: 'fade', timeout: 10000 });
$wickedweb('#StaticAdvertTop').cycle({ fx: 'fade', timeout: 10000 });

