/*
	Observers
*/

// search and subscribe buttons
$(document).ready(function() {
	
	initNavbar();
	initNavSections();
	initTabBar();
	initOvalLinks();
	initFrontNewsSection();
	
	observeEuropeanFlags();
	
	fixFlashWmodes();
	fixExternalLinks();
	
	validateForms();
});	

function fixExternalLinks()
{
	$("a:not([@href*=brandwatchtech])").not("[href^=#]").not("[href^=/]").not("[@href*=javascript]").attr('target', '_blank');
}

function initNavbar()
{
	$('ul#nav li').hover(
		function() {
			$(this).children('a:first').addClass('hover');
			$(this).children('ul').slideDown(70);
		},
		function() {
			$(this).children('a:first').removeClass('hover');			
			$(this).children('ul').slideUp(70);
		}		
	);
}

function initNavSections()
{
	if (haveSection()) {
		$("#nav_"+section).addClass('active');
	}
	initSubNavSections()
}

function haveSection()
{
	return (section != "{embed:section}");
}

function initSubNavSections()
{
	if (haveSubSection()) {
		$("#sub_"+subsection).addClass('active');
	}
}

function haveSubSection()
{
	return (subsection != "{embed:subsection}");
}


function initTabBar()
{
	$('#tab1').hover(function() {
		if ($(this).hasClass('active') == false) {		
			$(this).addClass('hover');
		};
	}, function() {
			$(this).removeClass('hover');			
	});

	$('#tab2').hover(function() {
		if ($(this).hasClass('active') == false) {				
			$(this).addClass('hover');
		};
	}, function() {
		$(this).removeClass('hover');
	});

	$('#tab3').hover(function() {
		if ($(this).hasClass('active') == false) {				
			$(this).addClass('hover');
		};
	}, function() {
		$(this).removeClass('hover');
	});

	$('#tab1').click(function() {
		$('#tab2').removeClass('active');
		$('#tab3').removeClass('active');
		$('#tab_content_1').show();
		$('#tab_content_2').hide();
		$('#tab_content_3').hide();		
		$(this).removeClass('hover').addClass('active');
		return false;
	});

	$('#tab2').click(function() {
		$('#tab1').removeClass('active');
		$('#tab3').removeClass('active');		
		$('#tab_content_1').hide();
		$('#tab_content_2').show();
		$('#tab_content_3').hide();		
		$(this).removeClass('hover').addClass('active');
		return false;
	});

	$('#tab3').click(function() {
		$('#tab1').removeClass('active');
		$('#tab2').removeClass('active');		
		$('#tab_content_1').hide();
		$('#tab_content_2').hide();
		$('#tab_content_3').show();		
		$(this).removeClass('hover').addClass('active');
		return false;
	});
	
}

function initFrontNewsSection()
{
	// Show articles initially
	$('#front_news table').hide();
	$('#front_news_nav li:first a').click();	
}

function initOvalLinks()
{
	if ( $('#front_news_nav li').length ) {
		$("#front_news_nav li a").buttonup({
			className 				: 'oval_button',
			height 						: 31,
			horizontalPadding : 25,
			showHoverState		: true,
			clickTextColor		: '#fff'
		});	
	}
}

function observeEuropeanFlags()
{

  $("a#european_flags").hover(
    function () {
    	$("#european_partner_popup").show();
    }, 
    function () {
    	$("#european_partner_popup").hide();    
    }
  );
}

function fixFlashWmodes()
{
	$('object').prepend('<param name="wmode" value="transparent" />');
	$('embed').attr({wmode: "transparent"});
}

function validateForms()
{	
	if ( $('#contact').length ) {	
		var validator = $("#contact").validate({
			errorLabelContainer: $("div.errors")		
		});
	}		

	if ( $('#comment_form').length ) {	
		var validator = $("#comment_form").validate({
			errorLabelContainer: $("div.errors")		
		});
	}		

}