/*******************************************************************************

	CSS on Sails Framework
	Title: columbiasmiles
	Author: XHTMLized (http://www.xhtmlized.com/)
	Date: July 2011

*******************************************************************************/

$(document).ready(function() {
	
	xSetup.init();

});

var xSetup  = {
	
	init: function(){
		xSetup.equalHeight();
		xSetup.validateSchedule();
		xSetup.validateContact();
		$("#home #browseable").scrollable({circular: true}).navigator().autoscroll({interval: 5000});
		$("#bottom .blog .scrollable").scrollable({ vertical: true, mousewheel: false, circular: true }).autoscroll({interval: 5000});
		$("#home .clients-slider .scrollable, #bottom .blog .scrollable").jcarousel({
			wrap: 'circular'
		});
		$(".clients-slider .next-slide").click(function(){
			$(".clients-slider .jcarousel-container .jcarousel-next").trigger('click');
			return false;
		})
		$(".clients-slider .prev-slide").click(function(){
			$(".clients-slider .jcarousel-container .jcarousel-prev").trigger('click');
			return false;
		})
		
		$('.contact .main-content form p').each(function(){
			$(this).height($(this).innerHeight())
		});
		
		
//Add hover and click event to each of the item in the carousel
    $('.clients-slider li a').click(function () {
         
        //remove the active class from the clients-inside
        $('#home .clients-inside div').removeClass('active');
         
        //display the main image by appending active class to it.        
        $('#home .clients-inside div.' + $(this).attr('rel')).addClass('active'); 
             
        //remove the active class from the clients-slider
        $('.clients-slider li a img').removeClass('active');
         
        //display the main image by appending active class to it.        
        $(this).children('img').addClass('active'); 
		         
        return false;
    });
	
	
	
	
	//Slideshow on home page
		$(".slidetabs").tabs("#slider > div", {
		// enable "cross-fading" effect
		effect: 'fade',
		fadeOutSpeed: "slow",
		// start from the beginning after the last tab
		rotate: true
	// use the slideshow plugin. It accepts its own configuration
	}).slideshow({ autoplay: true, interval: 5000, clickable: false});

		
	},
	
	//equal height for sidebar on page
	equalHeight: function(){
		var contentH = $('.page .main-content').height()
		if(!$('body').hasClass('contact')){
			if($('.page #sidebar').length != 0){
				$('.page #sidebar').height(contentH).find('.logo').show();
			}			
		}
	},
	
	validateSchedule: function (){
		var name = $('#bottom form #name').val();
		var email = $('#bottom form #email').val();
		var interest = $('#bottom form #service-interest').val();
		var err = 0;
		$('#bottom .col form').submit(function(){
			if($('#bottom form #name').val() == ''){
				$('#bottom form #name').parent().prepend('<span class="err"></span>');
				err = 1;
			}else{
				$('#bottom form #name').siblings('span.err').remove();
				err = 0;
			}
			if($('#bottom form #email').val() == ''){
				$('#bottom form #email').parent().prepend('<span class="err"></span>');
				err = 1;
			}else{
				$('#bottom form #email').siblings('span.err').remove();
				err = 0;
			}
			if($('#bottom form #service-interest').val() == ''){
				$('#bottom form #service-interest').parent().prepend('<span class="err"></span>');
				err = 1;
			}else{
				$('#bottom form #service-interest').siblings('span.err').remove();
				err = 0;
			}
											
			if(err == 1){
				$('#bottom form div.err').show(); 
				return false;				
			}
		})
	},
	
	validateContact: function(){
		var name = $('#contact-main .my-name');
		var email = $('#contact-main .my-email');
		var err = 0;
		var errMsg = '<span class="err">Please complete this mandatory field.</span>';
		$('#contact-main').submit(function(){
			if($('#contact-main .my-name').val() == ''){
				$(errMsg).insertAfter(name).show();
				err = 1;
			}else{
				name.siblings('span.err').remove();
				err = 0;
			}
			if($('#contact-main .my-email').val() == ''){
				$(errMsg).insertAfter(email).show();
				err = 1;
			}else{
				email.siblings('span.err').remove();
				err = 0;
			}
			
			if(err == 1){
				return false;				
			}			
		})
		
		$('#contact-main .err').live('hover',function(){
			$(this).each(function(){
				$(this).prev().focus();
				$(this).remove();				
			})
			
		});
	}	
	
	
}


// DD_belatedPNG fix for IE6
if (typeof DD_belatedPNG !== 'undefined') {
	var PNG_fix_selectors = [
		'#sidebar .logo',
		'#bottom span.shadow',
		'#bottom form button',
		'#bottom .social .tw',
		'a.more',
		'.wrap',
		'#header .site-name',
		'#header .info a',
		'#navigation .content-wrap',
		'#navigation .shadow-left',
		'#navigation .shadow-right',
		'#navigation li a span',
		'#home .clients .ribbon',
		'.main .subscribe'
	];
	DD_belatedPNG.fix(PNG_fix_selectors.join(','));
}

// ===================
// = AddThis =
// ===================
     var addthis_config = {
        data_ga_property: 'UA-24168043-1',
        data_track_clickback: true
     };

//(function($){
jQuery(document).ready(function($) {
  $.fn.wrapFirstLetter = function(options) {
    
    var settings = {
      'charpattern' : /[a-z0-9]/i,
      'tag'         : 'span',
      'class'       : 'f-letter'
    };
    
    if (options) {
        $.extend(true, settings, options);
    }
        
    function _wrap(content) {
      var len = content.length;
      var intag = false;
      
      for (var i=0; i<len; i++) {
        if (content[i] == '<') {
          intag = true;
          continue;
        }

        if (content[i] == '>') {
          intag = false;
          continue;
        }

        if (intag) {
          continue;
        }

        if (settings.charpattern.test(content[i])) {
          var pre = '';
          var post = '';
          var t = settings.tag;
          var c = settings['class'];
          if (i==0) {
            post = content.substring(1);
            return pre + "<"+t+" class=\"" + c + "\">" + content[i] + "</"+t+">" + post;
          }
          if (i==(len-1)) {
            pre = content.substring(0,i);
            return pre + "<"+t+" class=\"" + c + "\">" + content[i] + "</"+t+">" + post;
          }

          pre = content.substring(0,i);
          post = content.substring(i+1);
          return pre + "<"+t+" class=\"" + c + "\">" + content[i] + "</"+t+">" + post;
        }
      }
    }
        
        
    return this.each(function() {
      var obj = $(this);
      var content = obj.html();
      var newcontent = _wrap(content);
      obj.html(newcontent);
    });
    
  };
    
//})(jQuery);
});


$(function() {
	//adding wrap on First Letter to style it
	$('#default .main .blogs p:first').wrapFirstLetter();
	$('#platform .main p:first').wrapFirstLetter();
});



