$(document).ready(function(){
    
    nav_access();
    center_logo();
	event_listing();
	contact_other_registry();
	search_value_focus();
	map_replace();
	demonstrator_colorbox();
	
	// On load, set top ticket item to be current, set ticker rolling
	$('ul#news_ticker li:eq(0)').css({top:'10px'}).addClass('cur');
	setInterval(rotate_news_ticker, 5000);
    
    // Tabs and colorbox
    $('div.tabs').tabs();
    $('a#feedback').colorbox();

});
// end document.ready

/**
    * NAV_ACCESS
    * Accessibility nav enhancements
    * ----
*/
function nav_access(){
    $('ul#nav_access li a').focus(function(){ $(this).addClass('focus'); }).blur(function(){ $(this).removeClass('focus'); });
}

/**
    * DEMONSTRATOR_COLORBOX
    * Demonstrator colorbox function
    * ----
*/
function demonstrator_colorbox() {
    $('ul#demonstrator_listing a').colorbox({height: '600px'}, function(){
        var $tabs = $('div#demo_tabs').tabs();
        $(".ui-tabs-panel").each(function(i){
            var totalSize = $(".ui-tabs-panel").size() - 1;
            if (i != totalSize) {
                next = i + 2;
                $(this).append("<a class='next-tab mover' rel='" + next + "'>Next</a>");
            }
            if (i != 0) {
                prev = i;
                $(this).append("<a class='prev-tab mover' rel='" + prev + "'>Previous</a>");
            }
        });
        $('.next-tab, .prev-tab').click(function() {
            $tabs.tabs('select', $(this).attr("rel"));
        });
    });
}

/**
    * ROTATE_NEWS_TICKER
    * News ticker rotation logic
    * ----
*/
function rotate_news_ticker() {

    if ($('ul#news_ticker li').length == 1) {
        // Ticker does not run if only one entry - logic by GREG WOOD!
        return false;
    }
	var headline_count = $('ul#news_ticker li').length - 1;

	var current_headline = $('ul#news_ticker li').index($('ul#news_ticker li.cur'));

	if (current_headline < headline_count) {
		next_headline = current_headline + 1;
	} else {
		next_headline = 0;
	}
	var old_headline = current_headline;

	$("ul#news_ticker li.cur").animate({"top": "56px"}, "slow",function(){
		$(this).removeClass('cur').css('top','56px');
		$('ul#news_ticker li:eq('+next_headline+')').animate({"top": "10px"}, "slow").addClass('cur');
	});
	
}

/**
    * MAP_REPLACE
    * Replace map with Flash version
    * ----
*/
function map_replace() {
    $('#map_replace').flash({ 
        src: '/assets/swf/europe_map.swf',
        width: 460,
        height: 351,
        flashvars: { 
            englandLink: '/service/countries-profile/england-and-wales',
            austriaLink: '/service/countries-profile/austria',
            czechLink: '/service/countries-profile/czech-republic',
            englandLink: '/service/countries-profile/england-and-wales',
            finlandLink: '/service/countries-profile/finland',
            icelandLink: '/service/countries-profile/iceland',
            italyLink: '/service/countries-profile/italy',
            lithuaniaLink: '/service/countries-profile/lithuania',
            netherlandsLink: '/service/countries-profile/netherlands',
            northernIrelandLink: '/service/countries-profile/northern-ireland',
            norwayLink: '/service/countries-profile/norway',
            scotlandLink: '/service/countries-profile/scotland',
            southernIrelandLink: '/service/countries-profile/ireland',
            swedenLink: '/service/countries-profile/sweden',
            sloveniaLink: '/service/countries-profile/slovenia',
            latviaLink: '/service/countries-profile/latvia',
            belgiumLink: '/service/countries-profile/belgium',
            estoniaLink: '/service/countries-profile/estonia',
            spainLink: '/service/countries-profile/spain',
            slovakRepublicLink: '/service/countries-profile/slovak-republic',
            serbiaLink: '/service/countries-profile/serbia'
        }
    }, { version: 8 });
}

/**
    * SEARCH_VALUE_FOCUS
    * Clear search input on focus
    * ----
*/
function search_value_focus() {
	$('input#q').click(function(){
	    $(this).val('');
	});

	$('input#q').blur(function(){
	    if( $(this).val() == ''){
	        $(this).val('Keyword search');
	    }
	});
}

/**
    * EMPLOYER LOGOS
    * Vertically centering the logo within container
    * ----
*/
function center_logo(){
    $('img.logo').each(function(i){
        var margin_height = $(this).parent().height() - $(this).height();
        var final_height = margin_height / 2;
        $(this).css({marginTop: final_height});
    });
}

/**
    * EVENT LISTING
    * Removes containing div if no results listed
    * ----
*/
function event_listing(){
    var event_number = $('div#upcoming_events ul li').size();
    if (event_number == '0') {
        $('div#upcoming_events').hide();
    }
}

/**
	* CONTACT FORM
	* Behaviour to show additional input box if "other" is selected
*/
function contact_other_registry(){
    $('input#other_registry, label#other_registry_label').hide();
    $('select#registry').change(function() {
        if ($(this).val() == "other" ) { 
            $('input#other_registry, label#other_registry_label').show();	        
        } else {
            $('input#other_registry, label#other_registry_label').hide();	 
        }
    });	
}
