/**
 * global.js
 */
$(document).ready(function() {    
    /**
     * Analytics Event Tracking
     */
    $('#sendCvBtn').click(function(){
        _gaq.push(['_trackEvent', 'SendCV', 'submit', 'Send CV Sidebar Submitted']);
    });
    $('#quickSearchBtn').click(function(){
        _gaq.push(['_trackEvent', 'QuickSearch', 'submit', 'Quick search Sidebar Submitted']);
    });
    $('#registerJobBtn').click(function(){
        _gaq.push(['_trackEvent', 'RegisterJob', 'submit', 'Register Job Sidebar Submitted']);
    });
    $('#jobsByEmailBtn').click(function(){
        _gaq.push(['_trackEvent', 'JobsByEmail', 'submit', 'Jobs By Email Sidebar Submitted']);
    });

	$("a[rel='external']").addClass("external").attr('title', function() { 
        return this.title + ' (Opens in New Window)'; 
    }).click(function() { 
        window.open(this.href);return false; 
    });
	$('a.btnReplace').css('visibility', 'visible').click(function() { 
        $(this).parents('form').submit();return false; 
    });
	$('input.btnReplace').hide();
	
	$('div#hdr ul select').hide();
	$('div#language-dropdown').show();
	
	$('div#sidebar .contentBox.closed .contentBody').hide();
	
	$('.labelOver').labelOver();
	/**
     * Force open in new window
     */
    $('#social-bar .f-left a, .new-window').attr('target', '_blank');
    
	/**
     * Language Dropdown
     */
    setSelectedLang($("#language-dropdown dd ul li a.selected"));
    $("#language-dropdown img.flag").addClass("flagvisibility");
    $("#language-dropdown dt a").click(function() {
        $(this).toggleClass('active');
        $("#language-dropdown dd ul").toggle();
    });

    $("#language-dropdown dd ul li a").click(function() {
        setSelectedLang($(this));
        $("#language-dropdown dd ul").hide();
        $("#language-dropdown dt a").removeClass('active');
        window.location = '/' + $(this).find('span.value').html();
    });

    $(document).bind('click', function(e) {
        var $clicked = $(e.target);
        if (! $clicked.parents().hasClass("dropdown")) {
            $("#language-dropdown dd ul").hide();
            $("#language-dropdown dt a").removeClass('active');
        }
    });
    /**
     * set the current lang
     */
    function setSelectedLang(elt)
    {
        $("#language-dropdown dt a span").html(elt.html());
    }
    
    ////////////////////////////////////////////////////////////
	
	if (jQuery.browser.msie && jQuery.browser.version < 8)
	{
		$('div.contentBox.closed div.dropReplace').css({"position":"static"});
	}
	// Sidebar animations
	$('div#sidebar .contentTitle a').click(function() {
		if ($(this).hasClass('link')) {
			return true;
		}
		var $parent = $(this).parents('div.contentBox');
		
		if ($parent.hasClass('closed')) {
			$parent.toggleClass('closed');
			//slide down.
			$('div.contentBody', $parent).slideToggle('slow');
			if (jQuery.browser.msie && jQuery.browser.version < 8)
			{
				$('div.dropReplace', $parent).css({"position":"relative"});
			}
		} else {
			if (jQuery.browser.msie && jQuery.browser.version < 8)
			{
				$('div.dropReplace', $parent).css({"position":"static"});
			}
			//slide up.
			$('div.contentBody', $parent).slideToggle('slow', function() {$parent.toggleClass('closed');});

		}
		
		$(this).blur();
		
		return false;
	});
	
	// Folded hidden content
	$('div.hiddenTitle a').click(function() {
		var $content = eval('(' + $('#__FOLDING__').val() + ')');
		if (!$(this).hasClass('open')) {
			$('div.hiddenTitle a.open').removeClass('open').html($content.close.text).attr('title', $content.close.title);
			/*$('div.hiddenContent.open').slideUp('slow').animate({ paddingTop : '0px' }, 'slow', function() { $(this).removeClass('open'); });*/
			$('div.hiddenContent.open').slideUp('slow', function () {$(this).removeClass('open');} );
			/*$('div#content-' + $(this).attr('rel')).slideDown('slow').animate({ paddingTop : '20px' }, 'slow', function() { $(this).addClass('open'); });*/
			$('div#content-' + $(this).attr('rel')).slideDown('slow' , function () {$(this).addClass('open');} );
			$(this).addClass('open').html($content.open.text).attr('title', $content.open.title).blur();
		} else {
			$('div.hiddenTitle a.open').removeClass('open').html($content.close.text).attr('title', $content.close.title);
			/*$('div.hiddenContent.open').slideUp('slow').animate({ paddingTop : '0px' }, 'slow', function() { $(this).removeClass('open'); });*/
			$('div.hiddenContent.open').slideUp('slow', function () {$(this).removeClass('open');} );
			$(this).blur().attr('title', $content.close.title);
		}
		return false;
	});
	
	$('.folder div.folding a.switch').click(function() {
	
		var $content = eval('(' + $('#__FOLDING__').val() + ')');

		var $this = $(this);
		if ($this.hasClass('open'))
		{
			$this.removeClass('open').html($content.close.text).attr('title', $content.close.title);
			$this.next('div.hiddenContent').slideUp('slow', function () {$(this).removeClass('open');} );
		}
		else
		{
			$('.folder div.folding a.switch').removeClass('open').html($content.close.text).attr('title', $content.close.title);
			$('.folder div.folding div.hiddenContent.open').slideUp('slow', function () {$(this).removeClass('open');} );
			$this.addClass('open').html($content.open.text).attr('title', $content.open.title);
			$this.next('div.hiddenContent').slideDown('slow', function () {$(this).addClass('open');} );
		}
		return false;
	});
	
	// Folded hidden content
	$('p.hiddenLink a').click(function() {
		var $content = eval('(' + $('#__FOLDING__').val() + ')');
		if (!$(this).hasClass('open')) {
			$('p.hiddenLink a.open').removeClass('open').html($content.close.text).attr('title', $content.close.title);
			$('div.hiddenContent.open').slideUp('slow').removeClass('open');
			$('div#content-' + $(this).attr('rel')).slideDown('slow').addClass('open');
			$(this).addClass('open').html($content.open.text).attr('title', $content.open.title).blur();
		} else {
			$('p.hiddenLink a.open').removeClass('open').html($content.close.text).attr('title', $content.close.title);
			$('div.hiddenContent.open').slideUp('slow').removeClass('open');
			$(this).attr('title', $content.close.title).blur();
		}
		return false;
	});
	
	// Multichoice dropdowns
	
	// #### START JOB SEARCH DROPDOWNS #### //
	var $technologies = new Array();
	//$('#selectSkill').hide();
	$('select.hasDrop').hide();
	$('.dropReplace a, .multidrop a').click(function() {$('#drop-multi-' + $(this).attr('rel')).slideToggle('normal');$(this).blur();return false;});
	
	$('#consultCountry').change( function() {
		if($(this).attr('value') == '29') {
			$('#cons_region').show();
			$('#region_head').show();
		}else{
			$('#cons_region').hide();
			$('#region_head').hide();
		}
	});
	
	$('#selectCountry').change( function() {
		if($(this).attr('value') == '29') {
			$('#cons_region').show();
			$('#region_head').show();
		}else{
			$('#cons_region').hide();
			$('#region_head').hide();
		}
		
	});
	
	$('*[id*=drop-multi] input').click(function() {
		
		var $this = $(this);
		var $container = $this.parents('div.multidrop > ul li');
		if ($this.parents('ul').length > 1 ) // is a child node
		{
			var isPar = false;
			var $par = $container.find('> label input');
			var $children = $container.find('li label input');
		}
		else
		{
			var isPar = true;
			var $par = $this;
			var $children = $container.find('ul li input');;
		}
		if ( isPar )
		{
			if ($this.attr('checked')) {
				$children.each(function()
				{
					$(this).attr('checked','checked');
				});	
			}
			else {
				$children.each(function() {
					var $child = $(this);
					$child.removeAttr('checked')
				});
			}
		}
		else
		{
			var allchecked = true;
			$children.each(function(){
				if(!$(this).attr('checked')) {
					allchecked = false;
				}
			});
			if(allchecked) {
				
				$par.attr('checked','checked');
			}
			else {
				$par.removeAttr('checked');
			}
		}

		var $sector = $this.parents('div.multidrop');
		var sectorName = $sector.attr('id').replace('drop-multi-','');
		replaceText(sectorName);
		
		if(sectorName == 'country' && $this.attr('value') == 29) {
			if($this.attr('checked')) {
				$('#region_row').show();
			}else{
				$('#region_row').hide();	
			}
		}
				
		if(sectorName == 'emailcountry' && $this.attr('value') == 29) {
			if($this.attr('checked')) {
				$('#jbe_region').show();
			}else{
				$('#jbe_region').hide();	
			}
		}		
	});
	
	function replaceText(sectorName)
	{
		var $sector = $('#drop-multi-'+sectorName);

		var labels = new Array();
		$sector.find('input:checked').each(function()
		{
			var $ths = $(this);
			var $pr = $ths.parents('div.multidrop > ul li').children('label').children('input');
			if ( $ths.parents('div.multidrop ul ul').length == 0 ) // parent node
			{
				labels.push($ths.parent().text());
			}
			else if (!$pr.attr('checked'))
			{
				labels.push($ths.parent().text());
			}
			
		});

		if (labels.length == 0)
		{
			var label = $('select[name='+sectorName+'[]] option:first').text();
		}
		else
		{
			var label = labels.join(', ');
		}
		
		$('input[name='+sectorName+'_text]').val(label);	
	}
	// #### START SIDEBAR DROPDOWNS #### //
	
	replaceText('country');
	replaceText('region');
	replaceText('technology');
	replaceText('position');
	replaceText('emailcountry');
	replaceText('emailregion');
	replaceText('emailtechnology');
	replaceText('emailposition');
	replaceText('countries_eligible');	
	replaceText('countries_ineligible');
	replaceText('regions');
	
	hideReplacedFields();
});

function hideReplacedFields()
{
	$('div.multidrop').each(function() {
		var id = $(this).attr('id').replace('drop-multi-','');
		$('select[name='+id+'[]] option:selected').removeAttr('selected');
	});
}

/**
 * Test if cookies are available
 *
 * @return bool
 */
function cookiesEnabled()
{
    var cookieEnabled = (navigator.cookieEnabled) ? true : false;
 
    if (typeof navigator.cookieEnabled == "undefined" && !cookieEnabled)
    {
        document.cookie="testcookie";
        cookieEnabled =
            (document.cookie.indexOf("testcookie") != -1) ? true : false;
    }
    return (cookieEnabled);
}
