window.onload = initialise;
var curHomeBanner = 1;
var rotateTimer;

$(document).ready(function() {
  $('.section-roll').mouseenter(function() { $(this).css("background-color", "#ABDDF6"); $(this).css("border", "1px solid #ABDDF6"); });
  $('.section-roll').mouseleave(function() { $(this).css("background-color", "#F1F1F1"); $(this).css("border", "1px solid #F1F1F1"); });
  correct_cur_banner_text_width(curHomeBanner);
});

function initialise() {
  new_window_links();

  if(document.getElementById('filter-results')) {
    document.getElementById('filter-results').onclick = function() {
      apply_filters();
      return false;
    }
  }
  add_field_value('form-recipient-email', 'email address...');
  add_field_value('searchtext', 'Search');
  if(document.getElementById('map')) {
    load();
  }
  if(document.getElementById('showMoreFabric1')){
    pass = 0;
    count = 1;
    while(pass == 0){
      if(document.getElementById('showMoreFabric'+count)){
        document.getElementById('showMoreFabric'+count).style.display = 'block';
        document.getElementById('moreFabricContent'+count).style.display = 'none';
        document.getElementById('showLessFabric'+count).style.display = 'block';
        count++;
      }else{
        pass = 1;
      }
    }
    //document.getElementById('showMoreFabric1').style.display = 'block';
    //document.getElementById('moreFabricContent1').style.display = 'none';
    //document.getElementById('showLessFabric1').style.display = 'block';
  }
  if(document.getElementById('google-map')){
    loadMap();
  }

  // preload homepage banner images and set up banner rotation after all other content has loaded
  $(document).ready(function() {
    var i = 2;
    // loop through all divs inside banners container and preload background images, except for first 3 divs (used for other purposes)
    $("#home-banners-container > div:gt(2)").each( function() {
      $(this).css("background-image", "url('content/images/banners/main/" + bannerImgs[i] + "')");
      i++;
    });

    if(document.getElementById('home-banners-container')) {
      rotateTimer = setInterval("rotate_home_banners()", 5000);
    }

    $('#home-banner-nav').fadeIn('slow');
  });

}

function show_home_banner(bannerNum) {
  banner_fade(curHomeBanner, bannerNum);

  // reset banner rotation timer
  clearInterval(rotateTimer);
  rotateTimer = setInterval("rotate_home_banners()", 5000);
}

function rotate_home_banners() {
  var nextBanner = (curHomeBanner % (bannerImgs.length - 1)) + 1;
  banner_fade(curHomeBanner, nextBanner);
}

function banner_fade(fromBanner, toBanner) {
  curHomeBanner = toBanner;

  document.getElementById('banner-' + fromBanner + '-nav').src = "content/images/icons/banner-nav-inactive.png";
  document.getElementById('banner-' + toBanner + '-nav').src = "content/images/icons/banner-nav-active.png";

  $('#home-banner-' + fromBanner).fadeOut(150, function() {
    $('.info-overlay').css('display', 'none');
    $('#home-banner-' + toBanner).fadeIn(150, function() {
      correct_cur_banner_text_width(toBanner);
    });
  });
}

// nasty hack to work around IE7 text width issues...
function correct_cur_banner_text_width(curHomeBanner) {
  document.getElementById('banner-info-text-' + curHomeBanner).style.width = document.getElementById('banner-info-text-' + curHomeBanner).firstChild.offsetWidth;
  $('.info-overlay').fadeTo(100, 0.4);
}


function new_window_links() {
  if(!document.getElementsByTagName) {
    return;
  } else {
    var anchors = document.getElementsByTagName("a");
    for(var i = 0; i < anchors.length; i++) {
      var anchor = anchors[i];
      if(anchor.getAttribute("href") && anchor.getAttribute("rel") == "external" || anchor.getAttribute("rel") == "external nofollow") {
        anchor.target = "_blank";
      }
    }
  }
}
function regenerate_captcha(imageID, viewName) {
  xmlhttp.open("GET", "ajax/generatecaptcha.php?imageid=" + imageID + "&viewname=" + viewName);
  xmlhttp.onreadystatechange = function() {
    if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {
      document.getElementById(imageID).src = xmlhttp.responseText;
    }
  }
  xmlhttp.send(null);
}
function apply_filters() {
  var getString = '';
  if(document.getElementById('filter-resultsForm')) {
    for(x = 0; x < document.getElementById('filter-resultsForm').elements.length; x++) {
      if(document.getElementById('filter-resultsForm').elements[x].value != 'All') {
        getString = getString + document.getElementById('filter-resultsForm').elements[x].name + '=' + document.getElementById('filter-resultsForm').elements[x].value + '&';
      }
    }
  }
  getString = getString.substring(0, getString.length - 1);
  if(getString.length > 0) {
    window.location = document.getElementById('filter-resultsForm').action + '?' + getString;
  } else {
    window.location = document.getElementById('filter-resultsForm').action;
  }
}
function add_field_value(element, val) {
  if(document.getElementById(element)) {
    document.getElementById(element).onfocus = function () { clear_field(this, val); };
    document.getElementById(element).onblur = function () { reinstate_field(this, val); };
  }
}
function trim(str, chars) {
  var replaceLeft, replaceRight;
  chars = chars || "\\s";
  replaceLeft = str.replace(new RegExp("^[" + chars + "]+", "g"), "");
  replaceRight = replaceLeft.replace(new RegExp("[" + chars + "]+$", "g"), "");
  return replaceRight;
}
function clear_field(element, val) {
  trimValue = trim(element.value);
  if(trimValue == val) {
    element.value = '';
  }
}
function reinstate_field(element, val) {
  trimValue = trim(element.value);
  if(trimValue == '') {
    element.value = val;
  }
}
function preload(imageArray) {
  $(imageArray).each(function() {
    $('<img/>')[0].src = this;
  });
}
function closeFabric(id){
  //fabricCont = document.getElementById('moreFabricContent'+id);
  fabricShowMore = document.getElementById('showMoreFabric'+id);
  //fabricCont.style.display = 'none';
  fabricShowMore.style.display = 'block';
  /*$('#moreFabricContent'+id).slideUp(1000, function() {
    // Animation complete.
  });*/
  $('#moreFabricContent'+id).hide(function(){
      //$(this).html(data).show();
  });
}
function openFabric(id){
  //fabricCont = document.getElementById('moreFabricContent'+id);
  fabricShowMore = document.getElementById('showMoreFabric'+id);
  //fabricCont.style.display = 'block';
  fabricShowMore.style.display = 'none';
  /*$('#moreFabricContent'+id).slideDown(1000, function() {
    $(this).css("position", "inherit")
  });*/
  $('#moreFabricContent'+id).show(function(){
      //$(this).html(data).show();
  });
}

function loadMap() {
    var myLatlng = new google.maps.LatLng(51.31488,-1.383981);
    var myOptions = {
      zoom: 15,
      center: myLatlng,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    }

    var map = new google.maps.Map(document.getElementById("google-map"), myOptions);

    var contentString = //'<div id="content">'+
        //'<div id="siteNotice">'+
        //'</div>'+
        '<p><strong>Berrydesign</strong></p>'+
        //'<div id="bodyContent">'+
        '<p>The Barn<br/>' +
        'Newbury, Berkshire RG20 9QE, United Kingdom<br/>' +
        '01635 255352</p>'//+
        //'</div>'+
        //'</div>';

    var infowindow = new google.maps.InfoWindow({
        content: contentString
    });

    var marker = new google.maps.Marker({
        position: myLatlng,
        map: map,
        title: 'Berry Design'
    });
    google.maps.event.addListener(marker, 'click', function() {
      infowindow.open(map,marker);
    });
  }
