﻿Cufon.replace(".search-right .description");
Cufon.replace(".search-right .title");
Cufon.replace("h1");
Cufon.replace("h2");
Cufon.replace("h3");

$(function () {
  var processingGifLocation = '../Layout/Images/processing.gif';

  //SDS Links need to display a warnnning
  $(".sdsLink").click(function (e) {
    var link = $(this).attr('href');
    e.preventDefault();

    if (confirm('This will open the Safety Data Sheet in a new window.\r\nPress OK to continue.')) {
      window.open(link);
    } 
  });
  
  var preloadedProcessingGif = $('<img />').attr('src', processingGifLocation );
  preloadedProcessingGif.load( function(){
    $(this).css({ display: 'none'});
  });
  
  $('body').append(preloadedProcessingGif);
  
  $('form input[type=submit], form input[type=image]').click(function(){
    if (typeof (ValidatorOnSubmit) == 'function' && ValidatorOnSubmit() == false) {
          return;
    }
    
    var currentParent = $(this).parent();
    
    if( $(currentParent).hasClass('search-product-container') || $(currentParent).hasClass('search-school-container') ){
      return;
    }
  
    $(this)
      .hide()
      .after('<span class="noteText processingText"><img class="proccessImg" src="" alt=""/>&nbsp;Processing</span>');
      
    $(this)
      .parent()
      .find('.proccessImg')
      .attr('src',processingGifLocation)
      .css({visibility: 'visible'});
  });


  //auto-complete search box
  $(".search-school").attr({ value: 'Search for your School' }).focus(function () {
    if ($(this).val() == "Search for your School") {
      $(this).val("");
    }
  }).blur(function () {
    if ($(this).val() == "") {
      $(this).val("Search for your School");
    }
  });

  $(".search-product").attr({ value: 'Search for Products' }).focus(function () {
    if ($(this).val() == "Search for Products") {
      $(this).val("");
    }
  }).blur(function () {
    if ($(this).val() == "") {
      $(this).val("Search for Products");
    }
  });

  $(".search-school").autocomplete({
    source: function (request, response) {
      $.ajax({
        url: "/PublishedSchools.asmx/GetSchools",
        data: "{ 'term': '" + request.term.replace(/\'/g, "\\'") + "' }",
        dataType: "json",
        type: "POST",
        contentType: "application/json; charset=utf-8",
        dataFilter: function (data) { return data; },
        success: function (data) {
          response($.map(data.d, function (item) {
            return {
              value: item
            }
          }))
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
          alert(textStatus);
        }
      });
    },
    minLength: 2,
    select: function (event, ui) { window.location.href = "/MySchool/SearchResults.aspx?school=" + ui.item.value; }
  });

  //cart popup
  $(".show-cart").click(function () {
    var source = $(".arrow-down").attr("src");
    if ($(".header-cart-popup").is(":hidden")) {
      $(".arrow-down").attr("src", source.replace("-down", "-up"));
    }
    else {
      $(".arrow-down").attr("src", source.replace("-up", "-down"));
    }
    $(".header-cart-popup").fadeSliderToggle("slow");
    return false;
  });

  $(".close-cart").click(function () {
    $(".header-cart-popup").animate({ opacity: "hide" }, "slow");
  });

  $(".left-cart").click(function () {
    window.location.href = "/MySchool/ShoppingCart.aspx";
  });

  //recommended items
  $('.RecommendedItems')
                .find('.List')
                    .hide().end()
                .find('.Show')
                    .click(function (event) {
                      event.preventDefault();
                      $(this).next().slideToggle();
                    });

  //misc
  $(".bodyLink")
    .mouseover(function () {
      $(this).attr("class", "bodyLinkOver");
    })
    .mouseout(function () {
      $(this).attr("class", "bodyLink");
    });

  //block ui for fees payment page
  $('.processPayment').click(function () {
    BlockUI();
  });

  //delivery date radiobutton list
  if (document.getElementById('ctl00_MainContent_valCompanyName') == undefined)
    return;
  $(".companyRequired").hide();
  document.getElementById('ctl00_MainContent_valCompanyName').enabled = false;

  var $radBtn = $(".deliveryType input:radio");
  $radBtn.click(function () {
    var $radChecked = $('.deliveryType input:radio:checked');
    if ($radChecked.val() == undefined)
      return;

    if ($radChecked.val() == "1") {
      $(".companyRequired").show();
      document.getElementById('ctl00_MainContent_valCompanyName').enabled = true;
    }
    else {
      $(".companyRequired").hide();
      document.getElementById('ctl00_MainContent_valCompanyName').enabled = false;
    }
  });

});

function confirmFees() {
    var answer = confirm('Are you sure that you do not want to pay the fees?');
    if (answer) {
        BlockUI();
        return true;
    }
    else {
        return false;
    }
}

function BlockUI() {
    $.blockUI({ css: {
        border: 'none',
        padding: '15px',
        backgroundColor: '#000',
        '-webkit-border-radius': '10px',
        '-moz-border-radius': '10px',
        opacity: .5,
        color: '#fff'
    }
    });
}

// JS Dropdown Menu
var timeout = 500;
var closetimer = 0;
var ddmenuitem = 0;

function jsddm_open() {
    jsddm_canceltimer();
    jsddm_close();
    ddmenuitem = $(this).find('ul').css('visibility', 'visible');
}

function jsddm_close()
{ if (ddmenuitem) ddmenuitem.css('visibility', 'hidden'); }

function jsddm_timer()
{ closetimer = window.setTimeout(jsddm_close, timeout); }

function jsddm_canceltimer() {
    if (closetimer) {
        window.clearTimeout(closetimer);
        closetimer = null;
    } 
}

$(document).ready(function () {
    $('#jsddm > li').bind('mouseover', jsddm_open)
    $('#jsddm > li').bind('mouseout', jsddm_timer)
});

document.onclick = jsddm_close;
