/*
* homepage.js
* dedicated to the tubeCentric homepage
* switches products on hover and on switchProductTime
* 
*/


var time;  // used as the hoder for the interval for switching products
var switchProductTime = 20000; // 20 seconds


// contstant variables for determining which product is currently being displayed
var TUBECORE = 'tubecore'; 
var ONCORE = 'oncore';



var hoverPanel = TUBECORE; // the variable used as product in current use


/*
* startTimer()
* starts the product panel
*/
function startTimer(){
  time=setInterval("switchProduct()", switchProductTime);
}



/*
* switchProduct()
* checks to see which product is current then 
* fakes a click on a dummy link to switch current panel
*/
function switchProduct(){
  if(hoverPanel == TUBECORE){
  
    hoverPanel = ONCORE;
    $('a#trigger-oncore').click();
  
  }else if(hoverPanel== ONCORE){
  
    hoverPanel = TUBECORE;
    $('a#trigger-tubecore').click();   
  
  }
}



/*
* stopTimer()
* stops/clears out the current timer 
*/
function stopTimer(){
  clearTimeout(time);
}



/*
* addRemoveClass()
* basically adds and removes the necessary
* class names to give each product a selected or unselected state
*/
function addRemoveClass(navelement, otherelement){  
  $('#'+navelement+'-content-li').addClass(navelement+'-selected'); 
  $('#'+navelement+'-content-li').removeClass(navelement+'-notselected');
  
  $('#'+otherelement+'-content-li').remove(otherelement+'-selected');
  $('#'+otherelement+'-content-li').addClass(otherelement+'-notselected');
  
}


    
//validation setup
var setupFormValidation = function(){

    $("a#subscribebutton").click(function(){
        submitForm();
    });
  
    $("#subscribeform").validate({
        errorLabelContainer: $("div#subscribeform-errors"),
      	rules: {
      	   firstname: { required: true}, 
      	   lastname: { required: true},
      	   email: { required: true, email:true }
      	},
      	messages:{  
      	   firstname: "please enter your first name",
      	   lastname: "please enter your last name",
      	   email: "please enter a valid email"		
      	}
   });

};



var processing = false;
var processed = false;


var submitForm = function(){
  
  var loadingMessage = 'Please wait while we process ';
  var loadingUrl = 'http://www.tubecentric.tv/wp-content/themes/tubecentric/images/76.gif';
  
  $('#subscriberesult').html('<p style="color:#4da9e0; font-size:10px;">'+loadingMessage+'<img src="'+loadingUrl+'"></p>');

  
  if(!processed && !processing && $("#subscribeform").valid()){  
	  processing = true;
	   
	  var firstname =  $("input#firstname").val();
	  var lastname =  $("input#lastname").val();
	  var email =  $("input#email").val();    
    var dataString = 'email='+ email + '&lastname=&firstname='; 
    
    //alert(dataString);return false;  
  	$.ajax({  
     	 type: "POST", 
     	 url: "subscribe.php", 
     	 data: dataString, 
  	     success: function(html){
			
			processing = false
			processed = true;
			
		   	$('#subscribeFormWrapper').slideUp(500, function(){

				if(window.console){console.info('should have hid the div... ')}

				$("#subscriberesult").html("");
				//$("#subscriberesult").html("Thank you for subscribing...");
				
				$('#subscribeFormWrapper').html('<h2>Thank you for subscribing</h2>');
				$('#subscribeFormWrapper').slideDown('fast', function(){});
				
			});
           //$("#subscriberesult").html("Thank you for subscribing");
           //$('input#firstname').val('');
           //$('input#lastname').val('');
           //$('input#email').val('');
  	     },

		 error : function(){
			processing = false;
			
		 }
  	 });
  	
  
    
    return false;

 }else{  
    $('#subscriberesult').empty();
    $(".errors").prependTo("div#subscribeform-errors");
    $("div#loading").empty();
 }
 
} 



/*
* Begin the revolution.  jquery function that 
* gets called when the page is done loading
* in here all other necessary functions are setup
* including binding the click and hoverIntent
* events to the html elements
*/         
$(document).ready(function(){
  
  setupFormValidation();
    
  //a#trigger-oncore is a fake link made
  //specifically for switching the products
  $('a#trigger-oncore').click(function(){
  
      addRemoveClass('oncore', 'tubecore');

      $('#oncore-content').fadeIn(600, function () {}); 
      $('#tubecore-content').fadeOut(300, function () {});
      
    
      return false;
		  
  });
  
  
  //switches on hover
  //performs a fake click on the the fake trigger-oncore link
  $('#oncore-content-li').hoverIntent({    
    
    timeout: 10,
    over:function () {
      
      stopTimer();
      $('a#trigger-oncore').click();
    
    },
     out: function () {
      
      startTimer();
      
    }
  });
	




  //a#trigger-tubecore is a fake link made
  //specifically for switching the products
  $('a#trigger-tubecore').click(function(){
  
      addRemoveClass('tubecore', 'oncore');
       
      $('#oncore-content').fadeOut(500, function () {});
      $('#tubecore-content').fadeIn(1000, function () {});
      

      return false;
  });
  	
  	
  	
  //switches on hover
  //performs a fake click on the the fake trigger-tubecore link  	
	$('#tubecore-content-li').hoverIntent({  
    timeout: 10,
    over:function () {
      
      stopTimer();
      $('a#trigger-tubecore').click();
    
    },
     out: function () {
      
      startTimer();
      
    }
  });
  

  //initial timer start
  startTimer();

});

