﻿/*!\brief This page includes all the master page related javascript.
 * \details This page includes all the master page related javascript and jquery scripts.
 */

function hideMenu() {
  $('#ctl00_pnlLeftMenu').hide();
  $('#content-main').css('width', '100%');
}

function showMenu() {
  $('#ctl00_pnlLeftMenu').show();
  $('#content-main').css('width', '790px');
}

function pageLoad(sender, args) {
  //jquery components need to be reinitialized on updatepanel refreshes
  //This will check for existance of those controls update functions and call them
  //only on partial loads, which is what occures on update panel refreshes.
  if (args.get_isPartialLoad()) {
    if (typeof product_display_jquery_register == 'function') {
      product_display_jquery_register();
    }
    if (typeof drilldown_jquery_register == 'function') {
      drilldown_jquery_register();
    }
    $(document).ready(function() {
      $('a.basic').cluetip();
      $('#houdini').cluetip({
        splitTitle: '|', // use the invoking element's title attribute to populate the clueTip...
        // ...and split the contents into separate divs where there is a "|"
        showTitle: false, // hide the clueTip's heading       
        waitImage: false
      });
    });
    $(document).trigger('hideCluetip');
    Shadowbox.setup();
  }
}

function ClearSearch(obj) {
  if (obj.value == "Search keywords, brand, or model #") {
    obj.value = "";
  }
}

function ResetSearch(obj) {
  if (obj.value == "") {
    obj.value = "Search keywords, brand, or model #";
  }
}

function CloseAllMenus() {
  if (document.getElementById('dd-sunglasses')) {
    document.getElementById('dd-sunglasses').style.display = 'none';
    document.getElementById('dd-eyeglasses').style.display = 'none';
    document.getElementById('dd-rxsunglasses').style.display = 'none';
    document.getElementById('dd-contacts').style.display = 'none';
  }
}

function MegaMenuOn(menu) {
  document.getElementById('dd-sunglasses').style.display = 'none';
  document.getElementById('dd-eyeglasses').style.display = 'none';
  document.getElementById('dd-rxsunglasses').style.display = 'none';
  document.getElementById('dd-contacts').style.display = 'none';
  document.getElementById(menu).style.display = 'block';
}

function CloseMegaMenu(event, element) {
  var current_mouse_target = null;
  if (event.toElement) {
    current_mouse_target = event.toElement;
  }
  else if (event.relatedTarget) {
    current_mouse_target = event.relatedTarget;
  }
  if (!is_child_of(element, current_mouse_target) && element != current_mouse_target) {
    element.style.display = 'none';
    if (element.id == 'dd-eyeglasses') {
      if (document.getElementById('mt-item-eyeglasses-selected')) {
        document.getElementById('mt-item-eyeglasses-selected').setAttribute('id', 'mt-item-eyeglasses')
      }
    }
    else if (element.id == 'dd-sunglasses') {
      if (document.getElementById('mt-item-sunglasses-selected')) {
        document.getElementById('mt-item-sunglasses-selected').setAttribute('id', 'mt-item-sunglasses')
      }
    }
    else if (element.id == 'dd-rxsunglasses') {
      if (document.getElementById('mt-item-rxsunglasses-selected')) {
        document.getElementById('mt-item-rxsunglasses-selected').setAttribute('id', 'mt-item-rxsunglasses')
      }
    }
    else if (element.id == 'dd-contacts') {
      if (document.getElementById('mt-item-contactlenses-selected')) {
        document.getElementById('mt-item-contactlenses-selected').setAttribute('id', 'mt-item-contactlenses')
      }
    }
  }
}

function LockTopMenu(id) {
  if (document.getElementById(id)) {
      document.getElementById(id).setAttribute('id', id + '-selected');
  }
}

function is_child_of(parent, child) {
  if (child != null) {
    while (child.parentNode) {
      if ((child = child.parentNode) == parent) {
        return true;
      }
    }
  }
  return false;
}

function IsBlankSearch() {
  if ((document.getElementById("ctl00_txtSearch").value == "Search keywords, brand, or model #") || (document.getElementById("ctl00_txtSearch").value == "")) {
    alert('Search keywords, brand, or model #');
    document.getElementById("ctl00_txtSearch").value = "";
    document.getElementById("ctl00_txtSearch").focus();
    return false;
  }
  else {
    return true;
  }
}

function LiveChat() {
  var bReturn = true;
  var dDateTime = new Date();
  var iHour = dDateTime.getHours();
  var iMinute = dDateTime.getMinutes();
  var iSecond = dDateTime.getSeconds();
  var iDay = dDateTime.getDay();

  if (iDay > 0 && iDay < 6) {
    if ((iHour >= 8) && ((iHour < 17) || (iHour == 17 && iMinute == 0 && iSecond == 0))) {
      bReturn = false;
      window.open('http://livechat.framesdirect.com/SightMaxAgentInterface/PreChatSurvey.aspx?accountID=1&siteID=1&queueID=2&AllQueues=yes', 'chatWindow', 'width=490,height=404,resizable=0,scrollbars=no,menubar=no,status=no');
    }
  }
  return bReturn;
}

/* JavaScript for Tabs
*  Function to hide & show div.
*/
function func_show_active(sActiveDivId, sDeactiveDiv2Id, sDeactiveDiv3Id, sDeactiveDiv4Id, sLtrlInfo, sPageTitle) {
  document.getElementById(sActiveDivId).style.display = "block";
  document.getElementById(sDeactiveDiv2Id).style.display = "none";
  document.getElementById(sDeactiveDiv3Id).style.display = "none";
  document.getElementById(sDeactiveDiv4Id).style.display = "none";
  document.getElementById(sLtrlInfo).innerHTML = sPageTitle;
  document.title = sPageTitle;
  return false;
}

function func_showhide_tab_contact_lenses(iTabId) {
  var objTabLinks1 = document.getElementById("tab_links_1");
  var objTabLinks2 = document.getElementById("tab_links_2");
  var objTabLinks3 = document.getElementById("tab_links_3");

  if (objTabLinks1 && objTabLinks2 && objTabLinks3) {
    objTabLinks1.style.display = (iTabId == 1 ? "block" : "none");
    objTabLinks2.style.display = (iTabId == 2 ? "block" : "none");
    objTabLinks3.style.display = (iTabId == 3 ? "block" : "none");
    document.getElementById("all_contact_lenses").innerHTML = (iTabId == 1 ? "<img src=/images/global/contact-lenses/all_contacts_active.jpg border=0 width=198 height=28>" : "<img src=/images/global/contact-lenses/all_contacts_dactive.jpg border=0 width=198 height=23>");
    document.getElementById("best").innerHTML = (iTabId == 2 ? "<img src=/images/global/contact-lenses/best-sellers-active.jpg border=0 width=145 height=28>" : "<img src=/images/global/contact-lenses/best_sellers_dactive.jpg border=0 width=145 height=23>");
    document.getElementById("specialty_collections").innerHTML = (iTabId == 3 ? "<img src=/images/global/contact-lenses/special_collections_active.jpg border=0 width=198 height=28>" : "<img src=/images/global/contact-lenses/special_collections_dactive.jpg border=0 width=198 height=23>");
  }
  return false;
}

function func_show_hide_parent(parentId) {
  parent_div = document.getElementById("menu" + parentId)

  if (parent_div.style.display == "none") {
    parent_div.style.display = "block";
  }
  else {
    parent_div.style.display = "none";
  }
}

/* From Eyewear Deals JS */
function func_showhide_tab(iTabId) {

  var numTabs = $('.btn_blue li').size();

  for (i=1;i<=numTabs;i++) {
    var objTabLinks = new Array();
    objTabLinks[i] = document.getElementById("tab_links_" + i);
    var whichTab = objTabLinks[i];                                                      
    whichTab.style.display = (iTabId == i ? "block" : "none");
  }
  return false;
}

/* From popup.master so it's available without that wrapper */
function closeWindow() {
  if (window.opener) {
    window.close();
  }
  else {
    parent.window.Shadowbox.close();
  }
}

//search issue for 404 pg
function fn_hide_search() {
  if (document.getElementById("ctl00_hdnErrorPage").value == "true") {
    document.getElementById("search-box").style.display = "none";
  }
}
window.onload = function () { fn_hide_search() };

/*
*	easyListSplitter 1.0.2 - jQuery Plugin
*	written by Andrea Cima Serniotti	
*	http://www.madeincima.eu
*
*	Copyright (c) 2010 Andrea Cima Serniotti (http://www.madeincima.eu)
*	Dual licensed under the MIT (MIT-LICENSE.txt)
*	and GPL (GPL-LICENSE.txt) licenses.
*
*	Built for jQuery library
*	http://jquery.com
*
*/

/*
To activate the plugin add the following code to your own js file:
	
$('.your-list-class-name').easyListSplitter({ 
colNumber: 3,
direction: 'horizontal'
});
	
*/

var j = 1;

(function (jQuery) {
  jQuery.fn.easyListSplitter = function (options) {

    var defaults = {
      colNumber: 2, // Insert here the number of columns you want. Consider that the plugin will create the number of cols requested only if there are enough items in the list.
      direction: 'vertical'
    };

    this.each(function () {

      var obj = jQuery(this);
      var settings = jQuery.extend(defaults, options);
      var totalListElements = jQuery(this).children('li').size();
      var baseColItems = Math.ceil(totalListElements / settings.colNumber);
      var listClass = jQuery(this).attr('class');

      // -------- Create List Elements given colNumber ------------------------------------------------------------------------------

      for (i = 1; i <= settings.colNumber; i++) {
        if (i == 1) {
          jQuery(this).addClass('listCol1').wrap('<div class="listContainer' + j + '"></div>');
        } else if (jQuery(this).is('ul')) { // Check whether the list is ordered or unordered
          jQuery(this).parents('.listContainer' + j).append('<ul class="listCol' + i + '"></ul>');
        } else {
          jQuery(this).parents('.listContainer' + j).append('<ol class="listCol' + i + '"></ol>');
        }
        jQuery('.listContainer' + j + ' > ul,.listContainer' + j + ' > ol').addClass(listClass);
      }

      var listItem = 0;
      var k = 1;
      var l = 0;

      if (settings.direction == 'vertical') { // -------- Append List Elements to the respective listCol  - Vertical -------------------------------

        jQuery(this).children('li').each(function () {
          listItem = listItem + 1;
          if (listItem > baseColItems * (settings.colNumber - 1)) {
            jQuery(this).parents('.listContainer' + j).find('.listCol' + settings.colNumber).append(this);
          }
          else {
            if (listItem <= (baseColItems * k)) {
              jQuery(this).parents('.listContainer' + j).find('.listCol' + k).append(this);
            }
            else {
              jQuery(this).parents('.listContainer' + j).find('.listCol' + (k + 1)).append(this);
              k = k + 1;
            }
          }
        });

        jQuery('.listContainer' + j).find('ol,ul').each(function () {
          if (jQuery(this).children().size() == 0) {
            jQuery(this).remove();
          }
        });

      } else {  // -------- Append List Elements to the respective listCol  - Horizontal ----------------------------------------------------------

        jQuery(this).children('li').each(function () {
          l = l + 1;

          if (l <= settings.colNumber) {
            jQuery(this).parents('.listContainer' + j).find('.listCol' + l).append(this);

          } else {
            l = 1;
            jQuery(this).parents('.listContainer' + j).find('.listCol' + l).append(this);
          }
        });
      }

      jQuery('.listContainer' + j).find('ol:last,ul:last').addClass('last'); // Set class last on the last UL or OL	
      j = j + 1;

    });
  };
})(jQuery);

function loadTwitter() {
  $('span.sm-tw').html('<a href="http://twitter.com/share" class="twitter-share-button" data-count="none">Tweet</a><script src="http://platform.twitter.com/widgets.js"></script>');
}

$(document).ready(function () {
  $("#accordion > div").hide();
  //toggle the componenet with class msg_body
  var pathname = window.location.pathname;
  if (pathname == '/' || pathname == '/default2.aspx') {
    for (i = 0; i < 4; i++) {
      $("#accordion > div:eq(" + i + ")").slideToggle(300);
      $("#accordion > div:eq(" + i + ")").prev('h3').toggleClass('activeMenu');
    }
  }

  $("#accordion h3").click(function () {
    $(this).toggleClass('activeMenu');
    $(this).next("#accordion > div").slideToggle(300);
    return false;
  });

  $('a.basic').cluetip({
    showTitle: false
  });

  oHandler = $("#ctl00_wuc_currency_ddlCurrencyList").msDropDown().data("dd");

  $('.blurb ul').innerfade({
    speed: 1000,
    timeout: 9000,
    type: 'sequence',
    containerheight: '200px'
  });

  $('#prod-search-text').html($('#ctl00_txtSearch').val());


  $(".btn_blue li a").click(function () {
    $(".btn_blue li").removeClass('tab-active');
    $(this).parent('li').addClass('tab-active');
  });
  var pathname = window.location.pathname;
  if ((pathname == "/eyeglasses/") || (pathname == "/sunglasses/") || (pathname == "/prescription-sunglasses/")) {
    $(".btn_blue li:nth-child(2)").addClass('tab-active');
  } else {
    $(".btn_blue li:nth-child(1)").addClass('tab-active');
  }

  /* Ellipsis */
  $.fn.ellipsis = function () {
    return this.each(function () {
      var el = $(this);

      if (el.css("overflow") == "hidden") {
        var text = el.html();
        var multiline = el.hasClass('multiline');
        var t = $(this.cloneNode(true))
				.hide()
				.css('position', 'absolute')
				.css('overflow', 'visible')
				.width(multiline ? el.width() : 'auto')
				.height(multiline ? 'auto' : el.height());

        el.after(t);

        function height() { return t.height() > el.height(); };
        function width() { return t.width() > el.width(); };

        var func = multiline ? height : width;

        while (text.length > 0 && func()) {
          text = text.substr(0, text.length - 1);
          t.html(text + "...");
        }

        el.html(t.html());
        t.remove();
      }
    });
  };

  //run ellipsis code for each item
  var ie = (function () {
    var undef,
	  v = 3,
    div = document.createElement('div'),
    all = div.getElementsByTagName('i');

    while (
	  div.innerHTML = '<!--[if gt IE ' + (++v) + ']><i></i><![endif]-->',
	  all[0]
	);

    return v > 4 ? v : undef;

  } ());

  if (!ie) {
    $("#breadcrumb-container").ellipsis();
    $("#current-results-container").ellipsis();
    $("#product-model-list").ellipsis();
  }

  Shadowbox.init({
    animate: false,
    animateFade: false
  });

  //print class to support breaks properly in IE
  $('.prod-area .prod-row:nth-child(4)').addClass('page-break');
});
