/*************************************************/
/* Lauren Weir Designs -- Javascript (All Pages) */
/*************************************************/

// global reference to slide show 
// functions for animation access
var lwd__rotate_slides = null;
var lwd__sifr_tooltips = null;

jQuery(document).ready(function($) {
  
  
  
  /*************************/
  /* Auto-Sizing of Footer */
  /*************************/
  
  var lws__auto_sizing_of_footer = function() {
    var f = $('#lwd-footer');
    var c = $('#lwd-content');
    var h = $('#lwd-header');
    var wh = $(window).height();
    var avail = wh - h.height() - c.height();
    var minimum = 137;
    f.height(avail < minimum ? minimum : avail);
  };
  
  // adjust the size of the footer when the 
  // window is resized and initially size it
  $(window).resize(lws__auto_sizing_of_footer);
  $(window).resize();
  
  
    
  /****************/
  /* Contact Page */
  /****************/
  
  // create a shortcut page reference
  var pg = $('div#lwd-content.contact');
  if (pg.size())
  {
    // initially focus when prepopulated
    var e = pg.find('input[name=email]');
    var m = pg.find('textarea[name=message]');    
    if (m.val()) e.focus();
  }
  
  // enable the checkbox to change its state when it is clicked using styles
  pg.find('div.contact-form div.add-to-list div.checkbox').click(function(e) {
    e.preventDefault();
    e.stopPropagation();    
    $(this).toggleClass('checked');
  });
  
  // when the send message link is clicked start ajax process
  pg.find('div.contact-form div.send').data('click', function() {
    var f = $('div#lwd-content.contact div.contact-form form');
    
    // the first task in our attempt to 
    // send the message is to show status
    var ajs = f.find('div.ajax-status');
    ajs.find('.loading-indicator').show();
    ajs.find('.results').hide();
    ajs.show();
    sIFR.replace(gothic, { selector: 'div#lwd-content.contact div.content div.contact-form div.ajax-status div.loading-indicator p.text', css: ['.sIFR-root { color: #361F0D; font-size: 10px; text-align: center; }'], wmode: 'transparent' });
    
    // define a function which will handle
    // both success and failure of our post
    var complete = function(data, status) {
      if (!(data instanceof Object)) data = new Object();
      var msg = null;
      ajs.find('.results .text').empty();
      
      // determine if any error was reported by the
      // process of simply from the ajax request
      if ((!data.success) || (status != 'success'))
      {
        msg = data.error || 'Error; Please try again.';
        ajs.find('.results').addClass('error');
      }
      else
      {
        msg = ajs.find('.results .success').text();
        ajs.find('.results').removeClass('error');
      }
      
      // after the proper status message is constructed show
      ajs.find('.results .text').html('<span>'+msg+'</span>');
      ajs.find('.loading-indicator').hide();
      ajs.find('.results').show();
      var close = ajs.find('.results .close');
      sIFR.replace(gothic, { selector: 'div#lwd-content.contact div.content div.contact-form div.ajax-status div.results p.text span', css: ['.sIFR-root { color: #'+(ajs.find('.results').hasClass('error') ? 'AA0000' : '361F0D')+'; letter-spacing: 0.3; leading: 1.0; font-size: 9px; text-align: left; }'], wmode: 'transparent' });
      if (!close.hasClass('sifred')) { sIFR.replace(gothicb, { selector: 'div#lwd-content.contact div.content div.contact-form div.ajax-status div.results p.close', css: ['.sIFR-root { color: #361F0D; letter-spacing: 0.4; font-size: 9px; text-align: right; }', 'a { color: #361F0D; text-decoration: none; }', 'a:hover { color: #361F0D; text-decoration: underline; }'], wmode: 'transparent', onRelease: function (fi) { var fn = $(fi.getAncestor()).data('click'); if (fn) fn(); }}); close.addClass('sifred'); }
    };
    
    // compose an execute the ajax request
    $.ajax({ url: 'contact_form_processor.php',
             type: "POST",
             cache: false,
             data: { email: f.find('input[name=email]').val(),
                     message: f.find('textarea[name=message]').val(),
                     addme: (f.find('div.add-to-list div.checkbox').hasClass('checked') ? 'YES' : 'NO')
                   },
             dataType: 'json',
             success: complete,
             error: complete });
  });
  
  // store a function which will be called with a sifr link to close the ajax status window
  pg.find('div.contact-form div.ajax-status .results .close').data('click', function() {
    var ajs = $('div#lwd-content.contact div.contact-form div.ajax-status');
    ajs.hide();
    if (ajs.find('.results').hasClass('error')) return;
    var f = ajs.parents('form');
    f.find('input[name=email]').val('');
    f.find('textarea[name=message]').val('');
  });
  
  // initialize the opacity of the ajax status window to transparent
  pg.find('div.contact-form div.ajax-status').css('opacity', 0.9);
  
  
  
  /*****************/
  /* Products Page */
  /*****************/
  
  // create a shortcut page reference
  var pg = $('div#lwd-content.products');
  
  // in order to have the multiple image gallery function redirect clicks
  pg.find('div.product-previews div.product .caption').click(function(e) {
    e.preventDefault();
    e.stopPropagation(); 
    $(this).parents('div.product').find('a.thickbox').click();
  });

  
  
  /************/
  /* Tooltips */
  /************/
  
  // preload the background image
  var lwd__tooltipBG = new Image();
  lwd__tooltipBG.src = 'pics/bg__transparent_brown.png';
  
  // create a shortcut page reference
  var pg = $('div#lwd-content');
  
  // define a global function which
  // will sifr tooltips when visible
  lwd__sifr_tooltips = function() 
  {
    var tips = $('div#lwd-content div.featured-image').find('div.lwd-tooltip');
    tips.each(function(i, el) {
      var tip = $(el);      
      if (!tip.parents('div.featured-image').data('hover')) { tip.hide(); return; }
      tip.show();
      if (tip.hasClass('sifred')) return;
      if ((tip.parents('div.slide').size() && (!tip.parents('div.slide:visible').size()))) return;
      sIFR.replace(gothic, { selector: 'div#lwd-content div.lwd-tooltip div.link div.text-link', css: ['.sIFR-root { color: #F48F44; letter-spacing: 0.5; font-size: 9px; }', 'a { color: #F48F44; text-decoration: none; }', 'a:hover { color: #F48F44; text-decoration: underline; }'], wmode: 'transparent', onRelease: function (fi) { var fn = $(fi.getAncestor()).parent().data('click'); if (fn) fn($(fi.getAncestor()).parent().data('product-name')); }});
      sIFR.replace(gothic, { selector: 'div#lwd-content div.lwd-tooltip div.message', css: ['.sIFR-root { color: #EECAAF; letter-spacing: 0.3; font-size: 9px; leading: 6.0; }', 'a { color: #F48F44; text-decoration: none; }', 'a:hover { color: #F48F44; text-decoration: underline; }'], wmode: 'transparent' });
      tip.addClass('sifred');
    });
  };
  
  // we have to show and hide the tooltips
  // with javascript as we cannot sifr 
  // text unless its visible so need hook
  pg.find('div.featured-image').hover(
    function() {
      $(this).data('hover', true);
      lwd__sifr_tooltips();
    },    
    function() {
      $(this).data('hover', false);
      var tip = $(this).find('div.lwd-tooltip');
      tip.hide();
    }
  );
  
  // enable the product link to use contact form with name
  pg.find('div.lwd-tooltip div.link').each(function(i, el) {
    $(el).data('product-name', $(el).find('span.product-name').text());
  });
  pg.find('div.lwd-tooltip div.link').data('click', function(name) {
    var f = $('form.lwd-product-enquiry');
	  f.find('input[name=product_name]').val(name);
	  f.get(0).submit();
  });
	pg.find('div.lwd-tooltip div.link .lwd-arrow-right').click(function(e) {
	  var fn = $(this).parent().data('click');
	  if (fn) fn($(this).parent().data('product-name'));
	});
  
  
  
  /**************************/
  /* Slide Show (Home Page) */
  /**************************/
  
  // create a shortcut page reference
  var pg = $('div#lwd-content.home');  
  if (pg.size())
  {
    // time to wait between slides
    var lwd__delay = 5; // seconds
    
    // retrieve all the slides from the home page content
    var lwd__slides = pg.find('div.slideshow div.slide').get();
    var lwd__current = 0;
    
    // determine of current slide by locating the first visible slide within the array and saving the offset
    for (var i=0; i<lwd__slides.length; i++) if (!($(lwd__slides[i]).hasClass('lwd-hidden'))) lwd__current = i;
    
    // define a global rotation function
    // which can be schedule with timeout
    lwd__rotate_slides = function() {
      
      // determine the next index using a modulus operator
      lwd__next = ((lwd__current + 1) % lwd__slides.length);
      var c = $(lwd__slides[lwd__current]);
      var n = $(lwd__slides[lwd__next]);
      
      // begin the animation
      // of current and next
      c.fadeOut(3000);
      n.fadeIn(3000);
      
      // update our current offset
      lwd__current = lwd__next;
      
      // ensure that the next slide
      // is visible when it fades in
      n.removeClass('lwd-hidden');
      
      // call this function to ensure
      // that the next slide has its
      // tooltips properly sifred
      lwd__sifr_tooltips();
      
      // schedule the next rotation of the slide show images
      setTimeout('lwd__rotate_slides()', lwd__delay * 1000);
    };
    
    // schedule the first rotation of the slide show images
    setTimeout('lwd__rotate_slides()', lwd__delay * 1000);
  }
  
 
   
  /********/
  /* sIFR */
  /********/
  
  sIFR.replace(gothic, { selector: 'div#lwd-header div.navigation .link', css: ['.sIFR-root { color: #361F0D; }', 'a { color: #361F0D; text-decoration: none; }', 'a:hover { color: #972000; }'], wmode: 'transparent' });
  sIFR.replace(gothic, { selector: 'div#lwd-header div.navigation .divider', css: ['.sIFR-root { color: #361F0D; text-align: center; }'], wmode: 'transparent' });
  sIFR.replace(gothic, { selector: 'div#lwd-content.home div.content div.heading div.tagline', css: ['.sIFR-root { color: #EECAAF; letter-spacing: 3.5; }'], wmode: 'transparent' });
  sIFR.replace(gothic, { selector: 'div#lwd-content.home div.content div.headlines div.summary', css: ['.sIFR-root { color: #EECAAF; leading: 4.2; letter-spacing: 1.0; font-size: 8px; }', 'a { color: #F48F44; text-decoration: none; }', 'a:hover { color: #F48F44; text-decoration: underline; }'], wmode: 'transparent' });
  sIFR.replace(gothic, { selector: 'div#lwd-content.about div.content p.biography', css: ['.sIFR-root { color: #EECAAF; leading: 4.6; letter-spacing: 1.0; font-size: 8px; }'], wmode: 'transparent' });
  sIFR.replace(gothic, { selector: 'div#lwd-content.about div.content div.designs-links div.text-link', css: ['.sIFR-root { letter-spacing: 1.0; font-size: 8px; }', 'a { color: #F48F44; text-decoration: none; }', 'a:hover { color: #F48F44; text-decoration: underline; }'], wmode: 'transparent' });
  sIFR.replace(gothic, { selector: 'div#lwd-content.contact div.content p.contact-information', css: ['.sIFR-root { color: #EECAAF; leading: 9.2; letter-spacing: 1.0; font-size: 8px; }', 'a { color: #F48F44; text-decoration: none; }', 'a:hover { color: #F48F44; text-decoration: underline; }'], wmode: 'transparent' });
  sIFR.replace(gothic, { selector: 'div#lwd-content.contact div.content p.stockists-link', css: ['.sIFR-root { color: #EECAAF; leading: 9.2; letter-spacing: 1.0; font-size: 8px; }', 'a { color: #F48F44; text-decoration: none; }', 'a:hover { color: #F48F44; text-decoration: underline; }'], wmode: 'transparent', onRelease: function (fi) { $('#lwd-footer a.stockists').click(); }});
  sIFR.replace(gothic, { selector: 'div#lwd-content.contact div.content div.contact-form div.label', css: ['.sIFR-root { color: #EECAAF; letter-spacing: 1.0; font-size: 8px; }'], wmode: 'transparent' });
  sIFR.replace(gothic, { selector: 'div#lwd-content.contact div.content div.contact-form div.add-to-list div.box-label', css: ['.sIFR-root { color: #EECAAF; letter-spacing: 1.0; font-size: 7px; }'], wmode: 'transparent' });
  sIFR.replace(gothic, { selector: 'div#lwd-content.contact div.content div.contact-form div.send div.text-link', css: ['.sIFR-root { letter-spacing: 1.0; font-size: 8px; }', 'a { color: #F48F44; text-decoration: none; }', 'a:hover { color: #F48F44; text-decoration: underline; }'], wmode: 'transparent', onRelease: function (fi) { var fn = $(fi.getAncestor()).parent().data('click'); if (fn) fn(); }});
  sIFR.replace(gothicb, { selector: 'div#lwd-content.designs div.categories p.name', css: ['.sIFR-root { color: #361F0D; letter-spacing: 3.4; text-align: center; }', 'a { color: #361F0D; text-decoration: none; }', 'a:hover { color: #972000; text-decoration: none; }'], wmode: 'transparent' });
  sIFR.replace(gothic, { selector: 'div#lwd-content.products div.coming-soon div', css: ['.sIFR-root { color: #361F0D; letter-spacing: 0.5; text-align: center; }'], wmode: 'transparent' });
  sIFR.replace(gothicb, { selector: 'div#lwd-content.products div.product-previews p.caption', css: ['.sIFR-root { color: #361F0D; letter-spacing: 0.5; font-size: 9px; }', 'a { color: #361F0D; text-decoration: none; }', 'a:hover { color: #361F0D; text-decoration: underline; }'], wmode: 'transparent' });

});

