jQuery(document).ready(function(){

	if((jQuery('body').attr('class')).indexOf('browserSafar') < 0){
			//Only check if css is disabled for other browsers accept safari, if CSS is disabled, stop all javascript executions
       	if(jQuery('#wrap').css('fontSize') == '16px'){
       		return;
       	}       	
	}

	pageWidth = jQuery(window).width();
	
	$tma("ul.fcbNav").fcbNav().superbump().find('ul').bgIframe({opacity:false});
    
	jQuery("img.imgNormal").parent().addClass("linkedimage");
	jQuery("img.imgLeft").parent().addClass("linkedimage");
	jQuery("img.imgRight").parent().addClass("linkedimage");
	
    //jQuery('#breadcrumbs').prepend('<a href="#" class="printThisPage">Print page<\/a>');
    jQuery('.printThisPage').click(function (){
   		window.print();
   		return false;
    });	
	 
	assignTeaserHover('#featured .teaser');
	assignTeaserHover('#contentSecondary .teaser');
	assignTeaserHover('#indexPageContent .teaser');
	assignTeaserHover('.searchResults li');
	assignTeaserHover('#teasers li.teaser');
	assignTeaserHover('.rssTeaser');
	assignTeaserHover('.module .indexTeaser');
	
	$tma('#mainContactForm').validate({
		rules: {
			yourName : "required",
			email : {required: true, email: true},
			comment : "required",
			phone : {required: true, number : true}
								
		},
		messages: {
			yourName : 'Pleaser enter your name.',
			email : 'Please enter a valid email address',
			comment: 'Please enter a comment',
			phone : 'Please enter your phone number in digits with no spaces between them.'
		},		
        errorElement: "p"

	});
	
	if(!jQuery.browser.msie || jQuery.browser.msie && jQuery.browser.version > 6){

		jQuery("#loginDetails .loginButton").toggle(function(){
			jQuery("#clientLogin").animate({
				height: '37px'
			}, "medium");
			jQuery("#clientLogin form").animate({
				top: '0'
			}, "medium");
		},function(){
			jQuery("#clientLogin").animate({
				height: '0'
			}, "medium");
			jQuery("#clientLogin form").animate({
				top: '-37px'
			}, "medium");
		});	
	
	}

	setInputPlaceHolder();
	
});

jQuery(window).resize(function(){
	pageWidth = jQuery(window).width();
	jQuery("ul.fcbNav").superbump();
});

function replaceInput(inputClass,anchorClass){   

    if(inputClass == ''){
        inputClass = 'inputButton';
    }
    
    jQuery('form').each(function () {         	    	
        jQuery(this).find('input.'+inputClass).each(function (){    	                       
            jQuery(this).hide();
            jQuery(this).after('<a href="#" class="'+anchorClass+'"><span>'+jQuery(this).val()+'<\/span><\/a>');                                
        });    
    });
    
    jQuery("."+anchorClass).click(function (){
		var sFormName = jQuery(this).parents('form').attr("id");
		jQuery("#"+sFormName).submit();
		return false;
	});
}

function assignTeaserHover(className){		
			
	jQuery(className).click(function(){
		var sHref = jQuery(this).find("a").attr("href");
		
		if(jQuery(this).find("a").attr("target") == '_blank')
			window.open(sHref);			
		else
		   	window.location= sHref;
		   	
		return false;			
	});
	
    jQuery(className).hover(
   		function() { jQuery(this).addClass('teaserHover');}, 
        function() { jQuery(this).removeClass('teaserHover');
    });
}

function addEventTracking(category, action, label){
	try{
		pageTracker._trackEvent(category, action, label);	
	}
	catch(err){}
}

function trim(str) {
    return str.replace(/^\s*/, "").replace(/\s*$/, "");
}

function setInputPlaceHolder(){	
	jQuery('[placeholder]').focus(function() {
		  var input = jQuery(this);
		  if (input.val() == input.attr('placeholder')) {
		    input.val('');
		    input.removeClass('placeholder');
		  }
		}).blur(function() {
		  var input = jQuery(this);
		  if (input.val() == '' || input.val() == input.attr('placeholder')) {
		    input.addClass('placeholder');
		    input.val(input.attr('placeholder'));
		  }
		}).blur().parents('form').submit(function() {
		  jQuery(this).find('[placeholder]').each(function() {
		    var input = jQuery(this);
		    if (input.val() == input.attr('placeholder')) {
		      input.val('');
		    }
		  })
	});
}

function scrollTo(selector) {
    var targetOffset = jQuery(selector).offset().top;
    jQuery('html,body').animate({scrollTop: targetOffset}, 500,"linear");
}

(function($) {
	jQuery.fn.tmaCarousel = function(options) {
				
		//set default options
		var defaults = {
			objectid: '',
			total: 0,
			bAnimate: 0,
			aJaxURL: '/apps/ajaxContentByCategory.cfm'
		};
		
		// Extend our default options with those provided.
		var opts = jQuery.extend(defaults, options);
		
		// check if objectid is provided, if not do nothing.
		if (defaults.objectid.length > 0) {
	
			// grab the width and calculate left value
			var item_width = this.find('li').outerWidth(); 
			var left_value = item_width * (-1); 
			var cc = this.find('ul.carouselContent');
			var currentPage = 1;
			
			// set width of clip of carousel so things float correctly
			cc.css({'width' : cc.find('li').length * cc.parent().width() + "px"});
			
			//move the last item before first item, just in case user click prev button
			cc.find('li:first').before(cc.find('li:last'));
			
			//set the default item to the correct position 
			cc.css({'left' : left_value});
			
			setInterval(function(){ 
				
				//get the right position
				var left_indent = parseInt(cc.css('left')) - item_width;
				
				currentPage = currentPage + 1;
				if (currentPage > options.total) {
					//set page the first page
					currentPage = 1;
				}
				
				//slide the item 
				cc.parent().parent().find('ul.carouselContent:not(:animated)').animate({'left' : left_indent}, 1500, function () {
					
					//move the first item and put it as last item
					cc.find('li:last').after(cc.find('li:first'));                 	
					
					//set the default item to correct position
					cc.css({'left' : left_value});
				
					cc.parent().parent().find('.screenNumber').html(currentPage + ' of ' + options.total);
					
				});

			},7000);
			
			//autoScroll(this,defaults,cc,item_width,left_value,currentPage);
			
			// add functionality to paginator
			this.find(".pagination a").bind('click', function() {
				
				if(jQuery(this).hasClass('next')) {
					
					//get the right position
					var left_indent = parseInt(cc.css('left')) - item_width;
					
					currentPage = currentPage + 1;
					if (currentPage > defaults.total) {
						//set page the first page
						currentPage = 1;
					}
					
					//slide the item 
					jQuery(this).parent().parent().find('ul.carouselContent:not(:animated)').animate({'left' : left_indent}, 1500, function () {
						
						//move the first item and put it as last item
						cc.find('li:last').after(cc.find('li:first'));                 	
						
						//set the default item to correct position
						cc.css({'left' : left_value});
					
						jQuery(this).parent().parent().find('.screenNumber').html(currentPage + ' of ' + defaults.total);
						
					});
					
				} else if (jQuery(this).hasClass('prev')) {

					//get the right position            
					var left_indent = parseInt(cc.css('left')) + item_width;
					
					currentPage = currentPage - 1;
					if (currentPage < 1) {
						//set page the last page
						currentPage = defaults.total;
					}
	
					//slide the item            
					jQuery(this).parent().parent().find('ul.carouselContent:not(:animated)').animate({'left' : left_indent}, 1500,function(){    
						
						//move the last item and put it as first item            	
						cc.find('li:first').before(cc.find('li:last'));           
	
						//set the default item to correct position
						cc.css({'left' : left_value});
						
						jQuery(this).parent().parent().find('.screenNumber').html(currentPage + ' of ' + defaults.total);
					
					});
				}
				
				//cancel the link behavior
				return false;
								
			});
		}
		//cancel the link behavior
		return false;
	};
	
	function autoScroll(el,options,obj,item_width,left_value,currentPage) {
	    play = setInterval(function(){ //Set timer - this will repeat itself every 7 seconds
	        
			//get the right position
			var left_indent = parseInt(obj.css('left')) - item_width;
			console.log(currentPage);
			currentPage = currentPage + 1;
			if (currentPage > options.total) {
				//set page the first page
				currentPage = 1;
			}
			console.log(el);
			console.log(jQuery().html());
			//slide the item 
			jQuery(el).find('ul.carouselContent:not(:animated)').animate({'left' : left_indent}, 1000, function () {
				
				//move the first item and put it as last item
				obj.find('li:last').after(obj.find('li:first'));                 	
				
				//set the default item to correct position
				obj.css({'left' : left_value});
			
				jQuery(el).find('.screenNumber').html(currentPage + ' of ' + options.total);
				
			});

	    }, 7000); //Timer speed in milliseconds (7 seconds)
	};
	
})(jQuery);
