
if (!window.console){console={log:function(){}}};
if(Modernizr.csstransitions) {
//Homepage CSS3 animation timings
window.onload = function(){
  var autoRotate = true;
	var counter = 1;
	var classes = new Array;
	classes[0] = "zero";
	classes[1] = "one";
	classes[2] = "two";
	classes[3] = "three";
	classes[4] = "four";
	classes[5] = "five";
	var maxIndex = 4;
	var loop = true;
	
	function updateClass(cls) {
		if (null == cls) {
			cls = classes[counter];
		}
		$("#animation-banner").removeClass().addClass(cls);
	}
	
	// Get index with wrap
	function getNewIndex(increment) {
		var index = counter;
		
		// No loop
		if (!loop) {
			index += increment ? 1 : -1;
			if (index > maxIndex) {
				index = 1;
			} else if (index < 1) {
				index = maxIndex;
			}
		// Loop
		} else {
			if (increment) {
				if (index == 0) {
					updateClass(classes[1] + ' notrans');
				
					counter = 1;
					setTimeout(function() {
						updateClass();
						getNewIndex(increment);
					}, 1);
					return;
					
					//index = 2;
				} else if(index == maxIndex) {
					index = 0;	
				} else if (index == maxIndex + 1) {
					index = 1;
				} else {
					index = index + 1;
				}
			} else {
				if (index == 0) {
					index = maxIndex;
				} else if(index == 1) {
					index = maxIndex + 1;
				} else if (index == maxIndex + 1) {
					updateClass(classes[maxIndex] + ' notrans');
				
					counter = maxIndex;
					setTimeout(function() {
						updateClass();
						getNewIndex(increment);
					}, 1);
					return;
					
					//index = maxIndex - 1;
				} else {
					index = index - 1;
				}
			}
		}
		
		//console.log('New index: ' + index + ' (Inc: ' + (increment ? 'Y' : 'N') + ')');
		
		counter = index;
		
		updateClass();
	}
	
	function changeClass(){
		//return;
	  	console.log('call changeClass');
    	/*if(counter == 2) {  
		  console.log("Animation starting; fade in #next button")
			$("#next").fadeIn("slow");
		}*/
    
		/*if(counter == 4){
		  console.log("Animation complete; fade out #next button")
			$("#animation-pagination #prev").fadeOut("slow");
		} else {*/
		  console.log("classes[counter]: " + classes[counter])
			setTimeout( function() {
				if(autoRotate == true){
				  console.log('call change class again, autoRotate is true (actual value:' + autoRotate + ')');
  			
  				getNewIndex(true);
  				
  				//updateClass();
				  changeClass();
        };
			}, 5000 );
		//}
		
	}
	$("#animation-pagination #prev").fadeIn("slow");
	$("#animation-pagination #next").fadeIn("slow");
  changeClass();
	
	$("#animation-pagination #next").click(function(){
	  autoRotate = false; // disable autorate once the user has clicked next
	  console.log('autoRotate: ' + autoRotate);
	  console.log('click #next');
	  
	  	getNewIndex(false);
	  
		//if(counter > 1){
  		//counter--;
  		//updateClass();
		//}
		/*
		if(counter== 1) {
  	  console.log('counter is == 1, fade out the #next button');
  		$("#animation-pagination #next").fadeOut();
		} else {
  	  console.log('counter is != 1, fade in the #prev button');
	  	$("#animation-pagination #prev").fadeIn();
		}
		*/
	});
	
	$("#animation-pagination #prev").click(function(){
	  autoRotate = false; // disable autorate once the user has clicked prev
	  console.log('autoRotate: ' + autoRotate);
	  console.log('click #prev');
	  
	  	getNewIndex(true);
	  
		//if(counter < 4){
  	  //console.log('counter is < 4, so increment the counter and add a class matching the new counter value');
  		//counter++;
  		
  		//updateClass();

		//}
		
		/*
		if(counter== 4) {
  	  console.log('counter is == 4, fade out the #prev button');
  		$("#animation-pagination #prev").fadeOut();
		} else {
  	  console.log('counter is != 4, fade in the #next button');
	  	$("#animation-pagination #next").fadeIn();
		}
		*/
		
	});
	
}



} // end modernizr
else {
        $('#animation-pagination').hide();
        var params = {};
        params.wmode = "opaque";
        swfobject.embedSWF("homePageBanner_988x345.swf?v=2", "flash-replacement", "988", "345", "9.0.0", null, null, params);
        //$('#home-banner').empty().append('<object id="myId" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="988" height="345"><param name="movie" value="homePageBanner_988x345.swf?v=2" wmode="opaque" /><!--[if !IE]>--><object type="application/x-shockwave-flash" data="homePageBanner_988x345.swf?v=2" width="988" height="345"><!--<![endif]--><!--[if !IE]>--></object><!--<![endif]--></object>');
        //$('#home-banner').append('');
}

$(document).ready(function() {

//Carousel for resellers on homepage
$('#reseller-carousel-container').carouFredSel({
	width: 986,
	height: 190,
	circular: false,
	infinite: false,
	items: {
		visible: 3
	},
	pagination: "#carousel-pagination",
	auto: {
	  play: false,
		pauseDuration: 5000
	},
	scroll: {
		duration: 750,
		pauseOnHover: true
	}
});

//Carousel for homepage features if browser can't handle CSS3 animation

$("#animation-backup-container").carouFredSel({
	auto: {
	  play: true,
	  pauseDuration: 5000
	},
	scroll: {
		duration: 750,
		pauseOnHover: true
	}
}
);
       
   
    

});

/* Take a tour page tabs */

$(document).ready(function() {

	$( "#become-a-reseller, #take-a-tour" ).tabs({
    // Rather than an AJAX load, behave like a normal link when there's a URL
    // in the href of a tab
    select: function(event, ui) {
      var url = $.data(ui.tab, 'load.tabs');
      if( url ) {
          location.href = url;
          return false;
      }
      return true;
    }
  });
  
  // The Support tabs are handled differently because we want to do an ajax load
  // of one tab
	$( "#support" ).tabs({
    // TODO: fix transition so that screen doesn't jump vertically when this code is uncommented
    // fx: { 
    //   opacity: 'toggle' 
    // }
  });
	
	// Lightbox
	$(".marketing-video-link").colorbox({iframe:true, innerWidth:645, innerHeight:400});
	
  screenZindex = 1;
  // Take a Tour, features tab
  $('.feature-tab a, .features-table-inner a').click(function(e){
    screenZindex += 1;
    $('.feature-tab.active, .item.active').removeClass('active');
    $(this).parent().addClass('active');
    // $(".feature-col .screen").fadeOut();
    existingScreen = $(".feature-col .screen");
    console.log("Show screen: " + $(this).attr('href'));
    $("#phone-with-screens").prepend($("<img src='" + $(this).attr('href') + "' class='screen' style='display:none; z-index:" + screenZindex +"'>").fadeIn(300, function() { existingScreen.remove(); }));
    
    
    console.log('click feature tab');
    e.preventDefault;
    return false;
  });


  // Main Become a reseller page
  // -----------------------------
  $('#solutions-table a').click(function(e){
    console.log($(this).attr('href'));
    $('body,html').scrollTop(0);
    target = parseInt($(this).attr('href'));
    $('#become-a-reseller').tabs("select", parseInt($(this).attr('href')));
    e.preventDefault;
    return false;
  })

  // Features tab, bottom carousel
  // -----------------------------
  // TODO: DRY up this code.
  var featureCarouselCurrent = 1;
  var featureCarouselNext = function(){
    if(featureCarouselCurrent == 1) {
      showFeatureCarouselTwo();
    } else if(featureCarouselCurrent == 2){
      showFeatureCarouselThree();
    } else if(featureCarouselCurrent == 3){
      showFeatureCarouselFour();
    } else if(featureCarouselCurrent == 4){
      showFeatureCarouselFive();
    } else if(featureCarouselCurrent == 5){
      showFeatureCarouselSix();
    }
  }
  var featureCarouselPrevious = function(){
    if(featureCarouselCurrent == 2) {
      showFeatureCarouselOne();
    } else if(featureCarouselCurrent == 3){
      showFeatureCarouselTwo();
    } else if(featureCarouselCurrent == 4){
      showFeatureCarouselThree();
    } else if(featureCarouselCurrent == 5){
      showFeatureCarouselFour();
    } else if(featureCarouselCurrent == 6){
      showFeatureCarouselFive();
    }
  }

  var showFeatureCarouselOne = function(){
    console.log('showFeatureCarouselOne')
    $('#features-tab .item-wrapper').animate({
      "margin-left": 52
    }, 500, 'easeInOutQuad');
    $('#features-carousel-pagination a.selected').removeClass('selected');
    $('#show1').addClass('selected');
    featureCarouselCurrent = 1;
    console.log("featureCarouselCurrent: " + featureCarouselCurrent);
    $('#features-tab a#next').fadeIn();
    $('#features-tab a#prev').fadeOut();
  }
  var showFeatureCarouselTwo = function(){
    console.log('showFeatureCarouselTwo')
    $('#features-tab .item-wrapper').animate({
      "margin-left": -728
    }, 500, 'easeInOutQuad');
    $('#features-carousel-pagination a.selected').removeClass('selected');
    $('#show2').addClass('selected');
    featureCarouselCurrent = 2;
    console.log("featureCarouselCurrent: " + featureCarouselCurrent);
    $('#features-tab a#next').fadeIn();
    $('#features-tab a#prev').fadeIn();
  }
  var showFeatureCarouselThree = function(){
    console.log('showFeatureCarouselThree')
    $('#features-tab .item-wrapper').animate({
      "margin-left": -1508
    }, 500, 'easeInOutQuad');
    $('#features-carousel-pagination a.selected').removeClass('selected');
    $('#show3').addClass('selected');
    featureCarouselCurrent = 3;
    console.log("featureCarouselCurrent: " + featureCarouselCurrent);
    $('#features-tab a#next').fadeIn();
    $('#features-tab a#prev').fadeIn();
  }
  var showFeatureCarouselFour = function(){
    console.log('showFeatureCarouselFour')
    $('#features-tab .item-wrapper').animate({
      "margin-left": -2288
    }, 500, 'easeInOutQuad');
    $('#features-carousel-pagination a.selected').removeClass('selected');
    $('#show4').addClass('selected');
    featureCarouselCurrent = 4;
    console.log("featureCarouselCurrent: " + featureCarouselCurrent);
    $('#features-tab a#next').fadeIn();
    $('#features-tab a#prev').fadeIn();
  }
  var showFeatureCarouselFive = function(){
    console.log('showFeatureCarouselFive')
    $('#features-tab .item-wrapper').animate({
      "margin-left": -3068
    }, 500, 'easeInOutQuad');
    $('#features-carousel-pagination a.selected').removeClass('selected');
    $('#show5').addClass('selected');
    featureCarouselCurrent = 5;
    console.log("featureCarouselCurrent: " + featureCarouselCurrent);
    $('#features-tab a#next').fadeIn();
    $('#features-tab a#prev').fadeIn();
  }
  var showFeatureCarouselSix = function(){
    console.log('showFeatureCarouselSix')
    $('#features-tab .item-wrapper').animate({
      "margin-left": -3848
    }, 500, 'easeInOutQuad');
    $('#features-carousel-pagination a.selected').removeClass('selected');
    $('#show6').addClass('selected');
    featureCarouselCurrent = 5;
    console.log("featureCarouselCurrent: " + featureCarouselCurrent);
    $('#features-tab a#next').fadeOut();
    $('#features-tab a#prev').fadeIn();
  }

  $('#features-tab #show1').click(function(e){
    showFeatureCarouselOne();
    e.preventDefault();
    return false;
  })
  
  $('#features-tab #show2').click(function(e){
    showFeatureCarouselTwo();
    e.preventDefault();
    return false;
  })
  
  $('#features-tab #show3').click(function(e){
    showFeatureCarouselThree();
    e.preventDefault();
    return false;
  })
  
  $('#features-tab #show4').click(function(e){
    showFeatureCarouselFour();
    e.preventDefault();
    return false;
  })
  
  $('#features-tab #show5').click(function(e){
    showFeatureCarouselFive();
    e.preventDefault();
    return false;
  })
  
  $('#features-tab #show6').click(function(e){
    showFeatureCarouselSix();
    e.preventDefault();
    return false;
  })
  
  $('#features-tab a#next').click(function(e){
    featureCarouselNext();
    e.preventDefault();
    return false;
  })
  
  $('#features-tab a#prev').click(function(e){
    featureCarouselPrevious();
    e.preventDefault();
    return false;
  })


  // FAQs
  
  $('#support .faqs h4').click(function(e){
    $('.faqs h4').next().hide();
    $('.faqs h4').removeClass('active')
    //console.log('click FAQ question');
    $(this).toggleClass('active');
    $(this).next().toggle();
    e.preventDefault;
    return false;
  })
  
  // Case Studies

  $('#view-case-study-1').click(function(e){
	$('#case-study-1').fadeOut(300);
	$('#case-study-2').fadeOut(300);
	$('#case-study-3').fadeOut(300);
	$('#case-study-4').fadeOut(300);		
	$('#full-case-study-1').delay(300).fadeIn(300);	
  })
  
 $('#view-case-study-2').click(function(e){
	$('#case-study-1').fadeOut(300);
	$('#case-study-2').fadeOut(300);
	$('#case-study-3').fadeOut(300);
	$('#case-study-4').fadeOut(300);		
	$('#full-case-study-2').delay(300).fadeIn(300);	
  })
  
   $('#view-case-study-3').click(function(e){
	$('#case-study-1').fadeOut(300);
	$('#case-study-2').fadeOut(300);
	$('#case-study-3').fadeOut(300);
	$('#case-study-4').fadeOut(300);		
	$('#full-case-study-3').delay(300).fadeIn(300);	
  })
  
   $('#view-case-study-4').click(function(e){
	$('#case-study-1').fadeOut(300);
	$('#case-study-2').fadeOut(300);
	$('#case-study-3').fadeOut(300);
	$('#case-study-4').fadeOut(300);		
	$('#full-case-study-4').delay(300).fadeIn(300);	
  })
  
  $('.back-to-case-study').click(function(e){
	$('#full-case-study-1').fadeOut(300);	
	$('#full-case-study-2').fadeOut(300);
	$('#full-case-study-3').fadeOut(300);
	$('#full-case-study-4').fadeOut(300);		
	$('#case-study-1').fadeIn(300);
	$('#case-study-2').fadeIn(300);
	$('#case-study-3').fadeIn(300);
	$('#case-study-4').fadeIn(300);		
  })


  // Contact form
  
  $("select#SFDC00ND0000002kRcN").change( function() {
    //console.log($(this).attr('value'));
    if($(this).attr('value') == "other") {
      $('div.comp_desc_other').slideDown();
    } else {
      $('div.comp_desc_other').slideUp();
      $('input#comp_desc_other').val('');
    }
    // check input ($(this).val()) for validity here
  });
  
  $("#partFrm").validate({
    rules: {
      first_name: "required",// simple rule, converted to {required:true}
  	  last_name: "required",
  	  country: "required",
  	  phone: "required",
  	  SFDC00ND0000002kRcN: "required",
  	  rep_phone:{
    	  required: true,
    	  equalTo: "#phone"
  	  },
      email: {// compound rule
        required: true,
        email: true
  	  },
  	  rep_email: {// compound rule
        required: true,
        email: true,
    	  equalTo: "#email"
      }       
    },
    messages: {
      fname: "<div>This field is mandatory</div>",
  	  lname:"<div>This field is mandatory</div>",
  	  country:"<div>This field is mandatory</div>",
  	  phone:"<div>This field is mandatory</div>",
  	  SFDC00ND0000002kRcN:"<div>This field is mandatory</div>",
  	  rep_email:{
    		equalTo:"<div>Please correct your email address</div>"
  	  },
  	  rep_phone:{
    		equalTo:"<div>Please correct your phone number</div>"
	    }
    }
  });

  function numbersonly(myfield, e, dec)
  {
  var key;
  var keychar;

  if (window.event)
     key = window.event.keyCode;
  else if (e)
     key = e.which;
  else
     return true;
  keychar = String.fromCharCode(key);

  // control keys
   if ((key==null) || (key==0) || (key==8) || 
      (key==9) || (key==13) || (key==27)  || (key==43) || (key==45) || (key==32))
     return true;

  // numbers
  else if ((("0123456789").indexOf(keychar) > -1))
     return true;

  // decimal point jump
  else if (dec && (keychar == "."))
     {
     myfield.form.elements[dec].focus();
     return false;
     }
  else
     return false;
  }


});
