$(document).ready(function() {
  // show first teaser
  $('#carousel-teaser a:first').addClass('highlighted');
  $('.teaser-typo:first').css('visibility', 'visible');
  $('h3').attr('onfocus','if(this.blur)this.blur()');
  $('a').attr('onfocus','if(this.blur)this.blur()');  
  
  
  if($('#socialshareprivacy').length > 0){
    $('#socialshareprivacy').socialSharePrivacy({
    services : {
        facebook : {
            'status' : 'on',        
            dummy_img: '/extension/site_carevision/design/carevision_user/images/dummy_facebook.png',
            perma_option: 'off'
            
        },
        twitter : {
            'status' : 'off'
        },
        gplus : {
            'status' : 'off'
        }        
    },
    uri : 'https://www.facebook.com/pages/CARE-Vision-Nederland/157784040929451',
    txt_help: 'Wenn Sie diese Felder durch einen Klick aktivieren, werden Informationen an Facebook in die USA übertragen und unter Umständen auch dort gespeichert.',
    info_link: '#'

    }); 
  }
  
  // language selector	
  $('.menu-meta li.language a').click(function() {
	  $('.menu-meta .menu-language').toggle();
  });
  
  // teaser carousel
  $('#carousel-teaser').jcarousel({
	 scroll:	1,
	 auto: 		0,
	 animation:	750,
	 wrap:		'circular',
	 itemVisibleOutCallback: {
	  	onBeforeAnimation: highlightCarouselTeaser,
	  	onAfterAnimation:  null
  	 }

  });

  // logo carousel
  $('#carousel-logo').jcarousel({
	  	scroll:	1,
		wrap:	'circular'
  });

  // testimonial carousel
  $('#testimonial-carousel').jcarousel({
	  	scroll:	1,
		wrap:	'circular'
  });

  // teaser carousel mouseover -> change teaser image and text
  $('#carousel-teaser a').each(function() {
	$(this).mouseenter(function(){
		if(!$(this).hasClass('highlighted')) {

			$('#carousel-teaser a.highlighted').removeClass('highlighted');
			$(this).addClass('highlighted');

			$('.teaser-image').hide();
			$('.teaser-image').css('background-image', 'url(' + $(this).attr('rel') + ')');

			$('.teaser-typo').css('visibility', 'hidden');
            matches = $(this).attr('id').match(/teaser-link-(\d+)/);
			$('#teaser-typo-'+matches[1]).css('visibility', 'visible');
            
			$('.teaser-image').fadeIn(500);
		}
	}); 
  });
  
  // popup-boxes
  $("#box-appointment, #box-contact").fancybox({
        'type':				'iframe',
        'scrolling':			'no',
        'titleShow': 			false,
        'overlayColor': 		'#000000',
        'overlayOpacity': 		0.6,
        'hideOnOverlayClick':           false,
        'width':			950,
        'height':			630,
        'autoDimensions':		false,
        'autoScale':			false
  });

  $("#box-friend").fancybox({
      'type':				'iframe',
      'scrolling':			'no',
      'titleShow': 			false,
      'overlayColor':           	'#000000',
      'overlayOpacity': 		0.6,
      'hideOnOverlayClick':             false,
      'width':				950,
      'height':				630,
      'autoDimensions':                 false,
      'autoScale':			false
  });

  $("#box-movies").fancybox({
      'type':				'iframe',
      'scrolling':			'no',
      'titleShow': 			false,
      'overlayColor':                   '#000000',
      'overlayOpacity': 		0.6,
      'hideOnOverlayClick':             true,
      'width':				775,
      'height':				500,
      'autoDimensions':                 false,
      'autoScale':			false
});

  // popup-boxes
  $("#box-survey").fancybox({
        'type':				'iframe',
        'scrolling':			'no',
        'titleShow': 			false,
        'overlayColor': 		'#000000',
        'overlayOpacity': 		0.6,
        'hideOnOverlayClick':           false,
        'width':			920,
        'height':			740,
        'autoDimensions':		false,
        'autoScale':			false
  });
  
  $("#box-calculation").fancybox({
        'type':				'iframe',
        'scrolling':			'no',
        'titleShow': 			false,
        'overlayColor': 		'#000000',
        'overlayOpacity': 		0.6,
        'hideOnOverlayClick':           false,
        'width':			863,
        'height':			740,
        'autoDimensions':		false,
        'autoScale':			false                
  });
  
    $('a.fb_dynamic').each(function(){
        if ($(this).attr('href').match(/width=[0-9]+/i) && $(this).attr('href').match(/height=[0-9]+/i)) {
            var dWidth 	= parseInt($(this).attr('href').match(/width=[0-9]+/i)[0].replace('width=',''));
            var dHeight =  parseInt($(this).attr('href').match(/height=[0-9]+/i)[0].replace('height=',''));
            $(this).fancybox({
                'width': dWidth,
                'height': dHeight,
                'autoScale'     	: false,
                'transitionIn'		: 'elastic',
                'transitionOut'		: 'elastic',
                'type'			: 'iframe',
        	  	'scrolling':			'no'
            });
        } else {
            $(this).fancybox({
                'autoScale'     	: true,
                'transitionIn'		: 'elastic',
                'transitionOut'		: 'elastic',
                'type'			: 'iframe',
        	  	'scrolling':			'no'
            });
        }
    });
  
   $('.hoverImage').hover(function(){
      
       hoverText = $(this).next('p');
       hoverImg = $(this).find('div.attribute-image img');
       hoverDiv = $(this).find('div.attribute-image');

       hoverImg.css('display','none');
       
       if(hoverText.length > 0)
       {
           hoverDiv.append(hoverText);
       }
       
       hoverDiv.find('p').show();

    },function(){
        hoverImg.css('display','block');
        hoverDiv.find('p').hide();
    });
    
    $('.hoverImageLast').hover(function(){
      
       hoverText = $(this).next('p');
       hoverImg = $(this).find('div.attribute-image img');
       hoverDiv = $(this).find('div.attribute-image');

       hoverImg.css('display','none');
       
       if(hoverText.length > 0)
       {
           hoverDiv.append(hoverText);
       }
       
       hoverDiv.find('p').show();

    },function(){
        hoverImg.css('display','block');
        hoverDiv.find('p').hide();
    });
    
    
    
//   $('.no-margin-right').hover(function(){
//        $(this).next().toggle();
//    })

  
  
  
  // dropdown menu
  $('ul.main-navi').supersubs({
	minWidth:	12,
	maxWidth:	25 
//	extraWidth:	1 
  }).superfish({
		autoArrows:	false,
		hoverClass:	'hover', 
		delay:		500
//		animation:	{opacity:'show', height:'show'},
//		speed:		400
  });

  // ie6 png fix
  $(document).pngie();

});
   
   
function highlightCarouselTeaser() {
	$('#carousel-teaser a.highlighted').parent().next('li').find('a').mouseenter();
}

/**
 * Set validator defaults
 */
$.validator.setDefaults({
        errorClass: 'form-error',
        errorElement: 'span',
        errorContainer: 'p.form-error'
});


/**
 * Initialize a listener on an input field to display hints
 */
function addFormHint(inputSelector, hint) {

    // add hints on input focus
    $(inputSelector).bind('focus', function() {
        $(this).removeClass('default');
        if ($(this).val() == hint) {
            $(this).val('');
        }
    });

    // remove hints on input focus lost
    $(inputSelector).bind('blur', function() {
        if ($(this).val() == '') {
            $(this).val(hint);
            $(this).addClass('default');
        }
    });

    // remove hints on form submit
    $(inputSelector).parents('form').bind('submit', function() {
        if ($(inputSelector).val() == hint) {
            $(inputSelector).val('');
            $(inputSelector).removeClass('default');
        }
    });

    // initialize on empty form
    if ($(inputSelector).val() == '') {
        $(inputSelector).val(hint);
        $(inputSelector).addClass('default');
    }
}
   

