// remap jQuery to $
(function($){})(window.jQuery);


/* trigger when page is ready */
$(document).ready(function (){

	$('.resourcethumblist').equalHeights();
	
	
    // validate Download form on keyup and submit     
    var validator = $(".resourceform").validate({ 
        rules: { 
            firstName: "required",
            lastName: "required",
            emailAddress: {
				required: true, 
				email: true
            },
            jobRole: "required",
            familiarWithDigitalInterviewing: "required",
            howWouldYouBestDescribeDigitalInterviewing: "required",
            industry: "required"
            
        }, 
        messages: { 
            firstName: " ", 
            lastName: " ",
            emailAddress: " ",
            jobRole: " ",
            familiarWithDigitalInterviewing: " ", 
            howWouldYouBestDescribeDigitalInterviewing: " ",
            industry: " " 
        }, 
 
        // the errorPlacement has to take the table layout into account 
        errorPlacement: function(error, element) { 
            //if ( element.is(":radio") ) 
            //    error.appendTo( element.parent().next().next() ); 
            //else if ( element.is(":checkbox") ) 
            //    error.appendTo ( element.next() ); 
            //else 
                error.appendTo( element.parent().parent() ); 
        },
        errorElement: "div", 
        // set this class to error-labels to indicate valid fields 
        success: function(label) { 
            // set   as text for IE 
            label.html(" ").addClass("checked"); 
        },
        submitHandler: function(form) {
			//alert('success');
			//return false;
			
			// Ajax Form Submit ::
			var form_options = { 
		        success:       showResponse,
		        error:		   showError,
		        type: 		   'POST',
		        timeout:   	   3000 
		    }; 
			
			//$('#form62').submit(function() { 
		    	$('#form62').ajaxSubmit(form_options); 
		        return false; 
		   // }); 

		}
 
    }); 
	

    $('#mycarousel').jcarousel({
        auto: 5,
        scroll:6,
        wrap: 'circular',
        animation:3000,
        initCallback: mycarousel_initCallback
    });
    
    
	var $container = $('.customer_bricks');
	$container.imagesLoaded(function(){
	  $container.masonry({
	    itemSelector : '.brick',
	    columnWidth : 0
	  });
	});  

	$("a.fancyvideo").fancybox({
		'transitionIn'	:	'fade',
		'speedIn'		:	1000, 
		'speedOut'		:	500,
		'padding'		:	0, 
		'overlayShow'	:	true,
		'overlayOpacity':	0.9,
		'overlayColor'	:	'#000',
    	'scrolling'   : 'no'
	});
	

	
	
	$("a.fancydemo").fancybox({
		'width'				: 500,
		'height'			: 600,
        'autoScale'     	: false,
        'transitionIn'		: 'none',
		'transitionOut'		: 'none',
		'type'				: 'iframe',
		'scrolling'			: 'no'
	});	
	
	
    // validate Download form on keyup and submit     
    var c_validator = $(".contact_form").validate({ 
        rules: { 
            firstName: "required",
            lastName: "required",
            email: {
				required: true, 
				email: true
            },
            jobRole: "required",
            familiarWithDigitalInterviewing: "required",
            videoInterviewsWhen: "required",
            industry: "required"
        }, 
        messages: { 
            firstName: " ", 
            lastName: " ",
            email: " ",
            jobRole: " ",
            familiarWithDigitalInterviewing: " ", 
            videoInterviewsWhen: " ",
            industry: " " 
        }, 
 
        // the errorPlacement has to take the table layout into account 
        errorPlacement: function(error, element) { 
            //if ( element.is(":radio") ) 
            //    error.appendTo( element.parent().next().next() ); 
            //else if ( element.is(":checkbox") ) 
            //    error.appendTo ( element.next() ); 
            //else 
                error.appendTo( element.parent().parent() ); 
        },
        errorElement: "div", 
        // set this class to error-labels to indicate valid fields 
        success: function(label) { 
            // set   as text for IE 
            label.html(" ").addClass("checked"); 
        }
 
    });
    
    
    $(".docstrip a").bind('click', function() {
    	//alert($(this).attr('href'));
    	$("#resource_detail").fadeOut().load($(this).attr('href'), function() {
    		$("#resource_detail").fadeIn();
    	});
    	
    	return false;
    });
    
   
	

	
	

}); // End Document.ready

autoSubmitForm = function() {
	var form_options = { 
        success:       showResponse,
        error:		   showError,
        type: 		   'POST',
        timeout:   	   3000 
    }; 

    $('#form62').ajaxSubmit(form_options); 
    return false; 

}


showResponse = function(responseText, statusText, xhr, $form)  { 
	var _response = statusText;
	//var _reponseObj = jQuery.parseJSON(_response);
	//var _success = _reponseObj.success;
	//alert(responseText + "////" + statusText);
	// Success
	if (_response == 'success') {	
		
		$('#formcontainer').hide();
		$('#resourcecontainer').show();
		
	}
	// Fail
	if (_response != 'success') {
    	
		//alert('The form was submitted, but there were errors');
	}
	
}

// System Error
showError = function(responseText, statusText, xhr, $form) {
	//alert('There was an error processing. Please try again');
	
}



var interval = 0;

startHeaderRotation = function() {
	getImages();
	var _totalSlides = $('#_total').text();
	var _sliderWidth = parseInt(_totalSlides)*960;
	$('.slideset').css('width',_sliderWidth+'px');
	if (_totalSlides == 1) {
		$('#slider_arrow_next').css('display','none');
		$('#slider_arrow_previous').css('display','none');
	} else {
		interval = setInterval('autoRotate()',8000);
	}
		
	
}

getImages = function() {
	var _totalSlides = parseInt($('#_total').text())-1;
	var i=1;
	do
  	{
  		$.get('/embeds/ajax_slide/'+i+'/', function(data) {
			$('.slideset').append(data);
		});
		i++;
  	}
	while (i<=_totalSlides);
	
	$('#slider_arrow_next').fadeIn("slow");
	$('#slider_arrow_previous').fadeIn("slow");
	
	enableSlideButtons();
}

imageClickForward = function() {

	if (!arguments[0]) {
		// Kill Auto ::
		clearInterval(interval);
	}	 
	
	disableSlideButtons();
	 
	$('.slide').first().stop().animate({'marginLeft':'-960px'}, 600, function() {	
		$(".slide:first").remove().insertAfter($(".slide:last"));
		$(".slide:last").css({'marginLeft':'0px'});
		enableSlideButtons();
	});

}

imageClickBackward = function() {

	if (!arguments[0]) {
		// Kill Auto ::
		clearInterval(interval);
	}	
	
	disableSlideButtons();
	
	$(".slide:last").css({'marginLeft':'-960px'});
	$(".slide:last").remove().insertBefore($(".slide:first"));
	
	$('.slide').first().stop().animate({'marginLeft':'0px'}, 600, function() {	
		enableSlideButtons();
	});

}

autoRotate = function() {
	//disableSlideButtons();
	imageClickForward('auto');
}

enableSlideButtons = function() {
	$('#slider_arrow_next').css({'backgroundPosition':'0 0'});
	$('#slider_arrow_next').bind('click', function() {
		imageClickForward();
	});
	$('#slider_arrow_previous').css({'backgroundPosition':'0 0'});
	$('#slider_arrow_previous').bind('click', function() {
		imageClickBackward();
	});
	
}
disableSlideButtons = function() {
	$('#slider_arrow_next').unbind('click');	
	$('#slider_arrow_next').css({'backgroundPosition':'-96px 0'});
	$('#slider_arrow_previous').unbind('click');	
	$('#slider_arrow_previous').css({'backgroundPosition':'-96px 0'});
	
}






function mycarousel_initCallback(carousel)
{
    // Disable autoscrolling if the user clicks the prev or next button.
    carousel.buttonNext.bind('click', function() {
        carousel.startAuto(0);
    });

    carousel.buttonPrev.bind('click', function() {
        carousel.startAuto(0);
    });

    // Pause autoscrolling if the user moves with the cursor over the clip.
    carousel.clip.hover(function() {
        carousel.stopAuto();
    }, function() {
        carousel.startAuto();
    });
};


$.fn.equalHeights = function(px) {
	
	$(this).each(function(){
		
		var currentTallest = 0;
		$(this).children().each(function(i){
			if ($(this).height() > currentTallest) { currentTallest = $(this).height(); }
		});
		$(this).children().css({'height': currentTallest}); 
	});
	return this;
};


/* optional triggers

$(window).load(function() {
	
});

$(window).resize(function() {
	
});

*/
