$(document).ready(function() {

    //add flash to homepage		
    var data0 = "&fullRun=true&introPoints=p-1790,-30,10p-1330,58,-122p-1110,283,77p-920,-38,-180p-580,-98,21p-530,78,85&outroPoints=p150,-0.5,0p485,-77,-105p656,228,51p789,96,-127p941,-4,33p1382,193,166p2520,-290,150&cameraPoints=p-10,0,-1230p-10,10,-780&lookAtPoints=p30,0,-20p0,0,0&durationCamera=1&durationIntro=1.5&durationSVG=1&durationOutro=1.5&wireLength=300&retreatIntro=1&retreatSVG=7&retreatOutro=1";
    var data1 = "&fullRun=true&introPoints=p-1420,540,10p-1320,268,-122p-1100,133,77p-920,-38,-180p-620,-258,21p-480,-2,85&outroPoints=p150,-0.5,0p485,-77,-105p746,228,221p939,96,23p1191,-4,23p1312,193,-124p2520,-290,150&cameraPoints=p-10,0,-1230p-10,10,-780&lookAtPoints=p30,0,-20p0,0,0&durationCamera=1&durationIntro=1.5&durationSVG=1&durationOutro=1.5&wireLength=300&retreatIntro=1&retreatSVG=7&retreatOutro=1";
    var data2 = "&fullRun=true&introPoints=p-2170,-400,-420p-680,-12,-122p-690,203,77p-940,-58,-180p-620,-258,21p-540,-2,205&outroPoints=p150,-0.5,0p375,3,-35p656,228,41p859,96,193p731,-4,-77p1152,173,26p1800,480,-510&cameraPoints=p-10,0,-1230p-10,10,-780&lookAtPoints=p30,0,-20p0,0,0&durationCamera=1&durationIntro=1.5&durationSVG=1&durationOutro=1.5&wireLength=300&retreatIntro=1&retreatSVG=7&retreatOutro=1";

    data0 = data0.replace(/=/g, "0=");
    data1 = data1.replace(/=/g, "1=");
    data2 = data2.replace(/=/g, "2=");

    $('.flash').flash(
	 	{
	 	    src: 'flash/preloader.swf',
	 	    width: 960,
	 	    height: 342,
	 	    wmode: 'transparent',
	 	    flashvars: {
	 	        source: "flash/assets/world_icon.svg",
	 	        pictures: "[flash/assets/text001.jpg][flash/assets/text002.jpg][flash/assets/text003.jpg]",
	 	        data: data0 + data1 + data2
	 	    }

	 	},
	 	{ version: 9 }
	);


    //add js print button
    $('<p class="print"><a href="#">Print this page</a></p>').insertAfter('.cf ul.breadcrumbs').click(function() { window.print(); })
    $('<li class="print"><a href="#">Print this page</a></li>').insertAfter('.office-location .arr-lnk .prev').click(function() { window.print(); })


    //search tabs
    $('.tab .results-content').hide();
    var displayTab;
    var displayContent;

    if ($('#tabNumPosition').val() == "") {
        //do first time round
        displayContent = $('.tab:first-child .results-content'); //get first tab content to display
        displayTab = $('.tab:first-child a.lnk-tab').attr('class'); //get first tab link to highlight
        displayTab = displayTab.slice(8); //remove lnk-tab from string
        $('#tabNumPosition').val(displayTab) //set value first time round

    }
    else {
        displayTab = $('#tabNumPosition').val(); //get hidden value and display correct tab
        displayContent = $('.tab a.' + displayTab).parent('.tab').find('div.results-content', this); //get hidden field ad display content
    }

    $('.tab a.' + displayTab).addClass('on');
    $(displayContent).show()

    $('.tab a.lnk-tab').click(function() {
        $('div.results-content').hide();
        $('.tab a').removeClass('on');
        displayTab = $(this).attr('class');

        $('#tabNumPosition').val(displayTab.slice(8))
        $(this).addClass('on').parent('li').find('div.results-content', this).show();

        return false;

    });


    //if country has more than one city, add accordian
    if ($('div.cities').find('li').length > 1) {
        $('div.cities').addClass('contacts-con').find('ul').addClass('contacts');

    }

    //accordian style display for contacts - only ever show one open
    $('dl.contacts dd').hide();
    $('dl.contacts dt a').click(function() {
        var $next = $(this).parent('dt').next();
        var $contacts = $next.siblings('dd:visible');

        if ($contacts.length) {
            $('dl.contacts > dt a').removeClass('open');
            $(this).addClass('open');
            $contacts.slideUp('normal', function() {
                $next.slideToggle('normal');

            });
        } else {
            $next.slideToggle('normal');
            $(this).toggleClass('open');
        }

        return false;
    });


    //accordian style display for contacts - can show multiple
    $('ul.contacts li .vcard').hide();
    $('ul.contacts li').children('a:first-child').click(function() {
        $(this).toggleClass('open');
        $(this).parent('li').find('div.vcard', this).slideToggle();
        return false;
    });


    //sign in - show / hide publication type
    $('.pub-type').hide();
    $('.order-subscribe').empty().html('<h3>You are about the subscribe to:</h3><p>You have not selected any publications yet.</p>');



    //run subscribe function if user refreshes or returns to page
    $('fieldset.signup input[type=checkbox][checked]').each(function() {
        subscribe(this);
    });

    //run subscribe function when checkbox is clicked
    $('.signup input').click(function() {
        subscribe(this);
    })

    function subscribe(el) {
        $div = $(el).attr('id');
        $(el).siblings('div .' + $div).toggle();
        var $type;
        var $ul = '.order-subscribe ul';
        var $head = ''
        var $p = '.order-subscribe p';
        $($ul).remove();
        $($p).remove();


        $('.order-subscribe').append('<ul></ul>');

        $('fieldset.signup input:checkbox').each(function() {

            if ($(this).is(':checked')) {

                if ($(this).parent('div').find('div.pub-type').html() != null) {
                    if ($(this).parent('div').find('div').find("input[@type=radio]:checked").attr('class') == 'hard-copy') {
                        $type = '&#160;&#40;hard copy&#41;';
                    }
                    else {
                        $type = '&#160;&#40;via email&#41;';
                    }

                    $('.order-subscribe ul').append('<li>' + $(this).nextAll('label').eq(0).html() + $type + '</li>');
                }
                else {

                    $('.order-subscribe ul').append('<li>' + $(this).nextAll('label').eq(0).html() + '&#160;&#40;via email&#41;</li>');
                }

            }

        })
    }

    //form validation
    $('.button input').click(function() {
        $('div.form-row').removeClass('error');
        $('fieldset.signup p.error').remove();
        var error = true;
        $('input.required').each(function() {
            if ($(this).val() == "") {
                $(this).parent('div', this).addClass('error');
                error = false;
            }
        })

        $('select.required').each(function() {
            if ($(this + ':selected').val() == "#" || $(this + ':selected').val() == undefined || $(this + ':selected').val() == "") {
                if ($(this).parent('div', this).next().find('input.required-other').val() == "") {
                    $(this).parent('div', this).addClass('error');
                    error = false;

                }
            }
        })

        if (error == false) {
            $('fieldset.signup p').after('<p class="error">! Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer dui est, mattis quis, lobortis ac, pellentesque in, leo. In libero lorem, bibendum sed, dignissim eu, blandit at, justo.</p>');
            return false;
        }
    })

    //Primary navigation for IE6 - actives for all browsers but others work without	
    $('#primary-nav ul li').hover(
	function() {
	    $(this).addClass('nav-hover').find('>a').addClass('hover')
	},
	function() {
	    $(this).removeClass('nav-hover').find('>a').removeClass('hover');
	});


    //table sorter - calling tablesorter plugin
    $('table.table-sort').tablesorter({
        sortList: [[0, 2]]
    });


    $('#search').bind("click keyup", function() {   
        if ($(this).attr('value') == 'Search this site') {
            $(this).attr('value', '');
        }

    })
});  
	
	
	