$(function () {
  $(".popup_block .radio").dgStyle();
  $(".popup_block .checkbox").dgStyle();
  
  if (!$.browser.opera) {
      $('.popup_block select.select').each(function(){
          var title = $(this).attr('title');
          if( $('option:selected', this).val() != ''  ) title = $('option:selected',this).text();
          $(this)
              .css({'z-index':10,'opacity':0,'-khtml-appearance':'none'})
              .after('<span class="select">' + title + '</span>')
              .change(function(){
                  val = $('option:selected',this).text();
                  $(this).next().text(val);
                  })
      });
    };
    
 function clear_form_elements(ele) {

    jQuery(ele).find(':input').each(function() {
        switch(this.type) {
            case 'password':
            case 'select-multiple':
            case 'select-one':
            case 'text':
            case 'textarea':
                jQuery(this).val('');
                break;
            case 'checkbox':
            case 'radio':
                this.checked = false;
        }
    });

  }
  
  jQuery('.submitNewsletter').click(function(){
      clear_form_elements(jQuery('#popup-newletter-slide-form'));
      
      jQuery('#popup-newletter-slide').fadeIn();
      jQuery('#popup-newletter-slide-email').val(jQuery('#text_field_mail').val());
      
      var popMargTop = (jQuery('#popup-newletter-slide').height() + 80) / 2;
      var popMargLeft = (jQuery('#popup-newletter-slide').width() + 80) / 2;
      if($(window).height() > jQuery('#popup-newletter-slide').height() +80) {
        jQuery('#popup-newletter-slide').css({
            'margin-top' : -popMargTop,
            'margin-left' : -popMargLeft
        });
      }
      else {
        jQuery('#popup-newletter-slide').css({
            'top' : '10px',
            'margin-left' : -popMargLeft
        });
        jQuery('.popup_block').css('position', 'absolute');
      }
      
  
      jQuery('body').append('<div id="fade"></div>'); 
      jQuery('#fade').css({'filter' : 'alpha(opacity=80)'}).fadeIn();  
  
      return false;
   });
   
   
   //Close Popups and Fade Layer
  jQuery('a.close-popup').click(function() { //When clicking on the close or fade layer...
      jQuery('#fade , .popup_block').fadeOut(function() {
          jQuery('#fade').remove();  //fade them both out
      });
      return false;
  });

  $('#popup-newletter-slide-submit').click(function(){
    jQuery.ajax({
        type: 'POST',
        url: "/p/newsletter-subscribe.php",
        dataType: "json",
        data:
          jQuery('#popup-newletter-slide-form').serialize()
        ,
        success: function(data) {
          jQuery("#popup-newletter-slide-error").html('<ul></ul>');
          if(!data.succes){
            jQuery.each(data.errors, function(index, element){jQuery("#popup-newletter-slide-error ul").append('<li>'+element+'</li>');});
          }else {
            jQuery('#fade , .popup_block').fadeOut(function() {
              jQuery('#fade, a.close-popup').remove();  //fade them both out
            });
         }
        }
    });       
  });
});

