String.prototype.capitalize = function(){
   return this.replace( /(^|\s)([a-z])/g , function(m,p1,p2){return p1+p2.toUpperCase();} );
};


function pind1(){
   $('#select_section option').each(function(i,el) {
        var element = el;
        $(element).click(function(){
            $('.level_three #sector li a').each(function(i,el1){
                if($(element).text()==$(el1).text()){
                    $(el1).click();
                }
            });
        });
    });
}

function pind2(){
   $('#select_section option').each(function(i,el) {
        var element = el;
        $(element).click(function(){
            $('.level_three #technology li a').each(function(i,el1){
                if($(element).text()==$(el1).text()){
                    $(el1).click();
                }
            });
        });
    });
}

function str_replace (search, replace, subject, count) {
    // Replaces all occurrences of search in haystack with replace
    //
    // version: 1009.2513
    // discuss at: http://phpjs.org/functions/str_replace    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Gabriel Paderni
    // +   improved by: Philip Peterson
    // +   improved by: Simon Willison (http://simonwillison.net)
    // +    revised by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)    // +   bugfixed by: Anton Ongson
    // +      input by: Onno Marsman
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +    tweaked by: Onno Marsman
    // +      input by: Brett Zamir (http://brett-zamir.me)    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   input by: Oleg Eremeev
    // +   improved by: Brett Zamir (http://brett-zamir.me)
    // +   bugfixed by: Oleg Eremeev
    // %          note 1: The count parameter must be passed as a string in order    // %          note 1:  to find a global variable in which the result will be given
    // *     example 1: str_replace(' ', '.', 'Kevin van Zonneveld');
    // *     returns 1: 'Kevin.van.Zonneveld'
    // *     example 2: str_replace(['{name}', 'l'], ['hello', 'm'], '{name}, lars');
    // *     returns 2: 'hemmo, mars'    var i = 0, j = 0, temp = '', repl = '', sl = 0, fl = 0,
            f = [].concat(search),
            r = [].concat(replace),
            s = subject,
            ra = r instanceof Array, sa = s instanceof Array;s = [].concat(s);
    if (count) {
        this.window[count] = 0;
    }
     for (i=0, sl=s.length; i < sl; i++) {
        if (s[i] === '') {
            continue;
        }
        for (j=0, fl=f.length; j < fl; j++) {temp = s[i]+'';
            repl = ra ? (r[j] !== undefined ? r[j] : '') : r[0];
            s[i] = (temp).split(f[j]).join(repl);
            if (count && s[i] !== temp) {
                this.window[count] += (temp.length-s[i].length)/f[j].length;}}
    }
    return sa ? s : s[0];
}

$(document).ready(function(){

$('div.sidebar a.success-stories:first').click();
var textCat;

$('.level_three #sector li a').each(function(i,el){
    textCat = $(el).text();
    textCat = str_replace('-',' ',textCat);
    $(el).text(textCat.capitalize());

});
pind1();
success_section();

/**************************************
	Triger the search
**************************************/
$('#go').click(
	function() {
		$('form#searchform').submit();
	}
);

/**************************************
	Ajax Indicator Functions
**************************************/
	
	$(document).ajaxStart(function(){
                
		$('div.content_wrap div.content_wrap_inner').block(
			{
				message: $('#ajaxLoader'),
				css:{padding:'5px 5px 5px 20px'}
			}
		);
	});
	
	$(document).ajaxStop(function(){
		$('div.content_wrap div.content_wrap_inner').unblock();
                pind1();
                pind2();
	});	success_section();

/**************************************
	End of Ajax Indicator Functions
**************************************/

/**************************************
	Navigation Functions
**************************************/

	var ul = 'div.level_one ul.nav ';
	var ul2 = 'div.level_two ul.nav ';
	var ul3 = 'div.level_three ul.nav ';
	
	$(ul + 'a').click(function(){
		var anchor = $(this);
		return nav(anchor, ul, 1);	
	});
	
	
	// function to fix jump menu
	$('.pro-services').click(function(){
    $('.tech').hide();
    $('.sol').hide();
	});
	
	$(".sol-sec").click(function() {
	  $('.sol').show();
 	  $('.tech').hide();
	  $('.tech').css('z-index','1');
	});
	
	$(".tech-sec").click(function() {
 	  $('.tech').show();
	  $('.sol').hide();
	  $('.tech').css('z-index','2');
 	  $('.tech').css('margin-top','50px');
	});
	
	$(ul2 + 'a').click(function(){
		var anchor = $(this);
		return nav(anchor, ul2, 2);	
	});
	
	$(ul3 + 'a').click(function(){
		var anchor = $(this);
		return nav(anchor, ul3, 3);	
	});
	
	var anchor = $(ul + 'li.current-cat a');
	nav(anchor, ul, 1);

/**************************************
	End of Navigation Functions
**************************************/

/**************************************
	Print PDF Function
**************************************/

	$('a.pdf').click(function(){
		var content;
		content = '<img src=http://jbs.hellospring.net/wp-content/themes/jbs_new/images/logo-trans.jpg /><br />';
		content+= $('div.content').html();
		$('#pdf_content').val(content);
		$('#pdf_form').submit();
	});

/**************************************
	End of Print PDF Function
**************************************/

});


/**************************************
	Dropdown Menus
**************************************/

	function select_section(menu){
		var page = menu.val();

		$('div.level_three a').each(function(){
			var anchor = $(this);
			var href = anchor.attr('href');

			anchor.parents('li').removeClass('current-cat');
			
			if(href == page){
				$.get(site_url + page + '?ajaxCall=true', function(data){
						data += '<div class="clear"></div>';
						$('div.page_content').html(data);
						anchor.parents('li').addClass('current-cat');
				});				
			}
		});
	}
	
//	function success_section(menu){
//		var page = menu.val();
//		if(page) {
//			$.get(site_url + page + '?ajaxCall=true', function(data){
//				data += '<div class="clear"></div>';
//				$('div.content').html(data);
//			});
//		}
//	}
	
/**************************************
	End of Dropdown Menus
**************************************/	



// Navigation Function

function nav(anchor, ul, level){
	
	//var ul = 'div.level_one ul.nav ';
	var ul2 = 'div.level_two ul.nav ';
	var ul3 = 'div.level_three ul.nav ';	
	var page = anchor.attr('href');
	
	if(page) {
		$.get(site_url + page + '?ajaxCall=true', function(data){
		data += '<div class="clear"></div>';
		$('div.page_content').html(data);
		$(ul + 'li').removeClass('current-cat');
		anchor.parents('li').addClass('current-cat');
		
		if(level == 1) {
			$(ul2 + ', ' + ul3).hide();
			$(ul2 + 'li, ' + ul3 + 'li').removeClass('current-cat');
		}
		
		if(level == 2) {
			$(ul3).hide();
			$(ul3 + 'li').removeClass('current-cat');
		}
		
		if(level == 3) { 
			/* do nothing */ 
		}
		
		if(anchor.attr('lid') != 'undefined') {
			var lid = anchor.attr('lid');
			$('ul#' + lid).show();
			
			var lid2 = $('ul#' + lid + ' a').attr('lid');
			if( lid2 != 'undefined') {
				$('ul#' + lid2).show();
			}
		}
	});
	}

	return false;
}

function success_section(){
    $('div.sidebar a.success-stories').click(function(){
        var anchor = $(this);
        var href = anchor.text();
        $.get(site_url + str_replace(' ','-',href) + '?ajaxCall=true', function(data){
            data += '<div class="clear"></div>';
            $('div.content').html(data);
            anchor.parents('li').addClass('current-cat');
            $('div.sidebar a.headline_inner').each(function(i,el) {
                if($(this).text()!=$(anchor).text()) {
                    $(this).addClass('success-stories');
                    $(this).parent().addClass('success-stories-div');
                }
            });
        });
        $(this).removeClass('success-stories');
        $(this).addClass('headline_inner');
        $(this).parent().removeClass('success-stories-div');
        $(this).parent().addClass('headline');
    });
    $('div.sidebar a.success-stories').attr('href','javascript:void(0)');
}

