$(document).ready(function() {
  $('#password1, #password2').change(function() {
    var password1 = $('#password1').val();
    var password2 = $('#password2').val();
    if(password1 != "" && password2 != "" && password1 == password2)
      { $('#password1').removeClass("error");
	$('#password2').removeClass("error");
	$('input.button').show(); }
    if(password1 != "" && password2 != "" && password1 != password2)
      { $('#password1').addClass("error");
        $('#password2').addClass("error");
	$('input.button').hide(); }
  })

  $(function(){
      $("#overscroll").overscroll();
  });

  $("a.delete, a.buttonDelete").click(function( ev ) { var txt = $(ev.target).attr("title"); if ( txt==null || typeof(txt)=="undefined" || txt=="" ) txt = "Are you sure you wish to delete this item?"; return confirm(txt) });

  // User registration & edit page
  //$('#userEditVat').hide();
  //$('#userEditCompany').change(function(){
  //  if ( $('#userEditCompany').val()!="") {
  //    $('#userEditVat').show();
  //  }
  //  else { $('#userEditVat').hide(); }
  //});

  // Required field controls
  $(".requiredInputField").each(function() {
    if ($(this).val()=="" ) { $(this).addClass("requiredEmpty"); }
  });

  $(".requiredInputField").focus(function () {
    if ($(this).val()=="") { $(this).removeClass("requiredEmpty"); }
  });

  $(".requiredInputField").blur(function () {
    if ($(this).val()=="") { $(this).addClass("requiredEmpty"); }
    else { $(this).removeClass("requiredEmpty"); }
  });

  $('#mosaicWrapper').cycle({
  	before: function(current, next, options, forward) { updateImage(next); } ,
  	timeout:  6000,
  	delay:  6000,
	fx: 'fade',
	//pause: 1,
	speed: 1400,
        requeueTimeout: 0
  });
  $("#mosaicWrapper").hover(function(){this.cyclePause=1;},function(){this.cyclePause=0;}).click(function(){this.cyclePause=0;});;
  ImagePreview.setup();
});

var currentImage = new Array();
currentImage['mainImages'] = 0;
var mosaic = new Array();
function addMosaic( handle, img )
{
  if ( mosaic[handle]==null || typeof(mosaic[handle])=="undefined" )
  {
    mosaic[handle] = new Array();
  }
  mosaic[handle][mosaic[handle].length] = img;
}
/*
mosaic["mosaicImages"] =
(new Array(
  '<a title="Close up Of Organ Stops" href="/view/images/organ_stops"><img src="/public/images/homepage/mosaicImages/1.jpg" alt="Close up Of Organ Stops" /></a>',
  '<a title="African elephant, Loxodonta africa Pilanesburg National Park South Africa" href="/view/images/african_elephant"><img src="/public/images/homepage/mosaicImages/2.jpg" alt="African elephant, Loxodonta africa Pilanesburg National Park South Africa" /></a>',
  '<a title="Poppy field with all the colour stripped out except of red - ideal for remembrance" href="/view/images/poppies2"><img src="/public/images/homepage/mosaicImages/3.jpg" alt="Poppy field with all the colour stripped out except of red - ideal for remembrance" /></a>',
  '<a title="A beautiful church in Bignor, West Sussex" href="/view/images/sussex_church"><img src="/public/images/homepage/mosaicImages/4.jpg" alt="A beautiful church in Bignor, West Sussex." /></a>',
  '<img src="/public/images/homepage/mosaicImages/5.jpg" alt="" />',
  '<a href="/contribute" title="Contribute now"><img src="/public/images/homepage/mosaicImages/6.jpg" alt="Contribute now" /></a>'
  ));
mosaic["mainImages"] = (new Array(
  '<a href="/contribute" title="Contribute now"><img src="/public/images/homepage/mainImages/1.jpg" alt="Submit and share your original work. Create it. Use it. Share it." /></a>',
  '<a title="Contribute now to over 14,000 user contributed images, written, hymns, presentations avaliable for download" href="/contribute"><img src="/public/images/homepage/mainImages/2.jpg" alt="" /></a>'
  ));
*/
function updateImage(element) {
  if($(element).hasClass('random')) randomImage(element);
  else nextImage(element);
};

function randomImage(element) {
  var taken = new Array();
  var available = mosaic[$(element).attr("id")];
  if ( available==null || typeof(available)=="undefined" ) return;
  $(element).find("div").each( function()
  {
    var add = false;
    while(!add && taken.length < available.length) {
      add = true;
      var choice = Math.floor(Math.random()*available.length);
      for(var j=0;j<=taken.length;j++)
      {
        if(choice == taken[j])
        {
          add=false; break;
        }
      }
      if (add) taken[taken.length] = choice;
      $(this).html(available[choice]);
      }
  });
};

function nextImage(element) {
  var available = mosaic[$(element).attr("id")];
  if ( available==null || typeof(available)=="undefined" ) return;
  currentImage[$(element).attr("id")]++;
  if (currentImage[$(element).attr("id")] >= available.length) currentImage[$(element).attr("id")] = 0;
  $(element).find("div").each( function()
  {
    $(this).html(available[currentImage[$(element).attr("id")]]);
  })
};

var ImagePreview = {

  setup : function()
  {
    $(".preview").hover( function() { ImagePreview.mouseOver(this); }, function() { ImagePreview.mouseOut(this); } ).click( function() { return false; } );
  },
  mouseOver : function( a )
  {
    var ori = $(a).find("img");
    var pop = $("#previewPop");
    if ( pop.size()<=0 ) pop = $("<div></div>").attr("id","previewPop").css( { "position":"absolute", "display":"none", "background-color":"#fff", "border":"1px solid #aaa" } ).appendTo(document.body);
    pop.html("<img src=\"" + $(a).attr("href") + "\" />");
    pop.css( { "top":$(ori).offset().top+"px", "right":($(window).width()-$(ori).offset().left-10)+"px" } );
    pop.fadeIn( );
  },
  mouseOut : function( a )
  {
    $("#previewPop").fadeOut();
  }
}
