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

// NAVIGATION HOVER!



 $('.sc_tab').click(function() {

    $('.sc_tab').removeClass('sc_tab_active');

    $(this).addClass('sc_tab_active');

    var which = $(this).attr('title');

    $(this).parent().parent().find('.sc_tab_single_box').css('display','none'); 

    $(this).parent().parent().find('.sc_tab_single_box').eq(which).css('display','block');

  });



  $('.menu-item').click(function() {


    window.location = $(this).find('a:first').attr('href');

    //alert(';dsds');

    //window.location($(this).find('a:first').attr('href'));

  });

    // var dropdown_level = 0; // bsedit


    $('.sub-menu').parent().find('a:first').addClass('nav_sub_arrow');

    $('#nav_wrapper .menu').children('li').children('a').addClass('top_level');

    $('#nav_wrapper .menu').children('li').children('a').removeClass('nav_sub_arrow');

    

    $('#nav_wrapper .menu-item').hover(function(){
		// bsedit 
		dropdown_level = $(this).parents('ul').size() - 1;
		// end of bsedit

		if(dropdown_level <= 0){

            $('#nav_wrapper .menu').find('a').removeClass('nav_sub_arrow_active');  

            //$('#nav_wrapper .menu').find('a').addClass('nav_sub_arrow_passive');  

          $(this).addClass('main_hover_left');

          $(this).children('a').addClass('main_hover_right');

            $('.sub-menu').parent().find('a:first').addClass('nav_sub_arrow');

        $('#nav_wrapper .menu').children('li').children('a').addClass('top_level');

        $('#nav_wrapper .menu').children('li').children('a').removeClass('nav_sub_arrow');

      //alert('sasa');

      }                                                                                             

       

                  Cufon.replace('#nav_wrapper ul.sub-menu li a', {

fontFamily: 'Myriad Pro Semibold',

textShadow: 'none',

hover: 'true'

});

      $(this).find('.sub-menu:first').stop(true,true).slideDown(200).show();        

      $(this).find('a:first').addClass('nav_sub_arrow_active');

      $('#nav_wrapper .menu').children('li').children('a').removeClass('nav_sub_arrow_active');       

     // dropdown_level++; // bsedit

    },function(){            

      $(this).find('.sub-menu:first').stop(true,true).slideUp(0);

      $(this).find('a:first').removeClass('nav_sub_arrow_active');

     // dropdown_level--; // bsedit
	
		// bsedit 
		dropdown_level = $(this).parents('ul').size() - 1;
		// end of bsedit

       if(dropdown_level <= 0){

        $(this).removeClass('main_hover_left');

          $(this).children('a').removeClass('main_hover_right');

       }

    } );

  /*  $('.sub-menu').parent().find('a:first').addClass('nav_sub_arrow');    

    $('.menu').children('li').children('a').removeClass('nav_sub_arrow');

     $('.menu').children('li').children('a').addClass('top_level');

    $('.menu').find('a').addClass('sub_menu_active');

    $('.top_level').removeClass('sub_menu_active');

    

  $('#nav_wrapper .menu-item').hover(function(){



//   alert('ds');

    if($(this).children('a:first').hasClass('top_level') == true)

    {

    //  $(this).find('a').removeClass('nav_sub_arrow_active');

      $(this).addClass('main_hover_left');

      $(this).find('a:first').addClass('main_hover_right');

    }

    else

    {

     

    }

     $(this).parent().parent().addClass('nav_sub_arrow_active');

    // $(this).find('.sub-menu').parent().children('a:first').addClass('nav_sub_arrow_active');

    dropdown_level++;

    // DROPDOWN //

    $(this).find('.sub-menu:first').stop(true,true).slideDown(200).show();

  },function(){

    dropdown_level--;

    $(this).children('a:first').removeClass('nav_sub_arrow_active');

    $(this).removeClass('main_hover_left');

    $(this).find('a:first').removeClass('main_hover_right');

    if(dropdown_level== 0)

    {

        $('#nav_wrapper .menu-item').find('a').removeClass('nav_sub_arrow_active');

    }

    //alert(dropdown_level);

    // DROPDOWN //

    

    $(this).find('.sub-menu:first').stop(true,true).slideUp(0);

  } );

                                              */

  

  // PORTFOLIO IMAGE HOVER:

  var speed = 300;

  var portfolio_link_opacity = 1;

  var icon_link_opacity = 1;

  

  

  var tour_slide_width = 700;

  var actual_tour_selected = 1;

 

    $('.tour_pagenavi_left').click(function(){

    var clicked = $(this).attr('rel');

    

         $('#tour_nav a').removeClass('tour_nav_active');

     $('#tour_nav a').eq(clicked-1).addClass('tour_nav_active');

     

    $('#tour_slider ul').children('li').eq(clicked-1).css('display','block');

    $('#tour_slider ul').children('li').eq(clicked-1).css('height','auto');

    $('#tour_slider ul').stop().animate({left: tour_slide_width*(clicked-1)*-1}, 300, function() {

      var size = $('#tour_slider ul').children('li').size();

      for(var i =0; i< size; i++)

      {

      

        

        if(i != (clicked-1))

        {

     //   alert (clicked + " " + i);  

         $('#tour_slider ul').children('li').eq(i).css('height','7px');

        }

      }

    });

    

    //alert()

    return false;

  } ); 

  

  

   $('.tour_pagenavi_right').click(function(){

       

    var clicked = $(this).attr('rel');

    

     $('#tour_nav a').removeClass('tour_nav_active');

     $('#tour_nav a').eq(clicked-1).addClass('tour_nav_active');

    $('#tour_slider ul').children('li').eq(clicked-1).css('display','block');

    $('#tour_slider ul').children('li').eq(clicked-1).css('height','auto');

    $('#tour_slider ul').stop().animate({left: tour_slide_width*(clicked-1)*-1}, 300, function() {

      var size = $('#tour_slider ul').children('li').size();

      for(var i =0; i< size; i++)

      {

      

        

        if(i != (clicked-1))

        {

     //   alert (clicked + " " + i);  

         $('#tour_slider ul').children('li').eq(i).css('height','7px');

        }

      }

    });

    

    //alert()

    return false;

  } );

  

  $('#tour_nav a').click(function(){

     $('#tour_nav a').removeClass('tour_nav_active');

     $(this).addClass('tour_nav_active');

    var clicked = $(this).attr('rel');

    $('#tour_slider ul').children('li').eq(clicked-1).css('display','block');

    $('#tour_slider ul').children('li').eq(clicked-1).css('height','auto');

    $('#tour_slider ul').stop().animate({left: tour_slide_width*(clicked-1)*-1}, 300, function() {

      var size = $('#tour_slider ul').children('li').size();

      for(var i =0; i< size; i++)

      {

      

        

        if(i != (clicked-1))

        {

     //   alert (clicked + " " + i);  

         $('#tour_slider ul').children('li').eq(i).css('height','7px');

        }

      }

    });

    

    //alert()

    return false;

  } );

  



  $('.widget_sidebar_content .menu li').hover(function(){

   $(this).children('.sub-menu').stop().slideDown(100);//.css('display', 'block');

  },function(){

   $(this).children('.sub-menu').stop().slideUp(0);

  }); 





  if($.browser.msie)

  { portfolio_link_opacity = 0.2;

    icon_link_opacity = 0.65;

  }

   $('.wp-caption a').click(function(){

    $.prettyPhoto.open($(this).attr('href'),'','' );

    return false;

   });

   $('.gallery-icon').find('a').click(function(){

    $.prettyPhoto.open($(this).attr('href'),'','');

    

   return false;

   });

  $('#fc_submit').click(function(){

     var send_email = true;

    var filter = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/;

        



    if (!filter.test($('#fc_email').attr('value')) || $('#fc_email').attr('value') == '') {

        $('#fc_email').animate({left:-10},100).animate({left:10},100).animate({left:-10},100).animate({left:10},100).animate({left:0},100);

       

    /*    $('#cf_mail').animate({borderTop: '1px solid #ff0000'},{duration:5000, queue:false}); 

        $('#cf_mail').animate({borderLeft: '1px solid #ff0000'},{duration:5000, queue:false});  

        $('#cf_mail').animate({borderRight: '1px solid #ff0000'},{duration:5000, queue:false});

        $('#cf_mail').animate({borderBottom: '1px solid #ff0000'},{duration:5000, queue:false});    */

       send_email = false;  

    }

 

    if( $('#fc_name').attr('value') == '' ) { 

      $('#fc_name').animate({left:-10},100).animate({left:10},100).animate({left:-10},100).animate({left:10},100).animate({left:0},100);

     

      send_email = false;  

    }

   

    if( $('#fc_text').attr('value') == '' ) {

      $('#fc_text').animate({left:-10},100).animate({left:10},100).animate({left:-10},100).animate({left:10},100).animate({left:0},100);

     

      send_email = false;

    }

 

    if(send_email == false){ return false;}    

  } );

  

  

  $('.portfolio_link').hover(function(){

  $(this).find('img').stop().animate({opacity:0.5}, speed);

  /*

    $(this).find('.portfolio_hover').css('opacity', '0');

    $(this).find('.portfolio_hover').css('display', 'block');

    $(this).find('.portfolio_hover').stop().animate({opacity:portfolio_link_opacity},speed);

    

    $(this).find('.icon_zoom').css('opacity', '0');

    $(this).find('.icon_zoom').css('display', 'block');

    $(this).find('.icon_zoom').stop().animate({opacity:icon_link_opacity},speed);

    

    $(this).find('.icon_play').css('opacity', '0');

    $(this).find('.icon_play').css('display', 'block');

    $(this).find('.icon_play').stop().animate({opacity:icon_link_opacity},speed);        */

    

  },function(){   

  $(this).find('img').stop().animate({opacity:1}, speed);

  /*

    var div_holder = $(this).find('div');

    $(this).find('.portfolio_hover').stop().animate({opacity:0},speed, function(){ div_holder.css('display','none'); });

    $(this).find('.icon_zoom').stop().animate({opacity:0},speed, function(){ div_holder.css('display','none'); });

    $(this).find('.icon_play').stop().animate({opacity:0},speed, function(){ div_holder.css('display','none'); });                */

  });

  

    $('.gallery_link').hover(function(){

    $(this).find('img').stop().animate({opacity:0.5}, speed);/*

    $(this).find('.gallery_hover').css('opacity', '0');

    $(this).find('.gallery_hover').css('display', 'block');

    $(this).find('.gallery_hover').stop().animate({opacity:portfolio_link_opacity},speed);

    

    $(this).find('.icon_zoom').css('opacity', '0');

    $(this).find('.icon_zoom').css('display', 'block');

    $(this).find('.icon_zoom').stop().animate({opacity:icon_link_opacity},speed);

    

    $(this).find('.icon_play').css('opacity', '0');

    $(this).find('.icon_play').css('display', 'block');

    $(this).find('.icon_play').stop().animate({opacity:icon_link_opacity},speed);     */

    

  },function(){ 

    $(this).find('img').stop().animate({opacity:1}, speed);                                                                           /*

    var div_holder = $(this).find('div');

    $(this).find('.gallery_hover').stop().animate({opacity:0},speed, function(){ div_holder.css('display','none'); });

    $(this).find('.icon_zoom').stop().animate({opacity:0},speed, function(){ div_holder.css('display','none'); });

    $(this).find('.icon_play').stop().animate({opacity:0},speed, function(){ div_holder.css('display','none'); });  */

  });

	jQuery(".toggle_body").hide(); 



	jQuery("h4.toggle").toggle(function(){

		jQuery(this).addClass("toggle_active");

		}, function () {

		jQuery(this).removeClass("toggle_active");

	});

	

	jQuery("h4.toggle").click(function(){

		jQuery(this).next(".toggle_body").slideToggle();



	});

 

  $('#submit_contactform').click(function(){

    var send_email = true;

    var filter = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/;

        



    if (!filter.test($('#cf_mail').attr('value')) || $('#cf_mail').attr('value') == '') {

        $('#cf_mail').animate({left:-10},100).animate({left:10},100).animate({left:-10},100).animate({left:10},100).animate({left:0},100);

        $('#cf_mail').addClass('input_alert');

    /*    $('#cf_mail').animate({borderTop: '1px solid #ff0000'},{duration:5000, queue:false}); 

        $('#cf_mail').animate({borderLeft: '1px solid #ff0000'},{duration:5000, queue:false});  

        $('#cf_mail').animate({borderRight: '1px solid #ff0000'},{duration:5000, queue:false});

        $('#cf_mail').animate({borderBottom: '1px solid #ff0000'},{duration:5000, queue:false});    */

       send_email = false;  

    }

       else

    {

      $('#cf_mail').removeClass('input_alert');

    }

            

    if( $('#cf_name').attr('value') == '' ) { 

      $('#cf_name').animate({left:-10},100).animate({left:10},100).animate({left:-10},100).animate({left:10},100).animate({left:0},100);

      $('#cf_name').addClass('input_alert');

      send_email = false;  

    }

    else

    {

      $('#cf_name').removeClass('input_alert');

    }

    

    if( $('#cf_text').attr('value') == '' ) {

      $('#cf_text').animate({left:-10},100).animate({left:10},100).animate({left:-10},100).animate({left:10},100).animate({left:0},100);

      $('#cf_text').addClass('input_alert');

      send_email = false;

    }

       else

    {

      $('#cf_text').removeClass('input_alert');

    }

    if(send_email == false){$('.contact_form .btn_a').removeClass('btn_a').addClass('btn_a'); return false;}      



  } );

  $('#submit').click(function(){

  

       var send_email = true;

    var filter = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/;

    if($(this).parent().parent().find('.admin_checker').attr('title') != 'admin')

    {    



      if (!filter.test($('#email').attr('value')) || $('#email').attr('value') == '') {

          $('#email').animate({left:-10},100).animate({left:10},100).animate({left:-10},100).animate({left:10},100).animate({left:0},100);

         send_email = false;  

      }

              

      if( $('#author').attr('value') == '' ) { 

        $('#author').animate({left:-10},100).animate({left:10},100).animate({left:-10},100).animate({left:10},100).animate({left:0},100);

        send_email = false;  

      }

    }

    if( $('#comment').attr('value') == '' ) {

      $('#comment').animate({left:-10},100).animate({left:10},100).animate({left:-10},100).animate({left:10},100).animate({left:0},100);

      send_email = false;

    }

    if(send_email == false){$('#submit').removeClass('btn_a').addClass('btn_a'); return false;}      

  });

  
// bsedit start
	// Remove anchor (#) links from main menu
	$('#nav_wrapper ul.menu a[href=#]').attr('href', '').click(function(){return false});
	
	// Activate datepicket
	$.datepicker.setDefaults( $.datepicker.regional[ "it" ] );
	$('.datepicker').datepicker({
		dateFormat: 'dd-mm-yy',
		minDate: '01-01-1911',
		maxDate: '31-12-2011',
		defaultDate: '01-01-1990',
		gotoCurrent: true,
		changeYear: true,
		changeMonth: true,
		yearRange: '1911:2011'
	});
	$('.datepromotion').datepicker({
		dateFormat: 'dd-mm-yy',
		minDate: '02-06-2011',
		maxDate: '28-08-2011',
		defaultDate: '+1d',
		gotoCurrent: true,
		beforeShowDay: function (date) {
			var ok = true;
			var past = false;
			// check past days
			var today = new Date();
			today.setDate(today.getDate()-1); // Trick to allow to choose today
			if (date < today) {
				ok = false;
				past = true;
			}
			// check sundays and saturdays
			if (date.getDay() == 0 || date.getDay() == 6) {
				ok = false;
			}
			// check other holidays
			if (date.getDate() == 15 && date.getMonth() == 7) { // August 15th
				ok = false;
			}
			if (date.getDate() == 2 && date.getMonth() == 5) { // June 2nd
				ok = false;
			}
			// check high season dates
			var d1 = new Date(2011, 7, 7, 0, 0 , 0, 0); // August 7th
			var d2 = new Date(2011, 7, 21, 0, 0 , 0, 0); // August 21st
			if (date >= d1 && date <= d2) {
				ok = false;
			}
			// return values
			if (!ok) {
				return Array(false, '', past? '' : 'Promozione non valida in questo giorno');
			}
			else {
				return Array(true, '', '');
			}
		}
	});
	// Conditional field
	$('form#mp_1 #mp_1_14, form#mp_2 #mp_2_32').change(function() {
		switch($(this).val()) {
			case "Centri commerciali": 
			case "Volantinaggio scuole": 
			case "Radio": 
			case "Centri estivi": 
			case "Altro": 
				$('#mp_1_39, #mp_2_40').val('');
				$('p:has(#mp_1_39), p:has(#mp_2_40)').show();
				break;
			default:
				$('#mp_1_39, #mp_2_40').val('-');
				$('p:has(#mp_1_39), p:has(#mp_2_40)').hide(); // Not empty to allow validation
				break;
		}
	});
	$('form#mp_1 #mp_1_14, form#mp_2 #mp_2_32').change();
	// Link to rules of promotion
	$('label[for=mp_1_19]').wrapInner('<a id="zoomRules" href="#sidebar_widgets"></a>');
	$('#zoomRules').prettyPhoto({width: 600, hieght: 400});
	// Verify checkboxes are checked 
	$('form#mp_1').submit(function(){
		if($('#mp_1_19:checked').size() != 1) {
			alert('Devi accettare il regolamento della promozione.');
			return false;
		}
		else if($('#mp_1_16:checked').size() != 1) {
			alert('Devi acconsentire al trattamento dei dati personali.');
			return false;
		}
	});
// bsedit end
  

});
