jQuery(document).ready(function($) {

	$('a.imwb-nav-tab').click(function(event) {
	
   	event.preventDefault();
  
	   if($(this).hasClass('imwb-nav-tab-active'))
	      return false;
	      
	   // set the current variation number
	   IMWB_ZP_Ads.curAdNum = $(this).attr( "href" ).split('_')[2];
	   
	   $('.imwb-nav-tab-wrapper a.imwb-nav-tab-active').removeClass('imwb-nav-tab-active');
	   $(this).addClass('imwb-nav-tab-active');
	   	   
      //  Update title and content fields
	   $('input[name=imwbZonPressAd_TitleFld]').val($('input[name=imwbZonPressAd_Title\\['+IMWB_ZP_Ads.curAdNum+'\\]]').val());   

	   $('textarea[name=imwbZonPressAd_ContentFld]').val($('textarea[name=imwbZonPressAd_Content\\['+IMWB_ZP_Ads.curAdNum+'\\]]').val());
	   
	   return false;
	});
	
	$('input[name=imwbZonPressAd_TitleFld]').bind('keyup change blur paste propertychange input', function(){
	   $('input[name=imwbZonPressAd_Title\\['+IMWB_ZP_Ads.curAdNum+'\\]]').val($(this).val());
	});	
	
	$('textarea[name=imwbZonPressAd_ContentFld]').bind('keyup change blur paste propertychange input', function(){
	   var updText = $(this).val();
	   $('textarea[name=imwbZonPressAd_Content\\['+IMWB_ZP_Ads.curAdNum+'\\]]').val(updText);
	});
	
	$('select[name="imwb_zonpress_theme_ads[numads]"]').change(function(){
      var numAds = $(this).val();
      if(numAds == 0) 
         $('#zp-ads-wrapper').hide();
      else {
         for(var i = 1; i <= 5; i++) {
            if(i <= numAds)
               $('#zp_ad_'+i).removeClass('hidden');
            else
               $('#zp_ad_'+i).addClass('hidden');
         } 
      
         $('#zp-ads-wrapper').show();
      }
      if(numAds < $('input[name="imwb_zonpress_theme_ads[adsperpage]"]').val())
         $('input[name="imwb_zonpress_theme_ads[adsperpage]"]').val(numAds);
	}); 
	
	$('input[name="imwb_zonpress_theme_ads[adsperpage]"]').bind('change blur', function(){
	   var numAds = $('select[name="imwb_zonpress_theme_ads[numads]"]').val();
	   if($(this).val() > numAds) {
	      $(this).val(numAds);
	      $(this).next('#error-msg').show().html("You entered more ads than you are using").fadeOut(4000);
	   }
	});
	
	$('input[name="imwb_zonpress_theme_ads[postsshownfrom]"]').bind('blur', function(){
	   var toFld = $('input[name="imwb_zonpress_theme_ads[postsshownto]"]').val();
	   if($(this).val() > toFld) {
	      $('input[name="imwb_zonpress_theme_ads[postsshownto]"]').val($(this).val());
	   }
	});
	
	$('input[name="imwb_zonpress_theme_ads[postsshownto]"]').bind('blur', function(){
  	   var fromFld = $('input[name="imwb_zonpress_theme_ads[postsshownfrom]"]').val();
      if($(this).val() < fromFld ) {
	      $(this).val(fromFld );
	      $(this).nextAll('#error-msg').show().html("You entered a value less than the from field").fadeOut(4000);
	   }
	});
});