/*
	Observers
*/

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

function fixExternalLinks()
{
	$("a:not([href*=brandwatchtech])").not("[href*=blackantmedia]").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');
		
		$('#tab_1_slider').data('nivo:vars').stop = false;		
		if ($('#tab_2_slider').data('nivo:vars') != undefined) {		
			$('#tab_2_slider').data('nivo:vars').stop = true;				
		}
		if ($('#tab_3_slider').data('nivo:vars') != undefined) {
			$('#tab_3_slider').data('nivo:vars').stop = true;		
		}		
		
		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');

		$('#tab_1_slider').data('nivo:vars').stop = true;	
		
		if ($('#tab_2_slider').data('nivo:vars') == undefined) {
			var total = $('#tab_2_slider img').length;
			var rand = Math.floor(Math.random()*total);		
			$('#tab_2_slider').nivoSlider({
				startSlide:rand,
				effect:'fade',
				directionNav:false,
				directionNavHide:true,
				controlNav:false, 				
				captionOpacity: 0.7,
				pauseTime:6000
			});		
		}
		else {
			$('#tab_2_slider').data('nivo:vars').stop = false;		
		}	
		if ($('#tab_3_slider').data('nivo:vars') != undefined) {
			$('#tab_3_slider').data('nivo:vars').stop = true;		
		}

		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');
		
		$('#tab_1_slider').data('nivo:vars').stop = true;		
		if ($('#tab_2_slider').data('nivo:vars') != undefined) {		
			$('#tab_2_slider').data('nivo:vars').stop = true;				
		}
		if ($('#tab_3_slider').data('nivo:vars') == undefined) {
			var total = $('#tab_3_slider img').length;
			var rand = Math.floor(Math.random()*total);
			$('#tab_3_slider').nivoSlider({
				startSlide:rand,
				effect:'fade',
				directionNav:false,
				directionNavHide:true,
				controlNav:false, 				
				captionOpacity: 0.7,
				pauseTime:6000
			});		
		}
		else {
			$('#tab_3_slider').data('nivo:vars').stop = false;		
		}	
		

		return false;
	});
	
}

function initSliders() 
{
	var total = $('#tab_1_slider img').length;
	var rand = Math.floor(Math.random()*total);
	$('#tab_1_slider').nivoSlider({
		startSlide:rand,
		effect:'fade',
		directionNav:false,
		directionNavHide:true,
		controlNav:false, 				
		captionOpacity: 0.7,
		pauseTime:6000
	});
}

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 observeCornerstones()
{
  $('li.corner').live('mouseover mouseout', function(event) {
    if (event.type == 'mouseover') {
      $(this).addClass('hover');
    }
    else {
      $(this).removeClass('hover');
    }
  });    
  
  $('li.tl').click(function() {window.location = "/services/for/evaluation_services";});
  $('li.tr').click(function() {window.location = "/services/for/protection_services";});
  $('li.bl').click(function() {window.location = "/services/for/information_services";});
  $('li.br').click(function() {window.location = "/services/for/enforcement_services";});      
}

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")		
		});
	}		

}

(function($) {
  var cache = [];
  // Arguments are image paths relative to the current page.
  $.preLoadImages = function() {
    var args_len = arguments.length;
    for (var i = args_len; i--;) {
      var cacheImage = document.createElement('img');
      cacheImage.src = arguments[i];
      cache.push(cacheImage);
    }
  }
})(jQuery)