/*
 *
 *	This file is divided up into blocks, only functions needed are included on each page.
 * 	Index:
 *	1) homepage_start() used at the home page
 *	2)
 *
 */
var total_width = 0;
var alltheway = "left";
auto_touter = null;

function auto_tout_next(){
	if(alltheway == "right"){
		$(".tout-rotator").animate({
			left: "0"
		}, 370, null, function(){
			check_arrows();
		});	
	}else{
		$(".tout-rotator").animate({
			left: "-=589px"
		}, 370, null, function(){
			check_arrows();
		});
	}
	clearTimeout(auto_touter);
	auto_touter = setTimeout("auto_tout_next()", 5000 );
}

function check_arrows(){
	if($(".tout-rotator").css("left") >= "0px"){
		$(".prev-arrow").fadeOut("fast");
		alltheway = "left";
		//$(".user-card-supplement p").html(alltheway);
	}else{
		$(".prev-arrow").fadeIn("fast");
	}
	cur_left = ($(".tout-rotator").css("left").substring(0,($(".tout-rotator").css("left").length)-2)) * 1;
	if(cur_left <= ((total_width - 589)* -1 ) ){
		$(".next-arrow").fadeOut("fast");
		alltheway = "right";
		//$(".user-card-supplement p").html(alltheway);
	}else{
		$(".next-arrow").fadeIn("fast")
	}
	return(alltheway);
}

function tout(){//initializes the main tout
	var tout_total = 0;//initialize tout counter
	var cur_left = 0;
	$(".tout-rotator li").each(function(){
		tout_total ++;
	});
	//count all the touts
	total_width = tout_total * 589;//get the total width needed.
	$(".tout-rotator").css("width", total_width);//set the parent width accordangly,

	check_arrows();//initial check

	$(".prev-arrow").click(function(){
		//alert(auto_touter);
		clearTimeout(auto_touter);
		$(".tout-rotator").animate({
			left: "+=589px"
		}, 370, null, function(){
			check_arrows();
		});
		auto_touter = setTimeout("auto_tout_next()", 5000 );
		return false;
	});

	$(".next-arrow").click(function(){
		//alert(auto_touter);
		clearTimeout(auto_touter);
		$(".tout-rotator").animate({
			left: "-=589px"
		}, 370, null, function(){
			check_arrows();
		});
		auto_touter = setTimeout("auto_tout_next()", 5000 );
		return false;
	});
}


function homepage_start(){
	var auto_touter = null;
	$(".tab2").hide();
	$(".tab3").hide();


	$(".tab-members").click(function(){
		$(".tab1").show();
		$(".tab2").hide();
		$(".tab3").hide();
	
		$(".book-tabs").removeClass("tab2-active tab3-active");
		$(".book-tabs").addClass("tab1-active");
	
		return false;
	});

	$(".tab-welcome").click(function(){
		$(".tab1").hide();
		$(".tab2").show();
		$(".tab3").hide();
	
		$(".book-tabs").removeClass("tab1-active tab3-active");
		$(".book-tabs").addClass("tab2-active book-tabs");
	
		return false;
	});

	$(".tab-feeds").click(function(){
		$(".tab1").hide();
		$(".tab2").hide();
		$(".tab3").show();
	
		$(".book-tabs").removeClass("tab2-active tab1-active");
		$(".book-tabs").addClass("tab3-active book-tabs");
	
		return false;
	});

	tout();
	auto_touter = setTimeout("auto_tout_next()",3000);
}



/*function globalpage_start(){
$('#global-trends ul').kwicks({  
		max : 320,  
		spacing : 10,  
		isVertical : false,  
		sticky : true 
	});

	$(".global-interface .entry").each(function(){
		$(".entry").animate({height: "0px"}, 0);
	});
	
	$(".gtr-back").click(function(){
		$(".entry").animate({height: "0"}, 300);
		return false;
	});
	
	
	$(".blog-link").click(function(){	

		$(".entry").animate({height: "0"}, 300);
		
		var clickedElement = this;
		var thisId = clickedElement.id;		
		var className = ".blog-post" + thisId.charAt(4);
		
		$(className).css('height', 'auto'); // then temporarily set to auto
		var newWidth = $(className).height();
		$(className).css('height', '0px');
		//alert(newWidth);
		$(className).animate({height: newWidth + "px"},300);
		return false;
	});
	
}*/
/*
function sticky_fields_blur(ini_val,id){
	var current_val = $("#"+id).val();
	if(current_val == ''){
		$("#"+id).val(ini_val);
	}
	return(id);
}

function sticky_fields_focus(ini_val,id){
	var current_val = $("#"+id).val();
	if(current_val == ini_val){
		$("#"+id).val('');
	}
	return(id);
}*/

function sticky_fields_blur(ini_val,element){
	if(element.value == ''){
		element.value = ini_val;
	}
}

function sticky_fields_focus(ini_val,element){
	if(element.value == ini_val){
		element.value = '';
	}
}



function IsNumeric(sText)
{
	var ValidChars = "0123456789";
	var IsNumber=true;
	var Char;
	for (i = 0; i < sText.length && IsNumber == true; i++) 
	   { 
	   Char = sText.charAt(i); 
	   if (ValidChars.indexOf(Char) == -1) 
	      {
	      IsNumber = false;
	      }
	   }
	return IsNumber;
}

function lda_page(){
	$(".date-month").focus(function(){
		if($(".date-month").val() == "mm"){
			$(".date-month").val('');
			$(".date-month").css("color","#323133");
		}
	});
	$(".date-day").focus(function(){
		if($(".date-day").val() == "dd"){
			$(".date-day").val('');
			$(".date-day").css("color","#323133");
		}
	});
	$(".date-year").focus(function(){
		if($(".date-year").val() == "yy"){
			$(".date-year").val('');
			$(".date-year").css("color","#323133");
		}
	});

	
	$(".date-month").keyup(function(event){
		var value = $(".date-month").val();
		if(event.keyCode != 9 && event.keyCode != 13 && event.keyCode != 16){
			if(value.length > 1){
				if(!IsNumeric(value) || value == "00" || value > 13){
					$(".date-month").css("color","red");
				}else{
					$(".date-month").css("color","#323133");
					$(".date-day").focus();
				}
			}
	}
	});
	
	
	$(".date-day").keyup(function(event){
		var value = $(".date-day").val();
		if(event.keyCode != 9 && event.keyCode != 13 && event.keyCode != 16){
			if(value.length > 1){
				if(!IsNumeric(value) || value == "00" || value > 31){
					$(".date-day").css("color","red");
				}else{
					$(".date-day").css("color","#323133");
					$(".date-year").focus();
				}
			}
	}
	});
	
	$(".date-year").keyup(function(event){
		var value = $(".date-year").val();
		if(event.keyCode != 9 && event.keyCode != 13 && event.keyCode != 16){
			if(value.length > 1){
				if(!IsNumeric(value)){
					$(".date-year").css("color","red");
				}else{
					$(".date-year").css("color","#323133");
					$(".country").focus();
					//could fire an age check here
				}
			}
	}
	});
}

function member_search_page(){
	$(".language-drop-down").hide();
	
	$(".search-page-language-dd").click(function(){
		if($(".language-drop-down").hasClass("active")){
			$(".language-drop-down").slideUp("fast");
			$(".language-drop-down").removeClass("active");
			$(".search-page-language-dd").css("background","transparent url(/themes/gg_ambassadors/img/dwn-arrow.png) no-repeat scroll 240px 3px");
		}else{
			$(".language-drop-down").slideDown("fast");
			$(".language-drop-down").addClass("active");
			$(".search-page-language-dd").css("background","transparent url(/themes/gg_ambassadors/img/dwn-arrow.png) no-repeat scroll 240px -48px");
		}
		return false;
	});
	
}

function edit_profile_page(){
    $(".user-edit-tab").hide();
    $(".user-edit-tab1").show();
    $(".user-edit-tab1").addClass("tab-active");
    
    $(".edit-info-tab").click(function(e){
        var clicked = e.target; 
        var tab = $(clicked).attr("rel");
        var from = $(".tab-active");
        var to = $(".user-edit-tab"+tab);
        var ulElement = $(".tabs");
        
        from.removeClass("tab-active");
        $(".user-edit-tab").hide();
        to.show();
        to.addClass("tab-active");
        ulElement.removeClass();
        ulElement.addClass("tabs edit-info-active-tab-"+tab);
        return false;
    });
}

function country_dd_change(value){
	//$(".country-dd").html(value);
}

function gtr_page(){
	$(".col-wrappers").hover(
		function(){
			$(".col-wrappers").stop( true, false );
			if($(this).attr('id') == "gtr-tandt-col"){
				$("#gtr-tandt").html("Tools &amp; Techniques");
			}else{
				$("#gtr-tandt").html("Tools &amp; Tech...");
			}
			$(".col-wrappers").animate({
				width: 159,
				height: 450
			}, 200);
			$(this).stop( true, false );
			$(this).animate({
				width: 274
			}, 200);
		},
		function(){
			$(".col-wrappers").stop( true, false );
			$(".col-wrappers").animate({
				width: 182
			},150, 'linear', function(){$("#gtr-tandt").html("Tools &amp; Techniques");});
		}
	);	
}


function connect_subtabs(){
	$(".sub-tabs").show();
	
	$(".sub-tabs li a").click(function(e){
		var request = $(this).attr("id");
		var active = $(".sub-tabs li a.active");
		
		$(active).removeClass("active");
		$(this).addClass("active");
		activate_section(request);
		return false;
	});	
}

function activate_section(request){
	requestedClass = "." + request.substring(8);
	var deactivate = $("#connect-sections .active");
	$(deactivate).removeClass("active");
	$(requestedClass).addClass("active");
}


function external_links(){
	$("a[href^='http']").attr('target','_blank');
}

// Removes leading whitespaces
function LTrim( value ) {
	
	var re = /\s*((\S+\s*)*)/;
	return value.replace(re, "$1");
	
}

// Removes ending whitespaces
function RTrim( value ) {
	
	var re = /((\s*\S+)*)\s*/;
	return value.replace(re, "$1");
	
}

// Removes leading and ending whitespaces
function Trim( value ) {
	
	return LTrim(RTrim(value));
	
}

