//============================================
// Global functions
//============================================

$.fn.jSleight = function(r){
	$t = $(this);
	$t.css({
		'width'  :  $t.width() + "px",
		'height' :  $t.height() + "px",
		'filter' :  "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + $t.attr("src") + "', sizingMethod='scale')"
	});
	$t.attr("src",r);
	return $t;
}

$.fn.inField = function(){
	$(this).each(function(){
		var $l = $(this);
		var lVal=$l.text();
		var $f=$("input#"+$l.attr("for"))
		$l.hide();
		$f.val(lVal)
		  .bind('focus',function(e){
			  if($(e.target).val()==lVal)
				  $(e.target).val("");
		  })
		  .bind('blur',function(e){
				if($(e.target).val()=="")
					$(e.target).val(lVal);
			 });
	});
	return $(this);
}

$(document).ready(function(){
		
  if ( navigator.appVersion.indexOf("MSIE 6.") != -1 ){ 
    $("img.transparent").jSleight("images/struct/x.gif");
	}

	//============================================
	// Place .inField labels into their respective fields, remove the text on click
	//============================================
	$("label.inField").inField();
		
	//============================================
	// Wrap the insides of buttons
	//============================================
	$("button, .btn").each(function() {
		if ($(this).find("span")[0] == undefined) {
		  $(this).wrapInner("<span></span>");
		}
	});
	
	//============================================
	// Flip dev-notes on and off.
	//============================================
	if($(".dev-note").length > 0){
		$("body").append("<a href='#' class='dev-toggle'>Toggle Notes</a>");
	}
	
	$(".dev-toggle").click(function(){
		$(".dev-note").each(function(){
			if($(this).is(":hidden"))
				$(this).show();
			else
				$(this).hide();
		});
		return false;
	});

	
	//============================================
	// Replace HRs with Divs for styling
	//============================================
	$("hr").each(function() {
		if ( $(this).parent().hasClass("hr") != true ) {
			var hrClass = $(this).attr("class");
			if (hrClass == undefined)
			  hrClass = " ";
			$(this).wrap("<div class='hr " + hrClass +"'></div>");
		}
	});

	//============================================
	// Input focus and blur
	//============================================
	$("input, textarea", $("form")).focus(function(){
		$(this).addClass("focus");
		$f = $(this).parents(".form-field:not(.error)").addClass("cur")
	});
	$("input, textarea", $("form")).blur(function(){
		$(this).removeClass("focus");
		$(this).parents(".form-field").removeClass("cur");
	});
	
  // ======================================	
	// Using data() and indexing to select tabs
  // ======================================	

	$(".tabNav li:eq(0)").each(function(){
		$(this).addClass("cur");
	});
	$(".tabContent-outer").each(function(){
		$(this).children(":not(:first)").hide();	
	});
	$(".tabNav li a").click(function(){
	if (!($(this).parent("li").hasClass("cur")))
		{
			var thisTarget = $(this).attr("href");
			$(this).parent("li").toggleClass("cur").siblings('.cur').toggleClass("cur");
			$(thisTarget).siblings(":visible").fadeOut("fast", function(){
			  $(thisTarget).fadeIn("fast");
			});
		}
	return false; 
	});

  // ======================================	
	// Carousel work
  // ======================================
			
	$("#carouselSlides").cycle({
		fx:           'scrollLeft',
		timeout: 			'5000',
		speed: 				'1000',
		pager: 				'#carouselNav',
		pause:        'true',
		sync:         'true',
		before:  			onBefore, 
		pagerAnchorBuilder:
			function(idx, slide){
				return '#carouselNav li:eq(' + (idx) + ') a';
			}
	});
	
  // Carousel cycling
//  if ($("#carouselNav li").length > 4){
		//$("#carouselInner").append("<a href='#' class='carouselScrl' id='carouselScrl-prev'>&laquo; See more features</a><a href='#' class='carouselScrl' id='carouselScrl-next'>See more features &raquo;</a>");
//	}
	
	$("#carouselNav li a").click(function(){
		window.navActive = true;
		$("#carouselSlides").cycle('pause');
	})		
	
	$("#carouselInner .carouselScrl").click(function(){
		window.navActive = true;
	  if (($("#carouselNav").is(":not(:animated)")) && !($(this).is(".activeSlide"))) {
	  	var thisId = $(this).attr("id").split("-");
		  scrollNav(thisId[1]);
	  }
	  return false;
	});
	
});

function onBefore(curr, next, opts) { 
	var n = opts.nextSlide;
	if ($("#carouselNav li").length > 5) { 
	  if ( (Math.floor(n/5) == n/5 || (n == 0) ) && (window.navActive != true)) {
		  scrollNav("next");
	  }
  }
} 

function scrollNav(direction){
	
	var navItems     = $("#carouselNav li").length;
	var navItemWidth = (parseFloat($("#carouselNav li").css("width")) * 4)
	var slideCount   = Math.ceil(navItems/4) - 1;
	var fromLeft     = $("#carouselNav").css("left");
	
	if(direction == "prev"){
			if 			(fromLeft == "0px")
				var toLeft = "-" + slideCount * navItemWidth + "px";
			else
				var toLeft = "+=" + navItemWidth +"px";
	} else {
			if 			(fromLeft == "-" + slideCount * navItemWidth + "px")
				var toLeft = "0px";
			else
				var toLeft = "-=" + navItemWidth +"px";
	}

  $("#carouselNav").animate({
		left: toLeft
	});
	return false;
	
}

