NewMind.registerNameSpace("NewMind.Site"); //not destructive so can be called multiple times

$(document).ready(function() {
	$('div.mainNavigation > ul > li').addClass('mainNavigationItem');
	NewMind.ETWP.MouseOver.init('.mainNavigationItem');

	$('div#mainColumn a.showHide').click(function(event){
		event.preventDefault();
		$('div.highlightedProducts').slideToggle('slow');
	});
	
	// initialise superfish navigation
	$('#headerMain .mainNavigation ul.list').superfish({
		delay: 500,  // half second delay on mouseout 
		animation: { opacity: 'show', height: 'show' },
		speed: 'fast',
		pathClass: 'current',
		dropShadows: false // disable drop shadows 
	});
	
	// Initialise two column downloads
	NewMind.Site.ReplaceDownloadMarkup.CreateWrappers();
	
	$('.snc .ctl_ItineraryBasket .EmptyMsg img.ex_AddButton').attr('src','/images/scn_button_addtoitinerary.gif');
	
	$('.snc .ep_Add').css('background-image','url(/images/snc/addtoitinerary.gif)');
	
	$('.snc .ep_Adding').css('background-image','url(/images/snc/addtoitinerary.gif)');
	
	$('.snc .ep_Added').css('background-image','url(/images/snc/addtoitinerary.gif)');
	
	$('.snc #rightColumn .ctl_ProductDetail .ex_AddButton a').css('background-image','url(/images/snc/addtoitinerary.gif)');
	
	$('.snc .ctl_ItineraryDetail .order img[src="/images/itinerary/details/up_icon.gif"]').attr('src', function() { return '/images/snc/itinerary/up_icon.gif'; });

	$('.snc .ctl_ItineraryDetail .order img[src="/images/itinerary/details/down_icon.gif"]').attr('src', function() { return '/images/snc/itinerary/down_icon.gif'; });
	
	$('.snc .ctl_ItineraryDetail .order img[src="/images/itinerary/details/up_icon_shaded.gif"]').attr('src', function() { return '/images/snc/itinerary/up_icon_shaded.gif'; });

	$('.snc .ctl_ItineraryDetail .order img[src="/images/itinerary/details/down_icon_shaded.gif"]').attr('src', function() { return '/images/snc/itinerary/down_icon_shaded.gif'; });
	
	$('.snc .ctl_ItineraryDetail .delete img').attr('src','/images/snc/itinerary/remove_icon.gif');
	
	$('.stage2 .snc .columnB .ex_AddButton a').css('background-image','url(/images/snc/addtoitinerary.gif)');
	
	$('.snc .ex_AddButton img').attr('src','/images/snc/addtoitinerary.gif');
	
	$('.snc .InfoBoxContent .ex_AddButton a').css('background-image','url(/images/snc/addtoitinerary.gif');
	
	$('.snc .InfoBoxContent .ex_AddButton img').attr('src','/images/snc/addtoitinerary.gif');
		
		$.get('/inc/twitter.asp', function (data) {
                $('#ctl_VLTwitter').html(data);
                $('#rightColumn .ctl_Twitter').append(data);
        });
	
});

NewMind.Site.ReplaceDownloadMarkup = function() {

    //wraps the collection of dt and dd into their own separate dl's
    //and then converts the wrapping dl into a div
    var CreateWrappers = function() {
        var $dlwrap = $('div.brochureRequestDownloads > div.downloads > dl');

        //add a number on to the dlwrapper, this is mostly for
        //when we have more than one download control on the page
        $dlwrap.each(function(index) {

            var $dl = $(this);
            var movecount = 0;
            var moveflag = false;

            //get the direct children <dt> of the wrapping <dl> and add a <dl> before each one
            //this is separate because we want the index to be sequential. Rather than calling .children()
            $dl.children('dt').each(function(index) {
                var titlecount = index + 1;
                $(this).before('<dl class="dlitem' + titlecount + '"></dl>');
            });

            //children only travels a single level down the DOM tree
            //dont want to use the "dt,dd" JQuery selector because it does not run
            //through the child elements in order of the DOM structure. Jquery does all dt then all dd
            //we want dt,dd,dd,dd,dt,dd,dd,dd and so on
            $dl.children().each(function() {
                var $el = $(this);

                //reset the flag for moving the element
                //this is used because the new inserted <dl>s are also children
                //and we dont want to move those
                moveflag = false;

                var elClass = '' + $el.attr('class');

                //console.log(elClass);

                var elTagName = String($el.attr('tagName')).toLowerCase();

                //console.log(elTagName);

                if (elTagName === 'dt') {
                    if (elClass === '') {
                        $el.addClass('title');
                    }

                    //once we come across a dt, increasing the count by 1
                    //will move the dt and dd's after it into a separate dl
                    movecount++;
                    moveflag = true;

                } else if (elTagName === 'dd') {

                    if (elClass === '') {
                        $el.addClass('desc');
                    }
                    moveflag = true;
                }

                if (moveflag) {
                    //console.log('moving = ' + elTagName + ' to dl.dlitem' + movecount);
                    //console.log($el.text());

                    $dl.children('dl.dlitem' + movecount).append($el);
                }

                //console.log(movecount);

            });

            //once everything is completed remove the wrapping dl(s) and replace with a div
            //this doesnt work
            $dl.before('<div class="dlwrap">' + $dl.html() + '</div>').remove();
        });

    };

    //creates a row that will wrap a specified number of dl items
    //this will only work once you have already altered the markup using
    //CreateWrappers
    var CreateRows = function(itemNum) {

        //get every dl wrapper on the page that is a direct child of a downloads div
        //these should only ever be created by the CreateWrappers function
        var $dlwrap = $('div.ctl_Media > div.downloads > div.dlwrap');

        //as we can have multiple download controls we want to separate
        //out how these get split up into their relevant dl's.
        $dlwrap.each(function() {
            var $dl = $(this);
            var $dlitems = $dl.children('dl');
            var rowCount = Math.round($dlitems.length / itemNum);

            for (var i = 1; i <= rowCount; i++) {
                $dl.append('<div class="row' + i + '"></div>');
            }

            var movecount = 1;

            $dlitems.each(function(index) {
                //console.log(movecount);

                var $dlitem = $(this);
                //console.log($dlitem.text());
                //console.log(index);
                $dl.children('div.row' + movecount).append($dlitem);

                //example: index (10 + 1) mod 5 = 1, remainder is 1
                //we want rows of 5 so checking for 0
                if (((index + 1) % itemNum) === 0) {
                    movecount++;
                }
            });
        });

    };

    return {
        CreateWrappers: function() {
            CreateWrappers();
        },
        CreateRows: function(itemNum) {
            CreateRows(itemNum);
        }
    };
} ();
