$(document).ready(function(){

  $('.video_reveal').live('click',function(){
    var rel=$(this).attr('rel');
  // have to falisfy toggle to allow 'live'
   if($('#'+rel).is(':visible')){
         $('#'+$(this).attr('rel')).slideUp();
      return false;
    } else {
      $('.video_section').each(function(){$(this).slideUp();});
      try{$('#'+rel).slideDown()} catch(e) {}
      return false;
    }
    });
  
  $('.cartDelable').hover(function(){showDelete($(this));},function(){hideDelete($(this));});
  
  $('.deleteIcon').live('click',function(){
    cart('removeItem',$(this).parent().attr('id'));
  });
  
  $('#checkout').click(function(){
    //alert(checkout);
    return true;
  });
  
  function showDelete(target){
  // show the delete icon
  target.append("<div class='deleteIcon'><img src='sprites/x.png' alt='X' title='Click to Remove'></div>");
  }
  
  function hideDelete(target){
  // remove the delete icon
  target.children(".deleteIcon").remove();
  }
  
// cart ajax function, pass the action and item ID to the cart class and udpate the cart element accordingly
function cart(action,item,type){
  // if the cart is empty, fade it in.
    if($('.cartItem').length==0){$('#cart').fadeIn('400');}
  // send the action and item and eval the response JS
  $.post('classes/cart.class.php',{action:action,item:item,type:type},function(data){
        eval(data);
  });
}

function showOffices(country){
  
  if(country==''){
  country=$.url.setUrl(window.location).param('country');
  }
  $(".mapOverlay:not(#blueDots)").hide();
  $('.'+country).fadeIn(500);
  $('#blueDots').fadeIn(500);
}

function showHQ(hq){
  if(hq==''){
  hq=$.url.setUrl(window.location).param('country');
  }
  $(".mapOverlay:not(#redDots)").hide();
  $('.'+hq+'_office').fadeIn(500);
  $('#redDots').fadeIn(500);

}
$('#mapStage').ready(function(){
});


$('.officeJax').click(function(){
    // get the country argument from the href
    href=$(this).attr('href');
    country=$.url.setUrl(href).param('country');
    training=$.url.setUrl(href).param('training');
    // show the labels for the country
    if (training == 1) {
      showOffices(country);
    } else {
      showHQ(country);
    }
    
    // load the office info into the officeStage div
    $('#officeStage').fadeOut(200,function(){
    $('#officeStage').load('loaders/office.loader.php',"country="+country+"&training="+training,function(){officeToggle()});
    });
    return false;
  });
     

$('.subNav').fadeTo(1,0.01);
// top Menu Behavior
$('.topMenu').hover(function(){


	var fulltarget=$('base').attr('href')+$(this).children('a').attr('href').replace("./","");
	var location=window.location.toString().substring(0,fulltarget.length);
	//if(location==fulltarget){return false;} // uncomment this line to disable subnavs on current pages, prevents subnav blink on click
	// get our target
	var elem=$(this).attr('rel');

	// fade all other subNavs
	$('.subNav').hideSub(elem);

	//window.console.log($('#'+elem).width());
	// get offests
	var Otop=$('#menuBow').position().top;
	var Oleft=$(this).offset().left;
	SubWidth=$("#"+elem).width();
	SubHeight=$("#"+elem).height();
	$("#"+elem+" img").css({width:SubWidth,height:SubHeight});
	NavRight=$('#mainNav').width()+$('#mainNav').offset().left;
	if(Oleft+SubWidth>NavRight+20){ //limit right hand side of menu
		Oleft=(NavRight-SubWidth+20);
	}
		$('#'+elem).css({top:Otop,left:Oleft-10});
//	$('#'+elem).css({top:0,left:0});
	// set position
	// fade it in
	$('#'+elem).fadeTo(200,1);
},function(){
	elem=$(this).attr('rel');
	//$('#'+elem).fadeTo(200,0.01);
});

$('.subNav').mouseleave(function(){
	$('.subNav').hideSub();
});

var navtimeout;

$('#mainNav').mouseleave(function(){
    navtimeout=setTimeout("$('.subNav').hideSub()",200);
});

$('.subNav').mouseenter(function(){
   clearTimeout(navtimeout);
});

$('body').click(function(){
	$('.subNav').hideSub();
});

// trainngs toggle show/hide
function trainingToggle(){
$(".trainingTitle").toggle(function(){
  training=$(this).parent();
  newheight=training.children('.trainingScrip').height();
  if(newheight < 179){newheight=179;}
  //alert(newheight);
  training.animate({height:newheight+71},400);
  // hack to make the learnmore div appear correctly in IE since it won't disappear correctly
  // without hiding it.
  training.find('div.learnMore').show();
},
function(){
  training=$(this).parent();
  training.animate({height:61},200);
  $('.learnMore').hide();
}); 
}

// reloads with a sort=sortfield
$('#sortselect').change(function(){
  
    var vars = [];
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&'); // parse the existing GET var so we don't end up with duplicates
    for(var i = 0; i < hashes.length; i++)
    {
        hash = hashes[i].split('=');
        //vars.push(hash[0]);
        if(hash[0]!=''){
        vars[hash[0]] = hash[1];
        }
    }
    newURL=window.location.host+window.location.pathname+"?";
    for(var v in vars)
    {
        if(v!='sort'){
        newURL+="&"+v+"="+vars[v];
        }

    }
    newURL+="&sort="+$(this).val();
    window.location="http://"+newURL;
});

$('#sortselectmembers').change(function(){
    window.location= 'https://www.valuescentre.com/' + $(this).val();
});

$('#sortselectdocs').change(function(){
    window.location= 'https://www.valuescentre.com/' + $(this).val();
});

trainingToggle();

var oheight=$('.office .contact').height();

function officeToggle(){
  var oheight=$('.office .contact').height();
  $('.office .contact').toggle(function(){
    office=$(this).parent();
	 oheight=$(this).height();
	 var pheight=office.children('.bio').children('.scrip').height();
	 //office.animate({height:pheight+70},400);
	 office.animate({height:pheight+84},400);
  },function(){
	 office=$(this).parent();
	 office.animate({height:oheight},400);
  });
  $('#officeStage').fadeIn(300);
  // The following deals with an issue caused by this toggle method of initially having incorrect 
  // heights for the office divs if they aren't exactly 4 lines.
  $('.office .contact').each( function() {
    eheight = $(this).height();
    office = $(this).parent();
    $(office).height(eheight);
  });
}

officeToggle();
// Main Nav hijacker

  $('#generalNav a:not(#checkoutbtn, .ajax-click-ignore)').click(function(){
    category=$(this).attr('href').split("/",1);
    query=$(this).attr('href').split("?",2);
    $('#contentStage').load('loadContent.php',"type=content&category="+category+"&"+query[1],function(){
      $('#contentStage h2').sifr(); $('.video_reveal').sifr(); $('.sifr').sifr(); trainingToggle();
      fixHashes(); 
    });
    $('#breadcrumb').load('loadContent.php',"type=breadCrumb&category="+category+"&"+query[1]);
    $('#generalNav a').removeClass('active');
    $(this).addClass('active');
    return false;
  });

  $(document).ready(function() {
    $(".accordion").accordion( {active: 'false', collapsible: true});
  });

});

jQuery.fn.hideSub=function(except){
	this.fadeTo(1,0.1);
	this.css({top:'-9999px',left:'-9999px'});
}



