/*
* Input field labelizer.
* Makes form elements' value attribute act as a label 
* Ronnie Miller (me@ronniemlr.com)
* http://ronniemlr.com/jquery/labalize/
*/
$.fn.labelize = function() {
	return this.each(function() {
		elements = $(this).is('form') ? $(this).find('input, textarea') : $(this);
		elements.each(function() {
			$(this)
				.data('default', $(this).val())
				.focus(function() { if($(this).val() == $(this).data('default')) $(this).val(''); })
				.blur(function()  { if($(this).val() == '') $(this).val($(this).data('default')); });
		});
	});
}

iepixelfix = function() {
  width  = $(window).width();

  if(width % 2 != 0) {
    $('#nav').addClass('iepixelfix');
    if(!$.browser.mozilla) $('#nav li a').css({ left: '-1px' });
  } else {
    $('#nav').removeClass('iepixelfix');
    if(!$.browser.mozilla) $('#nav li a').css({ left: 'auto' });
  }
}

$(function() {
  if(window.location.href.indexOf('products/') != -1) {
  	// Slides
		$('.product_slides').slides({
		  play: 8000,
		  preload: true,
		  effect: 'slide',
		  crossfade: true,
		  slideSpeed: 400,
		  fadeSpeed: 500,
		  generateNextPrev: true,
		  generatePagination: false,
		  animationStart: function(i, direction, clicked) {
		    current_slide = 0;
		    // Pause (stop) the animation when the slide reaches a video.
		    // Allows the user to finish the video before the slider progresses.
		    // Also hide the pagination thumbnails, for easier video viewing.
		    switch(direction) {
		      case 'prev':
		      case 'next':
		        total_slides = $('.slides_control > div').length;
		        current_slide = (direction == 'prev') ? (i - 1) : (i + 1);
		        if(current_slide <= 0 || current_slide > total_slides) current_slide = (direction == 'prev' ? total_slides : 1);
		      break;
		
		      case 'pagination':
		        current_slide = parseInt(clicked[0]) + 1;
		      break;
		    }
		
		    if($('.slides_container div:eq('+current_slide+')').find('.youtube_video').length > 0) {
		      $('.product_slides ul.pagination').fadeOut('fast');
		      $('.product_slides').pause();
		    } else {
		      $('.product_slides ul.pagination').fadeIn('fast');
		    }
		  }
		});
  
    // Feedback modal controls
    $('a.leave_feedback').click(function(e) {
      e.preventDefault();

      // Show modal
      $('#feedback_overlay, #feedback_modal').fadeIn('fast');
      $('html, body').css({ overflow: 'hidden' });

      // Prep form
      product_name = $('h2.reg_title').attr('data-title');
      $('#feedback_product_name').val(product_name);
      $('#feedback_product_header').html(product_name);

      return false;
    });

    // Submit form
    $form = $('#feedback_form form');
    $form.submit(function() {
      url = window.location.href.match(/\.com/i) ? 'http://www.sun-world.com' : 'http://sunworld';
      
      $.ajax(url+'/send-feedback', {
          type: 'POST',
          dataType: 'json',
          data: $form.serialize(),
          success: function(response) {
          if(response && response.status == 'success') {
            $('#feedback_success').show().find('.success').html(response.message);
            $('#feedback_form_wrapper').hide();
          } else {
            error_fields = [];
            for(field in response) {
              error_fields.push(field);
            }

            $form.find('input, textarea').each(function() {
              field_id = $(this).attr('id');

              if($.inArray(field_id, error_fields) != -1) {
                $('#feedback_form #'+field_id).addClass('invalid');
              } else {
                $('#feedback_form #'+field_id).removeClass('invalid');
              }
            });
          }
        }
      });
      return false;
    });

    // Close form using escape or close button

    $('#close_modal').click(function(e) {
      e.preventDefault();
      closeModal();
      return false;
    });

    $(document).keyup(function(e) {
      if (e.keyCode == 27) { closeModal(); }
    });

    function closeModal() {
      $('#feedback_overlay, #feedback_modal').fadeOut('fast');
      $('html, body').css({ overflow: 'auto' });
      resetFeedbackForm();
    }

    function resetFeedbackForm() {
      $('#feedback_form').find('input, textarea').removeClass('invalid').each(function() {
        $(this).val($(this).data('default'));
      });
      $('#feedback_form select').val('');
      $('#feedback_stars').find('li').removeClass('active');
      $('#feedback_success').hide().find('.success').html('');
      $('#feedback_form_wrapper').show();
    }

    // Feedback star controls
    $('#feedback_stars a').bind('mouseover', function() {
      $(this).parent().prevAll().add($(this).parent()).addClass('active');
    }).bind('mouseout', function() {
      $(this).parent().nextAll().add($(this).parent()).removeClass('active');
    }).bind('click', function() {
      selected = $(this).html();
      $('#feedback_rating').val($(this).html());
      $('#feedback_stars').find('li').removeClass('active').end().find('li:lt('+(selected)+')').addClass('active');
    });

    $('#feedback_stars').bind('mouseout', function() {
      selected = $('#feedback_rating').val() || 0;
      $(this).find('li').removeClass('active').end().find('li:lt('+(selected)+')').addClass('active');
    });
    
    // Feedback form fields
    $('#feedback_form form').labelize();
  }

	if($.browser.msie || $.browser.mozilla) {
    iepixelfix();
    $(window).resize(iepixelfix);
  }
});
