var spin = '<img src="http://www.makeyourownyogurt.com/images/elements/progress-spinner.gif" width="16" height="16" alt="Loading&#8230;" id="spinner" />';
$(document).ready(function(){
  if (jQuery.browser.msie) { 
    if(parseInt(jQuery.browser.version) == '6') {
	  $('body').css({ width: '90%' });
      window.onresize=function() {
        document.documentElement.offsetWidth < 801 ? $('body').css({ width: '800px'}) : $('body').css({ width: '90%'});
      }
    }
  }
  makepopups();
  modal();
  validate();
}); 

function makepopups() {
  $('a.popup-link').click(function(event) {
    var w = $(window).width();
	var h = $(window).height();
	var defs = 'resizable,toolbar=yes,location=yes,scrollbars=yes';
    var r = $(this).attr('rel');
    if(!r){
      f = 'width=' + w + ',height=' + h + ',' + defs;
      popup($(this).attr('href'),'yogurt',f);
      return false;
    } else {
      r = $(this).attr('rel').split(' ');
	  if (jQuery.browser.msie) {
	    w = parseInt(r[1]) + 40;
		h = parseInt(r[2]) + 40;
	  } else {
	    w = r[1];
		h = r[2]
	  }
      popup($(this).attr('href'),r[0],'width=' + w + ', height=' + h + ',' + defs);
      return false;
	}
  });
}
/* SHOW/HIDE */
function show_hide (v,id,tog,open,shut) {
  if(v==0) {
    $('#'+id).hide();
  } else {
    $('#'+id).fadeIn('slow');
  }
  $('#'+tog).html(v
  ? '<a href="" onclick="show_hide(0,\'' + id + '\',\'' + tog + '\',\'' + open + '\',\'' + shut + '\'); return false" accesskey="l">' + shut + '</a>'
  : '<a href="" onclick="show_hide(1,\'' + id + '\',\'' + tog + '\',\'' + open + '\',\'' + shut + '\'); return false" accesskey="c">' + open + '</a>'
  );
}
function popup(u,n,f) {
  var newwin = window.open(u,n,f);
  newwin.focus();
}

function validate(){
  $('form.must-be-valid').submit(function(){
    var id = $(this).attr('id');
    $('#' + id + ' .error').remove();
	$('.success-box').remove();
    var hasError = false;
    var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
    var crack = /(\r|\n)(to:|from:|cc:|bcc:)/;
		
    $('#' + id +' span.req ~ :input').each(function(){
	  var t = $(this).prevAll('label').text().split(':');
	  if($(this).val() == '') {
	    $(this).after('<span class="error"><br />' + t[0] +' is Required</span>');
		hasError = true;
	  }
	});
    $('#' + id +' input[name$="email"]').each(function(){
	  if(!emailReg.test($(this).val())) {	
        $(this).after('<span class="error"><br />Please enter a valid email address</span>');
        hasError = true;
      }
	});
	$('#' + id +' textarea').each(function(){
	  if(crack.test($(this).val())) {
	    $(this).after('<span class="error"><br />There appear to be email headers in your message</span>');
	    hasError = true;
      }
	});
    if(hasError == false) {
	  var u = $(this).attr('action');
	  var d = $(this).serialize();
      var s = $('button[type="submit"]').attr('name');
      d += '&' + s + '=' + s + '&hijax=true';
      hijax(id,u,d);
	}
    return false;
  });
}

function hijax(id,u,d){
  $.ajax({
    cache: false,
    data: d,
    dataType: 'html',
    success: function(data) {
      if($('#' + id).parent('#modal-content').length > 0) {
        $('#modal-content').children('.success-box,#spinner').remove().end().prepend(data);
		$('#modal-content :text').val('');
		$('button').blur();
      } else {
	    $("#contact").slideUp("normal", function() { 
          $("#contact").before('<div class="success-box">Thank you for your Question/Comment. You will receive an appropriate response soon.</div>');
          $("#toggle-contact").hide();
        });
	  }
    },
    type: 'POST',
    url: u
  });
  return false;
}
var showModal = function(hash) {
  $('#modal-content').ajaxStart(function(){
    if($(this).html() == '') { 
      $(this).html(spin);
	} else {
	  if($('#spinner').length < 1 ) { $(this).prepend(spin); }
	}
  });
  var $t = $(hash.t);
  var $u = $t.attr('href');
  var $r = $t.attr('rel');
  if($r) {
    $r = $t.attr('rel').split(' ');
	if($r[0] == 'video') {
	  $v = $u.split('=');
	  $u = '/video.php?video=' + $v[1];
	}
    $l = $u + ' #' + $r[0];
    $f = $r[1];
  } else {
    $l = $u;
  }
  $(hash.w).css('opacity',100).show();
  $('#modal-content').load($l).ajaxStop(function() {
    if($f){ $(window).trigger($f); }
  });
}

function modal() {
  if(($('a.modal-link').length > 0) && $($('#dialog').length < 1)) {
    $('#content').after('<div class="jqmWindow" id="dialog"><h4 id="modal-head"><a href="" class="modal-close" title="Close this Window">Close</a></h4><div id="modal-content"></div><a href="#" id="modal-foot" class="modal-close">Close</a></div>');
    $('#dialog').jqm(
	  {
        overlayClass: 'modal-dimmer',
        closeClass: 'modal-close',
        trigger: 'a.modal-link',
        toTop: true,
        onShow: showModal,
        onHide: function(h) {
          $(h.w).hide().children('#modal-content').html('');
          $(h.o).remove();
        }
      }
    );
  }
}
