/*
	Fuse Pilates scripts (utilizing jQuery 1.5.1)
*/

/* ---------- Initialize page ---------- */

jQuery(document).ready(function($) {
	// Stripe table row colors
	$("table.data tr:nth-child(even)").not(".even").not(".odd").addClass("even");

	// Insert document icons
	$("a[href$=pdf]:not(:has(img))").append('<img class="docicon" src="/wp-content/themes/fuse/images/icon_pdf.gif" width="14" height="14" alt=" (PDF)">');
	$("a[href$=doc]:not(:has(img)), a[href$=docx]:not(:has(img))").append('<img class="docicon" src="/wp-content/themes/fuse/images/icon_word.gif" width="14" height="14" alt=" (Word Document)">');
	$("a[href$=xls]:not(:has(img)), a[href$=xlsx]:not(:has(img)), a[href$=csv]:not(:has(img))").append('<img class="docicon" src="/wp-content/themes/fuse/images/icon_excel.gif" width="14" height="14" alt=" (Excel Spreadsheet)">');
	$("a[href$=ppt]:not(:has(img)), a[href$=pptx]:not(:has(img))").append('<img class="docicon" src="/wp-content/themes/fuse/images/icon_powerpoint.gif" width="14" height="14" alt=" (Powerpoint Presentation)">');

	// Column child CSS3 fix
	$(".columns>.col:first-child").addClass("first-child");
	$(".columns.three>.col:nth-child(2)").addClass("middle-child");
	$(".columns>.col:last-child:not(:first-child):not('.middle-child')").addClass("last-child");

	// IE CSS fixes
	if ($.browser.msie) {
		$("#navigation li:last-child").addClass("last-child");

		if (parseInt($.browser.version) < 7) {
			$("#navigation td").hover(function(){
				$(this).addClass("hover");
			}, function(){
				$(this).removeClass("hover");
			});
		}
		
		
	}



	// Homepage carousel
	//initCarousel();


	// Collapsible blocks
	$(".collapsible").each(function(){

		var $block = $(this);

		// Get related links
		var $link = $("a[href='#" + this.id + "']");

		// Initialize blocks
		if ($block.hasClass("collapsed")) {
			$link.addClass("icon-collapsed");
			$block.hide();
		} else {
			$link.addClass("icon-expanded");
		}

		// Toggle class
		$link.click(function(){
			$block.slideToggle("fast");
			$block.toggleClass("collapsed");
			$link.toggleClass("icon-collapsed icon-expanded");
			return false;
		});
	});


	// IE6 PNG fix
	iepngfix();
	
	//Accordion Action	
	jQuery.noConflict();
	$('#accordion h3').click(function() {
	    $(this).toggleClass("active");
		$(this).next().slideToggle('slow');
		return false;
	}).next().hide();


});





// Homepage carousel
/*
function initCarousel() {

	var $allcarousels = $(".carousel");
	if ($allcarousels.length == 0) { return; }


	// Wrap each slide's contents with extra <div>
	$(".slide", $allcarousels).each(function(){
		var $slide = $(this);

		$slide.wrapInner("<div class='contents'></div>");
		$("img:first", $slide).prependTo($slide);
	});


	$allcarousels.each(function(){
		var $carousel = $(this);

		// Create slide controls
		var $controls = $("<div class='controls'><h3 class='hide'>Jump to slide</h3></div>");
		var slidecount = $(".slide", $carousel).length;

		for (var i = 0; i < slidecount; i++) {
			// Individual links
			$controls.append("<a href='#'>" + (i+1) + "</a>");
		}

		// Simulate continuous scroll -- clone first item to end of list
		$(".slide:first", $carousel)
			.clone().insertAfter($(".slide:last", $carousel));

		// Set first link to be "active"
		$("a:first", $controls).addClass("active");

		// Inject controls and wrapper into page
		$carousel.wrapInner("<div class='slides'></div>").prepend($controls);

		// Auto-animate after set time
		$carousel.data("timeout", setTimeout(function(){ runCarousel($carousel) }, 5000));
	})


	.hover(function(){
		var $carousel = $(this);

		// Pause on hover: cancel animation if cursor on block
		clearTimeout($carousel.data("timeout"));
		$carousel.data("timeout", null);

	}, function(){
		var $carousel = $(this);

		if ($carousel.data("timeout")) {
			// Double-check last timer is stopped
			clearTimeout($carousel.data("timeout"));
		}

		// Restart animation when cursor leaves (shorten interval)
		$carousel.data("timeout", setTimeout(function(){ runCarousel($carousel) }, 2000));
	});


	// Jump dot links
	$(".controls > a", $allcarousels).click(function(){
		if ($(this).hasClass("active")) { return false; }

		var $carousel = $(this).closest(".carousel");

		// Swap active link
		$(this)
			.addClass("active")
			.siblings(".active").removeClass("active");

		// Slide animation
		var width = $(".slide:first", $carousel).width();
		var slidenum = $(this).prevAll("a").length;


		// Show continuous loop if coming from end
		if ($carousel.data("continuous")) {
			slidenum = $(".slide", $carousel).length-1;

			// Animate to cloned last slide
			$(".slides", $carousel).stop().animate({ left:-width*slidenum }, "normal", function(){

				// Jump to first slide invisibily
				$(".slides", $carousel).css("left", 0);

			});
			$carousel.data("continuous", null);

		} else {
			// Animate to new slide
			$(".slides", $carousel).stop().animate({ left:-width*slidenum }, "normal");
		}

		return false;
	});


}


// Carousel timer function
function runCarousel($carousel) {

	// Get current slide from active control dot
	var current = $(".controls a.active", $carousel).prevAll("a").length;

	// Find next link
	var next = current+1;

	// If at end, simulate continuous loop
	if (next == $(".controls a", $carousel).length) {
		$carousel.data("continuous", true);
		next = 0;
	}

	// Click next link
	$(".controls a:eq(" + next + ")", $carousel).click();

	// Restart timeout
	$carousel.data("timeout", setTimeout(function(){ runCarousel($carousel) }, 7500));
}
*/



// IE6 PNG fix
function iepngfix() {

	if (!$.browser.msie || parseInt($.browser.version) >= 7) { return; }

	// IE 5.5 and 6 PNG support (derived from youngpup.net)
	$("img[src$=png]").each(function(){
		var src = this.src;
		var div = document.createElement("div");

		// Set replacement div properties
		if (this.id) { div.id = this.id; }
		if (this.className) { div.className = this.className; }
		if (this.title) { div.title = this.title; }
		if (this.alt) {
			// Move alternate text to element text and hide
			div.innerHTML = this.alt;
			div.style.textIndent = "-9999px";
			div.style.overflow = "hidden";
			div.style.zoom = 1;
		}

		// Set alpha filter for transparent PNG
		div.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + this.src + "', sizing='scale')";
		div.style.width = this.width + "px";
		div.style.height = this.height + "px";

		// Replace image with transparent div
		this.replaceNode(div);
	});
}



// MailChimp subscribe form (footer)
var fnames = new Array();var ftypes = new Array();fnames[0]='EMAIL';ftypes[0]='email';fnames[1]='FNAME';ftypes[1]='text';fnames[2]='LNAME';ftypes[2]='text';

$(document).ready( function($) {
  var options = { errorClass: 'mce_inline_error', errorElement: 'div', onkeyup: function(){}, onfocusout:function(){}, onblur:function(){}  };
  var mce_validator = $("#mc-embedded-subscribe-form").validate(options);
  options = { url: 'http://fusepilates.us2.list-manage.com/subscribe/post-json?u=34331ed1466b1e6cc5411762b&id=9e31bcb8a4&c=?', type: 'GET', dataType: 'json', contentType: "application/json; charset=utf-8",
				beforeSubmit: function(){
					$('#mce_tmp_error_msg').remove();
					$('.datefield','#mc_embed_signup').each(
						function(){
							var txt = 'filled';
							var fields = new Array();
							var i = 0;
							$(':text', this).each(
								function(){
									fields[i] = this;
									i++;
								});
							$(':hidden', this).each(
								function(){
									if ( fields[0].value=='MM' && fields[1].value=='DD' && fields[2].value=='YYYY' ){
										this.value = '';
									} else if ( fields[0].value=='' && fields[1].value=='' && fields[2].value=='' ){
										this.value = '';
									} else {
										this.value = fields[0].value+'/'+fields[1].value+'/'+fields[2].value;
									}
								});
						});
					return mce_validator.form();
				}, 
				success: mce_success_cb
			};
  $('#mc-embedded-subscribe-form').ajaxForm(options);      
  
});

function mce_success_cb(resp){
    $('#mce-success-response').hide();
    $('#mce-error-response').hide();
    if (resp.result=="success"){
        $('#mce-'+resp.result+'-response').show();
        $('#mce-'+resp.result+'-response').html(resp.msg);
        $('#mc-embedded-subscribe-form').each(function(){
            this.reset();
    	});
    } else {
        var index = -1;
        var msg;
        try {
            var parts = resp.msg.split(' - ',2);
            if (parts[1]==undefined){
                msg = resp.msg;
            } else {
                i = parseInt(parts[0]);
                if (i.toString() == parts[0]){
                    index = parts[0];
                    msg = parts[1];
                } else {
                    index = -1;
                    msg = resp.msg;
                }
            }
        } catch(e){
            index = -1;
            msg = resp.msg;
        }
        try{
            if (index== -1){
                $('#mce-'+resp.result+'-response').show();
                $('#mce-'+resp.result+'-response').html(msg);            
            } else {
                err_id = 'mce_tmp_error_msg';
                html = '<div id="'+err_id+'" style="'+err_style+'"> '+msg+'</div>';
                
                var input_id = '#mc_embed_signup';
                var f = $(input_id);
                if (ftypes[index]=='address'){
                    input_id = '#mce-'+fnames[index]+'-addr1';
                    f = $(input_id).parent().parent().get(0);
                } else if (ftypes[index]=='date'){
                    input_id = '#mce-'+fnames[index]+'-month';
                    f = $(input_id).parent().parent().get(0);
                } else {
                    input_id = '#mce-'+fnames[index];
                    f = $().parent(input_id).get(0);
                }
                if (f){
                    $(f).append(html);
                    $(input_id).focus();
                } else {
                    $('#mce-'+resp.result+'-response').show();
                    $('#mce-'+resp.result+'-response').html(msg);
                }
            }
        } catch(e){
            $('#mce-'+resp.result+'-response').show();
            $('#mce-'+resp.result+'-response').html(msg);
        }
    }
}

