// Add/remove Google CSE branding in search
(function () {
        var cseBranding = 'url(http://www.google.com/cse/intl/zh-Hans/images/google_custom_search_watermark.gif) no-repeat scroll left center';
	var $cseBox = $('form#search input[type="text"]');
	
	// Initial state
	$cseBox.css('background', $cseBox.val() == '' ? cseBranding : 'none');
	
	// Focus handler
	$cseBox.focus(function () {
		$cseBox.css('background', 'none');
	});
	
	// Reset on blur if clear
	$cseBox.blur(function () {
		$cseBox.css('background', $cseBox.val() == '' ? cseBranding : 'none');
	});
})();

// Attach handler to video 
$('.video .play').click(function (evt) {
	evt.preventDefault();
	$('#video').modal({
		opacity: 90,
		overlayClose: true,
		overlayCss: {
			backgroundColor: '#000'
		}
	});
});

// Popup pdf links
$('.pdf,.popup').click(function(e) {
    try {
        e.preventDefault();
        window.open(this.href, 'popup', 'width=700,height=500,scrollbars=yes,resizable=yes,menubar=no,toolbar=no,location=no');
    }catch(e) { alert(e.message); }
});

// Handle language bar
function toggleLanguageBar() {
    $('ul.languages').toggleClass('droppedDown');

    $('.languages .dropdownItem').each(function(i) {
        $(this).css('display', ($(this).css('display') == 'none' ? 'block' : 'none'));
    });
}

$('.dropdown').mouseover(function(e) {
    if(!$(this).parent('ul.languages').hasClass('droppedDown')) {
        toggleLanguageBar();
    }
});

$('.dropdown').click(function(e) {
    e.preventDefault();
    toggleLanguageBar();
});
