// make contains case insensitive
jQuery.expr[':'].contains = function(a,i,m){
    return jQuery(a).text().toUpperCase().indexOf(m[3].toUpperCase())>=0;
};

function showModelList(){
	if(!$('#model_list').is(':visible')){
		$('#down_arrow').css({
			'border-color': 'transparent transparent white transparent',
		    'border-style': 'none solid solid solid',
		    'border-width': '0 11px 12px 10px'
		});
	}
	else {
		$('#down_arrow').css({
			'border-color': 'white transparent transparent transparent',
		    'border-style': 'solid solid none solid',
		    'border-width': '12px 11px 0 10px'
		});
	}
	
	$('#model_list').slideToggle();
}

function setModel(pid){
    if(checkForm()){
        showLoading();
        $.post('/includes/templates/header.inc',
               {setModel:1, modelid:pid},
               function(){
                    if(window.location.toString().search('edituser.php') + window.location.toString().search('adduser.php') > -1){
                        window.location.reload();
                    }
                    else {
                        window.location = '/';
                    }
                });
    }
}

function checkForm(){
	var curVal, origVal, bGood = true, bSelect = false, bCheck = false;
	
	$('form').find(':input').each(function(){
			curVal = $(this).val();
			origVal = $(this)[0].defaultValue;
			
			bCheck = (typeof(curVal) != 'undefined' && typeof(origVal) != 'undefined' && curVal != origVal) || (bSelect && $(this).attr('id') == 'target_demographic1');
			
			if( bCheck ){
				bGood = confirm('There are unsaved changes on this page.  Are you sure you want to navigate away?');
				if(!bGood){
					$('#loading').hide();
				}
				return false;
			}
			bSelect = bSelect || $(this).attr('id') == 'target_demographic1';
		});

	return bGood;
}

function showLoading(){
	$('#loading img').remove();
	$('#loading').show().append('<img src="/images/loading.gif"/>');
	return false;
}

function hideLoading(){
	$('#loading').hide();
	return false;
}

function showNavArrows(){
	if($('#secondarynav').css('margin-left') == '0px'){
		$('#leftarrow a').css('background-position', 'bottom left');
	}
	$('#arrows').show();
	
	return false;
}

function fixFooter(){
    var margin = $('body').height() < $('#non_footer').height() ? 0 : '-61px';
    $('#non_footer').css('margin-bottom', margin);
}

var mouseDown = false;
function moveSecondaryNav(pstrDir){
	var width = 0;
	var containerwidth = $('#secondarynav').width();
	
	$('#secondarynav li').each(function(){
		width += $(this).width();
	});
	
	switch(pstrDir){
		case 'left':
			$('#rightarrow a').css('background-position', 'top left');
			var marginleft = parseInt($('#secondarynav').css('margin-left'));
			if(marginleft < 0){
				$('#secondarynav').css('margin-left', marginleft + 10);
			}
			if(parseInt($('#secondarynav').css('margin-left')) == 0){
				$('#leftarrow a').css('background-position', 'bottom left');
			}

		break;
		case 'right':
			$('#leftarrow a').css('background-position', 'top left');
			var marginleft = parseInt($('#secondarynav').css('margin-left'));

			if(containerwidth - width < 0){
				$('#secondarynav').css('margin-left', marginleft - 10);
			}
			else {
				$('#rightarrow a').css('background-position', 'bottom left');
			}
		break;
	}
	
	return marginleft;
}

function mouseDownNav(b, dir){
	mouseDown = b;

	var marginLeft = moveSecondaryNav(dir);
	if(b){
		setTimeout(function(){mouseDownNav(mouseDown, dir);}, 50);
	}
	else {
		$.post('/skills/index.php', {setMarginLeft: 1, marginLeft: marginLeft});
	}
	
	return false;
}

function emsiObf(name) {
	var one = "ma";
	var two = "ilt";
	var three = "o:";
	var emsidom = " 3c0n 0mi cm0d 3ling # c 0 m ";
	return one+two+three + name + "@" + emsidom.replace(/3/g,'e').replace(/0/g,'o').replace(/\s/g,'').replace(/#/g,'.');
}

function fixEmbeds(){
    $('embed').each(function(){
        $this = $(this);
        $this.parent('div').width($this.width()).css('margin', '0 auto');
    });
}

function showSuccess(strMessage){
    $('body').append("<div class='success'>"+strMessage+".</div>");
    var height = $('div.success').height();
    $('.success').animate({top:0}, 'fast', function(){
        setTimeout(function(){
            $('.success').animate({top:'-'+height+'px'}, 'fast');
        },2000);
    });
}

function in_array( what, where ){
	var a=false;
	for(var i=0;i<where.length;i++){
	  if(what == where[i]){
	    a=true;
        break;
	  }
	}
	return a;
}

var aChosenInds = Array();

// both parameters are optional here, if you don't pass anything in, it will simply clear the aChosenInds array
function chooseIndustry(poRow, piIndustryID){
    if(typeof poRow != 'undefined'){
        var $row = $(poRow), i = 0;
        $row.toggleClass('selected');
    }

    if(typeof piIndustryID != 'undefined'){
        if(in_array(piIndustryID, aChosenInds)){
            aChosenInds.splice(aChosenInds.indexOf(piIndustryID), 1);
        }
        else {
            aChosenInds.push(piIndustryID);
        }
    }
    else {
        aChosenInds = Array();
    }

    var $selected = $('tr.selected'), num_selected = $selected.size();
    switch(num_selected){
        case 0:
                $('#choose_industry_1, #choose_industry_2').animate({right: -600});
            break;
        case 1:
                $('#choose_industry_1').animate({right:0});
                $('#choose_industry_2').animate({right: -600});
            break;
        default:
                $('#choose_industry_1').animate({right: -600});
                $('#choose_industry_2').animate({right:0});
            break;
    }
}

function deselectInds(){
    $('tr.selected').removeClass('selected');
    chooseIndustry();
    return false;
}

function createTempScenario(poInput) {
    var num_jobs = $(poInput).val();

    switch(true){
        case aChosenInds.length == 1:
                $.post('/compare/index.php', {maketemp: 1, numjobs: num_jobs},
                        function(){
                            window.location = '/compare/';
                        });
            break;
    }
    return false;
}

function toggleRows(poLink, pstrText, pstrAltText){
	var $tbodies = $(poLink).parents('tbody').siblings('tbody'),
		$hidden = $tbodies.filter('tbody:hidden');
	
	if($hidden.size()){
		$hidden.fadeIn();
		$(poLink).html('<span class="plus"></span>' + pstrAltText);
	}
	else {
		$('tbody.hidden').fadeOut();
		$(poLink).html('<span class="plus"><span></span></span>' + pstrText);
	}
	
	return false;
}
